From cd13696116080ec34a7a935a0eb040bc4386d38f Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sun, 17 Sep 2023 17:05:28 +0900 Subject: [PATCH] crafting ui: highlighting should work after mode change and scroll --- .../terrarum/modulebasegame/ui/UICrafting.kt | 6 +- .../ui/UIItemCraftingCandidateGrid.kt | 63 ++++++++++++++----- .../ui/UIItemInventoryItemGrid.kt | 12 ++-- 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt b/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt index b0dcc85c9..48e8c0c1d 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt @@ -274,7 +274,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { _getItemListPlayer().rebuild(catAll) _getItemListIngredients().rebuild(catAll) - highlightCraftingCandidateButton(button) + highlightCraftingCandidateButton(recipe) oldSelectedItems.clear() oldSelectedItems.addAll(selectedItems) @@ -348,8 +348,8 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { itemListIngredients.rebuild(catAll) } - private fun highlightCraftingCandidateButton(button: UIItemInventoryCellBase?) { // a proxy function - itemListCraftable.highlightButton(button) + private fun highlightCraftingCandidateButton(recipe: CraftingCodex.CraftingRecipe?) { // a proxy function + itemListCraftable.highlightRecipe(recipe) itemListCraftable.rebuild(catAll) } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt index 91db1d572..99102a573 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt @@ -36,19 +36,39 @@ class UIItemCraftingCandidateGrid( internal val recipesSortList = ArrayList() // a dual to the [inventorySortList] which contains the actual recipes instead of crafting recipes - fun highlightButton(button: UIItemInventoryCellBase?) { - items.forEach { it.forceHighlighted = false } - button?.forceHighlighted = true + private var highlightedRecipe: CraftingCodex.CraftingRecipe? = null - button?.let { - printdbg(this, "highlighting button UIItemInventoryCellBase: ${it.item?.dynamicID}, ${it.amount}") + fun highlightRecipe(recipe: CraftingCodex.CraftingRecipe?) { + items.forEach { it.forceHighlighted = false } + + highlightedRecipe = recipe + + recipe?.let { + items.find { it.extraInfo == recipe }?.let { buttonFound -> + buttonFound.forceHighlighted = true + } } } + override var isCompactMode = false // this is INIT code + set(value) { + field = value + items = if (value) itemGrid else itemList + highlightRecipe(highlightedRecipe) + rebuild(currentFilter) + } + private fun isCraftable(player: FixtureInventory, recipe: CraftingCodex.CraftingRecipe, nearbyCraftingStations: List): Boolean { return UICrafting.recipeToIngredientRecord(player, recipe, nearbyCraftingStations).none { it.howManyPlayerHas <= 0L || !it.craftingStationAvailable } } + override fun scrollItemPage(relativeAmount: Int) { + super.scrollItemPage(relativeAmount) + + // update highlighter status + highlightRecipe(highlightedRecipe) + } + override fun rebuild(filter: Array) { // filtering policy: if the player have all the ingredient item (regardless of the amount!), make the recipe visible craftingRecipes.clear() @@ -68,28 +88,41 @@ class UIItemCraftingCandidateGrid( // map sortList to item list for (k in items.indices) { + val item = items[k] // we have an item try { val sortListItem = recipesSortList[k + itemPage * items.size] - items[k].item = ItemCodex[sortListItem.product] - items[k].amount = sortListItem.moq * numberMultiplier - items[k].itemImage = ItemCodex.getItemImage(sortListItem.product) - items[k].extraInfo = sortListItem + item.item = ItemCodex[sortListItem.product] + item.amount = sortListItem.moq * numberMultiplier + item.itemImage = ItemCodex.getItemImage(sortListItem.product) + item.extraInfo = sortListItem + item.forceHighlighted = (item.extraInfo == highlightedRecipe) } // we do not have an item, empty the slot catch (e: IndexOutOfBoundsException) { - items[k].item = null - items[k].amount = 0 - items[k].itemImage = null - items[k].quickslot = null - items[k].equippedSlot = null - items[k].extraInfo = null + item.item = null + item.amount = 0 + item.itemImage = null + item.quickslot = null + item.equippedSlot = null + item.extraInfo = null + item.forceHighlighted = false } } itemPageCount = (recipesSortList.size.toFloat() / items.size.toFloat()).ceilToInt() + + rebuildList = false } + + + + + override fun scrolled(amountX: Float, amountY: Float): Boolean { + super.scrolled(amountX, amountY) + return true + } } \ 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 cfbba47ab..9def7983a 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt @@ -81,7 +81,7 @@ open class UIItemInventoryItemGrid( arrayOf(GameItem.Category.MISC), arrayOf(CAT_ALL) )*/ - private var currentFilter = arrayOf(CAT_ALL) + protected var currentFilter = arrayOf(CAT_ALL) private val inventoryUI = parentUI @@ -183,7 +183,7 @@ open class UIItemInventoryItemGrid( val tooltipShowing = HashMap() // Long: `hash` field on UIItemInventoryItemGrid } - private val itemGrid = Array(horizontalCells * verticalCells) { + protected val itemGrid = Array(horizontalCells * verticalCells) { UIItemInventoryElemSimple( parentUI = inventoryUI, initialX = this.posX + (UIItemInventoryElemSimple.height + listGap) * (it % horizontalCells), @@ -202,7 +202,7 @@ open class UIItemInventoryItemGrid( private val itemListColumnCount = floor(horizontalCells / 5f).toInt().coerceAtLeast(1) private val actualItemCellWidth = (listGap + UIItemInventoryElemSimple.height) * horizontalCells - listGap // in pixels private val largeListWidth = ((listGap + actualItemCellWidth) / itemListColumnCount) - (itemListColumnCount - 1).coerceAtLeast(1) * listGap - private val itemList = Array(verticalCells * itemListColumnCount) { + protected val itemList = Array(verticalCells * itemListColumnCount) { UIItemInventoryElemWide( parentUI = inventoryUI, initialX = this.posX + (largeListWidth + listGap) * (it % itemListColumnCount), @@ -221,11 +221,11 @@ open class UIItemInventoryItemGrid( var items: Array = itemList - var isCompactMode = false // this is INIT code + open var isCompactMode = false // this is INIT code set(value) { + field = value items = if (value) itemGrid else itemList rebuild(currentFilter) - field = value } private val iconPosX = if (drawScrollOnRightside) @@ -243,7 +243,7 @@ open class UIItemInventoryItemGrid( itemList.forEach { it.customHighlightRule2 = predicate } } - fun scrollItemPage(relativeAmount: Int) { + open fun scrollItemPage(relativeAmount: Int) { itemPage = if (itemPageCount == 0) 0 else (itemPage + relativeAmount).fmod(itemPageCount) }