modularised inventory cell behaviour

This commit is contained in:
minjaesong
2021-03-13 17:26:32 +09:00
parent c25e9f92be
commit 5330a2be96
8 changed files with 110 additions and 204 deletions

View File

@@ -41,8 +41,9 @@ class UIItemInventoryElem(
override var quickslot: Int? = null,
override var equippedSlot: Int? = null,
val drawBackOnNull: Boolean = true,
val listRebuildFun: () -> Unit
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot) {
keyDownFun: (GameItem?, Int) -> Unit,
touchDownFun: (GameItem?, Int, Int, Int, Int) -> Unit
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun) {
companion object {
val height = 48
@@ -157,94 +158,6 @@ class UIItemInventoryElem(
}
override fun keyDown(keycode: Int): Boolean {
if (item != null && Terrarum.ingame != null && keycode in Input.Keys.NUM_0..Input.Keys.NUM_9) {
val player = (Terrarum.ingame!! as TerrarumIngame).actorNowPlaying
if (player == null) return false
val inventory = player.inventory
val slot = if (keycode == Input.Keys.NUM_0) 9 else keycode - Input.Keys.NUM_1
val currentSlotItem = inventory.getQuickslot(slot)
inventory.setQuickBar(
slot,
if (currentSlotItem?.item != item?.dynamicID)
item?.dynamicID // register
else
null // drop registration
)
// search for duplicates in the quickbar, except mine
// if there is, unregister the other
(0..9).minus(slot).forEach {
if (inventory.getQuickslot(it)?.item == item?.dynamicID) {
inventory.setQuickBar(it, null)
}
}
}
return super.keyDown(keycode)
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
if (item != null && Terrarum.ingame != null) {
// equip da shit
val itemEquipSlot = item!!.equipPosition
if (itemEquipSlot == GameItem.EquipPosition.NULL) {
TODO("Equip position is NULL, does this mean it's single-consume items like a potion? (from item: \"$item\" with itemID: ${item?.originalID}/${item?.dynamicID})")
}
val player = (Terrarum.ingame!! as TerrarumIngame).actorNowPlaying
if (player == null) return false
if (item != ItemCodex[player.inventory.itemEquipped.get(itemEquipSlot)]) { // if this item is unequipped, equip it
player.equipItem(item!!)
// also equip on the quickslot
player.actorValue.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL)?.let {
player.inventory.setQuickBar(it, item!!.dynamicID)
}
}
else { // if not, unequip it
player.unequipItem(item!!)
// also unequip on the quickslot
player.actorValue.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL)?.let {
player.inventory.setQuickBar(it, null)
}
}
}
listRebuildFun()
return super.touchDown(screenX, screenY, pointer, button)
}
override fun dispose() {
}
override fun keyUp(keycode: Int): Boolean {
return super.keyUp(keycode)
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
return super.mouseMoved(screenX, screenY)
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
return super.touchDragged(screenX, screenY, pointer)
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return super.touchUp(screenX, screenY, pointer, button)
}
override fun scrolled(amount: Int): Boolean {
return super.scrolled(amount)
}
}

View File

