[Linting] Some minor code purity stuff I came across (#10537)

* Centralize shift and control key detection (Unit multiselect now with right-shift too)

* Lint of an ugly curly brace placement - otherwise the original was fine

* Answer open question in comment

* Follow a Gdx 1.12 deprecation

* Another clarifying comment - on ScrollPane's built-in potential surprise that the constructor calls its own overridable public methods
This commit is contained in:
SomeTroglodyte
2023-11-22 22:58:57 +01:00
committed by GitHub
parent 4c8db02dfd
commit 8bd2363361
12 changed files with 34 additions and 40 deletions

View File

@ -256,11 +256,7 @@ open class UncivGame(val isConsoleMode: Boolean = false) : Game(), PlatformSpeci
throw UnsupportedOperationException("Use pushScreen or replaceCurrentScreen instead") throw UnsupportedOperationException("Use pushScreen or replaceCurrentScreen instead")
} }
override fun getScreen(): BaseScreen? { override fun getScreen(): BaseScreen? = super.getScreen() as? BaseScreen
val curScreen = super.getScreen()
return if (curScreen == null) { null } else { curScreen as BaseScreen
}
}
private fun setScreen(newScreen: BaseScreen) { private fun setScreen(newScreen: BaseScreen) {
debug("Setting new screen: %s, screenStack: %s", newScreen, screenStack) debug("Setting new screen: %s, screenStack: %s", newScreen, screenStack)

View File

@ -366,6 +366,8 @@ object GdxKeyCodeFixes {
} }
} }
fun Input.isShiftKeyPressed() = isKeyPressed(Input.Keys.SHIFT_LEFT) || isKeyPressed(Input.Keys.SHIFT_RIGHT)
fun Input.isControlKeyPressed() = isKeyPressed(Input.Keys.CONTROL_LEFT) || isKeyPressed(Input.Keys.CONTROL_RIGHT)
fun Input.areSecretKeysPressed() = isKeyPressed(Input.Keys.SHIFT_RIGHT) && fun Input.areSecretKeysPressed() = isKeyPressed(Input.Keys.SHIFT_RIGHT) &&
(isKeyPressed(Input.Keys.CONTROL_RIGHT) || isKeyPressed(Input.Keys.ALT_RIGHT)) (isKeyPressed(Input.Keys.CONTROL_RIGHT) || isKeyPressed(Input.Keys.ALT_RIGHT))

View File

@ -1,11 +1,11 @@
package com.unciv.ui.components.input package com.unciv.ui.components.input
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.scenes.scene2d.Actor import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.Group import com.badlogic.gdx.scenes.scene2d.Group
import com.badlogic.gdx.scenes.scene2d.InputEvent import com.badlogic.gdx.scenes.scene2d.InputEvent
import com.badlogic.gdx.scenes.scene2d.InputListener import com.badlogic.gdx.scenes.scene2d.InputListener
import com.unciv.ui.components.extensions.isControlKeyPressed
import com.unciv.ui.components.input.KeyShortcutDispatcherVeto.DispatcherVetoResult import com.unciv.ui.components.input.KeyShortcutDispatcherVeto.DispatcherVetoResult
@ -20,7 +20,7 @@ import com.unciv.ui.components.input.KeyShortcutDispatcherVeto.DispatcherVetoRes
val key = when { val key = when {
event == null -> event == null ->
KeyCharAndCode.UNKNOWN KeyCharAndCode.UNKNOWN
Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT) || Gdx.input.isKeyPressed(Input.Keys.CONTROL_RIGHT) -> Gdx.input.isControlKeyPressed() ->
KeyCharAndCode.ctrlFromCode(event.keyCode) KeyCharAndCode.ctrlFromCode(event.keyCode)
else -> else ->
KeyCharAndCode(event.keyCode) KeyCharAndCode(event.keyCode)

View File

@ -1,12 +1,12 @@
package com.unciv.ui.components.input package com.unciv.ui.components.input
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.scenes.scene2d.InputEvent import com.badlogic.gdx.scenes.scene2d.InputEvent
import com.badlogic.gdx.scenes.scene2d.InputListener import com.badlogic.gdx.scenes.scene2d.InputListener
import com.badlogic.gdx.scenes.scene2d.actions.Actions import com.badlogic.gdx.scenes.scene2d.actions.Actions
import com.badlogic.gdx.scenes.scene2d.actions.RepeatAction import com.badlogic.gdx.scenes.scene2d.actions.RepeatAction
import com.badlogic.gdx.scenes.scene2d.ui.TextField import com.badlogic.gdx.scenes.scene2d.ui.TextField
import com.unciv.ui.components.extensions.isControlKeyPressed
import com.unciv.ui.components.widgets.ZoomableScrollPane import com.unciv.ui.components.widgets.ZoomableScrollPane
class KeyboardPanningListener( class KeyboardPanningListener(
@ -40,9 +40,8 @@ class KeyboardPanningListener(
if (event.target is TextField) return false if (event.target is TextField) return false
if (keycode !in allowedKeys) return false if (keycode !in allowedKeys) return false
// Without the following Ctrl-S would leave WASD map scrolling stuck // Without the following Ctrl-S would leave WASD map scrolling stuck
// Might be obsolete with keyboard shortcut refactoring // _Not_ obsolete with keyboard shortcut refactoring
if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT)) return false if (Gdx.input.isControlKeyPressed()) return false
if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_RIGHT)) return false
pressedKeys.add(keycode) pressedKeys.add(keycode)
startLoop() startLoop()
return true return true

