From d3d8933bbfc24d360aac66104a958bccf2496745 Mon Sep 17 00:00:00 2001 From: Timo T Date: Mon, 27 Jan 2020 07:32:16 +0100 Subject: [PATCH] Fix popups (#1784) * Fix community popup not opening Fixes a regression in b95844d2f4601f726eedac93df3c12d7394f5793. This commit refactored popups and it was thought that the "screen has popup -> don't show popup" was correct for all popups. That assumption was incorrect, the community popup was not opening anymore as well as the game menu popups (editor and normal game) could not be opened over other popups anymore. This commit fixes that by introducing a queue for popups. When you try to open a popup and one is already open, the popup you tried to open only gets shown when the popup that was already open is closed. This can be manually overridden with a calling the `open` method with a `(force = true)` argument. Also, all popups are now and should be opened and closed only with their `open()` and `close()` methods to ensure this behavior works. * Refactor: Remove all open() methods from popup constructors While it may be a little less to type, it should be up to the caller to decide to open a popup over other popups (via the `force = true` parameter) or not. This is not possible if a popup is opened automatically within its constructor, which is why that is the wrong place to open the popup- --- .../com/unciv/ui/cityscreen/CityInfoTable.kt | 2 +- .../unciv/ui/cityscreen/ConstructionsTable.kt | 3 +- .../com/unciv/ui/mapeditor/LoadMapScreen.kt | 5 +- .../unciv/ui/mapeditor/MapDownloadPopup.kt | 4 +- .../unciv/ui/mapeditor/MapEditorMenuPopup.kt | 4 +- .../com/unciv/ui/mapeditor/MapEditorScreen.kt | 5 +- .../src/com/unciv/ui/trade/DiplomacyScreen.kt | 4 +- .../unciv/ui/utils/CameraStageBaseScreen.kt | 20 +------ core/src/com/unciv/ui/utils/Popup.kt | 23 ++++++-- core/src/com/unciv/ui/utils/YesNoPopup.kt | 1 - .../com/unciv/ui/worldscreen/AlertPopup.kt | 1 - .../com/unciv/ui/worldscreen/TradePopup.kt | 18 +++---- .../com/unciv/ui/worldscreen/WorldScreen.kt | 4 +- .../unciv/ui/worldscreen/WorldScreenTopBar.kt | 4 +- .../mainmenu/WorldScreenMenuPopup.kt | 24 ++++----- .../mainmenu/WorldScreenOptionsPopup.kt | 52 ++++++++++--------- .../unciv/ui/worldscreen/unit/UnitActions.kt | 4 +- 17 files changed, 86 insertions(+), 92 deletions(-) diff --git a/core/src/com/unciv/ui/cityscreen/CityInfoTable.kt b/core/src/com/unciv/ui/cityscreen/CityInfoTable.kt index 5cfd63c7..42cd8422 100644 --- a/core/src/com/unciv/ui/cityscreen/CityInfoTable.kt +++ b/core/src/com/unciv/ui/cityscreen/CityInfoTable.kt @@ -95,7 +95,7 @@ class CityInfoTable(private val cityScreen: CityScreen) : Table(CameraStageBaseS cityScreen.city.sellBuilding(building.name) cityScreen.city.cityStats.update() cityScreen.update() - }, cityScreen) + }, cityScreen).open() } if (cityScreen.city.hasSoldBuildingThisTurn || cityScreen.city.isPuppet || !UncivGame.Current.worldScreen.isPlayersTurn) diff --git a/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt b/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt index 49fa99f9..142421b0 100644 --- a/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt +++ b/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt @@ -14,7 +14,6 @@ import com.unciv.models.UncivSound import com.unciv.models.stats.Stat import com.unciv.models.translations.tr import com.unciv.ui.utils.* -import com.unciv.ui.utils.YesNoPopup class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScreen.skin) { /* -2 = Nothing, -1 = current construction, >= 0 queue entry */ @@ -294,7 +293,7 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre } if (!construction.shouldBeDisplayed(cityConstructions)) cityScreen.selectedConstruction = null cityScreen.update() - }, cityScreen) + }, cityScreen).open() } if (constructionGoldCost > city.civInfo.gold) diff --git a/core/src/com/unciv/ui/mapeditor/LoadMapScreen.kt b/core/src/com/unciv/ui/mapeditor/LoadMapScreen.kt index 231c0bb6..6daa7700 100644 --- a/core/src/com/unciv/ui/mapeditor/LoadMapScreen.kt +++ b/core/src/com/unciv/ui/mapeditor/LoadMapScreen.kt @@ -12,7 +12,6 @@ import com.unciv.models.translations.tr import com.unciv.ui.pickerscreens.PickerScreen import com.unciv.ui.saves.Gzip import com.unciv.ui.utils.* -import com.unciv.ui.utils.YesNoPopup class LoadMapScreen(previousMap: TileMap?) : PickerScreen(){ var chosenMap = "" @@ -43,7 +42,7 @@ class LoadMapScreen(previousMap: TileMap?) : PickerScreen(){ val downloadMapButton = TextButton("Download map".tr(), skin) downloadMapButton.onClick { - MapDownloadPopup(this) + MapDownloadPopup(this).open() } rightSideTable.add(downloadMapButton).row() @@ -70,7 +69,7 @@ class LoadMapScreen(previousMap: TileMap?) : PickerScreen(){ YesNoPopup("Are you sure you want to delete this map?", { MapSaver().deleteMap(chosenMap) UncivGame.Current.setScreen(LoadMapScreen(previousMap)) - }, this) + }, this).open() } deleteMapButton.disable() deleteMapButton.color = Color.RED diff --git a/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt b/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt index 7ec9bb47..6469f2a8 100644 --- a/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt +++ b/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt @@ -8,9 +8,9 @@ import com.unciv.UncivGame import com.unciv.logic.MapSaver import com.unciv.ui.saves.Gzip import com.unciv.ui.utils.CameraStageBaseScreen +import com.unciv.ui.utils.Popup import com.unciv.ui.utils.onClick import com.unciv.ui.worldscreen.mainmenu.DropBox -import com.unciv.ui.utils.Popup import kotlin.concurrent.thread class MapDownloadPopup(loadMapScreen: LoadMapScreen): Popup(loadMapScreen) { @@ -39,6 +39,7 @@ class MapDownloadPopup(loadMapScreen: LoadMapScreen): Popup(loadMapScreen) { val couldNotDownloadMapPopup = Popup(screen) couldNotDownloadMapPopup.addGoodSizedLabel("Could not download map!").row() couldNotDownloadMapPopup.addCloseButton() + couldNotDownloadMapPopup.open() } } } @@ -50,6 +51,5 @@ class MapDownloadPopup(loadMapScreen: LoadMapScreen): Popup(loadMapScreen) { addGoodSizedLabel("Could not get list of maps!").row() } addCloseButton() - open() } } diff --git a/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt b/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt index ed6ff130..5c5b6ddb 100644 --- a/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt +++ b/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt @@ -11,9 +11,9 @@ import com.unciv.logic.map.MapType import com.unciv.logic.map.RoadStatus import com.unciv.models.translations.tr import com.unciv.ui.saves.Gzip +import com.unciv.ui.utils.Popup import com.unciv.ui.utils.onClick import com.unciv.ui.worldscreen.mainmenu.DropBox -import com.unciv.ui.utils.Popup import kotlin.concurrent.thread class MapEditorMenuPopup(mapEditorScreen: MapEditorScreen): Popup(mapEditorScreen){ @@ -95,7 +95,5 @@ class MapEditorMenuPopup(mapEditorScreen: MapEditorScreen): Popup(mapEditorScree val closeOptionsButton = TextButton("Close".tr(), skin) closeOptionsButton.onClick { close() } add(closeOptionsButton).row() - - open() } } diff --git a/core/src/com/unciv/ui/mapeditor/MapEditorScreen.kt b/core/src/com/unciv/ui/mapeditor/MapEditorScreen.kt index be09dcb8..d2b969b7 100644 --- a/core/src/com/unciv/ui/mapeditor/MapEditorScreen.kt +++ b/core/src/com/unciv/ui/mapeditor/MapEditorScreen.kt @@ -13,6 +13,7 @@ import com.unciv.models.ruleset.RulesetCache import com.unciv.models.translations.tr import com.unciv.ui.utils.CameraStageBaseScreen import com.unciv.ui.utils.onClick +import com.unciv.ui.utils.popups import com.unciv.ui.utils.setFontSize class MapEditorScreen(): CameraStageBaseScreen() { @@ -76,9 +77,9 @@ class MapEditorScreen(): CameraStageBaseScreen() { val optionsMenuButton = TextButton("Menu".tr(), skin) optionsMenuButton.onClick { - if(stage.actors.any { it is MapEditorMenuPopup }) + if(popups.any { it is MapEditorMenuPopup }) return@onClick // already open - MapEditorMenuPopup(this) + MapEditorMenuPopup(this).open(force = true) } optionsMenuButton.label.setFontSize(24) optionsMenuButton.labelCell.pad(20f) diff --git a/core/src/com/unciv/ui/trade/DiplomacyScreen.kt b/core/src/com/unciv/ui/trade/DiplomacyScreen.kt index b3b36143..55b56922 100644 --- a/core/src/com/unciv/ui/trade/DiplomacyScreen.kt +++ b/core/src/com/unciv/ui/trade/DiplomacyScreen.kt @@ -158,7 +158,7 @@ class DiplomacyScreen(val viewingCiv:CivilizationInfo):CameraStageBaseScreen() { tradeLogic.currentTrade.theirOffers.add(TradeOffer(Constants.peaceTreaty, TradeType.Treaty, 30)) tradeLogic.acceptTrade() updateLeftSideTable() - }, this) + }, this).open() } diplomacyTable.add(peaceButton).row() if(isNotPlayersTurn()) peaceButton.disable() @@ -373,7 +373,7 @@ class DiplomacyScreen(val viewingCiv:CivilizationInfo):CameraStageBaseScreen() { diplomacyManager.declareWar() setRightSideFlavorText(otherCiv, otherCiv.getTranslatedNation().attacked, "Very well.") updateLeftSideTable() - }, this) + }, this).open() } return declareWarButton } diff --git a/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt b/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt index b86a51cb..4dc25344 100644 --- a/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt +++ b/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt @@ -7,21 +7,8 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.GL20 import com.badlogic.gdx.graphics.g2d.Batch import com.badlogic.gdx.graphics.g2d.SpriteBatch -import com.badlogic.gdx.scenes.scene2d.Actor -import com.badlogic.gdx.scenes.scene2d.InputEvent -import com.badlogic.gdx.scenes.scene2d.InputListener -import com.badlogic.gdx.scenes.scene2d.Stage -import com.badlogic.gdx.scenes.scene2d.Touchable -import com.badlogic.gdx.scenes.scene2d.ui.Button -import com.badlogic.gdx.scenes.scene2d.ui.Cell -import com.badlogic.gdx.scenes.scene2d.ui.CheckBox -import com.badlogic.gdx.scenes.scene2d.ui.Image -import com.badlogic.gdx.scenes.scene2d.ui.Label -import com.badlogic.gdx.scenes.scene2d.ui.SelectBox -import com.badlogic.gdx.scenes.scene2d.ui.Skin -import com.badlogic.gdx.scenes.scene2d.ui.Table -import com.badlogic.gdx.scenes.scene2d.ui.TextButton -import com.badlogic.gdx.scenes.scene2d.ui.TextField +import com.badlogic.gdx.scenes.scene2d.* +import com.badlogic.gdx.scenes.scene2d.ui.* import com.badlogic.gdx.scenes.scene2d.utils.ClickListener import com.badlogic.gdx.utils.viewport.ExtendViewport import com.unciv.JsonParser @@ -83,9 +70,6 @@ open class CameraStageBaseScreen : Screen { tutorialController.showTutorial(tutorial) } - fun hasOpenPopups(): Boolean = - stage.actors.any { it is Popup } - companion object { var skin = Skin(Gdx.files.internal("skin/flat-earth-ui.json")) diff --git a/core/src/com/unciv/ui/utils/Popup.kt b/core/src/com/unciv/ui/utils/Popup.kt index 14b82c78..1dbee4b6 100644 --- a/core/src/com/unciv/ui/utils/Popup.kt +++ b/core/src/com/unciv/ui/utils/Popup.kt @@ -18,20 +18,30 @@ open class Popup(val screen: CameraStageBaseScreen): Table(CameraStageBaseScreen this.pad(20f) this.defaults().pad(5f) + + this.isVisible = false } /** - * Displays the Popup on the screen. Will not open the popup if another one is already open. + * Displays the Popup on the screen. If another popup is already open, this one will display after the other has + * closed. Use [force] = true if you want to open this popup above the other one anyway. */ - fun open() { - if (screen.hasOpenPopups()) return; + fun open(force: Boolean = false) { + if (force || !screen.hasOpenPopups()) { + this.isVisible = true + } + + screen.stage.addActor(this) pack() center(screen.stage) - screen.stage.addActor(this) + } open fun close() { remove() + if (screen.popups.isNotEmpty()) { + screen.popups[0].isVisible = true; + } } fun addGoodSizedLabel(text: String, size:Int=18): Cell