From 461028deed57ef43701472717e8b88bdc46ff670 Mon Sep 17 00:00:00 2001 From: vegeta1k95 <32207817+vegeta1k95@users.noreply.github.com> Date: Mon, 20 Feb 2023 15:51:40 +0100 Subject: [PATCH] UI Helper: GUI class (#8707) Co-authored-by: vegeta1k95 --- core/src/com/unciv/UncivGame.kt | 64 ++++++++++++++++++- .../unciv/logic/automation/ai/TacticalAI.kt | 3 +- .../com/unciv/logic/city/CityConstructions.kt | 4 +- .../managers/CityInfoConquestFunctions.kt | 6 +- .../civilization/managers/QuestManager.kt | 11 ++-- .../unciv/models/ruleset/tech/Technology.kt | 5 +- core/src/com/unciv/ui/components/UnitGroup.kt | 7 +- .../ui/components/tilegroups/CityButton.kt | 38 ++++++----- .../components/tilegroups/WorldTileGroup.kt | 4 +- .../unciv/ui/popups/options/AdvancedTab.kt | 4 +- .../com/unciv/ui/popups/options/DebugTab.kt | 10 +-- .../com/unciv/ui/popups/options/DisplayTab.kt | 8 ++- .../unciv/ui/popups/options/GameplayTab.kt | 3 +- .../unciv/ui/popups/options/OptionsPopup.kt | 3 +- .../unciv/ui/screens/cityscreen/CityScreen.kt | 3 +- .../cityscreen/ConstructionInfoTable.kt | 3 +- .../cityscreen/SpecialistAllocationTable.kt | 5 +- .../diplomacyscreen/DiplomacyScreen.kt | 3 +- .../screens/mainmenuscreen/MainMenuScreen.kt | 8 +-- .../EmpireOverviewCategories.kt | 2 +- .../NotificationsOverviewTable.kt | 10 +-- .../overviewscreen/StatsOverviewTable.kt | 3 +- .../overviewscreen/UnitOverviewTable.kt | 8 +-- .../ui/screens/pickerscreens/PickerPane.kt | 3 +- .../pickerscreens/PolicyPickerScreen.kt | 19 ++---- .../pickerscreens/PromotionPickerScreen.kt | 5 +- .../screens/pickerscreens/TechPickerScreen.kt | 3 +- .../ui/screens/worldscreen/WorldMapHolder.kt | 2 +- .../minimap/MapOverlayToggleButton.kt | 3 +- .../worldscreen/unit/actions/UnitActions.kt | 63 +++++++++--------- .../unit/actions/UnitActionsPillage.kt | 9 +-- .../unit/actions/UnitActionsTable.kt | 5 +- 32 files changed, 202 insertions(+), 125 deletions(-) diff --git a/core/src/com/unciv/UncivGame.kt b/core/src/com/unciv/UncivGame.kt index b5bfcc39e0..1d7aa08595 100644 --- a/core/src/com/unciv/UncivGame.kt +++ b/core/src/com/unciv/UncivGame.kt @@ -12,6 +12,7 @@ import com.unciv.logic.GameInfo import com.unciv.logic.IsPartOfGameInfoSerialization import com.unciv.logic.files.UncivFiles import com.unciv.logic.UncivShowableException +import com.unciv.logic.civilization.Civilization import com.unciv.logic.civilization.PlayerType import com.unciv.logic.multiplayer.OnlineMultiplayer import com.unciv.models.metadata.GameSettings @@ -36,7 +37,9 @@ import com.unciv.ui.screens.mainmenuscreen.MainMenuScreen import com.unciv.ui.screens.basescreen.BaseScreen import com.unciv.ui.components.extensions.center import com.unciv.ui.screens.worldscreen.PlayerReadyScreen +import com.unciv.ui.screens.worldscreen.WorldMapHolder import com.unciv.ui.screens.worldscreen.WorldScreen +import com.unciv.ui.screens.worldscreen.unit.UnitTable import com.unciv.utils.Log import com.unciv.utils.concurrency.Concurrency import com.unciv.utils.concurrency.launchOnGLThread @@ -48,6 +51,66 @@ import java.io.PrintWriter import java.util.* import kotlin.collections.ArrayDeque +object GUI { + + fun isDebugMapVisible(): Boolean { + return UncivGame.Current.viewEntireMapForDebug + } + + fun setUpdateWorldOnNextRender() { + UncivGame.Current.worldScreen?.shouldUpdate = true + } + + fun pushScreen(screen: BaseScreen) { + UncivGame.Current.pushScreen(screen) + } + + fun resetToWorldScreen() { + UncivGame.Current.resetToWorldScreen() + } + + fun getSettings(): GameSettings { + return UncivGame.Current.settings + } + + fun isWorldLoaded(): Boolean { + return UncivGame.Current.worldScreen != null + } + + fun isMyTurn(): Boolean { + return UncivGame.Current.worldScreen!!.isPlayersTurn + } + + fun isAllowedChangeState(): Boolean { + return UncivGame.Current.worldScreen!!.canChangeState + } + + fun getWorldScreen(): WorldScreen { + return UncivGame.Current.worldScreen!! + } + + fun getWorldScreenIfActive(): WorldScreen? { + return UncivGame.Current.getWorldScreenIfActive() + } + + fun getMap(): WorldMapHolder { + return UncivGame.Current.worldScreen!!.mapHolder + } + + fun getUnitTable(): UnitTable { + return UncivGame.Current.worldScreen!!.bottomUnitTable + } + + fun getViewingPlayer(): Civilization { + return UncivGame.Current.worldScreen!!.viewingCiv + } + + fun getSelectedPlayer(): Civilization { + return UncivGame.Current.worldScreen!!.selectedCiv + } + +} + class UncivGame(parameters: UncivGameParameters) : Game() { constructor() : this(UncivGameParameters()) @@ -61,7 +124,6 @@ class UncivGame(parameters: UncivGameParameters) : Game() { var deepLinkedMultiplayerGame: String? = null var gameInfo: GameInfo? = null - private set lateinit var settings: GameSettings lateinit var musicController: MusicController lateinit var onlineMultiplayer: OnlineMultiplayer diff --git a/core/src/com/unciv/logic/automation/ai/TacticalAI.kt b/core/src/com/unciv/logic/automation/ai/TacticalAI.kt index 98878ba6d3..e0779cda1d 100644 --- a/core/src/com/unciv/logic/automation/ai/TacticalAI.kt +++ b/core/src/com/unciv/logic/automation/ai/TacticalAI.kt @@ -1,6 +1,7 @@ package com.unciv.logic.automation.ai import com.badlogic.gdx.graphics.Color +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.IsPartOfGameInfoSerialization import com.unciv.logic.civilization.Civilization @@ -32,7 +33,7 @@ class TacticalAI : IsPartOfGameInfoSerialization { Log.debug("MYTAG Zone $zoneId City: ${zone?.city} Area: ${zone?.area} Area size: ${ tile.tileMap.continentSizes[tile.getContinent()]} Zone size: ${zone?.tileCount}") - val mapHolder = UncivGame.Current.worldScreen!!.mapHolder + val mapHolder = GUI.getMap() for (otherTile in mapHolder.tileMap.values.asSequence()) { diff --git a/core/src/com/unciv/logic/city/CityConstructions.kt b/core/src/com/unciv/logic/city/CityConstructions.kt index fd3c3899e6..a75b8f5ac7 100644 --- a/core/src/com/unciv/logic/city/CityConstructions.kt +++ b/core/src/com/unciv/logic/city/CityConstructions.kt @@ -1,5 +1,6 @@ package com.unciv.logic.city +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.IsPartOfGameInfoSerialization import com.unciv.logic.automation.Automation @@ -673,8 +674,7 @@ class CityConstructions : IsPartOfGameInfoSerialization { city.cityStats.update() city.civ.cache.updateCivResources() // If bought the worldscreen will not have been marked to update, and the new improvement won't show until later... - if (UncivGame.isCurrentInitialized() && UncivGame.Current.worldScreen != null) - UncivGame.Current.worldScreen!!.shouldUpdate = true + GUI.setUpdateWorldOnNextRender() } /** Support for [UniqueType.CreatesOneImprovement]: diff --git a/core/src/com/unciv/logic/city/managers/CityInfoConquestFunctions.kt b/core/src/com/unciv/logic/city/managers/CityInfoConquestFunctions.kt index 7d47f59671..442d330840 100644 --- a/core/src/com/unciv/logic/city/managers/CityInfoConquestFunctions.kt +++ b/core/src/com/unciv/logic/city/managers/CityInfoConquestFunctions.kt @@ -1,6 +1,7 @@ package com.unciv.logic.city.managers import com.unciv.Constants +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.battle.Battle import com.unciv.logic.city.City @@ -158,10 +159,7 @@ class CityInfoConquestFunctions(val city: City){ city.isPuppet = false city.cityConstructions.inProgressConstructions.clear() // undo all progress of the previous civ on units etc. city.cityStats.update() - val worldScreen = UncivGame.Current.worldScreen - if (!UncivGame.Current.consoleMode && worldScreen != null) { - worldScreen.shouldUpdate = true - } + GUI.setUpdateWorldOnNextRender() } private fun diplomaticRepercussionsForConqueringCity(oldCiv: Civilization, conqueringCiv: Civilization) { diff --git a/core/src/com/unciv/logic/civilization/managers/QuestManager.kt b/core/src/com/unciv/logic/civilization/managers/QuestManager.kt index a61fb4cfae..72e409cfe9 100644 --- a/core/src/com/unciv/logic/civilization/managers/QuestManager.kt +++ b/core/src/com/unciv/logic/civilization/managers/QuestManager.kt @@ -2,6 +2,7 @@ package com.unciv.logic.civilization.managers import com.badlogic.gdx.math.Vector2 import com.unciv.Constants +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.GameInfo import com.unciv.logic.IsPartOfGameInfoSerialization @@ -835,16 +836,14 @@ class AssignedQuest(val questName: String = "", } fun onClickAction() { - val game = UncivGame.Current - when (questName) { QuestName.ClearBarbarianCamp.value -> { - game.resetToWorldScreen() - game.worldScreen!!.mapHolder.setCenterPosition(Vector2(data1.toFloat(), data2.toFloat()), selectUnit = false) + GUI.resetToWorldScreen() + GUI.getMap().setCenterPosition(Vector2(data1.toFloat(), data2.toFloat()), selectUnit = false) } QuestName.Route.value -> { - game.resetToWorldScreen() - game.worldScreen!!.mapHolder.setCenterPosition(gameInfo.getCivilization(assigner).getCapital()!!.location, selectUnit = false) + GUI.resetToWorldScreen() + GUI.getMap().setCenterPosition(gameInfo.getCivilization(assigner).getCapital()!!.location, selectUnit = false) } } } diff --git a/core/src/com/unciv/models/ruleset/tech/Technology.kt b/core/src/com/unciv/models/ruleset/tech/Technology.kt index b940b783fa..8a9a6e4e14 100644 --- a/core/src/com/unciv/models/ruleset/tech/Technology.kt +++ b/core/src/com/unciv/models/ruleset/tech/Technology.kt @@ -1,5 +1,6 @@ package com.unciv.models.ruleset.tech +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.civilization.Civilization import com.unciv.models.ruleset.Building @@ -50,7 +51,7 @@ class Technology: RulesetObject() { } } - val viewingCiv = UncivGame.Current.worldScreen!!.viewingCiv + val viewingCiv = GUI.getViewingPlayer() val enabledUnits = getEnabledUnits(ruleset, viewingCiv) if (enabledUnits.any()) { lineList += "{Units enabled}: " @@ -224,7 +225,7 @@ class Technology: RulesetObject() { } } - val viewingCiv = UncivGame.Current.worldScreen?.viewingCiv + val viewingCiv = GUI.getViewingPlayer() val enabledUnits = getEnabledUnits(ruleset, viewingCiv) if (enabledUnits.any()) { lineList += FormattedLine() diff --git a/core/src/com/unciv/ui/components/UnitGroup.kt b/core/src/com/unciv/ui/components/UnitGroup.kt index 048838184e..640259aa1c 100644 --- a/core/src/com/unciv/ui/components/UnitGroup.kt +++ b/core/src/com/unciv/ui/components/UnitGroup.kt @@ -8,6 +8,7 @@ import com.badlogic.gdx.scenes.scene2d.actions.RepeatAction import com.badlogic.gdx.scenes.scene2d.ui.Image import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable import com.badlogic.gdx.utils.Align +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.map.mapunit.MapUnit import com.unciv.ui.components.extensions.addToCenter @@ -206,14 +207,14 @@ class UnitGroup(val unit: MapUnit, val size: Float): Group() { // Unit base icon is faded out only if out of moves // Foreign unit icons are never faded! - val shouldBeFaded = (unit.owner == UncivGame.Current.worldScreen?.viewingCiv?.civName - && unit.currentMovement == 0f && UncivGame.Current.settings.unitIconOpacity == 1f) + val shouldBeFaded = (unit.owner == GUI.getSelectedPlayer().civName + && unit.currentMovement == 0f && GUI.getSettings().unitIconOpacity == 1f) val alpha = if (shouldBeFaded) opacity * 0.5f else opacity flagIcon.color.a = alpha flagBg.color.a = alpha flagSelection.color.a = opacity - if (UncivGame.Current.settings.continuousRendering) { + if (GUI.getSettings().continuousRendering) { flagSelection.color.a = opacity flagSelection.addAction( Actions.repeat( diff --git a/core/src/com/unciv/ui/components/tilegroups/CityButton.kt b/core/src/com/unciv/ui/components/tilegroups/CityButton.kt index 811a1b86ff..f40613fc32 100644 --- a/core/src/com/unciv/ui/components/tilegroups/CityButton.kt +++ b/core/src/com/unciv/ui/components/tilegroups/CityButton.kt @@ -9,6 +9,7 @@ import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.actions.Actions import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Align +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.battle.CityCombatant import com.unciv.logic.city.City @@ -112,7 +113,7 @@ private class DefenceTable(city: City) : BorderedTable( init { - val viewingCiv = UncivGame.Current.worldScreen!!.viewingCiv + val viewingCiv = GUI.getViewingPlayer() borderSize = 4f bgColor = Color.BLACK @@ -160,18 +161,18 @@ class AirUnitTable(city: City, numberOfUnits: Int, size: Float=14f) : BorderedTa } -private class StatusTable(worldScreen: WorldScreen, city: City, iconSize: Float = 18f) : Table() { +private class StatusTable(city: City, iconSize: Float = 18f) : Table() { init { val padBetween = 2f - val viewingCiv = UncivGame.Current.worldScreen!!.viewingCiv + val viewingCiv = GUI.getViewingPlayer() if (city.civ == viewingCiv) { if (city.isBlockaded()) { val connectionImage = ImageGetter.getImage("OtherIcons/Blockade") add(connectionImage).size(iconSize) - worldScreen.displayTutorial(TutorialTrigger.CityBlockade) + GUI.getWorldScreen().displayTutorial(TutorialTrigger.CityBlockade) } else if (!city.isCapital() && city.isConnectedToCapital()) { val connectionImage = ImageGetter.getStatIcon("CityConnection") add(connectionImage).size(iconSize) @@ -210,7 +211,7 @@ private class CityTable(city: City, forPopup: Boolean = false) : BorderedTable( isTransform = false touchable = Touchable.enabled - val viewingCiv = UncivGame.Current.worldScreen!!.viewingCiv + val viewingCiv = GUI.getViewingPlayer() bgBorderColor = when { city.civ == viewingCiv -> colorFromRGB(233, 233, 172) @@ -392,9 +393,6 @@ private class CityTable(city: City, forPopup: Boolean = false) : BorderedTable( class CityButton(val city: City, private val tileGroup: TileGroup): Table(BaseScreen.skin){ - val worldScreen = UncivGame.Current.worldScreen!! - val uncivGame = worldScreen.game - init { touchable = Touchable.disabled } @@ -431,13 +429,13 @@ class CityButton(val city: City, private val tileGroup: TileGroup): Table(BaseSc add(cityTable).row() // If city state - add influence bar - if (city.civ.isCityState() && city.civ.knows(worldScreen.viewingCiv)) { - val diplomacyManager = city.civ.getDiplomacyManager(worldScreen.viewingCiv) + if (city.civ.isCityState() && city.civ.knows(GUI.getViewingPlayer())) { + val diplomacyManager = city.civ.getDiplomacyManager(GUI.getViewingPlayer()) add(InfluenceTable(diplomacyManager.getInfluence(), diplomacyManager.relationshipLevel())).padTop(1f).row() } // Add statuses: connection, resistance, puppet, raze, WLTKD - add(StatusTable(worldScreen, city)).padTop(3f) + add(StatusTable(city)).padTop(3f) pack() @@ -481,13 +479,13 @@ class CityButton(val city: City, private val tileGroup: TileGroup): Table(BaseSc (tile.civilianUnit != null) && direction.epsilonEquals(0f, 1f) -> insertHiddenUnitMarker(HiddenUnitMarkerPosition.Left) // detect military under the city - (tile.militaryUnit != null && !tile.hasEnemyInvisibleUnit(worldScreen.viewingCiv)) && direction.epsilonEquals(1f, 1f) -> + (tile.militaryUnit != null && !tile.hasEnemyInvisibleUnit(GUI.getViewingPlayer())) && direction.epsilonEquals(1f, 1f) -> insertHiddenUnitMarker(HiddenUnitMarkerPosition.Center) // detect civilian right-below the city (tile.civilianUnit != null) && direction.epsilonEquals(1f, 0f) -> insertHiddenUnitMarker(HiddenUnitMarkerPosition.Right) } - } else if (tile.militaryUnit != null && !tile.hasEnemyInvisibleUnit(worldScreen.viewingCiv)) { + } else if (tile.militaryUnit != null && !tile.hasEnemyInvisibleUnit(GUI.getViewingPlayer())) { when { // detect military left from the city direction.epsilonEquals(0f, 1f) -> @@ -518,11 +516,11 @@ class CityButton(val city: City, private val tileGroup: TileGroup): Table(BaseSc listOfHiddenUnitMarkers.add(indicator) } - private fun belongsToViewingCiv() = city.civ == worldScreen.viewingCiv + private fun belongsToViewingCiv() = city.civ == GUI.getViewingPlayer() private fun setButtonActions() { - val unitTable = worldScreen.bottomUnitTable + val unitTable = GUI.getUnitTable() // So you can click anywhere on the button to go to the city touchable = Touchable.childrenOnly @@ -531,12 +529,12 @@ class CityButton(val city: City, private val tileGroup: TileGroup): Table(BaseSc // clicking swings the button a little down to allow selection of units there. // this also allows to target selected units to move to the city tile from elsewhere. if (isButtonMoved) { - val viewingCiv = worldScreen.viewingCiv + val viewingCiv = GUI.getViewingPlayer() // second tap on the button will go to the city screen // if this city belongs to you and you are not iterating though the air units - if (uncivGame.viewEntireMapForDebug || viewingCiv.isSpectator() + if (GUI.isDebugMapVisible() || viewingCiv.isSpectator() || (belongsToViewingCiv() && !tileGroup.tile.airUnits.contains(unitTable.selectedUnit))) { - uncivGame.pushScreen(CityScreen(city)) + GUI.pushScreen(CityScreen(city)) } else if (viewingCiv.knows(city.civ)) { foreignCityInfoPopup() } @@ -584,13 +582,13 @@ class CityButton(val city: City, private val tileGroup: TileGroup): Table(BaseSc private fun foreignCityInfoPopup() { fun openDiplomacy() { // If city doesn't belong to you, go directly to its owner's diplomacy screen. - worldScreen.game.pushScreen(DiplomacyScreen(worldScreen.viewingCiv, city.civ)) + GUI.pushScreen(DiplomacyScreen(GUI.getViewingPlayer(), city.civ)) } // If there's nothing to display cuz no Religion - skip popup if (!city.civ.gameInfo.isReligionEnabled()) return openDiplomacy() - val popup = Popup(worldScreen).apply { + val popup = Popup(GUI.getWorldScreen()).apply { name = "ForeignCityInfoPopup" add(CityTable(city, true)).fillX().padBottom(5f).colspan(3).row() add(CityReligionInfoTable(city.religion, true)).colspan(3).row() diff --git a/core/src/com/unciv/ui/components/tilegroups/WorldTileGroup.kt b/core/src/com/unciv/ui/components/tilegroups/WorldTileGroup.kt index f7c2d9ae14..b06ba2728e 100644 --- a/core/src/com/unciv/ui/components/tilegroups/WorldTileGroup.kt +++ b/core/src/com/unciv/ui/components/tilegroups/WorldTileGroup.kt @@ -11,7 +11,7 @@ import com.unciv.ui.components.extensions.darken import com.unciv.ui.screens.worldscreen.WorldScreen -class WorldTileGroup(internal val worldScreen: WorldScreen, tile: Tile, tileSetStrings: TileSetStrings) +class WorldTileGroup(tile: Tile, tileSetStrings: TileSetStrings) : TileGroup(tile,tileSetStrings) { init { @@ -50,5 +50,5 @@ class WorldTileGroup(internal val worldScreen: WorldScreen, tile: Tile, tileSetS } } - override fun clone(): WorldTileGroup = WorldTileGroup(worldScreen, tile , tileSetStrings) + override fun clone(): WorldTileGroup = WorldTileGroup(tile , tileSetStrings) } diff --git a/core/src/com/unciv/ui/popups/options/AdvancedTab.kt b/core/src/com/unciv/ui/popups/options/AdvancedTab.kt index 2c741bd8e3..9bd27152d8 100644 --- a/core/src/com/unciv/ui/popups/options/AdvancedTab.kt +++ b/core/src/com/unciv/ui/popups/options/AdvancedTab.kt @@ -13,6 +13,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Cell import com.badlogic.gdx.scenes.scene2d.ui.SelectBox import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Array +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.models.metadata.GameSettings import com.unciv.models.metadata.ScreenSize @@ -169,7 +170,8 @@ private fun addMaxZoomSlider(table: Table, settings: GameSettings) { ) { settings.maxWorldZoomOut = it settings.save() - UncivGame.Current.worldScreen?.mapHolder?.reloadMaxZoom() + if (GUI.isWorldLoaded()) + GUI.getMap().reloadMaxZoom() } table.add(maxZoomSlider).pad(5f).row() } diff --git a/core/src/com/unciv/ui/popups/options/DebugTab.kt b/core/src/com/unciv/ui/popups/options/DebugTab.kt index aa0c7c471b..72c8687c51 100644 --- a/core/src/com/unciv/ui/popups/options/DebugTab.kt +++ b/core/src/com/unciv/ui/popups/options/DebugTab.kt @@ -1,6 +1,7 @@ package com.unciv.ui.popups.options import com.badlogic.gdx.scenes.scene2d.ui.Table +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.files.UncivFiles import com.unciv.logic.files.MapSaver @@ -19,8 +20,7 @@ fun debugTab() = Table(BaseScreen.skin).apply { defaults().pad(5f) val game = UncivGame.Current - val worldScreen = game.worldScreen - if (worldScreen != null) { + if (GUI.isWorldLoaded()) { val simulateButton = "Simulate until turn:".toTextButton() val simulateTextField = UncivTextField.create("Turn", game.simulateUntilTurnForDebug.toString()) val invalidInputLabel = "This is not a valid integer!".toLabel().also { it.isVisible = false } @@ -32,7 +32,7 @@ fun debugTab() = Table(BaseScreen.skin).apply { } game.simulateUntilTurnForDebug = simulateUntilTurns invalidInputLabel.isVisible = false - worldScreen.nextTurn() + GUI.getWorldScreen().nextTurn() } add(simulateButton) add(simulateTextField).row() @@ -89,7 +89,7 @@ fun debugTab() = Table(BaseScreen.skin).apply { } } curGameInfo.getCurrentPlayerCivilization().cache.updateSightAndResources() - if (worldScreen != null) worldScreen.shouldUpdate = true + GUI.setUpdateWorldOnNextRender() } add(unlockTechsButton).colspan(2).row() @@ -107,7 +107,7 @@ fun debugTab() = Table(BaseScreen.skin).apply { tile.changeImprovement(resource.getImprovements().first()) } curGameInfo.getCurrentPlayerCivilization().cache.updateSightAndResources() - if (worldScreen != null) worldScreen.shouldUpdate = true + GUI.setUpdateWorldOnNextRender() } add(giveResourcesButton).colspan(2).row() } diff --git a/core/src/com/unciv/ui/popups/options/DisplayTab.kt b/core/src/com/unciv/ui/popups/options/DisplayTab.kt index 46f5eb4911..d8ce9839ec 100644 --- a/core/src/com/unciv/ui/popups/options/DisplayTab.kt +++ b/core/src/com/unciv/ui/popups/options/DisplayTab.kt @@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.ui.SelectBox import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Array +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.models.metadata.GameSettings import com.unciv.models.metadata.ScreenSize @@ -39,8 +40,9 @@ fun displayTab( if (Gdx.app.type == Application.ApplicationType.Desktop) { addFullscreenSelectBox(this, settings, optionsPopup.selectBoxMinWidth) optionsPopup.addCheckbox(this, "Map mouse auto-scroll", settings.mapAutoScroll, true) { - UncivGame.Current.worldScreen?.mapHolder?.isAutoScrollEnabled = it settings.mapAutoScroll = it + if (GUI.isWorldLoaded()) + GUI.getMap().isAutoScrollEnabled = settings.mapAutoScroll } } @@ -112,9 +114,9 @@ private fun addMinimapSizeSlider(table: Table, settings: GameSettings, selectBox settings.minimapSize = size } settings.save() - val worldScreen = UncivGame.Current.getWorldScreenIfActive() + val worldScreen = GUI.getWorldScreenIfActive() if (worldScreen != null) - worldScreen.shouldUpdate = true + GUI.setUpdateWorldOnNextRender() } table.add(minimapSlider).minWidth(selectBoxMinWidth).pad(10f).row() } diff --git a/core/src/com/unciv/ui/popups/options/GameplayTab.kt b/core/src/com/unciv/ui/popups/options/GameplayTab.kt index 716a8ab3cd..92d31875c1 100644 --- a/core/src/com/unciv/ui/popups/options/GameplayTab.kt +++ b/core/src/com/unciv/ui/popups/options/GameplayTab.kt @@ -1,6 +1,7 @@ package com.unciv.ui.popups.options import com.badlogic.gdx.scenes.scene2d.ui.Table +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.civilization.PlayerType import com.unciv.models.metadata.GameSettings @@ -21,7 +22,7 @@ fun gameplayTab( optionsPopup.addCheckbox(this, "Move units with a single tap", settings.singleTapMove) { settings.singleTapMove = it } optionsPopup.addCheckbox(this, "Auto-assign city production", settings.autoAssignCityProduction, true) { shouldAutoAssignCityProduction -> settings.autoAssignCityProduction = shouldAutoAssignCityProduction - val worldScreen = UncivGame.Current.getWorldScreenIfActive() + val worldScreen = GUI.getWorldScreenIfActive() if (shouldAutoAssignCityProduction && worldScreen != null && worldScreen.viewingCiv.isCurrentPlayer() && worldScreen.viewingCiv.playerType == PlayerType.Human ) { diff --git a/core/src/com/unciv/ui/popups/options/OptionsPopup.kt b/core/src/com/unciv/ui/popups/options/OptionsPopup.kt index 0e8856171b..c649d9287a 100644 --- a/core/src/com/unciv/ui/popups/options/OptionsPopup.kt +++ b/core/src/com/unciv/ui/popups/options/OptionsPopup.kt @@ -8,6 +8,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.SelectBox import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener import com.badlogic.gdx.utils.Array +import com.unciv.GUI import com.unciv.ui.screens.mainmenuscreen.MainMenuScreen import com.unciv.UncivGame import com.unciv.logic.event.EventBus @@ -161,7 +162,7 @@ class OptionsPopup( val checkbox = text.toCheckBox(initialState) { action(it) settings.save() - val worldScreen = UncivGame.Current.getWorldScreenIfActive() + val worldScreen = GUI.getWorldScreenIfActive() if (updateWorld && worldScreen != null) worldScreen.shouldUpdate = true } if (newRow) table.add(checkbox).colspan(2).left().row() diff --git a/core/src/com/unciv/ui/screens/cityscreen/CityScreen.kt b/core/src/com/unciv/ui/screens/cityscreen/CityScreen.kt index 1540e48c24..6a8dad2e53 100644 --- a/core/src/com/unciv/ui/screens/cityscreen/CityScreen.kt +++ b/core/src/com/unciv/ui/screens/cityscreen/CityScreen.kt @@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.ui.Image import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Align +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.automation.Automation import com.unciv.logic.city.City @@ -53,7 +54,7 @@ class CityScreen( } /** Toggles or adds/removes all state changing buttons */ - val canChangeState = UncivGame.Current.worldScreen!!.canChangeState + val canChangeState = GUI.isAllowedChangeState() // Clockwise from the top-left diff --git a/core/src/com/unciv/ui/screens/cityscreen/ConstructionInfoTable.kt b/core/src/com/unciv/ui/screens/cityscreen/ConstructionInfoTable.kt index adcfa7b1f8..24566c933f 100644 --- a/core/src/com/unciv/ui/screens/cityscreen/ConstructionInfoTable.kt +++ b/core/src/com/unciv/ui/screens/cityscreen/ConstructionInfoTable.kt @@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.ui.Label import com.badlogic.gdx.scenes.scene2d.ui.Table +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.city.IConstruction import com.unciv.logic.city.PerpetualConstruction @@ -118,7 +119,7 @@ class ConstructionInfoTable(val cityScreen: CityScreen): Table() { } if (cityScreen.city.hasSoldBuildingThisTurn && !cityScreen.city.civ.gameInfo.gameParameters.godMode || cityScreen.city.isPuppet - || !UncivGame.Current.worldScreen!!.isPlayersTurn || !cityScreen.canChangeState) + || !GUI.isMyTurn() || !GUI.isAllowedChangeState()) sellBuildingButton.disable() } } diff --git a/core/src/com/unciv/ui/screens/cityscreen/SpecialistAllocationTable.kt b/core/src/com/unciv/ui/screens/cityscreen/SpecialistAllocationTable.kt index 86d6e9b87e..6797f08392 100644 --- a/core/src/com/unciv/ui/screens/cityscreen/SpecialistAllocationTable.kt +++ b/core/src/com/unciv/ui/screens/cityscreen/SpecialistAllocationTable.kt @@ -5,6 +5,7 @@ import com.badlogic.gdx.scenes.scene2d.Actor import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Align import com.unciv.Constants +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.ui.images.ImageGetter import com.unciv.ui.screens.basescreen.BaseScreen @@ -82,7 +83,7 @@ class SpecialistAllocationTable(val cityScreen: CityScreen) : Table(BaseScreen.s cityInfo.cityStats.update() cityScreen.update() } - if (cityInfo.population.getFreePopulation() == 0 || !UncivGame.Current.worldScreen!!.isPlayersTurn) + if (cityInfo.population.getFreePopulation() == 0 || !GUI.isMyTurn()) assignButton.clear() return assignButton } @@ -99,7 +100,7 @@ class SpecialistAllocationTable(val cityScreen: CityScreen) : Table(BaseScreen.s } if (assignedSpecialists <= 0 || cityInfo.isPuppet) unassignButton.isVisible = false - if (!UncivGame.Current.worldScreen!!.isPlayersTurn) unassignButton.clear() + if (!GUI.isMyTurn()) unassignButton.clear() return unassignButton } diff --git a/core/src/com/unciv/ui/screens/diplomacyscreen/DiplomacyScreen.kt b/core/src/com/unciv/ui/screens/diplomacyscreen/DiplomacyScreen.kt index b6089f458f..a4af2a7897 100644 --- a/core/src/com/unciv/ui/screens/diplomacyscreen/DiplomacyScreen.kt +++ b/core/src/com/unciv/ui/screens/diplomacyscreen/DiplomacyScreen.kt @@ -6,6 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.TextButton import com.badlogic.gdx.utils.Align import com.unciv.Constants +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.civilization.AlertType import com.unciv.logic.civilization.Civilization @@ -72,7 +73,7 @@ class DiplomacyScreen( private val rightSideTable = Table() private val closeButton = Constants.close.toTextButton() - private fun isNotPlayersTurn() = !UncivGame.Current.worldScreen!!.canChangeState + private fun isNotPlayersTurn() = !GUI.isMyTurn() || !GUI.isAllowedChangeState() init { val splitPane = SplitPane(leftSideScroll, rightSideTable, false, skin) diff --git a/core/src/com/unciv/ui/screens/mainmenuscreen/MainMenuScreen.kt b/core/src/com/unciv/ui/screens/mainmenuscreen/MainMenuScreen.kt index 90690ad012..1ba8340189 100644 --- a/core/src/com/unciv/ui/screens/mainmenuscreen/MainMenuScreen.kt +++ b/core/src/com/unciv/ui/screens/mainmenuscreen/MainMenuScreen.kt @@ -6,6 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.actions.Actions import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Align +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.GameInfo import com.unciv.logic.GameStarter @@ -219,11 +220,10 @@ class MainMenuScreen: BaseScreen(), RecreateOnResize { private fun resumeGame() { - val curWorldScreen = game.worldScreen - if (curWorldScreen != null) { - game.resetToWorldScreen() + if (GUI.isWorldLoaded()) { + GUI.resetToWorldScreen() + GUI.getWorldScreen().popups.filterIsInstance(WorldScreenMenuPopup::class.java).forEach(Popup::close) ImageGetter.ruleset = game.gameInfo!!.ruleset - curWorldScreen.popups.filterIsInstance(WorldScreenMenuPopup::class.java).forEach(Popup::close) } else { QuickSave.autoLoadGame(this) } diff --git a/core/src/com/unciv/ui/screens/overviewscreen/EmpireOverviewCategories.kt b/core/src/com/unciv/ui/screens/overviewscreen/EmpireOverviewCategories.kt index 6820416fbc..ffd15cf70f 100644 --- a/core/src/com/unciv/ui/screens/overviewscreen/EmpireOverviewCategories.kt +++ b/core/src/com/unciv/ui/screens/overviewscreen/EmpireOverviewCategories.kt @@ -64,7 +64,7 @@ enum class EmpireOverviewCategories( fun (viewingPlayer: Civilization) = (viewingPlayer.naturalWonders.isEmpty() && viewingPlayer.cities.isEmpty()).toState()), Notifications("OtherIcons/Notifications", 'N', Align.top, fun (viewingPlayer: Civilization, overviewScreen: EmpireOverviewScreen, persistedData: EmpireOverviewTabPersistableData?) - = NotificationsOverviewTable(worldScreen = UncivGame.Current.worldScreen!!, viewingPlayer, overviewScreen, persistedData), + = NotificationsOverviewTable(viewingPlayer, overviewScreen, persistedData), fun (_: Civilization) = EmpireOverviewTabState.Normal) ; //must be here diff --git a/core/src/com/unciv/ui/screens/overviewscreen/NotificationsOverviewTable.kt b/core/src/com/unciv/ui/screens/overviewscreen/NotificationsOverviewTable.kt index edd320d8f3..f4c525d980 100644 --- a/core/src/com/unciv/ui/screens/overviewscreen/NotificationsOverviewTable.kt +++ b/core/src/com/unciv/ui/screens/overviewscreen/NotificationsOverviewTable.kt @@ -3,6 +3,7 @@ package com.unciv.ui.screens.overviewscreen import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.ui.Table +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.civilization.Civilization import com.unciv.logic.civilization.Notification @@ -16,7 +17,6 @@ import com.unciv.ui.components.extensions.toLabel import com.unciv.ui.screens.worldscreen.WorldScreen class NotificationsOverviewTable( - val worldScreen: WorldScreen, viewingPlayer: Civilization, overviewScreen: EmpireOverviewScreen, persistedData: EmpireOverviewTabPersistableData? = null @@ -36,12 +36,14 @@ class NotificationsOverviewTable( persistableData.scrollY = pager.getPageScrollY(index) } - val notificationLog = viewingPlayer.notificationsLog + private val worldScreen = GUI.getWorldScreen() + + private val notificationLog = viewingPlayer.notificationsLog private val notificationTable = Table(BaseScreen.skin) - val scaleFactor = 0.3f - val inverseScaleFactor = 1f / scaleFactor + private val scaleFactor = 0.3f + private val inverseScaleFactor = 1f / scaleFactor private val maxWidthOfStage = 0.333f private val maxEntryWidth = worldScreen.stage.width * maxWidthOfStage * inverseScaleFactor diff --git a/core/src/com/unciv/ui/screens/overviewscreen/StatsOverviewTable.kt b/core/src/com/unciv/ui/screens/overviewscreen/StatsOverviewTable.kt index 519a11d4c0..cd45f9f56b 100644 --- a/core/src/com/unciv/ui/screens/overviewscreen/StatsOverviewTable.kt +++ b/core/src/com/unciv/ui/screens/overviewscreen/StatsOverviewTable.kt @@ -3,6 +3,7 @@ package com.unciv.ui.screens.overviewscreen import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.ui.Table import com.unciv.Constants +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.civilization.Civilization import com.unciv.models.ruleset.ModOptionsConstants @@ -138,7 +139,7 @@ class StatsOverviewTab( for (city in viewingPlayer.cities) { city.cityStats.update() } update() } - slider.isDisabled = !UncivGame.Current.worldScreen!!.canChangeState + slider.isDisabled = !GUI.isAllowedChangeState() sliderTable.add(slider).padTop(15f) add(sliderTable).colspan(2) diff --git a/core/src/com/unciv/ui/screens/overviewscreen/UnitOverviewTable.kt b/core/src/com/unciv/ui/screens/overviewscreen/UnitOverviewTable.kt index 8a83395536..495824fbef 100644 --- a/core/src/com/unciv/ui/screens/overviewscreen/UnitOverviewTable.kt +++ b/core/src/com/unciv/ui/screens/overviewscreen/UnitOverviewTable.kt @@ -6,6 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.Group import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Align import com.unciv.Constants +import com.unciv.GUI import com.unciv.logic.civilization.Civilization import com.unciv.logic.map.mapunit.MapUnit import com.unciv.logic.map.tile.Tile @@ -80,9 +81,8 @@ class UnitOverviewTab( } private fun showWorldScreenAt(position: Vector2, unit: MapUnit?) { - val game = overviewScreen.game - game.resetToWorldScreen() - game.worldScreen!!.mapHolder.setCenterPosition(position, forceSelectUnit = unit) + GUI.resetToWorldScreen() + GUI.getMap().setCenterPosition(position, forceSelectUnit = unit) } private fun showWorldScreenAt(unit: MapUnit) = showWorldScreenAt(unit.currentTile.position, unit) private fun showWorldScreenAt(tile: Tile) = showWorldScreenAt(tile.position, null) @@ -216,7 +216,7 @@ class UnitOverviewTab( if (unit.promotions.canBePromoted()) promotionsTable.add( ImageGetter.getImage("OtherIcons/Star").apply { - color = if (game.worldScreen!!.canChangeState && unit.currentMovement > 0f && unit.attacksThisTurn == 0) + color = if (GUI.isAllowedChangeState() && unit.currentMovement > 0f && unit.attacksThisTurn == 0) Color.GOLDENROD else Color.GOLDENROD.darken(0.25f) } diff --git a/core/src/com/unciv/ui/screens/pickerscreens/PickerPane.kt b/core/src/com/unciv/ui/screens/pickerscreens/PickerPane.kt index 265f9d2d86..e4ba146d12 100644 --- a/core/src/com/unciv/ui/screens/pickerscreens/PickerPane.kt +++ b/core/src/com/unciv/ui/screens/pickerscreens/PickerPane.kt @@ -6,6 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.SplitPane import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup import com.unciv.Constants +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.ui.images.IconTextButton import com.unciv.ui.components.AutoScrollPane @@ -73,7 +74,7 @@ class PickerPane( /** Sets the text of the [rightSideButton] and enables it if it's the player's turn */ fun pick(rightButtonText: String) { - if (UncivGame.Current.worldScreen!!.isPlayersTurn) rightSideButton.enable() + if (GUI.isMyTurn()) rightSideButton.enable() rightSideButton.setText(rightButtonText) } diff --git a/core/src/com/unciv/ui/screens/pickerscreens/PolicyPickerScreen.kt b/core/src/com/unciv/ui/screens/pickerscreens/PolicyPickerScreen.kt index 932eb4216d..3bb6006f96 100644 --- a/core/src/com/unciv/ui/screens/pickerscreens/PolicyPickerScreen.kt +++ b/core/src/com/unciv/ui/screens/pickerscreens/PolicyPickerScreen.kt @@ -8,6 +8,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Cell import com.badlogic.gdx.scenes.scene2d.ui.Image import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Align +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.civilization.Civilization import com.unciv.models.TutorialTrigger @@ -51,16 +52,13 @@ private object PolicyColors { } fun Policy.isAdopted() : Boolean { - val worldScreen = UncivGame.Current.worldScreen ?: return false - val viewingCiv = worldScreen.selectedCiv - return viewingCiv.policies.isAdopted(this.name) + return GUI.getSelectedPlayer().policies.isAdopted(this.name) } fun Policy.isPickable() : Boolean { - val worldScreen = UncivGame.Current.worldScreen ?: return false - val viewingCiv = worldScreen.viewingCiv - val selectedCiv = worldScreen.selectedCiv - if (!worldScreen.isPlayersTurn + val viewingCiv = GUI.getViewingPlayer() + val selectedCiv = GUI.getSelectedPlayer() + if (!GUI.isMyTurn() || viewingCiv.isDefeated() || selectedCiv.policies.isAdopted(this.name) || this.policyBranchType == PolicyBranchType.BranchComplete @@ -109,13 +107,10 @@ class PolicyButton(val policy: Policy, size: Float = 30f) : BorderedTable( return this } - fun updateState() { - - val worldScreen = UncivGame.Current.worldScreen ?: return - val viewingCiv = worldScreen.selectedCiv + private fun updateState() { val isPickable = policy.isPickable() - val isAdopted = viewingCiv.policies.isAdopted(policy.name) + val isAdopted = GUI.getSelectedPlayer().policies.isAdopted(policy.name) when { diff --git a/core/src/com/unciv/ui/screens/pickerscreens/PromotionPickerScreen.kt b/core/src/com/unciv/ui/screens/pickerscreens/PromotionPickerScreen.kt index 6534bb296e..a008250282 100644 --- a/core/src/com/unciv/ui/screens/pickerscreens/PromotionPickerScreen.kt +++ b/core/src/com/unciv/ui/screens/pickerscreens/PromotionPickerScreen.kt @@ -8,6 +8,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Cell import com.badlogic.gdx.scenes.scene2d.ui.Image import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Align +import com.unciv.GUI import com.unciv.logic.map.mapunit.MapUnit import com.unciv.models.TutorialTrigger import com.unciv.models.UncivSound @@ -161,7 +162,7 @@ class PromotionPickerScreen(val unit: MapUnit) : PickerScreen(), RecreateOnResiz } val canBePromoted = unit.promotions.canBePromoted() - val canChangeState = game.worldScreen!!.canChangeState + val canChangeState = GUI.isAllowedChangeState() val canPromoteNow = canBePromoted && canChangeState && unit.currentMovement > 0 && unit.attacksThisTurn == 0 rightSideButton.isEnabled = canPromoteNow @@ -202,7 +203,7 @@ class PromotionPickerScreen(val unit: MapUnit) : PickerScreen(), RecreateOnResiz val availablePromotions = unit.promotions.getAvailablePromotions() val canBePromoted = unit.promotions.canBePromoted() - val canChangeState = game.worldScreen!!.canChangeState + val canChangeState = GUI.isAllowedChangeState() // Create nodes // Pass 1 - create nodes for all promotions diff --git a/core/src/com/unciv/ui/screens/pickerscreens/TechPickerScreen.kt b/core/src/com/unciv/ui/screens/pickerscreens/TechPickerScreen.kt index 95652377bb..8ed6ed740f 100644 --- a/core/src/com/unciv/ui/screens/pickerscreens/TechPickerScreen.kt +++ b/core/src/com/unciv/ui/screens/pickerscreens/TechPickerScreen.kt @@ -8,6 +8,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Label import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Align import com.unciv.Constants +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.civilization.Civilization import com.unciv.logic.civilization.managers.TechManager @@ -398,7 +399,7 @@ class TechPickerScreen( return } - if (!UncivGame.Current.worldScreen!!.canChangeState) { + if (!GUI.isAllowedChangeState()) { rightSideButton.disable() return } diff --git a/core/src/com/unciv/ui/screens/worldscreen/WorldMapHolder.kt b/core/src/com/unciv/ui/screens/worldscreen/WorldMapHolder.kt index 6b3f1c3726..1cad690475 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/WorldMapHolder.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/WorldMapHolder.kt @@ -109,7 +109,7 @@ class WorldMapHolder( internal fun addTiles() { val tileSetStrings = TileSetStrings() - val tileGroupsNew = tileMap.values.map { WorldTileGroup(worldScreen, it, tileSetStrings) } + val tileGroupsNew = tileMap.values.map { WorldTileGroup(it, tileSetStrings) } tileGroupMap = TileGroupMap(this, tileGroupsNew, continuousScrollingX) for (tileGroup in tileGroupsNew) { diff --git a/core/src/com/unciv/ui/screens/worldscreen/minimap/MapOverlayToggleButton.kt b/core/src/com/unciv/ui/screens/worldscreen/minimap/MapOverlayToggleButton.kt index 89fb0295fb..07b17aa46f 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/minimap/MapOverlayToggleButton.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/minimap/MapOverlayToggleButton.kt @@ -3,6 +3,7 @@ package com.unciv.ui.screens.worldscreen.minimap import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.Actor import com.badlogic.gdx.scenes.scene2d.ui.Image +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.ui.images.IconCircleGroup import com.unciv.ui.components.extensions.onClick @@ -42,7 +43,7 @@ class MapOverlayToggleButton( /** Toggle overlay. Called on click. */ fun toggle() { setter(!getter()) - UncivGame.Current.worldScreen!!.shouldUpdate = true + GUI.setUpdateWorldOnNextRender() // Setting worldScreen.shouldUpdate implicitly causes this.update() to be called by the WorldScreen on the next update. } diff --git a/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActions.kt b/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActions.kt index e3c33c3647..f30b99d1b0 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActions.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActions.kt @@ -1,6 +1,7 @@ package com.unciv.ui.screens.worldscreen.unit.actions import com.unciv.Constants +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.automation.unit.UnitAutomation import com.unciv.logic.automation.unit.WorkerAutomation @@ -30,14 +31,13 @@ import com.unciv.ui.screens.worldscreen.unit.UnitTable object UnitActions { - fun getUnitActions(unit: MapUnit, worldScreen: WorldScreen): List { - return if (unit.showAdditionalActions) getAdditionalActions(unit, worldScreen) - else getNormalActions(unit, worldScreen) + fun getUnitActions(unit: MapUnit): List { + return if (unit.showAdditionalActions) getAdditionalActions(unit) + else getNormalActions(unit) } - private fun getNormalActions(unit: MapUnit, worldScreen: WorldScreen): List { + private fun getNormalActions(unit: MapUnit): List { val tile = unit.getTile() - val unitTable = worldScreen.bottomUnitTable val actionList = ArrayList() if (unit.isMoving()) @@ -53,12 +53,12 @@ object UnitActions { addPromoteAction(unit, actionList) UnitActionsUpgrade.addUnitUpgradeAction(unit, actionList) addTransformAction(unit, actionList) - UnitActionsPillage.addPillageAction(unit, actionList, worldScreen) + UnitActionsPillage.addPillageAction(unit, actionList) addParadropAction(unit, actionList) addAirSweepAction(unit, actionList) addSetupAction(unit, actionList) addFoundCityAction(unit, actionList, tile) - addBuildingImprovementsAction(unit, actionList, tile, worldScreen) + addBuildingImprovementsAction(unit, actionList, tile) addRepairAction(unit, actionList) addCreateWaterImprovements(unit, actionList) UnitActionsGreatPerson.addGreatPersonActions(unit, actionList, tile) @@ -71,32 +71,32 @@ object UnitActions { addTriggerUniqueActions(unit, actionList) addAddInCapitalAction(unit, actionList, tile) - addWaitAction(unit, actionList, worldScreen) + addWaitAction(unit, actionList) - addToggleActionsAction(unit, actionList, unitTable) + addToggleActionsAction(unit, actionList) return actionList } - private fun getAdditionalActions(unit: MapUnit, worldScreen: WorldScreen): List { + private fun getAdditionalActions(unit: MapUnit): List { val tile = unit.getTile() - val unitTable = worldScreen.bottomUnitTable val actionList = ArrayList() addSleepActions(actionList, unit, true) addFortifyActions(actionList, unit, true) - addSwapAction(unit, actionList, worldScreen) - addDisbandAction(actionList, unit, worldScreen) + addSwapAction(unit, actionList) + addDisbandAction(actionList, unit) addGiftAction(unit, actionList, tile) - addToggleActionsAction(unit, actionList, unitTable) + addToggleActionsAction(unit, actionList) return actionList } - private fun addSwapAction(unit: MapUnit, actionList: ArrayList, worldScreen: WorldScreen) { + private fun addSwapAction(unit: MapUnit, actionList: ArrayList) { + val worldScreen = GUI.getWorldScreen() // Air units cannot swap if (unit.baseUnit.movesLikeAirUnits()) return // Disable unit swapping if multiple units are selected. It would make little sense. @@ -118,16 +118,18 @@ object UnitActions { ) } - private fun addDisbandAction(actionList: ArrayList, unit: MapUnit, worldScreen: WorldScreen) { + private fun addDisbandAction(actionList: ArrayList, unit: MapUnit) { + val worldScreen = GUI.getWorldScreen() actionList += UnitAction(type = UnitActionType.DisbandUnit, action = { if (!worldScreen.hasOpenPopups()) { val disbandText = if (unit.currentTile.getOwner() == unit.civ) "Disband this unit for [${unit.baseUnit.getDisbandGold(unit.civ)}] gold?".tr() else "Do you really want to disband this unit?".tr() - ConfirmPopup(UncivGame.Current.worldScreen!!, disbandText, "Disband unit") { + ConfirmPopup(worldScreen, disbandText, "Disband unit") { unit.disband() - worldScreen.shouldUpdate = true - if (UncivGame.Current.settings.autoUnitCycle) worldScreen.switchToNextUnit() + GUI.setUpdateWorldOnNextRender() + if (GUI.getSettings().autoUnitCycle) + worldScreen.switchToNextUnit() }.open() } }.takeIf { unit.currentMovement > 0 }) @@ -189,7 +191,7 @@ object UnitActions { tile.changeImprovement("City center") tile.removeRoad() unit.destroy() - UncivGame.Current.worldScreen!!.shouldUpdate = true + GUI.setUpdateWorldOnNextRender() } if (unit.civ.playerType == PlayerType.AI) @@ -206,7 +208,7 @@ object UnitActions { else { // ask if we would be breaking a promise val text = "Do you want to break your promise to [$leaders]?" - ConfirmPopup(UncivGame.Current.worldScreen!!, text, "Break promise", action = foundAction).open(force = true) + ConfirmPopup(GUI.getWorldScreen(), text, "Break promise", action = foundAction).open(force = true) } } ) @@ -360,9 +362,7 @@ object UnitActions { private fun addBuildingImprovementsAction( unit: MapUnit, actionList: ArrayList, - tile: Tile, - worldScreen: WorldScreen - ) { + tile: Tile) { if (!unit.cache.hasUniqueToBuildImprovements) return if (unit.isEmbarked()) return @@ -376,7 +376,10 @@ object UnitActions { actionList += UnitAction(UnitActionType.ConstructImprovement, isCurrentAction = unit.currentTile.hasImprovementInProgress(), action = { - worldScreen.game.pushScreen(ImprovementPickerScreen(tile, unit) { if (UncivGame.Current.settings.autoUnitCycle) worldScreen.switchToNextUnit() }) + GUI.pushScreen(ImprovementPickerScreen(tile, unit) { + if (GUI.getSettings().autoUnitCycle) + GUI.getWorldScreen().switchToNextUnit() + }) }.takeIf { couldConstruct } ) } @@ -628,7 +631,7 @@ object UnitActions { unit.destroy() // City states don't get GPs else unit.gift(recipient) - UncivGame.Current.worldScreen!!.shouldUpdate = true + GUI.setUpdateWorldOnNextRender() } return UnitAction(UnitActionType.GiftUnit, action = giftAction) @@ -645,23 +648,23 @@ object UnitActions { } } - private fun addWaitAction(unit: MapUnit, actionList: ArrayList, worldScreen: WorldScreen) { + private fun addWaitAction(unit: MapUnit, actionList: ArrayList) { actionList += UnitAction( type = UnitActionType.Wait, action = { unit.due = false - worldScreen.switchToNextUnit() + GUI.getWorldScreen().switchToNextUnit() } ) } - private fun addToggleActionsAction(unit: MapUnit, actionList: ArrayList, unitTable: UnitTable) { + private fun addToggleActionsAction(unit: MapUnit, actionList: ArrayList) { actionList += UnitAction( type = if (unit.showAdditionalActions) UnitActionType.HideAdditionalActions else UnitActionType.ShowAdditionalActions, action = { unit.showAdditionalActions = !unit.showAdditionalActions - unitTable.update() + GUI.getWorldScreen().bottomUnitTable.update() } ) } diff --git a/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActionsPillage.kt b/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActionsPillage.kt index cf510201c6..42e8bb6e9c 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActionsPillage.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActionsPillage.kt @@ -1,5 +1,6 @@ package com.unciv.ui.screens.worldscreen.unit.actions +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.civilization.NotificationCategory import com.unciv.logic.civilization.NotificationIcon @@ -17,7 +18,7 @@ import kotlin.random.Random object UnitActionsPillage { - fun addPillageAction(unit: MapUnit, actionList: ArrayList, worldScreen: WorldScreen) { + fun addPillageAction(unit: MapUnit, actionList: ArrayList) { val pillageAction = getPillageAction(unit) ?: return if (pillageAction.action == null) @@ -27,16 +28,16 @@ object UnitActionsPillage { action = null) else actionList += UnitAction(type = UnitActionType.Pillage, title = "${UnitActionType.Pillage} [${unit.currentTile.getImprovementToPillageName()!!}]") { - if (!worldScreen.hasOpenPopups()) { + if (!GUI.getWorldScreen().hasOpenPopups()) { val pillageText = "Are you sure you want to pillage this [${unit.currentTile.getImprovementToPillageName()!!}]?" ConfirmPopup( - UncivGame.Current.worldScreen!!, + GUI.getWorldScreen(), pillageText, "Pillage", true ) { (pillageAction.action)() - worldScreen.shouldUpdate = true + GUI.setUpdateWorldOnNextRender() }.open() } } diff --git a/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActionsTable.kt b/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActionsTable.kt index 3d7a3849a5..04105363fb 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActionsTable.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActionsTable.kt @@ -3,6 +3,7 @@ package com.unciv.ui.screens.worldscreen.unit.actions import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.ui.Button import com.badlogic.gdx.scenes.scene2d.ui.Table +import com.unciv.GUI import com.unciv.UncivGame import com.unciv.logic.map.mapunit.MapUnit import com.unciv.models.UnitAction @@ -21,7 +22,7 @@ class UnitActionsTable(val worldScreen: WorldScreen) : Table() { clear() if (unit == null) return if (!worldScreen.canChangeState) return // No actions when it's not your turn or spectator! - for (button in UnitActions.getUnitActions(unit, worldScreen) + for (button in UnitActions.getUnitActions(unit) .map { getUnitActionButton(unit, it) }) add(button).left().padBottom(2f).row() pack() @@ -46,7 +47,7 @@ class UnitActionsTable(val worldScreen: WorldScreen) : Table() { } else { actionButton.onActivation(unitAction.uncivSound) { unitAction.action.invoke() - UncivGame.Current.worldScreen!!.shouldUpdate = true + GUI.setUpdateWorldOnNextRender() // We keep the unit action/selection overlay from the previous unit open even when already selecting another unit // so you need less clicks/touches to do things, but once we do an action with the new unit, we want to close this // overlay, since the user definitely wants to interact with the new unit.