View File

@ -11,6 +11,7 @@ import com.badlogic.gdx.scenes.scene2d.utils.ClickListener
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable
import com.badlogic.gdx.utils.Align import com.badlogic.gdx.utils.Align
import com.unciv.ui.components.UncivTooltip.Companion.addTooltip import com.unciv.ui.components.UncivTooltip.Companion.addTooltip
import com.unciv.ui.components.extensions.isControlKeyPressed
import com.unciv.ui.components.fonts.Fonts import com.unciv.ui.components.fonts.Fonts
import com.unciv.ui.components.input.KeyCharAndCode import com.unciv.ui.components.input.KeyCharAndCode
import com.unciv.ui.images.ImageGetter import com.unciv.ui.images.ImageGetter
@ -116,10 +117,6 @@ class KeyCapturingButton(
// Instead of storing a button reference one could use `(event?.listenerActor as? KeyCapturingButton)?.` // Instead of storing a button reference one could use `(event?.listenerActor as? KeyCapturingButton)?.`
private class ButtonListener(private val myButton: KeyCapturingButton) : ClickListener() { private class ButtonListener(private val myButton: KeyCapturingButton) : ClickListener() {
private fun controlDown() =
Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT) ||
Gdx.input.isKeyPressed(Input.Keys.CONTROL_RIGHT)
override fun enter(event: InputEvent?, x: Float, y: Float, pointer: Int, fromActor: Actor?) { override fun enter(event: InputEvent?, x: Float, y: Float, pointer: Int, fromActor: Actor?) {
if (myButton.stage == null) return if (myButton.stage == null) return
myButton.savedFocus = myButton.stage.keyboardFocus myButton.savedFocus = myButton.stage.keyboardFocus
@ -135,7 +132,7 @@ class KeyCapturingButton(
override fun keyDown(event: InputEvent?, keycode: Int): Boolean { override fun keyDown(event: InputEvent?, keycode: Int): Boolean {
if (keycode == Input.Keys.ESCAPE || keycode == Input.Keys.UNKNOWN) return false if (keycode == Input.Keys.ESCAPE || keycode == Input.Keys.UNKNOWN) return false
if (keycode == Input.Keys.CONTROL_LEFT || keycode == Input.Keys.CONTROL_RIGHT) return false if (keycode == Input.Keys.CONTROL_LEFT || keycode == Input.Keys.CONTROL_RIGHT) return false
myButton.handleKey(keycode, controlDown()) myButton.handleKey(keycode, Gdx.input.isControlKeyPressed())
return true return true
} }

View File

@ -1,7 +1,6 @@
package com.unciv.ui.components.widgets package com.unciv.ui.components.widgets
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.math.Interpolation import com.badlogic.gdx.math.Interpolation
import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.math.Vector2
@ -21,10 +20,12 @@ import com.badlogic.gdx.utils.Timer
import com.unciv.Constants import com.unciv.Constants
import com.unciv.models.UncivSound import com.unciv.models.UncivSound
import com.unciv.ui.audio.SoundPlayer import com.unciv.ui.audio.SoundPlayer
import com.unciv.ui.images.IconCircleGroup import com.unciv.ui.components.extensions.isShiftKeyPressed
import com.unciv.ui.components.input.onClick
import com.unciv.ui.components.extensions.surroundWithCircle import com.unciv.ui.components.extensions.surroundWithCircle
import com.unciv.ui.components.extensions.toLabel import com.unciv.ui.components.extensions.toLabel
import com.unciv.ui.components.input.onClick
import com.unciv.ui.components.widgets.UncivSlider.Companion.formatPercent
import com.unciv.ui.images.IconCircleGroup
import com.unciv.ui.screens.basescreen.BaseScreen import com.unciv.ui.screens.basescreen.BaseScreen
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.sign import kotlin.math.sign
@ -132,10 +133,10 @@ class UncivSlider (
setPlusMinusEnabled() setPlusMinusEnabled()
} }
/** Will make this slider snap to the specified values, if the knob is within the threshold. */ /** Will make this slider snap to the specified values, if the knob is within the threshold. */
fun setSnapToValues(values: FloatArray?, threshold: Float) { fun setSnapToValues(threshold: Float, vararg values: Float) {
snapToValues = values // make a copy so our plus/minus code can snap snapToValues = values // make a copy so our plus/minus code can snap
snapThreshold = threshold snapThreshold = threshold
slider.setSnapToValues(values, threshold) slider.setSnapToValues(threshold, *values)
} }
// java format string for the value tip, set by changing stepSize // java format string for the value tip, set by changing stepSize
@ -206,10 +207,7 @@ class UncivSlider (
// un-snapping with Shift is taken from Slider source, and the loop mostly as well // un-snapping with Shift is taken from Slider source, and the loop mostly as well
// with snap active, plus/minus buttons will go to the next snap position regardless of stepSize // with snap active, plus/minus buttons will go to the next snap position regardless of stepSize
// this could be shorter if Slider.snap(), Slider.snapValues and Slider.threshold weren't protected // this could be shorter if Slider.snap(), Slider.snapValues and Slider.threshold weren't protected
if (snapToValues?.isEmpty() != false || if (snapToValues?.isEmpty() != false || Gdx.input.isShiftKeyPressed()) {
Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT) ||
Gdx.input.isKeyPressed(Input.Keys.SHIFT_RIGHT)
) {
value += delta value += delta
onChange?.invoke(value) onChange?.invoke(value)
return return

View File

@ -15,8 +15,8 @@ import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener
import com.badlogic.gdx.scenes.scene2d.utils.Cullable import com.badlogic.gdx.scenes.scene2d.utils.Cullable
import com.unciv.models.metadata.GameSettings
import com.unciv.UncivGame import com.unciv.UncivGame
import com.unciv.models.metadata.GameSettings
import com.unciv.ui.components.ZoomGestureListener import com.unciv.ui.components.ZoomGestureListener
import com.unciv.ui.components.input.KeyboardPanningListener import com.unciv.ui.components.input.KeyboardPanningListener
import java.lang.Float.max import java.lang.Float.max
@ -70,6 +70,8 @@ open class ZoomableScrollPane(
return if (group.hasChildren()) group.children[0] else null return if (group.hasChildren()) group.children[0] else null
} }
// This override will be called from ScrollPane's constructor to store the empty Group() we're passing,
// therefore super.getActor() _can_ return null.
override fun setActor(content: Actor?) { override fun setActor(content: Actor?) {
val group: Group? = super.getActor() as Group? val group: Group? = super.getActor() as Group?
if (group != null) { if (group != null) {

View File

@ -12,15 +12,15 @@ import com.unciv.models.metadata.GameSettings.ScreenSize
import com.unciv.models.skins.SkinCache import com.unciv.models.skins.SkinCache
import com.unciv.models.tilesets.TileSetCache import com.unciv.models.tilesets.TileSetCache
import com.unciv.models.translations.tr import com.unciv.models.translations.tr
import com.unciv.ui.components.widgets.TranslatedSelectBox
import com.unciv.ui.components.widgets.UncivSlider
import com.unciv.ui.components.widgets.WrappableLabel
import com.unciv.ui.components.extensions.addSeparator import com.unciv.ui.components.extensions.addSeparator
import com.unciv.ui.components.extensions.brighten import com.unciv.ui.components.extensions.brighten
import com.unciv.ui.components.extensions.toLabel import com.unciv.ui.components.extensions.toLabel
import com.unciv.ui.components.extensions.toTextButton import com.unciv.ui.components.extensions.toTextButton
import com.unciv.ui.components.input.onChange import com.unciv.ui.components.input.onChange
import com.unciv.ui.components.input.onClick import com.unciv.ui.components.input.onClick
import com.unciv.ui.components.widgets.TranslatedSelectBox
import com.unciv.ui.components.widgets.UncivSlider
import com.unciv.ui.components.widgets.WrappableLabel
import com.unciv.ui.images.ImageGetter import com.unciv.ui.images.ImageGetter
import com.unciv.ui.popups.ConfirmPopup import com.unciv.ui.popups.ConfirmPopup
import com.unciv.ui.screens.basescreen.BaseScreen import com.unciv.ui.screens.basescreen.BaseScreen
@ -169,7 +169,7 @@ private fun addPediaUnitArtSizeSlider(table: Table, settings: GameSettings, sele
settings.pediaUnitArtSize = it settings.pediaUnitArtSize = it
GUI.setUpdateWorldOnNextRender() GUI.setUpdateWorldOnNextRender()
} }
unitArtSizeSlider.setSnapToValues(floatArrayOf(0f, 32f, 48f, 64f, 96f, 120f, 180f, 240f, 360f), 60f) unitArtSizeSlider.setSnapToValues(threshold = 60f, 0f, 32f, 48f, 64f, 96f, 120f, 180f, 240f, 360f)
table.add(unitArtSizeSlider).minWidth(selectBoxMinWidth).pad(10f).row() table.add(unitArtSizeSlider).minWidth(selectBoxMinWidth).pad(10f).row()
} }

View File

@ -16,16 +16,16 @@ import com.unciv.models.ruleset.nation.Nation
import com.unciv.models.ruleset.tile.ResourceType import com.unciv.models.ruleset.tile.ResourceType
import com.unciv.models.stats.Stats import com.unciv.models.stats.Stats
import com.unciv.models.translations.tr import com.unciv.models.translations.tr
import com.unciv.ui.components.widgets.ExpanderTab
import com.unciv.ui.components.widgets.TabbedPager
import com.unciv.ui.components.widgets.UncivSlider
import com.unciv.ui.components.widgets.WrappableLabel
import com.unciv.ui.components.extensions.addSeparator import com.unciv.ui.components.extensions.addSeparator
import com.unciv.ui.components.extensions.darken import com.unciv.ui.components.extensions.darken
import com.unciv.ui.components.extensions.pad import com.unciv.ui.components.extensions.pad
import com.unciv.ui.components.extensions.toLabel import com.unciv.ui.components.extensions.toLabel
import com.unciv.ui.components.extensions.toTextButton import com.unciv.ui.components.extensions.toTextButton
import com.unciv.ui.components.input.onClick import com.unciv.ui.components.input.onClick
import com.unciv.ui.components.widgets.ExpanderTab
import com.unciv.ui.components.widgets.TabbedPager
import com.unciv.ui.components.widgets.UncivSlider
import com.unciv.ui.components.widgets.WrappableLabel
import com.unciv.ui.popups.ToastPopup import com.unciv.ui.popups.ToastPopup
import com.unciv.ui.screens.basescreen.BaseScreen import com.unciv.ui.screens.basescreen.BaseScreen
import com.unciv.ui.screens.civilopediascreen.CivilopediaScreen import com.unciv.ui.screens.civilopediascreen.CivilopediaScreen
@ -230,7 +230,7 @@ class MapEditorViewTab(
editorScreen.updateTile(tile) editorScreen.updateTile(tile)
editorScreen.isDirty = true editorScreen.isDirty = true
} }
slider.setSnapToValues(floatArrayOf(0f,1f,2f,3f,4f,5f,6f,7f,8f,9f,10f,12f,15f,20f,30f,40f), 5f) slider.setSnapToValues(threshold = 5f, 0f,1f,2f,3f,4f,5f,6f,7f,8f,9f,10f,12f,15f,20f,30f,40f)
add(slider).right().minWidth(80f).fillX().padTop(15f) add(slider).right().minWidth(80f).fillX().padTop(15f)
}).fillX() }).fillX()
} }

