From e1935fb659c29c317a75a0528a5ba9f79fdd8cb5 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Wed, 31 Jan 2024 02:07:17 +0900 Subject: [PATCH] smelter: scroll wheel to operate the item slots --- .../gameactors/ActorInventory.kt | 6 +- .../gameactors/FixtureInventory.kt | 17 +- .../terrarum/modulebasegame/ui/UICrafting.kt | 167 ++++++++--------- .../modulebasegame/ui/UIInventoryCells.kt | 15 +- .../ui/UIItemCraftingCandidateGrid.kt | 21 +-- .../ui/UIItemInventoryCellBase.kt | 9 + .../ui/UIItemInventoryEquippedView.kt | 3 +- .../ui/UIItemInventoryItemGrid.kt | 35 ++-- .../modulebasegame/ui/UIJukeboxInventory.kt | 1 + .../modulebasegame/ui/UISmelterBasic.kt | 168 ++++++++++++++++-- .../modulebasegame/ui/UIStorageChest.kt | 15 +- .../ui/UITemplateHalfInventory.kt | 6 +- .../modulebasegame/ui/UIWorldPortalCargo.kt | 2 + .../terrarum/ui/UIItemInventoryElemSimple.kt | 3 +- .../terrarum/ui/UIItemInventoryElemWide.kt | 3 +- 15 files changed, 324 insertions(+), 147 deletions(-) diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt index 8ca695648..2209d3551 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt @@ -40,11 +40,11 @@ class ActorInventory() : FixtureInventory() { val quickSlot = Array(UIQuickslotBar.SLOT_COUNT) { null } // 0: Slot 1, 9: Slot 10 - override fun remove(itemID: ItemID, count: Long) = remove(ItemCodex[itemID]!!, count) + override fun remove(itemID: ItemID, count: Long): Long = remove(ItemCodex[itemID]!!, count) /** Will check existence of the item using its Dynamic ID; careful with command order! * e.g. re-assign after this operation */ - override fun remove(item: GameItem, count: Long) { - super.remove(item, count) { existingItem -> + override fun remove(item: GameItem, count: Long): Long { + return super.remove(item, count) { existingItem -> // unequip, if applicable actor.unequipItem(existingItem.itm) // also unequip on the quickslot diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInventory.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInventory.kt index a2c3d80d2..efd3cfc7c 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInventory.kt @@ -136,19 +136,23 @@ open class FixtureInventory() { open fun remove(itemID: ItemID, count: Long, unequipFun: (InventoryPair) -> Unit) = remove(ItemCodex[itemID]!!, count, unequipFun) /** Will check existence of the item using its Dynamic ID; careful with command order! - * e.g. re-assign after this operation */ - open fun remove(item: GameItem, count: Long = 1, unequipFun: (InventoryPair) -> Unit) { + * e.g. re-assign after this operation + * + * @return the actual amount of item that has been removed + **/ + open fun remove(item: GameItem, count: Long = 1, unequipFun: (InventoryPair) -> Unit): Long { // printdbg(this, "remove $item, $count") if (count == 0L) // throw IllegalArgumentException("[${this.javaClass.canonicalName}] Item count is zero.") - return + return 0L if (count < 0L) throw IllegalArgumentException("[${this.javaClass.canonicalName}] Item count is negative number. If you intended adding items, use add()" + "These commands are NOT INTERCHANGEABLE; they handle things differently according to the context.") + var delta = 0L val existingItem = searchByID(item.dynamicID) if (existingItem != null) { // if the item already exists @@ -158,10 +162,12 @@ open class FixtureInventory() { throw InventoryFailedTransactionError("[${this.javaClass.canonicalName}] Tried to remove $count of $item, but the inventory only contains ${existingItem.qty} of them.") } else*/ if (newCount > 0) { + delta = count // decrement count existingItem.qty = newCount } else { + delta = existingItem.qty // unequip must be done before the entry removal unequipFun(existingItem) // depleted item; remove entry from inventory @@ -170,11 +176,14 @@ open class FixtureInventory() { } else { // throw InventoryFailedTransactionError("[${this.javaClass.canonicalName}] Tried to remove $item, but the inventory does not have it.") + return 0L } - itemList.sumOf { ItemCodex[it.itm]!!.mass * it.qty } +// itemList.sumOf { ItemCodex[it.itm]!!.mass * it.qty } // ??? updateEncumbrance() + + return delta } open fun clear(): List { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt b/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt index 47143c4b9..055727b24 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt @@ -121,92 +121,93 @@ class UICrafting(val full: UIInventoryFull?) : UICanvas( // ingredient list itemListIngredients = UIItemInventoryItemGrid( - this, - { ingredients }, - thisOffsetX, - thisOffsetY + LAST_LINE_IN_GRID, - 6, 1, - drawScrollOnRightside = false, - drawWallet = false, - hideSidebar = true, - colourTheme = UIItemInventoryCellCommonRes.defaultInventoryCellTheme.copy( - cellHighlightSubCol = Toolkit.Theme.COL_INACTIVE - ), - keyDownFun = { _, _, _, _, _ -> }, - touchDownFun = { gameItem, amount, _, _, _ -> gameItem?.let { gameItem -> - // if the item is craftable one, load its recipe instead - CraftingRecipeCodex.getRecipesFor(gameItem.originalID)?.let { recipes -> - // select most viable recipe (completely greedy search) - val player = getPlayerInventory() - // list of [Score, Ingredients, Recipe] - recipes.map { recipe -> - // list of (Item, How many player has, How many the recipe requires) - val items = recipeToIngredientRecord(player, recipe, nearbyCraftingStations) + this, + { ingredients }, + thisOffsetX, + thisOffsetY + LAST_LINE_IN_GRID, + 6, 1, + drawScrollOnRightside = false, + drawWallet = false, + hideSidebar = true, + colourTheme = UIItemInventoryCellCommonRes.defaultInventoryCellTheme.copy( + cellHighlightSubCol = Toolkit.Theme.COL_INACTIVE + ), + keyDownFun = { _, _, _, _, _ -> }, + wheelFun = { _, _, _, _, _, _ -> }, + touchDownFun = { gameItem, amount, _, _, _ -> gameItem?.let { gameItem -> + // if the item is craftable one, load its recipe instead + CraftingRecipeCodex.getRecipesFor(gameItem.originalID)?.let { recipes -> + // select most viable recipe (completely greedy search) + val player = getPlayerInventory() + // list of [Score, Ingredients, Recipe] + recipes.map { recipe -> + // list of (Item, How many player has, How many the recipe requires) + val items = recipeToIngredientRecord(player, recipe, nearbyCraftingStations) - val score = items.fold(1L) { acc, item -> - (item.howManyPlayerHas).times(16L) + 1L - } - - listOf(score, items, recipe) - }.maxByOrNull { it[0] as Long }?.let { (_, items, recipe) -> - val items = items as List> - val recipe = recipe as CraftingCodex.CraftingRecipe - - // change selected recipe to mostViableRecipe then update the UIs accordingly - // FIXME recipe highlighting will not change correctly! - val selectedItems = ArrayList() - - // auto-dial the spinner so that player would just have to click the Craft! button (for the most time, that is) - val howManyRequired = craftMult * amount - val howManyPlayerHas = player.searchByID(gameItem.dynamicID)?.qty ?: 0 - val howManyPlayerMightNeed = ceil((howManyRequired - howManyPlayerHas).toDouble() / recipe.moq).toLong() - resetSpinner(howManyPlayerMightNeed.coerceIn(1L, App.getConfigInt("basegame:gameplay_max_crafting").toLong())) - - ingredients.clear() - recipeClicked = recipe - - items.forEach { - val itm = it[0] as ItemID - val qty = it[2] as Long - - selectedItems.add(itm) - ingredients.add(itm, qty) - } - - _getItemListPlayer().let { - it.removeFromForceHighlightList(oldSelectedItems) - filterPlayerListUsing(recipeClicked) - it.addToForceHighlightList(selectedItems) - it.rebuild(FILTER_CAT_ALL) - } - _getItemListIngredients().rebuild(FILTER_CAT_ALL) - - // highlighting CraftingCandidateButton by searching for the buttons that has the recipe - _getItemListCraftables().let { - // turn the highlights off - it.items.forEach { it.forceHighlighted = false } - - // search for the recipe - // also need to find what "page" the recipe might be in - // use it.isCompactMode to find out the current mode - var ord = 0 - while (ord < it.craftingRecipes.indices.last) { - if (recipeClicked == it.craftingRecipes[ord]) break - ord += 1 - } - val itemSize = it.items.size - - it.itemPage = ord / itemSize - it.items[ord % itemSize].forceHighlighted = true - } - - oldSelectedItems.clear() - oldSelectedItems.addAll(selectedItems) - - refreshCraftButtonStatus() + val score = items.fold(1L) { acc, item -> + (item.howManyPlayerHas).times(16L) + 1L } + + listOf(score, items, recipe) + }.maxByOrNull { it[0] as Long }?.let { (_, items, recipe) -> + val items = items as List> + val recipe = recipe as CraftingCodex.CraftingRecipe + + // change selected recipe to mostViableRecipe then update the UIs accordingly + // FIXME recipe highlighting will not change correctly! + val selectedItems = ArrayList() + + // auto-dial the spinner so that player would just have to click the Craft! button (for the most time, that is) + val howManyRequired = craftMult * amount + val howManyPlayerHas = player.searchByID(gameItem.dynamicID)?.qty ?: 0 + val howManyPlayerMightNeed = ceil((howManyRequired - howManyPlayerHas).toDouble() / recipe.moq).toLong() + resetSpinner(howManyPlayerMightNeed.coerceIn(1L, App.getConfigInt("basegame:gameplay_max_crafting").toLong())) + + ingredients.clear() + recipeClicked = recipe + + items.forEach { + val itm = it[0] as ItemID + val qty = it[2] as Long + + selectedItems.add(itm) + ingredients.add(itm, qty) + } + + _getItemListPlayer().let { + it.removeFromForceHighlightList(oldSelectedItems) + filterPlayerListUsing(recipeClicked) + it.addToForceHighlightList(selectedItems) + it.rebuild(FILTER_CAT_ALL) + } + _getItemListIngredients().rebuild(FILTER_CAT_ALL) + + // highlighting CraftingCandidateButton by searching for the buttons that has the recipe + _getItemListCraftables().let { + // turn the highlights off + it.items.forEach { it.forceHighlighted = false } + + // search for the recipe + // also need to find what "page" the recipe might be in + // use it.isCompactMode to find out the current mode + var ord = 0 + while (ord < it.craftingRecipes.indices.last) { + if (recipeClicked == it.craftingRecipes[ord]) break + ord += 1 + } + val itemSize = it.items.size + + it.itemPage = ord / itemSize + it.items[ord % itemSize].forceHighlighted = true + } + + oldSelectedItems.clear() + oldSelectedItems.addAll(selectedItems) + + refreshCraftButtonStatus() } - } } + } + } } ) // make sure grid buttons for ingredients do nothing (even if they are hidden!) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt index d59c3be6b..b5fb0d906 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt @@ -87,13 +87,14 @@ internal class UIInventoryCells( internal val itemList: UIItemInventoryItemGrid = UIItemInventoryItemGrid( - full, - { full.actor.inventory }, - INVENTORY_CELLS_OFFSET_X(), - INVENTORY_CELLS_OFFSET_Y(), - CELLS_HOR, CELLS_VRT, - keyDownFun = createInvCellGenericKeyDownFun(), - touchDownFun = createInvCellGenericTouchDownFun { rebuildList() } + full, + { full.actor.inventory }, + INVENTORY_CELLS_OFFSET_X(), + INVENTORY_CELLS_OFFSET_Y(), + CELLS_HOR, CELLS_VRT, + keyDownFun = createInvCellGenericKeyDownFun(), + touchDownFun = createInvCellGenericTouchDownFun { rebuildList() }, + wheelFun = { _, _, _, _, _, _ -> }, ) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt index 797d9b1fc..160bc89fd 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt @@ -21,16 +21,17 @@ class UIItemCraftingCandidateGrid( keyDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Keycode, extra info, keyed button touchDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit // Item, Amount, Button, extra info, clicked button ) : UIItemInventoryItemGrid( - parentUI, - { TODO() /* UNUSED and MUST NOT BE USED! */ }, - initialX, initialY, - horizontalCells, verticalCells, - drawScrollOnRightside, - drawWallet = false, - hideSidebar = false, - keyDownFun = keyDownFun, - touchDownFun = touchDownFun, - useHighlightingManager = false + parentUI, + { TODO() /* UNUSED and MUST NOT BE USED! */ }, + initialX, initialY, + horizontalCells, verticalCells, + drawScrollOnRightside, + drawWallet = false, + hideSidebar = false, + keyDownFun = keyDownFun, + touchDownFun = touchDownFun, + wheelFun = { _, _, _, _, _, _ -> }, + useHighlightingManager = false ) { val craftingRecipes = ArrayList() diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt index ed7ce8e05..12f93bf57 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt @@ -30,6 +30,7 @@ abstract class UIItemInventoryCellBase( open var equippedSlot: Int? = null, var keyDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Keycode, extra info, self var touchDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Button, extra info, self + var wheelFun: (GameItem?, Long, Float, Float, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, scroll x, scroll y, extra info, self open var extraInfo: Any?, open protected val highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting var colourTheme: InventoryCellColourTheme = UIItemInventoryCellCommonRes.defaultInventoryCellTheme, @@ -70,6 +71,14 @@ abstract class UIItemInventoryCellBase( } return true } + + override fun scrolled(amountX: Float, amountY: Float): Boolean { + if (mouseUp) { + wheelFun(item, amount, amountX, amountY, extraInfo, this) + super.scrolled(amountX, amountY) + } + return true + } } object UIItemInventoryCellCommonRes { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt index 28f24b143..c2c9b2e66 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt @@ -56,7 +56,8 @@ class UIItemInventoryEquippedView( drawBackOnNull = true, keyDownFun = createInvCellGenericKeyDownFun(), touchDownFun = createInvCellGenericTouchDownFun(inventoryListRebuildFun), // to "unselect" the equipped item and main item grid would "untick" accordingly - emptyCellIcon = equipPosIcon.get(cellToIcon[it], 1) + wheelFun = { _, _, _, _, _, _ -> }, + emptyCellIcon = equipPosIcon.get(cellToIcon[it], 1), ) } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt index 896232889..a596ba0cd 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt @@ -45,6 +45,7 @@ open class UIItemInventoryItemGrid( 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 + wheelFun: (GameItem?, Long, Float, Float, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, scroll x, scroll y, 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 private val colourTheme: InventoryCellColourTheme = defaultInventoryCellTheme @@ -183,13 +184,14 @@ open class UIItemInventoryItemGrid( protected val itemGrid = Array(horizontalCells * verticalCells) { UIItemInventoryElemSimple( - parentUI = inventoryUI, - initialX = this.posX + (UIItemInventoryElemSimple.height + listGap) * (it % horizontalCells), - initialY = this.posY + (UIItemInventoryElemSimple.height + listGap) * (it / horizontalCells), - keyDownFun = keyDownFun, - touchDownFun = touchDownFun, - highlightEquippedItem = highlightEquippedItem, - colourTheme = colourTheme + parentUI = inventoryUI, + initialX = this.posX + (UIItemInventoryElemSimple.height + listGap) * (it % horizontalCells), + initialY = this.posY + (UIItemInventoryElemSimple.height + listGap) * (it / horizontalCells), + keyDownFun = keyDownFun, + touchDownFun = touchDownFun, + wheelFun = wheelFun, + highlightEquippedItem = highlightEquippedItem, + colourTheme = colourTheme ) } // automatically determine how much columns are needed. Minimum Width = 5 grids @@ -198,14 +200,15 @@ open class UIItemInventoryItemGrid( private val largeListWidth = ((listGap + actualItemCellWidth) / itemListColumnCount) - (itemListColumnCount - 1).coerceAtLeast(1) * listGap protected val itemList = Array(verticalCells * itemListColumnCount) { UIItemInventoryElemWide( - parentUI = inventoryUI, - initialX = this.posX + (largeListWidth + listGap) * (it % itemListColumnCount), - initialY = this.posY + (UIItemInventoryElemWide.height + listGap) * (it / itemListColumnCount), - width = largeListWidth, - keyDownFun = keyDownFun, - touchDownFun = touchDownFun, - highlightEquippedItem = highlightEquippedItem, - colourTheme = colourTheme + parentUI = inventoryUI, + initialX = this.posX + (largeListWidth + listGap) * (it % itemListColumnCount), + initialY = this.posY + (UIItemInventoryElemWide.height + listGap) * (it / itemListColumnCount), + width = largeListWidth, + keyDownFun = keyDownFun, + touchDownFun = touchDownFun, + wheelFun = wheelFun, + highlightEquippedItem = highlightEquippedItem, + colourTheme = colourTheme ) } @@ -599,6 +602,8 @@ open class UIItemInventoryItemGrid( override fun scrolled(amountX: Float, amountY: Float): Boolean { super.scrolled(amountX, amountY) + items.forEach { if (it.mouseUp) it.scrolled(amountX, amountY) } + // scroll the item list (for now) if (mouseUp) { scrollItemPage(amountY.toInt()) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt index 2ca4cfaa0..20c84e880 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt @@ -48,6 +48,7 @@ class UIJukeboxInventory(val parent: UIJukebox) : UICanvas() { showItemCount = false, keyDownFun = { _, _, _, _, _ -> Unit }, touchDownFun = { _, _, _, _, _ -> Unit }, + wheelFun = { _, _, _, _, _, _ -> }, ) }.toTypedArray() diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UISmelterBasic.kt b/src/net/torvald/terrarum/modulebasegame/ui/UISmelterBasic.kt index 3cc0f7fc1..f68054281 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UISmelterBasic.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UISmelterBasic.kt @@ -5,7 +5,7 @@ import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.colourutil.cieluv_getGradient import net.torvald.terrarum.* -import net.torvald.terrarum.gameitems.GameItem +import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory import net.torvald.terrarum.modulebasegame.gameactors.FixtureSmelterBasic @@ -19,7 +19,6 @@ import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack import net.torvald.unicode.getKeycapPC import net.torvald.unicode.getMouseButton import kotlin.math.roundToInt -import kotlin.math.roundToLong /** * Created by minjaesong on 2024-01-29. @@ -68,6 +67,67 @@ class UISmelterBasic(val smelter: FixtureSmelterBasic) : UICanvas( itemListUpdateKeepCurrentFilter() } + it.itemListWheelFun = { gameItem, _, _, scrollY, _, _ -> + val scrollY = -scrollY + if (gameItem != null) { + val playerInventory = getPlayerInventory() + val addCount1 = scrollY.toLong() + + if (clickedOn == 1 && (smelter.oreItem == null || smelter.oreItem!!.itm == gameItem.dynamicID)) { + val itemToUse = smelter.oreItem?.itm ?: gameItem.dynamicID + + val addCount2 = scrollY.toLong().coerceIn( + -(playerInventory.searchByID(itemToUse)?.qty ?: 0L), + smelter.oreItem?.qty ?: 0L, + ) + + // add to the inventory slot + if (smelter.oreItem != null && addCount1 >= 1L) { + getPlayerInventory().add(smelter.oreItem!!.itm, addCount2) + smelter.oreItem!!.qty -= addCount2 + } + // remove from the inventory slot + else if (addCount1 <= -1L) { + playerInventory.remove(itemToUse, -addCount2) + if (smelter.oreItem == null) + smelter.oreItem = InventoryPair(itemToUse, -addCount2) + else + smelter.oreItem!!.qty -= addCount2 + } + if (smelter.oreItem != null && smelter.oreItem!!.qty == 0L) smelter.oreItem = null + else if (smelter.oreItem != null && smelter.oreItem!!.qty < 0L) throw Error("Item removal count is larger than what was on the slot") + itemListUpdateKeepCurrentFilter() + } + else if (clickedOn == 2 && (smelter.fireboxItem == null || smelter.fireboxItem!!.itm == gameItem.dynamicID)) { + val itemToUse = smelter.fireboxItem?.itm ?: gameItem.dynamicID + + val addCount2 = scrollY.toLong().coerceIn( + -(playerInventory.searchByID(itemToUse)?.qty ?: 0L), + smelter.fireboxItem?.qty ?: 0L, + ) + + // add to the inventory slot + if (smelter.fireboxItem != null && addCount1 >= 1L) { + getPlayerInventory().add(smelter.fireboxItem!!.itm, addCount2) + smelter.fireboxItem!!.qty -= addCount2 + } + // remove from the inventory slot + else if (addCount1 <= -1L) { + playerInventory.remove(itemToUse, -addCount2) + if (smelter.fireboxItem == null) + smelter.fireboxItem = InventoryPair(itemToUse, -addCount2) + else + smelter.fireboxItem!!.qty -= addCount2 + } + if (smelter.fireboxItem != null && smelter.fireboxItem!!.qty == 0L) smelter.fireboxItem = null + else if (smelter.fireboxItem != null && smelter.fireboxItem!!.qty < 0L) throw Error("Item removal count is larger than what was on the slot") + itemListUpdateKeepCurrentFilter() + } + else { + itemListUpdateKeepCurrentFilter() + } + } + } } fun getPlayerInventory(): FixtureInventory = INGAME.actorNowPlaying!!.inventory @@ -157,6 +217,33 @@ class UISmelterBasic(val smelter: FixtureSmelterBasic) : UICanvas( else { itemListUpdateKeepCurrentFilter() } + }, + wheelFun = { _, _, _, scrollY, _, _ -> + val scrollY = -scrollY + if (clickedOn == 1 && smelter.oreItem != null) { + val playerInventory = getPlayerInventory() + val removeCount1 = scrollY.toLong() + val removeCount2 = scrollY.toLong().coerceIn( + -smelter.oreItem!!.qty, + playerInventory.searchByID(smelter.oreItem!!.itm)?.qty ?: 0L, + ) + + // add to the slot + if (removeCount1 >= 1L) { + playerInventory.remove(smelter.oreItem!!.itm, removeCount2) + smelter.oreItem!!.qty += removeCount2 + } + // remove from the slot + else if (removeCount1 <= -1L) { + getPlayerInventory().add(smelter.oreItem!!.itm, -removeCount2) + smelter.oreItem!!.qty += removeCount2 + } + if (smelter.oreItem!!.qty == 0L) smelter.oreItem = null + itemListUpdateKeepCurrentFilter() + } + else { + itemListUpdateKeepCurrentFilter() + } } ) private val fireboxItemSlot: UIItemInventoryElemSimple = UIItemInventoryElemSimple( @@ -189,6 +276,33 @@ class UISmelterBasic(val smelter: FixtureSmelterBasic) : UICanvas( else { itemListUpdateKeepCurrentFilter() } + }, + wheelFun = { _, _, _, scrollY, _, _ -> + val scrollY = -scrollY + if (clickedOn == 2 && smelter.fireboxItem != null) { + val playerInventory = getPlayerInventory() + val removeCount1 = scrollY.toLong() + val removeCount2 = scrollY.toLong().coerceIn( + -smelter.fireboxItem!!.qty, + playerInventory.searchByID(smelter.fireboxItem!!.itm)?.qty ?: 0L, + ) + + // add to the slot + if (removeCount1 >= 1L) { + playerInventory.remove(smelter.fireboxItem!!.itm, removeCount2) + smelter.fireboxItem!!.qty += removeCount2 + } + // remove from the slot + else if (removeCount1 <= -1L) { + getPlayerInventory().add(smelter.fireboxItem!!.itm, -removeCount2) + smelter.fireboxItem!!.qty += removeCount2 + } + if (smelter.fireboxItem!!.qty == 0L) smelter.fireboxItem = null + itemListUpdateKeepCurrentFilter() + } + else { + itemListUpdateKeepCurrentFilter() + } } ) private val productItemslot: UIItemInventoryElemSimple = UIItemInventoryElemSimple( @@ -218,6 +332,27 @@ class UISmelterBasic(val smelter: FixtureSmelterBasic) : UICanvas( } itemListUpdateKeepCurrentFilter() } + }, + wheelFun = { _, _, _, scrollY, _, _ -> + val scrollY = -scrollY + if (smelter.productItem != null) { + val removeCount1 = scrollY.toLong() + val removeCount2 = scrollY.toLong().coerceIn( + -smelter.productItem!!.qty, + 0L, + ) + + // remove from the slot + if (removeCount1 <= -1L) { + getPlayerInventory().add(smelter.productItem!!.itm, -removeCount2) + smelter.productItem!!.qty += removeCount2 + } + if (smelter.productItem!!.qty == 0L) smelter.productItem = null + itemListUpdateKeepCurrentFilter() + } + else { + itemListUpdateKeepCurrentFilter() + } } ) @@ -309,25 +444,28 @@ class UISmelterBasic(val smelter: FixtureSmelterBasic) : UICanvas( } private val SP = "\u3000" + private val ML = getMouseButton(App.getConfigInt("config_mouseprimary")) + private val MR = getMouseButton(App.getConfigInt("config_mousesecondary")) + private val MW = getMouseButton(2) private val controlHelpForSmelter = listOf( // no slot selected { if (App.environment == RunningEnvironment.PC) "${getKeycapPC(ControlPresets.getKey("control_key_inventory"))} ${Lang["GAME_ACTION_CLOSE"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mouseprimary"))} ${Lang["GAME_ACTION_SELECT_SLOT"]}" + "$ML ${Lang["GAME_ACTION_SELECT_SLOT"]}" else "${App.gamepadLabelStart} ${Lang["GAME_ACTION_CLOSE"]}" }, // ore slot { if (App.environment == RunningEnvironment.PC) "${getKeycapPC(ControlPresets.getKey("control_key_inventory"))} ${Lang["GAME_ACTION_CLOSE"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mouseprimary"))} ${Lang["GAME_ACTION_TAKE_ALL_CONT"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mousesecondary"))} ${Lang["GAME_ACTION_TAKE_ONE_CONT"]}" + "$ML ${Lang["GAME_ACTION_TAKE_ALL_CONT"]}$SP" + + "$MW$MR ${Lang["GAME_ACTION_TAKE_ONE_CONT"]}" else "${App.gamepadLabelStart} ${Lang["GAME_ACTION_CLOSE"]}" }, // firebox slot { if (App.environment == RunningEnvironment.PC) "${getKeycapPC(ControlPresets.getKey("control_key_inventory"))} ${Lang["GAME_ACTION_CLOSE"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mouseprimary"))} ${Lang["GAME_ACTION_TAKE_ALL_CONT"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mousesecondary"))} ${Lang["GAME_ACTION_TAKE_ONE_CONT"]}" + "$ML ${Lang["GAME_ACTION_TAKE_ALL_CONT"]}$SP" + + "$MW$MR ${Lang["GAME_ACTION_TAKE_ONE_CONT"]}" else "${App.gamepadLabelStart} ${Lang["GAME_ACTION_CLOSE"]}" } ) @@ -337,14 +475,14 @@ class UISmelterBasic(val smelter: FixtureSmelterBasic) : UICanvas( { "" }, // ore slot { if (App.environment == RunningEnvironment.PC) - "${getMouseButton(App.getConfigInt("config_mouseprimary"))} ${Lang["GAME_ACTION_PUT_ALL_CONT"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mousesecondary"))} ${Lang["GAME_ACTION_PUT_ONE_CONT"]}" + "$ML ${Lang["GAME_ACTION_PUT_ALL_CONT"]}$SP" + + "$MW$MR ${Lang["GAME_ACTION_PUT_ONE_CONT"]}" else "${App.gamepadLabelStart} ${Lang["GAME_ACTION_CLOSE"]}" }, // firebox slot { if (App.environment == RunningEnvironment.PC) - "${getMouseButton(App.getConfigInt("config_mouseprimary"))} ${Lang["GAME_ACTION_PUT_ALL_CONT"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mousesecondary"))} ${Lang["GAME_ACTION_PUT_ONE_CONT"]}" + "$ML ${Lang["GAME_ACTION_PUT_ALL_CONT"]}$SP" + + "$MW$MR ${Lang["GAME_ACTION_PUT_ONE_CONT"]}" else "${App.gamepadLabelStart} ${Lang["GAME_ACTION_CLOSE"]}" } ) @@ -354,14 +492,14 @@ class UISmelterBasic(val smelter: FixtureSmelterBasic) : UICanvas( { "" }, // ore slot { if (App.environment == RunningEnvironment.PC) - "${getMouseButton(App.getConfigInt("config_mouseprimary"))} ${Lang["GAME_ACTION_PUT_ALL"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mousesecondary"))} ${Lang["GAME_ACTION_PUT_ONE"]}" + "$ML ${Lang["GAME_ACTION_PUT_ALL"]}$SP" + + "$MW$MR ${Lang["GAME_ACTION_PUT_ONE"]}" else "${App.gamepadLabelStart} ${Lang["GAME_ACTION_CLOSE"]}" }, // firebox slot { if (App.environment == RunningEnvironment.PC) - "${getMouseButton(App.getConfigInt("config_mouseprimary"))} ${Lang["GAME_ACTION_PUT_ALL"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mousesecondary"))} ${Lang["GAME_ACTION_PUT_ONE"]}" + "$ML ${Lang["GAME_ACTION_PUT_ALL"]}$SP" + + "$MW$MR ${Lang["GAME_ACTION_PUT_ONE"]}" else "${App.gamepadLabelStart} ${Lang["GAME_ACTION_CLOSE"]}" } ) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt index eb2717c6c..104638823 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt @@ -206,25 +206,28 @@ internal class UIStorageChest : UICanvas( private val cellsWidth = (UIItemInventoryItemGrid.listGap + UIItemInventoryElemWide.height) * 6 - UIItemInventoryItemGrid.listGap private val SP = "\u3000" + private val ML = getMouseButton(App.getConfigInt("config_mouseprimary")) + private val MR = getMouseButton(App.getConfigInt("config_mousesecondary")) + private val MW = getMouseButton(2) private val controlHelpLeft: String get() = if (App.environment == RunningEnvironment.PC) "${getKeycapPC(ControlPresets.getKey("control_key_inventory"))} ${Lang["GAME_ACTION_CLOSE"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mouseprimary"))} ${Lang["GAME_ACTION_TAKE_ALL_CONT"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mousesecondary"))} ${Lang["GAME_ACTION_TAKE_ONE_CONT"]}" + "$ML ${Lang["GAME_ACTION_TAKE_ALL_CONT"]}$SP" + + "$MW$MR ${Lang["GAME_ACTION_TAKE_ONE_CONT"]}" else "${App.gamepadLabelStart} ${Lang["GAME_ACTION_CLOSE"]}" private val controlHelpRight: String get() = if (App.environment == RunningEnvironment.PC) - "${getMouseButton(App.getConfigInt("config_mouseprimary"))} ${Lang["GAME_ACTION_PUT_ALL_CONT"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mousesecondary"))} ${Lang["GAME_ACTION_PUT_ONE_CONT"]}" + "$ML ${Lang["GAME_ACTION_PUT_ALL_CONT"]}$SP" + + "$MW$MR ${Lang["GAME_ACTION_PUT_ONE_CONT"]}" else "${App.gamepadLabelStart} ${Lang["GAME_ACTION_CLOSE"]}" private val controlHelpRightTwoRows: String get() = if (App.environment == RunningEnvironment.PC) - "${getMouseButton(App.getConfigInt("config_mouseprimary"))} ${Lang["GAME_ACTION_PUT_ALL"]}$SP" + - "${getMouseButton(App.getConfigInt("config_mousesecondary"))} ${Lang["GAME_ACTION_PUT_ONE"]}" + "$ML ${Lang["GAME_ACTION_PUT_ALL"]}$SP" + + "$MW$MR ${Lang["GAME_ACTION_PUT_ONE"]}" else "${App.gamepadLabelStart} ${Lang["GAME_ACTION_CLOSE"]}" diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt b/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt index 08eaf8162..cc9b123f3 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt @@ -46,6 +46,9 @@ class UITemplateHalfInventory( internal var itemListTouchDownFun = { gameItem: GameItem?, amount: Long, mouseButton: Int, itemExtraInfo: Any?, theButton: UIItemInventoryCellBase -> /* crickets */ } + internal var itemListWheelFun = { gameItem: GameItem?, amount: Long, scrollX: Float, scrollY: Float, itemExtraInfo: Any?, theButton: UIItemInventoryCellBase -> + /* crickets */ + } init { itemList = UIItemInventoryItemGrid( @@ -58,7 +61,8 @@ class UITemplateHalfInventory( drawWallet = false, highlightEquippedItem = false, keyDownFun = { a, b, c, d, e -> itemListKeyDownFun(a, b, c, d, e) }, - touchDownFun = { a, b, c, d, e -> itemListTouchDownFun.invoke(a, b, c, d, e) } + touchDownFun = { a, b, c, d, e -> itemListTouchDownFun.invoke(a, b, c, d, e) }, + wheelFun = { a, b, c, d, e, f -> itemListWheelFun.invoke(a, b, c, d, e, f)} ) // make grid mode buttons work together // itemListPlayer.gridModeButtons[0].clickOnceListener = { _,_ -> setCompact(false) } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt index 9cfeed0f1..4ab555d96 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt @@ -96,6 +96,7 @@ class UIWorldPortalCargo(val full: UIWorldPortal) : UICanvas(), HasInventory { drawScrollOnRightside = false, drawWallet = false, keyDownFun = { _, _, _, _, _ -> Unit }, + wheelFun = { _, _, _, _, _, _ -> }, touchDownFun = { gameItem, amount, button, _, _ -> if (button == App.getConfigInt("config_mouseprimary")) { if (gameItem != null) { @@ -118,6 +119,7 @@ class UIWorldPortalCargo(val full: UIWorldPortal) : UICanvas(), HasInventory { drawScrollOnRightside = true, drawWallet = false, keyDownFun = { _, _, _, _, _ -> Unit }, + wheelFun = { _, _, _, _, _, _ -> }, touchDownFun = { gameItem, amount, button, _, _ -> if (button == App.getConfigInt("config_mouseprimary")) { if (gameItem != null) { diff --git a/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt b/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt index 7bbe974f6..4e79fb668 100644 --- a/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt +++ b/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt @@ -29,6 +29,7 @@ class UIItemInventoryElemSimple( val drawBackOnNull: Boolean = true, 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 + wheelFun: (GameItem?, Long, Float, Float, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, scroll x, scroll y, extra info, self extraInfo: Any? = null, highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting colourTheme: InventoryCellColourTheme = defaultInventoryCellTheme, @@ -36,7 +37,7 @@ class UIItemInventoryElemSimple( var emptyCellIcon: TextureRegion? = null, // icon to draw when the cell is empty var emptyCellIconColour: Color = Color(0xdddddd7f.toInt()), val updateOnNull: Boolean = false, -) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem, colourTheme) { +) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, wheelFun, extraInfo, highlightEquippedItem, colourTheme) { companion object { val height = UIItemInventoryElemWide.height diff --git a/src/net/torvald/terrarum/ui/UIItemInventoryElemWide.kt b/src/net/torvald/terrarum/ui/UIItemInventoryElemWide.kt index fa6dc7a7d..27011c053 100644 --- a/src/net/torvald/terrarum/ui/UIItemInventoryElemWide.kt +++ b/src/net/torvald/terrarum/ui/UIItemInventoryElemWide.kt @@ -33,11 +33,12 @@ class UIItemInventoryElemWide( val drawBackOnNull: Boolean = true, 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 + wheelFun: (GameItem?, Long, Float, Float, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, scroll x, scroll y, extra info, self extraInfo: Any? = null, highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting colourTheme: InventoryCellColourTheme = UIItemInventoryCellCommonRes.defaultInventoryCellTheme, var showItemCount: Boolean = true, -) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem, colourTheme) { +) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, wheelFun, extraInfo, highlightEquippedItem, colourTheme) { companion object { val height = 48