Skip to content

Commit

Permalink
Add pointerX,pointerY into StickManager.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Dec 24, 2023
1 parent 2f81df1 commit 3486646
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 66 deletions.
7 changes: 7 additions & 0 deletions appendix/GamePadViewer/src/StickManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class StickManager: ObservableObject {
@Published var deltaVertical = 0.0
@Published var deltaRadian = 0.0
@Published var deltaMagnitude = 0.0
@Published var pointerX = 0.5 // 0.0 ... 1.0
@Published var pointerY = 0.5 // 0.0 ... 1.0
let remainDeadzoneThresholdMilliseconds: UInt64 = 100
let strokeAccelerationMeasurementTime = 0.05 // 50 ms
let updateTimerInterval = 0.02 // 20 ms
Expand Down Expand Up @@ -144,6 +146,11 @@ public class StickManager: ObservableObject {
}
}

pointerX += deltaMagnitude * cos(deltaRadian)
pointerX = max(0.0, min(1.0, pointerX))
pointerY += deltaMagnitude * sin(deltaRadian)
pointerY = max(0.0, min(1.0, pointerY))

previousHorizontalDoubleValue = horizontal.lastDoubleValue
previousVerticalDoubleValue = vertical.lastDoubleValue
}
Expand Down
162 changes: 96 additions & 66 deletions appendix/GamePadViewer/src/View/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,74 +1,13 @@
import SwiftUI

struct ContentView: View {
@ObservedObject var inputMonitoringAlertData = InputMonitoringAlertData.shared
@ObservedObject var eventObserver = EventObserver.shared
@ObservedObject var rightStick = StickManager.shared.rightStick

let circleSize = 100.0
let indicatorSize = 10.0
@ObservedObject private var inputMonitoringAlertData = InputMonitoringAlertData.shared

var body: some View {
VStack {
VStack(alignment: .leading) {
Group {
Text("counter: \(eventObserver.counter)")
Text("horizontal: \(rightStick.horizontal.lastDoubleValue)")
Text("vertical: \(rightStick.vertical.lastDoubleValue)")
}
Divider()
Group {
Text("radian: \(rightStick.radian)")
Text("magnitude: \(rightStick.magnitude)")
Text("strokeAccelerationTransitionValue: \(rightStick.strokeAccelerationTransitionValue)")
Text(
"strokeAccelerationDestinationValue: \(rightStick.strokeAccelerationDestinationValue)")
}
Divider()
Group {
Text("radianDiff \(rightStick.radianDiff)")
Text("deltaHorizontal: \(rightStick.deltaHorizontal)")
Text("deltaVertical: \(rightStick.deltaVertical)")
Text("deltaRadian: \(rightStick.deltaRadian)")
Text("deltaMagnitude: \(rightStick.deltaMagnitude)")
}
}
.frame(width: 400)

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(rightStick.radian)
* rightStick.magnitude * circleSize / 2.0 - indicatorSize / 2.0
)
.padding(
.top,
circleSize / 2.0 + sin(rightStick.radian)
* 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(
to: CGPoint(
x: circleSize / 2.0 + cos(rightStick.radian)
* rightStick.strokeAccelerationTransitionValue
* circleSize / 2.0,
y: circleSize / 2.0 + sin(rightStick.radian)
* rightStick.strokeAccelerationTransitionValue
* circleSize / 2.0
))
}
.stroke(Color.red, lineWidth: 2)
.frame(width: circleSize, height: circleSize)
}
HStack {
InformationView()
StickView()
PointerView()
}
.alert(isPresented: inputMonitoringAlertData.showing) {
InputMonitoringAlertView()
Expand All @@ -81,6 +20,97 @@ struct ContentView: View {
}
}

struct InformationView: View {
@ObservedObject private var eventObserver = EventObserver.shared
@ObservedObject private var rightStick = StickManager.shared.rightStick

var body: some View {
VStack(alignment: .leading) {
Group {
Text("counter: \(eventObserver.counter)")
Text("horizontal: \(rightStick.horizontal.lastDoubleValue)")
Text("vertical: \(rightStick.vertical.lastDoubleValue)")
}
Divider()
Group {
Text("radian: \(rightStick.radian)")
Text("magnitude: \(rightStick.magnitude)")
Text(
"strokeAccelerationTransitionValue: \(rightStick.strokeAccelerationTransitionValue)")
Text(
"strokeAccelerationDestinationValue: \(rightStick.strokeAccelerationDestinationValue)"
)
}
Divider()
Group {
Text("radianDiff \(rightStick.radianDiff)")
Text("deltaHorizontal: \(rightStick.deltaHorizontal)")
Text("deltaVertical: \(rightStick.deltaVertical)")
Text("deltaRadian: \(rightStick.deltaRadian)")
Text("deltaMagnitude: \(rightStick.deltaMagnitude)")
}
}
.frame(width: 350)
}
}

struct StickView: View {
@ObservedObject private var rightStick = StickManager.shared.rightStick

private let circleSize = 100.0
private let indicatorSize = 10.0

var body: some View {
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(rightStick.radian)
* rightStick.magnitude * circleSize / 2.0 - indicatorSize / 2.0
)
.padding(
.top,
circleSize / 2.0 + sin(rightStick.radian)
* rightStick.magnitude * circleSize / 2.0 - indicatorSize / 2.0
)
}
.frame(
width: circleSize + indicatorSize * 2,
height: circleSize + indicatorSize * 2)
}
}

struct PointerView: View {
@ObservedObject private var rightStick = StickManager.shared.rightStick

private let boxWidth = 400.0
private let boxHeight = 225.0
private let indicatorSize = 10.0

var body: some View {
ZStack(alignment: .topLeading) {
Rectangle()
.stroke(Color.gray, lineWidth: 2)
.frame(width: boxWidth, height: boxHeight)

Circle()
.fill(Color.blue)
.frame(width: indicatorSize, height: indicatorSize)
.padding(.leading, rightStick.pointerX * boxWidth - indicatorSize / 2)
.padding(.top, rightStick.pointerY * boxHeight - indicatorSize / 2)
}
.frame(
width: boxWidth + indicatorSize * 2,
height: boxHeight + indicatorSize * 2)
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
Expand Down

0 comments on commit 3486646

Please sign in to comment.