inventory lister's category setting must be held by its parent

This commit is contained in:
minjaesong
2019-08-06 02:28:07 +09:00
parent 0f283f2aaa
commit 08c18caa25
4 changed files with 45 additions and 34 deletions

View File

@@ -36,8 +36,10 @@ class UIItemInventoryCatBar(
private val mainButtons: Array<UIItemImageButton> private val mainButtons: Array<UIItemImageButton>
private val buttonGapSize = (width.toFloat() - (catArrangement.size * catIcons.tileW)) / (catArrangement.size) private val buttonGapSize = (width.toFloat() - (catArrangement.size * catIcons.tileW)) / (catArrangement.size)
var selectedIndex = 0 // default to ALL /** raw order */
private var selectedIndex = 0 // default to ALL
private set private set
/** re-arranged order */
val selectedIcon: Int val selectedIcon: Int
get() = catArrangement[selectedIndex] get() = catArrangement[selectedIndex]
@@ -141,7 +143,8 @@ class UIItemInventoryCatBar(
} }
/** (oldIndex: Int?, newIndex: Int) -> Unit */ /** (oldIndex: Int?, newIndex: Int) -> Unit
* Indices are raw index. That is, not re-arranged. */
var selectionChangeListener: ((Int?, Int) -> Unit)? = null var selectionChangeListener: ((Int?, Int) -> Unit)? = null
override fun update(delta: Float) { override fun update(delta: Float) {

View File

@@ -11,8 +11,6 @@ import net.torvald.terrarum.gameitem.GameItem
import net.torvald.terrarum.gameworld.GameWorld import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.itemproperties.ItemCodex import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.itemproperties.Material import net.torvald.terrarum.itemproperties.Material
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull
import net.torvald.terrarum.realestate.LandUtil import net.torvald.terrarum.realestate.LandUtil
import net.torvald.terrarum.worlddrawer.LightmapRenderer import net.torvald.terrarum.worlddrawer.LightmapRenderer
import org.dyn4j.geometry.Vector2 import org.dyn4j.geometry.Vector2
@@ -590,14 +588,14 @@ open class ActorHumanoid(
} }
// force update inventory UI, but when the pie menu is not open (pie menu constantly writes to the actorvalue, which will rebuildList() // force update inventory UI, but when the pie menu is not open (pie menu constantly writes to the actorvalue, which will rebuildList()
try { /*try {
if (!(Terrarum.ingame!! as TerrarumIngame).uiPieMenu.isVisible) { if (!(Terrarum.ingame!! as TerrarumIngame).uiPieMenu.isVisible) {
((Terrarum.ingame!! as TerrarumIngame).uiInventoryPlayer as UIInventoryFull).rebuildList() ((Terrarum.ingame!! as TerrarumIngame).uiInventoryPlayer as UIInventoryFull).rebuildList()
} }
} }
catch (LateInitMyArse: kotlin.UninitializedPropertyAccessException) { catch (LateInitMyArse: kotlin.UninitializedPropertyAccessException) {
} }*/
// commented; works without it
} }
} }

View File

