-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
JoyStickViewMonitor.swift
94 lines (76 loc) · 2.78 KB
/
JoyStickViewMonitor.swift
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
// Copyright © 2020 Brad Howes. All rights reserved.
import CoreGraphics
/**
JoyStickView handle position as X, Y deltas from the base center. Note that here a positive `y` indicates that the
joystick handle is pushed upwards.
*/
public struct JoyStickViewXYReport {
/// Delta X of handle from base center
public let x: CGFloat // swiftlint:disable:this identifier_name
/// Delta Y of handle from base center
public let y: CGFloat // swiftlint:disable:this identifier_name
/**
Constructor of new XY report
- parameter x: X offset from center of the base
- parameter y: Y offset from center of the base (positive values towards up/north)
*/
public init(x: CGFloat, y: CGFloat) { // swiftlint:disable:this identifier_name
self.x = x
self.y = y
}
/// Convert this report into polar format
public var polar: JoyStickViewPolarReport {
return .init(angle: (180.0 - atan2(x, -y) * 180.0 / .pi), displacement: sqrt(x * x + y * y))
}
}
/**
JoyStickView handle position as angle/displacement values from the base center. Note that `angle` is given in degrees,
with 0° pointing up (north) and 90° pointing right (east).
*/
public struct JoyStickViewPolarReport {
/// Clockwise angle of the handle with respect to north/up of 0°.
public let angle: CGFloat
/// Distance from the center of the base
public let displacement: CGFloat
/**
Constructor of new polar report
- parameter angle: clockwise angle of the handle with respect to north/up of 0°.
- parameter displacement: distance from the center of the base
*/
public init(angle: CGFloat, displacement: CGFloat) {
self.angle = angle
self.displacement = displacement
}
/// Convert this report into XY format
public var rectangular: JoyStickViewXYReport {
let rads = angle * .pi / 180.0
return .init(x: sin(rads) * displacement, y: cos(rads) * displacement)
}
}
/**
Prototype of a monitor function that accepts a JoyStickViewXYReport.
*/
public typealias JoyStickViewXYMonitor = (_ value: JoyStickViewXYReport) -> Void
/**
Prototype of a monitor function that accepts a JoyStickViewXYReport.
*/
public typealias JoyStickViewPolarMonitor = (_ value: JoyStickViewPolarReport) -> Void
/**
Monitor kind. Determines the type of reporting that will be emitted from a JoyStickView instance.
*/
public enum JoyStickViewMonitorKind {
/**
Install monitor that accepts polar position change reports
- parameter monitor: function that accepts a JoyStickViewPolarReport
*/
case polar(monitor: JoyStickViewPolarMonitor)
/**
Install monitor that accepts cartesian (XY) position change reports
- parameter monitor: function that accepts a JoyStickViewXYReport
*/
case xy(monitor: JoyStickViewXYMonitor)
/**
No monitoring for a JoyStickView instance.
*/
case none
}