-
Notifications
You must be signed in to change notification settings - Fork 0
/
zoom_gesture.lua
172 lines (145 loc) · 4.48 KB
/
zoom_gesture.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
-- Kazari, a touch gesture library.
--
-- Copyright (C) 2022 Miku AuahDark
--
-- This software is provided 'as-is', without any express or implied
-- warranty. In no event will the authors be held liable for any damages
-- arising from the use of this software.
--
-- Permission is granted to anyone to use this software for any purpose,
-- including commercial applications, and to alter it and redistribute it
-- freely, subject to the following restrictions:
--
-- 1. The origin of this software must not be misrepresented; you must not
-- claim that you wrote the original software. If you use this software
-- in a product, an acknowledgment in the product documentation would be
-- appreciated but is not required.
-- 2. Altered source versions must be plainly marked as such, and must not be
-- misrepresented as being the original software.
-- 3. This notice may not be removed or altered from any source distribution.
---@type string
local path = (...):sub(1, -string.len(".zoom_gesture") - 1)
---@type Kazari.BaseGesture
local BaseGesture = require(path..".base_gesture")
---@type Kazari.Util
local util = require(path..".util")
---Implements pinch zoom touch gesture.
---@class Kazari.ZoomGesture: Kazari.BaseGesture
local ZoomGesture = {}
ZoomGesture.__index = ZoomGesture ---@private
ZoomGesture.__parent = BaseGesture ---@private
---@package
---@param clip boolean?
---@param constraint Kazari.AnyConstraint?
function ZoomGesture:init(clip, constraint)
self.constraint = constraint ---@private
self.clipTouch = not not clip ---@private
end
---@generic T
---@param context T
---@param func fun(context:T, scale: number, midX: number, midY: number)
function ZoomGesture:onZoom(context, func)
self.onZoomContext = context ---@private
self.onZoomCallback = func ---@private
end
---@generic U
---@param context U
---@param func fun(context:U, scale: number)
function ZoomGesture:onZoomComplete(context, func)
self.onZoomDoneContext = context ---@private
self.onZoomDoneCallback = func ---@private
end
---@param x number
---@param y number
---@param pressure number
function ZoomGesture:touchpressed(id, x, y, pressure)
if not util.pointInConstraint(x, y, self.constraint) then
return false
end
if not self.t1 then
self.t1 = {id, x, y} ---@private
elseif not self.t2 then
self.t2 = {id, x, y} ---@private
-- Start gesture
self.initDistance = util.distance(self.t1[2], self.t1[3], self.t2[2], self.t2[3]) ---@private
self:_updateGesture(false)
else
return false
end
return true
end
---@param x number
---@param y number
---@param dx number
---@param dy number
---@param pressure number
function ZoomGesture:touchmoved(id, x, y, dx, dy, pressure)
-- Update gesture
if self.t1 and id == self.t1[1] then
if self.clipTouch then
x, y = util.ensurePointInside(x, y, self.constraint)
end
self.t1[2], self.t1[3] = x, y
if self.t2 then
self:_updateGesture(false)
end
elseif self.t2 and id == self.t2[1] then
if self.clipTouch then
x, y = util.ensurePointInside(x, y, self.constraint)
end
self.t2[2], self.t2[3] = x, y
self:_updateGesture(false)
else
return false
end
return true
end
---@param x number
---@param y number
function ZoomGesture:touchreleased(id, x, y)
if self.t1 and id == self.t1[1] then
if self.clipTouch then
x, y = util.ensurePointInside(x, y, self.constraint)
end
self.t1[2], self.t1[3] = x, y
if self.t2 then
self:_updateGesture(true)
end
self.t1 = self.t2 ---@private
self.t2 = nil ---@private
elseif self.t2 and id == self.t2[1] then
if self.clipTouch then
x, y = util.ensurePointInside(x, y, self.constraint)
end
self.t2[2], self.t2[3] = x, y
self:_updateGesture(true)
self.t2 = nil ---@private
else
return false
end
return true
end
---@private
---@param finish boolean
function ZoomGesture:_updateGesture(finish)
local d = util.distance(self.t1[2], self.t1[3], self.t2[2], self.t2[3])
local scale = d / self.initDistance
local cx = (self.t1[2] + self.t2[2]) / 2
local cy = (self.t1[3] + self.t2[3]) / 2
if finish and self.onZoomDoneCallback then
self.onZoomDoneCallback(self.onZoomDoneContext, scale)
elseif self.onZoomCallback then
self.onZoomCallback(self.onZoomContext, scale, cx, cy)
end
end
function ZoomGesture:__tostring()
return string.format("ZoomGesture<%p>(%s, %p)", self, self.clipTouch, self.constraint)
end
setmetatable(ZoomGesture, {
__call = function(_, clip, constraint)
local object = setmetatable({}, ZoomGesture)
object:init(clip, constraint)
return object
end
})
return ZoomGesture