diff --git a/druid/base/scroll.lua b/druid/base/scroll.lua index 1632bc5..3c65a40 100644 --- a/druid/base/scroll.lua +++ b/druid/base/scroll.lua @@ -27,7 +27,6 @@ local function on_scroll_drag(self, dx, dy) local t = self.target_pos local b = self.available_pos local eb = self.available_pos_extra - local extra_size = self.style.EXTRA_STRECH_SIZE -- Handle soft zones -- Percent - multiplier for delta. Less if outside of scroll zone @@ -52,7 +51,7 @@ local function on_scroll_drag(self, dx, dy) y_perc = inverse_lerp(eb.y, b.y, t.y) end -- Bot border (maximum y) - if t.y > b.w and dy > 0 and extra_size > 0 then + if t.y > b.w and dy > 0 then y_perc = inverse_lerp(eb.w, b.w, t.y) end if not self.can_y then @@ -141,15 +140,16 @@ local function check_points(self) local index = false local index_on_inert = false local pos = self.current_pos + for i = 1, #self.points do local p = self.points[i] - local dist = helper.distance(pos.x, pos.y, p.x, p.y) + local dist = helper.distance(pos.x, pos.y, -p.x, -p.y) local on_inert = true -- If inert ~= 0, scroll only by move direction - if inert.x ~= 0 and helper.sign(inert.x) ~= helper.sign(p.x - pos.x) then + if inert.x ~= 0 and helper.sign(inert.x) ~= helper.sign(-p.x - pos.x) then on_inert = false end - if inert.y ~= 0 and helper.sign(inert.y) ~= helper.sign(p.y - pos.y) then + if inert.y ~= 0 and helper.sign(inert.y) ~= helper.sign(-p.y - pos.y) then on_inert = false end @@ -179,10 +179,7 @@ local function check_threshold(self) self.inertion.y = 0 end - if is_stopped or not self.inert then - if self.points then - print("check points free inert?") - end + if is_stopped or not self.is_inert then check_points(self) end end @@ -231,12 +228,7 @@ local function update_size(self) --== AVAILABLE POSITION -- (min_x, min_y, max_x, max_y) - self.available_pos = vmath.vector4( - self.view_border.x - self.content_border.x, - self.view_border.y - self.content_border.y, - self.view_border.z - self.content_border.z, - self.view_border.w - self.content_border.w - ) + self.available_pos = self.view_border - self.content_border if self.available_pos.x > self.available_pos.z then self.available_pos.x, self.available_pos.z = self.available_pos.z, self.available_pos.x @@ -261,14 +253,14 @@ local function update_size(self) self.content_size_extra = helper.get_border(self.content_node) if self.can_x then local sign = self.content_size.x > self.view_size.x and 1 or -1 - self.content_size_extra.x = self.content_size_extra.x - self.style.EXTRA_STRECH_SIZE * sign - self.content_size_extra.z = self.content_size_extra.z + self.style.EXTRA_STRECH_SIZE * sign + self.content_size_extra.x = self.content_size_extra.x - self.extra_stretch_size * sign + self.content_size_extra.z = self.content_size_extra.z + self.extra_stretch_size * sign end if self.can_y then local sign = self.content_size.y > self.view_size.y and 1 or -1 - self.content_size_extra.y = self.content_size_extra.y + self.style.EXTRA_STRECH_SIZE * sign - self.content_size_extra.w = self.content_size_extra.w - self.style.EXTRA_STRECH_SIZE * sign + self.content_size_extra.y = self.content_size_extra.y + self.extra_stretch_size * sign + self.content_size_extra.w = self.content_size_extra.w - self.extra_stretch_size * sign end self.available_pos_extra = vmath.vector4( @@ -288,19 +280,6 @@ local function update_size(self) self.available_pos_extra.z - self.available_pos_extra.x, self.available_pos_extra.w - self.available_pos_extra.y, 0) - --== END CONTENT EXTRA - - -- print("VIEW BORDER", self.view_border) - -- print("CONTENT BORDER", self.content_border) - -- print("AVAILABLE POS", self.available_pos) - -- print("CURRENT POS", self.current_pos) - -- print("VIEW_SIZE", self.view_size) - -- print("CONTENT_SIZE", self.content_size) - -- print("AVAILABLE_SIZE", self.available_size) - - -- print("CONTENT SIZE EXTRA", self.content_size_extra) - -- print("AVAILABLE POS EXTRA", self.available_pos_extra) - -- print("") end @@ -330,6 +309,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(0) + self.extra_stretch_size = self.style.EXTRA_STRECH_SIZE self.drag = self.druid:new_drag(view_zone, on_scroll_drag) self.drag.on_touch_start:subscribe(on_touch_start) @@ -368,9 +348,9 @@ end -- @usage scroll:scroll_to(vmath.vector3(0), true) function M.scroll_to(self, point, is_instant) local b = self.available_pos - local target = vmath.vector3(point) - target.x = helper.clamp(point.x, b.x, b.z) - target.y = helper.clamp(point.y, b.y, b.w) + local target = vmath.vector3(-point.x, -point.y, 0) + target.x = helper.clamp(target.x, b.x, b.z) + target.y = helper.clamp(target.y, b.y, b.w) cancel_animate(self) @@ -414,30 +394,19 @@ end function M.scroll_to_percent(self, percent, is_instant) local border = self.available_pos - local size_x = math.abs(border.z - border.x) - if size_x == 0 then - size_x = 1 - end - local size_y = math.abs(border.w - border.y) - if size_y == 0 then - size_y = 1 - end - local pos = vmath.vector3( - -size_x * percent.x + border.x, - -size_y * percent.y + border.y, - 0) + -helper.lerp(border.x, border.z, 1 - percent.x), + -helper.lerp(border.w, border.y, 1 - percent.y), + 0 + ) + M.scroll_to(self, pos, is_instant) end function M.get_percent(self) - local y_dist = self.available_size.y - local y_perc = y_dist ~= 0 and (self.current_pos.y - self.available_pos.w) / y_dist or 1 - - local x_dist = self.available_size.x - local x_perc = x_dist ~= 0 and (self.current_pos.x - self.available_pos.z) / x_dist or 1 - + local x_perc = 1 - inverse_lerp(self.available_pos.x, self.available_pos.z, self.current_pos.x) + local y_perc = inverse_lerp(self.available_pos.w, self.available_pos.y, self.current_pos.y) return vmath.vector3(x_perc, y_perc, 0) end @@ -454,17 +423,20 @@ function M.set_inert(self, state) end +function M.set_extra_strech_size(self, stretch_size) + self.extra_stretch_size = stretch_size or self.style.EXTRA_STRECH_SIZE + update_size(self) + + return self +end + + --- Set points of interest. -- Scroll will always centered on closer points -- @function scroll:set_points -- @tparam table points Array of vector3 points function M.set_points(self, points) self.points = points - -- cause of parent move in other side by y - for i = 1, #self.points do - self.points[i].x = -self.points[i].x - self.points[i].y = -self.points[i].y - end table.sort(self.points, function(a, b) return a.x > b.x or a.y < b.y @@ -472,8 +444,6 @@ function M.set_points(self, points) check_threshold(self) - pprint(self.points) - return self end diff --git a/druid/helper.lua b/druid/helper.lua index 0243de8..1e1076f 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -126,6 +126,11 @@ function M.round(num, numDecimalPlaces) end +function M.lerp(a, b, t) + return a + (b - a) * t +end + + --- Check if node is enabled in gui hierarchy. -- Return false, if node or any his parent is disabled -- @function helper.is_enabled diff --git a/druid/styles/default/style.lua b/druid/styles/default/style.lua index 7c4b63e..8c5b049 100644 --- a/druid/styles/default/style.lua +++ b/druid/styles/default/style.lua @@ -49,13 +49,13 @@ M["drag"] = { M["scroll"] = { - ANIM_SPEED = 0.3, -- gui.animation speed to point + ANIM_SPEED = 0.2, -- gui.animation speed to point SCROLL_WHEEL_SPEED = 10, DEADZONE = 20, FRICT = 0.94, -- mult for free inert FRICT_HOLD = 0.75, -- mult. for inert, while touching - INERT_THRESHOLD = 2, -- speed to stop inertion + INERT_THRESHOLD = 2.5, -- speed to stop inertion INERT_SPEED = 27, -- koef. of inert speed BACK_SPEED = 0.35, -- Lerp speed of return to soft position diff --git a/example/page/scroll_page.lua b/example/page/scroll_page.lua index 4d9e036..e49d018 100644 --- a/example/page/scroll_page.lua +++ b/example/page/scroll_page.lua @@ -1,7 +1,7 @@ local M = {} -local function init_grid(self) +local function init_scroll_with_grid(self) local prefab = gui.get_node("grid_prefab") local grid_scroll = self.druid:new_scroll("scroll_with_grid_size", "grid_content") @@ -15,7 +15,6 @@ local function init_grid(self) local button = self.druid:new_button(clone_prefab["grid_button"], function() local position = gui.get_position(clone_prefab["grid_prefab"]) - position.x = -position.x grid_scroll:scroll_to(position) end) button:set_click_zone(gui.get_node("scroll_with_grid_size")) @@ -26,7 +25,7 @@ local function init_grid(self) grid_scroll:set_size(grid:get_size()) local scroll_slider = self.druid:new_slider("grid_scroll_pin", vmath.vector3(300, 0, 0), function(_, value) - -- grid_scroll:scroll_to_percent(vmath.vector3(value, 0, 0), true) + grid_scroll:scroll_to_percent(vmath.vector3(value, 0, 0), true) end) grid_scroll.on_scroll:subscribe(function(_, point) @@ -36,19 +35,28 @@ end function M.setup_page(self) + -- Usual scroll for whole page self.druid:new_scroll("scroll_page", "scroll_page_content") + + -- Simple scroll with no adjust self.druid:new_scroll("simple_scroll_input", "simple_scroll_content") - -- scroll contain scrolls: - -- parent first + -- Scroll with grid example + init_scroll_with_grid(self) + + -- Scroll contain children scrolls: + -- Parent scroll self.druid:new_scroll("children_scroll", "children_scroll_content") - -- chilren next + -- Childre scrolls self.druid:new_scroll("children_scroll_1", "children_scroll_content_1") self.druid:new_scroll("children_scroll_2", "children_scroll_content_2") self.druid:new_scroll("children_scroll_3", "children_scroll_content_3") + -- Content with less size than view self.druid:new_scroll("scroll_smaller_view", "scroll_smaller_content") + :set_extra_strech_size(0) + -- Scroll with points of interests self.druid:new_scroll("scroll_with_points", "scroll_with_points_content") :set_points({ vmath.vector3(300, 0, 0), @@ -56,8 +64,6 @@ function M.setup_page(self) vmath.vector3(1500, 0, 0), vmath.vector3(2100, 0, 0), }) - - init_grid(self) end