From 523dd458ced872348fd8cc0865670f8f3fec3673 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 2 Jul 2022 23:28:33 +0900 Subject: [PATCH] crafting ui nearly complete --- .../terrarum/UIItemInventoryElemSimple.kt | 26 +++++--- .../terrarum/UIItemInventoryElemWide.kt | 37 ++++++++--- .../gameactors/FixtureInventory.kt | 8 ++- .../gameactors/FixtureStorageChest.kt | 2 +- .../modulebasegame/gameactors/UICrafting.kt | 66 +++++++++++++++---- .../modulebasegame/ui/HasInventory.kt | 2 +- ...r.kt => InventoryTransactionNegotiator.kt} | 2 +- .../ui/UIItemInventoryCellBase.kt | 33 +++++++++- .../ui/UIItemInventoryItemGrid.kt | 20 +++++- src/net/torvald/terrarum/ui/UIItem.kt | 50 ++++++++------ .../torvald/terrarum/ui/UIItemTextButton.kt | 5 +- .../terrarum/ui/UIItemTextLineInput.kt | 2 +- 12 files changed, 189 insertions(+), 64 deletions(-) rename src/net/torvald/terrarum/modulebasegame/ui/{InventoryNegotiator.kt => InventoryTransactionNegotiator.kt} (94%) diff --git a/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt b/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt index 446987b20..322b3ef76 100644 --- a/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt +++ b/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt @@ -5,8 +5,10 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.TextureRegion import net.torvald.terrarum.gameitems.GameItem +import net.torvald.terrarum.modulebasegame.ui.InventoryCellColourTheme import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellBase import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes +import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.defaultInventoryCellTheme import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.toItemCountText import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.UICanvas @@ -28,7 +30,8 @@ class UIItemInventoryElemSimple( keyDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Keycode, extra info, self touchDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Button, extra info, self extraInfo: Any? = null, - highlightEquippedItem: Boolean = true // for some UIs that only cares about getting equipped slot number but not highlighting + highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting + var colourTheme: InventoryCellColourTheme = defaultInventoryCellTheme ) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem) { companion object { @@ -47,18 +50,25 @@ class UIItemInventoryElemSimple( } + private var highlightToMainCol = false + private var highlightToSubCol = false + override fun render(batch: SpriteBatch, camera: Camera) { blendNormal(batch) + highlightToMainCol = customHighlightRuleMain?.invoke(this) ?: (equippedSlot != null && highlightEquippedItem) || forceHighlighted + highlightToSubCol = customHighlightRule2?.invoke(this) ?: false + // cell background if (item != null || drawBackOnNull) { batch.color = Toolkit.Theme.COL_CELL_FILL Toolkit.fillArea(batch, posX, posY, width, height) } // cell border - batch.color = if ((equippedSlot != null && highlightEquippedItem) || forceHighlighted) Toolkit.Theme.COL_HIGHLIGHT - else if (mouseUp && item != null) Toolkit.Theme.COL_ACTIVE - else Toolkit.Theme.COL_INVENTORY_CELL_BORDER + batch.color = if (highlightToMainCol) colourTheme.cellHighlightMainCol + else if (highlightToSubCol) colourTheme.cellHighlightSubCol + else if (mouseUp && item != null) colourTheme.cellHighlightMouseUpCol + else colourTheme.cellHighlightNormalCol Toolkit.drawBoxBorder(batch, posX, posY, width, height) @@ -96,10 +106,10 @@ class UIItemInventoryElemSimple( // if mouse is over, text lights up // highlight item count (blocks/walls) if the item is equipped batch.color = item!!.nameColour mul ( - if ((equippedSlot != null && highlightEquippedItem) || forceHighlighted) Toolkit.Theme.COL_HIGHLIGHT - else if (mouseUp && item != null) Toolkit.Theme.COL_ACTIVE - else Color.WHITE - ) + if (highlightToMainCol) colourTheme.textHighlightMainCol + else if (highlightToSubCol) colourTheme.textHighlightSubCol + else if (mouseUp && item != null) colourTheme.textHighlightMouseUpCol + else colourTheme.textHighlightNormalCol) App.fontSmallNumbers.draw(batch, amountString, diff --git a/src/net/torvald/terrarum/UIItemInventoryElemWide.kt b/src/net/torvald/terrarum/UIItemInventoryElemWide.kt index 84ea41e80..01fbc05a7 100644 --- a/src/net/torvald/terrarum/UIItemInventoryElemWide.kt +++ b/src/net/torvald/terrarum/UIItemInventoryElemWide.kt @@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.TextureRegion import net.torvald.terrarum.gameitems.GameItem +import net.torvald.terrarum.modulebasegame.ui.InventoryCellColourTheme import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellBase import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.toItemCountText @@ -31,7 +32,8 @@ class UIItemInventoryElemWide( keyDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Keycode, extra info, self touchDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Button, extra info, self extraInfo: Any? = null, - highlightEquippedItem: Boolean = true // for some UIs that only cares about getting equipped slot number but not highlighting + highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting + var colourTheme: InventoryCellColourTheme = UIItemInventoryCellCommonRes.defaultInventoryCellTheme ) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem) { companion object { @@ -63,20 +65,35 @@ class UIItemInventoryElemWide( } } - private val fwsp = 0x3000.toChar() + private var highlightToMainCol = false + private var highlightToSubCol = false + + var cellHighlightMainCol = Toolkit.Theme.COL_HIGHLIGHT + var cellHighlightSubCol = Toolkit.Theme.COL_LIST_DEFAULT + var cellHighlightMouseUpCol = Toolkit.Theme.COL_ACTIVE + var cellHighlightNormalCol = Toolkit.Theme.COL_INVENTORY_CELL_BORDER + + var textHighlightMainCol = Toolkit.Theme.COL_HIGHLIGHT + var textHighlightSubCol = Color.WHITE + var textHighlightMouseUpCol = Toolkit.Theme.COL_ACTIVE + var textHighlightNormalCol = Color.WHITE override fun render(batch: SpriteBatch, camera: Camera) { blendNormal(batch) + highlightToMainCol = customHighlightRuleMain?.invoke(this) ?: (equippedSlot != null && highlightEquippedItem) || forceHighlighted + highlightToSubCol = customHighlightRule2?.invoke(this) ?: false + // cell background if (item != null || drawBackOnNull) { batch.color = Toolkit.Theme.COL_CELL_FILL Toolkit.fillArea(batch, posX, posY, width, height) } // cell border - batch.color = if ((equippedSlot != null && highlightEquippedItem) || forceHighlighted) Toolkit.Theme.COL_HIGHLIGHT - else if (mouseUp && item != null) Toolkit.Theme.COL_ACTIVE - else Toolkit.Theme.COL_INVENTORY_CELL_BORDER + batch.color = if (highlightToMainCol) colourTheme.cellHighlightMainCol + else if (highlightToSubCol) colourTheme.cellHighlightSubCol + else if (mouseUp && item != null) colourTheme.cellHighlightMouseUpCol + else colourTheme.cellHighlightNormalCol Toolkit.drawBoxBorder(batch, posX, posY, width, height) @@ -92,15 +109,15 @@ class UIItemInventoryElemWide( // if mouse is over, text lights up // highlight item name and count (blocks/walls) if the item is equipped batch.color = item!!.nameColour mul ( - if ((equippedSlot != null && highlightEquippedItem) || forceHighlighted) Toolkit.Theme.COL_HIGHLIGHT - else if (mouseUp && item != null) Toolkit.Theme.COL_ACTIVE - else Color.WHITE - ) + if (highlightToMainCol) colourTheme.textHighlightMainCol + else if (highlightToSubCol) colourTheme.textHighlightSubCol + else if (mouseUp && item != null) colourTheme.textHighlightMouseUpCol + else colourTheme.textHighlightNormalCol) // draw name of the item App.fontGame.draw(batch, // print name and amount in parens - item!!.name + (if (amount > 0 && item!!.stackable) "$fwsp($amountString)" else if (amount != 1L) "$fwsp!!$amountString!!" else ""), + item!!.name + (if (amount > 0 && item!!.stackable) "\u3000($amountString)" else if (amount != 1L) "\u3000!!$amountString!!" else ""), posX + textOffsetX, posY + textOffsetY diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInventory.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInventory.kt index 9f91805e1..205b33304 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInventory.kt @@ -106,7 +106,7 @@ open class FixtureInventory() { val newCount = existingItem.qty - count /*if (newCount < 0) { - throw Error("[${this.javaClass.canonicalName}] Tried to remove $count of $item, but the inventory only contains ${existingItem.qty} of them.") + throw InventoryFailedTransactionError("[${this.javaClass.canonicalName}] Tried to remove $count of $item, but the inventory only contains ${existingItem.qty} of them.") } else*/ if (newCount > 0) { // decrement count @@ -120,7 +120,7 @@ open class FixtureInventory() { } } else { -// throw Error("[${this.javaClass.canonicalName}] Tried to remove $item, but the inventory does not have it.") +// throw InventoryFailedTransactionError("[${this.javaClass.canonicalName}] Tried to remove $item, but the inventory does not have it.") } } @@ -234,4 +234,6 @@ class InventoryPair : Comparable { operator fun component2() = qty override fun compareTo(other: InventoryPair) = this.itm.compareTo(other.itm) -} \ No newline at end of file +} + +class InventoryTransactionFailedError(msg: String) : Error(msg) \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt index d3176258e..edb4220f4 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt @@ -77,7 +77,7 @@ internal class UIStorageChest : UICanvas( override var height = App.scr.height override var openCloseTime: Second = 0.0f - private val negotiator = object : InventoryNegotiator() { + private val negotiator = object : InventoryTransactionNegotiator() { override fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Long) { player.remove(item, amount) fixture.add(item, amount) diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt index f153f9229..449cf56aa 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt @@ -42,7 +42,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { private val craftables = FixtureInventory() // might be changed to something else private val ingredients = FixtureInventory() // this one is definitely not to be changed - private val negotiator = object : InventoryNegotiator() { + private val negotiator = object : InventoryTransactionNegotiator() { override fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Long) { // TODO() } @@ -81,6 +81,9 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { private val oldSelectedItems = ArrayList() + private val craftMult + get() = spinnerCraftCount.value.toLong() + init { val craftButtonsY = thisOffsetY + 23 + (UIItemInventoryElemWide.height + listGap) * (UIInventoryFull.CELLS_VRT - 1) val buttonWidth = (UIItemInventoryElemWide.height + listGap) * 3 - listGap - 2 @@ -96,6 +99,9 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { drawScrollOnRightside = false, drawWallet = false, hideSidebar = true, + colourTheme = UIItemInventoryCellCommonRes.defaultInventoryCellTheme.copy( + cellHighlightSubCol = Toolkit.Theme.COL_INACTIVE + ), keyDownFun = { _, _, _, _, _ -> }, touchDownFun = { _, _, _, _, _ -> } ) @@ -104,6 +110,14 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { itemListIngredients.gridModeButtons[0].touchDownListener = { _,_,_,_ -> } itemListIngredients.gridModeButtons[1].touchDownListener = { _,_,_,_ -> } itemListIngredients.isCompactMode = true + itemListIngredients.setCustomHighlightRuleSub { + it.item?.let { ingredient -> + return@setCustomHighlightRuleSub getPlayerInventory().searchByID(ingredient.dynamicID)?.let { itemOnPlayer -> + itemOnPlayer.qty * craftMult >= it.amount * craftMult + } == true + } + false + } // player inventory to the right @@ -137,6 +151,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { (it.keyMode == CraftingCodex.CraftingItemKeyMode.TAG && ItemCodex[itemPair.itm]!!.tags.contains(it.key)) } changeIngredient(oldItem, itemID) + refreshCraftButtonStatus() } } } } ) @@ -195,6 +210,8 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { oldSelectedItems.clear() oldSelectedItems.addAll(selectedItems) + + refreshCraftButtonStatus() } } ) @@ -205,25 +222,31 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { itemListIngredients.rebuild(catAll) itemListCraftable.numberMultiplier = it.toLong() itemListCraftable.rebuild(catAll) + refreshCraftButtonStatus() } buttonCraft.touchDownListener = { _,_,_,_ -> getPlayerInventory().let { player -> recipeClicked?.let { recipe -> - val mult = spinnerCraftCount.value.toLong() - - // TODO check if player has enough amount of ingredients - - itemListIngredients.getInventory().itemList.forEach { (itm, qty) -> - player.remove(itm, qty * mult) + // check if player has enough amount of ingredients + val itemCraftable = itemListIngredients.getInventory().itemList.all { (itm, qty) -> + (player.searchByID(itm)?.qty ?: -1) >= qty * craftMult } - player.add(recipe.product, recipe.moq * mult) - // reset selection status after a crafting to hide the possible artefact where no-longer-craftable items are still displayed due to ingredient depletion - resetUI() // also clears forcehighlightlist - itemListPlayer.rebuild(catAll) - itemListCraftable.rebuild(catAll) + + if (itemCraftable) { + itemListIngredients.getInventory().itemList.forEach { (itm, qty) -> + player.remove(itm, qty * craftMult) + } + player.add(recipe.product, recipe.moq * craftMult) + + // reset selection status after a crafting to hide the possible artefact where no-longer-craftable items are still displayed due to ingredient depletion + resetUI() // also clears forcehighlightlist + itemListPlayer.rebuild(catAll) + itemListCraftable.rebuild(catAll) + } } } + refreshCraftButtonStatus() } // make grid mode buttons work together // itemListCraftable.gridModeButtons[0].touchDownListener = { _,_,_,_ -> setCompact(false) } @@ -260,7 +283,22 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { itemListCraftable.highlightButton(button) itemListCraftable.rebuild(catAll) } - + + /** + * Updates Craft! button so that the button is correctly highlighted + */ + fun refreshCraftButtonStatus() { + val itemCraftable = if (itemListIngredients.getInventory().itemList.size < 1) false + else getPlayerInventory().let { player -> + // check if player has enough amount of ingredients + itemListIngredients.getInventory().itemList.all { (itm, qty) -> + (player.searchByID(itm)?.qty ?: -1) >= qty * craftMult + } + } + + buttonCraft.isActive = itemCraftable + } + // reset whatever player has selected to null and bring UI to its initial state fun resetUI() { // reset spinner @@ -274,6 +312,8 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { ingredients.clear() itemListPlayer.removeFromForceHighlightList(oldSelectedItems) itemListIngredients.rebuild(catAll) + + refreshCraftButtonStatus() } private var openingClickLatched = false diff --git a/src/net/torvald/terrarum/modulebasegame/ui/HasInventory.kt b/src/net/torvald/terrarum/modulebasegame/ui/HasInventory.kt index 1d6e5b4cd..4aa86bd5f 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/HasInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/HasInventory.kt @@ -4,7 +4,7 @@ import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory interface HasInventory { - fun getNegotiator(): InventoryNegotiator + fun getNegotiator(): InventoryTransactionNegotiator fun getFixtureInventory(): FixtureInventory fun getPlayerInventory(): FixtureInventory diff --git a/src/net/torvald/terrarum/modulebasegame/ui/InventoryNegotiator.kt b/src/net/torvald/terrarum/modulebasegame/ui/InventoryTransactionNegotiator.kt similarity index 94% rename from src/net/torvald/terrarum/modulebasegame/ui/InventoryNegotiator.kt rename to src/net/torvald/terrarum/modulebasegame/ui/InventoryTransactionNegotiator.kt index 7619af772..9486f9c47 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/InventoryNegotiator.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/InventoryTransactionNegotiator.kt @@ -7,7 +7,7 @@ import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory /** * Created by minjaesong on 2021-03-12. */ -abstract class InventoryNegotiator { +abstract class InventoryTransactionNegotiator { /** Retrieve item filter to be used to show only the acceptable items when player's own inventory is being displayed */ open fun getItemFilter(): List = listOf(CAT_ALL) // GameItem.Category /** Accepts item from the player and pass it to right inventory (object), slot (UI), etc... */ diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt index 9bed42797..474358931 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt @@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.TextureRegion import net.torvald.terrarum.GdxColorMap import net.torvald.terrarum.gameitems.GameItem +import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UIItem import kotlin.math.absoluteValue @@ -34,6 +35,14 @@ abstract class UIItemInventoryCellBase( abstract override fun update(delta: Float) abstract override fun render(batch: SpriteBatch, camera: Camera) + /** Custom highlight rule to highlight tihs button to primary accent colour (blue by default). + * Set to `null` to use default rule: + * + * "`equippedSlot` defined and set to `highlightEquippedItem`" or "`forceHighlighted`" */ + var customHighlightRuleMain: ((UIItemInventoryCellBase) -> Boolean)? = null + /** Custom highlight rule to highlight this button to secondary accent colour (yellow by default). Set to `null` to use default rule (which does nothing). */ + var customHighlightRule2: ((UIItemInventoryCellBase) -> Boolean)? = null + var forceHighlighted = false /*set(value) { if (field != value) { @@ -85,4 +94,26 @@ object UIItemInventoryCellCommonRes { // -2.14B } -} \ No newline at end of file + + val defaultInventoryCellTheme = InventoryCellColourTheme( + Toolkit.Theme.COL_HIGHLIGHT, + Toolkit.Theme.COL_LIST_DEFAULT, + Toolkit.Theme.COL_ACTIVE, + Toolkit.Theme.COL_INVENTORY_CELL_BORDER, + Toolkit.Theme.COL_HIGHLIGHT, + Color.WHITE, + Toolkit.Theme.COL_ACTIVE, + Color.WHITE, + ) +} + +data class InventoryCellColourTheme( + val cellHighlightMainCol: Color, + val cellHighlightSubCol: Color, + val cellHighlightMouseUpCol: Color, + val cellHighlightNormalCol: Color, + val textHighlightMainCol: Color, + val textHighlightSubCol: Color, + val textHighlightMouseUpCol: Color, + val textHighlightNormalCol: Color, +) \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt index a36298202..bd5cb93e6 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt @@ -14,6 +14,7 @@ import net.torvald.terrarum.modulebasegame.TerrarumIngame import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory import net.torvald.terrarum.modulebasegame.gameactors.InventoryPair +import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.defaultInventoryCellTheme import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UIItem @@ -46,7 +47,8 @@ open class UIItemInventoryItemGrid( keyDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Keycode, extra info, self touchDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Button, extra info, self protected val useHighlightingManager: Boolean = true, // only used by UIItemCraftingCandidateGrid which addresses buttons directly to set highlighting - open protected val highlightEquippedItem: Boolean = true // for some UIs that only cares about getting equipped slot number but not highlighting + open protected val highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting + colourTheme: InventoryCellColourTheme = defaultInventoryCellTheme ) : UIItem(parentUI, initialX, initialY) { // deal with the moving position @@ -186,7 +188,8 @@ open class UIItemInventoryItemGrid( drawBackOnNull = true, keyDownFun = keyDownFun, touchDownFun = touchDownFun, - highlightEquippedItem = highlightEquippedItem + highlightEquippedItem = highlightEquippedItem, + colourTheme = colourTheme ) } // automatically determine how much columns are needed. Minimum Width = 5 grids @@ -205,7 +208,8 @@ open class UIItemInventoryItemGrid( drawBackOnNull = true, keyDownFun = keyDownFun, touchDownFun = touchDownFun, - highlightEquippedItem = highlightEquippedItem + highlightEquippedItem = highlightEquippedItem, + colourTheme = colourTheme ) } @@ -266,6 +270,16 @@ open class UIItemInventoryItemGrid( highlightable = false ) + fun setCustomHighlightRuleMain(predicate: ((UIItemInventoryCellBase) -> Boolean)?) { + itemGrid.forEach { it.customHighlightRuleMain = predicate } + itemList.forEach { it.customHighlightRuleMain = predicate } + } + + fun setCustomHighlightRuleSub(predicate: ((UIItemInventoryCellBase) -> Boolean)?) { + itemGrid.forEach { it.customHighlightRule2 = predicate } + itemList.forEach { it.customHighlightRule2 = predicate } + } + fun scrollItemPage(relativeAmount: Int) { itemPage = if (itemPageCount == 0) 0 else (itemPage + relativeAmount).fmod(itemPageCount) } diff --git a/src/net/torvald/terrarum/ui/UIItem.kt b/src/net/torvald/terrarum/ui/UIItem.kt index 744146736..37d590ecd 100644 --- a/src/net/torvald/terrarum/ui/UIItem.kt +++ b/src/net/torvald/terrarum/ui/UIItem.kt @@ -125,6 +125,11 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I * gamepad button events for one or more UIItems in one or more UICanvases. */ open var controllerInFocus = false + /** + * Whether the button is "available" or not to the player + */ + open var isActive = true + open fun show() {} open fun hide() {} @@ -136,23 +141,24 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I updateListener!!.invoke(delta) } + if (isActive) { + mouseOverCall?.update(delta) - mouseOverCall?.update(delta) + if (mouseUp) { + if (mouseOverCall?.isVisible ?: false) { + mouseOverCall?.setAsOpen() + } - if (mouseUp) { - if (mouseOverCall?.isVisible ?: false) { - mouseOverCall?.setAsOpen() + mouseOverCall?.updateUI(delta) } - - mouseOverCall?.updateUI(delta) - } - else { - if (mouseOverCall?.isVisible ?: false) { - mouseOverCall?.setAsClose() + else { + if (mouseOverCall?.isVisible ?: false) { + mouseOverCall?.setAsClose() + } } - - mouseLatched = false } + + if (!mouseUp) mouseLatched = false } } @@ -161,17 +167,19 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I */ open fun render(batch: SpriteBatch, camera: Camera) { if (parentUI.isVisible) { - mouseOverCall?.render(batch, camera) + if (isActive) { + mouseOverCall?.render(batch, camera) - if (mouseUp) { - mouseOverCall?.renderUI(batch, camera) + if (mouseUp) { + mouseOverCall?.renderUI(batch, camera) + } } } } // keyboard controlled open fun keyDown(keycode: Int): Boolean { - if (parentUI.isVisible && keyDownListener != null) { + if (parentUI.isVisible && keyDownListener != null && isActive) { keyDownListener!!.invoke(keycode) return true } @@ -179,7 +187,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I return false } open fun keyUp(keycode: Int): Boolean { - if (parentUI.isVisible && keyUpListener != null) { + if (parentUI.isVisible && keyUpListener != null && isActive) { keyUpListener!!.invoke(keycode) return true } @@ -187,7 +195,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I return false } open fun keyTyped(character: Char): Boolean { - if (parentUI.isVisible && keyTypedListener != null) { + if (parentUI.isVisible && keyTypedListener != null && isActive) { keyTypedListener!!.invoke(character) return true } @@ -197,7 +205,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I // mouse controlled open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean { - if (parentUI.isVisible && touchDraggedListener != null) { + if (parentUI.isVisible && touchDraggedListener != null && isActive) { touchDraggedListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer) return true } @@ -207,7 +215,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I open fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { var actionDone = false - if (parentUI.isVisible) { + if (parentUI.isVisible && isActive) { if (touchDownListener != null && mouseUp) { touchDownListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer, button) actionDone = true @@ -232,7 +240,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I return false } open fun scrolled(amountX: Float, amountY: Float): Boolean { - if (parentUI.isVisible && scrolledListener != null && mouseUp) { + if (parentUI.isVisible && scrolledListener != null && mouseUp && isActive) { scrolledListener!!.invoke(amountX, amountY) return true } diff --git a/src/net/torvald/terrarum/ui/UIItemTextButton.kt b/src/net/torvald/terrarum/ui/UIItemTextButton.kt index a33f7a76e..d325632c8 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextButton.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextButton.kt @@ -37,6 +37,8 @@ open class UIItemTextButton( /** Colour on normal status */ val inactiveCol: Color = Toolkit.Theme.COL_LIST_DEFAULT, + val disabledCol: Color = Toolkit.Theme.COL_INVENTORY_CELL_BORDER, + val hasBorder: Boolean = false, val paddingLeft: Int = 0, @@ -108,7 +110,8 @@ open class UIItemTextButton( } - batch.color = if (highlighted) highlightCol + batch.color = if (!isActive) disabledCol + else if (highlighted) highlightCol else if (mouseUp) activeCol else inactiveCol diff --git a/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt b/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt index 7b98dde2f..aedd887a3 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt @@ -99,7 +99,7 @@ class UIItemTextLineInput( false ) - var isActive: Boolean = false // keep it false by default! + override var isActive: Boolean = false // keep it false by default! set(value) { if (field && !value) endComposing(true) field = value