Skip to content

Commit

Permalink
Aded support for allowed characters
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
britzl committed Feb 7, 2019
1 parent 2ebf981 commit d9d5621
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ Perform input and state handling for a text input field
The configuration table accepts the following values:

* ```max_length``` (number) - Maximum length of entered text
* ```empty_text``` (string) - Text to show if the text field is empty
* ```allowed_characters``` (string) - Lua pattern to filter allowed characters (eg "[%a%d]" for alpha numeric)

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

Expand Down
213 changes: 213 additions & 0 deletions example/dirtylarry.gui
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,219 @@ nodes {
template_node_child: true
size_mode: SIZE_MODE_MANUAL
}
nodes {
position {
x: -2.317
y: -40.381
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_TEMPLATE
id: "input_alphanumeric"
parent: "buttons"
layer: ""
inherit_alpha: true
alpha: 1.0
template: "/gooey/themes/dirtylarry/components/input.gui"
template_node_child: false
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 400.0
y: 64.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: "dirtylarry/input"
id: "input_alphanumeric/bg"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_W
adjust_mode: ADJUST_MODE_FIT
parent: "input_alphanumeric"
layer: "below"
inherit_alpha: true
slice9 {
x: 12.0
y: 12.0
z: 12.0
w: 15.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: true
size_mode: SIZE_MODE_MANUAL
}
nodes {
position {
x: 12.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "<text>"
font: "dirtylarry"
id: "input_alphanumeric/text"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_W
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "input_alphanumeric/bg"
layer: "text"
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: true
text_leading: 1.0
text_tracking: 0.0
}
nodes {
position {
x: 12.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 2.0
y: 40.0
z: 0.0
w: 1.0
}
color {
x: 0.5019608
y: 0.5019608
z: 0.5019608
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "input_alphanumeric/cursor"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
parent: "input_alphanumeric/bg"
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: true
size_mode: SIZE_MODE_MANUAL
}
layers {
name: "below"
}
Expand Down
6 changes: 4 additions & 2 deletions example/dirtylarry.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function init(self)
dirtylarry.checkbox("checkbox").set_checked(true)
dirtylarry.radio("radio2").set_selected(true)
dirtylarry.input("input", gui.KEYBOARD_TYPE_DEFAULT, nil, nil, { empty_text = "EMPTY, MAX 8 CHARS" })

dirtylarry.input("input_alphanumeric", gui.KEYBOARD_TYPE_DEFAULT, nil, nil, { empty_text = "ALPHA NUMERIC CHARS" })

self.list_data = { "Sherlock", "Poirot", "Magnum", "Miss Marple", "Morse", "Columbo" }
dirtylarry.dynamic_list("dynamiclist", self.list_data)
end
Expand Down Expand Up @@ -38,7 +39,8 @@ function on_input(self, action_id, action)
end)

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

dirtylarry.input("input_alphanumeric", gui.KEYBOARD_TYPE_DEFAULT, action_id, action, { empty_text = "ALPHA NUMERIC CHARS", max_length = 8, allowed_characters = "[%a%d%s]" })

dirtylarry.dynamic_list("dynamiclist", self.list_data, action_id, action, function(list)
print("selected item", list.selected)
end)
Expand Down
2 changes: 2 additions & 0 deletions gooey/gooey.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ end
-- @param action_id
-- @param action
-- @param config Optional config table. Accepted values
-- * empty_text (string) - Text to show when the field is empty
-- * max_length (number) - Maximum number of characters that can be entered
-- * allowed_characters (string) - Lua pattern to filter which characters to accept
-- @return Component state
function M.input(node_id, keyboard_type, action_id, action, config, refresh_fn)
core.TOUCH = M.TOUCH
Expand Down
8 changes: 5 additions & 3 deletions gooey/internal/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ function M.input(node_id, keyboard_type, action_id, action, config, refresh_fn)
end)
-- ignore arrow keys
if not string.match(hex, "EF9C8[0-3]") then
input.text = input.text .. action.text
if config and config.max_length then
input.text = input.text:sub(1, config.max_length)
if not config.allowed_characters or action.text:match(config.allowed_characters) then
input.text = input.text .. action.text
if config and config.max_length then
input.text = input.text:sub(1, config.max_length)
end
end
input.marked_text = ""
end
Expand Down

0 comments on commit d9d5621

Please sign in to comment.