Added enable/disable functions to textbuttons, can now stop moving and automated units even when out of movement

This commit is contained in:
Yair Morgenstern
2018-03-13 22:07:06 +02:00
parent 53e332e9e8
commit eadf7b01db
9 changed files with 78 additions and 47 deletions

View File

@ -32,20 +32,22 @@ class BuildingsTable(private val cityScreen: CityScreen) : Table() {
}
}
if (!wonders.isEmpty()) {
val label = Label("Wonders", skin)
fun createLabel(text:String): Label {
val label = Label(text, skin)
label.setFontScale(1.5f)
label.color = Color.GREEN
add(label).pad(5f).row()
return label
}
if (!wonders.isEmpty()) {
add(createLabel("Wonders")).pad(5f).row()
for (building in wonders)
add(Label(building.name, skin)).pad(5f).row()
}
if (!specialistBuildings.isEmpty()) {
val label = Label("Specialist Buildings", skin)
label.setFontScale(1.5f)
label.color = Color.GREEN
add(label).pad(5f).row()
add(createLabel("Specialist Buildings")).pad(5f).row()
for (building in specialistBuildings) {
add(Label(building.name, skin)).pad(5f)
val specialists = Table()
@ -66,10 +68,7 @@ class BuildingsTable(private val cityScreen: CityScreen) : Table() {
}
if (!others.isEmpty()) {
val label = Label("Buildings", skin)
label.setFontScale(1.5f)
label.color = Color.GREEN
add(label).pad(5f).row()
add(createLabel("Buildings")).pad(5f).row()
for (building in others)
add(Label(building.name, skin)).pad(5f).row()
}

View File

@ -3,7 +3,6 @@ package com.unciv.ui.cityscreen
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Group
import com.badlogic.gdx.scenes.scene2d.InputEvent
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.*
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener
import com.badlogic.gdx.utils.Align
@ -14,6 +13,7 @@ import com.unciv.ui.tilegroups.TileGroup
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.HexMath
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.disable
import java.util.*
class CityScreen(internal val city: CityInfo) : CameraStageBaseScreen() {
@ -235,8 +235,7 @@ class CityScreen(internal val city: CityInfo) : CameraStageBaseScreen() {
update()
}
if (buildingGoldCost > city.civInfo.gold) {
buildingBuyButton.color = Color.GRAY
buildingBuyButton.touchable = Touchable.disabled
buildingBuyButton.disable()
}
buildingBuyButton.label.setFontScale(buttonScale)
cityStatsTable.add(buildingBuyButton).colspan(2).pad(10f)

View File

@ -6,6 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.*
import com.badlogic.gdx.utils.Align
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.disable
open class PickerScreen : CameraStageBaseScreen() {
@ -36,8 +37,7 @@ open class PickerScreen : CameraStageBaseScreen() {
buttonTable.add(rightSideButton).width(stage.width / 4)
buttonTable.height = stage.height * (1 - screenSplit)
buttonTable.align(Align.center)
rightSideButton.color = Color.GRAY
rightSideButton.touchable = Touchable.disabled
rightSideButton.disable()
topTable = Table()
val scrollPane = ScrollPane(topTable)

View File

@ -1,7 +1,6 @@
package com.unciv.ui.pickerscreens
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.Button
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
@ -12,6 +11,8 @@ import com.unciv.models.gamebasics.StringUtils
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.disable
import com.unciv.ui.utils.enable
class PolicyPickerScreen(internal val civInfo: CivilizationInfo) : PickerScreen() {
@ -25,8 +26,7 @@ class PolicyPickerScreen(internal val civInfo: CivilizationInfo) : PickerScreen(
if (policies.freePolicies > 0) {
rightSideButton.setText("Adopt free policy")
closeButton.color = Color.GRAY
closeButton.touchable = Touchable.disabled
closeButton.disable()
}
rightSideButton.addClickListener {
@ -79,11 +79,9 @@ class PolicyPickerScreen(internal val civInfo: CivilizationInfo) : PickerScreen(
if (civInfo.policies.isAdopted(policy.name)
|| !civInfo.policies.getAdoptedPolicies().containsAll(policy.requires!!)
|| !civInfo.policies.canAdoptPolicy()) {
rightSideButton.touchable = Touchable.disabled
rightSideButton.color = Color.GRAY
rightSideButton.disable()
} else {
rightSideButton.color = Color.WHITE
rightSideButton.touchable = Touchable.enabled
rightSideButton.enable()
}
pickedPolicy = policy
var policyText = policy.name + "\r\n" + policy.description + "\r\n"

View File

@ -1,7 +1,6 @@
package com.unciv.ui.pickerscreens
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.civilization.TechManager
@ -9,6 +8,7 @@ import com.unciv.models.gamebasics.GameBasics
import com.unciv.models.gamebasics.Technology
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.disable
import java.util.*
class TechPickerScreen(internal val civInfo: CivilizationInfo) : PickerScreen() {
@ -112,8 +112,7 @@ class TechPickerScreen(internal val civInfo: CivilizationInfo) : PickerScreen()
if (civTech.isResearched(tech.name)) {
rightSideButton.setText("Research")
rightSideButton.touchable = Touchable.disabled
rightSideButton.color = Color.GRAY
rightSideButton.disable()
setButtonsInfo()
return
}
@ -147,8 +146,7 @@ class TechPickerScreen(internal val civInfo: CivilizationInfo) : PickerScreen()
pick("Pick " + selectedTech!!.name + "\r\n as free tech!")
} else {
rightSideButton.setText("Pick a free tech")
rightSideButton.touchable = Touchable.disabled
rightSideButton.color = Color.GRAY
rightSideButton.disable()
}
}

View File

@ -124,9 +124,10 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
// Here, we want to have the roads start HALFWAY THERE and extend towards the tiles, so we give them a position of 0.8*25.
image.moveBy(-relativeWorldPosition.x * 0.8f * 25f, -relativeWorldPosition.y * 0.8f * 25f)
image.setSize(10f, 2f)
addActor(image)
image.setOrigin(0f, 1f) // This is so that the rotation is calculated from the middle of the road and not the edge
image.rotation = (180 / Math.PI * Math.atan2(relativeWorldPosition.y.toDouble(), relativeWorldPosition.x.toDouble())).toFloat()
addActor(image)
}
if (tileInfo.roadStatus === RoadStatus.Railroad && neighbor.roadStatus === RoadStatus.Railroad)
@ -136,6 +137,31 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
}
}
if(tileInfo.owner!=null){
for (neighbor in tileInfo.neighbors.filter { it.owner!=tileInfo.owner }){
val image = ImageGetter.getImage(ImageGetter.WhiteDot)
// roadImages[neighbor.position.toString()] = image
val relativeHexPosition = tileInfo.position.cpy().sub(neighbor.position)
val relativeWorldPosition = HexMath.Hex2WorldCoords(relativeHexPosition)
// This is some crazy voodoo magic so I'll explain.
image.setSize(20f, 2f)
image.moveBy(this.width/2-image.width/2,
this.height/2-image.height/2)
// in addTiles, we set the position of groups by relative world position *0.8*groupSize, filter groupSize = 50
// Here, we want to have the roads start HALFWAY THERE and extend towards the tiles, so we give them a position of 0.8*25.
image.moveBy(-relativeWorldPosition.x * 0.8f * 25f, -relativeWorldPosition.y * 0.8f * 25f)
image.setColor(Color.RED)
image.setOrigin(image.width/2, image.height/2) // This is so that the rotation is calculated from the middle of the road and not the edge
image.rotation = (90 + 180 / Math.PI * Math.atan2(relativeWorldPosition.y.toDouble(), relativeWorldPosition.x.toDouble())).toFloat()
addActor(image)
}
}
}
}

View File

@ -7,6 +7,7 @@ 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.Stage
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.Label
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.badlogic.gdx.scenes.scene2d.ui.Table
@ -93,4 +94,14 @@ open class CameraStageBaseScreen : Screen {
internal var batch: Batch = SpriteBatch()
}
}
fun TextButton.disable(){
touchable= Touchable.disabled
color= Color.GRAY
}
fun TextButton.enable() {
color = Color.WHITE
touchable = Touchable.enabled
}

View File

@ -1,11 +1,11 @@
package com.unciv.ui.worldscreen.unit
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.unciv.logic.map.TileInfo
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.disable
import com.unciv.ui.utils.enable
import com.unciv.ui.worldscreen.WorldScreen
class IdleUnitButton internal constructor(internal val worldScreen: WorldScreen) : TextButton("Select next idle unit", CameraStageBaseScreen.skin) {
@ -27,13 +27,8 @@ class IdleUnitButton internal constructor(internal val worldScreen: WorldScreen)
}
internal fun update() {
if (worldScreen.civInfo.gameInfo.tileMap.values.any { it.hasIdleUnit() }) {
color = Color.WHITE
touchable = Touchable.enabled
} else {
color = Color.GRAY
touchable = Touchable.disabled
}
if (worldScreen.civInfo.gameInfo.tileMap.values.any { it.hasIdleUnit() }) enable()
else disable()
}
}

View File

@ -1,7 +1,6 @@
package com.unciv.ui.worldscreen.unit
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.unciv.logic.map.MapUnit
import com.unciv.logic.map.TileInfo
@ -12,6 +11,8 @@ import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.pickerscreens.ImprovementPickerScreen
import com.unciv.ui.pickerscreens.TechPickerScreen
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.disable
import com.unciv.ui.utils.enable
import java.util.*
class UnitActions {
@ -32,7 +33,8 @@ class UnitActions {
val actionList = ArrayList<TextButton>()
if (unitTable.currentlyExecutingAction != "moveTo"){
if (unitTable.currentlyExecutingAction != "moveTo"
&& (unit.action==null || !unit.action!!.startsWith("moveTo") )){
actionList += getUnitActionButton(unit, "Move unit", true, {
unitTable.currentlyExecutingAction = "moveTo"
// Set all tiles transparent except those in unit range
@ -50,10 +52,13 @@ class UnitActions {
}
else {
actionList += getUnitActionButton(unit, "Stop movement", true, {
val stopUnitAction =getUnitActionButton(unit, "Stop movement", true, {
unitTable.currentlyExecutingAction = null
unit.action=null
tileMapHolder.updateTiles()
})
stopUnitAction.enable() // Stopping automation is always enabled;
actionList += stopUnitAction
}
if (unit.name == "Settler") {
@ -78,8 +83,10 @@ class UnitActions {
{ worldScreen.game.screen = ImprovementPickerScreen(tile) })
if("automation" == tile.unit!!.action){
actionList += getUnitActionButton(unit,"Stop automation",true,
val automationAction = getUnitActionButton(unit,"Stop automation",true,
{tile.unit!!.action = null})
automationAction.enable() // Stopping automation is always enabled;
actionList += automationAction
}
else {
actionList += getUnitActionButton(unit, "Automate", true,
@ -144,10 +151,8 @@ class UnitActions {
private fun getUnitActionButton(unit: MapUnit, actionText: String, canAct: Boolean, action: () -> Unit): TextButton {
val actionButton = TextButton(actionText, CameraStageBaseScreen.skin)
actionButton.addClickListener({ action(); UnCivGame.Current.worldScreen!!.update() })
if (unit.currentMovement == 0f || !canAct) {
actionButton.color = Color.GRAY
actionButton.touchable = Touchable.disabled
}
if (unit.currentMovement == 0f || !canAct) actionButton.disable()
return actionButton
}
}
}