diff --git a/druid/base/drag.lua b/druid/base/drag.lua index f4439d8..8eed307 100644 --- a/druid/base/drag.lua +++ b/druid/base/drag.lua @@ -22,11 +22,11 @@ local function start_touch(self, touch) self.is_touch = true self.is_drag = false - self.touch_start_pos.x = touch.screen_x - self.touch_start_pos.y = touch.screen_y + self.touch_start_pos.x = touch.x + self.touch_start_pos.y = touch.y - self.screen_x = touch.screen_x - self.screen_y = touch.screen_y + self.x = touch.x + self.y = touch.y self.on_touch_start:trigger(self:get_context()) end @@ -40,22 +40,24 @@ local function end_touch(self) self.is_drag = false self.is_touch = false self.on_touch_end:trigger(self:get_context()) + self:reset_input_priority() self.touch_id = 0 end local function process_touch(self, touch) if not self.can_x then - self.touch_start_pos.x = touch.screen_x + self.touch_start_pos.x = touch.x end if not self.can_y then - self.touch_start_pos.y = touch.screen_y + self.touch_start_pos.y = touch.y end - local distance = helper.distance(touch.screen_x, touch.screen_y, self.touch_start_pos.x, self.touch_start_pos.y) + local distance = helper.distance(touch.x, touch.y, self.touch_start_pos.x, self.touch_start_pos.y) if not self.is_drag and distance >= self.drag_deadzone then self.is_drag = true self.on_drag_start:trigger(self:get_context()) + self:increase_input_priority() end end @@ -93,8 +95,8 @@ local function on_touch_release(self, action_id, action) end if next_touch then - self.screen_x = next_touch.screen_x - self.screen_y = next_touch.screen_y + self.x = next_touch.x + self.y = next_touch.y self.touch_id = next_touch.id else end_touch(self) @@ -116,8 +118,8 @@ function M.init(self, node, on_drag_callback) self.dx = 0 self.dy = 0 self.touch_id = 0 - self.screen_x = 0 - self.screen_y = 0 + self.x = 0 + self.y = 0 self.is_touch = false self.is_drag = false self.touch_start_pos = vmath.vector3(0) @@ -185,13 +187,13 @@ function M.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.screen_x - self.screen_x - self.dy = touch_modified.screen_y - self.screen_y + self.dx = touch_modified.x - self.x + self.dy = touch_modified.y - self.y end if touch_modified then - self.screen_x = touch_modified.screen_x - self.screen_y = touch_modified.screen_y + self.x = touch_modified.x + self.y = touch_modified.y end if self.is_drag then diff --git a/druid/base/scroll.lua b/druid/base/scroll.lua index 6255c30..d1537ae 100644 --- a/druid/base/scroll.lua +++ b/druid/base/scroll.lua @@ -22,7 +22,7 @@ local function on_scroll_drag(self, dx, dy) dy = -dy local t = self.target_pos local b = self.available_soft_pos - local soft = 100 + local extra_size = self.style.EXTRA_STRECH_SIZE -- Handle soft zones -- Percent - multiplier for delta. Less if outside of scroll zone @@ -30,10 +30,10 @@ local function on_scroll_drag(self, dx, dy) local y_perc = 1 if t.x < b.x and dx < 0 then - x_perc = (soft - (b.x - t.x)) / soft + x_perc = (-extra_size.x - (b.x - t.x)) / -extra_size.x end if t.x > b.z and dx > 0 then - x_perc = (soft - (t.x - b.z)) / soft + x_perc = (extra_size.z - (t.x - b.z)) / extra_size.z end -- If disabled scroll by x if not self.can_x then @@ -41,24 +41,16 @@ local function on_scroll_drag(self, dx, dy) end if t.y > b.y and dy < 0 then - y_perc = (soft - (t.y - b.y)) / soft + y_perc = (extra_size.y - (t.y - b.y)) / extra_size.y end if t.y < b.w and dy > 0 then - y_perc = (soft - (b.w - t.y)) / soft + y_perc = (-extra_size.w - (b.w - t.y)) / -extra_size.w end -- If disabled scroll by y if not self.can_y then y_perc = 0 end - -- Reset inert if outside of scroll zone - if x_perc ~= 1 then - self.inertion.x = 0 - end - if y_perc ~= 1 then - self.inertion.y = 0 - end - t.x = t.x + dx * x_perc t.y = t.y - dy * y_perc end @@ -79,21 +71,8 @@ local function update_hand_scroll(self, dt) local dx = self.target_pos.x - self.current_pos.x local dy = self.target_pos.y - self.current_pos.y - if helper.sign(dx) ~= helper.sign(self.inertion.x) then - self.inertion.x = 0 - end - if helper.sign(dy) ~= helper.sign(self.inertion.y) then - self.inertion.y = 0 - end - - self.inertion.x = self.inertion.x + dx - self.inertion.y = self.inertion.y + dy - - self.inertion.x = math.abs(self.inertion.x) * helper.sign(dx) - self.inertion.y = math.abs(self.inertion.y) * helper.sign(dy) - - self.inertion.x = self.inertion.x * self.style.FRICT_HOLD - self.inertion.y = self.inertion.y * self.style.FRICT_HOLD + self.inertion.x = (self.inertion.x + dx) * self.style.FRICT_HOLD + self.inertion.y = (self.inertion.y + dy) * self.style.FRICT_HOLD set_pos(self, self.target_pos) end @@ -119,8 +98,10 @@ end local function check_threshold(self) - if vmath.length(self.inertion) < self.style.INERT_THRESHOLD then + if math.abs(self.inertion.x) < self.style.INERT_THRESHOLD then self.inertion.x = 0 + end + if math.abs(self.inertion.y) < self.style.INERT_THRESHOLD then self.inertion.y = 0 end end @@ -221,7 +202,7 @@ function M.init(self, view_zone, content_zone) self.current_pos = gui.get_position(self.content_node) self.target_pos = vmath.vector3(self.current_pos) - self.inertion = vmath.vector3() + self.inertion = vmath.vector3(0) self.drag = self.druid:new_drag(view_zone, on_scroll_drag) self.drag.on_touch_start:subscribe(on_touch_start) diff --git a/druid/styles/default/style.lua b/druid/styles/default/style.lua index 8bd8c60..10d0953 100644 --- a/druid/styles/default/style.lua +++ b/druid/styles/default/style.lua @@ -52,14 +52,14 @@ M["scroll"] = { ANIM_SPEED = 0.3, -- gui.animation speed to point SCROLL_WHEEL_SPEED = 10, - FRICT = 0.93, -- mult for free inert - FRICT_HOLD = 0.8, -- mult. for inert, while touching + FRICT = 0.94, -- mult for free inert + FRICT_HOLD = 0.75, -- mult. for inert, while touching INERT_THRESHOLD = 2, -- speed to stop inertion - INERT_SPEED = 25, -- koef. of inert speed - EXTRA_STRECH_SIZE = vmath.vector4(-100, 100, 100, -100), -- size of outside zone (back move) + INERT_SPEED = 27, -- koef. of inert speed - BACK_SPEED = 0.2, -- lerp speed - LERP_SPEED = 1, + BACK_SPEED = 0.35, -- Lerp speed of return to soft position + + EXTRA_STRECH_SIZE = vmath.vector4(-100, 100, 100, -100), -- size of outside zone (back move) } diff --git a/example/gui/main/main.gui b/example/gui/main/main.gui index bce3fa1..3675838 100644 --- a/example/gui/main/main.gui +++ b/example/gui/main/main.gui @@ -180,7 +180,7 @@ nodes { nodes { position { x: 0.0 - y: 1200.0 + y: 450.0 z: 0.0 w: 1.0 } @@ -2882,7 +2882,7 @@ nodes { nodes { position { x: 0.0 - y: -1890.0 + y: -890.0 z: 0.0 w: 1.0 } @@ -3225,179 +3225,6 @@ nodes { text_leading: 1.0 text_tracking: 0.0 } -nodes { - position { - x: 0.0 - y: -1150.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: 400.0 - z: 0.0 - w: 1.0 - } - color { - x: 0.5019608 - y: 0.2 - z: 0.3019608 - w: 1.0 - } - type: TYPE_BOX - blend_mode: BLEND_MODE_ALPHA - texture: "" - id: "scroll2_view" - xanchor: XANCHOR_NONE - yanchor: YANCHOR_NONE - pivot: PIVOT_CENTER - adjust_mode: ADJUST_MODE_FIT - parent: "scroll_content" - 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: false - size_mode: SIZE_MODE_MANUAL -} -nodes { - position { - x: 0.0 - y: 200.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: 800.0 - z: 0.0 - w: 1.0 - } - color { - x: 0.4 - y: 0.3019608 - z: 0.7019608 - w: 1.0 - } - type: TYPE_BOX - blend_mode: BLEND_MODE_ALPHA - texture: "" - id: "scroll2_content" - xanchor: XANCHOR_NONE - yanchor: YANCHOR_NONE - pivot: PIVOT_N - adjust_mode: ADJUST_MODE_FIT - parent: "scroll2_view" - 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: 0.5 - template_node_child: false - size_mode: SIZE_MODE_MANUAL -} -nodes { - position { - x: 0.0 - y: -200.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: "Content" - font: "game" - id: "text" - xanchor: XANCHOR_NONE - yanchor: YANCHOR_NONE - pivot: PIVOT_CENTER - 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: "scroll2_content" - layer: "" - inherit_alpha: true - alpha: 1.0 - outline_alpha: 0.0 - shadow_alpha: 0.0 - template_node_child: false - text_leading: 1.0 - text_tracking: 0.0 -} nodes { position { x: 600.0 @@ -6962,6 +6789,179 @@ nodes { text_leading: 1.0 text_tracking: 0.0 } +nodes { + position { + x: 0.0 + y: -1400.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: 550.0 + y: 500.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.5019608 + y: 0.2 + z: 0.3019608 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "" + id: "scroll_smaller_view" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "scroll_page_content" + 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: false + size_mode: SIZE_MODE_MANUAL +} +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: 200.0 + y: 200.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.4 + y: 0.3019608 + z: 0.7019608 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "" + id: "scroll_smaller_content" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "scroll_smaller_view" + 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: 0.5 + template_node_child: false + size_mode: SIZE_MODE_MANUAL +} +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: 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: "Content" + font: "game" + id: "text" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + 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: "scroll_smaller_content" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 0.0 + shadow_alpha: 0.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 +} nodes { position { x: 2400.0 diff --git a/example/gui/main/main.gui_script b/example/gui/main/main.gui_script index f38a0ae..ba4ef8d 100644 --- a/example/gui/main/main.gui_script +++ b/example/gui/main/main.gui_script @@ -21,12 +21,8 @@ local pages = { local function on_control_button(self, delta) self.page = self.page + delta - if self.page < 1 then - self.page = #pages - end - if self.page > #pages then - self.page = 1 - end + self.page = math.max(1, self.page) + self.page = math.min(self.page, #pages) self.header:translate(pages[self.page]) local node = gui.get_node("C_Anchor") diff --git a/example/page/main.lua b/example/page/main.lua index 4828fbe..5563c0d 100644 --- a/example/page/main.lua +++ b/example/page/main.lua @@ -98,7 +98,6 @@ end local function setup_scroll(self) self.druid:new_scroll("main_page", "scroll_content") - self.scroll = self.druid:new_scroll("scroll2_view", "scroll2_content") end diff --git a/example/page/scroll.lua b/example/page/scroll.lua index 284615a..205b4e8 100644 --- a/example/page/scroll.lua +++ b/example/page/scroll.lua @@ -47,6 +47,8 @@ function M.setup_page(self) self.druid:new_scroll("children_scroll_2", "children_scroll_content_2") self.druid:new_scroll("children_scroll_3", "children_scroll_content_3") + self.druid:new_scroll("scroll_smaller_view", "scroll_smaller_content") + init_grid(self) end