From 60497e42015916cd4c6c8a00360f71e6ff004f24 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 2 Jul 2022 15:57:58 +0900 Subject: [PATCH] fixed a bug where selected recipe does not highlight --- .../modulebasegame/gameactors/UICrafting.kt | 20 ++-------- .../ui/UIItemCraftingCandidateGrid.kt | 39 +++++-------------- .../ui/UIItemInventoryCellBase.kt | 7 ++++ .../ui/UIItemInventoryItemGrid.kt | 14 +++---- 4 files changed, 28 insertions(+), 52 deletions(-) diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt index 0b14c522f..60296a9ee 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt @@ -210,6 +210,9 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { 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) } @@ -252,6 +255,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { private fun highlightCraftingCandidateButton(button: UIItemInventoryCellBase?) { // a proxy function itemListCraftable.highlightButton(button) + itemListCraftable.rebuild(catAll) } // reset whatever player has selected to null and bring UI to its initial state @@ -292,22 +296,6 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { } } - private fun setCompact(yes: Boolean) { - itemListCraftable.isCompactMode = yes - itemListCraftable.gridModeButtons[0].highlighted = !yes - itemListCraftable.gridModeButtons[1].highlighted = yes - itemListCraftable.itemPage = 0 - itemListCraftable.rebuild(catAll) - - itemListPlayer.isCompactMode = yes - itemListPlayer.gridModeButtons[0].highlighted = !yes - itemListPlayer.gridModeButtons[1].highlighted = yes - itemListPlayer.itemPage = 0 - itemListPlayer.rebuild(catAll) - - itemListUpdate() - } - override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { if (!openingClickLatched) { return super.touchDown(screenX, screenY, pointer, button) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt index a20d4608e..ede794951 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt @@ -1,5 +1,6 @@ package net.torvald.terrarum.modulebasegame.ui +import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.CraftingRecipeCodex import net.torvald.terrarum.ItemCodex import net.torvald.terrarum.UIItemInventoryCatBar @@ -27,7 +28,8 @@ class UIItemCraftingCandidateGrid( drawWallet = false, hideSidebar = false, keyDownFun = keyDownFun, - touchDownFun = touchDownFun + touchDownFun = touchDownFun, + useHighlightingManager = false ) { val craftingRecipes = ArrayList() @@ -38,13 +40,18 @@ class UIItemCraftingCandidateGrid( fun highlightButton(button: UIItemInventoryCellBase?) { items.forEach { it.forceHighlighted = false } button?.forceHighlighted = true + + button?.let { + printdbg(this, "highlighting button UIItemInventoryCellBase: ${it.item?.dynamicID}, ${it.amount}") + } } override fun rebuild(filter: Array) { - // test fill craftingRecipes with every possible recipes in the game + // TODO test fill craftingRecipes with every possible recipes in the game + // filtering policy: if the player have all the ingredient item (regardless of the amount!), make the recipe visible craftingRecipes.clear() CraftingRecipeCodex.props.forEach { (_, recipes) -> craftingRecipes.addAll(recipes) } - + // end of test fill recipesSortList.clear() // kinda like the output list @@ -62,32 +69,6 @@ class UIItemCraftingCandidateGrid( items[k].amount = sortListItem.moq * numberMultiplier items[k].itemImage = ItemCodex.getItemImage(sortListItem.product) items[k].extraInfo = sortListItem - - // set quickslot number - /*if (getInventory() is ActorInventory) { - val ainv = getInventory() as ActorInventory - - for (qs in 1..UIQuickslotBar.SLOT_COUNT) { - if (sortListItem.product == ainv.getQuickslotItem(qs - 1)?.itm) { - items[k].quickslot = qs % 10 // 10 -> 0, 1..9 -> 1..9 - break - } - else - items[k].quickslot = null - } - - // set equippedslot number - for (eq in ainv.itemEquipped.indices) { - if (eq < ainv.itemEquipped.size) { - if (ainv.itemEquipped[eq] == items[k].item?.dynamicID) { - items[k].equippedSlot = eq - break - } - else - items[k].equippedSlot = null - } - } - }*/ } // we do not have an item, empty the slot catch (e: IndexOutOfBoundsException) { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt index 3c18ddbb7..68b90c177 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt @@ -34,6 +34,13 @@ abstract class UIItemInventoryCellBase( abstract override fun render(batch: SpriteBatch, camera: Camera) var forceHighlighted = false + /*set(value) { + if (field != value) { + printdbg(this, "forceHighlighted: ${field} -> ${value}; ${App.GLOBAL_RENDER_TIMER}") + printStackTrace(this) + } + field = value + }*/ override fun keyDown(keycode: Int): Boolean { keyDownFun(item, amount, keycode, extraInfo, this) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt index 7fb4a6df8..4194b6983 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt @@ -44,7 +44,8 @@ open class UIItemInventoryItemGrid( val drawWallet: Boolean = true, val hideSidebar: Boolean = false, 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 + 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 ) : UIItem(parentUI, initialX, initialY) { // deal with the moving position @@ -322,7 +323,7 @@ open class UIItemInventoryItemGrid( // define each button's highlighted status from the list of forceHighlighted, then render the button items.forEach { - it.forceHighlighted = forceHighlightList.contains(it.item?.dynamicID) + if (useHighlightingManager) it.forceHighlighted = forceHighlightList.contains(it.item?.dynamicID) it.render(batch, camera) } @@ -405,6 +406,10 @@ open class UIItemInventoryItemGrid( } private val forceHighlightList = HashSet() + get() { + if (!useHighlightingManager) throw IllegalStateException("useHighlightingManager is set to false") + return field + } /** * Call before rebuild() @@ -481,11 +486,6 @@ open class UIItemInventoryItemGrid( } } } - - // highlight if matches - if (forceHighlightList.contains(item.item?.dynamicID)) { - item.forceHighlighted = true - } } // we do not have an item, empty the slot catch (e: IndexOutOfBoundsException) {