Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
KeyboardMappingScreen.cs
浏览该文件的文档.
1using System.Xml.Linq;
2using Engine;
3using Engine.Input;
5
6namespace Game {
8 public Widget KeyInfoWidget(object item) {
9 XElement node = ContentManager.Get<XElement>("Widgets/KeyboardMappingItem");
10 node.SetAttributeValue("Name", $"KeyboardMappingItem_{item}");
11 ContainerWidget containerWidget = (ContainerWidget)LoadWidget(this, node, null);
12 LabelWidget labelWidget = containerWidget.Children.Find<LabelWidget>("Name");
13 LabelWidget labelWidget2 = containerWidget.Children.Find<LabelWidget>("BoundKey");
14 string itemString = item.ToString();
15 if (itemString != null) {
16 string translated = LanguageControl.Get(out bool r, fName, itemString);
17 labelWidget.Text = r ? translated : itemString;
19 m_widgetsByString[itemString] = containerWidget;
20 }
21 return containerWidget;
22 }
23
24 public const string fName = "KeyboardMappingScreen";
25 public const string keyName = "KeyboardMappingScreenKeys";
26
33 public Dictionary<string, ContainerWidget> m_widgetsByString = new();
34 public Dictionary<object, List<string>> m_conflicts = new();
35
37 XElement node = ContentManager.Get<XElement>("Screens/KeyboardMappingScreen");
38 LoadContents(this, node);
39 m_keysList = Children.Find<ListPanelWidget>("KeysList");
40 m_keysList.ItemWidgetFactory = (Func<object, Widget>)Delegate.Combine(m_keysList.ItemWidgetFactory, KeyInfoWidget);
41 m_keysList.ScrollPosition = 0f;
42 m_keysList.ScrollSpeed = 0f;
43 m_keysList.ItemClicked += item => { m_keysList.SelectedItem = m_keysList.SelectedItem == item ? null : item; };
48 }
49
50 public override void Update() {
51 string selectedKeyName = m_keysList.SelectedItem?.ToString() ?? string.Empty;
52 m_setKeyButton.IsEnabled = !string.IsNullOrEmpty(selectedKeyName);
53 m_disableKeyButton.IsEnabled = !string.IsNullOrEmpty(selectedKeyName);
54 if (Children.Find<ButtonWidget>("TopBar.Back").IsClicked) {
56 return;
57 }
58 foreach (string key in m_widgetsByString.Keys) {
59 LabelWidget labelWidget = m_widgetsByString[key].Children.Find<LabelWidget>("BoundKey");
60 object value = SettingsManager.GetKeyboardMapping(key);
61 if (value is Key valueKey
62 && valueKey == Key.Null) {
63 labelWidget.Text = string.Empty;
64 }
65 else {
66 string text = HumanReadableConverter.ConvertToString(value);
67 string translated = LanguageControl.Get(out bool r, keyName, text);
68 labelWidget.Text = r ? translated : text;
69 bool hasConflict = false;
70 if (m_conflicts.TryGetValue(value, out List<string> valueList)) {
71 hasConflict = KeyCompatibleGroupsManager.HasConflict(valueList);
72 }
73 labelWidget.Color = hasConflict ? Color.Red : Color.White;
74 }
75 }
76 if (m_disableKeyButton.IsClicked) {
77 SetKeyboardMapping(selectedKeyName, Key.Null);
79 }
80 if (m_resetButton.IsClicked) {
81 MessageDialog dialog = new(
82 LanguageControl.Get("ContentWidgets", fName, "ResetTitle"),
83 LanguageControl.Get("ContentWidgets", fName, "ResetText"),
86 delegate(MessageDialogButton button) {
87 if (button == MessageDialogButton.Button1) { //重设所有按键
88 ResetAll();
89 }
90 }
91 );
92 DialogsManager.ShowDialog(null, dialog);
94 }
96 m_setKeyButton.IsChecked = true;
97 if (Input.Back
98 || Input.Cancel) {
100 return;
101 }
102 foreach (Key key in EnumUtils.GetEnumValues<Key>()) {
103 if (key != Key.Null
104 && Input.IsKeyDown(key)) {
105 SetKeyboardMapping(selectedKeyName, key);
106 IsWaitingForKeyInput = false;
107 return;
108 }
109 }
110 foreach (MouseButton mouseButton in EnumUtils.GetEnumValues<MouseButton>()) {
111 if (Input.IsMouseButtonDown(mouseButton)) {
112 SetKeyboardMapping(selectedKeyName, mouseButton);
113 IsWaitingForKeyInput = false;
114 return;
115 }
116 }
117 }
118 else {
119 m_setKeyButton.IsChecked = false;
120 }
121 if (m_setKeyButton.IsClicked) {
123 }
124 if (m_gameHelpButton.IsClicked) {
126 }
128 && (Input.Back || Input.Cancel)) {
130 }
131 }
132
133 public override void Enter(object[] parameters) {
134 m_gameHelpButton.IsVisible = ScreensManager.PreviousScreen is GameScreen;
135 m_keysList.ClearItems();
136 foreach (string keyName1 in ModSettingsManager.CombinedKeyboardMappingSettings.Keys) {
137 m_keysList.AddItem(keyName1);
138 }
140 }
141
142 public void SetKeyboardMapping(string keyName1, object value) {
143 SettingsManager.SetKeyboardMapping(keyName1, value);
145 }
146
152
153 public void RefreshConflicts() {
154 m_conflicts.Clear();
155 foreach (KeyValuePair<string, object> item in ModSettingsManager.CombinedKeyboardMappingSettings) {
156 string name = item.Key;
157 object obj = item.Value;
158 if (!m_conflicts.TryGetValue(obj, out List<string> value)) {
159 value = new List<string>();
160 m_conflicts[obj] = value;
161 }
162 if (!value.Contains(name)) {
163 value.Add(name);
164 }
165 }
166 }
167 }
168}
readonly WidgetsList Children
static object Get(Type type, string name)
static void ShowDialog(ContainerWidget parentWidget, Dialog dialog)
static IList< int > GetEnumValues(Type type)
static bool HasConflict(List< string > list)
输入按键名称列表,检查是否存在冲突
Dictionary< object, List< string > > m_conflicts
Dictionary< string, ContainerWidget > m_widgetsByString
override void Enter(object[] parameters)
void SetKeyboardMapping(string keyName1, object value)
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
static Dictionary< string, object > CombinedKeyboardMappingSettings
static Screen PreviousScreen
上一个Screen
static void GoBack(params object[] parameters)
static void SwitchScreen(string name, params object[] parameters)
static object GetKeyboardMapping(string keyName, bool throwIfNotFound=true)
static void SetKeyboardMapping(string keyName, object value)
仅用于修改现有键盘鼠标键位,添加键位请使用ModLoader.GetKeyboardMappings
static void InitializeKeyboardMappingSettings()
static Widget LoadWidget(object eventsTarget, XElement node, ContainerWidget parentWidget)
virtual void LoadContents(object eventsTarget, XElement node)
Widget Find(string name, Type type, bool throwIfNotFound=true)
static Color White