Skip to content

Commit 41f2f34

Browse files
committed
layed the foundation for 2 finger markers
1 parent 3f5f18c commit 41f2f34

2 files changed

Lines changed: 71 additions & 49 deletions

File tree

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

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import androidx.compose.foundation.shape.CircleShape
1111
import androidx.compose.runtime.Composable
1212
import androidx.compose.runtime.collectAsState
1313
import androidx.compose.runtime.getValue
14-
import androidx.compose.runtime.mutableStateOf
1514
import androidx.compose.runtime.remember
16-
import androidx.compose.runtime.setValue
1715
import androidx.compose.ui.Alignment
1816
import androidx.compose.ui.Modifier
1917
import androidx.compose.ui.draw.clip
@@ -37,8 +35,10 @@ import com.imsproject.watch.viewmodel.FlourMillViewModel
3735
import com.imsproject.watch.viewmodel.GameViewModel
3836
import androidx.compose.ui.input.pointer.PointerEventType
3937
import com.imsproject.watch.BRIGHT_CYAN_COLOR
38+
import com.imsproject.watch.SILVER_COLOR
4039
import com.imsproject.watch.TOUCH_CIRCLE_RADIUS
4140
import com.imsproject.watch.utils.Angle
41+
import com.imsproject.watch.viewmodel.FlourMillViewModel.AxleSide
4242

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

