From decb9fd9fd811d1029dfac243de2a01de277c876 Mon Sep 17 00:00:00 2001 From: Insality Date: Sun, 10 Apr 2022 15:20:57 +0300 Subject: [PATCH] Solve #73 fix scroll drag on stretch/zoom nodes --- druid/base/drag.lua | 13 +- druid/extended/layout.lua | 12 +- druid/helper.lua | 9 ++ example/examples/general/layout/layout.gui | 126 +++++++++++++++++- .../examples/general/layout/layout.gui_script | 1 + 5 files changed, 147 insertions(+), 14 deletions(-) diff --git a/druid/base/drag.lua b/druid/base/drag.lua index b51c1af..71281bd 100644 --- a/druid/base/drag.lua +++ b/druid/base/drag.lua @@ -186,6 +186,8 @@ function Drag.init(self, node, on_drag_callback) self.on_drag_start = Event() self.on_drag = Event(on_drag_callback) self.on_drag_end = Event() + + self:on_window_resized() end @@ -199,6 +201,13 @@ function Drag.on_late_init(self) end +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 +end + + function Drag.on_input_interrupt(self) if self.is_drag or self.is_touch then end_touch(self) @@ -258,8 +267,8 @@ function Drag.on_input(self, action_id, action) local touch_modified = find_touch(action_id, action, self.touch_id) if touch_modified and self.is_drag then - self.dx = touch_modified.x - self.x - self.dy = touch_modified.y - self.y + self.dx = (touch_modified.x - self.x) * self._x_koef + self.dy = (touch_modified.y - self.y) * self._y_koef end if touch_modified then diff --git a/druid/extended/layout.lua b/druid/extended/layout.lua index 37963f0..aa1bc9f 100644 --- a/druid/extended/layout.lua +++ b/druid/extended/layout.lua @@ -30,7 +30,6 @@ function Layout:init(node, mode, on_size_changed_callback) self._min_size = nil self._max_size = nil - self.window_size = vmath.vector3(gui.get_width(), gui.get_height(), 0) self.mode = mode or const.LAYOUT_MODE.FIT self.on_size_changed = Event(on_size_changed_callback) @@ -40,12 +39,7 @@ end function Layout:on_window_resized() - local window_x, window_y = window.get_size() - local stretch_x = window_x / self.window_size.x - local stretch_y = window_y / self.window_size.y - - local x_koef = stretch_x / math.min(stretch_x, stretch_y) - local y_koef = stretch_y / math.min(stretch_x, stretch_y) + local x_koef, y_koef = helper.get_screen_aspect_koef() local new_size = vmath.vector3(self.origin_size) if self.mode == const.LAYOUT_MODE.STRETCH_X or self.mode == const.LAYOUT_MODE.STRETCH then @@ -65,8 +59,8 @@ function Layout:on_window_resized() gui.set_size(self.node, new_size) - self.position.x = self.origin_position.x * x_koef + self.origin_position.x * (1 - x_koef) * self.pivot.x * 2 - self.position.y = self.origin_position.y * y_koef + self.origin_position.y * (1 - y_koef) * self.pivot.y * 2 + self.position.x = self.origin_position.x + self.origin_position.x * (1 - x_koef) * self.pivot.x * 2 + self.position.y = self.origin_position.y + self.origin_position.y * (1 - y_koef) * self.pivot.y * 2 gui.set_position(self.node, self.position) self.on_size_changed:trigger(self:get_context(), new_size) diff --git a/druid/helper.lua b/druid/helper.lua index d5ebee2..74eda23 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -97,6 +97,15 @@ function M.centrate_nodes(margin, ...) end +function M.get_screen_aspect_koef() + local window_x, window_y = window.get_size() + local stretch_x = window_x / gui.get_width() + local stretch_y = window_y / gui.get_height() + return stretch_x / math.min(stretch_x, stretch_y), + stretch_y / math.min(stretch_x, stretch_y) +end + + function M.step(current, target, step) if current < target then return math.min(current + step, target) diff --git a/example/examples/general/layout/layout.gui b/example/examples/general/layout/layout.gui index f157508..9c1d020 100644 --- a/example/examples/general/layout/layout.gui +++ b/example/examples/general/layout/layout.gui @@ -71,7 +71,7 @@ nodes { nodes { position { x: 0.0 - y: 200.0 + y: 250.0 z: 0.0 w: 1.0 } @@ -191,7 +191,7 @@ nodes { nodes { position { x: 0.0 - y: 0.0 + y: 50.0 z: 0.0 w: 1.0 } @@ -226,7 +226,7 @@ nodes { xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE pivot: PIVOT_CENTER - adjust_mode: ADJUST_MODE_STRETCH + adjust_mode: ADJUST_MODE_FIT parent: "root" layer: "" inherit_alpha: true @@ -604,6 +604,126 @@ nodes { text_tracking: 0.0 custom_type: 0 } +nodes { + position { + x: -150.0 + y: -400.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: 300.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_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "kenney/slider_move" + id: "node_layout_stretch_y_anchor_w" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_W + adjust_mode: ADJUST_MODE_STRETCH + parent: "even_in_fit_node" + layer: "" + inherit_alpha: true + slice9 { + x: 17.0 + y: 17.0 + z: 17.0 + w: 17.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_MANUAL + custom_type: 0 +} +nodes { + position { + x: 150.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: 0.65 + y: 0.65 + z: 1.0 + w: 1.0 + } + size { + x: 450.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: "Layout Stretch by Y Anchor W" + font: "game" + id: "text_layout_stretch3" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: true + parent: "node_layout_stretch_y_anchor_w" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 0.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 + custom_type: 0 +} layers { name: "image" } diff --git a/example/examples/general/layout/layout.gui_script b/example/examples/general/layout/layout.gui_script index bce9c3c..2550a26 100644 --- a/example/examples/general/layout/layout.gui_script +++ b/example/examples/general/layout/layout.gui_script @@ -9,6 +9,7 @@ function init(self) self.druid:new_layout("node_layout_stretch", const_druid.LAYOUT_MODE.STRETCH) self.druid:new_layout("node_layout_stretch_x", const_druid.LAYOUT_MODE.STRETCH_X) self.druid:new_layout("node_layout_stretch_y", const_druid.LAYOUT_MODE.STRETCH_Y) + self.druid:new_layout("node_layout_stretch_y_anchor_w", const_druid.LAYOUT_MODE.STRETCH_Y) end