Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_KeybindRowComponent.c
Go to the documentation of this file.
1 //------------------------------------------------------------------------------------------------
3 {
4  protected const string WIDGET_BIND_PC = "PCBind";
5  protected const string WIDGET_BIND_CONTROLLER = "ControllerBind";
6 
7  static const string BUTTON_ADVANCED_KEYBIND = "AdvancedKeybind";
8  static const string BUTTON_RESET = "Reset";
9  static const string BUTTON_UNBIND = "Unbind";
10 
11  protected const string ACTION_DISPLAY_ROOT = "KeybindActionDisplayRow";
12 
13  protected bool m_bFocused;
14  protected bool m_bIsDialogOpen;
15 
16  protected ref InputBinding m_Binding;
17  protected ref SCR_KeyBindingEntry m_Entry;
18  protected SCR_SettingsManagerKeybindModule m_SettingsKeybindModule;
19 
20  protected string m_sActionName;
21  protected string m_sActionPreset;
22  protected string m_sGamepadAltActionName;
23  protected string m_sGamepadAltActionPreset;
24  protected string m_sActionDisplayName;
25 
26  protected SCR_EActionPrefixType m_eActionPrefixType;
27 
28  protected bool m_bOnInlineMouseButton;
29 
30  protected Widget m_wInlineButtonsWrapper;
31 
32  protected SCR_ModularButtonComponent m_BindButtonKeyboard;
33  protected SCR_ModularButtonComponent m_BindButtonController;
34 
35  protected SCR_KeybindActionDisplayRowComponent m_ActionDisplayKeyboard;
36  protected SCR_KeybindActionDisplayRowComponent m_ActionDisplayController;
37 
38  protected ref ScriptInvokerScriptedWidgetComponent m_OnFocus;
39  protected ref ScriptInvokerScriptedWidgetComponent m_OnFocusLost;
40 
41  protected ref ScriptInvokerVoid m_OnKeyCaptured;
42  protected ref ScriptInvokerString m_OnInlineButton;
43 
44  //------------------------------------------------------------------------------------------------
45  override event bool OnFocus(Widget w, int x, int y)
46  {
47  m_bFocused = true;
48  UpdateInlineButtons();
49  UpdateBindButtons();
50  UpdateActionDisplays();
51 
52  if (m_OnFocus)
53  m_OnFocus.Invoke(this);
54 
55  return super.OnFocus(w, x, y);
56  }
57 
58  //------------------------------------------------------------------------------------------------
59  override event bool OnFocusLost(Widget w, int x, int y)
60  {
61  m_bFocused = false;
62  UpdateInlineButtons();
63  UpdateBindButtons();
64  UpdateActionDisplays();
65 
66  if (m_OnFocusLost)
67  m_OnFocusLost.Invoke(this);
68 
69  return super.OnFocusLost(w, x, y);
70  }
71 
72  //------------------------------------------------------------------------------------------------
73  override void HandlerDeattached(Widget w)
74  {
75  super.HandlerDeattached(w);
76  SCR_MenuHelper.GetOnDialogClose().Remove(OnDialogClose);
77  }
78 
79  // Init
80  //------------------------------------------------------------------------------------------------
81  void Init(notnull InputBinding binding, SCR_KeyBindingEntry entry)
82  {
83  // Setup
84  m_sActionDisplayName = entry.m_sDisplayName;
85  if (m_sActionDisplayName.Length() == 0)
86  m_sActionDisplayName = "<#AR-Settings_Keybind_MissingName>" + entry.m_sActionName;
87 
88  m_sActionPreset = entry.m_sPreset;
89  m_sActionName = entry.m_sActionName;
90  m_sGamepadAltActionName = entry.m_sActionNameGamepadOptional;
91  m_sGamepadAltActionPreset = entry.m_sPresetGamepadOptional;
92  m_Binding = binding;
93  m_Entry = entry;
94  m_eActionPrefixType = entry.m_ePrefixType;
95 
98  return;
99 
100  RichTextWidget textBox = RichTextWidget.Cast(m_wRoot.FindAnyWidget("Name"));
101  textBox.SetText(m_sActionDisplayName);
102  textBox.ElideText(1, 1.0, "...");
103 
104  // Bindings buttons
105  // Keyboard
106  Widget pcBind = m_wRoot.FindAnyWidget(WIDGET_BIND_PC);
107  if (pcBind)
108  {
109  m_BindButtonKeyboard = SCR_ModularButtonComponent.FindComponent(pcBind);
110  m_ActionDisplayKeyboard = SCR_KeybindActionDisplayRowComponent.FindComponentInHierarchy(pcBind);
111  if (m_ActionDisplayKeyboard)
112  m_ActionDisplayKeyboard.GetOnAdditionalBindsClicked().Insert(OnAdvancedKeybind);
113  }
114 
115  if (m_BindButtonKeyboard)
116  {
118  m_BindButtonKeyboard.SetVisible(false);
119  else
120  SetRichTextAction(EInputDeviceType.KEYBOARD);
121 
122  m_BindButtonKeyboard.m_OnClicked.Insert(OnBindKeyboardClicked);
123  m_BindButtonKeyboard.m_OnMouseEnter.Insert(OnInlineMouseButtonEnter);
124  m_BindButtonKeyboard.m_OnMouseLeave.Insert(OnInlineMouseButtonLeave);
125  }
126 
127  // Controller
128  Widget controllerBind = m_wRoot.FindAnyWidget(WIDGET_BIND_CONTROLLER);
129  if (controllerBind)
130  {
131  m_BindButtonController = SCR_ModularButtonComponent.FindComponent(controllerBind);
132  m_ActionDisplayController = SCR_KeybindActionDisplayRowComponent.FindComponentInHierarchy(controllerBind);
133  }
134 
135  if (m_BindButtonController)
136  {
137  SetRichTextAction(EInputDeviceType.GAMEPAD);
138 
139  m_BindButtonController.m_OnClicked.Insert(OnBindControllerClicked);
140  m_BindButtonController.m_OnMouseEnter.Insert(OnInlineMouseButtonEnter);
141  m_BindButtonController.m_OnMouseLeave.Insert(OnInlineMouseButtonLeave);
142  }
143 
144  // Main button
145  SCR_ModularButtonComponent button = SCR_ModularButtonComponent.FindComponent(m_wRoot);
146  if (button)
147  button.m_OnClicked.Insert(OnButtonClick);
148 
149  // Inline buttons
150  m_wInlineButtonsWrapper = m_wRoot.FindAnyWidget("InlineButtons");
151  if (m_wInlineButtonsWrapper)
152  {
153  array<ref Widget> children = {};
154  SCR_WidgetHelper.GetAllChildren(m_wInlineButtonsWrapper, children);
155  SCR_ModularButtonComponent comp;
156  foreach (Widget child : children)
157  {
158  comp = SCR_ModularButtonComponent.FindComponent(child);
159  if (!comp)
160  continue;
161 
162  switch (child.GetName())
163  {
164  case BUTTON_ADVANCED_KEYBIND: comp.m_OnClicked.Insert(OnAdvancedKeybind); break;
165  case BUTTON_RESET: comp.m_OnClicked.Insert(OnReset); break;
166  case BUTTON_UNBIND: comp.m_OnClicked.Insert(OnUnbind); break;
167  }
168 
169  comp.m_OnMouseEnter.Insert(OnInlineMouseButtonEnter);
170  comp.m_OnMouseLeave.Insert(OnInlineMouseButtonLeave);
171  }
172  }
173 
174  UpdateInlineButtons();
175  UpdateBindButtons();
176  UpdateActionDisplays();
177 
178  GetGame().OnInputDeviceUserChangedInvoker().Insert(OnInputDeviceChange);
179  SCR_MenuHelper.GetOnDialogOpen().Insert(OnDialogOpen);
180  SCR_MenuHelper.GetOnDialogClose().Insert(OnDialogClose);
181  }
182 
183  // Protected
184  //------------------------------------------------------------------------------------------------
185  protected void OnButtonClick()
186  {
187  EInputDeviceType device = GetGame().GetInputManager().GetLastUsedInputDevice();
188 
189  if (m_bOnInlineMouseButton && device == EInputDeviceType.MOUSE)
190  return;
191 
192  if (device == EInputDeviceType.MOUSE || device == EInputDeviceType.KEYBOARD)
193  CaptureKeyboard();
194 
195  else if (device == EInputDeviceType.GAMEPAD)
196  CaptureGamepad();
197  }
198 
199  //------------------------------------------------------------------------------------------------
200  protected void CaptureKeyboard()
201  {
202  if (m_sActionPreset.IsEmpty())
203  return;
204 
205  string finalPreset = m_SettingsKeybindModule.GetPrimaryPresetPrefix() + m_sActionPreset;
206 
207  // Enfusion supports capturing mouse and keyboard at once under the EInputDeviceType.KEYBOARD
208  m_SettingsKeybindModule.StartCaptureForAction(m_sActionName, finalPreset, EInputDeviceType.KEYBOARD, false);
209 
210  // Dialog
211  SCR_SimpleKeybindDialogUI dialog = SCR_KeybindDialogs.CreateSimpleKeybindDialog(
212  m_Entry,
213  m_sActionDisplayName,
215  m_Binding,
216  EInputDeviceType.KEYBOARD
217  );
218 
219  if (!dialog)
220  return;
221 
222  dialog.GetOnKeyCaptured().Insert(OnKeyCaptured);
223  }
224 
225  //------------------------------------------------------------------------------------------------
226  protected void CaptureGamepad()
227  {
228  if (m_sActionPreset.IsEmpty() || !m_sGamepadAltActionPreset.IsEmpty())
229  return;
230 
231  string finalPreset;
232 
233  if (m_sGamepadAltActionPreset.IsEmpty())
234  finalPreset = m_SettingsKeybindModule.GetGamepadPresetPrefix() + m_sActionPreset;
235  else
236  finalPreset = m_SettingsKeybindModule.GetGamepadPresetPrefix() + m_sGamepadAltActionPreset;
237 
238  m_SettingsKeybindModule.StartCaptureForAction(m_sActionName, finalPreset, EInputDeviceType.GAMEPAD, false);
239 
240  // Dialog
241  SCR_SimpleKeybindDialogUI dialog = SCR_KeybindDialogs.CreateSimpleKeybindDialog(
242  m_Entry,
243  m_sActionDisplayName,
245  m_Binding,
246  EInputDeviceType.GAMEPAD
247  );
248 
249  if (!dialog)
250  return;
251 
252  dialog.GetOnKeyCaptured().Insert(OnKeyCaptured);
253  }
254 
255  //------------------------------------------------------------------------------------------------
256  protected void OnInlineMouseButtonEnter(SCR_ModularButtonComponent comp, bool mouseInput)
257  {
258  m_bOnInlineMouseButton = true;
259  }
260 
261  //------------------------------------------------------------------------------------------------
262  protected void OnInlineMouseButtonLeave(SCR_ModularButtonComponent comp, bool mouseInput)
263  {
264  m_bOnInlineMouseButton = false;
265  }
266 
267  //------------------------------------------------------------------------------------------------
268  protected void OnBindKeyboardClicked(SCR_ModularButtonComponent comp)
269  {
270  if (!comp.GetToggled())
271  CaptureKeyboard();
272  }
273 
274  //------------------------------------------------------------------------------------------------
275  protected void OnBindControllerClicked(SCR_ModularButtonComponent comp)
276  {
277  if (!comp.GetToggled())
278  CaptureGamepad();
279  }
280 
281  //------------------------------------------------------------------------------------------------
282  protected void OnAdvancedKeybind()
283  {
284  if (m_OnInlineButton)
285  m_OnInlineButton.Invoke(BUTTON_ADVANCED_KEYBIND);
286  }
287 
288  //------------------------------------------------------------------------------------------------
289  protected void OnReset()
290  {
291  if (m_OnInlineButton)
292  m_OnInlineButton.Invoke(BUTTON_RESET);
293  }
294 
295  //------------------------------------------------------------------------------------------------
296  protected void OnUnbind()
297  {
298  if (m_OnInlineButton)
299  m_OnInlineButton.Invoke(BUTTON_UNBIND);
300  }
301 
302  //------------------------------------------------------------------------------------------------
303  protected void OnKeyCaptured()
304  {
305  if (m_OnKeyCaptured)
306  m_OnKeyCaptured.Invoke();
307  }
308 
309  //------------------------------------------------------------------------------------------------
310  protected void OnInputDeviceChange(EInputDeviceType oldDevice, EInputDeviceType newDevice)
311  {
312  UpdateInlineButtons();
313  UpdateBindButtons();
314  UpdateActionDisplays();
315  }
316 
317  //------------------------------------------------------------------------------------------------
318  protected void OnDialogOpen(DialogUI dialog)
319  {
320  m_bIsDialogOpen = true;
321  }
322 
323  //------------------------------------------------------------------------------------------------
324  protected void OnDialogClose(DialogUI dialog)
325  {
326  m_bIsDialogOpen = false;
327  UpdateBindButtons();
328  UpdateActionDisplays();
329  }
330 
331  //------------------------------------------------------------------------------------------------
332  // Currently supports either KEYBOARD or GAMEPAD
333  protected void SetRichTextAction(EInputDeviceType device)
334  {
335  if (device == EInputDeviceType.MOUSE)
336  device = EInputDeviceType.KEYBOARD;
337 
338  if (m_ActionDisplayKeyboard && device != EInputDeviceType.GAMEPAD)
339  m_ActionDisplayKeyboard.CreateActionDisplays(m_Entry, m_SettingsKeybindModule, device);
340  else if (m_ActionDisplayController)
341  m_ActionDisplayController.CreateActionDisplays(m_Entry, m_SettingsKeybindModule, device);
342  }
343 
344  //------------------------------------------------------------------------------------------------
345  protected void UpdateActionDisplayScrolling(bool enable)
346  {
347  if (m_ActionDisplayKeyboard)
348  m_ActionDisplayKeyboard.UpdateScrolling(enable);
349 
350  if (m_ActionDisplayController)
351  m_ActionDisplayController.UpdateScrolling(enable);
352  }
353 
354  //------------------------------------------------------------------------------------------------
355  protected void UpdateInlineButtons()
356  {
357  bool visible = m_bFocused && GetGame().GetInputManager().GetLastUsedInputDevice() == EInputDeviceType.MOUSE;
358 
359  if (m_wInlineButtonsWrapper)
360  m_wInlineButtonsWrapper.SetVisible(visible);
361  }
362 
363  //------------------------------------------------------------------------------------------------
364  protected void UpdateBindButtons()
365  {
366  if (m_bIsDialogOpen)
367  return;
368 
369  bool isController = GetGame().GetInputManager().GetLastUsedInputDevice() == EInputDeviceType.GAMEPAD;
370  bool keyboardBindEnabled = !isController && (!GetGame().IsPlatformGameConsole() || GetGame().GetHasKeyboard());
371 
372  // Toggling is used as an alternative to disabling, so that they may still react to mouse inputs
373  if (m_BindButtonKeyboard)
374  m_BindButtonKeyboard.SetToggled(!keyboardBindEnabled || m_sActionPreset.IsEmpty(), false);
375 
376  if (m_BindButtonController)
377  m_BindButtonController.SetToggled(!isController || m_sActionPreset.IsEmpty(), false);
378  }
379 
380  //------------------------------------------------------------------------------------------------
381  protected void UpdateActionDisplays()
382  {
383  bool isController = GetGame().GetInputManager().GetLastUsedInputDevice() == EInputDeviceType.GAMEPAD;
384  SCR_EActionDisplayState stateKeyboard = SCR_EActionDisplayState.DEFAULT;
385  SCR_EActionDisplayState stateGamepad = SCR_EActionDisplayState.DEFAULT;
386 
387  if (isController)
388  stateKeyboard = SCR_EActionDisplayState.DISABLED;
389  else
390  stateGamepad = SCR_EActionDisplayState.DISABLED;
391 
392  if (m_ActionDisplayKeyboard)
393  m_ActionDisplayKeyboard.SetAllActionDisplayStates(stateKeyboard);
394 
395  if (m_ActionDisplayController)
396  m_ActionDisplayController.SetAllActionDisplayStates(stateGamepad);
397  }
398 
399  // Public
400  //------------------------------------------------------------------------------------------------
401  void ResetAction()
402  {
403  EInputDeviceType device = GetGame().GetInputManager().GetLastUsedInputDevice();
404 
405  m_SettingsKeybindModule.ResetAction(m_sActionName, GetFinalActionPreset(), device);
406 
407  SetRichTextAction(device);
408  UpdateActionDisplayScrolling(false);
409  }
410 
411  //------------------------------------------------------------------------------------------------
412  void Unbind()
413  {
414  EInputDeviceType device = GetGame().GetInputManager().GetLastUsedInputDevice();
415 
416  // KEYBOARD is also used for mouse
417  if (device == EInputDeviceType.MOUSE)
418  device = EInputDeviceType.KEYBOARD;
419 
420  m_SettingsKeybindModule.UnbindAction(m_sActionName, GetFinalActionPreset(), device);
421 
422  SetRichTextAction(device);
423  UpdateActionDisplayScrolling(false);
424  }
425 
426  //------------------------------------------------------------------------------------------------
427  SCR_KeyBindingEntry GetEntry()
428  {
429  return m_Entry;
430  }
431 
432  //------------------------------------------------------------------------------------------------
433  string GetActionName()
434  {
435  return m_sActionName;
436  }
437 
438  //------------------------------------------------------------------------------------------------
439  string GetDisplayName()
440  {
441  return m_sActionDisplayName;
442  }
443 
444  //------------------------------------------------------------------------------------------------
445  string GetActionPreset()
446  {
447  return m_sActionPreset;
448  }
449 
450  //------------------------------------------------------------------------------------------------
451  string GetFinalActionPreset()
452  {
453  string finalPreset;
454  string devicePrefix;
455  EInputDeviceType deviceType = GetGame().GetInputManager().GetLastUsedInputDevice();
456 
457  if (deviceType == EInputDeviceType.GAMEPAD)
458  devicePrefix = m_SettingsKeybindModule.GetGamepadPresetPrefix();
459  else
460  devicePrefix = m_SettingsKeybindModule.GetPrimaryPresetPrefix();
461 
462  if (!m_sActionPreset.IsEmpty())
463  finalPreset = devicePrefix + m_sActionPreset;
464 
465  return finalPreset;
466  }
467 
468  //------------------------------------------------------------------------------------------------
469  SCR_EActionPrefixType GetActionPrefixType()
470  {
471  return m_eActionPrefixType;
472  }
473 
474  //------------------------------------------------------------------------------------------------
476  {
477  if (!m_OnFocus)
479 
480  return m_OnFocus;
481  }
482 
483  //------------------------------------------------------------------------------------------------
484  ScriptInvokerScriptedWidgetComponent GetOnFocusLost()
485  {
486  if (!m_OnFocusLost)
488 
489  return m_OnFocusLost;
490  }
491 
492  //------------------------------------------------------------------------------------------------
493  ScriptInvokerVoid GetOnKeyCaptured()
494  {
495  if (!m_OnKeyCaptured)
496  m_OnKeyCaptured = new ScriptInvokerVoid();
497 
498  return m_OnKeyCaptured;
499  }
500 
501  //------------------------------------------------------------------------------------------------
502  ScriptInvokerString GetOnInlineButton()
503  {
504  if (!m_OnInlineButton)
505  m_OnInlineButton = new ScriptInvokerString();
506 
507  return m_OnInlineButton;
508  }
509 
510  //------------------------------------------------------------------------------------------------
511  static SCR_KeybindRowComponent FindComponent(Widget w)
512  {
513  return SCR_KeybindRowComponent.Cast(w.FindHandler(SCR_KeybindRowComponent));
514  }
515 }
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
m_SettingsKeybindModule
protected SCR_SettingsManagerKeybindModule m_SettingsKeybindModule
Definition: SCR_KeybindDialogs.c:43
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_SettingsManagerKeybindModule
Definition: SCR_SettingsManagerKeybindModule.c:2
m_sActionName
protected string m_sActionName
Definition: SCR_ActionsRadialMenuEditorComponent.c:24
GetHasKeyboard
bool GetHasKeyboard()
Definition: game.c:218
SCR_KeybindDialogs
Definition: SCR_KeybindDialogs.c:1
IsPlatformGameConsole
bool IsPlatformGameConsole()
Definition: game.c:1393
m_Entry
SCR_MapJournalUI m_Entry
m_OnFocusLost
ref ScriptInvoker m_OnFocusLost
Definition: SCR_ModularButtonComponent.c:64
SCR_WidgetHelper
Definition: SCR_WidgetHelper.c:1
SCR_KeybindRowComponent
Definition: SCR_KeybindRowComponent.c:2
ScriptInvokerString
ScriptInvokerBase< ScriptInvokerStringMethod > ScriptInvokerString
Definition: SCR_ScriptInvokerHelper.c:75
DialogUI
Definition: DialogUI.c:1
SCR_SimpleKeybindDialogUI
Definition: SCR_SimpleKeybindDialogUI.c:2
SCR_MenuHelper
Definition: SCR_MenuHelper.c:15
SCR_KeybindActionDisplayRowComponent
Definition: SCR_KeybindActionDisplayRowComponent.c:7
ScriptInvokerVoid
ScriptInvokerBase< ScriptInvokerVoidMethod > ScriptInvokerVoid
Definition: SCR_ScriptInvokerHelper.c:9
m_OnFocus
ref ScriptInvoker m_OnFocus
Definition: SCR_ModularButtonComponent.c:63
SCR_ScriptedWidgetComponent
Definition: SCR_ScriptedWidgetComponent.c:7
GetSettingsManager
SCR_SettingsManager GetSettingsManager()
Definition: game.c:191
ScriptInvokerScriptedWidgetComponent
ScriptInvokerBase< ScriptInvokerScriptedWidgetComponentMethod > ScriptInvokerScriptedWidgetComponent
Definition: SCR_ScriptedWidgetComponent.c:4
ESettingManagerModuleType
ESettingManagerModuleType
Definition: SCR_SettingsManager.c:2