jukebox ui wip

This commit is contained in:
minjaesong
2024-01-13 05:08:40 +09:00
parent 5c6cc36437
commit fb9640e615
19 changed files with 389 additions and 103 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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