Update documentation, refactoring

This commit is contained in:
Insality
2020-05-04 00:16:08 +03:00
parent 0c1bf63e13
commit 1e568c2fa6
12 changed files with 631 additions and 678 deletions

View File

@@ -1,14 +1,30 @@
--- Component to handle drag action on node
--- Component to handle drag action on node.
-- Drag have correct handling for multitouch and swap
-- touched while dragging. Drag will be processed even
-- the cursor is outside of node, if drag is already started
-- @module druid.drag
--- Components fields
-- @table Fields
--- Component events
-- @table Events
-- @tfield druid_event on_touch_start (self) Event on touch start
-- @tfield druid_event on_touch_end (self) Event on touch end
-- @tfield druid_event on_drag_start (self) Event on drag start
-- @tfield druid_event on_drag (self, dx, dy) Event on drag progress
-- @tfield druid_event on_drag_end (self) Event on drag end
--- Components fields
-- @table Fields
-- @tfield bool is_touch Is component now touching
-- @tfield bool is_drag Is component now dragging
-- @tfield bool can_x Is drag component process vertical dragging. Default - true
-- @tfield bool can_y Is drag component process horizontal. Default - true
-- @tfield number x Current touch x position
-- @tfield number y Current touch y position
-- @tfield vector3 touch_start_pos Touch start position
--- Component style params
-- @table Style
-- @tfield number DRAG_DEADZONE Distance in pixels to start dragging
local Event = require("druid.event")
local const = require("druid.const")
@@ -49,12 +65,13 @@ 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.drag_deadzone then
if not self.is_drag and distance >= self.style.DRAG_DEADZONE then
self.is_drag = true
self.on_drag_start:trigger(self:get_context())
self:increase_input_priority()
@@ -62,6 +79,8 @@ local function process_touch(self, touch)
end
--- Return current touch action from action input data
-- If touch_id stored - return exact this touch action
local function find_touch(action_id, action, touch_id)
local act = helper.is_mobile() and const.ACTION_MULTITOUCH or const.ACTION_TOUCH
@@ -83,6 +102,8 @@ local function find_touch(action_id, action, touch_id)
end
--- Process on touch release. We should to find, if any other
-- touches exists to switch to another touch.
local function on_touch_release(self, action_id, action)
if #action.touch >= 2 then
-- Find next unpressed touch
@@ -107,14 +128,14 @@ local function on_touch_release(self, action_id, action)
end
--- Component init function
--- Drag component constructor
-- @tparam node node GUI node to detect dragging
-- @tparam function on_drag_callback Callback for on_drag_event(self, dx, dy)
-- @function drag:init
function M.init(self, node, on_drag_callback)
self.style = self:get_style()
self.node = self:get_node(node)
self.drag_deadzone = self.style.DRAG_DEADZONE or 10
self.dx = 0
self.dy = 0
self.touch_id = 0
@@ -162,8 +183,6 @@ function M.on_input(self, action_id, action)
return false
end
self.dx = 0
self.dy = 0
local touch = find_touch(action_id, action, self.touch_id)
if not touch then
@@ -174,6 +193,9 @@ function M.on_input(self, action_id, action)
self.touch_id = touch.id
end
self.dx = 0
self.dy = 0
if touch.pressed and not self.is_touch then
start_touch(self, touch)
end