@@ -64,8 +64,7 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
6464
@Composable
6565
fun FlourMill() {
6666
val (centerX, centerY) = remember { polarToCartesian(0f, 0.0) }
67-
val axle = remember { viewModel.axle }
68-
var showAxle by remember { mutableStateOf(false) }
67+
val axle = viewModel.axle.collectAsState().value
6968
val myTouchPoint = viewModel.myTouchPoint.collectAsState().value
7069
val opponentTouchPoint = viewModel.opponentTouchPoint.collectAsState().value
7170

@@ -78,21 +77,15 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
7877
val pointerEvent = awaitPointerEvent()
7978
val inputChange = pointerEvent.changes.first()
8079
inputChange.consume()
81-
val position = inputChange.position
82-
val (radius, angle) = cartesianToPolar(position.x, position.y)
83-
val relativeRadius = radius / SCREEN_RADIUS
8480
when (pointerEvent.type) {
85-
PointerEventType.Press -> {
86-
axle.angle = angle + -90f
87-
viewModel.setTouchPoint(relativeRadius, angle)
88-
showAxle = true
89-
}
90-
PointerEventType.Move -> {
81+
PointerEventType.Press, PointerEventType.Move -> {
82+
val position = inputChange.position
83+
val (radius, angle) = cartesianToPolar(position.x, position.y)
84+
val relativeRadius = radius / SCREEN_RADIUS
9185
viewModel.setTouchPoint(relativeRadius, angle)
9286
}
9387
PointerEventType.Release -> {
9488
viewModel.setTouchPoint(-1f, Angle.undefined())
95-
showAxle = false
9689
}
9790
}
9891
}
@@ -138,46 +131,68 @@ class FlourMillActivity : GameActivity(GameType.FLOUR_MILL) {
138131
.background(color = DARK_BACKGROUND_COLOR)
139132
)
140133
Canvas(modifier = Modifier.Companion.fillMaxSize()){
141-
for(touchPoint in listOf(myTouchPoint, opponentTouchPoint)){
142-
if(touchPoint.first < 0f) continue
143-
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)
134+
if(axle != null){
147135

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)
136+
val axleLeftAngle = axle.angle + -90f
137+
val axleRightAngle = axle.angle + 90f
138+
val (leftTipStartX, leftTipStartY) = polarToCartesian(SCREEN_RADIUS * 0.7f, axleLeftAngle)
139+
val (leftTipEndX, leftTipEndY) = polarToCartesian(SCREEN_RADIUS * 0.9f, axleLeftAngle)
140+
val (rightTipStartX, rightTipStartY) = polarToCartesian(SCREEN_RADIUS * 0.7f, axleRightAngle)
141+
val (rightTipEndX, rightTipEndY) = polarToCartesian(SCREEN_RADIUS * 0.9f, axleRightAngle)
155142

156143
val axlePath = Path().apply {
157-
moveTo(rightX,rightY)
158-
lineTo(leftX, leftY)
144+
moveTo(rightTipStartX,rightTipStartY)
145+
lineTo(leftTipStartX, leftTipStartY)
159146
}
160-
161-
val axlePath2 = Path().apply {
162-
moveTo(leftX,leftY)
163-
lineTo(leftX2, leftY2)
147+
val leftTipPath = Path().apply {
148+
moveTo(leftTipStartX,leftTipStartY)
149+
lineTo(leftTipEndX, leftTipEndY)
164150
}
151+
val rightTipPath = Path().apply {
152+
moveTo(rightTipStartX,rightTipStartY)
153+
lineTo(rightTipEndX, rightTipEndY)
154+
}
155+
drawPath(
156+
path = axlePath,
157+
color = GLOWING_YELLOW_COLOR,
158+
style = Stroke(width = AXLE_WIDTH)
159+
)
165160

166-
if(showAxle){
167-
drawPath(
168-
path = axlePath,
169-
color = GLOWING_YELLOW_COLOR,
170-
style = Stroke(width = AXLE_WIDTH)
171-
)
161+
val sides = listOf(myTouchPoint to axle.mySide, opponentTouchPoint to axle.mySide.otherSide())
162+
for((touchPoint, side) in sides){
163+
val color = if (side == axle.mySide) BRIGHT_CYAN_COLOR else SILVER_COLOR
164+
val path: Path
165+
val sideAngle: Angle
166+
when (side) {
167+
AxleSide.LEFT -> {
168+
path = leftTipPath
169+
sideAngle = axleLeftAngle
170+
}
171+
AxleSide.RIGHT -> {
172+
path = rightTipPath
173+
sideAngle = axleRightAngle
174+
}
175+
}
172176
drawPath(
173-
path = axlePath2,
174-
color = BRIGHT_CYAN_COLOR,
177+
path = path,
178+
color = color,
175179
style = Stroke(width = AXLE_WIDTH)
176180
)
181+
182+
if(touchPoint.first < 0f) continue
183+
184+
val touchPointDistance = touchPoint.first * SCREEN_RADIUS
185+
val touchPointAngle = touchPoint.second
186+
val touchPointInBounds = (touchPointAngle - sideAngle <= (360f / TOUCH_CIRCLE_RADIUS))
187+
&& touchPointDistance > (SCREEN_RADIUS * 0.7f - TOUCH_CIRCLE_RADIUS)
188+
&& touchPointDistance < (SCREEN_RADIUS * 0.9f + TOUCH_CIRCLE_RADIUS)
189+
190+
val (x,y) = polarToCartesian(touchPointDistance, touchPointAngle)
191+
177192
drawCircle(
178193
radius = TOUCH_CIRCLE_RADIUS,
179194
center = Offset(x, y),
180-
color = if (touchPointInBounds) BRIGHT_CYAN_COLOR else BRIGHT_CYAN_COLOR.copy(alpha = 0.5f),
195+
color = if (touchPointInBounds) color else color.copy(alpha = 0.5f),
181196
)
182197
}
183198
}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
5050
}
5151
}
5252

53-
class Axle(startingAngle: Angle, mySide: AxleSide) {
53+
class Axle(startingAngle: Angle, val mySide: AxleSide) {
5454
var angle by mutableStateOf(startingAngle)
5555
var targetAngle = startingAngle
5656
}
@@ -59,18 +59,19 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
5959
// ================================ STATE FIELDS ================================== |
6060
// ================================================================================ |
6161

62-
lateinit var axle : Axle
63-
private set
62+
private val _axle = MutableStateFlow<Axle?>(null)
63+
val axle: StateFlow<Axle?> = _axle
64+
6465
lateinit var myAxleSide : AxleSide
6566
private set
6667

67-
private var _myTouchPoint = MutableStateFlow<Pair<Float,Angle>>(-1f to Angle.undefined())
68+
private val _myTouchPoint = MutableStateFlow<Pair<Float,Angle>>(-1f to Angle.undefined())
6869
/**
6970
* <relativeRadius,angle>
7071
*/
7172
val myTouchPoint: StateFlow<Pair<Float,Angle>> = _myTouchPoint
7273

73-
private var _opponentTouchPoint = MutableStateFlow<Pair<Float,Angle>>(-1f to Angle.undefined())
74+
private val _opponentTouchPoint = MutableStateFlow<Pair<Float,Angle>>(-1f to Angle.undefined())
7475
/**
7576
* <relativeRadius,angle>
7677
*/
@@ -85,7 +86,7 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
8586

8687
if(ACTIVITY_DEBUG_MODE) {
8788
myAxleSide = AxleSide.RIGHT
88-
axle = Axle(AXLE_STARTING_ANGLE.toAngle(), myAxleSide)
89+
// axle = Axle(AXLE_STARTING_ANGLE.toAngle(), myAxleSide)
8990
viewModelScope.launch(Dispatchers.Default) {
9091
while (true) {
9192
delay(1000)
@@ -96,7 +97,6 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
9697
}
9798

9899
myAxleSide = intent.getStringExtra("$PACKAGE_PREFIX.additionalData")?.let { AxleSide.fromString(it) }!!
99-
axle = Axle(AXLE_STARTING_ANGLE.toAngle(), myAxleSide)
100100

101101
val syncTolerance = intent.getLongExtra("$PACKAGE_PREFIX.syncTolerance", -1)
102102
if (syncTolerance <= 0L) {
@@ -109,6 +109,13 @@ class FlourMillViewModel : GameViewModel(GameType.FLOUR_MILL) {
109109

110110
fun setTouchPoint(relativeRadius: Float, angle: Angle) {
111111
_myTouchPoint.value = relativeRadius to angle
112+
113+
if(relativeRadius < 0f){
114+
_axle.value = null
115+
} else if (relativeRadius >= 0f && _axle.value == null){
116+
val axleAngle = if(myAxleSide == AxleSide.RIGHT) angle + -90f else angle + 90f
117+
_axle.value = Axle(axleAngle, myAxleSide)
118+
}
112119
}
113120

114121
// ================================================================================ |

0 commit comments

Comments
 (0)