@@ -11,10 +11,12 @@ import net.torvald.terrarum.*
import net.torvald.terrarum.AppLoader.* import net.torvald.terrarum.AppLoader.*
import net.torvald.terrarum.blockstats.MinimapComposer import net.torvald.terrarum.blockstats.MinimapComposer
import net.torvald.terrarum.gameactors.ActorWBMovable import net.torvald.terrarum.gameactors.ActorWBMovable
import net.torvald.terrarum.gameitem.GameItem
import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.modulebasegame.TerrarumIngame import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory.Companion.CAPACITY_MODE_NO_ENCUMBER import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory.Companion.CAPACITY_MODE_NO_ENCUMBER
import net.torvald.terrarum.modulebasegame.gameactors.Pocketed import net.torvald.terrarum.modulebasegame.gameactors.Pocketed
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryDynamicList.Companion.CAT_ALL
import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.ui.UIItem import net.torvald.terrarum.ui.UIItem
import net.torvald.terrarum.ui.UIItemTextButtonList import net.torvald.terrarum.ui.UIItemTextButtonList
@@ -57,6 +59,18 @@ class UIInventoryFull(
internal val catIcons: TextureRegionPack = CommonResourcePool.getAsTextureRegionPack("inventory_caticons") internal val catIcons: TextureRegionPack = CommonResourcePool.getAsTextureRegionPack("inventory_caticons")
internal val catArrangement: IntArray = intArrayOf(9,6,7,1,0,2,3,4,5,8) internal val catArrangement: IntArray = intArrayOf(9,6,7,1,0,2,3,4,5,8)
internal val catIconsMeaning = listOf( // sortedBy: catArrangement
arrayOf(GameItem.Category.WEAPON),
arrayOf(GameItem.Category.TOOL, GameItem.Category.WIRE),
arrayOf(GameItem.Category.ARMOUR),
arrayOf(GameItem.Category.GENERIC),
arrayOf(GameItem.Category.POTION),
arrayOf(GameItem.Category.MAGIC),
arrayOf(GameItem.Category.BLOCK),
arrayOf(GameItem.Category.WALL),
arrayOf(GameItem.Category.MISC),
arrayOf(CAT_ALL)
)
private val SP = "${0x3000.toChar()} " private val SP = "${0x3000.toChar()} "
@@ -100,10 +114,7 @@ class UIInventoryFull(
42 + (AppLoader.screenH - internalHeight) / 2, 42 + (AppLoader.screenH - internalHeight) / 2,
catBarWidth catBarWidth
) )
val catSelection: Int
get() = categoryBar.selectedIndex
val catSelectedIcon: Int
get() = categoryBar.selectedIcon
override var openCloseTime: Second = 0.0f override var openCloseTime: Second = 0.0f
@@ -181,7 +192,7 @@ class UIInventoryFull(
categoryBar.selectionChangeListener = { old, new -> categoryBar.selectionChangeListener = { old, new ->
rebuildList() rebuildList()
itemList.itemPage = 0 // set scroll to zero itemList.itemPage = 0 // set scroll to zero
itemList.rebuild() // have to manually rebuild, too! itemList.rebuild(catIconsMeaning[catArrangement[new]]) // have to manually rebuild, too!
} }
@@ -525,7 +536,7 @@ class UIInventoryFull(
fun rebuildList() { fun rebuildList() {
printdbg(this, "rebuilding list") printdbg(this, "rebuilding list")
itemList.rebuild() itemList.rebuild(catIconsMeaning[categoryBar.selectedIcon])
equipped.rebuild() equipped.rebuild()
encumbrancePerc = actor.inventory.capacity.toFloat() / actor.inventory.maxCapacity encumbrancePerc = actor.inventory.capacity.toFloat() / actor.inventory.maxCapacity

View File

@@ -4,7 +4,6 @@ import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.* import net.torvald.terrarum.*
import net.torvald.terrarum.gameitem.GameItem
import net.torvald.terrarum.gameworld.fmod import net.torvald.terrarum.gameworld.fmod
import net.torvald.terrarum.itemproperties.ItemCodex import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.modulebasegame.TerrarumIngame import net.torvald.terrarum.modulebasegame.TerrarumIngame
@@ -59,7 +58,8 @@ class UIItemInventoryDynamicList(
CommonResourcePool.loadAll() CommonResourcePool.loadAll()
} }
val catIconsMeaning = listOf( // sortedBy: catArrangement // info regarding cat icon should not be here, move it to the parent call (e.g. UIInventoryFull, CraftingUI)
/*val catIconsMeaning = listOf( // sortedBy: catArrangement
arrayOf(GameItem.Category.WEAPON), arrayOf(GameItem.Category.WEAPON),
arrayOf(GameItem.Category.TOOL, GameItem.Category.WIRE), arrayOf(GameItem.Category.TOOL, GameItem.Category.WIRE),
arrayOf(GameItem.Category.ARMOUR), arrayOf(GameItem.Category.ARMOUR),
@@ -69,22 +69,19 @@ class UIItemInventoryDynamicList(
arrayOf(GameItem.Category.BLOCK), arrayOf(GameItem.Category.BLOCK),
arrayOf(GameItem.Category.WALL), arrayOf(GameItem.Category.WALL),
arrayOf(GameItem.Category.MISC), arrayOf(GameItem.Category.MISC),
arrayOf("__all__") arrayOf(CAT_ALL)
) )*/
private var currentFilter = arrayOf(CAT_ALL)
private val inventoryUI = parentUI private val inventoryUI = parentUI
private val selection: Int //private val selectedIcon: Int
get() = inventoryUI.catSelection // get() = inventoryUI.catSelectedIcon
private val selectedIcon: Int
get() = inventoryUI.catSelectedIcon
private val compactViewCat = setOf(3, 4, 6, 7, 9) // ingredients, potions, blocks, walls, all (spritesheet order)
var itemPage = 0 var itemPage = 0
set(value) { set(value) {
field = if (itemPageCount == 0) 0 else (value).fmod(itemPageCount) field = if (itemPageCount == 0) 0 else (value).fmod(itemPageCount)
rebuild() rebuild(currentFilter)
} }
var itemPageCount = 1 // TODO total size of current category / items.size var itemPageCount = 1 // TODO total size of current category / items.size
private set private set
@@ -111,6 +108,8 @@ class UIItemInventoryDynamicList(
fun getEstimatedW(horizontalCells: Int) = horizontalCells * UIItemInventoryElemSimple.height + (horizontalCells - 1) * listGap fun getEstimatedW(horizontalCells: Int) = horizontalCells * UIItemInventoryElemSimple.height + (horizontalCells - 1) * listGap
fun getEstimatedH(verticalCells: Int) = verticalCells * UIItemInventoryElemSimple.height + (verticalCells - 1) * listGap fun getEstimatedH(verticalCells: Int) = verticalCells * UIItemInventoryElemSimple.height + (verticalCells - 1) * listGap
const val CAT_ALL = "__all__"
} }
private val itemGrid = Array<UIItemInventoryCellBase>(horizontalCells * verticalCells) { private val itemGrid = Array<UIItemInventoryCellBase>(horizontalCells * verticalCells) {
@@ -129,7 +128,7 @@ class UIItemInventoryDynamicList(
inactiveTextCol = defaultTextColour inactiveTextCol = defaultTextColour
) )
} }
// TODO automatically determine how much columns are needed. Minimum width = 5 grids // TODO automatically determine how much columns are needed. Minimum Width = 5 grids
private val itemList = Array<UIItemInventoryCellBase>(verticalCells * 2) { private val itemList = Array<UIItemInventoryCellBase>(verticalCells * 2) {
UIItemInventoryElem( UIItemInventoryElem(
parentUI = inventoryUI, parentUI = inventoryUI,
@@ -153,7 +152,7 @@ class UIItemInventoryDynamicList(
var isCompactMode = false // this is INIT code var isCompactMode = false // this is INIT code
set(value) { set(value) {
items = if (value) itemGrid else itemList items = if (value) itemGrid else itemList
rebuild() rebuild(currentFilter)
field = value field = value
} }
@@ -219,14 +218,14 @@ class UIItemInventoryDynamicList(
gridModeButtons[0].highlighted = true gridModeButtons[0].highlighted = true
gridModeButtons[1].highlighted = false gridModeButtons[1].highlighted = false
itemPage = 0 itemPage = 0
rebuild() rebuild(currentFilter)
} }
gridModeButtons[1].touchDownListener = { _, _, _, _ -> gridModeButtons[1].touchDownListener = { _, _, _, _ ->
isCompactMode = true isCompactMode = true
gridModeButtons[0].highlighted = false gridModeButtons[0].highlighted = false
gridModeButtons[1].highlighted = true gridModeButtons[1].highlighted = true
itemPage = 0 itemPage = 0
rebuild() rebuild(currentFilter)
} }
scrollUpButton.clickOnceListener = { _, _, _ -> scrollUpButton.clickOnceListener = { _, _, _ ->
@@ -333,19 +332,19 @@ class UIItemInventoryDynamicList(
} }
internal fun rebuild(filter: Array<String>) {
internal fun rebuild() {
//println("Rebuilt inventory") //println("Rebuilt inventory")
//println("rebuild: actual itempage: $itemPage") //println("rebuild: actual itempage: $itemPage")
val filter = catIconsMeaning[selectedIcon] //val filter = catIconsMeaning[selectedIcon]
currentFilter = filter
inventorySortList = ArrayList<InventoryPair>() inventorySortList = ArrayList<InventoryPair>()
// filter items // filter items
inventory.forEach { inventory.forEach {
if ((filter.contains(ItemCodex[it.item]!!.inventoryCategory) || filter[0] == "__all__")) if ((filter.contains(ItemCodex[it.item]!!.inventoryCategory) || filter[0] == CAT_ALL))
inventorySortList.add(it) inventorySortList.add(it)
} }
@@ -440,7 +439,7 @@ class UIItemInventoryDynamicList(
super.keyDown(keycode) super.keyDown(keycode)
items.forEach { if (it.mouseUp) it.keyDown(keycode) } items.forEach { if (it.mouseUp) it.keyDown(keycode) }
rebuild() rebuild(currentFilter)
return true return true
} }
@@ -449,7 +448,7 @@ class UIItemInventoryDynamicList(
super.keyUp(keycode) super.keyUp(keycode)
items.forEach { if (it.mouseUp) it.keyUp(keycode) } items.forEach { if (it.mouseUp) it.keyUp(keycode) }
rebuild() rebuild(currentFilter)
return true return true
} }