Prevent activation of disabled actors via key shortcuts (#9736)

Avoid activation concurrent modification problems
This commit is contained in:
SomeTroglodyte 2023-07-04 12:58:34 +02:00 committed by GitHub
parent 8263d972ff
commit fb3064dd4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 1 deletions

View File

@ -43,6 +43,7 @@ private class RestorableTextButtonStyle(
/** Disable a [Button] by setting its [touchable][Button.touchable] and [style][Button.style] properties. */
fun Button.disable() {
touchable = Touchable.disabled
isDisabled = true
val oldStyle = style
if (oldStyle is RestorableTextButtonStyle) return
val disabledStyle = BaseScreen.skin.get("disabled", TextButtonStyle::class.java)
@ -54,6 +55,7 @@ fun Button.enable() {
if (oldStyle is RestorableTextButtonStyle) {
style = oldStyle.restoreStyle
}
isDisabled = false
touchable = Touchable.enabled
}
/** Enable or disable a [Button] by setting its [touchable][Button.touchable] and [style][Button.style] properties,

View File

@ -51,7 +51,8 @@ internal class ActivationActionMap : MutableMap<ActivationTypes, ActivationActio
if (actions.isEmpty()) return false
if (actions.sound != UncivSound.Silent)
Concurrency.runOnGLThread("Sound") { SoundPlayer.play(actions.sound) }
for (action in actions)
// We can't know an activation handler won't redefine activations, so better iterate over a copy
for (action in actions.toList())
action.invoke()
return true
}

View File

@ -1,6 +1,7 @@
package com.unciv.ui.components.input
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.utils.Disableable
import com.unciv.models.UncivSound
internal class ActorAttachments private constructor(actor: Actor) {
@ -37,6 +38,7 @@ internal class ActorAttachments private constructor(actor: Actor) {
fun activate(type: ActivationTypes): Boolean {
if (!this::activationActions.isInitialized) return false
if ((actor as? Disableable)?.isDisabled == true) return false // Skip if disabled - could reach here through key shortcuts
return activationActions.activate(type)
}