crafting ui nearly complete

This commit is contained in:
minjaesong
2022-07-02 23:28:33 +09:00
parent 4eb3ad07f3
commit 523dd458ce
12 changed files with 189 additions and 64 deletions

View File

@@ -5,8 +5,10 @@ import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.modulebasegame.ui.InventoryCellColourTheme
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellBase import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellBase
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.defaultInventoryCellTheme
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.toItemCountText import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.toItemCountText
import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.Toolkit
import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UICanvas
@@ -28,7 +30,8 @@ class UIItemInventoryElemSimple(
keyDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Keycode, extra info, self keyDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Keycode, extra info, self
touchDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Button, extra info, self touchDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Button, extra info, self
extraInfo: Any? = null, extraInfo: Any? = null,
highlightEquippedItem: Boolean = true // for some UIs that only cares about getting equipped slot number but not highlighting highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting
var colourTheme: InventoryCellColourTheme = defaultInventoryCellTheme
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem) { ) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem) {
companion object { companion object {
@@ -47,18 +50,25 @@ class UIItemInventoryElemSimple(
} }
private var highlightToMainCol = false
private var highlightToSubCol = false
override fun render(batch: SpriteBatch, camera: Camera) { override fun render(batch: SpriteBatch, camera: Camera) {
blendNormal(batch) blendNormal(batch)
highlightToMainCol = customHighlightRuleMain?.invoke(this) ?: (equippedSlot != null && highlightEquippedItem) || forceHighlighted
highlightToSubCol = customHighlightRule2?.invoke(this) ?: false
// cell background // cell background
if (item != null || drawBackOnNull) { if (item != null || drawBackOnNull) {
batch.color = Toolkit.Theme.COL_CELL_FILL batch.color = Toolkit.Theme.COL_CELL_FILL
Toolkit.fillArea(batch, posX, posY, width, height) Toolkit.fillArea(batch, posX, posY, width, height)
} }
// cell border // cell border
batch.color = if ((equippedSlot != null && highlightEquippedItem) || forceHighlighted) Toolkit.Theme.COL_HIGHLIGHT batch.color = if (highlightToMainCol) colourTheme.cellHighlightMainCol
else if (mouseUp && item != null) Toolkit.Theme.COL_ACTIVE else if (highlightToSubCol) colourTheme.cellHighlightSubCol
else Toolkit.Theme.COL_INVENTORY_CELL_BORDER else if (mouseUp && item != null) colourTheme.cellHighlightMouseUpCol
else colourTheme.cellHighlightNormalCol
Toolkit.drawBoxBorder(batch, posX, posY, width, height) Toolkit.drawBoxBorder(batch, posX, posY, width, height)
@@ -96,10 +106,10 @@ class UIItemInventoryElemSimple(
// if mouse is over, text lights up // if mouse is over, text lights up
// highlight item count (blocks/walls) if the item is equipped // highlight item count (blocks/walls) if the item is equipped
batch.color = item!!.nameColour mul ( batch.color = item!!.nameColour mul (
if ((equippedSlot != null && highlightEquippedItem) || forceHighlighted) Toolkit.Theme.COL_HIGHLIGHT if (highlightToMainCol) colourTheme.textHighlightMainCol
else if (mouseUp && item != null) Toolkit.Theme.COL_ACTIVE else if (highlightToSubCol) colourTheme.textHighlightSubCol
else Color.WHITE else if (mouseUp && item != null) colourTheme.textHighlightMouseUpCol
) else colourTheme.textHighlightNormalCol)
App.fontSmallNumbers.draw(batch, App.fontSmallNumbers.draw(batch,
amountString, amountString,

View File

@@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.modulebasegame.ui.InventoryCellColourTheme
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellBase import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellBase
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.toItemCountText import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.toItemCountText
@@ -31,7 +32,8 @@ class UIItemInventoryElemWide(
keyDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Keycode, extra info, self keyDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Keycode, extra info, self
touchDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Button, extra info, self touchDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Button, extra info, self
extraInfo: Any? = null, extraInfo: Any? = null,
highlightEquippedItem: Boolean = true // for some UIs that only cares about getting equipped slot number but not highlighting highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting
var colourTheme: InventoryCellColourTheme = UIItemInventoryCellCommonRes.defaultInventoryCellTheme
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem) { ) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem) {
companion object { companion object {
@@ -63,20 +65,35 @@ class UIItemInventoryElemWide(
} }
} }
private val fwsp = 0x3000.toChar() private var highlightToMainCol = false
private var highlightToSubCol = false
var cellHighlightMainCol = Toolkit.Theme.COL_HIGHLIGHT
var cellHighlightSubCol = Toolkit.Theme.COL_LIST_DEFAULT
var cellHighlightMouseUpCol = Toolkit.Theme.COL_ACTIVE
var cellHighlightNormalCol = Toolkit.Theme.COL_INVENTORY_CELL_BORDER
var textHighlightMainCol = Toolkit.Theme.COL_HIGHLIGHT
var textHighlightSubCol = Color.WHITE
var textHighlightMouseUpCol = Toolkit.Theme.COL_ACTIVE
var textHighlightNormalCol = Color.WHITE
override fun render(batch: SpriteBatch, camera: Camera) { override fun render(batch: SpriteBatch, camera: Camera) {
blendNormal(batch) blendNormal(batch)
highlightToMainCol = customHighlightRuleMain?.invoke(this) ?: (equippedSlot != null && highlightEquippedItem) || forceHighlighted
highlightToSubCol = customHighlightRule2?.invoke(this) ?: false
// cell background // cell background
if (item != null || drawBackOnNull) { if (item != null || drawBackOnNull) {
batch.color = Toolkit.Theme.COL_CELL_FILL batch.color = Toolkit.Theme.COL_CELL_FILL
Toolkit.fillArea(batch, posX, posY, width, height) Toolkit.fillArea(batch, posX, posY, width, height)
} }
// cell border // cell border
batch.color = if ((equippedSlot != null && highlightEquippedItem) || forceHighlighted) Toolkit.Theme.COL_HIGHLIGHT batch.color = if (highlightToMainCol) colourTheme.cellHighlightMainCol
else if (mouseUp && item != null) Toolkit.Theme.COL_ACTIVE else if (highlightToSubCol) colourTheme.cellHighlightSubCol
else Toolkit.Theme.COL_INVENTORY_CELL_BORDER else if (mouseUp && item != null) colourTheme.cellHighlightMouseUpCol
else colourTheme.cellHighlightNormalCol
Toolkit.drawBoxBorder(batch, posX, posY, width, height) Toolkit.drawBoxBorder(batch, posX, posY, width, height)
@@ -92,15 +109,15 @@ class UIItemInventoryElemWide(
// if mouse is over, text lights up // if mouse is over, text lights up
// highlight item name and count (blocks/walls) if the item is equipped // highlight item name and count (blocks/walls) if the item is equipped
batch.color = item!!.nameColour mul ( batch.color = item!!.nameColour mul (
if ((equippedSlot != null && highlightEquippedItem) || forceHighlighted) Toolkit.Theme.COL_HIGHLIGHT if (highlightToMainCol) colourTheme.textHighlightMainCol
else if (mouseUp && item != null) Toolkit.Theme.COL_ACTIVE else if (highlightToSubCol) colourTheme.textHighlightSubCol
else Color.WHITE else if (mouseUp && item != null) colourTheme.textHighlightMouseUpCol
) else colourTheme.textHighlightNormalCol)
// draw name of the item // draw name of the item
App.fontGame.draw(batch, App.fontGame.draw(batch,
// print name and amount in parens // print name and amount in parens
item!!.name + (if (amount > 0 && item!!.stackable) "$fwsp($amountString)" else if (amount != 1L) "$fwsp!!$amountString!!" else ""), item!!.name + (if (amount > 0 && item!!.stackable) "\u3000($amountString)" else if (amount != 1L) "\u3000!!$amountString!!" else ""),
posX + textOffsetX, posX + textOffsetX,
posY + textOffsetY posY + textOffsetY

View File

@@ -106,7 +106,7 @@ open class FixtureInventory() {
val newCount = existingItem.qty - count val newCount = existingItem.qty - count
/*if (newCount < 0) { /*if (newCount < 0) {
throw Error("[${this.javaClass.canonicalName}] Tried to remove $count of $item, but the inventory only contains ${existingItem.qty} of them.") throw InventoryFailedTransactionError("[${this.javaClass.canonicalName}] Tried to remove $count of $item, but the inventory only contains ${existingItem.qty} of them.")
} }
else*/ if (newCount > 0) { else*/ if (newCount > 0) {
// decrement count // decrement count
@@ -120,7 +120,7 @@ open class FixtureInventory() {
} }
} }
else { else {
// throw Error("[${this.javaClass.canonicalName}] Tried to remove $item, but the inventory does not have it.") // throw InventoryFailedTransactionError("[${this.javaClass.canonicalName}] Tried to remove $item, but the inventory does not have it.")
} }
} }
@@ -234,4 +234,6 @@ class InventoryPair : Comparable<InventoryPair> {
operator fun component2() = qty operator fun component2() = qty
override fun compareTo(other: InventoryPair) = this.itm.compareTo(other.itm) override fun compareTo(other: InventoryPair) = this.itm.compareTo(other.itm)
} }
class InventoryTransactionFailedError(msg: String) : Error(msg)

View File

@@ -77,7 +77,7 @@ internal class UIStorageChest : UICanvas(
override var height = App.scr.height override var height = App.scr.height
override var openCloseTime: Second = 0.0f override var openCloseTime: Second = 0.0f
private val negotiator = object : InventoryNegotiator() { private val negotiator = object : InventoryTransactionNegotiator() {
override fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Long) { override fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Long) {
player.remove(item, amount) player.remove(item, amount)
fixture.add(item, amount) fixture.add(item, amount)

View File

@@ -42,7 +42,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
private val craftables = FixtureInventory() // might be changed to something else private val craftables = FixtureInventory() // might be changed to something else
private val ingredients = FixtureInventory() // this one is definitely not to be changed private val ingredients = FixtureInventory() // this one is definitely not to be changed
private val negotiator = object : InventoryNegotiator() { private val negotiator = object : InventoryTransactionNegotiator() {
override fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Long) { override fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Long) {
// TODO() // TODO()
} }
@@ -81,6 +81,9 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
private val oldSelectedItems = ArrayList<ItemID>() private val oldSelectedItems = ArrayList<ItemID>()
private val craftMult
get() = spinnerCraftCount.value.toLong()
init { init {
val craftButtonsY = thisOffsetY + 23 + (UIItemInventoryElemWide.height + listGap) * (UIInventoryFull.CELLS_VRT - 1) val craftButtonsY = thisOffsetY + 23 + (UIItemInventoryElemWide.height + listGap) * (UIInventoryFull.CELLS_VRT - 1)
val buttonWidth = (UIItemInventoryElemWide.height + listGap) * 3 - listGap - 2 val buttonWidth = (UIItemInventoryElemWide.height + listGap) * 3 - listGap - 2
@@ -96,6 +99,9 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
drawScrollOnRightside = false, drawScrollOnRightside = false,
drawWallet = false, drawWallet = false,
hideSidebar = true, hideSidebar = true,
colourTheme = UIItemInventoryCellCommonRes.defaultInventoryCellTheme.copy(
cellHighlightSubCol = Toolkit.Theme.COL_INACTIVE
),
keyDownFun = { _, _, _, _, _ -> }, keyDownFun = { _, _, _, _, _ -> },
touchDownFun = { _, _, _, _, _ -> } touchDownFun = { _, _, _, _, _ -> }
) )
@@ -104,6 +110,14 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
itemListIngredients.gridModeButtons[0].touchDownListener = { _,_,_,_ -> } itemListIngredients.gridModeButtons[0].touchDownListener = { _,_,_,_ -> }
itemListIngredients.gridModeButtons[1].touchDownListener = { _,_,_,_ -> } itemListIngredients.gridModeButtons[1].touchDownListener = { _,_,_,_ -> }
itemListIngredients.isCompactMode = true itemListIngredients.isCompactMode = true
itemListIngredients.setCustomHighlightRuleSub {
it.item?.let { ingredient ->
return@setCustomHighlightRuleSub getPlayerInventory().searchByID(ingredient.dynamicID)?.let { itemOnPlayer ->
itemOnPlayer.qty * craftMult >= it.amount * craftMult
} == true
}
false
}
// player inventory to the right // player inventory to the right
@@ -137,6 +151,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
(it.keyMode == CraftingCodex.CraftingItemKeyMode.TAG && ItemCodex[itemPair.itm]!!.tags.contains(it.key)) (it.keyMode == CraftingCodex.CraftingItemKeyMode.TAG && ItemCodex[itemPair.itm]!!.tags.contains(it.key))
} }
changeIngredient(oldItem, itemID) changeIngredient(oldItem, itemID)
refreshCraftButtonStatus()
} }
} } } } } }
) )
@@ -195,6 +210,8 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
oldSelectedItems.clear() oldSelectedItems.clear()
oldSelectedItems.addAll(selectedItems) oldSelectedItems.addAll(selectedItems)
refreshCraftButtonStatus()
} }
} }
) )
@@ -205,25 +222,31 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
itemListIngredients.rebuild(catAll) itemListIngredients.rebuild(catAll)
itemListCraftable.numberMultiplier = it.toLong() itemListCraftable.numberMultiplier = it.toLong()
itemListCraftable.rebuild(catAll) itemListCraftable.rebuild(catAll)
refreshCraftButtonStatus()
} }
buttonCraft.touchDownListener = { _,_,_,_ -> buttonCraft.touchDownListener = { _,_,_,_ ->
getPlayerInventory().let { player -> recipeClicked?.let { recipe -> getPlayerInventory().let { player -> recipeClicked?.let { recipe ->
val mult = spinnerCraftCount.value.toLong() // check if player has enough amount of ingredients
val itemCraftable = itemListIngredients.getInventory().itemList.all { (itm, qty) ->
// TODO check if player has enough amount of ingredients (player.searchByID(itm)?.qty ?: -1) >= qty * craftMult
itemListIngredients.getInventory().itemList.forEach { (itm, qty) ->
player.remove(itm, qty * mult)
} }
player.add(recipe.product, recipe.moq * mult)
// reset selection status after a crafting to hide the possible artefact where no-longer-craftable items are still displayed due to ingredient depletion
resetUI() // also clears forcehighlightlist if (itemCraftable) {
itemListPlayer.rebuild(catAll) itemListIngredients.getInventory().itemList.forEach { (itm, qty) ->
itemListCraftable.rebuild(catAll) player.remove(itm, qty * craftMult)
}
player.add(recipe.product, recipe.moq * craftMult)
// reset selection status after a crafting to hide the possible artefact where no-longer-craftable items are still displayed due to ingredient depletion
resetUI() // also clears forcehighlightlist
itemListPlayer.rebuild(catAll)
itemListCraftable.rebuild(catAll)
}
} } } }
refreshCraftButtonStatus()
} }
// make grid mode buttons work together // make grid mode buttons work together
// itemListCraftable.gridModeButtons[0].touchDownListener = { _,_,_,_ -> setCompact(false) } // itemListCraftable.gridModeButtons[0].touchDownListener = { _,_,_,_ -> setCompact(false) }
@@ -260,7 +283,22 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
itemListCraftable.highlightButton(button) itemListCraftable.highlightButton(button)
itemListCraftable.rebuild(catAll) itemListCraftable.rebuild(catAll)
} }
/**
* Updates Craft! button so that the button is correctly highlighted
*/
fun refreshCraftButtonStatus() {
val itemCraftable = if (itemListIngredients.getInventory().itemList.size < 1) false
else getPlayerInventory().let { player ->
// check if player has enough amount of ingredients
itemListIngredients.getInventory().itemList.all { (itm, qty) ->
(player.searchByID(itm)?.qty ?: -1) >= qty * craftMult
}
}
buttonCraft.isActive = itemCraftable
}
// reset whatever player has selected to null and bring UI to its initial state // reset whatever player has selected to null and bring UI to its initial state
fun resetUI() { fun resetUI() {
// reset spinner // reset spinner
@@ -274,6 +312,8 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
ingredients.clear() ingredients.clear()
itemListPlayer.removeFromForceHighlightList(oldSelectedItems) itemListPlayer.removeFromForceHighlightList(oldSelectedItems)
itemListIngredients.rebuild(catAll) itemListIngredients.rebuild(catAll)
refreshCraftButtonStatus()
} }
private var openingClickLatched = false private var openingClickLatched = false

