mirror of
https://github.com/Insality/druid
synced 2025-06-27 18:37:45 +02:00
Scroll polishing
This commit is contained in:
parent
99781f333d
commit
facfe39269
@ -27,7 +27,6 @@ local function on_scroll_drag(self, dx, dy)
|
|||||||
local t = self.target_pos
|
local t = self.target_pos
|
||||||
local b = self.available_pos
|
local b = self.available_pos
|
||||||
local eb = self.available_pos_extra
|
local eb = self.available_pos_extra
|
||||||
local extra_size = self.style.EXTRA_STRECH_SIZE
|
|
||||||
|
|
||||||
-- Handle soft zones
|
-- Handle soft zones
|
||||||
-- Percent - multiplier for delta. Less if outside of scroll zone
|
-- 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)
|
y_perc = inverse_lerp(eb.y, b.y, t.y)
|
||||||
end
|
end
|
||||||
-- Bot border (maximum y)
|
-- 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)
|
y_perc = inverse_lerp(eb.w, b.w, t.y)
|
||||||
end
|
end
|
||||||
if not self.can_y then
|
if not self.can_y then
|
||||||
@ -141,15 +140,16 @@ local function check_points(self)
|
|||||||
local index = false
|
local index = false
|
||||||
local index_on_inert = false
|
local index_on_inert = false
|
||||||
local pos = self.current_pos
|
local pos = self.current_pos
|
||||||
|
|
||||||
for i = 1, #self.points do
|
for i = 1, #self.points do
|
||||||
local p = self.points[i]
|
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
|
local on_inert = true
|
||||||
-- If inert ~= 0, scroll only by move direction
|
-- 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
|
on_inert = false
|
||||||
end
|
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
|
on_inert = false
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -179,10 +179,7 @@ local function check_threshold(self)
|
|||||||
self.inertion.y = 0
|
self.inertion.y = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
if is_stopped or not self.inert then
|
if is_stopped or not self.is_inert then
|
||||||
if self.points then
|
|
||||||
print("check points free inert?")
|
|
||||||
end
|
|
||||||
check_points(self)
|
check_points(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -231,12 +228,7 @@ local function update_size(self)
|
|||||||
|
|
||||||
--== AVAILABLE POSITION
|
--== AVAILABLE POSITION
|
||||||
-- (min_x, min_y, max_x, max_y)
|
-- (min_x, min_y, max_x, max_y)
|
||||||
self.available_pos = vmath.vector4(
|
self.available_pos = self.view_border - self.content_border
|
||||||
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
|
|
||||||
)
|
|
||||||
|
|
||||||
if self.available_pos.x > self.available_pos.z then
|
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
|
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)
|
self.content_size_extra = helper.get_border(self.content_node)
|
||||||
if self.can_x then
|
if self.can_x then
|
||||||
local sign = self.content_size.x > self.view_size.x and 1 or -1
|
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.x = self.content_size_extra.x - self.extra_stretch_size * sign
|
||||||
self.content_size_extra.z = self.content_size_extra.z + self.style.EXTRA_STRECH_SIZE * sign
|
self.content_size_extra.z = self.content_size_extra.z + self.extra_stretch_size * sign
|
||||||
end
|
end
|
||||||
|
|
||||||
if self.can_y then
|
if self.can_y then
|
||||||
local sign = self.content_size.y > self.view_size.y and 1 or -1
|
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.y = self.content_size_extra.y + self.extra_stretch_size * sign
|
||||||
self.content_size_extra.w = self.content_size_extra.w - self.style.EXTRA_STRECH_SIZE * sign
|
self.content_size_extra.w = self.content_size_extra.w - self.extra_stretch_size * sign
|
||||||
end
|
end
|
||||||
|
|
||||||
self.available_pos_extra = vmath.vector4(
|
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.z - self.available_pos_extra.x,
|
||||||
self.available_pos_extra.w - self.available_pos_extra.y,
|
self.available_pos_extra.w - self.available_pos_extra.y,
|
||||||
0)
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -330,6 +309,7 @@ function M.init(self, view_zone, content_zone)
|
|||||||
self.current_pos = gui.get_position(self.content_node)
|
self.current_pos = gui.get_position(self.content_node)
|
||||||
self.target_pos = vmath.vector3(self.current_pos)
|
self.target_pos = vmath.vector3(self.current_pos)
|
||||||
self.inertion = vmath.vector3(0)
|
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 = self.druid:new_drag(view_zone, on_scroll_drag)
|
||||||
self.drag.on_touch_start:subscribe(on_touch_start)
|
self.drag.on_touch_start:subscribe(on_touch_start)
|
||||||
@ -368,9 +348,9 @@ end
|
|||||||
-- @usage scroll:scroll_to(vmath.vector3(0), true)
|
-- @usage scroll:scroll_to(vmath.vector3(0), true)
|
||||||
function M.scroll_to(self, point, is_instant)
|
function M.scroll_to(self, point, is_instant)
|
||||||
local b = self.available_pos
|
local b = self.available_pos
|
||||||
local target = vmath.vector3(point)
|
local target = vmath.vector3(-point.x, -point.y, 0)
|
||||||
target.x = helper.clamp(point.x, b.x, b.z)
|
target.x = helper.clamp(target.x, b.x, b.z)
|
||||||
target.y = helper.clamp(point.y, b.y, b.w)
|
target.y = helper.clamp(target.y, b.y, b.w)
|
||||||
|
|
||||||
cancel_animate(self)
|
cancel_animate(self)
|
||||||
|
|
||||||
@ -414,30 +394,19 @@ end
|
|||||||
function M.scroll_to_percent(self, percent, is_instant)
|
function M.scroll_to_percent(self, percent, is_instant)
|
||||||
local border = self.available_pos
|
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(
|
local pos = vmath.vector3(
|
||||||
-size_x * percent.x + border.x,
|
-helper.lerp(border.x, border.z, 1 - percent.x),
|
||||||
-size_y * percent.y + border.y,
|
-helper.lerp(border.w, border.y, 1 - percent.y),
|
||||||
0)
|
0
|
||||||
|
)
|
||||||
|
|
||||||
M.scroll_to(self, pos, is_instant)
|
M.scroll_to(self, pos, is_instant)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function M.get_percent(self)
|
function M.get_percent(self)
|
||||||
local y_dist = self.available_size.y
|
local x_perc = 1 - inverse_lerp(self.available_pos.x, self.available_pos.z, self.current_pos.x)
|
||||||
local y_perc = y_dist ~= 0 and (self.current_pos.y - self.available_pos.w) / y_dist or 1
|
local y_perc = inverse_lerp(self.available_pos.w, self.available_pos.y, self.current_pos.y)
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
return vmath.vector3(x_perc, y_perc, 0)
|
return vmath.vector3(x_perc, y_perc, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -454,17 +423,20 @@ function M.set_inert(self, state)
|
|||||||
end
|
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.
|
--- Set points of interest.
|
||||||
-- Scroll will always centered on closer points
|
-- Scroll will always centered on closer points
|
||||||
-- @function scroll:set_points
|
-- @function scroll:set_points
|
||||||
-- @tparam table points Array of vector3 points
|
-- @tparam table points Array of vector3 points
|
||||||
function M.set_points(self, points)
|
function M.set_points(self, points)
|
||||||
self.points = 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)
|
table.sort(self.points, function(a, b)
|
||||||
return a.x > b.x or a.y < b.y
|
return a.x > b.x or a.y < b.y
|
||||||
@ -472,8 +444,6 @@ function M.set_points(self, points)
|
|||||||
|
|
||||||
check_threshold(self)
|
check_threshold(self)
|
||||||
|
|
||||||
pprint(self.points)
|
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -126,6 +126,11 @@ function M.round(num, numDecimalPlaces)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M.lerp(a, b, t)
|
||||||
|
return a + (b - a) * t
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Check if node is enabled in gui hierarchy.
|
--- Check if node is enabled in gui hierarchy.
|
||||||
-- Return false, if node or any his parent is disabled
|
-- Return false, if node or any his parent is disabled
|
||||||
-- @function helper.is_enabled
|
-- @function helper.is_enabled
|
||||||
|
@ -49,13 +49,13 @@ M["drag"] = {
|
|||||||
|
|
||||||
|
|
||||||
M["scroll"] = {
|
M["scroll"] = {
|
||||||
ANIM_SPEED = 0.3, -- gui.animation speed to point
|
ANIM_SPEED = 0.2, -- gui.animation speed to point
|
||||||
SCROLL_WHEEL_SPEED = 10,
|
SCROLL_WHEEL_SPEED = 10,
|
||||||
DEADZONE = 20,
|
DEADZONE = 20,
|
||||||
|
|
||||||
FRICT = 0.94, -- mult for free inert
|
FRICT = 0.94, -- mult for free inert
|
||||||
FRICT_HOLD = 0.75, -- mult. for inert, while touching
|
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
|
INERT_SPEED = 27, -- koef. of inert speed
|
||||||
|
|
||||||
BACK_SPEED = 0.35, -- Lerp speed of return to soft position
|
BACK_SPEED = 0.35, -- Lerp speed of return to soft position
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
|
||||||
local function init_grid(self)
|
local function init_scroll_with_grid(self)
|
||||||
local prefab = gui.get_node("grid_prefab")
|
local prefab = gui.get_node("grid_prefab")
|
||||||
|
|
||||||
local grid_scroll = self.druid:new_scroll("scroll_with_grid_size", "grid_content")
|
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 button = self.druid:new_button(clone_prefab["grid_button"], function()
|
||||||
local position = gui.get_position(clone_prefab["grid_prefab"])
|
local position = gui.get_position(clone_prefab["grid_prefab"])
|
||||||
position.x = -position.x
|
|
||||||
grid_scroll:scroll_to(position)
|
grid_scroll:scroll_to(position)
|
||||||
end)
|
end)
|
||||||
button:set_click_zone(gui.get_node("scroll_with_grid_size"))
|
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())
|
grid_scroll:set_size(grid:get_size())
|
||||||
|
|
||||||
local scroll_slider = self.druid:new_slider("grid_scroll_pin", vmath.vector3(300, 0, 0), function(_, value)
|
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)
|
end)
|
||||||
|
|
||||||
grid_scroll.on_scroll:subscribe(function(_, point)
|
grid_scroll.on_scroll:subscribe(function(_, point)
|
||||||
@ -36,19 +35,28 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function M.setup_page(self)
|
function M.setup_page(self)
|
||||||
|
-- Usual scroll for whole page
|
||||||
self.druid:new_scroll("scroll_page", "scroll_page_content")
|
self.druid:new_scroll("scroll_page", "scroll_page_content")
|
||||||
|
|
||||||
|
-- Simple scroll with no adjust
|
||||||
self.druid:new_scroll("simple_scroll_input", "simple_scroll_content")
|
self.druid:new_scroll("simple_scroll_input", "simple_scroll_content")
|
||||||
|
|
||||||
-- scroll contain scrolls:
|
-- Scroll with grid example
|
||||||
-- parent first
|
init_scroll_with_grid(self)
|
||||||
|
|
||||||
|
-- Scroll contain children scrolls:
|
||||||
|
-- Parent scroll
|
||||||
self.druid:new_scroll("children_scroll", "children_scroll_content")
|
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_1", "children_scroll_content_1")
|
||||||
self.druid:new_scroll("children_scroll_2", "children_scroll_content_2")
|
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("children_scroll_3", "children_scroll_content_3")
|
||||||
|
|
||||||
|
-- Content with less size than view
|
||||||
self.druid:new_scroll("scroll_smaller_view", "scroll_smaller_content")
|
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")
|
self.druid:new_scroll("scroll_with_points", "scroll_with_points_content")
|
||||||
:set_points({
|
:set_points({
|
||||||
vmath.vector3(300, 0, 0),
|
vmath.vector3(300, 0, 0),
|
||||||
@ -56,8 +64,6 @@ function M.setup_page(self)
|
|||||||
vmath.vector3(1500, 0, 0),
|
vmath.vector3(1500, 0, 0),
|
||||||
vmath.vector3(2100, 0, 0),
|
vmath.vector3(2100, 0, 0),
|
||||||
})
|
})
|
||||||
|
|
||||||
init_grid(self)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user