Skip to content

Commit 3f5f18c

Browse files
committed
changed touch point data to be relative to screen radius instead of hard coordinates
1 parent f66737b commit 3f5f18c

4 files changed

Lines changed: 68 additions & 45 deletions

File tree

watch/app/src/main/java/com/imsproject/watch/Properties.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const val AXLE_STARTING_ANGLE = -90f
8686
var FLOUR_MILL_SYNC_TIME_THRESHOLD = 100L
8787
const val RESET_COOLDOWN_WAIT_TIME = 16
8888
const val STRETCH_PEAK = 12.0f
89+
const val TOUCH_CIRCLE_RADIUS = 30f
8990
// ============================================ |
9091
// these values must be a normal fraction of STRETCH_PEAK to ensure that we reach
9192
// the target angle exactly and not overshoot it

watch/app/src/main/java/com/imsproject/watch/utils/Angle.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,7 @@ class Angle(
151151
from.floatValue < to.floatValue
152152
}
153153
}
154+
155+
fun undefined(): Angle = Angle(UNDEFINED_ANGLE)
154156
}
155157
}

watch/app/src/main/java/com/imsproject/watch/view/FlourMillActivity.kt

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.imsproject.watch.view
22

33
import android.os.Bundle
4-
import android.view.WindowManager
54
import androidx.activity.compose.setContent
65
import androidx.activity.viewModels
76
import androidx.compose.foundation.Canvas
@@ -32,13 +31,14 @@ import com.imsproject.watch.DARK_BACKGROUND_COLOR
3231
import com.imsproject.watch.GLOWING_YELLOW_COLOR
3332
import com.imsproject.watch.LIGHT_BROWN_COLOR
3433
import com.imsproject.watch.SCREEN_RADIUS
35-
import com.imsproject.watch.initProperties
3634
import com.imsproject.watch.utils.cartesianToPolar
3735
import com.imsproject.watch.utils.polarToCartesian
3836
import com.imsproject.watch.viewmodel.FlourMillViewModel
3937
import com.imsproject.watch.viewmodel.GameViewModel
4038
import androidx.compose.ui.input.pointer.PointerEventType
4139
import com.imsproject.watch.BRIGHT_CYAN_COLOR
40+
import com.imsproject.watch.TOUCH_CIRCLE_RADIUS
41+
import com.imsproject.watch.utils.Angle
4242

4343
class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
4444