View File

@@ -4,7 +4,7 @@ import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory
interface HasInventory { interface HasInventory {
fun getNegotiator(): InventoryNegotiator fun getNegotiator(): InventoryTransactionNegotiator
fun getFixtureInventory(): FixtureInventory fun getFixtureInventory(): FixtureInventory
fun getPlayerInventory(): FixtureInventory fun getPlayerInventory(): FixtureInventory

View File

@@ -7,7 +7,7 @@ import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory
/** /**
* Created by minjaesong on 2021-03-12. * Created by minjaesong on 2021-03-12.
*/ */
abstract class InventoryNegotiator { abstract class InventoryTransactionNegotiator {
/** Retrieve item filter to be used to show only the acceptable items when player's own inventory is being displayed */ /** 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 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... */ /** Accepts item from the player and pass it to right inventory (object), slot (UI), etc... */

View File

@@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.GdxColorMap import net.torvald.terrarum.GdxColorMap
import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.ui.Toolkit
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 kotlin.math.absoluteValue import kotlin.math.absoluteValue
@@ -34,6 +35,14 @@ abstract class UIItemInventoryCellBase(
abstract override fun update(delta: Float) abstract override fun update(delta: Float)
abstract override fun render(batch: SpriteBatch, camera: Camera) abstract override fun render(batch: SpriteBatch, camera: Camera)
/** Custom highlight rule to highlight tihs button to primary accent colour (blue by default).
* Set to `null` to use default rule:
*
* "`equippedSlot` defined and set to `highlightEquippedItem`" or "`forceHighlighted`" */
var customHighlightRuleMain: ((UIItemInventoryCellBase) -> Boolean)? = null
/** Custom highlight rule to highlight this button to secondary accent colour (yellow by default). Set to `null` to use default rule (which does nothing). */
var customHighlightRule2: ((UIItemInventoryCellBase) -> Boolean)? = null
var forceHighlighted = false var forceHighlighted = false
/*set(value) { /*set(value) {
if (field != value) { if (field != value) {
@@ -85,4 +94,26 @@ object UIItemInventoryCellCommonRes {
// -2.14B // -2.14B
} }
}
val defaultInventoryCellTheme = InventoryCellColourTheme(
Toolkit.Theme.COL_HIGHLIGHT,
Toolkit.Theme.COL_LIST_DEFAULT,
Toolkit.Theme.COL_ACTIVE,
Toolkit.Theme.COL_INVENTORY_CELL_BORDER,
Toolkit.Theme.COL_HIGHLIGHT,
Color.WHITE,
Toolkit.Theme.COL_ACTIVE,
Color.WHITE,
)
}
data class InventoryCellColourTheme(
val cellHighlightMainCol: Color,
val cellHighlightSubCol: Color,
val cellHighlightMouseUpCol: Color,
val cellHighlightNormalCol: Color,
val textHighlightMainCol: Color,
val textHighlightSubCol: Color,
val textHighlightMouseUpCol: Color,
val textHighlightNormalCol: Color,
)

View File

@@ -14,6 +14,7 @@ import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory
import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory
import net.torvald.terrarum.modulebasegame.gameactors.InventoryPair import net.torvald.terrarum.modulebasegame.gameactors.InventoryPair
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.defaultInventoryCellTheme
import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.Toolkit
import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.ui.UIItem import net.torvald.terrarum.ui.UIItem
@@ -46,7 +47,8 @@ open class UIItemInventoryItemGrid(
keyDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Keycode, extra info, self keyDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Keycode, extra info, self
touchDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Button, extra info, self touchDownFun: (GameItem?, Long, Int, Any?, UIItemInventoryCellBase) -> Unit, // Item, Amount, Button, extra info, self
protected val useHighlightingManager: Boolean = true, // only used by UIItemCraftingCandidateGrid which addresses buttons directly to set highlighting protected val useHighlightingManager: Boolean = true, // only used by UIItemCraftingCandidateGrid which addresses buttons directly to set highlighting
open protected val highlightEquippedItem: Boolean = true // for some UIs that only cares about getting equipped slot number but not highlighting open protected val highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting
colourTheme: InventoryCellColourTheme = defaultInventoryCellTheme
) : UIItem(parentUI, initialX, initialY) { ) : UIItem(parentUI, initialX, initialY) {
// deal with the moving position // deal with the moving position
@@ -186,7 +188,8 @@ open class UIItemInventoryItemGrid(
drawBackOnNull = true, drawBackOnNull = true,
keyDownFun = keyDownFun, keyDownFun = keyDownFun,
touchDownFun = touchDownFun, touchDownFun = touchDownFun,
highlightEquippedItem = highlightEquippedItem highlightEquippedItem = highlightEquippedItem,
colourTheme = colourTheme
) )
} }
// automatically determine how much columns are needed. Minimum Width = 5 grids // automatically determine how much columns are needed. Minimum Width = 5 grids
@@ -205,7 +208,8 @@ open class UIItemInventoryItemGrid(
drawBackOnNull = true, drawBackOnNull = true,
keyDownFun = keyDownFun, keyDownFun = keyDownFun,
touchDownFun = touchDownFun, touchDownFun = touchDownFun,
highlightEquippedItem = highlightEquippedItem highlightEquippedItem = highlightEquippedItem,
colourTheme = colourTheme
) )
} }
@@ -266,6 +270,16 @@ open class UIItemInventoryItemGrid(
highlightable = false highlightable = false
) )
fun setCustomHighlightRuleMain(predicate: ((UIItemInventoryCellBase) -> Boolean)?) {
itemGrid.forEach { it.customHighlightRuleMain = predicate }
itemList.forEach { it.customHighlightRuleMain = predicate }
}
fun setCustomHighlightRuleSub(predicate: ((UIItemInventoryCellBase) -> Boolean)?) {
itemGrid.forEach { it.customHighlightRule2 = predicate }
itemList.forEach { it.customHighlightRule2 = predicate }
}
fun scrollItemPage(relativeAmount: Int) { fun scrollItemPage(relativeAmount: Int) {
itemPage = if (itemPageCount == 0) 0 else (itemPage + relativeAmount).fmod(itemPageCount) itemPage = if (itemPageCount == 0) 0 else (itemPage + relativeAmount).fmod(itemPageCount)
} }

View File

@@ -125,6 +125,11 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
* gamepad button events for one or more UIItems in one or more UICanvases. */ * gamepad button events for one or more UIItems in one or more UICanvases. */
open var controllerInFocus = false open var controllerInFocus = false
/**
* Whether the button is "available" or not to the player
*/
open var isActive = true
open fun show() {} open fun show() {}
open fun hide() {} open fun hide() {}
@@ -136,23 +141,24 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
updateListener!!.invoke(delta) updateListener!!.invoke(delta)
} }
if (isActive) {
mouseOverCall?.update(delta)
mouseOverCall?.update(delta) if (mouseUp) {
if (mouseOverCall?.isVisible ?: false) {
mouseOverCall?.setAsOpen()
}
if (mouseUp) { mouseOverCall?.updateUI(delta)
if (mouseOverCall?.isVisible ?: false) {
mouseOverCall?.setAsOpen()
} }
else {
mouseOverCall?.updateUI(delta) if (mouseOverCall?.isVisible ?: false) {
} mouseOverCall?.setAsClose()
else { }
if (mouseOverCall?.isVisible ?: false) {
mouseOverCall?.setAsClose()
} }
mouseLatched = false
} }
if (!mouseUp) mouseLatched = false
} }
} }
@@ -161,17 +167,19 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
*/ */
open fun render(batch: SpriteBatch, camera: Camera) { open fun render(batch: SpriteBatch, camera: Camera) {
if (parentUI.isVisible) { if (parentUI.isVisible) {
mouseOverCall?.render(batch, camera) if (isActive) {
mouseOverCall?.render(batch, camera)
if (mouseUp) { if (mouseUp) {
mouseOverCall?.renderUI(batch, camera) mouseOverCall?.renderUI(batch, camera)
}
} }
} }
} }
// keyboard controlled // keyboard controlled
open fun keyDown(keycode: Int): Boolean { open fun keyDown(keycode: Int): Boolean {
if (parentUI.isVisible && keyDownListener != null) { if (parentUI.isVisible && keyDownListener != null && isActive) {
keyDownListener!!.invoke(keycode) keyDownListener!!.invoke(keycode)
return true return true
} }
@@ -179,7 +187,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
return false return false
} }
open fun keyUp(keycode: Int): Boolean { open fun keyUp(keycode: Int): Boolean {
if (parentUI.isVisible && keyUpListener != null) { if (parentUI.isVisible && keyUpListener != null && isActive) {
keyUpListener!!.invoke(keycode) keyUpListener!!.invoke(keycode)
return true return true
} }
@@ -187,7 +195,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
return false return false
} }
open fun keyTyped(character: Char): Boolean { open fun keyTyped(character: Char): Boolean {
if (parentUI.isVisible && keyTypedListener != null) { if (parentUI.isVisible && keyTypedListener != null && isActive) {
keyTypedListener!!.invoke(character) keyTypedListener!!.invoke(character)
return true return true
} }
@@ -197,7 +205,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
// mouse controlled // mouse controlled
open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean { open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
if (parentUI.isVisible && touchDraggedListener != null) { if (parentUI.isVisible && touchDraggedListener != null && isActive) {
touchDraggedListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer) touchDraggedListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer)
return true return true
} }
@@ -207,7 +215,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
open fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { open fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
var actionDone = false var actionDone = false
if (parentUI.isVisible) { if (parentUI.isVisible && isActive) {
if (touchDownListener != null && mouseUp) { if (touchDownListener != null && mouseUp) {
touchDownListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer, button) touchDownListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer, button)
actionDone = true actionDone = true
@@ -232,7 +240,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
return false return false
} }
open fun scrolled(amountX: Float, amountY: Float): Boolean { open fun scrolled(amountX: Float, amountY: Float): Boolean {
if (parentUI.isVisible && scrolledListener != null && mouseUp) { if (parentUI.isVisible && scrolledListener != null && mouseUp && isActive) {
scrolledListener!!.invoke(amountX, amountY) scrolledListener!!.invoke(amountX, amountY)
return true return true
} }

View File

@@ -37,6 +37,8 @@ open class UIItemTextButton(
/** Colour on normal status */ /** Colour on normal status */
val inactiveCol: Color = Toolkit.Theme.COL_LIST_DEFAULT, val inactiveCol: Color = Toolkit.Theme.COL_LIST_DEFAULT,
val disabledCol: Color = Toolkit.Theme.COL_INVENTORY_CELL_BORDER,
val hasBorder: Boolean = false, val hasBorder: Boolean = false,
val paddingLeft: Int = 0, val paddingLeft: Int = 0,
@@ -108,7 +110,8 @@ open class UIItemTextButton(
} }
batch.color = if (highlighted) highlightCol batch.color = if (!isActive) disabledCol
else if (highlighted) highlightCol
else if (mouseUp) activeCol else if (mouseUp) activeCol
else inactiveCol else inactiveCol

View File

@@ -99,7 +99,7 @@ class UIItemTextLineInput(
false false
) )
var isActive: Boolean = false // keep it false by default! override var isActive: Boolean = false // keep it false by default!
set(value) { set(value) {
if (field && !value) endComposing(true) if (field && !value) endComposing(true)
field = value field = value