Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
EditBoxFilterComponent.c
Go to the documentation of this file.
1 // This component handles visualization of weapon selection menu
2 //
3 //------------------------------------------------------------------------------------------------
4 
5 class EditBoxFilterComponent : ScriptedWidgetComponent
6 {
7  [Attribute("500", UIWidgets.EditBox, "Set maximum character limit of the editbox")]
8  private int m_iCharacterLimit;
9 
10  [Attribute("true", UIWidgets.CheckBox, "Allow punctuation character, like space, brackets, ,./@#$%^ etc")]
11  private bool m_bPunctuation;
12 
13  [Attribute("true", UIWidgets.CheckBox, "Allow numerical characters")]
14  private bool m_bNumbers;
15 
16  [Attribute("true", UIWidgets.CheckBox, "Allow basic set of characters")]
17  private bool m_bASCIIchars;
18 
19  [Attribute("true", UIWidgets.CheckBox, "Allow UTF-8 multibyte symbols - including special symbols and diacritics")]
20  private bool m_bUTFMultibyte;
21 
22  [Attribute("", UIWidgets.EditBox, "Explicitly blacklisted characters, write without spaces or or commas")]
23  private string m_sCharBlacklist;
24 
25  [Attribute("", UIWidgets.EditBox, "Explicitly whitelisted characters, write without spaces or or commas")]
26  private string m_sCharWhitelist;
27 
28  private ref array<int> m_aBlacklist = new array<int>;
29  private ref array<int> m_aWhitelist = new array<int>;
30 
31  private EditBoxWidget m_wEditBox;
32 
33  private int m_iCharacterCount;
34 
35  protected bool m_bLastInputValid = true;
36 
37  ref ScriptInvoker m_OnInvalidInput = new ScriptInvoker();
38  ref ScriptInvoker m_OnTextTooLong = new ScriptInvoker();
39  ref ScriptInvoker m_OnValidInput = new ScriptInvoker();
40 
41  //------------------------------------------------------------------------------------------------
42  override void HandlerAttached(Widget w)
43  {
44  super.HandlerAttached(w);
45 
46  m_wEditBox = EditBoxWidget.Cast(w);
47 
48  if (!m_wEditBox)
49  {
50  Print("EditBoxFilterComponent used on invalid widget type.", LogLevel.WARNING);
51  return;
52  }
53 
54  int blacklistSize = m_sCharBlacklist.Length();
55  for (int i = 0; i < blacklistSize; i++)
56  {
57  int char = m_sCharBlacklist.Get(i).ToAscii();
58  m_aBlacklist.Insert(char);
59  }
60 
61  int whitelistSize = m_sCharWhitelist.Length();
62  for (int i = 0; i < whitelistSize; i++)
63  {
64  int char = m_sCharWhitelist.Get(i).ToAscii();
65  m_aWhitelist.Insert(char);
66  }
67  }
68 
69  //------------------------------------------------------------------------------------------------
70  override void HandlerDeattached(Widget w)
71  {
72  m_aBlacklist = null;
73  m_aWhitelist = null;
74  }
75 
76  //------------------------------------------------------------------------------------------------
77  override bool OnChange(Widget w, int x, int y, bool finished)
78  {
79  bool validInput = true;
80  if (!m_wEditBox)
81  return false;
82 
83  string text = m_wEditBox.GetText();
84  int length = text.Length();
85  string shortText = text;
86 
87  if (length > 0)
88  {
89  if (length > m_iCharacterLimit)
90  {
91  shortText = text.Substring(0, m_iCharacterLimit);
92  m_wEditBox.SetText(shortText);
93 
94  m_OnTextTooLong.Invoke();
95  validInput = false;
96  }
97  else
98  {
99  // Find invalid characters
100  shortText = FilterSymbolsFromText(text);
101 
102  // Invoke wrong input if texts are not same
103  if (shortText != text)
104  {
105  m_wEditBox.SetText(shortText);
106  m_OnInvalidInput.Invoke();
107  validInput = false;
108  }
109  }
110  }
111 
112  m_iCharacterCount = length;
113 
114  if (validInput)
115  m_OnValidInput.Invoke();
116 
117  return false;
118  }
119 
120  //------------------------------------------------------------------------------------------------
121  int GetNumLimitOfCharacters()
122  {
123  return m_iCharacterLimit;
124  }
125 
126 
127  //------------------------------------------------------------------------------------------------
128  private bool FilterSymbol(string char)
129  {
130  bool found = false;
131  int asciiCode = char.ToAscii();
132 
133  if (asciiCode < 0)
134  {
135  if (m_bUTFMultibyte)
136  found = true;
137  }
138  else if (asciiCode >= 32 && asciiCode <= 47)
139  {
140  if (m_bPunctuation)
141  found = true;
142  }
143  else if (asciiCode <= 57)
144  {
145  if (m_bNumbers)
146  found = true;
147  }
148  else
149  {
150  if (m_bASCIIchars)
151  found = true;
152  }
153 
154  bool valid;
155  if (found)
156  {
157  // Apply blacklist
158  valid = !SearchWhitelist(asciiCode, true);
159  }
160  else
161  {
162  // Apply whitelist
163  valid = SearchWhitelist(asciiCode, false);
164  }
165 
166  return valid;
167  }
168 
169  //------------------------------------------------------------------------------------------------
170  private bool SearchWhitelist(int char, bool blacklist)
171  {
172  array<int> list;
173  if (blacklist)
174  list = m_aBlacklist;
175  else
176  list = m_aWhitelist;
177 
178  if (list)
179  {
180  foreach (int val : list)
181  {
182  if (val == char)
183  return true;
184  }
185  }
186  return false;
187  }
188 
189  //------------------------------------------------------------------------------------------------
192  protected string FilterSymbolsFromText(string text)
193  {
194  string newText = string.Empty;
195 
196  // Compare text
197  for (int i = 0; i < text.Length(); i++)
198  {
199  string char = text[i];
200 
201  m_bLastInputValid = FilterSymbol(char);
202  if (!m_bLastInputValid)
203  continue;
204 
205  newText += char;
206  }
207 
208  return newText;
209  }
210 
211  //------------------------------------------------------------------------------------------------
212  // API
213  //------------------------------------------------------------------------------------------------
214 
215  //------------------------------------------------------------------------------------------------
216  void SetCharacterLimit(int limit)
217  {
218  m_iCharacterLimit = limit;
219  }
220 
221  //------------------------------------------------------------------------------------------------
222  void SetPunctuation(bool enabled)
223  {
224  m_bPunctuation = enabled;
225  }
226 
227  //------------------------------------------------------------------------------------------------
228  void SetNumbers(bool enabled)
229  {
230  m_bNumbers = enabled;
231  }
232 
233  //------------------------------------------------------------------------------------------------
234  void SetASCIIchars(bool enabled)
235  {
236  m_bASCIIchars = enabled;
237  }
238 
239  //------------------------------------------------------------------------------------------------
240  void SetUTFMultibyte(bool enabled)
241  {
242  m_bUTFMultibyte = enabled;
243  }
244 
245  //------------------------------------------------------------------------------------------------
246  void SetCharBlacklist(string list)
247  {
248  m_aBlacklist.Clear();
249 
250  int blacklistSize = list.Length();
251  for (int i = 0; i < blacklistSize; i++)
252  {
253  int char = list.Get(i).ToAscii();
254  m_aBlacklist.Insert(char);
255  }
256  }
257 
258  //------------------------------------------------------------------------------------------------
259  void SetCharacterLimit(string list)
260  {
261  m_sCharWhitelist = list;
262  }
263 
264  //------------------------------------------------------------------------------------------------
265  bool IsLastInputValid()
266  {
267  return m_bLastInputValid;
268  }
269 };
Attribute
EditBoxFilterComponent Attribute
Post-process effect of scripted camera.
EditBoxFilterComponent
Definition: EditBoxFilterComponent.c:5