Skip to content

Commit

Permalink
Update indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Oct 6, 2023
1 parent 60a66df commit 4803a0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions appendix/GamePadViewer/src/StickManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ public class StickManager: ObservableObject {

@MainActor
func update() {
horizontal.update()
vertical.update()

radian = atan2(vertical.lastDoubleValue, horizontal.lastDoubleValue)
magnitude = sqrt(pow(vertical.lastDoubleValue, 2) + pow(horizontal.lastDoubleValue, 2))

horizontal.update()
vertical.update()
}
}

Expand Down
32 changes: 16 additions & 16 deletions appendix/GamePadViewer/src/View/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct ContentView: View {
@ObservedObject var stickManager = StickManager.shared

let circleSize = 100.0
let indicatorSize = 10.0
let stickDivider = 50.0

var body: some View {
Expand All @@ -19,11 +20,25 @@ struct ContentView: View {
StickSensorInfo(label: "Right Stick Y", stick: stickManager.rightStick.vertical)
}

ZStack(alignment: .center) {
ZStack(alignment: .topLeading) {
Circle()
.stroke(Color.gray, lineWidth: 2)
.frame(width: circleSize, height: circleSize)

Circle()
.fill(Color.blue)
.frame(width: indicatorSize, height: indicatorSize)
.padding(
.leading,
circleSize / 2.0 + cos(stickManager.rightStick.radian)
* stickManager.rightStick.magnitude * circleSize / 2.0 - indicatorSize / 2.0
)
.padding(
.top,
circleSize / 2.0 + sin(stickManager.rightStick.radian)
* stickManager.rightStick.magnitude * circleSize / 2.0 - indicatorSize / 2.0
)

Path { path in
path.move(to: CGPoint(x: circleSize / 2.0, y: circleSize / 2.0))
path.addLine(
Expand All @@ -36,21 +51,6 @@ struct ContentView: View {
}
.stroke(Color.red, lineWidth: 2)
.frame(width: circleSize, height: circleSize)

Path { path in
path.move(to: CGPoint(x: circleSize / 2.0, y: circleSize / 2.0))
path.addLine(
to: CGPoint(
x: cos(stickManager.rightStick.radian) * stickManager.rightStick.magnitude
* circleSize / 2.0
+ circleSize / 2.0,
y: sin(stickManager.rightStick.radian) * stickManager.rightStick.magnitude
* circleSize / 2.0
+ circleSize / 2.0
))
}
.stroke(Color.blue, lineWidth: 2)
.frame(width: circleSize, height: circleSize)
}
}
.alert(isPresented: inputMonitoringAlertData.showing) {
Expand Down

0 comments on commit 4803a0c

Please sign in to comment.