diff --git a/druid/base/drag.lua b/druid/base/drag.lua index cd0a50f..15d6a13 100644 --- a/druid/base/drag.lua +++ b/druid/base/drag.lua @@ -155,9 +155,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 IS_USE_SCREEN_KOEF If screen aspect ration affects on drag values function Drag.on_style_change(self, style) self.style = {} self.style.DRAG_DEADZONE = style.DRAG_DEADZONE or 10 + self.style.IS_USE_SCREEN_KOEF = style.IS_USE_SCREEN_KOEF or false end @@ -278,11 +280,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 not self.style.IS_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.dy * y_koef, + (self.x - self.touch_start_pos.x) * x_koef, + (self.y - self.touch_start_pos.y) * y_koef) end return self.is_drag diff --git a/druid/styles/default/style.lua b/druid/styles/default/style.lua index 897b599..6cdd878 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 + IS_USE_SCREEN_KOEF = true, } diff --git a/game.project b/game.project index d5aaf3a..f93d47e 100644 --- a/game.project +++ b/game.project @@ -1,5 +1,5 @@ [bootstrap] -main_collection = /example/example.collectionc +main_collection = /test/test.collectionc [script] shared_state = 1 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 91% rename from test/test.script rename to test/test.gui_script index 10e8d03..98187e0 100644 --- a/test/test.script +++ b/test/test.gui_script @@ -4,6 +4,7 @@ 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 index d27178d..41a7737 100644 --- a/test/tests/test_button.lua +++ b/test/tests/test_button.lua @@ -8,7 +8,7 @@ local druid_system = require("druid.druid") return function() local druid = nil local context = test_helper.get_context() - describe("Druid Button", function() + describe("Button Component", function() before(function() mock_gui.mock() mock_time.mock() diff --git a/test/tests/test_drag.lua b/test/tests/test_drag.lua new file mode 100644 index 0000000..9a78abe --- /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.IS_USE_SCREEN_KOEF = false + 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 index 24d0f7e..acc8921 100644 --- a/test/tests/test_hover.lua +++ b/test/tests/test_hover.lua @@ -7,7 +7,7 @@ local druid_system = require("druid.druid") return function() local druid = nil local context = test_helper.get_context() - describe("Eva lang", function() + describe("Hover component", function() before(function() mock_gui.mock() mock_time.mock() @@ -22,7 +22,7 @@ return function() druid = nil end) - it("Hover should fire callback on touch hover and unhover", function() + 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) @@ -40,7 +40,7 @@ return function() assert(instance:is_mouse_hovered() == false) end) - it("Hover should fire callback on mouse hover and unhover", function() + 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) @@ -76,7 +76,7 @@ return function() assert(instance:is_hovered() == false) end) - it("Hover should have set_enabled function", function() + 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) diff --git a/test/tests/test_template.lua b/test/tests/test_template.lua index b81690a..8d34c88 100644 --- a/test/tests/test_template.lua +++ b/test/tests/test_template.lua @@ -7,7 +7,7 @@ local druid_system = require("druid.druid") return function() local druid = nil local context = test_helper.get_context() - describe("Eva lang", function() + describe("Template component", function() before(function() mock_gui.mock() mock_time.mock()