Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_SettingsManagerKeybindModule.c
Go to the documentation of this file.
1 //------------------------------------------------------------------------------------------------
3 {
4  protected const string GAMEPAD_PRESET_PREFIX = "gamepad:";
5  protected const string PRIMARY_PRESET_PREFIX = "";
6 
7  protected const string DEVICE_KEYBOARD = "keyboard";
8  protected const string DEVICE_GAMEPAD = "gamepad";
9 
10  protected ref InputBinding m_Binding;
11 
12  protected ref SCR_KeyBindingMenuConfig m_KeybindConfig;
13 
14  protected static const string KEY_BINDING_CONFIG = "{4EE7794C9A3F11EF}Configs/System/keyBindingMenu.conf";
15 
16  //------------------------------------------------------------------------------------------------
18  {
19  //load keybinding confis
20  Resource holder = BaseContainerTools.LoadContainer(KEY_BINDING_CONFIG);
21  if (!holder)
22  {
23  Print("SCR_SettingsManagerKeybindModule: Loading of keybinding config failed!", LogLevel.WARNING);
24  return;
25  }
26 
27  BaseContainer container = holder.GetResource().ToBaseContainer();
28  if (!container)
29  {
30  Print("SCR_SettingsManagerKeybindModule: Loading of keybinding config failed!", LogLevel.WARNING);
31  return;
32  }
33 
34  m_KeybindConfig = SCR_KeyBindingMenuConfig.Cast(BaseContainerTools.CreateInstanceFromContainer(container));
35  if (!m_KeybindConfig)
36  {
37  Print("SCR_SettingsManagerKeybindModule: Loading of keybinding config failed!", LogLevel.WARNING);
38  return;
39  }
40 
41  SetModuleType(ESettingManagerModuleType.SETTINGS_MANAGER_KEYBINDING);
42  m_Binding = GetGame().GetInputManager().CreateUserBinding();
43  if (!m_Binding)
44  {
45  Print("SCR_SettingsManagerKeybindModule: InputBindings were not created!", LogLevel.WARNING);
46  return;
47  }
48  }
49 
50  //------------------------------------------------------------------------------------------------
51  int GetActionBindCount(string actionName, string actionPreset = string.Empty, EInputDeviceType device = EInputDeviceType.KEYBOARD)
52  {
53  if (!m_Binding)
54  return 0;
55 
56  return m_Binding.GetBindingsCount(actionName, device, actionPreset);
57  }
58 
59  //------------------------------------------------------------------------------------------------
60  InputBinding GetInputBindings()
61  {
62  return m_Binding;
63  }
64 
65  //------------------------------------------------------------------------------------------------
66  void DeleteActionBindByIndex(string actionName, int keybindIndex, string actionPreset = "", EInputDeviceType device = EInputDeviceType.KEYBOARD)
67  {
68  if (!m_Binding)
69  return;
70 
71  if (m_Binding.IsDefault(actionName, device, actionPreset))
72  {
73  // if it is default binding first create user binding
74  m_Binding.CreateUserBinding(actionName, device, actionPreset);
75  m_Binding.Save();
76  }
77 
78  m_Binding.RemoveBinding(actionName, device, actionPreset, keybindIndex);
79  m_Binding.Save();
80  }
81 
82  //------------------------------------------------------------------------------------------------
83  void StartCaptureForAction(string actionName, string actionPreset, EInputDeviceType device = EInputDeviceType.KEYBOARD, bool append = false)
84  {
85  if (!m_Binding)
86  return;
87 
88  m_Binding.StartCapture(actionName, device, actionPreset, append);
89  }
90 
91  //------------------------------------------------------------------------------------------------
92  void SetFilterForActionByIndex(string actionName, string actionPreset, int filterIndex, int keybindIndex, SCR_EActionPrefixType prefixType, EInputDeviceType device = EInputDeviceType.KEYBOARD)
93  {
94  if (!m_Binding)
95  return;
96 
97  array<ref SCR_KeyBindingFilter> filters = GetFilters(prefixType);
98  if (!filters)
99  return;
100 
101  SCR_KeyBindingFilter filter = filters.Get(filterIndex);
102  if (!filter)
103  return;
104 
105  if (m_Binding.IsDefault(actionName, device, actionPreset))
106  {
107  // if it is default binding first create user binding
108  m_Binding.CreateUserBinding(actionName, device, actionPreset);
109  m_Binding.Save();
110  }
111 
112  m_Binding.SetFilter(actionName, device, actionPreset, keybindIndex, filter.m_sFilterString);
113  m_Binding.Save();
114  }
115 
116  //------------------------------------------------------------------------------------------------
117  void AddKeybindToActionByIndex(string actionName, string preset, int bindIndex, string filterName = string.Empty)
118  {
119  if (!m_Binding)
120  return;
121 
122  array<ref SCR_KeyBindingBind> binds = GetCustomBinds();
123  if (!binds)
124  return;
125 
126  SCR_KeyBindingBind bind = binds.Get(bindIndex);
127  if (!bind)
128  return;
129 
130  m_Binding.AddBinding(actionName, preset, bind.m_sBindString, filterName);
131  m_Binding.Save();
132  }
133 
134  //------------------------------------------------------------------------------------------------
135  array<ref SCR_KeyBindingFilter> GetFilters(SCR_EActionPrefixType filterType)
136  {
137  if (!m_KeybindConfig)
138  return null;
139 
140  array <ref SCR_KeyBindingFilter> foundFilters = {};
141  foreach (SCR_KeyBindingFilter filter : m_KeybindConfig.m_aInputFilters)
142  {
143  if (filter.GetFilterType() == filterType)
144  foundFilters.Insert(filter);
145  }
146 
147  return foundFilters;
148  }
149 
150  //------------------------------------------------------------------------------------------------
151  array<ref SCR_KeyBindingBind> GetCustomBinds()
152  {
153  if (!m_KeybindConfig)
154  return null;
155 
156  return m_KeybindConfig.m_aInputBinds;
157  }
158 
159  //------------------------------------------------------------------------------------------------
160  array<ref SCR_KeyBindingCombo> GetCustomComboKeys()
161  {
162  if (!m_KeybindConfig)
163  return null;
164 
165  return m_KeybindConfig.m_aComboKeys;
166  }
167 
168  //------------------------------------------------------------------------------------------------
170  bool IsActionConflicted(string actionName, notnull out array<SCR_KeyBindingEntry> confirmedConflicts, int bindIndex, string preset = string.Empty)
171  {
172  if (!m_Binding || actionName.IsEmpty())
173  return false;
174 
175  confirmedConflicts.Clear();
176 
177  array<int> indices = {};
178  array<string> conflicts = {};
179  array<string> presets = {};
180  m_Binding.GetConflicts(actionName, indices, conflicts, presets, EInputDeviceType.KEYBOARD, preset);
181 
182  if (conflicts.IsEmpty())
183  return false;
184 
185  array<SCR_KeyBindingEntry> foundActions = {};
186 
187  //compare the actions against those configures in keybinding config, and save only those, to avoid actions for UI etc
188  foreach (int index, string action : conflicts)
189  {
190  if (indices[index] != bindIndex)
191  continue;
192 
193  foreach (SCR_KeyBindingCategory category : m_KeybindConfig.m_KeyBindingCategories)
194  {
195  foreach (SCR_KeyBindingEntry entry : category.m_KeyBindingEntries)
196  {
197  if (entry.m_sActionName == "separator" || entry.m_sActionName != action || entry.m_sPreset != presets[index])
198  continue;
199 
200  foundActions.Insert(entry);
201  }
202  }
203  }
204 
205  confirmedConflicts.Copy(foundActions);
206 
207  return !foundActions.IsEmpty();
208  }
209 
210  //------------------------------------------------------------------------------------------------
211  void UnbindAction(string actionName, string preset = "", EInputDeviceType device = EInputDeviceType.KEYBOARD)
212  {
213  m_Binding.StartCapture(actionName, device, preset, false);
214  m_Binding.SaveCapture();
215  m_Binding.Save();
216  }
217 
218  //------------------------------------------------------------------------------------------------
219  void AddComboToActionByIndex(string actionName, string preset, int comboIndex, int keybindIndex, string filterName = string.Empty, int comboPosition = 0)
220  {
221  if (!m_Binding)
222  return;
223 
224  array<ref SCR_KeyBindingCombo> combos = GetCustomComboKeys();
225  if (!combos)
226  return;
227 
228  SCR_KeyBindingCombo combo = combos.Get(comboIndex);
229  if (!combo)
230  return;
231 
232  m_Binding.InsertCombo(actionName, preset, combo.m_sComboString, filterName, keybindIndex, comboPosition);
233  m_Binding.Save();
234  }
235 
236  //------------------------------------------------------------------------------------------------
237  void ResetAction(string actionName, string preset = "", EInputDeviceType device = EInputDeviceType.KEYBOARD)
238  {
239  if (!m_Binding)
240  return;
241 
242  bool reset;
243  switch (device)
244  {
245  case EInputDeviceType.KEYBOARD:
246  case EInputDeviceType.MOUSE:
247  {
248  if (!m_Binding.IsDefault(actionName, EInputDeviceType.KEYBOARD, preset))
249  {
250  m_Binding.ResetDefault(actionName, EInputDeviceType.KEYBOARD, preset);
251  reset = true;
252  }
253 
254  if (!m_Binding.IsDefault(actionName, EInputDeviceType.MOUSE, preset))
255  {
256  m_Binding.ResetDefault(actionName, EInputDeviceType.MOUSE, preset);
257  reset = true;
258  }
259 
260  break;
261  }
262  case EInputDeviceType.GAMEPAD:
263  {
264  if (!m_Binding.IsDefault(actionName, EInputDeviceType.GAMEPAD, preset))
265  {
266  m_Binding.ResetDefault(actionName, EInputDeviceType.GAMEPAD, preset);
267  reset = true;
268  }
269 
270  break;
271  }
272  }
273 
274  if (!reset)
275  return;
276 
277  m_Binding.Save();
278  }
279 
280  //------------------------------------------------------------------------------------------------
281  void ResetAllActions(EInputDeviceType device = EInputDeviceType.KEYBOARD)
282  {
283  array<string> contexts = {};
284  m_Binding.GetContexts(contexts);
285  string finalPreset;
286  string devicePrefix;
287 
288  foreach (SCR_KeyBindingCategory category: m_KeybindConfig.m_KeyBindingCategories)
289  {
290  foreach (SCR_KeyBindingEntry entry: category.m_KeyBindingEntries)
291  {
292  if (device == EInputDeviceType.GAMEPAD)
293  devicePrefix = GetGamepadPresetPrefix();
294  else
295  devicePrefix = GetPrimaryPresetPrefix();
296 
297  if (!entry.m_sPreset.IsEmpty())
298  finalPreset = devicePrefix + entry.m_sPreset;
299 
300  ResetAction(entry.m_sActionName, finalPreset, device);
301  }
302  }
303  m_Binding.Save();
304  }
305 
306  //------------------------------------------------------------------------------------------------
307  string GetGamepadPresetPrefix()
308  {
309  return GAMEPAD_PRESET_PREFIX;
310  }
311 
312  //------------------------------------------------------------------------------------------------
313  string GetPrimaryPresetPrefix()
314  {
315  return PRIMARY_PRESET_PREFIX;
316  }
317 
318  //------------------------------------------------------------------------------------------------
319  string GetKeyboardDeviceString()
320  {
321  return DEVICE_KEYBOARD;
322  }
323 
324  //------------------------------------------------------------------------------------------------
325  string GetGamepadDeviceString()
326  {
327  return DEVICE_GAMEPAD;
328  }
329 }
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_SettingsManagerKeybindModule
Definition: SCR_SettingsManagerKeybindModule.c:2
SCR_KeyBindingCombo
Definition: SCR_KeyBindingMenuConfig.c:124
SCR_SettingsManagerModuleBase
void SCR_SettingsManagerModuleBase()
Definition: SCR_SettingsManager.c:65
SetModuleType
void SetModuleType(ESettingManagerModuleType managerType)
Definition: SCR_SettingsManager.c:59
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
SCR_KeyBindingCategory
Definition: SCR_KeyBindingMenuConfig.c:82
SCR_KeyBindingMenuConfig
Definition: SCR_KeyBindingMenuConfig.c:9
SCR_KeyBindingFilter
Definition: SCR_KeyBindingMenuConfig.c:95
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180
ESettingManagerModuleType
ESettingManagerModuleType
Definition: SCR_SettingsManager.c:2