@@ -65,8 +65,9 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
6565
fun FlourMill() {
6666
val (centerX, centerY) = remember { polarToCartesian(0f, 0.0) }
6767
val axle = remember { viewModel.axle }
68-
var touchPoint by remember { mutableStateOf(-1f to -1f) }
6968
var showAxle by remember { mutableStateOf(false) }
69+
val myTouchPoint = viewModel.myTouchPoint.collectAsState().value
70+
val opponentTouchPoint = viewModel.opponentTouchPoint.collectAsState().value
7071

7172
Box(
7273
modifier = Modifier
@@ -78,20 +79,19 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
7879
val inputChange = pointerEvent.changes.first()
7980
inputChange.consume()
8081
val position = inputChange.position
82+
val (radius, angle) = cartesianToPolar(position.x, position.y)
83+
val relativeRadius = radius / SCREEN_RADIUS
8184
when (pointerEvent.type) {
8285
PointerEventType.Press -> {
83-
val (_, angle) = cartesianToPolar(position.x, position.y)
8486
axle.angle = angle + -90f
85-
touchPoint = position.x to position.y
87+
viewModel.setTouchPoint(relativeRadius, angle)
8688
showAxle = true
8789
}
8890
PointerEventType.Move -> {
89-
viewModel.setTouchPoint(position.x, position.y)
90-
touchPoint = position.x to position.y
91+
viewModel.setTouchPoint(relativeRadius, angle)
9192
}
9293
PointerEventType.Release -> {
93-
viewModel.setTouchPoint(-1f, -1f)
94-
touchPoint = -1f to -1f
94+
viewModel.setTouchPoint(-1f, Angle.undefined())
9595
showAxle = false
9696
}
9797
}
@@ -138,42 +138,48 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
138138
.background(color = DARK_BACKGROUND_COLOR)
139139
)
140140
Canvas(modifier = Modifier.Companion.fillMaxSize()){
141-
val touchCircleRadius = 30f
142-
val (leftX, leftY) = polarToCartesian(SCREEN_RADIUS * 0.7f, axle.angle + 90f)
143-
val (leftX2, leftY2) = polarToCartesian(SCREEN_RADIUS * 0.9f, axle.angle + 90f)
144-
val (rightX, rightY) = polarToCartesian(SCREEN_RADIUS * 0.9f, axle.angle + -90f)
145-
146-
val (touchPointDistance,touchPointAngle) = cartesianToPolar(touchPoint.first, touchPoint.second)
147-
val touchPointInBounds = (touchPointAngle - (axle.angle + 90f) <= (360f / touchCircleRadius))
148-
&& touchPointDistance > (SCREEN_RADIUS * 0.7f - touchCircleRadius)
149-
&& touchPointDistance < (SCREEN_RADIUS * 0.9f + touchCircleRadius)
150-
151-
val axlePath = Path().apply {
152-
moveTo(rightX,rightY)
153-
lineTo(leftX, leftY)
154-
}
141+
for(touchPoint in listOf(myTouchPoint, opponentTouchPoint)){
142+
if(touchPoint.first < 0f) continue
155143

156-
val axlePath2 = Path().apply {
157-
moveTo(leftX,leftY)
158-
lineTo(leftX2, leftY2)
159-
}
144+
val (leftX, leftY) = polarToCartesian(SCREEN_RADIUS * 0.7f, axle.angle + 90f)
145+
val (leftX2, leftY2) = polarToCartesian(SCREEN_RADIUS * 0.9f, axle.angle + 90f)
146+
val (rightX, rightY) = polarToCartesian(SCREEN_RADIUS * 0.9f, axle.angle + -90f)
160147

161-
if(showAxle){
162-
drawPath(
163-
path = axlePath,
164-
color = GLOWING_YELLOW_COLOR,
165-
style = Stroke(width = AXLE_WIDTH)
166-
)
167-
drawPath(
168-
path = axlePath2,
169-
color = BRIGHT_CYAN_COLOR,
170-
style = Stroke(width = AXLE_WIDTH)
171-
)
172-
drawCircle(
173-
radius = touchCircleRadius,
174-
center = Offset(touchPoint.first, touchPoint.second),
175-
color = if (touchPointInBounds) BRIGHT_CYAN_COLOR else BRIGHT_CYAN_COLOR.copy(alpha = 0.5f),
176-
)
148+
val touchPointDistance = touchPoint.first * SCREEN_RADIUS
149+
val touchPointAngle = touchPoint.second
150+
val touchPointInBounds = (touchPointAngle - (axle.angle + 90f) <= (360f / TOUCH_CIRCLE_RADIUS))
151+
&& touchPointDistance > (SCREEN_RADIUS * 0.7f - TOUCH_CIRCLE_RADIUS)
152+
&& touchPointDistance < (SCREEN_RADIUS * 0.9f + TOUCH_CIRCLE_RADIUS)
153+
154+
val (x,y) = polarToCartesian(touchPointDistance, touchPointAngle)
155+
156+
val axlePath = Path().apply {
157+
moveTo(rightX,rightY)
158+
lineTo(leftX, leftY)
159+
}
160+
161+
val axlePath2 = Path().apply {
162+
moveTo(leftX,leftY)
163+
lineTo(leftX2, leftY2)
164+
}
165+
166+
if(showAxle){
167+
drawPath(
168+
path = axlePath,
169+
color = GLOWING_YELLOW_COLOR,
170+
style = Stroke(width = AXLE_WIDTH)
171+
)
172+
drawPath(
173+
path = axlePath2,
174+
color = BRIGHT_CYAN_COLOR,
175+
style = Stroke(width = AXLE_WIDTH)
176+
)
177+
drawCircle(
178+
radius = TOUCH_CIRCLE_RADIUS,
179+
center = Offset(x, y),
180+
color = if (touchPointInBounds) BRIGHT_CYAN_COLOR else BRIGHT_CYAN_COLOR.copy(alpha = 0.5f),
181+
)
182+
}
177183
}
178184
}
179185
}

watch/app/src/main/java/com/imsproject/watch/viewmodel/FlourMillViewModel.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import com.imsproject.watch.utils.toAngle
2424
import com.imsproject.watch.view.contracts.Result
2525
import kotlinx.coroutines.Dispatchers
2626
import kotlinx.coroutines.delay
27+
import kotlinx.coroutines.flow.MutableStateFlow
28+
import kotlinx.coroutines.flow.StateFlow
2729
import kotlinx.coroutines.launch
2830
import kotlinx.coroutines.withContext
2931
import kotlin.math.absoluteValue
@@ -62,6 +64,18 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
6264
lateinit var myAxleSide : AxleSide
6365
private set
6466

67+
private var _myTouchPoint = MutableStateFlow<Pair<Float,Angle>>(-1f to Angle.undefined())
68+
/**
69+
* <relativeRadius,angle>
70+
*/
71+
val myTouchPoint: StateFlow<Pair<Float,Angle>> = _myTouchPoint
72+
73+
private var _opponentTouchPoint = MutableStateFlow<Pair<Float,Angle>>(-1f to Angle.undefined())
74+
/**
75+
* <relativeRadius,angle>
76+
*/
77+
val opponentTouchPoint: StateFlow<Pair<Float,Angle>> = _opponentTouchPoint
78+
6579
// ================================================================================ |
6680
// ============================ PUBLIC METHODS ==================================== |
6781
// ================================================================================ |
@@ -93,8 +107,8 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
93107
Log.d(TAG, "syncTolerance: $syncTolerance")
94108
}
95109

96-
fun setTouchPoint(x: Float, y: Float) {
97-
110+
fun setTouchPoint(relativeRadius: Float, angle: Angle) {
111+
_myTouchPoint.value = relativeRadius to angle
98112
}
99113

100114
// ================================================================================ |

0 commit comments

Comments
 (0)