Skip to content

Commit

Permalink
Added support for setting the text of an input node directly
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Jul 9, 2018
1 parent 3faed04 commit a11f3f8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ The state table contains the following fields:
gooey.button("button/bg", action_id, action, on_pressed, update_button)
end

**INITIAL STATE**
It is possible to set the initial state of a button:
**STATE**
It is possible to set the state of a button:

update_button(gooey.button("button/bg").set_visible(false))

Expand Down Expand Up @@ -160,8 +160,8 @@ The state table contains the following fields:
gooey.checkbox("checkbox/bg", action_id, action, on_checked, update_checkbox)
end

**INITIAL STATE**
It is possible to set the initial state of a checkbox:
**STATE**
It is possible to set the state of a checkbox. This is good for setting the initial state of the checkbox:

update_checkbox(gooey.checkbox("checkbox/bg").check())
update_checkbox(gooey.radio("checkbox/bg").set_visible(false))
Expand Down Expand Up @@ -223,8 +223,8 @@ The state table contains the following fields:
end), update_radio)
end

**INITIAL STATE**
It is possible to set the initial state of a radiobutton:
**STATE**
It is possible to set the state of a radiobutton. This is good for setting the initial state of the radiobutton:

update_radio(gooey.radio("radio1/bg").set_selected(true))
update_radio(gooey.radio("radio1/bg").set_visible(false))
Expand Down Expand Up @@ -399,6 +399,12 @@ The state table contains the following fields:
gooey.input("input/text", gui.KEYBOARD_TYPE_DEFAULT, action_id, action, nil, update_input)
end

**STATE**
It is possible to set the state of an input node:

update_input(gooey.input("input/text", gui.KEYBOARD_TYPE_DEFAULT).set_visible(false))
update_input(gooey.input("input/text", gui.KEYBOARD_TYPE_DEFAULT).set_text("foobar"))


## Consuming input
Each Gooey component has a ```consumed``` variable in its state stable. Consuming input in a gui_script to prevent input propagation can be done by checking the consumed state of each component. If any component has ```consumed``` set to true it is safe to return true from the ```on_input()``` function to prevent input propagation. It is also possible to wrap all component interaction in an input group and check the consumed state for the entire group:
Expand Down
4 changes: 3 additions & 1 deletion example/rpg.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function init(self)
self.list_data = {
"Legolas", "Bilbo", "Frodo", "Gandalf", "Gimli", "Aragorn"
}
rpg.input("input", gui.KEYBOARD_TYPE_DEFAULT, nil, nil, { empty_text = "DEFAULT TEXT" })
rpg.static_list("static_list", self.list_ids)
rpg.dynamic_list("dynamic_list", self.list_data)
end
Expand Down Expand Up @@ -46,9 +47,10 @@ function on_input(self, action_id, action)

rpg.dynamic_list("dynamic_list", self.list_data, action_id, action, function(list)
print("selected dynamic list item", list.selected_item)
rpg.input("input", gui.KEYBOARD_TYPE_DEFAULT).set_text(self.list_data[list.selected_item])
end)

rpg.input("input", gui.KEYBOARD_TYPE_DEFAULT, action_id, action)
rpg.input("input", gui.KEYBOARD_TYPE_DEFAULT, action_id, action, { empty_text = "DEFAULT TEXT" })
end)
return group.consumed
end
59 changes: 36 additions & 23 deletions gooey/internal/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,36 @@ local INPUT = {
set_visible = function(input, visible)
gui.set_enabled(input.node, visible)
end,
set_text = function(input, text)
input.text = text

-- only update the text if it has changed
local current_text = input.text .. input.marked_text
if current_text ~= input.current_text then
input.current_text = current_text

-- mask text if password field
if input.keyboard_type == gui.KEYBOARD_TYPE_PASSWORD then
input.masked_text = M.mask_text(input.text, "*")
input.masked_marked_text = M.mask_text(input.marked_text, "*")
else
input.masked_text = nil
input.masked_marked_text = nil
end

-- text + marked text
local text = input.masked_text or input.text
local marked_text = input.masked_marked_text or input.marked_text
input.empty = #text == 0 and #marked_text == 0

-- measure it
input.text_width = get_text_width(input.node, text)
input.marked_text_width = get_text_width(input.node, marked_text)
input.total_width = input.text_width + input.marked_text_width

gui.set_text(input.node, text .. marked_text)
end
end,
}

function M.input(node_id, keyboard_type, action_id, action, config, refresh_fn)
Expand All @@ -74,14 +104,15 @@ function M.input(node_id, keyboard_type, action_id, action, config, refresh_fn)
input.node = node
input.refresh_fn = refresh_fn

input.text = input.text or ""
input.marked_text = input.marked_text or ""
input.keyboard_type = keyboard_type

if not action then
input.empty = #input.text == 0 and #input.marked_text == 0
input.refresh()
return input
end

input.text = input.text or ""
input.marked_text = input.marked_text or ""
input.keyboard_type = keyboard_type

core.clickable(input, action_id, action)

Expand Down Expand Up @@ -130,27 +161,9 @@ function M.input(node_id, keyboard_type, action_id, action, config, refresh_fn)
end
input.text = string.sub(input.text, 1, string.len(input.text) - last_s)
end

if keyboard_type == gui.KEYBOARD_TYPE_PASSWORD then
input.masked_text = M.mask_text(input.text, "*")
input.masked_marked_text = M.mask_text(input.marked_text, "*")
else
input.masked_text = nil
input.masked_marked_text = nil
end
end

local text = input.masked_text or input.text
local marked_text = input.masked_marked_text or input.marked_text
input.empty = #text == 0 and #marked_text == 0

input.text_width = get_text_width(input.node, text)
input.marked_text_width = get_text_width(input.node, marked_text)
input.total_width = input.text_width + input.marked_text_width

if input.selected then
gui.set_text(input.node, text .. marked_text)
end
input.set_text(input.text)
end
if refresh_fn then refresh_fn(input) end
return input
Expand Down

0 comments on commit a11f3f8

Please sign in to comment.