View File

@ -338,7 +338,7 @@ class GameOptionsTable(
} }
slider.isDisabled = locked slider.isDisabled = locked
val snapValues = floatArrayOf(100f,150f,200f,250f,300f,350f,400f,450f,500f,550f,600f,650f,700f,750f,800f,900f,1000f,1250f,1500f) val snapValues = floatArrayOf(100f,150f,200f,250f,300f,350f,400f,450f,500f,550f,600f,650f,700f,750f,800f,900f,1000f,1250f,1500f)
slider.setSnapToValues(snapValues, 125f) slider.setSnapToValues(threshold = 125f, *snapValues)
return slider return slider
} }

View File

@ -2,7 +2,6 @@ package com.unciv.ui.screens.worldscreen
import com.badlogic.gdx.Application import com.badlogic.gdx.Application
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.Batch import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.math.Vector2
@ -35,6 +34,7 @@ import com.unciv.ui.components.MiscArrowTypes
import com.unciv.ui.components.extensions.center import com.unciv.ui.components.extensions.center
import com.unciv.ui.components.extensions.colorFromRGB import com.unciv.ui.components.extensions.colorFromRGB
import com.unciv.ui.components.extensions.darken import com.unciv.ui.components.extensions.darken
import com.unciv.ui.components.extensions.isShiftKeyPressed
import com.unciv.ui.components.extensions.surroundWithCircle import com.unciv.ui.components.extensions.surroundWithCircle
import com.unciv.ui.components.extensions.toLabel import com.unciv.ui.components.extensions.toLabel
import com.unciv.ui.components.input.ActivationTypes import com.unciv.ui.components.input.ActivationTypes
@ -423,7 +423,7 @@ class WorldMapHolder(
if (unit.currentMovement == 0f) unitGroup.color.a = 0.66f if (unit.currentMovement == 0f) unitGroup.color.a = 0.66f
unitGroup.touchable = Touchable.enabled unitGroup.touchable = Touchable.enabled
unitGroup.onClick { unitGroup.onClick {
worldScreen.bottomUnitTable.selectUnit(unit, Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT)) worldScreen.bottomUnitTable.selectUnit(unit, Gdx.input.isShiftKeyPressed())
worldScreen.shouldUpdate = true worldScreen.shouldUpdate = true
removeUnitActionOverlay() removeUnitActionOverlay()
} }

