Skip to content

Commit f69db3d

Browse files
committed
Merge branch 'master' of https://github.com/britzl/defold-input
2 parents b90a743 + bdeffd8 commit f69db3d

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

in/cursor.script

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local cursor = require "in.cursor"
55
go.property("action_id", hash("touch"))
66
go.property("drag", true)
77
go.property("drag_threshold", 20)
8+
go.property("event_only_drag", false)
89
go.property("drag_lock_x", false)
910
go.property("drag_lock_y", false)
1011
go.property("collisionobject_filter", hash(""))
@@ -58,6 +59,8 @@ local function clear_drag_state(self)
5859
self.state.dragging = false
5960
self.state.dragging_cancelled = nil
6061
self.state.dragging_mode = nil
62+
self.action.origin_x = 0
63+
self.action.origin_y = 0
6164
end
6265

6366
local function clear_pressed_state(self)
@@ -254,6 +257,8 @@ function update(self, dt)
254257
-- start drag?
255258
local distance = vmath.length(diff)
256259
if distance >= self.drag_threshold then
260+
self.action.origin_x = self.state.pressed_pos.x
261+
self.action.origin_y = self.state.pressed_pos.y
257262
if trigger_event(self, self.state.pressed_id, self.state.pressed_group, self.action, cursor.DRAG_START) then
258263
start_dragging(self, self.state.pressed_id, mode)
259264
else
@@ -271,21 +276,23 @@ function update(self, dt)
271276
trigger_event(self, self.state.pressed_id, self.state.pressed_group, self.action, cursor.DRAG_END)
272277
trigger_event(self, self.state.pressed_id, self.state.pressed_group, self.action, cursor.RELEASED)
273278
else
274-
-- restrict movement to a single axis
275-
if self.state.drag_mode == cursor.DRAG_MODE_HORIZONTAL then
276-
cursor_pos.y = self.state.pressed_pos.y
277-
elseif self.state.drag_mode == cursor.DRAG_MODE_VERTICAL then
278-
cursor_pos.x = self.state.pressed_pos.x
279-
end
280-
-- retain the offset between the cursor and the dragged object and use the z value from the dragged object
281-
if self.state.pressed_id then
282-
cursor_pos.x = cursor_pos.x - self.state.pressed_offset.x
283-
cursor_pos.y = cursor_pos.y - self.state.pressed_offset.y
284-
cursor_pos.z = go.get_position(self.state.pressed_id).z
285-
go.set_position(cursor_pos, self.state.pressed_id)
279+
if not self.event_only_drag then
280+
-- restrict movement to a single axis
281+
if self.state.drag_mode == cursor.DRAG_MODE_HORIZONTAL then
282+
cursor_pos.y = self.state.pressed_pos.y
283+
elseif self.state.drag_mode == cursor.DRAG_MODE_VERTICAL then
284+
cursor_pos.x = self.state.pressed_pos.x
285+
end
286+
-- retain the offset between the cursor and the dragged object and use the z value from the dragged object
287+
if self.state.pressed_id then
288+
cursor_pos.x = cursor_pos.x - self.state.pressed_offset.x
289+
cursor_pos.y = cursor_pos.y - self.state.pressed_offset.y
290+
cursor_pos.z = go.get_position(self.state.pressed_id).z
291+
go.set_position(cursor_pos, self.state.pressed_id)
292+
end
293+
self.action.dx = self.state.pressed_pos.x - cursor_pos.x
294+
self.action.dy = self.state.pressed_pos.y - cursor_pos.y
286295
end
287-
self.action.dx = self.state.pressed_pos.x - cursor_pos.x
288-
self.action.dy = self.state.pressed_pos.y - cursor_pos.y
289296
trigger_event(self, self.state.pressed_id, self.state.pressed_group, self.action, cursor.DRAG)
290297
end
291298
end

0 commit comments

Comments
 (0)