From 9410989d5841af5962a2c43fa3422ab64e0c0fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ritzl?= Date: Mon, 14 Mar 2022 08:03:25 +0100 Subject: [PATCH] Added selected_now and deselected_now to input Fixes #70 --- README.md | 2 ++ gooey/internal/input.lua | 8 +++++++- test/test_input.lua | 12 ++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b11e90b..3d60efe 100644 --- a/README.md +++ b/README.md @@ -524,6 +524,8 @@ The state table contains the following fields: * ```pressed_now``` (boolean) - true if the text field was pressed this call * ```long_pressed``` (boolean) - true if the registered press was a long press or not * ```released_now``` (boolean) - true if the text field was released this call +* ```selected_now``` (boolean) - true if the text field was selected this call +* ```deselected_now``` (boolean) - true if the text field was deselected this call * ```text``` (string) - The text in the field * ```marked_text``` (string) - The marked (non-committed) text * ```keyboard_type``` (number) diff --git a/gooey/internal/input.lua b/gooey/internal/input.lua index 222348a..eaabf1e 100644 --- a/gooey/internal/input.lua +++ b/gooey/internal/input.lua @@ -119,7 +119,9 @@ function M.input(node_id, keyboard_type, action_id, action, config, refresh_fn) input.node = node input.node_id = node_id input.refresh_fn = refresh_fn - + input.deselected_now = false + input.selected_now = false + local use_marked_text = (config and config.use_marked_text == nil) and true or (config and config.use_marked_text) input.text = input.text or "" .. (not use_marked_text and input.marked_text or "") input.marked_text = input.marked_text or "" @@ -136,12 +138,16 @@ function M.input(node_id, keyboard_type, action_id, action, config, refresh_fn) if input.enabled then input.deselected_now = false if input.released_now then + if not input.selected then + input.selected_now = true + end input.selected = true input.marked_text = "" gui.reset_keyboard() gui.show_keyboard(keyboard_type, true) elseif input.selected and action.pressed and action_id == actions.TOUCH and not input.over then input.selected = false + input.deselected_now = true input.text = input.text .. (not use_marked_text and input.marked_text or "") input.marked_text = "" gui.hide_keyboard() diff --git a/test/test_input.lua b/test/test_input.lua index cdeaf25..b0d84f8 100644 --- a/test/test_input.lua +++ b/test/test_input.lua @@ -57,6 +57,18 @@ return function() assert(input.text == "fooba") end) + test("it should be possible to know if input was selected or deselected now", function() + local node = gui.new_text_node(vmath.vector3(10, 10, 0), "") + gui.set_id(node, "text") + + gooey.input("text", gui.KEYBOARD_TYPE_PASSWORD, action_ids.TOUCH, actions.pressed(10, 10)) + local input = gooey.input("text", gui.KEYBOARD_TYPE_PASSWORD, action_ids.TOUCH, actions.released(10, 10)) + assert(input.selected_now) + + local input = gooey.input("text", gui.KEYBOARD_TYPE_PASSWORD, action_ids.TOUCH, actions.pressed(1000, 1000)) + assert(input.deselected_now) + end) + test("it should mask entered text if it is a password", function() local node = gui.new_text_node(vmath.vector3(10, 10, 0), "") gui.set_id(node, "text")