Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
GamepadMappingScreen.cs
浏览该文件的文档.
1using System.Xml.Linq;
2using Engine;
3using Engine.Input;
6
7namespace Game {
8 public class GamepadMappingScreen : Screen {
9 public Widget GamepadKeyInfoWidget(object item) {
10 XElement node = ContentManager.Get<XElement>("Widgets/KeyboardMappingItem");
11 node.SetAttributeValue("Name", $"GamepadMappingItem_{item}");
12 ContainerWidget containerWidget = (ContainerWidget)LoadWidget(this, node, null);
13 LabelWidget labelWidget = containerWidget.Children.Find<LabelWidget>("Name");
14 LabelWidget labelWidget2 = containerWidget.Children.Find<LabelWidget>("BoundKey");
15 string itemString = item.ToString();
16 if (itemString != null) {
17 string translated = LanguageControl.Get(out bool r, fName, itemString);
18 labelWidget.Text = r ? translated : itemString;
19 object value = SettingsManager.GetGamepadMapping(itemString);
20 if (value is GamePadButton valueKey
21 && valueKey == GamePadButton.Null) {
22 labelWidget2.Text = string.Empty;
23 }
24 else if (value is ValuesDictionary valuesDictionary) {
25 labelWidget2.Text = ConvertGamepadName(valuesDictionary.GetValue<object>("ModifierKey", null))
26 + " + "
27 + ConvertGamepadName(valuesDictionary.GetValue<object>("ActionKey", null));
28 }
29 else {
30 labelWidget2.Text = ConvertGamepadName(value);
31 }
32 m_widgetsByString[itemString] = containerWidget;
33 }
34 return containerWidget;
35 }
36
37 public static string ConvertGamepadName(object obj) {
38 if (obj == null) {
39 return string.Empty;
40 }
41 string text = HumanReadableConverter.ConvertToString(obj);
42 string translated = LanguageControl.Get(out bool r, keyName, text);
43 return r ? translated : text;
44 }
45
46 public static string fName => typeof(KeyboardMappingScreen).Name;
47 public static string keyName => "GamepadMappingScreenKeys";
48
55 public Dictionary<string, ContainerWidget> m_widgetsByString = [];
56 public Dictionary<object, List<string>> m_conflicts = [];
57
59 XElement node = ContentManager.Get<XElement>("Screens/KeyboardMappingScreen");
60 LoadContents(this, node);
61 m_keysList = Children.Find<ListPanelWidget>("KeysList");
62 m_keysList.ItemWidgetFactory = (Func<object, Widget>)Delegate.Combine(m_keysList.ItemWidgetFactory, GamepadKeyInfoWidget);
63 m_keysList.ScrollPosition = 0f;
64 m_keysList.ScrollSpeed = 0f;
65 m_keysList.ItemClicked += item => { m_keysList.SelectedItem = m_keysList.SelectedItem == item ? null : item; };
70 }
71
72 public override void Update() {
73 string selectedKeyName = m_keysList.SelectedItem?.ToString() ?? string.Empty;
74 m_setKeyButton.IsEnabled = !string.IsNullOrEmpty(selectedKeyName);
75 m_disableKeyButton.IsEnabled = !string.IsNullOrEmpty(selectedKeyName);
76 if (Children.Find<ButtonWidget>("TopBar.Back").IsClicked) {
78 return;
79 }
80 foreach (KeyValuePair<string, ContainerWidget> item in m_widgetsByString) {
81 string key = item.Key;
82 LabelWidget labelWidget = item.Value.Children.Find<LabelWidget>("BoundKey");
83 object value = SettingsManager.GetGamepadMapping(key);
84 if (value is GamePadButton valueKey
85 && valueKey == GamePadButton.Null) {
86 labelWidget.Text = string.Empty;
87 }
88 else {
89 if (value is ValuesDictionary valuesDictionary) {
90 labelWidget.Text = ConvertGamepadName(valuesDictionary.GetValue<object>("ModifierKey", null))
91 + " + "
92 + ConvertGamepadName(valuesDictionary.GetValue<object>("ActionKey", null));
93 }
94 else {
95 labelWidget.Text = ConvertGamepadName(value); // r ? translated : text;
96 }
97 bool hasConflict = false;
98 if (m_conflicts.TryGetValue(value, out List<string> valueList)) {
99 hasConflict = KeyCompatibleGroupsManager.HasConflict(valueList);
100 }
101 labelWidget.Color = hasConflict ? Color.Red : Color.White;
102 }
103 }
104 if (m_disableKeyButton.IsClicked) {
105 SetGamepadMapping(selectedKeyName, GamePadButton.Null);
106 IsWaitingForKeyInput = false;
107 }
108 if (m_resetButton.IsClicked) {
109 MessageDialog dialog = new(
110 LanguageControl.Get("ContentWidgets", fName, "ResetTitle"),
111 LanguageControl.Get("ContentWidgets", fName, "ResetText"),
114 delegate(MessageDialogButton button) {
115 if (button == MessageDialogButton.Button1) { //重设所有按键
116 ResetAll();
117 }
118 }
119 );
120 DialogsManager.ShowDialog(null, dialog);
121 IsWaitingForKeyInput = false;
122 }
124 m_setKeyButton.IsChecked = true;
125 if (Input.Back) {
126 IsWaitingForKeyInput = false;
127 return;
128 }
129 object holdingModifierKey = null;
130 if (Input.IsPadButtonDown(GamePadButton.LeftShoulder)) {
131 holdingModifierKey = GamePadButton.LeftShoulder;
132 }
133 else if (Input.IsPadButtonDown(GamePadButton.RightShoulder)) {
134 holdingModifierKey = GamePadButton.RightShoulder;
135 }
136 else if (Input.IsPadTriggerDown(GamePadTrigger.Left, 0f, SettingsManager.GamepadTriggerThreshold)) {
137 holdingModifierKey = GamePadTrigger.Left;
138 }
139 else if (Input.IsPadTriggerDown(GamePadTrigger.Right, 0f, SettingsManager.GamepadTriggerThreshold)) {
140 holdingModifierKey = GamePadTrigger.Right;
141 }
142 foreach (GamePadButton button in EnumUtils.GetEnumValues<GamePadButton>().Select(v => (GamePadButton)v)) {
143 if (button != GamePadButton.Null
144 && Input.IsPadButtonDownOnce(button)) {
145 if (holdingModifierKey != null
146 && !GamePad.IsModifierKey(button)) {
147 ValuesDictionary combinedKey = [];
148 combinedKey.SetValue("ModifierKey", holdingModifierKey);
149 combinedKey.SetValue("ActionKey", button);
150 SetGamepadMapping(selectedKeyName, combinedKey);
151 }
152 else {
153 SetGamepadMapping(selectedKeyName, button);
154 }
155 IsWaitingForKeyInput = false;
156 return;
157 }
158 }
159 foreach (GamePadTrigger trigger in EnumUtils.GetEnumValues<GamePadTrigger>().Select(v => (GamePadTrigger)v)) {
160 if (Input.IsTriggerDownOnce(trigger)) {
161 SetGamepadMapping(selectedKeyName, trigger);
162 IsWaitingForKeyInput = false;
163 return;
164 }
165 }
166 }
167 else {
168 m_setKeyButton.IsChecked = false;
169 }
170 if (m_setKeyButton.IsClicked) {
172 }
173 if (m_gameHelpButton.IsClicked) {
175 }
177 && (Input.Back || Input.Cancel)) {
179 }
180 }
181
182 public override void Enter(object[] parameters) {
183 m_gameHelpButton.IsVisible = ScreensManager.PreviousScreen is GameScreen;
184 m_keysList.ClearItems();
185 foreach (string keyName1 in ModSettingsManager.CombinedGamepadMappingSettings.Keys) {
186 m_keysList.AddItem(keyName1);
187 }
189 }
190
191 public void SetGamepadMapping(string keyName1, object value) {
192 SettingsManager.SetGamepadMapping(keyName1, value);
194 }
195
201
202 public void RefreshConflicts() {
203 m_conflicts.Clear();
204 foreach (KeyValuePair<string, object> item in ModSettingsManager.CombinedGamepadMappingSettings) {
205 string name = item.Key;
206 object obj = item.Value;
207 if (!m_conflicts.TryGetValue(obj, out List<string> value)) {
208 value = [];
209 m_conflicts[obj] = value;
210 }
211 if (!value.Contains(name)) {
212 value.Add(name);
213 }
214 }
215 }
216 }
217}
static bool IsModifierKey(object obj)
readonly WidgetsList Children
static object Get(Type type, string name)
static void ShowDialog(ContainerWidget parentWidget, Dialog dialog)
static IList< int > GetEnumValues(Type type)
void SetGamepadMapping(string keyName1, object value)
Dictionary< object, List< string > > m_conflicts
static string ConvertGamepadName(object obj)
override void Enter(object[] parameters)
Dictionary< string, ContainerWidget > m_widgetsByString
static bool HasConflict(List< string > list)
输入按键名称列表,检查是否存在冲突
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
static Dictionary< string, object > CombinedGamepadMappingSettings
static Screen PreviousScreen
上一个Screen
static void GoBack(params object[] parameters)
static void SwitchScreen(string name, params object[] parameters)
static float GamepadTriggerThreshold
手柄扳机触发阈值,范围0~1,默认0.5。扳机的按压幅度只有超过这个数时才会被视为“按下”状态,越小则越容易触发。
static object GetGamepadMapping(string keyName, bool throwIfNotFound=true)
static void InitializeGamepadMappingSettings()
static void SetGamepadMapping(string keyName, object value)
仅用于修改现有手柄键位,添加键位请使用ModLoader.GetGamepadMappings
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