Skip to content

Commit a113a97

Browse files
committed
Added filter field to TextField
1 parent e03b3ff commit a113a97

2 files changed

Lines changed: 47 additions & 10 deletions

File tree

penguingui.lua

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,7 @@ TextField.defaultTextColor = "#333333"
15841584
TextField.defaultTextHoverColor = "#777777"
15851585
TextField.cursorColor = "white"
15861586
TextField.cursorRate = 1
1587+
TextField.filter = nil
15871588

15881589

15891590
function TextField:_init(x, y, width, height, defaultText)
@@ -1757,15 +1758,26 @@ function TextField:keyEvent(keyCode, pressed)
17571758
local text = self.text
17581759
local cursorPos = self.cursorPosition
17591760

1761+
local filter = self.filter
17601762
if #key == 1 then -- Type a character
1761-
self.text = text:sub(1, cursorPos) .. key .. text:sub(cursorPos + 1)
1762-
if text ~= self.text then
1763-
self:setCursorPosition(cursorPos + 1)
1763+
text = text:sub(1, cursorPos) .. key .. text:sub(cursorPos + 1)
1764+
if filter then
1765+
if not text:match(filter) then
1766+
return true
1767+
end
17641768
end
1769+
self.text = text
1770+
self:setCursorPosition(cursorPos + 1)
17651771
else -- Special character
17661772
if key == "backspace" then
17671773
if cursorPos > 0 then
1768-
self.text = text:sub(1, cursorPos - 1) .. text:sub(cursorPos + 1)
1774+
text = text:sub(1, cursorPos - 1) .. text:sub(cursorPos + 1)
1775+
if filter then
1776+
if not text:match(filter) then
1777+
return true
1778+
end
1779+
end
1780+
self.text = text
17691781
self:setCursorPosition(cursorPos - 1)
17701782
end
17711783
elseif key == "enter" then
@@ -1774,7 +1786,13 @@ function TextField:keyEvent(keyCode, pressed)
17741786
end
17751787
elseif key == "delete" then
17761788
if cursorPos < #text then
1777-
self.text = text:sub(1, cursorPos) .. text:sub(cursorPos + 2)
1789+
text = text:sub(1, cursorPos) .. text:sub(cursorPos + 2)
1790+
if filter then
1791+
if not text:match(filter) then
1792+
return true
1793+
end
1794+
end
1795+
self.text = text
17781796
end
17791797
elseif key == "right" then
17801798
self:setCursorPosition(math.min(cursorPos + 1, #text))

penguingui/TextField.lua

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ TextField.defaultTextHoverColor = "#777777"
2525
TextField.cursorColor = "white"
2626
--- The period of the cursor's blinking.
2727
TextField.cursorRate = 1
28+
--- The filter pattern to restrict this TextField's text to, or nil if none.
29+
TextField.filter = nil
2830

2931
--- Constructor
3032
-- @section
@@ -220,15 +222,26 @@ function TextField:keyEvent(keyCode, pressed)
220222
local text = self.text
221223
local cursorPos = self.cursorPosition
222224

225+
local filter = self.filter
223226
if #key == 1 then -- Type a character
224-
self.text = text:sub(1, cursorPos) .. key .. text:sub(cursorPos + 1)
225-
if text ~= self.text then
226-
self:setCursorPosition(cursorPos + 1)
227+
text = text:sub(1, cursorPos) .. key .. text:sub(cursorPos + 1)
228+
if filter then
229+
if not text:match(filter) then
230+
return true
231+
end
227232
end
233+
self.text = text
234+
self:setCursorPosition(cursorPos + 1)
228235
else -- Special character
229236
if key == "backspace" then
230237
if cursorPos > 0 then
231-
self.text = text:sub(1, cursorPos - 1) .. text:sub(cursorPos + 1)
238+
text = text:sub(1, cursorPos - 1) .. text:sub(cursorPos + 1)
239+
if filter then
240+
if not text:match(filter) then
241+
return true
242+
end
243+
end
244+
self.text = text
232245
self:setCursorPosition(cursorPos - 1)
233246
end
234247
elseif key == "enter" then
@@ -237,7 +250,13 @@ function TextField:keyEvent(keyCode, pressed)
237250
end
238251
elseif key == "delete" then
239252
if cursorPos < #text then
240-
self.text = text:sub(1, cursorPos) .. text:sub(cursorPos + 2)
253+
text = text:sub(1, cursorPos) .. text:sub(cursorPos + 2)
254+
if filter then
255+
if not text:match(filter) then
256+
return true
257+
end
258+
end
259+
self.text = text
241260
end
242261
elseif key == "right" then
243262
self:setCursorPosition(math.min(cursorPos + 1, #text))

0 commit comments

Comments
 (0)