Skip to content

Commit 9aa7b91

Browse files
committed
fix: improve input handling code, console can be closed with ESC
1 parent 8c9fca8 commit 9aa7b91

File tree

1 file changed

+18
-10
lines changed
  • cake/core/src/main/java/org/demoth/cake

1 file changed

+18
-10
lines changed

cake/core/src/main/java/org/demoth/cake/Cake.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import org.demoth.cake.stages.ConsoleStage
3838
import org.demoth.cake.stages.Game3dScreen
3939
import org.demoth.cake.stages.MainMenuStage
4040

41-
4241
private enum class ClientNetworkState {
4342
DISCONNECTED,
4443
CONNECTING, // started the connection procedure
@@ -55,8 +54,18 @@ class Cake : KtxApplicationAdapter, KtxInputAdapter {
5554
private lateinit var consoleStage: ConsoleStage
5655
private lateinit var viewport: StretchViewport
5756

57+
// whenever these are changed, input handlers should be updated
5858
private var consoleVisible = false
59+
set(value) {
60+
field = value
61+
updateInputHandlers(consoleVisible, menuVisible)
62+
}
63+
5964
private var menuVisible = true
65+
set(value) {
66+
field = value
67+
updateInputHandlers(consoleVisible, menuVisible)
68+
}
6069

6170
// network
6271
private var networkState = DISCONNECTED
@@ -66,6 +75,11 @@ class Cake : KtxApplicationAdapter, KtxInputAdapter {
6675
private val netchan = netchan_t()
6776

6877
private var game3dScreen: Game3dScreen? = null
78+
set(value) {
79+
field = value
80+
// allow the game screen to receive the input
81+
updateInputHandlers(consoleVisible, menuVisible)
82+
}
6983

7084
private val assetManager = AssetManager().apply {
7185
// todo: implement resolvers for pak files
@@ -129,7 +143,6 @@ class Cake : KtxApplicationAdapter, KtxInputAdapter {
129143
// free unused resources
130144
game3dScreen?.dispose()
131145
game3dScreen = Game3dScreen(assetManager)
132-
updateInputHandlers(consoleVisible, menuVisible) // allow the game screen to receive the input
133146
}
134147

135148
// like a connect but more lightweight
@@ -159,7 +172,6 @@ class Cake : KtxApplicationAdapter, KtxInputAdapter {
159172
servername = it[1]
160173
networkState = CONNECTING
161174
game3dScreen = Game3dScreen(assetManager)
162-
updateInputHandlers(consoleVisible, menuVisible) // allow the game screen to receive the input
163175
// picked up later in the CheckForResend() // fixme: why not connect immediately?
164176
}
165177

@@ -224,7 +236,6 @@ class Cake : KtxApplicationAdapter, KtxInputAdapter {
224236
// todo: clear the game state and release resources
225237
game3dScreen?.dispose()
226238
game3dScreen = null
227-
updateInputHandlers(true, false)
228239

229240
// send a disconnect message to the server
230241
netchan.transmit(listOf(StringCmdMessage(StringCmdMessage.DISCONNECT)))
@@ -321,14 +332,12 @@ class Cake : KtxApplicationAdapter, KtxInputAdapter {
321332
}
322333
}
323334
Input.Keys.ESCAPE -> {
324-
consoleVisible = false
325-
menuVisible = !menuVisible
335+
if (consoleVisible)
336+
consoleVisible = false
337+
else menuVisible = !menuVisible
326338
}
327339
else -> return false
328340
}
329-
330-
331-
updateInputHandlers(consoleVisible, menuVisible)
332341
return true
333342
}
334343

@@ -457,7 +466,6 @@ class Cake : KtxApplicationAdapter, KtxInputAdapter {
457466
// networkState = CONNECTED // fixme: required?
458467
consoleVisible = false
459468
menuVisible = false
460-
updateInputHandlers(consoleVisible, menuVisible)
461469

462470
}
463471

0 commit comments

Comments
 (0)