Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
EditBoxFilterComponent.c
Go to the documentation of this file.
1// This component handles visualization of weapon selection menu
2//
3//------------------------------------------------------------------------------------------------
4
5//---- REFACTOR NOTE START: This code will need to be refactored as current implementation is not conforming to the standards ----
6// Missing a prefix
7
9{
10
11//---- REFACTOR NOTE END ----
12
13 [Attribute("500", UIWidgets.EditBox, "Set maximum character limit of the editbox")]
14 private int m_iCharacterLimit;
15
16 [Attribute("true", UIWidgets.CheckBox, "Allow punctuation character, like space, brackets, ,./@#$%^ etc")]
17 private bool m_bPunctuation;
18
19 [Attribute("true", UIWidgets.CheckBox, "Allow numerical characters")]
20 private bool m_bNumbers;
21
22 [Attribute("true", UIWidgets.CheckBox, "Allow basic set of characters")]
23 private bool m_bASCIIchars;
24
25 [Attribute("true", UIWidgets.CheckBox, "Allow UTF-8 multibyte symbols - including special symbols and diacritics")]
26 private bool m_bUTFMultibyte;
27
28//---- REFACTOR NOTE START: This code will need to be refactored as current implementation is not conforming to the standards ----
29// Aren't these mutually exclusive? Also filling them this way might make reading the values rather annoying. How are different alphabets handled?
30
31 [Attribute("", UIWidgets.EditBox, "Explicitly blacklisted characters, write without spaces or or commas")]
32 private string m_sCharBlacklist;
33
34 [Attribute("", UIWidgets.EditBox, "Explicitly whitelisted characters, write without spaces or or commas")]
35 private string m_sCharWhitelist;
36
37//---- REFACTOR NOTE END ----
38
39 private ref array<int> m_aBlacklist = new array<int>;
40 private ref array<int> m_aWhitelist = new array<int>;
41
42//---- REFACTOR NOTE START: This code will need to be refactored as current implementation is not conforming to the standards ----
43// Only works with single line edit box, because the widgets do not share a common parent, even though in the end they should be functionally almost identical
44
45 private EditBoxWidget m_wEditBox;
47
48//---- REFACTOR NOTE END ----
49
50 private int m_iCharacterCount;
51
52 protected bool m_bLastInputValid = true;
53
54//---- REFACTOR NOTE START: This code will need to be refactored as current implementation is not conforming to the standards ----
55// Untyped invokers
56
60
61//---- REFACTOR NOTE END ----
62
63 //------------------------------------------------------------------------------------------------
64 override void HandlerAttached(Widget w)
65 {
66 super.HandlerAttached(w);
67
68 m_wEditBox = EditBoxWidget.Cast(w);
69 if (!m_wEditBox)
71
72 if (!m_wEditBox || !m_wMultilineEditBox)
73 {
74 Print("EditBoxFilterComponent used on invalid widget type.", LogLevel.WARNING);
75 return;
76 }
77
78 int blacklistSize = m_sCharBlacklist.Length();
79 for (int i = 0; i < blacklistSize; i++)
80 {
81 int char = m_sCharBlacklist.Get(i).ToAscii();
82 m_aBlacklist.Insert(char);
83 }
84
85 int whitelistSize = m_sCharWhitelist.Length();
86 for (int i = 0; i < whitelistSize; i++)
87 {
88 int char = m_sCharWhitelist.Get(i).ToAscii();
89 m_aWhitelist.Insert(char);
90 }
91 }
92
93 //------------------------------------------------------------------------------------------------
94 override void HandlerDeattached(Widget w)
95 {
96 m_aBlacklist = null;
97 m_aWhitelist = null;
98 }
99
100 //------------------------------------------------------------------------------------------------
101 override bool OnChange(Widget w, bool finished)
102 {
103 bool validInput = true;
104 if (!m_wEditBox)
105 {
107 return OnChangeMultiline();
108
109 return false;
110 }
111
112 string text = m_wEditBox.GetText();
113 int length = text.Length();
114 string shortText = text;
115
116 if (length > 0)
117 {
118 if (length > m_iCharacterLimit)
119 {
120 shortText = text.Substring(0, m_iCharacterLimit);
121 m_wEditBox.SetText(shortText);
122
123 m_OnTextTooLong.Invoke();
124 validInput = false;
125 }
126 else
127 {
128 // Find invalid characters
129 shortText = FilterSymbolsFromText(text);
130
131 // Invoke wrong input if texts are not same
132 if (shortText != text)
133 {
134 m_wEditBox.SetText(shortText);
135 m_OnInvalidInput.Invoke();
136 validInput = false;
137 }
138 }
139 }
140
141 m_iCharacterCount = length;
142
143 if (validInput)
144 m_OnValidInput.Invoke();
145
146 return false;
147 }
148
149 //------------------------------------------------------------------------------------------------
150 protected bool OnChangeMultiline()
151 {
152 bool validInput = true;
154 return false;
155
156 string text = m_wMultilineEditBox.GetText();
157 int length = text.Length();
158 string shortText = text;
159
160 if (length > 0)
161 {
162 if (length > m_iCharacterLimit)
163 {
164 shortText = text.Substring(0, m_iCharacterLimit);
165 m_wMultilineEditBox.SetText(shortText);
166
167 m_OnTextTooLong.Invoke();
168 validInput = false;
169 }
170 else
171 {
172 // Find invalid characters
173 shortText = FilterSymbolsFromText(text);
174
175 // Invoke wrong input if texts are not same
176 if (shortText != text)
177 {
178 m_wMultilineEditBox.SetText(shortText);
179 m_OnInvalidInput.Invoke();
180 validInput = false;
181 }
182 }
183 }
184
185 m_iCharacterCount = length;
186
187 if (validInput)
188 m_OnValidInput.Invoke();
189
190 return false;
191 }
192
193 //------------------------------------------------------------------------------------------------
195 {
196 return m_iCharacterLimit;
197 }
198
199
200 //------------------------------------------------------------------------------------------------
201 private bool FilterSymbol(string char)
202 {
203 bool found = false;
204 int asciiCode = char.ToAscii();
205
206 if (asciiCode < 0)
207 {
208 if (m_bUTFMultibyte)
209 found = true;
210 }
211 else if (asciiCode >= 32 && asciiCode <= 47)
212 {
213 if (m_bPunctuation)
214 found = true;
215 }
216 else if (asciiCode <= 57)
217 {
218 if (m_bNumbers)
219 found = true;
220 }
221 else
222 {
223 if (m_bASCIIchars)
224 found = true;
225 }
226
227 bool valid;
228 if (found)
229 {
230 // Apply blacklist
231 valid = !SearchWhitelist(asciiCode, true);
232 }
233 else
234 {
235 // Apply whitelist
236 valid = SearchWhitelist(asciiCode, false);
237 }
238
239 return valid;
240 }
241
242 //------------------------------------------------------------------------------------------------
243 private bool SearchWhitelist(int char, bool blacklist)
244 {
245 array<int> list;
246 if (blacklist)
247 list = m_aBlacklist;
248 else
249 list = m_aWhitelist;
250
251 if (list)
252 {
253 foreach (int val : list)
254 {
255 if (val == char)
256 return true;
257 }
258 }
259 return false;
260 }
261
262 //------------------------------------------------------------------------------------------------
265 protected string FilterSymbolsFromText(string text)
266 {
267 string newText = string.Empty;
268
269 // Compare text
270 for (int i = 0; i < text.Length(); i++)
271 {
272 string char = text[i];
273
274 m_bLastInputValid = FilterSymbol(char);
276 continue;
277
278 newText += char;
279 }
280
281 return newText;
282 }
283
284 //------------------------------------------------------------------------------------------------
285 // API
286 //------------------------------------------------------------------------------------------------
287
288 //------------------------------------------------------------------------------------------------
289 void SetCharacterLimit(int limit)
290 {
291 m_iCharacterLimit = limit;
292 }
293
294 //------------------------------------------------------------------------------------------------
295 void SetPunctuation(bool enabled)
296 {
297 m_bPunctuation = enabled;
298 }
299
300 //------------------------------------------------------------------------------------------------
301 void SetNumbers(bool enabled)
302 {
303 m_bNumbers = enabled;
304 }
305
306 //------------------------------------------------------------------------------------------------
307 void SetASCIIchars(bool enabled)
308 {
309 m_bASCIIchars = enabled;
310 }
311
312 //------------------------------------------------------------------------------------------------
313 void SetUTFMultibyte(bool enabled)
314 {
315 m_bUTFMultibyte = enabled;
316 }
317
318 //------------------------------------------------------------------------------------------------
319 void SetCharBlacklist(string list)
320 {
321 m_aBlacklist.Clear();
322
323 int blacklistSize = list.Length();
324 for (int i = 0; i < blacklistSize; i++)
325 {
326 int char = list.Get(i).ToAscii();
327 m_aBlacklist.Insert(char);
328 }
329 }
330
331 //------------------------------------------------------------------------------------------------
332 void SetCharWhiteList(string list)
333 {
334 m_aWhitelist.Clear();
335
336 int whitelistSize = list.Length();
337 for (int i = 0; i < whitelistSize; i++)
338 {
339 int char = list.Get(i).ToAscii();
340 m_aWhitelist.Insert(char);
341 }
342 }
343
344 //------------------------------------------------------------------------------------------------
345 void SetCharacterLimit(string list)
346 {
347 m_sCharWhitelist = list;
348 }
349
350 //------------------------------------------------------------------------------------------------
352 {
353 return m_bLastInputValid;
354 }
355};
override void HandlerAttached(Widget w)
override bool OnChange(Widget w, bool finished)
void SetUTFMultibyte(bool enabled)
MultilineEditBoxWidget m_wMultilineEditBox
ref ScriptInvoker m_OnTextTooLong
void SetCharacterLimit(string list)
ref ScriptInvoker m_OnInvalidInput
void SetPunctuation(bool enabled)
override void HandlerDeattached(Widget w)
string FilterSymbolsFromText(string text)
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
SCR_FieldOfViewSettings Attribute
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134