Resolved #2937 - can add large increments of gold in trades

This commit is contained in:
Yair Morgenstern 2020-08-24 21:29:00 +03:00
parent 6c3faec6ac
commit b85506a7d5
3 changed files with 55 additions and 17 deletions

View file

@ -81,7 +81,7 @@ class DiplomacyScreen(val viewingCiv:CivilizationInfo):CameraStageBaseScreen() {
fun setTrade(civ: CivilizationInfo): TradeTable {
rightSideTable.clear()
val tradeTable =TradeTable(civ, stage)
val tradeTable =TradeTable(civ, this)
rightSideTable.add(tradeTable)
return tradeTable
}

View file

@ -1,17 +1,16 @@
package com.unciv.ui.trade
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.trade.TradeLogic
import com.unciv.logic.trade.TradeOffer
import com.unciv.logic.trade.TradeOffersList
import com.unciv.logic.trade.TradeType
import com.unciv.models.translations.tr
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.addSeparator
import com.unciv.ui.utils.*
/** This is the class that holds the 4 columns of the offers (ours/theirs/ offered/available) in trade */
class OfferColumnsTable(val tradeLogic: TradeLogic, stage: Stage, val onChange: ()->Unit): Table(CameraStageBaseScreen.skin) {
class OfferColumnsTable(val tradeLogic: TradeLogic, val screen: DiplomacyScreen, val onChange: ()->Unit): Table(CameraStageBaseScreen.skin) {
fun addOffer(offer:TradeOffer, offerList:TradeOffersList, correspondingOfferList:TradeOffersList){
offerList.add(offer.copy())
@ -19,28 +18,35 @@ class OfferColumnsTable(val tradeLogic: TradeLogic, stage: Stage, val onChange:
onChange()
}
// todo - add logic that treaties are added and removed from both sides of the table
val ourAvailableOffersTable = OffersListScroll { addOffer(it,tradeLogic.currentTrade.ourOffers, tradeLogic.currentTrade.theirOffers) }
val ourOffersTable = OffersListScroll { addOffer(it.copy(amount=-it.amount),tradeLogic.currentTrade.ourOffers, tradeLogic.currentTrade.theirOffers) }
val theirOffersTable = OffersListScroll { addOffer(it.copy(amount=-it.amount),tradeLogic.currentTrade.theirOffers, tradeLogic.currentTrade.ourOffers) }
val theirAvailableOffersTable = OffersListScroll { addOffer(it,tradeLogic.currentTrade.theirOffers, tradeLogic.currentTrade.ourOffers) }
val ourAvailableOffersTable = OffersListScroll {
if(it.type==TradeType.Gold) openGoldSelectionPopup(it, tradeLogic.currentTrade.ourOffers, tradeLogic.ourCivilization)
else addOffer(it,tradeLogic.currentTrade.ourOffers, tradeLogic.currentTrade.theirOffers) }
val ourOffersTable = OffersListScroll {
if (it.type==TradeType.Gold) openGoldSelectionPopup(it, tradeLogic.currentTrade.ourOffers, tradeLogic.ourCivilization)
else addOffer(it.copy(amount=-it.amount),tradeLogic.currentTrade.ourOffers, tradeLogic.currentTrade.theirOffers) }
val theirOffersTable = OffersListScroll {
if (it.type==TradeType.Gold) openGoldSelectionPopup(it, tradeLogic.currentTrade.theirOffers, tradeLogic.otherCivilization)
else addOffer(it.copy(amount=-it.amount),tradeLogic.currentTrade.theirOffers, tradeLogic.currentTrade.ourOffers) }
val theirAvailableOffersTable = OffersListScroll {
if (it.type==TradeType.Gold) openGoldSelectionPopup(it, tradeLogic.currentTrade.theirOffers, tradeLogic.otherCivilization)
else addOffer(it,tradeLogic.currentTrade.theirOffers, tradeLogic.currentTrade.ourOffers) }
init {
defaults().pad(5f)
val columnWidth = stage.width / 3
val columnWidth = screen.stage.width / 3
add("Our items".tr())
add("[${tradeLogic.otherCivilization.civName}]'s items".tr()).row()
add(ourAvailableOffersTable).size(columnWidth,stage.height/2)
add(theirAvailableOffersTable).size(columnWidth,stage.height/2).row()
add(ourAvailableOffersTable).size(columnWidth,screen.stage.height/2)
add(theirAvailableOffersTable).size(columnWidth,screen.stage.height/2).row()
addSeparator().height(2f)
add("Our trade offer".tr())
add("[${tradeLogic.otherCivilization.civName}]'s trade offer".tr()).row()
add(ourOffersTable).size(columnWidth,stage.height/5)
add(theirOffersTable).size(columnWidth,stage.height/5)
add(ourOffersTable).size(columnWidth,screen.stage.height/5)
add(theirOffersTable).size(columnWidth,screen.stage.height/5)
pack()
update()
}
@ -53,4 +59,37 @@ class OfferColumnsTable(val tradeLogic: TradeLogic, stage: Stage, val onChange:
theirOffersTable.update(tradeLogic.currentTrade.theirOffers, tradeLogic.ourAvailableOffers)
theirAvailableOffersTable.update(theirFilteredOffers, tradeLogic.ourAvailableOffers)
}
fun openGoldSelectionPopup(offer: TradeOffer, ourOffers: TradeOffersList, offeringCiv:CivilizationInfo) {
val selectionPopup = Popup(screen)
val existingGoldOffer = ourOffers.firstOrNull { it.type == TradeType.Gold }
if (existingGoldOffer != null)
offer.amount = existingGoldOffer.amount
val amountLabel = offer.amount.toLabel()
val minitable = Table().apply { defaults().pad(5f) }
fun incrementAmount(delta: Int) {
offer.amount += delta
if (offer.amount < 0) offer.amount = 0
if (offer.amount > offeringCiv.gold) offer.amount = offeringCiv.gold
amountLabel.setText(offer.amount)
}
minitable.add("-500".toTextButton().onClick { incrementAmount(-500) })
minitable.add("-50".toTextButton().onClick { incrementAmount(-50) })
minitable.add(amountLabel)
minitable.add("+50".toTextButton().onClick { incrementAmount(50) })
minitable.add("+500".toTextButton().onClick { incrementAmount(500) })
selectionPopup.add(minitable).row()
selectionPopup.addCloseButton {
if (existingGoldOffer == null)
ourOffers.add(offer)
else existingGoldOffer.amount = offer.amount
onChange()
}
selectionPopup.open()
}
}

View file

@ -1,6 +1,5 @@
package com.unciv.ui.trade
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.trade.TradeLogic
@ -8,7 +7,7 @@ import com.unciv.logic.trade.TradeRequest
import com.unciv.models.translations.tr
import com.unciv.ui.utils.*
class TradeTable(val otherCivilization: CivilizationInfo, stage: Stage): Table(CameraStageBaseScreen.skin){
class TradeTable(val otherCivilization: CivilizationInfo, stage: DiplomacyScreen): Table(CameraStageBaseScreen.skin){
val currentPlayerCiv = otherCivilization.gameInfo.getCurrentPlayerCivilization()
var tradeLogic = TradeLogic(currentPlayerCiv,otherCivilization)
var offerColumnsTable = OfferColumnsTable(tradeLogic, stage) { onChange() }