7 [
Attribute(
"500", UIWidgets.EditBox,
"Set maximum character limit of the editbox")]
8 private int m_iCharacterLimit;
10 [
Attribute(
"true", UIWidgets.CheckBox,
"Allow punctuation character, like space, brackets, ,./@#$%^ etc")]
11 private bool m_bPunctuation;
13 [
Attribute(
"true", UIWidgets.CheckBox,
"Allow numerical characters")]
14 private bool m_bNumbers;
16 [
Attribute(
"true", UIWidgets.CheckBox,
"Allow basic set of characters")]
17 private bool m_bASCIIchars;
19 [
Attribute(
"true", UIWidgets.CheckBox,
"Allow UTF-8 multibyte symbols - including special symbols and diacritics")]
20 private bool m_bUTFMultibyte;
22 [
Attribute(
"", UIWidgets.EditBox,
"Explicitly blacklisted characters, write without spaces or or commas")]
23 private string m_sCharBlacklist;
25 [
Attribute(
"", UIWidgets.EditBox,
"Explicitly whitelisted characters, write without spaces or or commas")]
26 private string m_sCharWhitelist;
28 private ref array<int> m_aBlacklist =
new array<int>;
29 private ref array<int> m_aWhitelist =
new array<int>;
31 private EditBoxWidget m_wEditBox;
33 private int m_iCharacterCount;
35 protected bool m_bLastInputValid =
true;
37 ref ScriptInvoker m_OnInvalidInput =
new ScriptInvoker();
38 ref ScriptInvoker m_OnTextTooLong =
new ScriptInvoker();
39 ref ScriptInvoker m_OnValidInput =
new ScriptInvoker();
42 override void HandlerAttached(Widget w)
44 super.HandlerAttached(w);
46 m_wEditBox = EditBoxWidget.Cast(w);
50 Print(
"EditBoxFilterComponent used on invalid widget type.", LogLevel.WARNING);
54 int blacklistSize = m_sCharBlacklist.Length();
55 for (
int i = 0; i < blacklistSize; i++)
57 int char = m_sCharBlacklist.Get(i).ToAscii();
58 m_aBlacklist.Insert(
char);
61 int whitelistSize = m_sCharWhitelist.Length();
62 for (
int i = 0; i < whitelistSize; i++)
64 int char = m_sCharWhitelist.Get(i).ToAscii();
65 m_aWhitelist.Insert(
char);
70 override void HandlerDeattached(Widget w)
77 override bool OnChange(Widget w,
int x,
int y,
bool finished)
79 bool validInput =
true;
83 string text = m_wEditBox.GetText();
84 int length = text.Length();
85 string shortText = text;
89 if (length > m_iCharacterLimit)
91 shortText = text.Substring(0, m_iCharacterLimit);
92 m_wEditBox.SetText(shortText);
94 m_OnTextTooLong.Invoke();
100 shortText = FilterSymbolsFromText(text);
103 if (shortText != text)
105 m_wEditBox.SetText(shortText);
106 m_OnInvalidInput.Invoke();
112 m_iCharacterCount = length;
115 m_OnValidInput.Invoke();
121 int GetNumLimitOfCharacters()
123 return m_iCharacterLimit;
128 private bool FilterSymbol(
string char)
131 int asciiCode =
char.ToAscii();
138 else if (asciiCode >= 32 && asciiCode <= 47)
143 else if (asciiCode <= 57)
158 valid = !SearchWhitelist(asciiCode,
true);
163 valid = SearchWhitelist(asciiCode,
false);
170 private bool SearchWhitelist(
int char,
bool blacklist)
180 foreach (
int val : list)
192 protected string FilterSymbolsFromText(
string text)
194 string newText =
string.Empty;
197 for (
int i = 0; i < text.Length(); i++)
199 string char = text[i];
201 m_bLastInputValid = FilterSymbol(
char);
202 if (!m_bLastInputValid)
216 void SetCharacterLimit(
int limit)
218 m_iCharacterLimit = limit;
222 void SetPunctuation(
bool enabled)
224 m_bPunctuation = enabled;
228 void SetNumbers(
bool enabled)
230 m_bNumbers = enabled;
234 void SetASCIIchars(
bool enabled)
236 m_bASCIIchars = enabled;
240 void SetUTFMultibyte(
bool enabled)
242 m_bUTFMultibyte = enabled;
246 void SetCharBlacklist(
string list)
248 m_aBlacklist.Clear();
250 int blacklistSize = list.Length();
251 for (
int i = 0; i < blacklistSize; i++)
253 int char = list.Get(i).ToAscii();
254 m_aBlacklist.Insert(
char);
259 void SetCharacterLimit(
string list)
261 m_sCharWhitelist = list;
265 bool IsLastInputValid()
267 return m_bLastInputValid;