Skip to content

Commit

Permalink
[Input]: Validate number string when losing focus.
Browse files Browse the repository at this point in the history
  • Loading branch information
coding-jackalope committed Feb 24, 2019
1 parent 3d23407 commit c4298f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
47 changes: 27 additions & 20 deletions Internal/UI/Input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,32 @@ local LastText = ""
local MIN_WIDTH = 150.0
local TEXT_CURSOR_PAD = 3.0

local function GetSelection(Instance)
if Instance ~= nil and TextCursorAnchor >= 0 and TextCursorAnchor ~= TextCursorPos then
local Min = math.min(TextCursorAnchor, TextCursorPos) + 1
local Max = math.max(TextCursorAnchor, TextCursorPos)

return string.sub(Instance.Text, Min, Max)
end
return ""
end

local function IsValidDigit(Instance, Ch)
if Instance ~= nil then
if Instance.NumbersOnly then
if string.match(Ch, "%d") ~= nil then
return true
end

if Ch == "." and string.find(Instance.Text, ".", 1, true) == nil then
return true
if Ch == "." then
local Selected = GetSelection(Instance)
if Selected ~= nil and string.find(Selected, ".", 1, true) ~= nil then
return true
end

if string.find(Instance.Text, ".", 1, true) == nil then
return true
end
end
else
return true
Expand Down Expand Up @@ -252,16 +269,6 @@ local function DrawCursor(Instance, X, Y, W, H)
end
end

local function GetSelection(Instance)
if Instance ~= nil and TextCursorAnchor >= 0 and TextCursorAnchor ~= TextCursorPos then
local Min = math.min(TextCursorAnchor, TextCursorPos) + 1
local Max = math.max(TextCursorAnchor, TextCursorPos)

return string.sub(Instance.Text, Min, Max)
end
return ""
end

local function GetInstance(Id)
for I, V in ipairs(Instances) do
if V.Id == Id then
Expand Down Expand Up @@ -339,8 +346,6 @@ function Input.Begin(Id, Options)
LastFocused = nil
end

local ValidateNumbersOnly = false

if Instance == Focused then
local Back = false
local ShouldDelete = false
Expand Down Expand Up @@ -437,7 +442,6 @@ function Input.Begin(Id, Options)
if Keyboard.IsPressed('return') then
Result = true
ClearFocus = true
ValidateNumbersOnly = true
end

if Instance.TextChanged or Back then
Expand All @@ -452,11 +456,7 @@ function Input.Begin(Id, Options)
TextCursorAnchor = -1
end
else
ValidateNumbersOnly = true
end

if ValidateNumbersOnly then
if Options.NumbersOnly and (Instance.Text == "" or Instance.Text == ".") then
if Instance.NumbersOnly and (Instance.Text == "" or Instance.Text == ".") then
Instance.Text = "0"
end
end
Expand Down Expand Up @@ -498,6 +498,13 @@ function Input.Begin(Id, Options)
Window.AddItem(X, Y, W, H, WinItemId)

if ClearFocus then
if Instance.NumbersOnly then
local Value = tonumber(Instance.Text)
if Value ~= nil then
Instance.Text = tostring(Value)
end
end

LastText = Instance.Text
Focused = nil
end
Expand Down
2 changes: 0 additions & 2 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ Style: Light theme.
Style: Push/Pop fonts.
Style: Add documentation for each style property.
Cursor: Expose cursor API to slab.
Input: Validate no leading or trailing zeroes.
Input: Validate digit before . separator.
Input: Double click to select word.
Input: Multi-line input control.
SlabDebug: Show frame stats.
Expand Down

0 comments on commit c4298f2

Please sign in to comment.