Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_EditBoxComponent.c
Go to the documentation of this file.
1// Component handling interaction with text input fields (edit boxes)
2
3// TODO:
4// During interaction with edit boxes on pc, write mode does not disable itself if the dialog/menu is overlayed by another.
5// This hasn't been a problem so far as there has never been an instance of a dialog/menu opening while an edit box interaction is allowed, but let's keep it in mind nonetheless
6
7//------------------------------------------------------------------------------------------------
9{
10 [Attribute("0.1")]
11 protected float m_fColorsAnimationTime;
12
13 [Attribute("0")]
14 protected bool m_bShowWriteIcon;
15
16 [Attribute("1")]
18
19 [Attribute(UIConstants.ICONS_IMAGE_SET, UIWidgets.ResourcePickerThumbnail, "Image resource for icons")]
21
22 [Attribute("")]
23 protected string m_sIconImageName;
24
25 [Attribute("WriteIconButton")]
26 protected string m_sWriteIconButtonName;
27
28 [Attribute("0.1", "0 1")]
29 protected float m_fOpacityDefault;
30
31 [Attribute("0.4", "0 1")]
32 protected float m_fOpacityFocused;
33
34 [Attribute("0.022995 0.022995 0.022995 1.000000", UIWidgets.ColorPicker)]
36
37 [Attribute("0.108995 0.108995 0.108995 1.000000", UIWidgets.ColorPicker)]
39
40 [Attribute("0.022995 0.022995 0.022995 1.000000", UIWidgets.ColorPicker)]
42
43 [Attribute("", UIWidgets.EditBox, "Text that should appear next to error warning on invalid input")]
44 protected string m_sWarningText;
45
51
54
55 protected SCR_ModularButtonComponent m_WriteIconButton;
56
58
59 protected ref Color COLOR_INVALID_INPUT = UIColors.WARNING;
60 protected ref Color COLOR_VALID_INPUT = Color.White;
61
62 // Arguments passed: SCR_EditBoxComponent, string (text)
65
69
70 protected const int INTERACTION_STATE_UPDATE_FREQUENCY = 50;
71 protected const int FOCUS_LOST_INTERACTION_STATE_UPDATE_DELAY = 1000 / UIConstants.FADE_RATE_DEFAULT;
72
73 protected bool m_bIsFocused;
74 protected bool m_bIsInWriteMode;
76 protected bool m_bValidInput = true;
77 protected bool m_bIsTyping;
79
80 // TODO: protect these
85
86 protected string m_sTextPrevious;
87
88
89 //------------------------------------------------------------------------------------------------
90 protected void OnCancel()
91 {
92 m_OnCancel.Invoke();
93 }
94
95 //------------------------------------------------------------------------------------------------
96 override void HandlerAttached(Widget w)
97 {
98 super.HandlerAttached(w);
99 m_wColorOverlay = w.FindAnyWidget("OverlayPanel");
100
101 m_wEditBox = w.FindAnyWidget("EditBox");
102 m_wWarningIcon = w.FindAnyWidget("ErrorIcon");
103 m_wEditBackground = w.FindAnyWidget("EditBackground");
104 m_wHorizontalLayout = w.FindAnyWidget("HorizontalLayout");
105
106 m_wWriteIconScale = w.FindAnyWidget("WriteIconScale");
107 m_wImgWriteIcon = ImageWidget.Cast(w.FindAnyWidget("WriteIcon"));
108
109 m_Hint = SCR_WidgetHintComponent.Cast(SCR_WidgetTools.FindHandlerOnWidget(w, "Hint", SCR_WidgetHintComponent));
110
111 // Setup warning
112 if (GetGame().InPlayMode() && m_wWarningIcon)
113 {
114 if (m_bUseLabel)
115 m_wWarningIcon.SetOpacity(0);
116 else
117 m_wWarningIcon.SetOpacity(0); // Without label, warning icon not shown at all
118 }
119
120 if (m_Hint)
121 {
122 m_Hint.SetMessage(m_sWarningText);
123 m_Hint.SetVisible(0);
124 }
125
126 // Setup for label-less edit boxes
127 m_wBackground.SetVisible(m_bUseLabel);
128
129 // Convert to transition rate
130 if (m_fColorsAnimationTime <= 0)
132 else
134
135 if (!m_wEditBox)
136 return;
137
138//---- REFACTOR NOTE START: This code will need to be refactored as current implementation is not conforming to the standards ----
139// This component needs to operate with two different classes that duplicate most of their functionality, but not all of it's functionalities actually support both! EditBoxFilterComponent does not work on MultilineEditBoxWidget
140
142 if (filter)
143 {
145 {
146 filter.m_OnInvalidInput.Insert(OnInvalidInput);
147 filter.m_OnTextTooLong.Insert(OnInvalidInput);
148 }
149
150 filter.m_OnValidInput.Insert(OnValueChanged);
151 }
152
154 if (m_EVHComponent)
155 {
156 m_EVHComponent.GetOnFocus().Insert(OnHandlerFocus);
157 m_EVHComponent.GetOnFocusLost().Insert(OnHandlerFocusLost);
158 m_EVHComponent.GetOnChangeFinal().Insert(OnConfirm);
159 }
160
162 {
166 }
167
168 // Image
170 {
171 bool hasResource = !m_sIconImageSet.IsEmpty() && !m_sIconImageName.IsEmpty();
172 m_wWriteIconScale.SetVisible(hasResource);
173
174 if (hasResource)
176
177 ShowWriteIcon(m_bShowWriteIcon && hasResource);
178 }
179
180 // Write Icon button
181 Widget iconButton = m_wRoot.FindAnyWidget(m_sWriteIconButtonName);
182 if (iconButton)
183 m_WriteIconButton = SCR_ModularButtonComponent.FindComponent(iconButton);
184
186 m_WriteIconButton.m_OnClicked.Insert(OnInternalButtonClicked);
187
188 //Type of edit box. Why aren't these derived from a common parent :(
191
192//---- REFACTOR NOTE END ----
193
196 else if (m_wEditBoxWidget)
197 m_bIsInWriteMode = m_wEditBoxWidget.IsInWriteMode();
198
200
202 }
203
204 //------------------------------------------------------------------------------------------------
205 override void HandlerDeattached(Widget w)
206 {
207 super.HandlerDeattached(w);
208
210 }
211
212 //------------------------------------------------------------------------------------------------
213 override bool OnFocus(Widget w, int x, int y)
214 {
215 // Set focus to handler - super.OnFocus will be called only after the handler is focused!
216 GetGame().GetWorkspace().SetFocusedWidget(m_wEditBox);
217 m_bIsTyping = true;
218
219 return true;
220 }
221
222 //------------------------------------------------------------------------------------------------
224 {
225 if (m_Hint)
226 m_Hint.SetVisible(false);
227
228 m_OnChanged.Invoke(this, GetEditBoxText());
229
230 if (m_bValidInput)
231 return;
232
234
236 m_bValidInput = true;
237
238 }
239
240 //------------------------------------------------------------------------------------------------
242 {
243 // Call focus event on parent class
244 super.OnFocus(m_wRoot, 0, 0);
245
246 m_bIsTyping = true;
247 m_bIsFocused = true;
248
249 // Make the widget unfocusable
250 m_wRoot.SetFlags(WidgetFlags.NOFOCUS);
251 m_OnFocusChangedEditBox.Invoke(this, m_wEditBox, true);
252
254
255 //Update interaction state timer, because there are no delegates for write start and end :(
257 GetGame().GetCallqueue().CallLater(UpdateInteractionState, INTERACTION_STATE_UPDATE_FREQUENCY, true, false);
258 }
259
260 //------------------------------------------------------------------------------------------------
262 {
263 // Call focusLost event on parent class
264 super.OnFocusLost(m_wRoot, 0, 0);
265
266 m_bIsTyping = false;
267 m_bIsFocused = false;
268
269 // Make focusable again
270 if (m_wRoot)
271 m_wRoot.ClearFlags(WidgetFlags.NOFOCUS);
272
273 m_OnFocusChangedEditBox.Invoke(this, m_wEditBox, false);
274
276 }
277
278 //------------------------------------------------------------------------------------------------
280 {
281 // Show simple icon next to text when no message
282 if (!m_Hint || m_Hint && m_Hint.GetMessage().IsEmpty())
284 else if (m_Hint && !m_Hint.GetMessage().IsEmpty())
285 m_Hint.SetVisible(true);
286
288 m_bValidInput = false;
289 }
290
291 //------------------------------------------------------------------------------------------------
301
302 //------------------------------------------------------------------------------------------------
304 {
305 m_wColorOverlay.SetColor(color);
306 }
307
308 //------------------------------------------------------------------------------------------------
313
314 //------------------------------------------------------------------------------------------------
315 protected void OnConfirm(Widget w)
316 {
317 if (m_bIsTyping)
318 m_OnConfirm.Invoke(this, GetValue()); //TODO: this does not get called on gamepad!?
319 }
320
321 //------------------------------------------------------------------------------------------------
322 protected void OnInternalButtonClicked()
323 {
324 ActivateWriteMode(true);
325 }
326
327 //------------------------------------------------------------------------------------------------
328 protected string GetEditBoxText()
329 {
330 if (!m_wEditBox)
331 return string.Empty;
332
333 EditBoxWidget editBox = EditBoxWidget.Cast(m_wEditBox);
334 if (editBox)
335 return editBox.GetText();
336
338 if (editMulti)
339 return editMulti.GetText();
340
341 return string.Empty;
342 }
343
344 //------------------------------------------------------------------------------------------------
345 protected void SetEditBoxText(string text)
346 {
347 if (!m_wEditBox)
348 return;
349
350 EditBoxWidget editBox = EditBoxWidget.Cast(m_wEditBox);
351 if (editBox)
352 {
353 editBox.SetText(text);
354 return;
355 }
356
358 if (editMulti)
359 editMulti.SetText(text);
360 }
361
362 //------------------------------------------------------------------------------------------------
363 protected string GetPlaceholderText()
364 {
365 EditBoxWidget editBox = EditBoxWidget.Cast(m_wEditBox);
366 if (editBox)
367 return editBox.GetPlaceholderText();
368 return string.Empty;
369 }
370
371 //------------------------------------------------------------------------------------------------
372 void SetPlaceholderText(string str)
373 {
374 EditBoxWidget editBox = EditBoxWidget.Cast(m_wEditBox);
375 if (editBox)
376 editBox.SetPlaceholderText(str);
377 }
378
379 //------------------------------------------------------------------------------------------------
382 static SCR_EditBoxComponent GetEditBoxComponent(string name, Widget parent, bool searchAllChildren = true)
383 {
384 if (!parent || name == string.Empty)
385 return null;
386
387 Widget w;
388 if (searchAllChildren)
389 w = parent.FindAnyWidget(name);
390 else
391 w = parent.FindWidget(name);
392
393 if (!w)
394 return null;
395
397 return comp;
398 }
399
400 //------------------------------------------------------------------------------------------------
402 void ActivateWriteMode(bool refocus = false)
403 {
404 if (refocus)
405 {
408 target = m_wEditBoxWidget;
409
410 GetGame().GetWorkspace().SetFocusedWidget(target);
411 }
412
414 m_wMultilineEditBoxWidget.ActivateWriteMode();
415
417 m_wEditBoxWidget.ActivateWriteMode();
418 }
419
420 //------------------------------------------------------------------------------------------------
421 void ShowHint(bool show)
422 {
423 if (m_Hint)
424 m_Hint.SetVisible(show);
425 }
426
427 //------------------------------------------------------------------------------------------------
428 void ShowWriteIcon(bool show)
429 {
431 m_wWriteIconScale.SetVisible(show);
432 }
433
434//---- REFACTOR NOTE START: This code will need to be refactored as current implementation is not conforming to the standards ----
435// Callqueue workaround due to lack of proper events for write mode
436
437 //------------------------------------------------------------------------------------------------
438 void UpdateInteractionState(bool forceDisabled)
439 {
440 int interactionStateUpdateDelay;
441 if (forceDisabled)
442 {
443 m_bIsInWriteMode = false;
444 interactionStateUpdateDelay = FOCUS_LOST_INTERACTION_STATE_UPDATE_DELAY;
445 }
446 else
447 {
448 //These don't come from a common parent :(
451
452 else if (m_wEditBoxWidget)
453 m_bIsInWriteMode = m_wEditBoxWidget.IsInWriteMode();
454 }
455
456 if (m_bIsInWriteMode != m_bIsInWriteModePrevious || forceDisabled)
457 {
459
461
463 {
464 GetGame().GetInputManager().AddActionListener(UIConstants.MENU_ACTION_BACK, EActionTrigger.PRESSED, OnCancel);
465 m_OnWriteModeEnter.Invoke();
466 }
467 else
468 {
469 GetGame().GetInputManager().RemoveActionListener(UIConstants.MENU_ACTION_BACK, EActionTrigger.PRESSED, OnCancel);
471 }
472 }
473
475
476 if (GetEditBoxText() != m_sTextPrevious || forceDisabled)
477 {
480 }
481 }
482
483//---- REFACTOR NOTE END ----
484
485 //------------------------------------------------------------------------------------------------
487 {
488 GetGame().GetCallqueue().Remove(UpdateInteractionState);
490 }
491
492 //------------------------------------------------------------------------------------------------
493 protected void UpdateBackgroundColor()
494 {
496 return;
497
498 Color idleColor = m_BackgroundDefault;
499 if (m_bIsFocused)
500 idleColor = m_BackgroundFocused;
501
504 else
505 m_BackgroundCurrent = idleColor;
506
508 }
509
510 //------------------------------------------------------------------------------------------------
512 void SetValue(string value) { SetEditBoxText(value); }
513 string GetValue() { return GetEditBoxText(); }
516 bool IsValidInput() { return m_bValidInput; }
519
520 //------------------------------------------------------------------------------------------------
525
526 //------------------------------------------------------------------------------------------------
528 void ConfirmValue(string value)
529 {
530 SetEditBoxText(value);
532 }
533
534};
ArmaReforgerScripted GetGame()
Definition game.c:1398
SCR_NewSessionToolbarAction SCR_EditorToolbarAction OnConfirm()
ScriptInvokerBase< ScriptInvokerVoidMethod > ScriptInvokerVoid
ScriptInvokerBase< ScriptInvokerStringMethod > ScriptInvokerString
static WidgetAnimationOpacity Opacity(Widget widget, float targetValue, float speed, bool toggleVisibility=false)
static WidgetAnimationColor Color(Widget widget, Color color, float speed)
Definition Color.c:13
ref ScriptInvoker m_OnTextTooLong
ref ScriptInvoker m_OnInvalidInput
Base class for all widgets that can change their internal state as editbox or spinbox.
override void HandlerAttached(Widget w)
void ConfirmValue(string value)
Set string value to editbox and confirm.
const int INTERACTION_STATE_UPDATE_FREQUENCY
SCR_ModularButtonComponent m_WriteIconButton
override void HandlerDeattached(Widget w)
ref ScriptInvoker m_OnConfirm
ref ScriptInvokerVoid m_OnCancel
const int FOCUS_LOST_INTERACTION_STATE_UPDATE_DELAY
void SetValue(string value)
User API.
ref ScriptInvokerVoid m_OnWriteModeEnter
ref ScriptInvokerString m_OnWriteModeLeave
void SetEditBoxText(string text)
override bool OnFocus(Widget w, int x, int y)
static SCR_EditBoxComponent GetEditBoxComponent(string name, Widget parent, bool searchAllChildren=true)
void ChangeOverlayColor(Color color)
SCR_EventHandlerComponent GetEVHComponent()
ref ScriptInvokerString m_OnTextChange
void UpdateInteractionState(bool forceDisabled)
void SetPlaceholderText(string str)
SCR_EventHandlerComponent m_EVHComponent
SCR_WidgetHintComponent m_Hint
ref ScriptInvoker m_OnFocusChangedEditBox
MultilineEditBoxWidget m_wMultilineEditBoxWidget
SCR_WidgetHintComponent GetHint()
void ActivateWriteMode(bool refocus=false)
Set write mode of editbox handler.
static void SetActiveWidgetInteractionState(bool isActive, int delay=0)
SCR_FieldOfViewSettings Attribute
EActionTrigger
WidgetFlags
Widget flags. See enf::Widget::SetFlags().
Definition WidgetFlags.c:14
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134