uiitem and template refactoring; new smelter ui wip

This commit is contained in:
minjaesong
2024-01-29 18:51:53 +09:00
parent 8aedd7c78a
commit 3214f11375
14 changed files with 290 additions and 64 deletions

View File

@@ -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)
}

View File

@@ -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<UIItemInventoryCellBase>(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)
}
}

View File

@@ -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() {

View File

@@ -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() {
}
}

View File

@@ -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)

View File

@@ -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<UIItem> {
return listOf(itemList)
}
override fun dispose() {
}
}