mirror of
https://github.com/Insality/druid
synced 2025-09-27 18:12:21 +02:00
Update
This commit is contained in:
@@ -34,129 +34,6 @@ local component = require("druid.component")
|
||||
local M = component.create("drag", const.PRIORITY_INPUT_HIGH)
|
||||
|
||||
|
||||
local function start_touch(self, touch)
|
||||
self.is_touch = true
|
||||
self.is_drag = false
|
||||
|
||||
self.touch_start_pos.x = touch.x
|
||||
self.touch_start_pos.y = touch.y
|
||||
|
||||
self.x = touch.x
|
||||
self.y = touch.y
|
||||
|
||||
self.screen_x = touch.screen_x
|
||||
self.screen_y = touch.screen_y
|
||||
|
||||
self._scene_scale = helper.get_scene_scale(self.node)
|
||||
|
||||
self.on_touch_start:trigger(self:get_context(), touch)
|
||||
end
|
||||
|
||||
|
||||
local function end_touch(self, touch)
|
||||
if self.is_drag then
|
||||
self.on_drag_end:trigger(
|
||||
self:get_context(),
|
||||
self.x - self.touch_start_pos.x,
|
||||
self.y - self.touch_start_pos.y,
|
||||
touch
|
||||
)
|
||||
end
|
||||
|
||||
self.is_drag = false
|
||||
if self.is_touch then
|
||||
self.is_touch = false
|
||||
self.on_touch_end:trigger(self:get_context(), touch)
|
||||
end
|
||||
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.x
|
||||
end
|
||||
|
||||
if not self.can_y then
|
||||
self.touch_start_pos.y = touch.y
|
||||
end
|
||||
|
||||
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.style.DRAG_DEADZONE then
|
||||
self.is_drag = true
|
||||
self.on_drag_start:trigger(self:get_context(), touch)
|
||||
self:set_input_priority(const.PRIORITY_INPUT_MAX, true)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
---Return current touch action from action input data
|
||||
---If touch_id stored - return exact this touch action
|
||||
---@param action_id hash Action id from on_input
|
||||
---@param action table Action from on_input
|
||||
---@param touch_id number Touch id
|
||||
---@return table|nil Touch action
|
||||
local function find_touch(action_id, action, touch_id)
|
||||
local act = helper.is_mobile() and const.ACTION_MULTITOUCH or const.ACTION_TOUCH
|
||||
|
||||
if action_id ~= act then
|
||||
return
|
||||
end
|
||||
|
||||
if action.touch then
|
||||
local touch = action.touch
|
||||
for i = 1, #touch do
|
||||
if touch[i].id == touch_id then
|
||||
return touch[i]
|
||||
end
|
||||
end
|
||||
return touch[1]
|
||||
else
|
||||
return action
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
---Process on touch release. We should to find, if any other
|
||||
---touches exists to switch to another touch.
|
||||
---@param self druid.drag
|
||||
---@param action_id hash Action id from on_input
|
||||
---@param action table Action from on_input
|
||||
local function on_touch_release(self, action_id, action)
|
||||
if #action.touch >= 2 then
|
||||
-- Find next unpressed touch
|
||||
local next_touch
|
||||
for i = 1, #action.touch do
|
||||
if not action.touch[i].released then
|
||||
next_touch = action.touch[i]
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if next_touch then
|
||||
self.x = next_touch.x
|
||||
self.y = next_touch.y
|
||||
self.touch_id = next_touch.id
|
||||
else
|
||||
end_touch(self)
|
||||
end
|
||||
elseif #action.touch == 1 then
|
||||
end_touch(self)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
---@param style druid.drag.style
|
||||
function M:on_style_change(style)
|
||||
self.style = {
|
||||
DRAG_DEADZONE = style.DRAG_DEADZONE or 10,
|
||||
NO_USE_SCREEN_KOEF = style.NO_USE_SCREEN_KOEF or false,
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
---Drag constructor
|
||||
---@param node_or_node_id node|string
|
||||
---@param on_drag_callback function
|
||||
function M:init(node_or_node_id, on_drag_callback)
|
||||
@@ -193,6 +70,15 @@ function M:init(node_or_node_id, on_drag_callback)
|
||||
end
|
||||
|
||||
|
||||
---@param style druid.drag.style
|
||||
function M:on_style_change(style)
|
||||
self.style = {
|
||||
DRAG_DEADZONE = style.DRAG_DEADZONE or 10,
|
||||
NO_USE_SCREEN_KOEF = style.NO_USE_SCREEN_KOEF or false,
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
---Set Drag component enabled state.
|
||||
---@param is_enabled boolean
|
||||
function M:set_drag_cursors(is_enabled)
|
||||
@@ -226,12 +112,11 @@ end
|
||||
|
||||
function M:on_input_interrupt()
|
||||
if self.is_drag or self.is_touch then
|
||||
end_touch(self)
|
||||
self:_end_touch()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
---@local
|
||||
---@param action_id hash
|
||||
---@param action table
|
||||
function M:on_input(action_id, action)
|
||||
@@ -245,12 +130,12 @@ function M:on_input(action_id, action)
|
||||
|
||||
local is_pick = helper.pick_node(self.node, action.x, action.y, self.click_zone)
|
||||
if not is_pick and not self.is_drag then
|
||||
end_touch(self)
|
||||
self:_end_touch()
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
local touch = find_touch(action_id, action, self.touch_id)
|
||||
local touch = self:_find_touch(action_id, action, self.touch_id)
|
||||
if not touch then
|
||||
return false
|
||||
end
|
||||
@@ -263,24 +148,24 @@ function M:on_input(action_id, action)
|
||||
self.dy = 0
|
||||
|
||||
if touch.pressed and not self.is_touch then
|
||||
start_touch(self, touch)
|
||||
self:_start_touch(touch)
|
||||
end
|
||||
|
||||
if touch.released and self.is_touch then
|
||||
if action.touch then
|
||||
-- Mobile
|
||||
on_touch_release(self, action_id, action)
|
||||
self:_on_touch_release(action_id, action)
|
||||
else
|
||||
-- PC
|
||||
end_touch(self, touch)
|
||||
self:_end_touch(touch)
|
||||
end
|
||||
end
|
||||
|
||||
if self.is_touch then
|
||||
process_touch(self, touch)
|
||||
self:_process_touch(touch)
|
||||
end
|
||||
|
||||
local touch_modified = find_touch(action_id, action, self.touch_id)
|
||||
local touch_modified = self:_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
|
||||
@@ -338,4 +223,118 @@ function M:is_enabled()
|
||||
end
|
||||
|
||||
|
||||
function M:_start_touch(touch)
|
||||
self.is_touch = true
|
||||
self.is_drag = false
|
||||
|
||||
self.touch_start_pos.x = touch.x
|
||||
self.touch_start_pos.y = touch.y
|
||||
|
||||
self.x = touch.x
|
||||
self.y = touch.y
|
||||
|
||||
self.screen_x = touch.screen_x
|
||||
self.screen_y = touch.screen_y
|
||||
|
||||
self._scene_scale = helper.get_scene_scale(self.node)
|
||||
|
||||
self.on_touch_start:trigger(self:get_context(), touch)
|
||||
end
|
||||
|
||||
|
||||
---@param touch touch|nil
|
||||
function M:_end_touch(touch)
|
||||
if self.is_drag then
|
||||
self.on_drag_end:trigger(
|
||||
self:get_context(),
|
||||
self.x - self.touch_start_pos.x,
|
||||
self.y - self.touch_start_pos.y,
|
||||
touch
|
||||
)
|
||||
end
|
||||
|
||||
self.is_drag = false
|
||||
if self.is_touch then
|
||||
self.is_touch = false
|
||||
self.on_touch_end:trigger(self:get_context(), touch)
|
||||
end
|
||||
self:reset_input_priority()
|
||||
self.touch_id = 0
|
||||
end
|
||||
|
||||
|
||||
---@param touch touch
|
||||
function M:_process_touch(touch)
|
||||
if not self.can_x then
|
||||
self.touch_start_pos.x = touch.x
|
||||
end
|
||||
|
||||
if not self.can_y then
|
||||
self.touch_start_pos.y = touch.y
|
||||
end
|
||||
|
||||
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.style.DRAG_DEADZONE then
|
||||
self.is_drag = true
|
||||
self.on_drag_start:trigger(self:get_context(), touch)
|
||||
self:set_input_priority(const.PRIORITY_INPUT_MAX, true)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
---Return current touch action from action input data
|
||||
---If touch_id stored - return exact this touch action
|
||||
---@param action_id hash Action id from on_input
|
||||
---@param action table Action from on_input
|
||||
---@param touch_id number Touch id
|
||||
---@return table|nil Touch action
|
||||
function M:_find_touch(action_id, action, touch_id)
|
||||
local act = helper.is_mobile() and const.ACTION_MULTITOUCH or const.ACTION_TOUCH
|
||||
|
||||
if action_id ~= act then
|
||||
return
|
||||
end
|
||||
|
||||
if action.touch then
|
||||
local touch = action.touch
|
||||
for i = 1, #touch do
|
||||
if touch[i].id == touch_id then
|
||||
return touch[i]
|
||||
end
|
||||
end
|
||||
return touch[1]
|
||||
else
|
||||
return action
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
---Process on touch release. We should to find, if any other
|
||||
---touches exists to switch to another touch.
|
||||
---@param action_id hash Action id from on_input
|
||||
---@param action table Action from on_input
|
||||
function M:_on_touch_release(action_id, action)
|
||||
if #action.touch >= 2 then
|
||||
-- Find next unpressed touch
|
||||
local next_touch
|
||||
for i = 1, #action.touch do
|
||||
if not action.touch[i].released then
|
||||
next_touch = action.touch[i]
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if next_touch then
|
||||
self.x = next_touch.x
|
||||
self.y = next_touch.y
|
||||
self.touch_id = next_touch.id
|
||||
else
|
||||
self:_end_touch()
|
||||
end
|
||||
elseif #action.touch == 1 then
|
||||
self:_end_touch()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return M
|
||||
|
Reference in New Issue
Block a user