From 55578ccc1e8fbd331c825697b29177a18a334749 Mon Sep 17 00:00:00 2001 From: Song Minjae Date: Fri, 28 Apr 2017 01:48:51 +0900 Subject: [PATCH] Quickbar: all working except for equip/unequip --- src/net/torvald/terrarum/StateUITest.kt | 1 - .../torvald/terrarum/UIItemInventoryElem.kt | 23 +++ src/net/torvald/terrarum/gameactors/AVKey.kt | 4 +- .../terrarum/gameactors/ActorHumanoid.kt | 5 +- .../terrarum/gameactors/ActorInventory.kt | 2 +- src/net/torvald/terrarum/ui/UIInventory.kt | 140 ++++++++++-------- src/net/torvald/terrarum/ui/UIPieMenu.kt | 2 +- src/net/torvald/terrarum/ui/UIQuickBar.kt | 2 +- 8 files changed, 106 insertions(+), 73 deletions(-) diff --git a/src/net/torvald/terrarum/StateUITest.kt b/src/net/torvald/terrarum/StateUITest.kt index 2ef77c5fe..c048b51d4 100644 --- a/src/net/torvald/terrarum/StateUITest.kt +++ b/src/net/torvald/terrarum/StateUITest.kt @@ -42,7 +42,6 @@ class StateUITest : BasicGameState() { // these are the test codes. // Item properties must be pre-composed using CSV/JSON, and read and made into the item instance // using factory/builder pattern. @see ItemCodex - actor.actorValue[AVKey.__PLAYER_QSPREFIX + "3"] = Block.STONE actor.inventory.add(object : InventoryItem() { init { diff --git a/src/net/torvald/terrarum/UIItemInventoryElem.kt b/src/net/torvald/terrarum/UIItemInventoryElem.kt index 21689bcab..5987d53a7 100644 --- a/src/net/torvald/terrarum/UIItemInventoryElem.kt +++ b/src/net/torvald/terrarum/UIItemInventoryElem.kt @@ -1,6 +1,7 @@ package net.torvald.terrarum import net.torvald.colourutil.CIELabUtil.darkerLab +import net.torvald.terrarum.gamecontroller.Key import net.torvald.terrarum.itemproperties.InventoryItem import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UIInventory @@ -125,6 +126,28 @@ class UIItemInventoryElem( } override fun keyPressed(key: Int, c: Char) { + if (item != null && Terrarum.ingame != null && key in Key.NUM_1..Key.NUM_0) { + val inventory = Terrarum.ingame!!.player?.inventory + val slot = key - Key.NUM_1 + val currentSlotItem = inventory?.getQuickBar(slot) + + + inventory?.setQuickBar( + slot, + if (currentSlotItem?.item != item) + 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?.getQuickBar(it)?.item == item) { + inventory?.setQuickBar(it, null) + } + } + } } override fun keyReleased(key: Int, c: Char) { diff --git a/src/net/torvald/terrarum/gameactors/AVKey.kt b/src/net/torvald/terrarum/gameactors/AVKey.kt index 24eb6af11..0cba40f14 100644 --- a/src/net/torvald/terrarum/gameactors/AVKey.kt +++ b/src/net/torvald/terrarum/gameactors/AVKey.kt @@ -93,9 +93,7 @@ object AVKey { const val __PLAYER_QUICKSLOTSEL = "__quickslotselection" - /** SYNOPSIS: __qsitem1 .. __qsitem10 - * contains tem ID; they are supposed to be unique. Indices must be ONE-BASED! */ - const val __PLAYER_QSPREFIX = "__qsitem" // __qsitem1 .. __qsitem10 (NOT ZERO BASED!) + /** Double * When using tool/arm/etc. how long action button is held, in milliseconds (Int) * Or for NPCs, how long it has been waiting for next move diff --git a/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt b/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt index ba7b696d5..2e846f1ff 100644 --- a/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt +++ b/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt @@ -3,6 +3,7 @@ package net.torvald.terrarum.gameactors import com.jme3.math.FastMath import net.torvald.terrarum.Terrarum import net.torvald.terrarum.gameactors.faction.Faction +import net.torvald.terrarum.gamecontroller.Key import net.torvald.terrarum.itemproperties.InventoryItem import net.torvald.terrarum.itemproperties.Material import net.torvald.terrarum.realestate.LandUtil @@ -333,7 +334,9 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) } override fun keyPressed(key: Int, c: Char) { - + if (key in Key.NUM_1..Key.NUM_0) { + actorValue[AVKey.__PLAYER_QUICKSLOTSEL] = key - Key.NUM_1 + } } /** diff --git a/src/net/torvald/terrarum/gameactors/ActorInventory.kt b/src/net/torvald/terrarum/gameactors/ActorInventory.kt index 9162d0495..d5d96a672 100644 --- a/src/net/torvald/terrarum/gameactors/ActorInventory.kt +++ b/src/net/torvald/terrarum/gameactors/ActorInventory.kt @@ -33,7 +33,7 @@ class ActorInventory(val actor: Pocketed, var maxCapacity: Int, var capacityMode * Sorted by referenceID. */ private val itemList = ArrayList() - private val quickBar = Array(10, { null }) + private val quickBar = Array(10, { null }) // 0: Slot 1, 9: Slot 10 init { } diff --git a/src/net/torvald/terrarum/ui/UIInventory.kt b/src/net/torvald/terrarum/ui/UIInventory.kt index 206bb80f8..172940310 100644 --- a/src/net/torvald/terrarum/ui/UIInventory.kt +++ b/src/net/torvald/terrarum/ui/UIInventory.kt @@ -24,8 +24,8 @@ class UIInventory( val inventory: ActorInventory? get() = actor?.inventory - val actorValue: ActorValue - get() = (actor as Actor).actorValue + //val actorValue: ActorValue + // get() = (actor as Actor).actorValue override var handler: UIHandler? = null override var openCloseTime: Int = 120 @@ -146,67 +146,7 @@ class UIInventory( if (rebuildList) { - val filter = catButtonsToCatIdent[catButtons.selectedButton.labelText] - - // encumbrance - encumbrancePerc = inventory!!.capacity.toFloat() / inventory!!.maxCapacity - isEncumbered = inventory!!.isEncumbered - - - - inventorySortList = ArrayList() - - // filter items - inventory?.forEach { - if (it.item.inventoryCategory == filter || filter == "__all__") - inventorySortList.add(it) - } - - rebuildList = false - - // sort if needed - // test sort by name - inventorySortList.sortBy { it.item.name } - - // map sortList to item list - for (k in 0..items.size - 1) { - // we have an item - try { - val sortListItem = inventorySortList[k + itemsScrollOffset] - items[k].item = sortListItem.item - items[k].amount = sortListItem.amount - items[k].itemImage = ItemCodex.getItemImage(sortListItem.item) - - // set quickslot number - for (qs in 1..QUICKSLOT_MAX) { - if (-sortListItem.item.dynamicID == actorValue.getAsInt(AVKey.__PLAYER_QSPREFIX + qs)) { - items[k].quickslot = qs % 10 // 10 -> 0, 1..9 -> 1..9 - break - } - else - items[k].quickslot = null - } - - // set equippedslot number - for (eq in 0..actor!!.inventory.itemEquipped.size - 1) { - if (eq < actor!!.inventory.itemEquipped.size) { - if (actor!!.inventory.itemEquipped[eq] == items[k].item) { - items[k].equippedSlot = eq - break - } - else - items[k].equippedSlot = null - } - } - } - // we do not have an item, empty the slot - catch (e: IndexOutOfBoundsException) { - items[k].item = null - items[k].amount = 0 - items[k].itemImage = null - items[k].quickslot = null - } - } + shutUpAndRebuild() } } @@ -278,13 +218,76 @@ class UIInventory( } - /** Persuade the UI to rebuild its item list */ fun rebuildList() { rebuildList = true } + fun shutUpAndRebuild() { + val filter = catButtonsToCatIdent[catButtons.selectedButton.labelText] + + // encumbrance + encumbrancePerc = inventory!!.capacity.toFloat() / inventory!!.maxCapacity + isEncumbered = inventory!!.isEncumbered + + + + inventorySortList = ArrayList() + + // filter items + inventory?.forEach { + if (it.item.inventoryCategory == filter || filter == "__all__") + inventorySortList.add(it) + } + + rebuildList = false + + // sort if needed + // test sort by name + inventorySortList.sortBy { it.item.name } + + // map sortList to item list + for (k in 0..items.size - 1) { + // we have an item + try { + val sortListItem = inventorySortList[k + itemsScrollOffset] + items[k].item = sortListItem.item + items[k].amount = sortListItem.amount + items[k].itemImage = ItemCodex.getItemImage(sortListItem.item) + + // set quickslot number + for (qs in 1..QUICKSLOT_MAX) { + if (sortListItem.item == actor?.inventory?.getQuickBar(qs - 1)?.item) { + items[k].quickslot = qs % 10 // 10 -> 0, 1..9 -> 1..9 + break + } + else + items[k].quickslot = null + } + + // set equippedslot number + for (eq in 0..actor!!.inventory.itemEquipped.size - 1) { + if (eq < actor!!.inventory.itemEquipped.size) { + if (actor!!.inventory.itemEquipped[eq] == items[k].item) { + items[k].equippedSlot = eq + break + } + else + items[k].equippedSlot = null + } + } + } + // we do not have an item, empty the slot + catch (e: IndexOutOfBoundsException) { + items[k].item = null + items[k].amount = 0 + items[k].itemImage = null + items[k].quickslot = null + } + } + } + //////////// @@ -312,19 +315,24 @@ class UIInventory( } override fun keyPressed(key: Int, c: Char) { - items.forEach { it.keyPressed(key, c) } + items.forEach { if (it.mouseUp) it.keyPressed(key, c) } + shutUpAndRebuild() } override fun mouseMoved(oldx: Int, oldy: Int, newx: Int, newy: Int) { } override fun keyReleased(key: Int, c: Char) { + items.forEach { if (it.mouseUp) it.keyReleased(key, c) } + shutUpAndRebuild() } override fun mouseDragged(oldx: Int, oldy: Int, newx: Int, newy: Int) { } override fun controllerButtonPressed(controller: Int, button: Int) { + items.forEach { if (it.mouseUp) it.controllerButtonPressed(controller, button) } + shutUpAndRebuild() } override fun mousePressed(button: Int, x: Int, y: Int) { @@ -332,6 +340,8 @@ class UIInventory( } override fun controllerButtonReleased(controller: Int, button: Int) { + items.forEach { if (it.mouseUp) it.controllerButtonReleased(controller, button) } + shutUpAndRebuild() } override fun mouseReleased(button: Int, x: Int, y: Int) { diff --git a/src/net/torvald/terrarum/ui/UIPieMenu.kt b/src/net/torvald/terrarum/ui/UIPieMenu.kt index 080cb9daa..aa7ae92f7 100644 --- a/src/net/torvald/terrarum/ui/UIPieMenu.kt +++ b/src/net/torvald/terrarum/ui/UIPieMenu.kt @@ -87,7 +87,7 @@ class UIPieMenu : UICanvas { itemImage, // using fixed CELL_SIZE for reasons slotX + (CELL_SIZE - itemW) / 2f, slotY + (CELL_SIZE - itemH) / 2f, - Color(1f, 1f, 1f, handler!!.opacity * UIQuickBar.finalOpacity) + Color(1f, 1f, 1f, handler!!.opacity) ) } } diff --git a/src/net/torvald/terrarum/ui/UIQuickBar.kt b/src/net/torvald/terrarum/ui/UIQuickBar.kt index 8df87093a..213978d49 100644 --- a/src/net/torvald/terrarum/ui/UIQuickBar.kt +++ b/src/net/torvald/terrarum/ui/UIQuickBar.kt @@ -70,7 +70,7 @@ class UIQuickBar : UICanvas, MouseControlled { itemImage, // using fixed CELL_SIZE for reasons slotX + (CELL_SIZE - itemW) / 2f, slotY + (CELL_SIZE - itemH) / 2f, - Color(1f, 1f, 1f, handler!!.opacity * UIQuickBar.finalOpacity) + Color(1f, 1f, 1f, handler!!.opacity) ) } }