@@ -38,8 +38,9 @@ class UIItemInventoryElemSimple(
override var quickslot: Int? = null,
override var equippedSlot: Int? = null,
val drawBackOnNull: Boolean = true,
val listRebuildFun: () -> Unit
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot) {
keyDownFun: (GameItem?, Int) -> Unit,
touchDownFun: (GameItem?, Int, Int, Int, Int) -> Unit
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun) {
companion object {
val height = UIItemInventoryElem.height
@@ -52,9 +53,7 @@ class UIItemInventoryElemSimple(
get() = (this.height - itemImage!!.regionHeight).div(2).toFloat() // to snap to the pixel grid
override fun update(delta: Float) {
if (item != null) {
}
}
override fun render(batch: SpriteBatch, camera: Camera) {
@@ -132,92 +131,6 @@ class UIItemInventoryElemSimple(
}
override fun keyDown(keycode: Int): Boolean {
if (item != null && Terrarum.ingame != null && keycode in Input.Keys.NUM_0..Input.Keys.NUM_9) {
val player = (Terrarum.ingame!! as TerrarumIngame).actorNowPlaying
if (player == null) return false
val inventory = player.inventory
val slot = if (keycode == Input.Keys.NUM_0) 9 else keycode - Input.Keys.NUM_1
val currentSlotItem = inventory.getQuickslot(slot)
inventory.setQuickBar(
slot,
if (currentSlotItem?.item != item?.dynamicID)
item?.dynamicID // register
else
null // drop registration
)
// search for duplicates in the quickbar, except mine
// if there is, unregister the other
(0..9).minus(slot).forEach {
if (inventory.getQuickslot(it)?.item == item?.dynamicID) {
inventory.setQuickBar(it, null)
}
}
}
return super.keyDown(keycode)
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
//println("touchdown elemgrid")
if (item != null && Terrarum.ingame != null) {
// equip da shit
val itemEquipSlot = item!!.equipPosition
val player = (Terrarum.ingame!! as TerrarumIngame).actorNowPlaying
if (player == null) return false
if (item != ItemCodex[player.inventory.itemEquipped.get(itemEquipSlot)]) { // if this item is unequipped, equip it
player.equipItem(item!!)
// also equip on the quickslot
player.actorValue.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL)?.let {
player.inventory.setQuickBar(it, item!!.dynamicID)
}
}
else { // if not, unequip it
player.unequipItem(item!!)
// also unequip on the quickslot
player.actorValue.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL)?.let {
player.inventory.setQuickBar(it, null)
}
}
}
listRebuildFun()
return super.touchDown(screenX, screenY, pointer, button)
}
override fun dispose() {
}
override fun keyUp(keycode: Int): Boolean {
return super.keyUp(keycode)
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
return super.mouseMoved(screenX, screenY)
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
return super.touchDragged(screenX, screenY, pointer)
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return super.touchUp(screenX, screenY, pointer, button)
}
override fun scrolled(amount: Int): Boolean {
return super.scrolled(amount)
}
}

View File

@@ -3,6 +3,7 @@ package net.torvald.terrarum.modulebasegame.gameactors
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.CommonResourcePool
import net.torvald.terrarum.Second
import net.torvald.terrarum.Terrarum
@@ -46,11 +47,11 @@ internal object UIStorageChest : UICanvas(), HasInventory {
private val negotiator = object : InventoryNegotiator() {
override fun accept(item: GameItem, amount: Int) {
TODO("Not yet implemented")
printdbg(this, "Accept")
}
override fun reject(item: GameItem, amount: Int) {
TODO("Not yet implemented")
printdbg(this, "Reject")
}
}
@@ -87,8 +88,9 @@ internal object UIStorageChest : UICanvas(), HasInventory {
4, 5,
drawScrollOnRightside = true,
drawWallet = true,
listRebuildFun = { itemListUpdate() }
)
keyDownFun = { _,_ -> Unit },
touchDownFun = { _,_,_,_,_ -> itemListUpdate() }
)
handler.allowESCtoClose = true

View File

@@ -8,6 +8,8 @@ import net.torvald.terrarum.gameactors.ActorWithBody
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.INVEN_DEBUG_MODE
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.createInvCellGenericKeyDownFun
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.createInvCellGenericTouchDownFun
import net.torvald.terrarum.ui.UICanvas
internal class UIInventoryCells(
@@ -35,7 +37,8 @@ internal class UIInventoryCells(
full.INVENTORY_CELLS_OFFSET_X,
full.INVENTORY_CELLS_OFFSET_Y,
full.CELLS_HOR, full.CELLS_VRT,
listRebuildFun = { rebuildList() }
keyDownFun = createInvCellGenericKeyDownFun(),
touchDownFun = createInvCellGenericTouchDownFun { rebuildList() }
)

View File

@@ -25,10 +25,24 @@ abstract class UIItemInventoryCellBase(
open var amount: Int,
open var itemImage: TextureRegion?,
open var quickslot: Int? = null,
open var equippedSlot: Int? = null
open var equippedSlot: Int? = null,
val keyDownFun: (GameItem?, Int) -> Unit,
val touchDownFun: (GameItem?, Int, Int, Int, Int) -> Unit
) : UIItem(parentUI, initialX, initialY) {
abstract override fun update(delta: Float)
abstract override fun render(batch: SpriteBatch, camera: Camera)
override fun keyDown(keycode: Int): Boolean {
keyDownFun(item, keycode)
super.keyDown(keycode)
return true
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
touchDownFun(item, screenX, screenY, pointer, button)
super.touchDown(screenX, screenY, pointer, button)
return true
}
}
object UIItemInventoryCellCommonRes {

View File

@@ -10,6 +10,8 @@ import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory
import net.torvald.terrarum.modulebasegame.ui.ItemSlotImageFactory.CELLCOLOUR_BLACK
import net.torvald.terrarum.modulebasegame.ui.ItemSlotImageFactory.CELLCOLOUR_BLACK_ACTIVE
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.createInvCellGenericKeyDownFun
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.createInvCellGenericTouchDownFun
import net.torvald.terrarum.ui.Toolkit
import net.torvald.terrarum.ui.Toolkit.DEFAULT_BOX_BORDER_COL
import net.torvald.terrarum.ui.UIItem
@@ -62,7 +64,8 @@ class UIItemInventoryEquippedView(
backCol = CELLCOLOUR_BLACK,
backBlendMode = BlendMode.NORMAL,
drawBackOnNull = true,
listRebuildFun = { parentUI.rebuildList() } // to "unselect" the equipped item and main item grid would "untick" accordingly
keyDownFun = createInvCellGenericKeyDownFun(),
touchDownFun = createInvCellGenericTouchDownFun { parentUI.rebuildList() } // to "unselect" the equipped item and main item grid would "untick" accordingly
)
}

View File

@@ -3,8 +3,11 @@ package net.torvald.terrarum.modulebasegame.ui
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.Input
import net.torvald.terrarum.*
import net.torvald.terrarum.UIItemInventoryCatBar.Companion.CAT_ALL
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameitem.GameItem
import net.torvald.terrarum.gameworld.fmod
import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.modulebasegame.TerrarumIngame
@@ -42,7 +45,8 @@ class UIItemInventoryItemGrid(
val verticalCells: Int,
val drawScrollOnRightside: Boolean = false,
val drawWallet: Boolean = true,
val listRebuildFun: () -> Unit
keyDownFun: (GameItem?, Int) -> Unit,
touchDownFun: (GameItem?, Int, Int, Int, Int) -> Unit
) : UIItem(parentUI, initialX, initialY) {
// deal with the moving position
@@ -98,16 +102,74 @@ class UIItemInventoryItemGrid(
companion object {
const val listGap = 8
//const val horizontalCells = 11
//const val verticalCells = 8
const val LIST_TO_CONTROL_GAP = 12
//val largeListWidth = (horizontalCells * UIItemInventoryElemSimple.height + (horizontalCells - 2) * listGap) / 2
//val WIDTH = horizontalCells * UIItemInventoryElemSimple.height + (horizontalCells - 1) * listGap
//val HEIGHT = verticalCells * UIItemInventoryElemSimple.height + (verticalCells - 1) * listGap
fun getEstimatedW(horizontalCells: Int) = horizontalCells * UIItemInventoryElemSimple.height + (horizontalCells - 1) * listGap
fun getEstimatedH(verticalCells: Int) = verticalCells * UIItemInventoryElemSimple.height + (verticalCells - 1) * listGap
fun createInvCellGenericKeyDownFun(): (GameItem?, Int) -> Unit {
return { item: GameItem?, keycode: Int ->
if (item != null && Terrarum.ingame != null && keycode in Input.Keys.NUM_0..Input.Keys.NUM_9) {
val player = (Terrarum.ingame!! as TerrarumIngame).actorNowPlaying
if (player != null) {
val inventory = player.inventory
val slot = if (keycode == Input.Keys.NUM_0) 9 else keycode - Input.Keys.NUM_1
val currentSlotItem = inventory.getQuickslot(slot)
inventory.setQuickBar(
slot,
if (currentSlotItem?.item != item.dynamicID)
item.dynamicID // register
else
null // drop registration
)
// search for duplicates in the quickbar, except mine
// if there is, unregister the other
(0..9).minus(slot).forEach {
if (inventory.getQuickslot(it)?.item == item.dynamicID) {
inventory.setQuickBar(it, null)
}
}
}
}
}
}
fun createInvCellGenericTouchDownFun(listRebuildFun: () -> Unit): (GameItem?, Int, Int, Int, Int) -> Unit {
return { item: GameItem?, screenX: Int, screenY: Int, pointer: Int, button: Int ->
if (item != null && Terrarum.ingame != null) {
// equip da shit
val itemEquipSlot = item.equipPosition
if (itemEquipSlot == GameItem.EquipPosition.NULL) {
TODO("Equip position is NULL, does this mean it's single-consume items like a potion? (from item: \"$item\" with itemID: ${item?.originalID}/${item?.dynamicID})")
}
val player = (Terrarum.ingame!! as TerrarumIngame).actorNowPlaying
if (player != null) {
if (item != ItemCodex[player.inventory.itemEquipped.get(itemEquipSlot)]) { // if this item is unequipped, equip it
player.equipItem(item)
// also equip on the quickslot
player.actorValue.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL)?.let {
player.inventory.setQuickBar(it, item.dynamicID)
}
}
else { // if not, unequip it
player.unequipItem(item)
// also unequip on the quickslot
player.actorValue.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL)?.let {
player.inventory.setQuickBar(it, null)
}
}
}
}
listRebuildFun()
}
}
}
private val itemGrid = Array<UIItemInventoryCellBase>(horizontalCells * verticalCells) {
@@ -124,11 +186,12 @@ class UIItemInventoryItemGrid(
backBlendMode = BlendMode.NORMAL,
drawBackOnNull = true,
inactiveTextCol = defaultTextColour,
listRebuildFun = listRebuildFun
keyDownFun = keyDownFun,
touchDownFun = touchDownFun
)
}
// TODO automatically determine how much columns are needed. Minimum Width = 5 grids
private val itemListColumnCount = floor(horizontalCells / 4f).toInt().coerceAtLeast(1)
// automatically determine how much columns are needed. Minimum Width = 5 grids
private val itemListColumnCount = floor(horizontalCells / 5f).toInt().coerceAtLeast(1)
private val largeListWidth = (horizontalCells * UIItemInventoryElemSimple.height + (horizontalCells - 2) * listGap) / itemListColumnCount
private val itemList = Array<UIItemInventoryCellBase>(verticalCells * itemListColumnCount) {
UIItemInventoryElem(
@@ -145,7 +208,8 @@ class UIItemInventoryItemGrid(
backBlendMode = BlendMode.NORMAL,
drawBackOnNull = true,
inactiveTextCol = defaultTextColour,
listRebuildFun = listRebuildFun
keyDownFun = keyDownFun,
touchDownFun = touchDownFun
)
}
@@ -221,8 +285,6 @@ class UIItemInventoryItemGrid(
gridModeButtons[1].highlighted = false
itemPage = 0
rebuild(currentFilter)
println("ItemGridMode 0 touchdown")
}
gridModeButtons[1].touchDownListener = { _, _, _, _ ->
isCompactMode = true
@@ -230,8 +292,6 @@ class UIItemInventoryItemGrid(
gridModeButtons[1].highlighted = true
itemPage = 0
rebuild(currentFilter)
println("ItemGridMode 1 touchdown")
}
scrollUpButton.clickOnceListener = { _, _, _ ->

View File

@@ -193,8 +193,6 @@ 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 {
println("uiitem ${this.javaClass.name} touchdown")
var actionDone = false
if (parentUI.isVisible) {