From 3214f11375c1a517a0a496fda9d0ca9dd2e708a8 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Mon, 29 Jan 2024 18:51:53 +0900 Subject: [PATCH] uiitem and template refactoring; new smelter ui wip --- assets/mods/basegame/crafting/fixtures.json | 6 + assets/mods/basegame/gui/smelter_icons.tga | 3 + .../gameactors/FixtureSmelterBasic.kt | 3 +- .../terrarum/modulebasegame/ui/UICrafting.kt | 15 +- .../ui/UIItemInventoryEquippedView.kt | 24 +-- .../modulebasegame/ui/UIJukeboxInventory.kt | 9 +- .../modulebasegame/ui/UISmelterBasic.kt | 174 ++++++++++++++++++ .../modulebasegame/ui/UIStorageChest.kt | 9 +- .../ui/UITemplateHalfInventory.kt | 22 ++- src/net/torvald/terrarum/ui/UICanvas.kt | 7 +- src/net/torvald/terrarum/ui/UIItem.kt | 26 +-- .../terrarum/ui/UIItemInventoryElemSimple.kt | 14 ++ src/net/torvald/terrarum/ui/UITemplate.kt | 36 +++- .../torvald/terrarum/ui/UITemplateCatBar.kt | 6 + 14 files changed, 290 insertions(+), 64 deletions(-) create mode 100644 assets/mods/basegame/gui/smelter_icons.tga create mode 100644 src/net/torvald/terrarum/modulebasegame/ui/UISmelterBasic.kt diff --git a/assets/mods/basegame/crafting/fixtures.json b/assets/mods/basegame/crafting/fixtures.json index 1e51fa9b4..26b2ef6c5 100644 --- a/assets/mods/basegame/crafting/fixtures.json +++ b/assets/mods/basegame/crafting/fixtures.json @@ -55,5 +55,11 @@ "ingredients": [ [1, 6, "basegame:51"] ] + }, + "item@basegame:6": { /* storage chest */ + "workbench": "basiccrafting", + "ingredients": [ + [1, 20, "$WOOD"] /* 1 chest = 20 wood */ + ] } } \ No newline at end of file diff --git a/assets/mods/basegame/gui/smelter_icons.tga b/assets/mods/basegame/gui/smelter_icons.tga new file mode 100644 index 000000000..3190c4c3f --- /dev/null +++ b/assets/mods/basegame/gui/smelter_icons.tga @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:20bbd4225d636e3b449e8efee08af082a9d6b8c6f30ee68a733b8a8477afb837 +size 6418 diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSmelterBasic.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSmelterBasic.kt index 66e9663dd..65ea9ab0c 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSmelterBasic.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSmelterBasic.kt @@ -14,6 +14,7 @@ import net.torvald.terrarum.modulebasegame.TerrarumIngame import net.torvald.terrarum.modulebasegame.gameitems.FixtureItemBase import net.torvald.terrarum.modulebasegame.ui.UICrafting import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull +import net.torvald.terrarum.modulebasegame.ui.UISmelterBasic import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack /** @@ -28,7 +29,7 @@ class FixtureSmelterBasic : FixtureBase, CraftingStation { constructor() : super( BlockBox(BlockBox.NO_COLLISION, 3, 4), // temporary value, will be overwritten by spawn() nameFun = { Lang["ITEM_SMELTER_SMALL"] }, - mainUI = UICrafting(null) + mainUI = UISmelterBasic() ) { CommonResourcePool.addToLoadingList("particles-tiki_smoke.tga") { TextureRegionPack(ModMgr.getGdxFile("basegame", "particles/bigger_smoke.tga"), 16, 16) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt b/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt index 978c62f0d..d0f497001 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt @@ -15,6 +15,7 @@ import net.torvald.terrarum.modulebasegame.gameactors.CraftingStation import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory import net.torvald.terrarum.modulebasegame.gameactors.InventoryPair import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.listGap +import net.torvald.terrarum.modulebasegame.ui.UITemplateHalfInventory.Companion.INVENTORY_NAME_TEXT_GAP import net.torvald.terrarum.ui.* import net.torvald.terrarum.ui.UIItemCatBar.Companion.FILTER_CAT_ALL import net.torvald.unicode.getKeycapPC @@ -30,7 +31,7 @@ class UICrafting(val full: UIInventoryFull?) : UICanvas( toggleButtonLiteral = if (full == null) "control_gamepad_start" else null ), HasInventory { - override var width = App.scr.width + override var width = Toolkit.drawWidth override var height = App.scr.height private val playerThings = UITemplateHalfInventory(this, false).also { @@ -82,7 +83,6 @@ class UICrafting(val full: UIInventoryFull?) : UICanvas( override fun getNegotiator() = negotiator override fun getFixtureInventory(): FixtureInventory = craftables - override fun getPlayerInventory(): FixtureInventory = INGAME.actorNowPlaying!!.inventory private val halfSlotOffset = (UIItemInventoryElemSimple.height + listGap) / 2 @@ -93,7 +93,6 @@ class UICrafting(val full: UIInventoryFull?) : UICanvas( private val thisOffsetY = UIInventoryFull.INVENTORY_CELLS_OFFSET_Y() private val cellsWidth = (listGap + UIItemInventoryElemWide.height) * 6 - listGap - private val TEXT_GAP = 26 private val LAST_LINE_IN_GRID = ((UIItemInventoryElemWide.height + listGap) * (UIInventoryFull.CELLS_VRT - 2)) + 22//359 // TEMPORARY VALUE! private var recipeClicked: CraftingCodex.CraftingRecipe? = null @@ -443,11 +442,9 @@ class UICrafting(val full: UIInventoryFull?) : UICanvas( // text label for two inventory grids val craftingLabel = Lang["GAME_CRAFTING"] val ingredientsLabel = Lang["GAME_INVENTORY_INGREDIENTS"] - val playerName = INGAME.actorNowPlaying!!.actorValue.getAsString(AVKey.NAME).orEmpty().let { it.ifBlank { Lang["GAME_INVENTORY"] } } - App.fontGame.draw(batch, craftingLabel, thisOffsetX + (cellsWidth - App.fontGame.getWidth(craftingLabel)) / 2, thisOffsetY - TEXT_GAP) - App.fontGame.draw(batch, ingredientsLabel, thisOffsetX + (cellsWidth - App.fontGame.getWidth(ingredientsLabel)) / 2, thisOffsetY + LAST_LINE_IN_GRID - TEXT_GAP) - App.fontGame.draw(batch, playerName, thisOffsetX2 + (cellsWidth - App.fontGame.getWidth(playerName)) / 2, thisOffsetY - TEXT_GAP) + App.fontGame.draw(batch, craftingLabel, thisOffsetX + (cellsWidth - App.fontGame.getWidth(craftingLabel)) / 2, thisOffsetY - INVENTORY_NAME_TEXT_GAP) + App.fontGame.draw(batch, ingredientsLabel, thisOffsetX + (cellsWidth - App.fontGame.getWidth(ingredientsLabel)) / 2, thisOffsetY + LAST_LINE_IN_GRID - INVENTORY_NAME_TEXT_GAP) // control hints @@ -456,11 +453,11 @@ class UICrafting(val full: UIInventoryFull?) : UICanvas( App.fontGame.draw(batch, controlHelp, controlHintXPos, UIInventoryFull.yEnd - 20) - if (full != null) { + if (INGAME.actorNowPlaying != null) { //draw player encumb val encumbBarXPos = thisXend - UIInventoryCells.weightBarWidth + 36 val encumbBarYPos = UIInventoryFull.yEnd - 20 + 3f - UIInventoryCells.drawEncumbranceBar(batch, encumbBarXPos, encumbBarYPos, encumbrancePerc, full.actor.inventory) + UIInventoryCells.drawEncumbranceBar(batch, encumbBarXPos, encumbBarYPos, encumbrancePerc, INGAME.actorNowPlaying!!.inventory) } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt index 19d33d5a0..28f24b143 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt @@ -44,19 +44,19 @@ class UIItemInventoryEquippedView( private val equipPosIcon = CommonResourcePool.getAsTextureRegionPack("inventory_category") private val cellToIcon = intArrayOf(0,1,2,3,4,5,6,7,6,7,6,7) - private val equipPosIconCol = Color(0xdddddd7f.toInt()) private val itemGrid = Array(2 * 6) { UIItemInventoryElemSimple( - parentUI = parentUI, - initialX = this.posX + (UIItemInventoryElemSimple.height + listGap) * ((it + 4) % 2), - initialY = this.posY + (UIItemInventoryElemSimple.height + listGap) * ((it + 4) / 2), - item = null, - amount = UIItemInventoryElemWide.UNIQUE_ITEM_HAS_NO_AMOUNT, - itemImage = null, - drawBackOnNull = true, - keyDownFun = createInvCellGenericKeyDownFun(), - touchDownFun = createInvCellGenericTouchDownFun(inventoryListRebuildFun) // to "unselect" the equipped item and main item grid would "untick" accordingly + parentUI = parentUI, + initialX = this.posX + (UIItemInventoryElemSimple.height + listGap) * ((it + 4) % 2), + initialY = this.posY + (UIItemInventoryElemSimple.height + listGap) * ((it + 4) / 2), + item = null, + amount = UIItemInventoryElemWide.UNIQUE_ITEM_HAS_NO_AMOUNT, + itemImage = null, + 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) ) } @@ -107,10 +107,6 @@ class UIItemInventoryEquippedView( // slot image on each cells itemGrid.forEachIndexed { index, cell -> cell.render(frameDelta, batch, camera) - if (cell.item == null) { - batch.color = equipPosIconCol - batch.draw(equipPosIcon.get(cellToIcon[index], 1), 15f + cell.posX, 15f + cell.posY) - } } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt index 8e9308e5e..2ca4cfaa0 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIJukeboxInventory.kt @@ -4,13 +4,13 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.* -import net.torvald.terrarum.gameactors.AVKey import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory import net.torvald.terrarum.modulebasegame.gameactors.InventoryPair import net.torvald.terrarum.modulebasegame.gameitems.ItemFileRef import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.defaultInventoryCellTheme import net.torvald.terrarum.modulebasegame.ui.UIJukebox.Companion.SLOT_SIZE +import net.torvald.terrarum.modulebasegame.ui.UITemplateHalfInventory.Companion.INVENTORY_NAME_TEXT_GAP import net.torvald.terrarum.ui.* import net.torvald.terrarum.ui.UIItemInventoryElemWide import net.torvald.unicode.EMDASH @@ -117,13 +117,8 @@ class UIJukeboxInventory(val parent: UIJukebox) : UICanvas() { uiItems.forEach { it.render(frameDelta, batch, camera) } // chest name text - val chestName = Lang["ITEM_JUKEBOX"] - val playerName = INGAME.actorNowPlaying!!.actorValue.getAsString(AVKey.NAME).orEmpty().let { it.ifBlank { Lang["GAME_INVENTORY"] } } - batch.color = Color.WHITE - App.fontGame.draw(batch, chestName, thisOffsetX + (cellsWidth - App.fontGame.getWidth(chestName)) / 2, thisOffsetY - 30) - App.fontGame.draw(batch, playerName, thisOffsetX2 + (cellsWidth - App.fontGame.getWidth(playerName)) / 2, thisOffsetY - 30) - + Toolkit.drawTextCentered(batch, App.fontGame, Lang["ITEM_JUKEBOX"], cellsWidth, thisOffsetX, thisOffsetY - INVENTORY_NAME_TEXT_GAP) } override fun dispose() { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UISmelterBasic.kt b/src/net/torvald/terrarum/modulebasegame/ui/UISmelterBasic.kt new file mode 100644 index 000000000..ad26ab37a --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/ui/UISmelterBasic.kt @@ -0,0 +1,174 @@ +package net.torvald.terrarum.modulebasegame.ui + +import com.badlogic.gdx.graphics.Color +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.langpack.Lang +import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory +import net.torvald.terrarum.modulebasegame.gameitems.FixtureItemBase +import net.torvald.terrarum.ui.* +import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack +import net.torvald.unicode.getKeycapPC + +/** + * Created by minjaesong on 2024-01-29. + */ +class UISmelterBasic() : UICanvas( + toggleKeyLiteral = "control_key_inventory", + toggleButtonLiteral = "control_gamepad_start" +), HasInventory { + + override var width = Toolkit.drawWidth + override var height = App.scr.height + + private val playerThings = UITemplateHalfInventory(this, false).also { + it.itemListTouchDownFun = { gameItem, _, _, _, _ -> + + + } + } + + private val negotiator = object : InventoryTransactionNegotiator() { + override fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Long) { +// TODO() + } + + override fun refund(fixture: FixtureInventory, player: FixtureInventory, item: GameItem, amount: Long) { +// TODO() + } + } + + override fun getNegotiator() = negotiator + override fun getFixtureInventory(): FixtureInventory = TODO() + override fun getPlayerInventory(): FixtureInventory = INGAME.actorNowPlaying!!.inventory + + init { + CommonResourcePool.addToLoadingList("basegame_gui_smelter_icons") { + TextureRegionPack(ModMgr.getGdxFile("basegame", "gui/smelter_icons.tga"), 20, 20) + } + CommonResourcePool.loadAll() + } + + private val smelterCellIcons = CommonResourcePool.getAsTextureRegionPack("basegame_gui_smelter_icons") + + private var smelterBackdrop = + FixtureItemBase.getItemImageFromSingleImage("basegame", "sprites/fixtures/smelter_tall.tga") + + private var fuelCaloriesNow = 0f + private var fuelCaloriesMax: Float? = null + + private var temperature = 0f // 0f..1f + private var progress = 0f + + + private val leftPanelWidth = playerThings.width + private val leftPanelHeight = playerThings.height + private val leftPanelX = playerThings.posX - leftPanelWidth - UIItemInventoryItemGrid.listGap - UIItemInventoryElemWide.height + private val leftPanelY = playerThings.posY + + private val backdropColour = Color(0x7f7f7fff) + private val backdropZoom = 6 + private val backdropX = (leftPanelX + (leftPanelWidth - smelterBackdrop.regionWidth * backdropZoom) / 2).toFloat() + private val backdropY = (leftPanelY + (leftPanelHeight - smelterBackdrop.regionHeight * backdropZoom) / 2).toFloat() + + private val oreX = backdropX + 12 * backdropZoom + 3 + private val oreY = backdropY + 23 * backdropZoom + 3 + + private val fireboxX = backdropX + 12 * backdropZoom + 3 + private val fireboxY = backdropY + 39 * backdropZoom + 3 + + private val productX = backdropX + 37 * backdropZoom + 3 + private val productY = backdropY + 39 * backdropZoom + 3 + + private val oreItemSlot = UIItemInventoryElemSimple( + this, oreX.toInt(), oreY.toInt(), + emptyCellIcon = smelterCellIcons.get(1, 1), + keyDownFun = { _, _, _, _, _ -> }, + touchDownFun = { item, amount, _, _, _ -> } + ) + private val fireboxItemSlot = UIItemInventoryElemSimple( + this, fireboxX.toInt(), fireboxY.toInt(), + emptyCellIcon = smelterCellIcons.get(0, 0), + keyDownFun = { _, _, _, _, _ -> }, + touchDownFun = { item, amount, _, _, _ -> } + ) + private val productItemslot = UIItemInventoryElemSimple( + this, productX.toInt(), productY.toInt(), + emptyCellIcon = smelterCellIcons.get(1, 0), + keyDownFun = { _, _, _, _, _ -> }, + touchDownFun = { item, amount, _, _, _ -> } + ) + + private var encumbrancePerc = 0f + + private fun itemListUpdate() { + // let itemlists be sorted +// itemListCraftable.rebuild(UIItemCatBar.FILTER_CAT_ALL) + playerThings.rebuild(UIItemCatBar.FILTER_CAT_ALL) + encumbrancePerc = getPlayerInventory().let { + it.capacity.toFloat() / it.maxCapacity + } + } + + init { + addUIitem(playerThings) + addUIitem(fireboxItemSlot) + addUIitem(oreItemSlot) + addUIitem(productItemslot) + } + + override fun show() { + super.show() + playerThings.setGetInventoryFun { INGAME.actorNowPlaying!!.inventory } + itemListUpdate() + + UIItemInventoryItemGrid.tooltipShowing.clear() + INGAME.setTooltipMessage(null) + } + + override fun updateUI(delta: Float) { + uiItems.forEach { it.update(delta) } + } + + private val controlHelp: String + get() = if (App.environment == RunningEnvironment.PC) + "${getKeycapPC(ControlPresets.getKey("control_key_inventory"))} ${Lang["GAME_ACTION_CLOSE"]}" + else + "${App.gamepadLabelStart} ${Lang["GAME_ACTION_CLOSE"]}" + + override fun renderUI(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) { + batch.color = backdropColour + batch.draw(smelterBackdrop, backdropX, backdropY, smelterBackdrop.regionWidth * 6f, smelterBackdrop.regionHeight * 6f) + + uiItems.forEach { it.render(frameDelta, batch, camera) } + + + + + // control hints + batch.color = Color.WHITE + val controlHintXPos = leftPanelX + 2f + blendNormalStraightAlpha(batch) + App.fontGame.draw(batch, controlHelp, controlHintXPos, UIInventoryFull.yEnd - 20) + + + if (INGAME.actorNowPlaying != null) { + //draw player encumb + val encumbBarXPos = playerThings.posX + playerThings.width - UIInventoryCells.weightBarWidth + 36 + val encumbBarYPos = UIInventoryFull.yEnd - 20 + 3f + UIInventoryCells.drawEncumbranceBar(batch, encumbBarXPos, encumbBarYPos, encumbrancePerc, INGAME.actorNowPlaying!!.inventory) + } + + + blendNormalStraightAlpha(batch) + } + + override fun dispose() { + } + + + + +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt index 25cfb47e9..5d8f6f914 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt @@ -95,7 +95,7 @@ internal class UIStorageChest : UICanvas( catBar.selectionChangeListener = { old, new -> itemListUpdate() } - itemListChest = UITemplateHalfInventory(this, true) { getFixtureInventory() }.also { + itemListChest = UITemplateHalfInventory(this, true, { getFixtureInventory() }, { chestNameFun() }).also { it.itemListKeyDownFun = { _, _, _, _, _ -> Unit } it.itemListTouchDownFun = { gameItem, amount, button, _, _ -> if (button == App.getConfigInt("config_mouseprimary")) { @@ -210,19 +210,12 @@ internal class UIStorageChest : UICanvas( blendNormalStraightAlpha(batch) // encumbrance meter - val chestName = chestNameFun() - val playerName = INGAME.actorNowPlaying!!.actorValue.getAsString(AVKey.NAME).orEmpty().let { it.ifBlank { Lang["GAME_INVENTORY"] } } val encumbBarXPos = itemListPlayer.posX + itemListPlayer.width - UIInventoryCells.weightBarWidth + 36 val yEnd = -UIInventoryFull.YPOS_CORRECTION + (App.scr.height + UIInventoryFull.internalHeight).div(2).toFloat() // directly copied from UIInventoryFull.yEnd val encumbBarYPos = yEnd - 20 + 3 // dunno why but extra 3 px is needed UIInventoryCells.drawEncumbranceBar(batch, encumbBarXPos, encumbBarYPos, encumbrancePerc, getPlayerInventory()) - // chest name text - batch.color = Color.WHITE - App.fontGame.draw(batch, chestName, thisOffsetX + (cellsWidth - App.fontGame.getWidth(chestName)) / 2, thisOffsetY - 30) - App.fontGame.draw(batch, playerName, thisOffsetX2 + (cellsWidth - App.fontGame.getWidth(playerName)) / 2, thisOffsetY - 30) - // control hint App.fontGame.draw(batch, controlHelp, thisOffsetX - 34f, encumbBarYPos - 3) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt b/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt index 6988aeb5b..6db032950 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt @@ -1,10 +1,13 @@ package net.torvald.terrarum.modulebasegame.ui +import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.* +import net.torvald.terrarum.gameactors.AVKey import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.ItemID +import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory import net.torvald.terrarum.modulebasegame.gameactors.InventoryPair @@ -22,9 +25,14 @@ import net.torvald.terrarum.ui.* class UITemplateHalfInventory( parent: UICanvas, drawOnLeft: Boolean, - getInventoryFun: () -> FixtureInventory = { INGAME.actorNowPlaying!!.inventory } + getInventoryFun: () -> FixtureInventory = { INGAME.actorNowPlaying!!.inventory }, + val inventoryNameFun: () -> String = { INGAME.actorNowPlaying!!.actorValue.getAsString(AVKey.NAME).orEmpty().let { it.ifBlank { Lang["GAME_INVENTORY"] } } } ) : UITemplate(parent) { + companion object { + const val INVENTORY_NAME_TEXT_GAP = 28 + } + val itemList: UIItemInventoryItemGrid private val halfSlotOffset = (UIItemInventoryElemSimple.height + UIItemInventoryItemGrid.listGap) / 2 @@ -78,8 +86,13 @@ class UITemplateHalfInventory( 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) + override fun update(delta: Float) = itemList.update(delta) + override fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) { + itemList.render(frameDelta, batch, camera) + + batch.color = Color.WHITE + Toolkit.drawTextCentered(batch, App.fontGame, inventoryNameFun(), width, posX, thisOffsetY - INVENTORY_NAME_TEXT_GAP) + } var posX: Int get() = itemList.posX @@ -95,4 +108,7 @@ class UITemplateHalfInventory( override fun getUIitems(): List { return listOf(itemList) } + + override fun dispose() { + } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/ui/UICanvas.kt b/src/net/torvald/terrarum/ui/UICanvas.kt index 3ac0a814d..4d0c52c8a 100644 --- a/src/net/torvald/terrarum/ui/UICanvas.kt +++ b/src/net/torvald/terrarum/ui/UICanvas.kt @@ -97,7 +97,7 @@ abstract class UICanvas( open var openCloseTime: Second = OPENCLOSE_GENERIC - protected val uiItems = ArrayList() + protected val uiItems = ArrayList() val relativeMouseX: Int @@ -189,12 +189,9 @@ abstract class UICanvas( abstract override fun dispose() - fun addUIitem(uiItem: UIItem) { + fun addUIitem(uiItem: UIItemisable) { if (!uiItems.contains(uiItem)) uiItems.add(uiItem) } - fun addUIitem(template: UITemplate) { - template.getUIitems().forEach { addUIitem(it) } - } fun mouseInScreen(x: Int, y: Int) = x in 0 until App.scr.windowW && y in 0 until App.scr.windowH diff --git a/src/net/torvald/terrarum/ui/UIItem.kt b/src/net/torvald/terrarum/ui/UIItem.kt index 438202b10..8c05a3ded 100644 --- a/src/net/torvald/terrarum/ui/UIItem.kt +++ b/src/net/torvald/terrarum/ui/UIItem.kt @@ -52,7 +52,7 @@ import net.torvald.terrarum.gamecontroller.TerrarumKeyboardEvent * * Created by minjaesong on 2015-12-31. */ -abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: Int): Disposable { // do not replace parentUI to UIHandler! +abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: Int): UIItemisable() { // do not replace parentUI to UIHandler! // X/Y Position relative to the containing canvas var posX: Int = initialX @@ -145,11 +145,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I open var isActive = true - open fun show() {} - open fun hide() {} - - - open fun update(delta: Float) { + override fun update(delta: Float) { if (parentUI.isVisible) { if (isActive) { updateListener.invoke(delta) @@ -177,7 +173,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I /** * In this time, you do write like: ```draw(posX + 4, posY + 32)```, unlike UICanvas, because posX/posY comes from the parent UI. */ - open fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) { + override fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) { if (parentUI.isVisible) { // if (isActive) { mouseOverCall?.render(frameDelta, batch, camera) @@ -190,7 +186,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I } // keyboard controlled - open fun keyDown(keycode: Int): Boolean { + override fun keyDown(keycode: Int): Boolean { if (parentUI.isVisible && isEnabled) { keyDownListener.invoke(keycode) return true @@ -198,7 +194,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I return false } - open fun keyUp(keycode: Int): Boolean { + override fun keyUp(keycode: Int): Boolean { if (parentUI.isVisible && isEnabled) { keyUpListener.invoke(keycode) return true @@ -206,7 +202,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I return false } - open fun keyTyped(character: Char): Boolean { + override fun keyTyped(character: Char): Boolean { if (parentUI.isVisible && isEnabled) { keyTypedListener.invoke(character) return true @@ -216,7 +212,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 { + override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean { if (parentUI.isVisible && isEnabled) { touchDraggedListener.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer) return true @@ -224,7 +220,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I return false } - open fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { + override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { var actionDone = false if (parentUI.isVisible && isEnabled) { @@ -241,7 +237,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I return actionDone } - open fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { + override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { clickOnceListenerFired = false if (parentUI.isVisible && mouseUp) { @@ -251,7 +247,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I return false } - open fun scrolled(amountX: Float, amountY: Float): Boolean { + override fun scrolled(amountX: Float, amountY: Float): Boolean { if (parentUI.isVisible && mouseUp && isEnabled) { scrolledListener.invoke(amountX, amountY) return true @@ -259,9 +255,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I return false } - open fun inputStrobed(e: TerrarumKeyboardEvent) { - } abstract override fun dispose() diff --git a/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt b/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt index d603a6d85..7a5ad8473 100644 --- a/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt +++ b/src/net/torvald/terrarum/ui/UIItemInventoryElemSimple.kt @@ -35,6 +35,8 @@ class UIItemInventoryElemSimple( highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting colourTheme: InventoryCellColourTheme = defaultInventoryCellTheme, var showItemCount: Boolean = true, + var emptyCellIcon: TextureRegion? = null, // icon to draw when the cell is empty + var emptyCellIconColour: Color = Color(0xdddddd7f.toInt()), ) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem, colourTheme) { companion object { @@ -52,6 +54,9 @@ class UIItemInventoryElemSimple( private val imgOffsetX: Float get() = (this.height - itemImageOrDefault.regionWidth).div(2).toFloat() // to snap to the pixel grid + private val emptyCellIconOffsetX = (this.height - (emptyCellIcon?.regionWidth ?: 0)).div(2).toFloat() + private val emptyCellIconOffsetY = (this.height - (emptyCellIcon?.regionHeight ?: 0)).div(2).toFloat() + override fun update(delta: Float) { } @@ -123,6 +128,15 @@ class UIItemInventoryElemSimple( ) } + } + else { + + // empty cell image + emptyCellIcon?.let { + batch.color = emptyCellIconColour + batch.draw(it, posX + emptyCellIconOffsetX, posY + emptyCellIconOffsetY) + } + } // see IFs above? diff --git a/src/net/torvald/terrarum/ui/UITemplate.kt b/src/net/torvald/terrarum/ui/UITemplate.kt index f92079abe..5899698a8 100644 --- a/src/net/torvald/terrarum/ui/UITemplate.kt +++ b/src/net/torvald/terrarum/ui/UITemplate.kt @@ -1,10 +1,44 @@ package net.torvald.terrarum.ui +import com.badlogic.gdx.graphics.OrthographicCamera +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import com.badlogic.gdx.utils.Disposable +import net.torvald.terrarum.gamecontroller.TerrarumKeyboardEvent + /** * Created by minjaesong on 2024-01-10. */ -abstract class UITemplate(val parent: UICanvas) { +abstract class UITemplate(val parent: UICanvas) : UIItemisable() { abstract fun getUIitems(): List + override fun show() { getUIitems().forEach { it.show() } } + override fun hide() { getUIitems().forEach { it.hide() } } + override fun inputStrobed(e: TerrarumKeyboardEvent) { getUIitems().forEach { it.inputStrobed(e) } } + override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean { getUIitems().forEach { it.touchDragged(screenX, screenY, pointer) }; return true } + override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { getUIitems().forEach { it.touchDown(screenX, screenY, pointer, button) }; return true } + override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { getUIitems().forEach { it.touchUp(screenX, screenY, pointer, button) }; return true } + override fun scrolled(amountX: Float, amountY: Float): Boolean { getUIitems().forEach { it.scrolled(amountX, amountY) }; return true } + override fun keyDown(keycode: Int): Boolean { getUIitems().forEach { it.keyDown(keycode) }; return true } + override fun keyUp(keycode: Int): Boolean { getUIitems().forEach { it.keyUp(keycode) }; return true } + override fun keyTyped(char: Char): Boolean { getUIitems().forEach { it.keyTyped(char) }; return true } + +} + +/** + * Created by minjaesong on 2024-01-29. + */ +abstract class UIItemisable : Disposable { + abstract fun update(delta: Float) + abstract fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) + open fun show() {} + open fun hide() {} + open fun inputStrobed(e: TerrarumKeyboardEvent) {} + open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean = false + open fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean = false + open fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean = false + open fun scrolled(amountX: Float, amountY: Float): Boolean = false + open fun keyDown(keycode: Int): Boolean = false + open fun keyUp(keycode: Int): Boolean = false + open fun keyTyped(char: Char): Boolean = false } \ No newline at end of file diff --git a/src/net/torvald/terrarum/ui/UITemplateCatBar.kt b/src/net/torvald/terrarum/ui/UITemplateCatBar.kt index 9b02c371a..16bffb3ab 100644 --- a/src/net/torvald/terrarum/ui/UITemplateCatBar.kt +++ b/src/net/torvald/terrarum/ui/UITemplateCatBar.kt @@ -1,5 +1,7 @@ package net.torvald.terrarum.ui +import com.badlogic.gdx.graphics.OrthographicCamera +import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.App import net.torvald.terrarum.CommonResourcePool import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull @@ -34,4 +36,8 @@ class UITemplateCatBar( override fun getUIitems(): List { return listOf(catBar) } + + override fun update(delta: Float) = catBar.update(delta) + override fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) = catBar.render(frameDelta, batch, camera) + override fun dispose() = catBar.dispose() } \ No newline at end of file