mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-30 22:58:50 +07:00
Better multiplayer game screen - all 'game settings' are accessible immediately without a subscreen!
This commit is contained in:
@ -1,144 +0,0 @@
|
||||
package com.unciv.ui.screens.multiplayerscreens
|
||||
|
||||
import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle
|
||||
import com.unciv.Constants
|
||||
import com.unciv.logic.multiplayer.OnlineMultiplayerGame
|
||||
import com.unciv.logic.multiplayer.storage.MultiplayerAuthException
|
||||
import com.unciv.models.translations.tr
|
||||
import com.unciv.ui.screens.pickerscreens.PickerScreen
|
||||
import com.unciv.ui.popups.ConfirmPopup
|
||||
import com.unciv.ui.popups.Popup
|
||||
import com.unciv.ui.popups.ToastPopup
|
||||
import com.unciv.ui.screens.savescreens.LoadGameScreen
|
||||
import com.unciv.ui.components.UncivTextField
|
||||
import com.unciv.ui.components.extensions.disable
|
||||
import com.unciv.ui.components.extensions.enable
|
||||
import com.unciv.ui.components.input.onClick
|
||||
import com.unciv.ui.components.extensions.toLabel
|
||||
import com.unciv.ui.components.extensions.toTextButton
|
||||
import com.unciv.ui.popups.AuthPopup
|
||||
import com.unciv.utils.Log
|
||||
import com.unciv.utils.Concurrency
|
||||
import com.unciv.utils.launchOnGLThread
|
||||
|
||||
/** Subscreen of MultiplayerScreen to edit and delete saves
|
||||
* backScreen is used for getting back to the MultiplayerScreen so it doesn't have to be created over and over again */
|
||||
class EditMultiplayerGameInfoScreen(val multiplayerGame: OnlineMultiplayerGame) : PickerScreen() {
|
||||
init {
|
||||
val textField = UncivTextField.create("Game name", multiplayerGame.name)
|
||||
|
||||
topTable.add("Rename".toLabel()).row()
|
||||
topTable.add(textField).pad(10f).padBottom(30f).width(stage.width / 2).row()
|
||||
|
||||
val negativeButtonStyle = skin.get("negative", TextButtonStyle::class.java)
|
||||
val deleteButton = "Delete save".toTextButton(negativeButtonStyle)
|
||||
deleteButton.onClick {
|
||||
val askPopup = ConfirmPopup(
|
||||
this,
|
||||
"Are you sure you want to delete this save?",
|
||||
"Delete save",
|
||||
) {
|
||||
try {
|
||||
game.onlineMultiplayer.deleteGame(multiplayerGame)
|
||||
(game.popScreen() as? MultiplayerScreen)?.onGameDeleted(multiplayerGame.name)
|
||||
} catch (ex: Exception) {
|
||||
Log.error("Could not delete game!", ex)
|
||||
ToastPopup("Could not delete game!", this)
|
||||
}
|
||||
}
|
||||
askPopup.open()
|
||||
}
|
||||
|
||||
val giveUpButton = "Resign".toTextButton(negativeButtonStyle)
|
||||
giveUpButton.onClick {
|
||||
val askPopup = ConfirmPopup(
|
||||
this,
|
||||
"Are you sure you want to resign?",
|
||||
"Resign",
|
||||
) {
|
||||
resign(multiplayerGame)
|
||||
}
|
||||
askPopup.open()
|
||||
}
|
||||
|
||||
topTable.add(deleteButton).pad(10f).row()
|
||||
topTable.add(giveUpButton)
|
||||
|
||||
//CloseButton Setup
|
||||
closeButton.setText("Back".tr())
|
||||
closeButton.onClick {
|
||||
game.popScreen()
|
||||
}
|
||||
|
||||
//RightSideButton Setup
|
||||
rightSideButton.setText("Save game".tr())
|
||||
rightSideButton.enable()
|
||||
rightSideButton.onClick {
|
||||
rightSideButton.setText("Saving...".tr())
|
||||
val newName = textField.text.trim()
|
||||
game.onlineMultiplayer.changeGameName(multiplayerGame, newName) {
|
||||
val popup = Popup(this)
|
||||
popup.addGoodSizedLabel("Could not save game!")
|
||||
popup.addCloseButton()
|
||||
popup.open()
|
||||
}
|
||||
val newScreen = game.popScreen()
|
||||
if (newScreen is MultiplayerScreen) {
|
||||
newScreen.selectGame(newName)
|
||||
}
|
||||
}
|
||||
|
||||
if (multiplayerGame.preview == null) {
|
||||
textField.isDisabled = true
|
||||
textField.color = Color.GRAY
|
||||
rightSideButton.disable()
|
||||
giveUpButton.disable()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to decrease indentation
|
||||
* Turns the current playerCiv into an AI civ and uploads the game afterwards.
|
||||
*/
|
||||
private fun resign(multiplayerGame: OnlineMultiplayerGame) {
|
||||
//Create a popup
|
||||
val popup = Popup(this)
|
||||
popup.addGoodSizedLabel(Constants.working).row()
|
||||
popup.open()
|
||||
|
||||
Concurrency.runOnNonDaemonThreadPool("Resign") {
|
||||
try {
|
||||
val resignSuccess = game.onlineMultiplayer.resign(multiplayerGame)
|
||||
if (resignSuccess) {
|
||||
launchOnGLThread {
|
||||
popup.close()
|
||||
game.popScreen()
|
||||
}
|
||||
} else {
|
||||
launchOnGLThread {
|
||||
popup.reuseWith("You can only resign if it's your turn", true)
|
||||
}
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
val (message) = LoadGameScreen.getLoadExceptionMessage(ex)
|
||||
|
||||
when (ex) {
|
||||
is MultiplayerAuthException -> {
|
||||
launchOnGLThread {
|
||||
AuthPopup(this@EditMultiplayerGameInfoScreen) { success ->
|
||||
if (success) resign(multiplayerGame)
|
||||
}.open(true)
|
||||
}
|
||||
return@runOnNonDaemonThreadPool
|
||||
}
|
||||
}
|
||||
|
||||
launchOnGLThread {
|
||||
popup.reuseWith(message, true)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -3,8 +3,11 @@ package com.unciv.ui.screens.multiplayerscreens
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Table
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
|
||||
import com.unciv.Constants
|
||||
import com.unciv.logic.multiplayer.OnlineMultiplayerGame
|
||||
import com.unciv.logic.multiplayer.storage.MultiplayerAuthException
|
||||
import com.unciv.models.translations.tr
|
||||
import com.unciv.ui.components.UncivTextField
|
||||
import com.unciv.ui.screens.pickerscreens.PickerScreen
|
||||
import com.unciv.ui.popups.Popup
|
||||
import com.unciv.ui.popups.ToastPopup
|
||||
@ -12,14 +15,24 @@ import com.unciv.ui.components.extensions.disable
|
||||
import com.unciv.ui.components.extensions.enable
|
||||
import com.unciv.ui.components.input.onClick
|
||||
import com.unciv.ui.components.extensions.toTextButton
|
||||
import com.unciv.ui.popups.AuthPopup
|
||||
import com.unciv.ui.popups.ConfirmPopup
|
||||
import com.unciv.ui.screens.savescreens.LoadGameScreen
|
||||
import com.unciv.utils.Concurrency
|
||||
import com.unciv.utils.Log
|
||||
import com.unciv.utils.launchOnGLThread
|
||||
import com.unciv.ui.components.widgets.AutoScrollPane as ScrollPane
|
||||
|
||||
class MultiplayerScreen : PickerScreen() {
|
||||
private var selectedGame: OnlineMultiplayerGame? = null
|
||||
|
||||
private val editButton = createEditButton()
|
||||
private val addGameButton = createAddGameButton()
|
||||
private val copyGameIdButton = createCopyGameIdButton()
|
||||
private val resignButton = createResignButton()
|
||||
private val deleteButton = createDeleteButton()
|
||||
private val renameButton = createRenameButton()
|
||||
private val gameSpecificButtons = listOf(copyGameIdButton, resignButton, deleteButton, renameButton)
|
||||
|
||||
private val addGameButton = createAddGameButton()
|
||||
private val copyUserIdButton = createCopyUserIdButton()
|
||||
private val friendsListButton = createFriendsListButton()
|
||||
private val refreshButton = createRefreshButton()
|
||||
@ -58,13 +71,22 @@ class MultiplayerScreen : PickerScreen() {
|
||||
val table = Table()
|
||||
table.defaults().uniformX()
|
||||
table.defaults().fillX()
|
||||
table.defaults().pad(10.0f)
|
||||
table.add(copyUserIdButton).padBottom(30f).row()
|
||||
table.add(copyGameIdButton).row()
|
||||
table.add(editButton).row()
|
||||
table.add(addGameButton).padBottom(30f).row()
|
||||
table.add(friendsListButton).padBottom(30f).row()
|
||||
table.add(refreshButton).row()
|
||||
table.defaults().pad(10f)
|
||||
|
||||
val gameSpecificActions = Table().apply { defaults().pad(10f) }
|
||||
gameSpecificActions.add(copyGameIdButton).row()
|
||||
gameSpecificActions.add(renameButton).row()
|
||||
gameSpecificActions.add(resignButton).row()
|
||||
gameSpecificActions.add(deleteButton).row()
|
||||
table.add(gameSpecificActions)
|
||||
|
||||
val generalActions = Table().apply { defaults().pad(10f) }
|
||||
generalActions.add(copyUserIdButton).row()
|
||||
generalActions.add(addGameButton).row()
|
||||
generalActions.add(friendsListButton).row()
|
||||
generalActions.add(refreshButton).row()
|
||||
table.add(generalActions)
|
||||
|
||||
return table
|
||||
}
|
||||
|
||||
@ -82,10 +104,104 @@ class MultiplayerScreen : PickerScreen() {
|
||||
return btn
|
||||
}
|
||||
|
||||
fun createEditButton(): TextButton {
|
||||
val btn = "Game settings".toTextButton().apply { disable() }
|
||||
fun createResignButton(): TextButton {
|
||||
val negativeButtonStyle = skin.get("negative", TextButton.TextButtonStyle::class.java)
|
||||
val resignButton = "Resign".toTextButton(negativeButtonStyle).apply { disable() }
|
||||
resignButton.onClick {
|
||||
val askPopup = ConfirmPopup(
|
||||
this,
|
||||
"Are you sure you want to resign?",
|
||||
"Resign",
|
||||
) {
|
||||
resign(selectedGame!!)
|
||||
}
|
||||
askPopup.open()
|
||||
}
|
||||
return resignButton
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to decrease indentation
|
||||
* Turns the current playerCiv into an AI civ and uploads the game afterwards.
|
||||
*/
|
||||
private fun resign(multiplayerGame: OnlineMultiplayerGame) {
|
||||
//Create a popup
|
||||
val popup = Popup(this)
|
||||
popup.addGoodSizedLabel(Constants.working).row()
|
||||
popup.open()
|
||||
|
||||
Concurrency.runOnNonDaemonThreadPool("Resign") {
|
||||
try {
|
||||
val resignSuccess = game.onlineMultiplayer.resign(multiplayerGame)
|
||||
|
||||
launchOnGLThread {
|
||||
if (resignSuccess) {
|
||||
popup.close()
|
||||
game.popScreen()
|
||||
} else {
|
||||
popup.reuseWith("You can only resign if it's your turn", true)
|
||||
}
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
val (message) = LoadGameScreen.getLoadExceptionMessage(ex)
|
||||
|
||||
if (ex is MultiplayerAuthException) {
|
||||
launchOnGLThread {
|
||||
AuthPopup(this@MultiplayerScreen) { success ->
|
||||
if (success) resign(multiplayerGame)
|
||||
}.open(true)
|
||||
}
|
||||
return@runOnNonDaemonThreadPool
|
||||
}
|
||||
|
||||
launchOnGLThread {
|
||||
popup.reuseWith(message, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun createDeleteButton(): TextButton {
|
||||
val negativeButtonStyle = skin.get("negative", TextButton.TextButtonStyle::class.java)
|
||||
val deleteButton = "Delete save".toTextButton(negativeButtonStyle).apply { disable() }
|
||||
deleteButton.onClick {
|
||||
val askPopup = ConfirmPopup(
|
||||
this,
|
||||
"Are you sure you want to delete this save?",
|
||||
"Delete save",
|
||||
) {
|
||||
try {
|
||||
game.onlineMultiplayer.deleteGame(selectedGame!!)
|
||||
onGameDeleted(selectedGame!!.name)
|
||||
} catch (ex: Exception) {
|
||||
Log.error("Could not delete game!", ex)
|
||||
ToastPopup("Could not delete game!", this)
|
||||
}
|
||||
}
|
||||
askPopup.open()
|
||||
}
|
||||
return deleteButton
|
||||
}
|
||||
|
||||
fun createRenameButton(): TextButton {
|
||||
val btn = "Rename".toTextButton().apply { disable() }
|
||||
btn.onClick {
|
||||
game.pushScreen(EditMultiplayerGameInfoScreen(selectedGame!!))
|
||||
Popup(this).apply {
|
||||
val textField = UncivTextField.create("Game name", selectedGame!!.name)
|
||||
add(textField).width(stageToShowOn.width / 2).row()
|
||||
val saveButton = "Save".toTextButton()
|
||||
saveButton.onClick {
|
||||
val newName = textField.text.trim()
|
||||
game.onlineMultiplayer.changeGameName(selectedGame!!, newName) {
|
||||
if (it != null) reuseWith("Could not save game!", true)
|
||||
}
|
||||
gameList.update()
|
||||
selectGame(newName)
|
||||
close()
|
||||
}
|
||||
add(saveButton)
|
||||
open()
|
||||
}
|
||||
}
|
||||
return btn
|
||||
}
|
||||
@ -157,10 +273,9 @@ class MultiplayerScreen : PickerScreen() {
|
||||
|
||||
private fun unselectGame() {
|
||||
selectedGame = null
|
||||
|
||||
editButton.disable()
|
||||
copyGameIdButton.disable()
|
||||
rightSideButton.disable()
|
||||
for (button in gameSpecificButtons)
|
||||
button.disable()
|
||||
descriptionLabel.setText("")
|
||||
}
|
||||
|
||||
@ -174,12 +289,14 @@ class MultiplayerScreen : PickerScreen() {
|
||||
|
||||
selectedGame = multiplayerGame
|
||||
|
||||
for (button in gameSpecificButtons)
|
||||
button.enable()
|
||||
if (multiplayerGame.preview != null) {
|
||||
copyGameIdButton.enable()
|
||||
} else {
|
||||
copyGameIdButton.disable()
|
||||
}
|
||||
editButton.enable()
|
||||
|
||||
rightSideButton.enable()
|
||||
|
||||
descriptionLabel.setText(MultiplayerHelpers.buildDescriptionText(multiplayerGame))
|
||||
|
Reference in New Issue
Block a user