https://kotlinlang.org logo
#glance
Title
j

James O'Claire

09/18/2023, 2:15 AM
Theoretically, could you make a chess game widget with glance?
p

Piotr Prus

09/18/2023, 5:07 AM
Personally, I dont think so. For such functionality you would need canvas and touch effect/pointerInput. Both are not available in glance modifier. Chess board could be done as boxes or static image, but the touch is important here. You need to detect which figure is pointed and dragged to new position. This is not available in GlanceModifier
j

James O'Claire

09/18/2023, 7:39 AM
Hm, the drag would be pretty easy to ignore since most chess apps use two taps. First tap selects a piece and the second tap attempts to move the piece. I was imagining that each square like a button, with one of the pieces displayed or not. Tap once to select which piece and a second time to move. The state of the board I guess would be stored in a datastore.
p

Piotr Prus

09/18/2023, 9:23 AM
Yeah, the state is not an issue. You could control the board logic by updating your state, so not only start and final position, but also the info if the move is allowed, who won, etc. That would work. In that case the pointerInput is not a concern. May I ask why you would like to make a widget for that? Is it just
for science
? 🙂
j

James O'Claire

09/19/2023, 8:28 AM
Yeah, mostly curiosity. I've worked with many games and never seen them using widgets. Was curious how far you could push widgets to do.
Do you think it is too far from the intended use of Glance?
b

bbade_

09/19/2023, 9:10 PM
Could you? Probably. Is it a good idea? If you want to challenge yourself to do something unusual, go for it, though the end result may not be suitable for general use. If it were done as a grid, you'd need 64 buttons, 8 rows, and 8 columns at a minimum. Then a minimum of six image assets for pieces (using tinting, you could have one asset for both sides). Your two options for images would be to pass them in memory, which is easy, but would be expensive when there's a lot, or you could provide them through a
ContentProvider
, which would probably be more memory efficient, but would not work across work profiles - probably not a big deal in this case if its a personal project, but potentially less ideal if released. Sizing would be an issue as well. Accessibility suggests that buttons be a minimum of 48x48dp, You'd probably want to implement it using a unidirectional data flow pattern like MVI, such that clicks are sent to a reducer that handles game logic, which then emits a new state that gets persisted, and then your glance layer, watching the repository, triggers recomposition to emit a new state.
tl;dr: for science? Go for it. For production? 😶‍🌫️
🙌 1
j

James O'Claire

09/21/2023, 2:53 AM
Thanks for the input. Made a simple tic-tac-toe game https://github.com/ddxv/tic-tac-toe-app/tree/main and it works OK so far.
2 Views