This repository has been archived by the owner on Jun 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
LabeledTextInput.lua
122 lines (99 loc) · 3.63 KB
/
LabeledTextInput.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
----------------------------------------
--
-- LabeledTextInput.lua
--
-- Creates a frame containing a label and a text input control.
--
----------------------------------------
GuiUtilities = require(script.Parent.GuiUtilities)
local kTextInputWidth = 100
local kTextBoxInternalPadding = 4
LabeledTextInputClass = {}
LabeledTextInputClass.__index = LabeledTextInputClass
function LabeledTextInputClass.new(nameSuffix, labelText, defaultValue)
local self = {}
setmetatable(self, LabeledTextInputClass)
-- Note: we are using "graphemes" instead of characters.
-- In modern text-manipulation-fu, what with internationalization,
-- emojis, etc, it's not enough to count characters, particularly when
-- concerned with "how many <things> am I rendering?".
-- We are using the
self._MaxGraphemes = 10
self._valueChangedFunction = nil
local defaultValue = defaultValue or ""
local frame = GuiUtilities.MakeStandardFixedHeightFrame('TextInput ' .. nameSuffix)
self._frame = frame
local label = GuiUtilities.MakeStandardPropertyLabel(labelText)
label.Parent = frame
self._label = label
self._value = defaultValue
-- Dumb hack to add padding to text box,
local textBoxWrapperFrame = Instance.new("Frame")
textBoxWrapperFrame.Name = "Wrapper"
textBoxWrapperFrame.Size = UDim2.new(0, kTextInputWidth, 0.6, 0)
textBoxWrapperFrame.Position = UDim2.new(0, GuiUtilities.StandardLineElementLeftMargin, .5, 0)
textBoxWrapperFrame.AnchorPoint = Vector2.new(0, .5)
textBoxWrapperFrame.Parent = frame
GuiUtilities.syncGuiElementInputFieldColor(textBoxWrapperFrame)
GuiUtilities.syncGuiElementBorderColor(textBoxWrapperFrame)
local textBox = Instance.new("TextBox")
textBox.Parent = textBoxWrapperFrame
textBox.Name = "TextBox"
textBox.Text = defaultValue
textBox.Font = Enum.Font.SourceSans
textBox.TextSize = 15
textBox.BackgroundTransparency = 1
textBox.TextXAlignment = Enum.TextXAlignment.Left
textBox.Size = UDim2.new(1, -kTextBoxInternalPadding, 1, GuiUtilities.kTextVerticalFudge)
textBox.Position = UDim2.new(0, kTextBoxInternalPadding, 0, 0)
textBox.ClipsDescendants = true
GuiUtilities.syncGuiElementFontColor(textBox)
textBox:GetPropertyChangedSignal("Text"):connect(function()
-- Never let the text be too long.
-- Careful here: we want to measure number of graphemes, not characters,
-- in the text, and we want to clamp on graphemes as well.
if (utf8.len(self._textBox.Text) > self._MaxGraphemes) then
local count = 0
for start, stop in utf8.graphemes(self._textBox.Text) do
count = count + 1
if (count > self._MaxGraphemes) then
-- We have gone one too far.
-- clamp just before the beginning of this grapheme.
self._textBox.Text = string.sub(self._textBox.Text, 1, start-1)
break
end
end
-- Don't continue with rest of function: the resetting of "Text" field
-- above will trigger re-entry. We don't need to trigger value
-- changed function twice.
return
end
self._value = self._textBox.Text
if (self._valueChangedFunction) then
self._valueChangedFunction(self._value)
end
end)
self._textBox = textBox
return self
end
function LabeledTextInputClass:SetValueChangedFunction(vcf)
self._valueChangedFunction = vcf
end
function LabeledTextInputClass:GetFrame()
return self._frame
end
function LabeledTextInputClass:GetValue()
return self._value
end
function LabeledTextInputClass:GetMaxGraphemes()
return self._MaxGraphemes
end
function LabeledTextInputClass:SetMaxGraphemes(newValue)
self._MaxGraphemes = newValue
end
function LabeledTextInputClass:SetValue(newValue)
if self._value ~= newValue then
self._textBox.Text = newValue
end
end
return LabeledTextInputClass