Skip to content

Commit

Permalink
Added config table to inout fields
Browse files Browse the repository at this point in the history
Support for max_length config value.
  • Loading branch information
britzl committed Dec 9, 2017
1 parent aa87484 commit 1a53b23
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,21 @@ The state table contains the following fields:
end))
end

### gooey.input(node_id, keyboard_type, action_id, action)
### gooey.input(node_id, keyboard_type, action_id, action, config)
Perform input and state handling for a text input field

**PARAMETERS**
* ```node_id``` (string|hash) - Id of the text node
* ```keyboard_type``` (number) - Keyboard type from gui.KEYBOARD_TYPE_*
* ```action_id``` (hash) - Action id as received from on_input()
* ```action``` (table) - Action as received from on_input()
* ```config``` (table) - Optional configuration values.

The configuration table accepts the following values:

* ```max_length``` (number) - Maximum length of entered text

NOTE: ```max_length``` currently measures string length in bytes so the value will be misleading for any characters longer than a single byte.

**RETURN**
* ```input``` (table) - State data for the input field based on current and previous input actions
Expand Down
4 changes: 3 additions & 1 deletion example/dirtylarry.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local dirtylarry = require "gooey.themes.dirtylarry.dirtylarry"

function init(self)
dirtylarry.acquire_input()

dirtylarry.input("input", gui.KEYBOARD_TYPE_DEFAULT, nil, { x=0, y=0 }, { empty_text = "EMPTY, MAX 8 CHARS" })
end

function on_input(self, action_id, action)
Expand Down Expand Up @@ -30,5 +32,5 @@ function on_input(self, action_id, action)

end)

local input = dirtylarry.input("input", gui.KEYBOARD_TYPE_DEFAULT, action_id, action, "EMPTY")
local input = dirtylarry.input("input", gui.KEYBOARD_TYPE_DEFAULT, action_id, action, { empty_text = "EMPTY, MAX 8 CHARS", max_length = 8 })
end
4 changes: 2 additions & 2 deletions example/kenneyblue.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function init(self)
"listitem1/bg", "listitem2/bg", "listitem3/bg", "listitem4/bg", "listitem5/bg",
"listitem6/bg", "listitem7/bg"
}
kenneyblue.input("input", gui.KEYBOARD_TYPE_DEFAULT, nil, { x=0, y=0 }, "EMPTY TEXT")
kenneyblue.input("input", gui.KEYBOARD_TYPE_DEFAULT, nil, { x=0, y=0 }, { empty_text = "EMPTY, MAX 8 CHARS" })
end

function on_input(self, action_id, action)
Expand Down Expand Up @@ -42,7 +42,7 @@ function on_input(self, action_id, action)
print("selected list item", list.selected_item)
end)

kenneyblue.input("input", gui.KEYBOARD_TYPE_DEFAULT, action_id, action, "EMPTY TEXT")
kenneyblue.input("input", gui.KEYBOARD_TYPE_DEFAULT, action_id, action, { empty_text = "EMPTY, MAX 8 CHARS", max_length = 8 })
end

function on_reload(self)
Expand Down
14 changes: 12 additions & 2 deletions gooey/gooey.lua
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,15 @@ function M.list(root_id, item_ids, action_id, action, fn)
return list
end

-- from dirty larry with modifications
function M.input(node_id, keyboard_type, action_id, action)
--- Input text
-- (from dirty larry with modifications)
-- @param node_id Id of a text node
-- @param keyboard_type Keyboard type to use (from gui.KEYBOARD_TYPE_*)
-- @param action_id
-- @param action
-- @param config Optional config table. Accepted values
-- * max_length (number) - Maximum number of characters that can be entered
function M.input(node_id, keyboard_type, action_id, action, config)
node_id = to_hash(node_id)
local node = gui.get_node(node_id)
assert(node)
Expand Down Expand Up @@ -341,6 +348,9 @@ function M.input(node_id, keyboard_type, action_id, action)
-- new raw text input
if action_id == M.TEXT then
input.text = input.text .. action.text
if config and config.max_length then
input.text = input.text:sub(1, config.max_length)
end
input.marked_text = ""
-- new marked text input (uncommitted text)
elseif action_id == M.MARKEDTEXT then
Expand Down
6 changes: 3 additions & 3 deletions gooey/themes/dirtylarry/dirtylarry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ function M.radio(node_id, group_id, action_id, action, fn)
end


function M.input(node_id, keyboard_type, action_id, action, empty_text)
local input = gooey.input(node_id .. "/text", keyboard_type, action_id, action)
function M.input(node_id, keyboard_type, action_id, action, config)
local input = gooey.input(node_id .. "/text", keyboard_type, action_id, action, config)

if input.empty and not input.selected then
gui.set_text(input.node, empty_text)
gui.set_text(input.node, config and config.empty_text or "")
end

local cursor = gui.get_node(node_id .. "/cursor")
Expand Down
6 changes: 3 additions & 3 deletions gooey/themes/kenneyblue/kenneyblue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ function M.radio(node_id, group_id, action_id, action, fn)
end


function M.input(node_id, keyboard_type, action_id, action, empty_text)
local input = gooey.input(node_id .. "/text", keyboard_type, action_id, action)
function M.input(node_id, keyboard_type, action_id, action, config)
local input = gooey.input(node_id .. "/text", keyboard_type, action_id, action, config)

if input.selected_now then
gui.play_flipbook(gui.get_node(node_id .. "/bg"), hash("blue_button05"))
Expand All @@ -90,7 +90,7 @@ function M.input(node_id, keyboard_type, action_id, action, empty_text)
end

if input.empty and not input.selected then
gui.set_text(input.node, empty_text)
gui.set_text(input.node, config and config.empty_text or "")
end

local cursor = gui.get_node(node_id .. "/cursor")
Expand Down

0 comments on commit 1a53b23

Please sign in to comment.