Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Login2FADialogUI.c
Go to the documentation of this file.
1 // TODO: make the number of edit boxes procedural
3 {
4  protected string m_s2FAText;
5  protected string m_sName;
6  protected string m_sCode;
7 
8  protected ref array<SCR_EditBoxComponent> m_aEditBoxComponents = {};
9 
10  protected Widget m_wCodeWrapper;
11 
12  protected const int EDIT_BOX_INITIAL_FOCUS_DELAY = 100;
13  protected const int EDIT_BOX_FLICKER_TIME = 400;
14 
15  //------------------------------------------------------------------------------------------------
16  void SCR_Login2FADialogUI(string name, string code)
17  {
18  m_sName = name;
19  m_sCode = code;
20  }
21 
22  //------------------------------------------------------------------------------------------------
23  override void OnMenuOpen(SCR_ConfigurableDialogUiPreset preset)
24  {
25  super.OnMenuOpen(preset);
26 
27  // Find all edit boxes
28  m_wCodeWrapper = m_wRoot.FindAnyWidget("CodeWrapper");
29  if (m_wCodeWrapper)
30  {
31  array<ref Widget> editBoxes = {};
32  SCR_WidgetHelper.GetAllChildren(m_wCodeWrapper, editBoxes);
33 
34  foreach (Widget w : editBoxes)
35  {
37  if (!comp)
38  continue;
39 
40  m_aEditBoxComponents.Insert(comp);
41  BindEditBoxInputEvent(comp);
42  }
43  }
44 
45  //TODO: there is something resetting the focus to null somewhere, so we need to delay the activation. FIX!
46  GetGame().GetCallqueue().CallLater(ActivateFirstEditBox, EDIT_BOX_INITIAL_FOCUS_DELAY);
47  }
48 
49  //------------------------------------------------------------------------------------------------
50  override void OnMenuClose()
51  {
52  super.OnMenuClose();
53 
54  GetGame().GetCallqueue().Remove(OnFailDelayed);
55  GetGame().GetCallqueue().Remove(ActivateFirstEditBox);
56  }
57 
58  //------------------------------------------------------------------------------------------------
59  override void OnConfirm()
60  {
61  if (m_bIsLoading)
62  return;
63 
64  string code = m_s2FAText.Trim();
65 
66  if (!VerifyFormatting(code))
67  {
68  ShowWarningMessage(true);
69  return;
70  }
71 
72  GetGame().GetBackendApi().SetCredentialsItem(EBackendCredentials.EBCRED_NAME, m_sName);
73  GetGame().GetBackendApi().SetCredentialsItem(EBackendCredentials.EBCRED_PWD, m_sCode);
74  GetGame().GetBackendApi().SetCredentialsItem(EBackendCredentials.EBCRED_2FA_TOKEN, code);
75  GetGame().GetBackendApi().VerifyCredentials(m_Callback, true);
76 
77  super.OnConfirm();
78  }
79 
80  //------------------------------------------------------------------------------------------------
81  override void OnTimeout(SCR_BackendCallback callback)
82  {
83  super.OnTimeout(callback);
84 
85  Clear();
86  }
87 
88  //------------------------------------------------------------------------------------------------
89  override void OnFailDelayed(SCR_BackendCallback callback, int code, int restCode, int apiCode)
90  {
91  super.OnFailDelayed(callback, code, restCode, apiCode);
92 
93  Clear();
94  }
95 
96  //------------------------------------------------------------------------------------------------
97  // Code must be entirely made out of numbers
98  override bool VerifyFormatting(string text)
99  {
100  if (!IsCodeComplete() || text.Contains(" "))
101  return false;
102 
103  for (int i = 0; i < text.Length(); i++)
104  {
105  string char = text[i];
106  int asciiCode = char.ToAscii();
107 
108  if (asciiCode < 48 || asciiCode > 57)
109  return false;
110  }
111 
112  return super.VerifyFormatting(text);
113  }
114 
115  //------------------------------------------------------------------------------------------------
117  protected void ActivateFirstEditBox()
118  {
119  if (!m_aEditBoxComponents.IsEmpty())
120  m_aEditBoxComponents[0].ActivateWriteMode(true);
121  }
122 
123  //------------------------------------------------------------------------------------------------
125  protected void BindEditBoxInputEvent(SCR_EditBoxComponent comp)
126  {
127  comp.m_OnTextChange.Insert(OnTextChange);
128  }
129 
130  //------------------------------------------------------------------------------------------------
132  protected void OnTextChange(string text)
133  {
134  if (m_bIsLoading || text.IsEmpty())
135  return;
136 
137  m_s2FAText = string.Empty;
138 
140  {
141  string value = comp.GetValue();
142  if (value.IsEmpty())
143  {
144  comp.ActivateWriteMode(true);
145  break;
146  }
147 
148  m_s2FAText += value;
149  }
150 
151  if (IsCodeComplete())
152  OnConfirm();
153  }
154 
155  //------------------------------------------------------------------------------------------------
157  protected bool IsCodeComplete()
158  {
159  return m_s2FAText.Length() >= m_aEditBoxComponents.Count();
160  }
161 
162  //------------------------------------------------------------------------------------------------
163  protected void Clear()
164  {
165  m_s2FAText = string.Empty;
166 
168  {
169  comp.SetValue(string.Empty);
170 
171  // Flicker components
172  GetGame().GetCallqueue().Remove(comp.ResetOverlayColor);
173 
174  comp.ChangeOverlayColor(Color.FromInt(UIColors.WARNING.PackToInt()));
175  GetGame().GetCallqueue().CallLater(comp.ResetOverlayColor, EDIT_BOX_FLICKER_TIME);
176  }
177 
178  ActivateFirstEditBox();
179  }
180 }
181 
182 //------------------------------------------------------------------------------------------------
183 class SCR_Login2FADialogConsoleUI : SCR_Login2FADialogUI
184 {
185  //------------------------------------------------------------------------------------------------
187  {
188  comp.m_OnWriteModeLeave.Insert(OnTextChange);
189  }
190 
191  //------------------------------------------------------------------------------------------------
192  // We have a single edit box in which the whole code is inserted
193  override bool IsCodeComplete()
194  {
195  return !m_s2FAText.IsEmpty();
196  }
197 
198  //------------------------------------------------------------------------------------------------
199  // We don't want to activate the edit box automatically or the dialog will be obscured by the virtual console, so we just focus it
200  override void ActivateFirstEditBox()
201  {
202  if (!m_aEditBoxComponents.IsEmpty())
203  GetGame().GetWorkspace().SetFocusedWidget(m_aEditBoxComponents[0].GetRootWidget());
204  }
205 }
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
IsCodeComplete
protected bool IsCodeComplete()
We have a number of single digit edit boxes. Overridden in console version.
Definition: Login2FADialogUI.c:155
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_wCodeWrapper
protected Widget m_wCodeWrapper
Definition: Login2FADialogUI.c:8
m_sCode
protected string m_sCode
Definition: Login2FADialogUI.c:4
m_Callback
protected ref CampaignCallback m_Callback
Definition: SCR_PlayerProfileManagerComponent.c:25
GetRootWidget
Widget GetRootWidget()
Definition: SCR_UITaskManagerComponent.c:160
SCR_BackendCallback
Scripted backend callback class unifying backend response.
Definition: SCR_BackendCallback.c:21
OnTextChange
protected void OnTextChange(string text)
If number was inserted into current EditBox, add that number to the string and go to next EditBox....
Definition: Login2FADialogUI.c:130
SCR_WidgetHelper
Definition: SCR_WidgetHelper.c:1
m_s2FAText
protected string m_s2FAText
Definition: Login2FADialogUI.c:2
UIColors
Definition: Constants.c:16
ActivateFirstEditBox
protected void ActivateFirstEditBox()
Overridden in console version.
Definition: Login2FADialogUI.c:115
BindEditBoxInputEvent
SCR_Login2FADialogUI SCR_LoginProcessDialogUI BindEditBoxInputEvent(SCR_EditBoxComponent comp)
Overridden in console version.
Definition: Login2FADialogUI.c:186
SCR_ConfigurableDialogUiPreset
Configuration for a dialog.
Definition: SCR_ConfigurableDialogUI.c:809
SCR_Login2FADialogUI
Definition: Login2FADialogUI.c:2
m_aEditBoxComponents
protected ref array< SCR_EditBoxComponent > m_aEditBoxComponents
Definition: Login2FADialogUI.c:6
SCR_EditBoxComponent
Definition: SCR_EditBoxComponent.c:8
callback
DownloadConfigCallback callback
m_sName
protected LocalizedString m_sName
Definition: SCR_GroupIdentityComponent.c:19
EDIT_BOX_FLICKER_TIME
const protected int EDIT_BOX_FLICKER_TIME
Definition: Login2FADialogUI.c:11
SCR_LoginProcessDialogUI
Definition: SCR_LoginProcessDialogUI.c:5
EDIT_BOX_INITIAL_FOCUS_DELAY
const protected int EDIT_BOX_INITIAL_FOCUS_DELAY
Definition: Login2FADialogUI.c:10