Skip to content

Commit 26d9523

Browse files
committed
Console: Allowed alt-navigation and deletion :D
1 parent 34b6b27 commit 26d9523

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

core/src/com/unciv/ui/components/extensions/Scene2dExtensions.kt

+1
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ object GdxKeyCodeFixes {
441441

442442
fun Input.isShiftKeyPressed() = isKeyPressed(Input.Keys.SHIFT_LEFT) || isKeyPressed(Input.Keys.SHIFT_RIGHT)
443443
fun Input.isControlKeyPressed() = isKeyPressed(Input.Keys.CONTROL_LEFT) || isKeyPressed(Input.Keys.CONTROL_RIGHT)
444+
fun Input.isAltKeyPressed() = isKeyPressed(Input.Keys.ALT_LEFT) || isKeyPressed(Input.Keys.ALT_RIGHT)
444445
fun Input.areSecretKeysPressed() = isKeyPressed(Input.Keys.SHIFT_RIGHT) &&
445446
(isKeyPressed(Input.Keys.CONTROL_RIGHT) || isKeyPressed(Input.Keys.ALT_RIGHT))
446447

core/src/com/unciv/ui/components/input/ActivationTypes.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ enum class ActivationTypes(
2424
this == other.equivalentTo || other == this.equivalentTo
2525

2626
internal companion object {
27-
fun equivalentValues(type: ActivationTypes) = values().asSequence()
27+
fun equivalentValues(type: ActivationTypes) = entries.asSequence()
2828
.filter { it.isEquivalent(type) }
29-
fun gestures() = values().asSequence()
29+
fun gestures() = entries.asSequence()
3030
.filter { it.isGesture }
3131
}
3232
}

core/src/com/unciv/ui/screens/devconsole/DevConsolePopup.kt

+51-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.unciv.ui.screens.devconsole
22

3+
import com.badlogic.gdx.Gdx
34
import com.badlogic.gdx.Input
45
import com.badlogic.gdx.graphics.Color
56
import com.badlogic.gdx.scenes.scene2d.Actor
@@ -11,10 +12,8 @@ import com.unciv.Constants
1112
import com.unciv.GUI
1213
import com.unciv.logic.civilization.Civilization
1314
import com.unciv.logic.map.mapunit.MapUnit
15+
import com.unciv.ui.components.extensions.*
1416
import com.unciv.ui.components.widgets.UncivTextField
15-
import com.unciv.ui.components.extensions.surroundWithCircle
16-
import com.unciv.ui.components.extensions.toCheckBox
17-
import com.unciv.ui.components.extensions.toLabel
1817
import com.unciv.ui.components.input.KeyCharAndCode
1918
import com.unciv.ui.components.input.keyShortcuts
2019
import com.unciv.ui.components.input.onClick
@@ -23,6 +22,7 @@ import com.unciv.ui.images.ImageGetter
2322
import com.unciv.ui.popups.Popup
2423
import com.unciv.ui.screens.devconsole.CliInput.Companion.splitToCliInput
2524
import com.unciv.ui.screens.worldscreen.WorldScreen
25+
import com.unciv.utils.Concurrency
2626

2727

2828
class DevConsolePopup(val screen: WorldScreen) : Popup(screen) {
@@ -65,6 +65,9 @@ class DevConsolePopup(val screen: WorldScreen) : Popup(screen) {
6565

6666
textField.keyShortcuts.add(Input.Keys.ENTER, ::onEnter)
6767
textField.keyShortcuts.add(KeyCharAndCode.TAB, ::onAutocomplete)
68+
textField.keyShortcuts.add(KeyCharAndCode.ctrlFromCode(Input.Keys.BACKSPACE), ::onAltDelete)
69+
textField.keyShortcuts.add(KeyCharAndCode.ctrlFromCode(Input.Keys.RIGHT), ::onAltRight)
70+
textField.keyShortcuts.add(KeyCharAndCode.ctrlFromCode(Input.Keys.LEFT), ::onAltLeft)
6871

6972
keyShortcuts.add(Input.Keys.UP) { navigateHistory(-1) }
7073
keyShortcuts.add(Input.Keys.DOWN) { navigateHistory(1) }
@@ -102,6 +105,51 @@ class DevConsolePopup(val screen: WorldScreen) : Popup(screen) {
102105
textField.text = textField.text.removeFromEnd(toRemove) + toAdd
103106
textField.cursorPosition = Int.MAX_VALUE // because the setText implementation actively resets it after the paste it uses (auto capped at length)
104107
}
108+
109+
private fun onAltDelete() {
110+
if (!Gdx.input.isAltKeyPressed()) return
111+
112+
Concurrency.runOnGLThread {
113+
val text = textField.text
114+
// textField.cursorPosition-1 to avoid the case where we're currently on a space catching the space we're on
115+
val lastSpace = text.lastIndexOf(' ', textField.cursorPosition-1)
116+
if (lastSpace == -1) {
117+
textField.text = text.removeRange(0, textField.cursorPosition)
118+
return@runOnGLThread
119+
}
120+
// KEEP the last space
121+
textField.text = text.removeRange(lastSpace + 1, textField.cursorPosition)
122+
textField.cursorPosition = lastSpace + 1
123+
}
124+
}
125+
126+
private fun onAltRight() {
127+
if (!Gdx.input.isAltKeyPressed()) return
128+
129+
Concurrency.runOnGLThread {
130+
val text = textField.text
131+
val nextSpace = text.indexOf(' ', textField.cursorPosition)
132+
if (nextSpace == -1) {
133+
textField.cursorPosition = text.length
134+
return@runOnGLThread
135+
}
136+
textField.cursorPosition = nextSpace
137+
}
138+
}
139+
140+
private fun onAltLeft() {
141+
if (!Gdx.input.isAltKeyPressed()) return
142+
143+
Concurrency.runOnGLThread {
144+
val text = textField.text
145+
val previousSpace = text.lastIndexOf(' ', textField.cursorPosition-1)
146+
if (previousSpace == -1) {
147+
textField.cursorPosition = 0
148+
return@runOnGLThread
149+
}
150+
textField.cursorPosition = previousSpace
151+
}
152+
}
105153

106154
private fun navigateHistory(delta: Int) {
107155
if (history.isEmpty()) return

0 commit comments

Comments
 (0)