View File

@ -1,7 +1,6 @@
package com.unciv.ui.screens.worldscreen.unit package com.unciv.ui.screens.worldscreen.unit
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Actor import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.Touchable
@ -16,6 +15,7 @@ import com.unciv.models.translations.tr
import com.unciv.ui.components.extensions.addSeparator import com.unciv.ui.components.extensions.addSeparator
import com.unciv.ui.components.extensions.center import com.unciv.ui.components.extensions.center
import com.unciv.ui.components.extensions.darken import com.unciv.ui.components.extensions.darken
import com.unciv.ui.components.extensions.isShiftKeyPressed
import com.unciv.ui.components.extensions.toImageButton import com.unciv.ui.components.extensions.toImageButton
import com.unciv.ui.components.extensions.toLabel import com.unciv.ui.components.extensions.toLabel
import com.unciv.ui.components.input.onClick import com.unciv.ui.components.input.onClick
@ -352,7 +352,7 @@ class UnitTable(val worldScreen: WorldScreen) : Table() {
selectedTile.isCityCenter() && selectedTile.isCityCenter() &&
(selectedTile.getOwner() == worldScreen.viewingCiv || worldScreen.viewingCiv.isSpectator()) -> (selectedTile.getOwner() == worldScreen.viewingCiv || worldScreen.viewingCiv.isSpectator()) ->
citySelected(selectedTile.getCity()!!) citySelected(selectedTile.getCity()!!)
nextUnit != null -> selectUnit(nextUnit, Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT)) nextUnit != null -> selectUnit(nextUnit, Gdx.input.isShiftKeyPressed())
selectedTile == previouslySelectedUnit?.currentTile -> { selectedTile == previouslySelectedUnit?.currentTile -> {
selectUnit() selectUnit()
isVisible = false isVisible = false