diff --git a/.luacheckrc b/.luacheckrc index 82ec63c..063fc09 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -45,8 +45,12 @@ globals = { "debug", "timer", "window", - "buffer", + "buffer", "resource", "defos", "html5", + "describe", + "before", + "after", + "it", } diff --git a/docs_md/changelog.md b/docs_md/changelog.md index a442d6b..3f66067 100644 --- a/docs_md/changelog.md +++ b/docs_md/changelog.md @@ -445,4 +445,4 @@ And yeah, the new **Druid** logo is here! - **#189** [System] Add optional flag to `component:set_input_priority` to mark it as temporary. It will reset to default input priority after the `component:reset_input_priority` - **#204** [System] Fix: wrong code example link, if open example from direct URL - **#202** [System] Enabled stencil check to true by default. To disable this, use `druid.no_stencil_check` in game.project settings -- [Examples] Add layout, layout fit, progres bar, data list + component examples +- [Examples] Add layout, layout fit, progress bar, data list + component examples diff --git a/druid/base/button.lua b/druid/base/button.lua index 3b18328..6514811 100755 --- a/druid/base/button.lua +++ b/druid/base/button.lua @@ -145,7 +145,7 @@ local function on_button_release(self) self.can_action = false local time = socket.gettime() - local is_long_click = (time - self.last_pressed_time) > self.style.LONGTAP_TIME + local is_long_click = (time - self.last_pressed_time) >= self.style.LONGTAP_TIME is_long_click = is_long_click and self.on_long_click:is_exist() local is_double_click = (time - self.last_released_time) < self.style.DOUBLETAP_TIME @@ -248,6 +248,10 @@ function Button.on_input(self, action_id, action) return false end + if not self:is_enabled() then + return false + end + local is_pick = true local is_key_trigger = (action_id == self.key_trigger) if not is_key_trigger then @@ -280,7 +284,7 @@ function Button.on_input(self, action_id, action) -- While hold button, repeat rate pick from input.repeat_interval if action.repeated then - if not self.disabled and self.on_repeated_click:is_exist() and self.can_action then + if self.on_repeated_click:is_exist() and self.can_action then on_button_repeated_click(self) return true end @@ -290,7 +294,7 @@ function Button.on_input(self, action_id, action) return on_button_release(self) end - if not self.disabled and self.can_action and self.on_long_click:is_exist() then + if self.can_action and self.on_long_click:is_exist() then local press_time = socket.gettime() - self.last_pressed_time if self.style.AUTOHOLD_TRIGGER <= press_time then diff --git a/druid/base/drag.lua b/druid/base/drag.lua index cd0a50f..2667ca4 100644 --- a/druid/base/drag.lua +++ b/druid/base/drag.lua @@ -63,6 +63,7 @@ local function start_touch(self, touch) self.x = touch.x self.y = touch.y + self._scene_scale = helper.get_scene_scale(self.node) self.on_touch_start:trigger(self:get_context()) end @@ -155,9 +156,11 @@ end -- or create your own style -- @table style -- @tfield[opt=10] number DRAG_DEADZONE Distance in pixels to start dragging +-- @tfield[opt=false] boolean NO_USE_SCREEN_KOEF If screen aspect ratio affects on drag values function Drag.on_style_change(self, style) self.style = {} self.style.DRAG_DEADZONE = style.DRAG_DEADZONE or 10 + self.style.NO_USE_SCREEN_KOEF = style.NO_USE_SCREEN_KOEF or false end @@ -181,6 +184,8 @@ function Drag.init(self, node, on_drag_callback) self.can_x = true self.can_y = true + self._scene_scale = helper.get_scene_scale(self.node) + self.click_zone = nil self.on_touch_start = Event() self.on_touch_end = Event() @@ -206,6 +211,7 @@ function Drag.on_window_resized(self) local x_koef, y_koef = helper.get_screen_aspect_koef() self._x_koef = x_koef self._y_koef = y_koef + self._scene_scale = helper.get_scene_scale(self.node) end @@ -229,7 +235,6 @@ function Drag.on_input(self, action_id, action) if self.click_zone then is_pick = is_pick and gui.pick_node(self.click_zone, action.x, action.y) end - if not is_pick and not self.is_drag then end_touch(self) return false @@ -278,11 +283,16 @@ function Drag.on_input(self, action_id, action) end if self.is_drag then + local x_koef, y_koef = self._x_koef, self._y_koef + if self.style.NO_USE_SCREEN_KOEF then + x_koef, y_koef = 1, 1 + end + self.on_drag:trigger(self:get_context(), - self.dx * self._x_koef, - self.dy * self._y_koef, - (self.x - self.touch_start_pos.x) * self._x_koef, - (self.y - self.touch_start_pos.y) * self._y_koef) + self.dx * x_koef / self._scene_scale.x, + self.dy * y_koef / self._scene_scale.y, + (self.x - self.touch_start_pos.x) * x_koef / self._scene_scale.x, + (self.y - self.touch_start_pos.y) * y_koef / self._scene_scale.y) end return self.is_drag diff --git a/druid/base/hover.lua b/druid/base/hover.lua index b4ab486..680f7ef 100644 --- a/druid/base/hover.lua +++ b/druid/base/hover.lua @@ -101,6 +101,15 @@ function Hover.set_hover(self, state) end end + +--- Return current hover state. True if touch action was on the node at current time +-- @tparam Hover self @{Hover} +-- @treturn bool The current hovered state +function Hover.is_hovered(self) + return self._is_hovered +end + + --- Set mouse hover state -- @tparam Hover self @{Hover} -- @tparam bool state The mouse hover state @@ -112,6 +121,14 @@ function Hover.set_mouse_hover(self, state) end +--- Return current hover state. True if nil action_id (usually desktop mouse) was on the node at current time +-- @tparam Hover self @{Hover} +-- @treturn bool The current hovered state +function Hover.is_mouse_hovered(self) + return self._is_mouse_hovered +end + + --- Strict hover click area. Useful for -- no click events outside stencil node -- @tparam Hover self @{Hover} diff --git a/druid/extended/layout.lua b/druid/extended/layout.lua index 76c5018..6e8db43 100644 --- a/druid/extended/layout.lua +++ b/druid/extended/layout.lua @@ -41,6 +41,8 @@ function Layout.init(self, node, mode, on_size_changed_callback) self._max_gui_upscale = nil self._fit_node = nil + self._anchors = {} + self.mode = mode or const.LAYOUT_MODE.FIT self.on_size_changed = Event(on_size_changed_callback) diff --git a/druid/extended/slider.lua b/druid/extended/slider.lua index 4a8c90c..4f48e8d 100644 --- a/druid/extended/slider.lua +++ b/druid/extended/slider.lua @@ -64,7 +64,7 @@ function Slider.init(self, node, end_pos, callback) self.start_pos = gui.get_position(self.node) self.pos = gui.get_position(self.node) - self.target_pos = self.pos + self.target_pos = vmath.vector3(self.pos) self.end_pos = end_pos self.dist = self.end_pos - self.start_pos @@ -72,6 +72,7 @@ function Slider.init(self, node, end_pos, callback) self.value = 0 self.on_change_value = Event(callback) + self:on_window_resized() assert(self.dist.x == 0 or self.dist.y == 0, "Slider for now can be only vertical or horizontal") end @@ -82,6 +83,14 @@ function Slider.on_layout_change(self) end +function Slider.on_window_resized(self) + local x_koef, y_koef = helper.get_screen_aspect_koef() + self._x_koef = x_koef + self._y_koef = y_koef + self._scene_scale = helper.get_scene_scale(self.node) +end + + function Slider.on_input(self, action_id, action) if action_id ~= const.ACTION_TOUCH then return false @@ -90,15 +99,17 @@ function Slider.on_input(self, action_id, action) if gui.pick_node(self.node, action.x, action.y) then if action.pressed then self.pos = gui.get_position(self.node) + self._scene_scale = helper.get_scene_scale(self.node) self.is_drag = true end end if not self.is_drag and self._input_node and gui.pick_node(self._input_node, action.x, action.y) then if action.pressed and gui.screen_to_local then + self._scene_scale = helper.get_scene_scale(self.node) self.pos = gui.screen_to_local(self.node, vmath.vector3(action.screen_x, action.screen_y, 0)) - self.pos.x = helper.clamp(self.pos.x, self.start_pos.x, self.end_pos.x) - self.pos.y = helper.clamp(self.pos.y, self.start_pos.y, self.end_pos.y) + self.pos.x = helper.clamp(self.pos.x / self._scene_scale.x, self.start_pos.x, self.end_pos.x) + self.pos.y = helper.clamp(self.pos.y / self._scene_scale.y, self.start_pos.y, self.end_pos.y) gui.set_position(self.node, self.pos) self.is_drag = true @@ -107,8 +118,8 @@ function Slider.on_input(self, action_id, action) if self.is_drag and not action.pressed then -- move - self.pos.x = self.pos.x + action.dx - self.pos.y = self.pos.y + action.dy + self.pos.x = self.pos.x + action.dx * self._x_koef / self._scene_scale.x + self.pos.y = self.pos.y + action.dy * self._y_koef / self._scene_scale.y local prev_x = self.target_pos.x local prev_y = self.target_pos.y diff --git a/druid/helper.lua b/druid/helper.lua index 4e25e4a..223886a 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -173,6 +173,21 @@ function M.contains(t, value) end +--- Get text metric from gui node. Replacement of previous gui.get_text_metrics_from_node function +-- @tparam Node text_node +-- @treturn table {width, height, max_ascent, max_descent} +function M.get_text_metrics_from_node(text_node) + local font_name = gui.get_font(text_node) + local font = gui.get_font_resource(font_name) + return resource.get_text_metrics(font, gui.get_text(text_node), { + width = gui.get_size(text_node).x, + leading = gui.get_leading(text_node), + tracking = gui.get_tracking(text_node), + line_break = gui.get_line_break(text_node), + }) +end + + --- Check if node is enabled in gui hierarchy. -- Return false, if node or any his parent is disabled -- @function helper.is_enabled @@ -190,8 +205,8 @@ function M.is_enabled(node) end ---- Return current node scene scale --- @function helper.is_enabled +--- Get cumulative parent's node scale +-- @function helper.get_scene_scale -- @tparam node node Gui node -- @tparam bool include_passed_node_scale True if add current node scale to result -- @treturn vector3 The scene node scale diff --git a/druid/styles/default/style.lua b/druid/styles/default/style.lua index 1cfa6c8..6908005 100644 --- a/druid/styles/default/style.lua +++ b/druid/styles/default/style.lua @@ -56,6 +56,7 @@ M["button"] = { M["drag"] = { DRAG_DEADZONE = 10, -- Size in pixels of drag deadzone + NO_USE_SCREEN_KOEF = false, } diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index ce259ea..a4004d4 100755 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -49,16 +49,17 @@ local static_grid = require("druid.base.static_grid") local swipe = require("druid.base.swipe") local text = require("druid.base.text") -local checkbox = require("druid.extended.checkbox") -local checkbox_group = require("druid.extended.checkbox_group") -local dynamic_grid = require("druid.extended.dynamic_grid") -local input = require("druid.extended.input") -local lang_text = require("druid.extended.lang_text") -local progress = require("druid.extended.progress") -local radio_group = require("druid.extended.radio_group") -local slider = require("druid.extended.slider") -local timer_component = require("druid.extended.timer") -local data_list = require("druid.extended.data_list") +-- To use this components, you should register them first +-- local checkbox = require("druid.extended.checkbox") +-- local checkbox_group = require("druid.extended.checkbox_group") +-- local dynamic_grid = require("druid.extended.dynamic_grid") +-- local input = require("druid.extended.input") +-- local lang_text = require("druid.extended.lang_text") +-- local progress = require("druid.extended.progress") +-- local radio_group = require("druid.extended.radio_group") +-- local slider = require("druid.extended.slider") +-- local timer_component = require("druid.extended.timer") +-- local data_list = require("druid.extended.data_list") local DruidInstance = class("druid.druid_instance") @@ -645,8 +646,7 @@ end -- @tparam node parent The gui node parent, where items will be placed -- @treturn DynamicGrid grid component function DruidInstance.new_dynamic_grid(self, parent) - -- return helper.extended_component("dynamic_grid") - return DruidInstance.new(self, dynamic_grid, parent) + return helper.extended_component("dynamic_grid") end @@ -657,8 +657,7 @@ end -- @tparam bool no_adjust If true, will not correct text size -- @treturn LangText lang_text component function DruidInstance.new_lang_text(self, node, locale_id, no_adjust) - -- return helper.extended_component("lang_text") - return DruidInstance.new(self, lang_text, node, locale_id, no_adjust) + return helper.extended_component("lang_text") end @@ -669,8 +668,7 @@ end -- @tparam[opt] function callback On slider change callback -- @treturn Slider slider component function DruidInstance.new_slider(self, node, end_pos, callback) - -- return helper.extended_component("slider") - return DruidInstance.new(self, slider, node, end_pos, callback) + return helper.extended_component("slider") end @@ -682,8 +680,7 @@ end -- @tparam[opt=false] boolean initial_state The initial state of checkbox, default - false -- @treturn Checkbox checkbox component function DruidInstance.new_checkbox(self, node, callback, click_node, initial_state) - -- return helper.extended_component("checkbox") - return DruidInstance.new(self, checkbox, node, callback, click_node, initial_state) + return helper.extended_component("checkbox") end @@ -694,8 +691,7 @@ end -- @tparam[opt] number keyboard_type Gui keyboard type for input field -- @treturn Input input component function DruidInstance.new_input(self, click_node, text_node, keyboard_type) - -- return helper.extended_component("input") - return DruidInstance.new(self, input, click_node, text_node, keyboard_type) + return helper.extended_component("input") end @@ -706,8 +702,7 @@ end -- @tparam[opt=node] node[] click_nodes Array of trigger nodes, by default equals to nodes -- @treturn CheckboxGroup checkbox_group component function DruidInstance.new_checkbox_group(self, nodes, callback, click_nodes) - -- return helper.extended_component("checkbox_group") - return DruidInstance.new(self, checkbox_group, nodes, callback, click_nodes) + return helper.extended_component("checkbox_group") end @@ -718,8 +713,7 @@ end -- @tparam function create_function The create function callback(self, data, index, data_list). Function should return (node, [component]) -- @treturn DataList data_list component function DruidInstance.new_data_list(self, druid_scroll, druid_grid, create_function) - -- return helper.extended_component("data_list") - return DruidInstance.new(self, data_list, druid_scroll, druid_grid, create_function) + return helper.extended_component("data_list") end @@ -730,8 +724,7 @@ end -- @tparam[opt=node] node[] click_nodes Array of trigger nodes, by default equals to nodes -- @treturn RadioGroup radio_group component function DruidInstance.new_radio_group(self, nodes, callback, click_nodes) - -- return helper.extended_component("radio_group") - return DruidInstance.new(self, radio_group, nodes, callback, click_nodes) + return helper.extended_component("radio_group") end @@ -743,8 +736,7 @@ end -- @tparam[opt] function callback Function on timer end -- @treturn Timer timer component function DruidInstance.new_timer(self, node, seconds_from, seconds_to, callback) - -- return helper.extended_component("timer") - return DruidInstance.new(self, timer_component, node, seconds_from, seconds_to, callback) + return helper.extended_component("timer") end @@ -755,8 +747,7 @@ end -- @tparam[opt=1] number init_value Initial value of progress bar -- @treturn Progress progress component function DruidInstance.new_progress(self, node, key, init_value) - -- return helper.extended_component("progress") - return DruidInstance.new(self, progress, node, key, init_value) + return helper.extended_component("progress") end diff --git a/example/example.gui_script b/example/example.gui_script index 0e7c15b..0e68862 100644 --- a/example/example.gui_script +++ b/example/example.gui_script @@ -3,6 +3,17 @@ local druid = require("druid.druid") local monarch = require("monarch.monarch") local default_style = require("druid.styles.default.style") +local checkbox = require("druid.extended.checkbox") +local checkbox_group = require("druid.extended.checkbox_group") +local dynamic_grid = require("druid.extended.dynamic_grid") +local input = require("druid.extended.input") +local lang_text = require("druid.extended.lang_text") +local progress = require("druid.extended.progress") +local radio_group = require("druid.extended.radio_group") +local slider = require("druid.extended.slider") +local timer_component = require("druid.extended.timer") +local data_list = require("druid.extended.data_list") + local cache_path = sys.get_save_file("druid", "cache") @@ -222,12 +233,28 @@ local function check_loading(self) end +local function register_druid_extended_components(self) + druid.register("checkbox", checkbox) + druid.register("checkbox_group", checkbox_group) + druid.register("dynamic_grid", dynamic_grid) + druid.register("input", input) + druid.register("lang_text", lang_text) + druid.register("progress", progress) + druid.register("radio_group", radio_group) + druid.register("slider", slider) + druid.register("timer", timer_component) + druid.register("data_list", data_list) +end + + + function init(self) -- Main lobby have more render priority (top panel) gui.set_render_order(10) window.set_listener(on_window_callback) druid.set_default_style(default_style) + register_druid_extended_components(self) self.druid = druid.new(self) self.cache = sys.load(cache_path) or {} diff --git a/test/helper/mock_input.lua b/test/helper/mock_input.lua new file mode 100644 index 0000000..27e575c --- /dev/null +++ b/test/helper/mock_input.lua @@ -0,0 +1,62 @@ +local M = {} + + +function M.click_pressed(x, y) + return hash("touch"), { + pressed = true, + x = x, + y = y, + } +end + + +function M.click_released(x, y) + return hash("touch"), { + released = true, + x = x, + y = y, + } +end + + +function M.key_pressed(key_id) + return hash(key_id), { + pressed = true + } +end + + +function M.key_released(key_id) + return hash(key_id), { + released = true + } +end + + +function M.click_repeated(x, y) + return hash("touch"), { + repeated = true, + x = x, + y = y, + } +end + + +function M.input_empty(x, y) + return hash("touch"), { + x = x, + y = y, + } +end + + +function M.input_empty_action_nil(x, y) + return nil, { + x = x, + y = y, + } +end + + + +return M diff --git a/test/helper/test_helper.lua b/test/helper/test_helper.lua new file mode 100644 index 0000000..a728a0d --- /dev/null +++ b/test/helper/test_helper.lua @@ -0,0 +1,19 @@ +local mock = require("deftest.mock.mock") + +local M = {} + +-- Userdata type instead of script self +function M.get_context() + return vmath.vector() +end + + +-- Callback for return value from function +function M.get_function(callback) + local listener = {} + listener.callback = function(...) if callback then return callback(...) end end + mock.mock(listener) + return function(...) return listener.callback(...) end, listener.callback +end + +return M diff --git a/test/test.collection b/test/test.collection index cbbebb6..fe2bf3f 100644 --- a/test/test.collection +++ b/test/test.collection @@ -4,7 +4,7 @@ embedded_instances { id: "test" data: "components {\n" " id: \"test\"\n" - " component: \"/test/test.script\"\n" + " component: \"/test/test.gui\"\n" " position {\n" " x: 0.0\n" " y: 0.0\n" @@ -16,6 +16,8 @@ embedded_instances { " z: 0.0\n" " w: 1.0\n" " }\n" + " property_decls {\n" + " }\n" "}\n" "" position { diff --git a/test/test.gui b/test/test.gui new file mode 100644 index 0000000..d0f87d1 --- /dev/null +++ b/test/test.gui @@ -0,0 +1,10 @@ +script: "/test/test.gui_script" +background_color { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 +} +material: "/builtins/materials/gui.material" +adjust_reference: ADJUST_REFERENCE_PARENT +max_nodes: 512 diff --git a/test/test.script b/test/test.gui_script similarity index 63% rename from test/test.script rename to test/test.gui_script index b2eb07f..98187e0 100644 --- a/test/test.script +++ b/test/test.gui_script @@ -1,7 +1,10 @@ -local deftest = require "deftest.deftest" +local deftest = require("deftest.deftest") local tests = { -- Test list + require("test.tests.test_button"), + require("test.tests.test_hover"), + require("test.tests.test_drag"), } diff --git a/test/tests/test_button.lua b/test/tests/test_button.lua new file mode 100644 index 0000000..2b7c23f --- /dev/null +++ b/test/tests/test_button.lua @@ -0,0 +1,266 @@ +local mock_gui = require("deftest.mock.gui") +local mock_time = require("deftest.mock.time") +local mock_input = require("test.helper.mock_input") +local test_helper = require("test.helper.test_helper") +local druid_system = require("druid.druid") + + +return function() + local druid = nil + local context = test_helper.get_context() + describe("Button Component", function() + before(function() + mock_gui.mock() + mock_time.mock() + mock_time.set(60) + druid = druid_system.new(context) + end) + + after(function() + mock_gui.unmock() + mock_time.unmock() + druid:final(context) + druid = nil + end) + + it("Should do usual click", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + local instance = druid:new_button(button, on_click, button_params) + local is_clicked_pressed = druid:on_input(mock_input.click_pressed(10, 10)) + local is_clicked_released = druid:on_input(mock_input.click_released(20, 10)) + + assert(is_clicked_pressed) + assert(is_clicked_released) + assert(on_click_mock.calls == 1) + assert(on_click_mock.params[1] == context) + assert(on_click_mock.params[2] == button_params) + assert(on_click_mock.params[3] == instance) + end) + + it("Should do long click if exists", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + local on_long_click, on_long_click_mock = test_helper.get_function() + + local instance = druid:new_button(button, on_click, button_params) + instance.on_long_click:subscribe(on_long_click) + druid:on_input(mock_input.click_pressed(10, 10)) + mock_time.elapse(0.3) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 1) + assert(on_long_click_mock.calls == 0) + + druid:on_input(mock_input.click_pressed(10, 10)) + mock_time.elapse(0.5) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 1) + assert(on_long_click_mock.calls == 1) + assert(on_long_click_mock.params[1] == context) + assert(on_long_click_mock.params[2] == button_params) + assert(on_long_click_mock.params[3] == instance) + end) + + it("Should do not long click if not exists", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + druid:new_button(button, on_click, button_params) + druid:on_input(mock_input.click_pressed(10, 10)) + mock_time.elapse(0.5) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 1) + end) + + it("Should do double click if exists", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + local on_double_click, on_double_click_mock = test_helper.get_function() + + local instance = druid:new_button(button, on_click, button_params) + instance.on_double_click:subscribe(on_double_click) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + mock_time.elapse(0.1) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 1) + assert(on_double_click_mock.calls == 1) + assert(on_double_click_mock.params[1] == context) + assert(on_double_click_mock.params[2] == button_params) + assert(on_double_click_mock.params[3] == instance) + + mock_time.elapse(1) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + mock_time.elapse(0.5) -- The double click should not register, big time between clicks + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 3) + assert(on_double_click_mock.calls == 1) + end) + + it("Should do not double click if not exists", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + druid:new_button(button, on_click, button_params) + + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + mock_time.elapse(0.1) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 2) + end) + + it("Should do hold click if exists", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + local on_long_click, on_long_click_mock = test_helper.get_function() + local on_hold_callback, on_hold_callback_mock = test_helper.get_function() + local instance = druid:new_button(button, on_click, button_params) + instance.on_long_click:subscribe(on_long_click) -- long click required for hold callback + instance.on_hold_callback:subscribe(on_hold_callback) + + druid:on_input(mock_input.click_pressed(10, 10)) + mock_time.elapse(0.5) -- time between hold treshold and autorelease hold time + druid:on_input(mock_input.click_repeated(10, 10)) + + assert(on_click_mock.calls == 0) + assert(on_hold_callback_mock.calls == 1) + assert(on_hold_callback_mock.params[1] == context) + assert(on_hold_callback_mock.params[2] == button_params) + assert(on_hold_callback_mock.params[3] == instance) + + druid:on_input(mock_input.click_released(10, 10)) + + assert(on_click_mock.calls == 0) + assert(on_long_click_mock.calls == 1) + end) + + it("Should do click outside if exists", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click = test_helper.get_function() + local on_click_outside, on_click_outside_mock = test_helper.get_function() + local instance = druid:new_button(button, on_click, button_params) + instance.on_click_outside:subscribe(on_click_outside) + + druid:on_input(mock_input.click_pressed(-10, 10)) + druid:on_input(mock_input.click_released(-10, 10)) + + assert(on_click_outside_mock.calls == 1) + + mock_time.elapse(1) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(-10, 10)) + + assert(on_click_outside_mock.calls == 2) + end) + + it("Should not click if mouse was outside while clicking", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + druid:new_button(button, on_click, button_params) + + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.input_empty(-10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 0) + end) + + it("Should work with check function", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + local instance = druid:new_button(button, on_click, button_params) + local check_function, check_function_mock = test_helper.get_function(function() return false end) + local failure_function, failure_function_mock = test_helper.get_function() + instance:set_check_function(check_function, failure_function) + + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 0) + assert(check_function_mock.calls == 1) + assert(failure_function_mock.calls == 1) + + local check_true_function, check_function_true_mock = test_helper.get_function(function() return true end) + instance:set_check_function(check_true_function, failure_function) + + mock_time.elapse(1) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 1) + assert(check_function_true_mock.calls == 1) + assert(failure_function_mock.calls == 1) + end) + + it("Should have key trigger", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + local instance = druid:new_button(button, on_click, button_params) + instance:set_key_trigger("key_a") + druid:on_input(mock_input.key_pressed("key_a")) + druid:on_input(mock_input.key_released("key_a")) + assert(on_click_mock.calls == 1) + assert(instance:get_key_trigger() == hash("key_a")) + end) + + it("Should work with click zone", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local zone = mock_gui.add_box("zone", 25, 25, 25, 25) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + local instance = druid:new_button(button, on_click, button_params) + instance:set_click_zone(zone) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(10, 10)) + assert(on_click_mock.calls == 0) + + druid:on_input(mock_input.click_pressed(25, 25)) + druid:on_input(mock_input.click_released(25, 25)) + assert(on_click_mock.calls == 1) + end) + + it("Should work with set_enabled", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + local instance = druid:new_button(button, on_click, button_params) + + instance:set_enabled(false) + local is_clicked_pressed = druid:on_input(mock_input.click_pressed(10, 10)) + local is_clicked_released = druid:on_input(mock_input.click_released(10, 10)) + assert(is_clicked_pressed == false) + assert(is_clicked_released == false) + assert(on_click_mock.calls == 0) + assert(instance:is_enabled() == false) + + instance:set_enabled(true) + local is_clicked_pressed2 = druid:on_input(mock_input.click_pressed(10, 10)) + assert(is_clicked_pressed2 == true) + local is_clicked_released2 = druid:on_input(mock_input.click_released(10, 10)) + assert(is_clicked_released2 == true) + assert(on_click_mock.calls == 1) + assert(instance:is_enabled() == true) + end) + end) +end diff --git a/test/tests/test_drag.lua b/test/tests/test_drag.lua new file mode 100644 index 0000000..05c5ebf --- /dev/null +++ b/test/tests/test_drag.lua @@ -0,0 +1,134 @@ +local mock_gui = require "deftest.mock.gui" +local mock_time = require("deftest.mock.time") +local mock_input = require("test.helper.mock_input") +local test_helper = require("test.helper.test_helper") +local druid_system = require("druid.druid") + +return function() + local druid = nil + local context = test_helper.get_context() + + local function create_drag_instance(on_drag) + local button = mock_gui.add_box("button", 0, 0, 20, 20) + local instance = druid:new_drag(button, on_drag) + instance.style.NO_USE_SCREEN_KOEF = true + instance.style.DRAG_DEADZONE = 4 + return instance + end + + describe("Drag component", function() + before(function() + mock_gui.mock() + mock_time.mock() + mock_time.set(60) + druid = druid_system.new(context) + end) + + after(function() + mock_gui.unmock() + mock_time.unmock() + druid:final(context) + druid = nil + end) + + it("Should call drag callback on node", function() + local on_drag, on_drag_mock = test_helper.get_function() + local instance = create_drag_instance(on_drag) + + druid:on_input(mock_input.click_pressed(10, 10)) + assert(instance.is_touch == true) + + druid:on_input(mock_input.input_empty(12, 10)) + assert(on_drag_mock.calls == 0) + + druid:on_input(mock_input.input_empty(14, 10)) + assert(on_drag_mock.calls == 1) + assert(on_drag_mock.params[2] == 2) -- From the last input dx + assert(on_drag_mock.params[3] == 0) + assert(on_drag_mock.params[4] == 4) -- Total X from drag start point + assert(on_drag_mock.params[5] == 0) + end) + + + it("Should call all included events", function() + local on_drag, on_drag_mock = test_helper.get_function() + local instance = create_drag_instance(on_drag) + + local on_touch_start, on_touch_start_mock = test_helper.get_function() + instance.on_touch_start:subscribe(on_touch_start) + local on_touch_end, on_touch_end_mock = test_helper.get_function() + instance.on_touch_end:subscribe(on_touch_end) + local on_drag_start, on_drag_start_mock = test_helper.get_function() + instance.on_drag_start:subscribe(on_drag_start) + local on_drag_end, on_drag_end_mock = test_helper.get_function() + instance.on_drag_end:subscribe(on_drag_end) + + assert(on_touch_start_mock.calls == 0) + druid:on_input(mock_input.click_pressed(10, 10)) + assert(on_touch_start_mock.calls == 1) + assert(on_touch_end_mock.calls == 0) + druid:on_input(mock_input.click_released(12, 10)) + assert(on_touch_end_mock.calls == 1) + + druid:on_input(mock_input.click_pressed(10, 10)) + assert(on_drag_start_mock.calls == 0) + druid:on_input(mock_input.input_empty(15, 10)) + assert(on_drag_start_mock.calls == 1) + assert(on_drag_mock.calls == 1) + assert(on_drag_end_mock.calls == 0) + druid:on_input(mock_input.click_released(15, 10)) + assert(on_drag_end_mock.calls == 1) + end) + + it("Should work with set_enabled", function() + local on_drag, on_drag_mock = test_helper.get_function() + local instance = create_drag_instance(on_drag) + + instance:set_enabled(false) + assert(instance:is_enabled() == false) + + druid:on_input(mock_input.click_pressed(10, 10)) + assert(instance.is_touch == false) + + druid:on_input(mock_input.input_empty(12, 10)) + assert(on_drag_mock.calls == 0) + + druid:on_input(mock_input.input_empty(15, 10)) + assert(on_drag_mock.calls == 0) + + instance:set_enabled(true) + assert(instance:is_enabled() == true) + + druid:on_input(mock_input.click_pressed(10, 10)) + assert(instance.is_touch == true) + + druid:on_input(mock_input.input_empty(12, 10)) + assert(on_drag_mock.calls == 0) + + druid:on_input(mock_input.input_empty(15, 10)) + assert(on_drag_mock.calls == 1) + end) + + it("Should work with click zone", function() + local on_drag, on_drag_mock = test_helper.get_function() + local instance = create_drag_instance(on_drag) + local zone = mock_gui.add_box("zone", 10, 10, 10, 10) + instance:set_click_zone(zone) + + druid:on_input(mock_input.click_pressed(5, 5)) + assert(instance.is_touch == false) + + druid:on_input(mock_input.input_empty(10, 5)) + assert(on_drag_mock.calls == 0) + + druid:on_input(mock_input.input_empty(15, 10)) + assert(on_drag_mock.calls == 0) + + druid:on_input(mock_input.click_pressed(15, 15)) + assert(instance.is_touch == true) + + druid:on_input(mock_input.input_empty(20, 15)) + assert(on_drag_mock.calls == 1) + end) + end) +end diff --git a/test/tests/test_hover.lua b/test/tests/test_hover.lua new file mode 100644 index 0000000..1ad23fc --- /dev/null +++ b/test/tests/test_hover.lua @@ -0,0 +1,102 @@ +local mock_gui = require "deftest.mock.gui" +local mock_time = require("deftest.mock.time") +local mock_input = require("test.helper.mock_input") +local test_helper = require("test.helper.test_helper") +local druid_system = require("druid.druid") + +return function() + local druid = nil + local context = test_helper.get_context() + describe("Hover component", function() + before(function() + mock_gui.mock() + mock_time.mock() + mock_time.set(60) + druid = druid_system.new(context) + end) + + after(function() + mock_gui.unmock() + mock_time.unmock() + druid:final(context) + druid = nil + end) + + it("Should fire callback on touch hover and unhover", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local is_hovered = false + local on_hover, on_hover_mock = test_helper.get_function(function(_, state) + is_hovered = state + end) + local instance = druid:new_hover(button, on_hover) + druid:on_input(mock_input.input_empty(10, 10)) + assert(is_hovered == true) + assert(instance:is_hovered() == true) + assert(instance:is_mouse_hovered() == false) + + druid:on_input(mock_input.input_empty(-10, 10)) + assert(is_hovered == false) + assert(instance:is_hovered() == false) + assert(instance:is_mouse_hovered() == false) + end) + + it("Should fire callback on mouse hover and unhover", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local is_hovered = false + local on_hover, on_hover_mock = test_helper.get_function(function(_, state) + is_hovered = state + end) + + local instance = druid:new_hover(button) + instance.on_mouse_hover:subscribe(on_hover) + druid:on_input(mock_input.input_empty_action_nil(10, 10)) + assert(is_hovered == true) + assert(instance:is_hovered() == false) + assert(instance:is_mouse_hovered() == true) + + druid:on_input(mock_input.input_empty_action_nil(-10, 10)) + assert(is_hovered == false) + assert(instance:is_hovered() == false) + assert(instance:is_mouse_hovered() == false) + end) + + it("Should work with click zone", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local zone = mock_gui.add_box("zone", 25, 25, 25, 25) + local on_hover, on_hover_mock = test_helper.get_function() + local instance = druid:new_hover(button, on_hover) + instance:set_click_zone(zone) + druid:on_input(mock_input.input_empty(10, 10)) + assert(instance:is_hovered() == false) + + druid:on_input(mock_input.input_empty(25, 25)) + assert(instance:is_hovered() == true) + + druid:on_input(mock_input.input_empty(24, 24)) + assert(instance:is_hovered() == false) + end) + + it("Should have set_enabled function", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local on_hover, on_hover_mock = test_helper.get_function() + local instance = druid:new_hover(button, on_hover) + + druid:on_input(mock_input.input_empty(10, 10)) + assert(instance:is_hovered() == true) + + instance:set_enabled(false) + assert(instance:is_enabled() == false) + assert(instance:is_hovered() == false) + druid:on_input(mock_input.input_empty(12, 12)) + assert(instance:is_hovered() == false) + + instance:set_enabled(true) + druid:on_input(mock_input.input_empty(12, 12)) + assert(instance:is_enabled() == true) + assert(instance:is_hovered() == true) + + druid:on_input(mock_input.input_empty(-10, 10)) + assert(instance:is_hovered() == false) + end) + end) +end diff --git a/test/tests/test_template.lua b/test/tests/test_template.lua new file mode 100644 index 0000000..8d34c88 --- /dev/null +++ b/test/tests/test_template.lua @@ -0,0 +1,29 @@ +local mock_gui = require "deftest.mock.gui" +local mock_time = require("deftest.mock.time") +local mock_input = require("test.helper.mock_input") +local test_helper = require("test.helper.test_helper") +local druid_system = require("druid.druid") + +return function() + local druid = nil + local context = test_helper.get_context() + describe("Template component", function() + before(function() + mock_gui.mock() + mock_time.mock() + mock_time.set(60) + druid = druid_system.new(context) + end) + + after(function() + mock_gui.unmock() + mock_time.unmock() + druid:final(context) + druid = nil + end) + + it("Should do something right", function() + assert(2 == 1 + 1) + end) + end) +end