Should be able to add key shortcuts in any screen now :)

This commit is contained in:
Yair Morgenstern
2020-10-15 21:37:09 +03:00
parent 55d0f7f1dd
commit ae9a026201
4 changed files with 28 additions and 26 deletions

View File

@ -11,7 +11,7 @@ import com.unciv.ui.worldscreen.WorldScreen
* [action] is not realized as lambda, as it would be too easy to introduce references to objects * [action] is not realized as lambda, as it would be too easy to introduce references to objects
* there that should not be serialized to the saved game. * there that should not be serialized to the saved game.
*/ */
open class Notification( open class Notification (
// default parameters necessary for json deserialization // default parameters necessary for json deserialization
var text: String = "", var text: String = "",
var color: Color = Color.BLACK, var color: Color = Color.BLACK,

View File

@ -12,7 +12,7 @@ class GameSettings {
var checkForDueUnits: Boolean = true var checkForDueUnits: Boolean = true
var singleTapMove: Boolean = false var singleTapMove: Boolean = false
var language: String = "English" var language: String = "English"
var resolution: String = "900x600" var resolution: String = "900x600" // Aut-detecting resolution was a BAD IDEA since it needs to be based on DPI AND resolution.
var tutorialsShown = HashSet<String>() var tutorialsShown = HashSet<String>()
var tutorialTasksCompleted = HashSet<String>() var tutorialTasksCompleted = HashSet<String>()
var hasCrashedRecently = false var hasCrashedRecently = false

View File

@ -20,6 +20,7 @@ import com.unciv.models.Tutorial
import com.unciv.models.UncivSound import com.unciv.models.UncivSound
import com.unciv.models.translations.tr import com.unciv.models.translations.tr
import com.unciv.ui.tutorials.TutorialController import com.unciv.ui.tutorials.TutorialController
import java.util.HashMap
import kotlin.concurrent.thread import kotlin.concurrent.thread
import kotlin.random.Random import kotlin.random.Random
@ -30,18 +31,32 @@ open class CameraStageBaseScreen : Screen {
protected val tutorialController by lazy { TutorialController(this) } protected val tutorialController by lazy { TutorialController(this) }
// An initialized val always turned out to illegally be null...
var keyPressDispatcher: HashMap<Char, (() -> Unit)>
init { init {
val width:Float keyPressDispatcher = hashMapOf()
val height:Float
if(game.settings.resolution=="Auto") { // Aut-detecting resolution was a BAD IDEA since it needs to be based on DPI AND resolution.
game.settings.resolution = "900x600"
game.settings.save()
}
val resolutions: List<Float> = game.settings.resolution.split("x").map { it.toInt().toFloat() } val resolutions: List<Float> = game.settings.resolution.split("x").map { it.toInt().toFloat() }
width = resolutions[0] val width = resolutions[0]
height = resolutions[1] val height = resolutions[1]
stage = Stage(ExtendViewport(width, height), batch) stage = Stage(ExtendViewport(width, height), batch)
stage.addListener(
object : InputListener() {
override fun keyTyped(event: InputEvent?, character: Char): Boolean {
if (character.toLowerCase() in keyPressDispatcher && !hasOpenPopups()) {
//try-catch mainly for debugging. Breakpoints in the vicinity can make the event fire twice in rapid succession, second time the context can be invalid
try {
keyPressDispatcher[character.toLowerCase()]?.invoke()
} catch (ex: Exception) {}
return true
}
return super.keyTyped(event, character)
}
}
)
} }
override fun show() {} override fun show() {}
@ -66,7 +81,7 @@ open class CameraStageBaseScreen : Screen {
override fun dispose() {} override fun dispose() {}
fun displayTutorial( tutorial: Tutorial, test: (()->Boolean)? = null ) { fun displayTutorial(tutorial: Tutorial, test: (() -> Boolean)? = null) {
if (!game.settings.showTutorials) return if (!game.settings.showTutorials) return
if (game.settings.tutorialsShown.contains(tutorial.name)) return if (game.settings.tutorialsShown.contains(tutorial.name)) return
if (test != null && !test()) return if (test != null && !test()) return
@ -95,7 +110,7 @@ open class CameraStageBaseScreen : Screen {
} }
/** It returns the assigned [InputListener] */ /** It returns the assigned [InputListener] */
fun onBackButtonClicked(action:()->Unit): InputListener { fun onBackButtonClicked(action: () -> Unit): InputListener {
val listener = object : InputListener() { val listener = object : InputListener() {
override fun keyDown(event: InputEvent?, keycode: Int): Boolean { override fun keyDown(event: InputEvent?, keycode: Int): Boolean {
if (keycode == Input.Keys.BACK || keycode == Input.Keys.ESCAPE) { if (keycode == Input.Keys.BACK || keycode == Input.Keys.ESCAPE) {

View File

@ -70,8 +70,6 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
private var backButtonListener : InputListener private var backButtonListener : InputListener
// An initialized val always turned out to illegally be null...
lateinit var keyPressDispatcher: HashMap<Char, (() -> Unit)>
init { init {
@ -226,19 +224,9 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
} }
return true return true
} }
override fun keyTyped(event: InputEvent?, character: Char): Boolean {
if (character.toLowerCase() in keyPressDispatcher && !hasOpenPopups()) {
//try-catch mainly for debugging. Breakpoints in the vicinity can make the event fire twice in rapid succession, second time the context can be invalid
try {
keyPressDispatcher[character.toLowerCase()]?.invoke()
} catch (ex: Exception) {}
return true
}
return super.keyTyped(event, character)
}
} }
) )
} }
private fun loadLatestMultiplayerState(){ private fun loadLatestMultiplayerState(){
@ -488,7 +476,6 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
nextTurnButton.labelCell.pad(10f) nextTurnButton.labelCell.pad(10f)
val nextTurnActionWrapped = { nextTurnAction() } val nextTurnActionWrapped = { nextTurnAction() }
nextTurnButton.onClick (nextTurnActionWrapped) nextTurnButton.onClick (nextTurnActionWrapped)
if (!::keyPressDispatcher.isInitialized) keyPressDispatcher = hashMapOf()
keyPressDispatcher[' '] = nextTurnActionWrapped keyPressDispatcher[' '] = nextTurnActionWrapped
keyPressDispatcher['n'] = nextTurnActionWrapped keyPressDispatcher['n'] = nextTurnActionWrapped