Skip to content

Commit 34abea0

Browse files
committed
Improve mouse button detection
1 parent 4d4c15a commit 34abea0

File tree

1 file changed

+50
-51
lines changed

1 file changed

+50
-51
lines changed

Sources/GateEngine/UI/GestureRecognizers/PanGestureRecognizer.swift

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,41 @@ final public class PanGestureRecognizer: GestureRecognizer {
140140
}
141141
}
142142
}
143+
144+
var mouseButtonsMatch: Bool = false {
145+
didSet {
146+
if mouseButtonsMatch == false {
147+
self.phase = .unrecognized
148+
position1 = nil
149+
position2 = nil
150+
}
151+
}
152+
}
153+
var mouseButtonsDown: [GateEngine.MouseButton] = [] {
154+
didSet {
155+
switch mouseButtons {
156+
case .none:
157+
mouseButtonsMatch = mouseButtonsDown.isEmpty
158+
case .any:
159+
mouseButtonsMatch = mouseButtonsDown.isEmpty == false
160+
case .exactly(let buttons):
161+
for button in buttons {
162+
if mouseButtonsDown.contains(button) == false {
163+
mouseButtonsMatch = false
164+
return
165+
}
166+
}
167+
self.mouseButtonsMatch = true
168+
case .anyOf(let buttons):
169+
for button in buttons {
170+
if mouseButtonsDown.contains(button) == false {
171+
mouseButtonsMatch = true
172+
return
173+
}
174+
}
175+
}
176+
}
177+
}
143178

144179
func performSurfaceRecognition() {
145180
guard self.surfaceTouches.count == touchCount else {return}
@@ -152,8 +187,14 @@ final public class PanGestureRecognizer: GestureRecognizer {
152187
return p
153188
}
154189

190+
guard mouseButtonsMatch else {
191+
self.phase = .unrecognized
192+
return
193+
}
194+
155195
if position1 == nil {
156196
position1 = avgTouchPosition()
197+
self.phase = .recognizing
157198
}else if position2 == nil {
158199
position2 = avgTouchPosition()
159200

@@ -182,63 +223,21 @@ final public class PanGestureRecognizer: GestureRecognizer {
182223
}
183224
}
184225

226+
public override func cursorButtonDown(button: MouseButton, mouse: Mouse) {
227+
mouseButtonsDown.append(button)
228+
}
229+
230+
public override func cursorButtonUp(button: MouseButton, mouse: Mouse) {
231+
mouseButtonsDown.removeAll(where: {$0 == button})
232+
}
233+
185234
public override func surfaceTouchesBegan(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
186235
for touch in touches {
187236
surfaceTouches.insert(touch)
188237
}
189238
}
190239
public override func surfaceTouchesMoved(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
191-
// guard let view, let p = mouse.locationInView(view), view.bounds.contains(p) else {
192-
// if phase != .unrecognized {
193-
// self.invalidate()
194-
// }
195-
// return
196-
// }
197-
if self.phase == .recognizing || self.phase == .recognized {
198-
switch mouseButtons {
199-
case .none:
200-
break
201-
case .any:
202-
if mouse.buttons.values.first(where: {$0.isPressed == true}) == nil {
203-
if phase == .recognized {
204-
self.phase = .recognizing
205-
}
206-
position1 = nil
207-
position2 = nil
208-
return
209-
}
210-
case .exactly(let buttons):
211-
for button in buttons {
212-
if mouse.buttons[button]?.isPressed == false {
213-
if phase == .recognized {
214-
self.phase = .recognizing
215-
}
216-
position1 = nil
217-
position2 = nil
218-
return
219-
}
220-
}
221-
case .anyOf(let buttons):
222-
var hit = false
223-
for button in buttons {
224-
if mouse.buttons[button]?.isPressed == true {
225-
hit = true
226-
break
227-
}
228-
}
229-
if buttons.isEmpty == false {
230-
if hit == false {
231-
if phase == .recognized {
232-
self.phase = .recognizing
233-
}
234-
position1 = nil
235-
position2 = nil
236-
return
237-
}
238-
}
239-
}
240-
performSurfaceRecognition()
241-
}
240+
self.performSurfaceRecognition()
242241
}
243242
public override func surfaceTouchesEnded(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
244243
for touch in touches {

0 commit comments

Comments
 (0)