From fb9640e615853f2327b8e31842b67d87615e730d Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 13 Jan 2024 05:08:40 +0900 Subject: [PATCH] jukebox ui wip --- assets/mods/basegame/gui/jukebox_caticons.tga | 3 + assets/mods/basegame/items/itemid.csv | 2 + .../gameactors/FixtureJukebox.kt | 4 +- .../modulebasegame/gameitems/ItemFileRef.kt | 64 ++++++++++ .../modulebasegame/gameitems/MusicDisc.kt | 15 +++ .../ui/InventoryTransactionNegotiator.kt | 6 +- .../terrarum/modulebasegame/ui/UICrafting.kt | 5 +- .../modulebasegame/ui/UIInventoryCells.kt | 1 - .../ui/UIItemCraftingCandidateGrid.kt | 2 +- .../ui/UIItemInventoryItemGrid.kt | 9 -- .../terrarum/modulebasegame/ui/UIJukebox.kt | 84 +++++++++++++ .../modulebasegame/ui/UIJukeboxInventory.kt | 112 ++++++++++++++++++ .../modulebasegame/ui/UIStorageChest.kt | 69 +++++------ ...nventory.kt => UITemplateHalfInventory.kt} | 38 ++++-- .../modulebasegame/ui/UIWorldPortalCargo.kt | 6 +- src/net/torvald/terrarum/ui/UIItemCatBar.kt | 57 +++++---- .../terrarum/ui/UIItemInventoryElemSimple.kt | 6 +- .../terrarum/ui/UIItemInventoryElemWide.kt | 6 +- .../torvald/terrarum/ui/UITemplateCatBar.kt | 3 +- 19 files changed, 389 insertions(+), 103 deletions(-) create mode 100644 assets/mods/basegame/gui/jukebox_caticons.tga create mode 100644 src/net/torvald/terrarum/modulebasegame/gameitems/ItemFileRef.kt create mode 100644 src/net/torvald/terrarum/modulebasegame/gameitems/MusicDisc.kt create mode 100644 src/net/torvald/terrarum/modulebasegame/ui/UIJukebox.kt create mode 100644 src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt rename src/net/torvald/terrarum/modulebasegame/ui/{CraftingPlayerInventory.kt => UITemplateHalfInventory.kt} (66%) diff --git a/assets/mods/basegame/gui/jukebox_caticons.tga b/assets/mods/basegame/gui/jukebox_caticons.tga new file mode 100644 index 000000000..6ebe9c8f5 --- /dev/null +++ b/assets/mods/basegame/gui/jukebox_caticons.tga @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4282512c65c0d1d845709dde7d2018e87601e27762b89e089a303e44f90efa74 +size 3218 diff --git a/assets/mods/basegame/items/itemid.csv b/assets/mods/basegame/items/itemid.csv index d5c702d19..74e705f5e 100644 --- a/assets/mods/basegame/items/itemid.csv +++ b/assets/mods/basegame/items/itemid.csv @@ -72,4 +72,6 @@ id;classname 320;net.torvald.terrarum.modulebasegame.gameitems.ItemWorldPortal +65536;net.torvald.terrarum.modulebasegame.gameitems.MusicDisc01 + 999999;net.torvald.terrarum.modulebasegame.gameitems.ItemTapestry diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureJukebox.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureJukebox.kt index 7ef2c4a49..943669862 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureJukebox.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureJukebox.kt @@ -22,6 +22,7 @@ import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.modulebasegame.MusicContainer import net.torvald.terrarum.modulebasegame.TerrarumMusicGovernor import net.torvald.terrarum.modulebasegame.gameitems.FixtureItemBase +import net.torvald.terrarum.modulebasegame.ui.UIJukebox import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack import org.dyn4j.geometry.Vector2 @@ -32,7 +33,8 @@ class FixtureJukebox : Electric { constructor() : super( BlockBox(BlockBox.NO_COLLISION, 2, 3), - nameFun = { Lang["ITEM_JUKEBOX"] } + nameFun = { Lang["ITEM_JUKEBOX"] }, + mainUI = UIJukebox() ) @Transient private var discCurrentlyPlaying: Int? = null diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/ItemFileRef.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemFileRef.kt new file mode 100644 index 000000000..617cd8c09 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemFileRef.kt @@ -0,0 +1,64 @@ +package net.torvald.terrarum.modulebasegame.gameitems + +import net.torvald.terrarum.gameitems.GameItem +import net.torvald.terrarum.gameitems.ItemID +import java.io.File +import java.util.UUID + +/** + * The base class for anything that holds a file like disc/disks + * + * Created by minjaesong on 2024-01-13. + */ +open class ItemFileRef(originalID: ItemID) : GameItem(originalID) { + + open var uuid: UUID = UUID(0, 0) + + /** + * String of path within the module (if not refIsShared), or path under savedir/Shared/ + */ + open var refPath: String = "" + + /** + * Module name (if not refIsShared), or empty string + */ + open var refModuleName: String = "" + + /** + * If the file must be found under `savedir/Shared` + */ + open var refIsShared: Boolean = false + + @Transient open lateinit var ref: File + + /** + * Application-defined. + * + * For Terrarum: + * - "music_disc" for music discs used by Jukebox + * + * For dwarventech: + * - "floppy_oem" (oem: for "commercial" software) + * - "floppy_small" + * - "floppy_mid" + * - "floppy_large" + * - "floppy_debug" + * - "floppy_homebrew" + * - "harddisk_small" + * - "harddisk_mid" + * - "harddisk_large" + * - "harddisk_debug" + * - "eeprom_bios" (bios chips only) + * - "eeprom_64k" (extra rom for custom motherboards) + * + */ + open var mediumIdentifier = "" + + + override var baseMass = 1.0 + override var baseToolSize: Double? = null + override var inventoryCategory = Category.GENERIC + override val isDynamic = true + override val materialId = "" + override var equipPosition = EquipPosition.HAND_GRIP +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/MusicDisc.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/MusicDisc.kt new file mode 100644 index 000000000..16332ea2c --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/MusicDisc.kt @@ -0,0 +1,15 @@ +package net.torvald.terrarum.modulebasegame.gameitems + +import net.torvald.terrarum.ModMgr +import net.torvald.terrarum.gameitems.ItemID + +/** + * Created by minjaesong on 2024-01-13. + */ +class MusicDisc01(originalID: ItemID) : ItemFileRef(originalID) { + override var refPath = "audio/music/discs/01 Thousands of Shards.ogg" + override var refModuleName = "basegame" + override val isDynamic = false + @Transient override var ref = ModMgr.getFile(refModuleName, refPath) + override var mediumIdentifier = "music_disc" +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/InventoryTransactionNegotiator.kt b/src/net/torvald/terrarum/modulebasegame/ui/InventoryTransactionNegotiator.kt index 6babac6e9..133d35ca1 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/InventoryTransactionNegotiator.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/InventoryTransactionNegotiator.kt @@ -10,8 +10,8 @@ import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory 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... */ + /** Called when the item is being accepted from the player and moved to the right inventory (object), slot (UI), etc... */ abstract fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Long = 1L) - /** Rejects item and perhaps returns it back to the player, or make explosion, etc... */ - abstract fun reject(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Long = 1L) + /** Called when the item is being removed from the fixture to returns it back to the player, or make explosion, etc... */ + abstract fun refund(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Long = 1L) } \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt b/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt index 53ac014b1..f30fb3539 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt @@ -35,7 +35,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { override var width = App.scr.width override var height = App.scr.height - private val playerThings = CraftingPlayerInventory(full, this).also { + private val playerThings = UITemplateHalfInventory(this, false).also { it.itemListTouchDownFun = { gameItem, _, _, _, _ -> recipeClicked?.let { recipe -> gameItem?.let { gameItem -> val itemID = gameItem.dynamicID // don't rely on highlightedness of the button to determine the item on the button is the selected @@ -77,7 +77,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { // TODO() } - override fun reject(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Long) { + override fun refund(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Long) { // TODO() } } @@ -126,7 +126,6 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { // ingredient list itemListIngredients = UIItemInventoryItemGrid( this, - catBar, { ingredients }, thisOffsetX, thisOffsetY + LAST_LINE_IN_GRID, diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt index e157a38c1..eefc834f3 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt @@ -44,7 +44,6 @@ internal class UIInventoryCells( internal val itemList: UIItemInventoryItemGrid = UIItemInventoryItemGrid( full, - full.catBar, { full.actor.inventory }, INVENTORY_CELLS_OFFSET_X(), INVENTORY_CELLS_OFFSET_Y(), diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt index 091a659ba..9e4c9598f 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt @@ -21,7 +21,7 @@ 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, catBar, + parentUI, { TODO() /* UNUSED and MUST NOT BE USED! */ }, initialX, initialY, horizontalCells, verticalCells, diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt index 4749b55bb..ce4e6f141 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryItemGrid.kt @@ -32,7 +32,6 @@ import kotlin.math.floor */ open class UIItemInventoryItemGrid( parentUI: UICanvas, - val catBar: UIItemCatBar, var getInventory: () -> FixtureInventory, // when you're going to display List of Craftables, you could implement a Delegator...? Or just build a virtual inventory initialX: Int, initialY: Int, @@ -189,10 +188,6 @@ open class UIItemInventoryItemGrid( parentUI = inventoryUI, initialX = this.posX + (UIItemInventoryElemSimple.height + listGap) * (it % horizontalCells), initialY = this.posY + (UIItemInventoryElemSimple.height + listGap) * (it / horizontalCells), - item = null, - amount = UIItemInventoryElemWide.UNIQUE_ITEM_HAS_NO_AMOUNT, - itemImage = null, - drawBackOnNull = true, keyDownFun = keyDownFun, touchDownFun = touchDownFun, highlightEquippedItem = highlightEquippedItem, @@ -209,10 +204,6 @@ open class UIItemInventoryItemGrid( initialX = this.posX + (largeListWidth + listGap) * (it % itemListColumnCount), initialY = this.posY + (UIItemInventoryElemWide.height + listGap) * (it / itemListColumnCount), width = largeListWidth, - item = null, - amount = UIItemInventoryElemWide.UNIQUE_ITEM_HAS_NO_AMOUNT, - itemImage = null, - drawBackOnNull = true, keyDownFun = keyDownFun, touchDownFun = touchDownFun, highlightEquippedItem = highlightEquippedItem, diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIJukebox.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIJukebox.kt new file mode 100644 index 000000000..98017c545 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIJukebox.kt @@ -0,0 +1,84 @@ +package net.torvald.terrarum.modulebasegame.ui + +import com.badlogic.gdx.graphics.OrthographicCamera +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import net.torvald.terrarum.* +import net.torvald.terrarum.ui.* +import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack + +/** + * Created by minjaesong on 2024-01-13. + */ +class UIJukebox : UICanvas( + toggleKeyLiteral = "control_key_inventory", + toggleButtonLiteral = "control_gamepad_start", +) { + + init { + CommonResourcePool.addToLoadingList("basegame-gui-jukebox_caticons") { + TextureRegionPack(ModMgr.getGdxFile("basegame", "gui/jukebox_caticons.tga"), 20, 20) + } + CommonResourcePool.loadAll() + } + + override var width = Toolkit.drawWidth + override var height = App.scr.height + + private val catbar = UITemplateCatBar( + this, false, + CommonResourcePool.getAsTextureRegionPack("basegame-gui-jukebox_caticons"), + intArrayOf(0, 1), + emptyList(), + listOf({ "" }, { "" }) + ) + + private val transitionalSonglistPanel = UIJukeboxSonglistPanel(this) + private val transitionalDiscInventory = UIJukeboxInventory(this) + + private val transitionPanel = UIItemHorizontalFadeSlide( + this, + 0, 0, width, height, 0f, + listOf(transitionalSonglistPanel), + listOf(transitionalDiscInventory) + ) + + init { + addUIitem(catbar) + addUIitem(transitionPanel) + } + + private var openingClickLatched = false + + override fun show() { + openingClickLatched = Terrarum.mouseDown + transitionPanel.show() + UIItemInventoryItemGrid.tooltipShowing.clear() + INGAME.setTooltipMessage(null) + } + + override fun hide() { + transitionPanel.hide() + } + + override fun updateUI(delta: Float) { + uiItems.forEach { it.update(delta) } + + if (openingClickLatched && !Terrarum.mouseDown) openingClickLatched = false + } + + override fun renderUI(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) { + UIInventoryFull.drawBackground(batch, 1f) + uiItems.forEach { it.render(frameDelta, batch, camera) } + } + + override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { + if (!openingClickLatched) { + return super.touchDown(screenX, screenY, pointer, button) + } + return false + } + + override fun dispose() { + } + +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt new file mode 100644 index 000000000..322e601ac --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt @@ -0,0 +1,112 @@ +package net.torvald.terrarum.modulebasegame.ui + +import com.badlogic.gdx.graphics.OrthographicCamera +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import net.torvald.terrarum.App +import net.torvald.terrarum.gameitems.GameItem +import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory +import net.torvald.terrarum.modulebasegame.gameitems.ItemFileRef +import net.torvald.terrarum.ui.Toolkit +import net.torvald.terrarum.ui.UICanvas +import net.torvald.terrarum.ui.UIItemInventoryElemSimple +import net.torvald.terrarum.ui.UIItemInventoryElemWide + +/** + * Created by minjaesong on 2024-01-13. + */ +class UIJukeboxInventory(val parent: UICanvas) : UICanvas(), HasInventory { + + override var width = Toolkit.drawWidth + override var height = App.scr.height + + private val negotiator = object : InventoryTransactionNegotiator() { + override fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Long) { + if (item is ItemFileRef && item.mediumIdentifier == "music_disc") { + player.remove(item, amount) + fixture.add(item, amount) + } + } + + override fun refund(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Long) { + fixture.remove(item, amount) + player.add(item, amount) + } + + } + + private val thisInventory = FixtureInventory() + + private var halfSlotOffset = (UIItemInventoryElemSimple.height + UIItemInventoryItemGrid.listGap) / 2 + private val thisOffsetX = UIInventoryFull.INVENTORY_CELLS_OFFSET_X() + UIItemInventoryElemSimple.height + UIItemInventoryItemGrid.listGap - halfSlotOffset + private val thisOffsetY = UIInventoryFull.INVENTORY_CELLS_OFFSET_Y() + + private val fixtureInventoryCells = (0..7).map { + UIItemInventoryElemWide(this, + thisOffsetX, thisOffsetY + (UIItemInventoryElemSimple.height + UIItemInventoryItemGrid.listGap) * it, + 6 * UIItemInventoryElemSimple.height + 5 * UIItemInventoryItemGrid.listGap, + keyDownFun = { _, _, _, _, _ -> Unit }, + touchDownFun = { gameItem, amount, button, _, _ -> + if (button == App.getConfigInt("config_mouseprimary")) { + if (gameItem != null) { + negotiator.refund(getFixtureInventory(), getPlayerInventory(), gameItem, amount) + } + } + } + ) + } + private val playerInventoryUI = UITemplateHalfInventory(this, false) + + init { + fixtureInventoryCells.forEach { + addUIitem(it) + } + addUIitem(playerInventoryUI) + } + + override fun updateUI(delta: Float) { + uiItems.forEach { it.update(delta) } + } + + override fun renderUI(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) { + uiItems.forEach { it.render(frameDelta, batch, camera) } + } + + override fun dispose() { + } + + override fun getNegotiator() = negotiator + + override fun getFixtureInventory(): FixtureInventory { + TODO() + } + + override fun getPlayerInventory(): FixtureInventory { + TODO() + } + + +} + + + +class UIJukeboxSonglistPanel(val parent: UICanvas) : UICanvas() { + + override var width = Toolkit.drawWidth + override var height = App.scr.height + + + + override fun updateUI(delta: Float) { + uiItems.forEach { it.update(delta) } + } + + override fun renderUI(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) { + uiItems.forEach { it.render(frameDelta, batch, camera) } + } + + + override fun dispose() { + } + +} + diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt index 899ed0eb8..0b8235963 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt @@ -34,7 +34,7 @@ internal class UIStorageChest : UICanvas( fixture.add(item, amount) } - override fun reject(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Long) { + override fun refund(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Long) { fixture.remove(item, amount) player.add(item, amount) } @@ -45,8 +45,8 @@ internal class UIStorageChest : UICanvas( override fun getPlayerInventory(): FixtureInventory = INGAME.actorNowPlaying!!.inventory private val catBar: UIItemCatBar - private val itemListChest: UIItemInventoryItemGrid - private val itemListPlayer: UIItemInventoryItemGrid + private val itemListChest: UITemplateHalfInventory + private val itemListPlayer: UITemplateHalfInventory private var encumbrancePerc = 0f private var isEncumbered = false @@ -93,40 +93,27 @@ internal class UIStorageChest : UICanvas( ) catBar.selectionChangeListener = { old, new -> itemListUpdate() } - itemListChest = UIItemInventoryItemGrid( - this, - catBar, - { getFixtureInventory() }, - Toolkit.hdrawWidth - getWidthOfCells(6) - halfSlotOffset, - UIInventoryFull.INVENTORY_CELLS_OFFSET_Y(), - 6, UIInventoryFull.CELLS_VRT, - drawScrollOnRightside = false, - drawWallet = false, - keyDownFun = { _, _, _, _, _ -> Unit }, - touchDownFun = { gameItem, amount, button, _, _ -> + + + itemListChest = UITemplateHalfInventory(this, true) { getFixtureInventory() }.also { + it.itemListKeyDownFun = { _, _, _, _, _ -> Unit } + it.itemListTouchDownFun = { gameItem, amount, button, _, _ -> if (button == App.getConfigInt("config_mouseprimary")) { if (gameItem != null) { - negotiator.reject(getFixtureInventory(), getPlayerInventory(), gameItem, amount) + negotiator.refund(getFixtureInventory(), getPlayerInventory(), gameItem, amount) } itemListUpdate() } } - ) + } // make grid mode buttons work together - itemListChest.navRemoCon.listButtonListener = { _,_ -> setCompact(false) } - itemListChest.navRemoCon.gridButtonListener = { _,_ -> setCompact(true) } + itemListChest.itemList.navRemoCon.listButtonListener = { _,_ -> setCompact(false) } + itemListChest.itemList.navRemoCon.gridButtonListener = { _,_ -> setCompact(true) } - itemListPlayer = UIItemInventoryItemGrid( - this, - catBar, - { INGAME.actorNowPlaying!!.inventory }, // literally a player's inventory - Toolkit.hdrawWidth + halfSlotOffset, - UIInventoryFull.INVENTORY_CELLS_OFFSET_Y(), - 6, UIInventoryFull.CELLS_VRT, - drawScrollOnRightside = true, - drawWallet = false, - keyDownFun = { _, _, _, _, _ -> Unit }, - touchDownFun = { gameItem, amount, button, _, _ -> + + itemListPlayer = UITemplateHalfInventory(this, false).also { + it.itemListKeyDownFun = { _, _, _, _, _ -> Unit } + it.itemListTouchDownFun = { gameItem, amount, button, _, _ -> if (button == App.getConfigInt("config_mouseprimary")) { if (gameItem != null) { negotiator.accept(getPlayerInventory(), getFixtureInventory(), gameItem, amount) @@ -134,9 +121,9 @@ internal class UIStorageChest : UICanvas( itemListUpdate() } } - ) - itemListPlayer.navRemoCon.listButtonListener = { _,_ -> setCompact(false) } - itemListPlayer.navRemoCon.gridButtonListener = { _,_ -> setCompact(true) } + } + itemListPlayer.itemList.navRemoCon.listButtonListener = { _,_ -> setCompact(false) } + itemListPlayer.itemList.navRemoCon.gridButtonListener = { _,_ -> setCompact(true) } handler.allowESCtoClose = true @@ -148,7 +135,7 @@ internal class UIStorageChest : UICanvas( private var openingClickLatched = false override fun show() { - itemListPlayer.getInventory = { INGAME.actorNowPlaying!!.inventory } + itemListPlayer.itemList.getInventory = { INGAME.actorNowPlaying!!.inventory } itemListUpdate() @@ -167,16 +154,16 @@ internal class UIStorageChest : UICanvas( } private fun setCompact(yes: Boolean) { - itemListChest.isCompactMode = yes - itemListChest.navRemoCon.gridModeButtons[0].highlighted = !yes - itemListChest.navRemoCon.gridModeButtons[1].highlighted = yes - itemListChest.itemPage = 0 + itemListChest.itemList.isCompactMode = yes + itemListChest.itemList.navRemoCon.gridModeButtons[0].highlighted = !yes + itemListChest.itemList.navRemoCon.gridModeButtons[1].highlighted = yes + itemListChest.itemList.itemPage = 0 itemListChest.rebuild(catBar.catIconsMeaning[catBar.selectedIndex]) - itemListPlayer.isCompactMode = yes - itemListPlayer.navRemoCon.gridModeButtons[0].highlighted = !yes - itemListPlayer.navRemoCon.gridModeButtons[1].highlighted = yes - itemListPlayer.itemPage = 0 + itemListPlayer.itemList.isCompactMode = yes + itemListPlayer.itemList.navRemoCon.gridModeButtons[0].highlighted = !yes + itemListPlayer.itemList.navRemoCon.gridModeButtons[1].highlighted = yes + itemListPlayer.itemList.itemPage = 0 itemListPlayer.rebuild(catBar.catIconsMeaning[catBar.selectedIndex]) itemListUpdate() diff --git a/src/net/torvald/terrarum/modulebasegame/ui/CraftingPlayerInventory.kt b/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt similarity index 66% rename from src/net/torvald/terrarum/modulebasegame/ui/CraftingPlayerInventory.kt rename to src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt index b4dfaffa7..548765740 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/CraftingPlayerInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt @@ -1,18 +1,29 @@ package net.torvald.terrarum.modulebasegame.ui +import com.badlogic.gdx.graphics.OrthographicCamera +import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.* import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.ItemID 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.ui.* /** - * Suite of objects for showing player inventory for various crafting UIs. + * Suite of objects for showing player inventory for various UIs involving inventory management. + * + * @param parent the parent UI + * @param drawOnLeft if the inventory should be drawn on the left panel + * @param getInventoryFun function that returns an inventory. Default value is for player's * * Created by minjaesong on 2023-10-04. */ -class CraftingPlayerInventory(val full: UIInventoryFull, val crafting: UICanvas) : UITemplate(crafting) { +class UITemplateHalfInventory( + parent: UICanvas, + drawOnLeft: Boolean, + getInventoryFun: () -> FixtureInventory = { INGAME.actorNowPlaying!!.inventory } +) : UITemplate(parent) { val itemList: UIItemInventoryItemGrid @@ -30,13 +41,12 @@ class CraftingPlayerInventory(val full: UIInventoryFull, val crafting: UICanvas) init { itemList = UIItemInventoryItemGrid( - crafting, - full.catBar, - { INGAME.actorNowPlaying!!.inventory }, // literally a player's inventory - thisOffsetX2, + parent, + getInventoryFun, + if (drawOnLeft) thisOffsetX else thisOffsetX2, thisOffsetY, 6, UIInventoryFull.CELLS_VRT, - drawScrollOnRightside = true, + drawScrollOnRightside = !drawOnLeft, drawWallet = false, highlightEquippedItem = false, keyDownFun = { a, b, c, d, e -> itemListKeyDownFun(a, b, c, d, e) }, @@ -68,6 +78,20 @@ class CraftingPlayerInventory(val full: UIInventoryFull, val crafting: UICanvas) itemList.getInventory = getter } + inline fun update(delta: Float) = itemList.update(delta) + inline fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) = itemList.render(frameDelta, batch, camera) + + var posX: Int + get() = itemList.posX + set(value) { itemList.posX = value } + var posY: Int + get() = itemList.posY + set(value) { itemList.posY = value } + val width: Int + get() = itemList.width + val height: Int + get() = itemList.height + override fun getUIitems(): List { return listOf(itemList) } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt index 1f2aeaf70..ab53ee462 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt @@ -27,7 +27,7 @@ class UIWorldPortalCargo(val full: UIWorldPortal) : UICanvas(), HasInventory { fixture.add(item, amount) } - override fun reject(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Long) { + override fun refund(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Long) { fixture.remove(item, amount) player.add(item, amount) } @@ -88,7 +88,6 @@ class UIWorldPortalCargo(val full: UIWorldPortal) : UICanvas(), HasInventory { catBar.selectionChangeListener = { old, new -> itemListUpdate() } itemListChest = UIItemInventoryItemGrid( this, - catBar, { getFixtureInventory() }, Toolkit.hdrawWidth - UIInventoryFull.getWidthOfCells(6) - halfSlotOffset, UIInventoryFull.INVENTORY_CELLS_OFFSET_Y(), @@ -99,7 +98,7 @@ class UIWorldPortalCargo(val full: UIWorldPortal) : UICanvas(), HasInventory { touchDownFun = { gameItem, amount, button, _, _ -> if (button == App.getConfigInt("config_mouseprimary")) { if (gameItem != null) { - negotiator.reject(getFixtureInventory(), getPlayerInventory(), gameItem, amount) + negotiator.refund(getFixtureInventory(), getPlayerInventory(), gameItem, amount) } itemListUpdate() } @@ -111,7 +110,6 @@ class UIWorldPortalCargo(val full: UIWorldPortal) : UICanvas(), HasInventory { itemListPlayer = UIItemInventoryItemGrid( this, - catBar, { INGAME.actorNowPlaying!!.inventory }, // literally a player's inventory Toolkit.hdrawWidth + halfSlotOffset, UIInventoryFull.INVENTORY_CELLS_OFFSET_Y(), diff --git a/src/net/torvald/terrarum/ui/UIItemCatBar.kt b/src/net/torvald/terrarum/ui/UIItemCatBar.kt index 24b4e1c9d..0a23f31b1 100644 --- a/src/net/torvald/terrarum/ui/UIItemCatBar.kt +++ b/src/net/torvald/terrarum/ui/UIItemCatBar.kt @@ -44,7 +44,7 @@ class UIItemCatBar( // val selectedIcon: Int // get() = catArrangement[selectedIndex] - private val sideButtons: Array + private lateinit var sideButtons: Array // set up all the buttons init { @@ -71,30 +71,32 @@ class UIItemCatBar( } - // side buttons - // NOTE: < > arrows must "highlightable = false"; "true" otherwise - // determine gaps: hacky way exploiting that we already know the catbar is always at the c of the ui - val relativeStartX = posX - (uiInternalWidth - width) / 2 - val sideButtonsGap = (((uiInternalWidth - width) / 2) - 2f * catIcons.tileW) / 3f - val iconIndex = arrayOf( - catIcons.get(9,1), - catIcons.get(16,0), - catIcons.get(17,0), - catIcons.get(13,0) - ) + if (showSideButtons) { + + // side buttons + // NOTE: < > arrows must "highlightable = false"; "true" otherwise + // determine gaps: hacky way exploiting that we already know the catbar is always at the c of the ui + val relativeStartX = posX - (uiInternalWidth - width) / 2 + val sideButtonsGap = (((uiInternalWidth - width) / 2) - 2f * catIcons.tileW) / 3f + val iconIndex = arrayOf( + catIcons.get(9, 1), + catIcons.get(16, 0), + catIcons.get(17, 0), + catIcons.get(13, 0) + ) - //println("[UIItemInventoryCatBar] relativeStartX: $relativeStartX") - //println("[UIItemInventoryCatBar] posX: $posX") + //println("[UIItemInventoryCatBar] relativeStartX: $relativeStartX") + //println("[UIItemInventoryCatBar] posX: $posX") - sideButtons = Array(iconIndex.size) { index -> - val iconPosX = if (index < 2) - (relativeStartX + sideButtonsGap + (sideButtonsGap + catIcons.tileW) * index).roundToInt() - else - (relativeStartX + width + 2 * sideButtonsGap + (sideButtonsGap + catIcons.tileW) * index).roundToInt() - val iconPosY = 0 + sideButtons = Array(iconIndex.size) { index -> + val iconPosX = if (index < 2) + (relativeStartX + sideButtonsGap + (sideButtonsGap + catIcons.tileW) * index).roundToInt() + else + (relativeStartX + width + 2 * sideButtonsGap + (sideButtonsGap + catIcons.tileW) * index).roundToInt() + val iconPosY = 0 - UIItemImageButton( + UIItemImageButton( inventoryUI, iconIndex[index], activeBackCol = Color(0), @@ -106,7 +108,8 @@ class UIItemCatBar( inactiveCol = if (index == 0 || index == 3) Color.WHITE else Color(0xffffff7f.toInt()), activeCol = if (index == 0 || index == 3) Toolkit.Theme.COL_MOUSE_UP else Color(0xffffff7f.toInt()), highlightable = (index == 0 || index == 3) - ) + ) + } } } @@ -136,9 +139,9 @@ class UIItemCatBar( if (n !in 0..2) throw IllegalArgumentException("$n") selectedPanel = n - sideButtons[0].highlighted = (n == 0) + if (showSideButtons) sideButtons[0].highlighted = (n == 0) mainButtons[selectedIndex].highlighted = (n == 1) - sideButtons[3].highlighted = (n == 2) + if (showSideButtons) sideButtons[3].highlighted = (n == 2) } @@ -214,8 +217,10 @@ class UIItemCatBar( if (selectedPanel == 1) { btn.highlighted = (index == selectedIndex) // forcibly highlight if this.highlighted != null - sideButtons[0].highlighted = false - sideButtons[3].highlighted = false + if (showSideButtons) { + sideButtons[0].highlighted = false + sideButtons[3].highlighted = false + } } } diff --git a/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt b/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt index 2d92078f0..d7671f21a 100644 --- a/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt +++ b/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt @@ -22,9 +22,9 @@ class UIItemInventoryElemSimple( parentUI: UICanvas, initialX: Int, initialY: Int, - override var item: GameItem?, - override var amount: Long, - override var itemImage: TextureRegion?, + override var item: GameItem? = null, + override var amount: Long = UIItemInventoryElemWide.UNIQUE_ITEM_HAS_NO_AMOUNT, + override var itemImage: TextureRegion? = null, override var quickslot: Int? = null, override var equippedSlot: Int? = null, // remnants of wide cell displaying slot number and highlighting; in this style of cell this field only determines highlightedness at render val drawBackOnNull: Boolean = true, diff --git a/src/net/torvald/terrarum/ui/UIItemInventoryElemWide.kt b/src/net/torvald/terrarum/ui/UIItemInventoryElemWide.kt index 3060b89f4..8db49bd5d 100644 --- a/src/net/torvald/terrarum/ui/UIItemInventoryElemWide.kt +++ b/src/net/torvald/terrarum/ui/UIItemInventoryElemWide.kt @@ -24,9 +24,9 @@ class UIItemInventoryElemWide( initialX: Int, initialY: Int, override val width: Int, - override var item: GameItem?, - override var amount: Long, - override var itemImage: TextureRegion?, + override var item: GameItem? = null, + override var amount: Long = UIItemInventoryElemWide.UNIQUE_ITEM_HAS_NO_AMOUNT, + override var itemImage: TextureRegion? = null, override var quickslot: Int? = null, override var equippedSlot: Int? = null, val drawBackOnNull: Boolean = true, diff --git a/src/net/torvald/terrarum/ui/UITemplateCatBar.kt b/src/net/torvald/terrarum/ui/UITemplateCatBar.kt index 94d9609f4..9b02c371a 100644 --- a/src/net/torvald/terrarum/ui/UITemplateCatBar.kt +++ b/src/net/torvald/terrarum/ui/UITemplateCatBar.kt @@ -12,6 +12,7 @@ import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack */ class UITemplateCatBar( parent: UICanvas, + showSidebuttons: Boolean, catIcons: TextureRegionPack = CommonResourcePool.getAsTextureRegionPack("inventory_category"), catArrangement: IntArray, // icon order @@ -26,7 +27,7 @@ class UITemplateCatBar( 42 - UIInventoryFull.YPOS_CORRECTION + (App.scr.height - UIInventoryFull.internalHeight) / 2, UIInventoryFull.internalWidth, UIInventoryFull.catBarWidth, - true, + showSidebuttons, catIcons, catArrangement, catIconsMeaning, catIconsLabels )