mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 18:44:05 +09:00
crafting ui nearly complete
This commit is contained in:
@@ -106,7 +106,7 @@ open class FixtureInventory() {
|
||||
val newCount = existingItem.qty - count
|
||||
|
||||
/*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) {
|
||||
// decrement count
|
||||
@@ -120,7 +120,7 @@ open class FixtureInventory() {
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
override fun compareTo(other: InventoryPair) = this.itm.compareTo(other.itm)
|
||||
}
|
||||
}
|
||||
|
||||
class InventoryTransactionFailedError(msg: String) : Error(msg)
|
||||
@@ -77,7 +77,7 @@ internal class UIStorageChest : UICanvas(
|
||||
override var height = App.scr.height
|
||||
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) {
|
||||
player.remove(item, amount)
|
||||
fixture.add(item, amount)
|
||||
|
||||
@@ -42,7 +42,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
private val craftables = FixtureInventory() // might be changed to something else
|
||||
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) {
|
||||
// TODO()
|
||||
}
|
||||
@@ -81,6 +81,9 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
|
||||
private val oldSelectedItems = ArrayList<ItemID>()
|
||||
|
||||
private val craftMult
|
||||
get() = spinnerCraftCount.value.toLong()
|
||||
|
||||
init {
|
||||
val craftButtonsY = thisOffsetY + 23 + (UIItemInventoryElemWide.height + listGap) * (UIInventoryFull.CELLS_VRT - 1)
|
||||
val buttonWidth = (UIItemInventoryElemWide.height + listGap) * 3 - listGap - 2
|
||||
@@ -96,6 +99,9 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
drawScrollOnRightside = false,
|
||||
drawWallet = false,
|
||||
hideSidebar = true,
|
||||
colourTheme = UIItemInventoryCellCommonRes.defaultInventoryCellTheme.copy(
|
||||
cellHighlightSubCol = Toolkit.Theme.COL_INACTIVE
|
||||
),
|
||||
keyDownFun = { _, _, _, _, _ -> },
|
||||
touchDownFun = { _, _, _, _, _ -> }
|
||||
)
|
||||
@@ -104,6 +110,14 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
itemListIngredients.gridModeButtons[0].touchDownListener = { _,_,_,_ -> }
|
||||
itemListIngredients.gridModeButtons[1].touchDownListener = { _,_,_,_ -> }
|
||||
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
|
||||
@@ -137,6 +151,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
(it.keyMode == CraftingCodex.CraftingItemKeyMode.TAG && ItemCodex[itemPair.itm]!!.tags.contains(it.key))
|
||||
}
|
||||
changeIngredient(oldItem, itemID)
|
||||
refreshCraftButtonStatus()
|
||||
}
|
||||
} } }
|
||||
)
|
||||
@@ -195,6 +210,8 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
|
||||
oldSelectedItems.clear()
|
||||
oldSelectedItems.addAll(selectedItems)
|
||||
|
||||
refreshCraftButtonStatus()
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -205,25 +222,31 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
itemListIngredients.rebuild(catAll)
|
||||
itemListCraftable.numberMultiplier = it.toLong()
|
||||
itemListCraftable.rebuild(catAll)
|
||||
refreshCraftButtonStatus()
|
||||
}
|
||||
|
||||
|
||||
buttonCraft.touchDownListener = { _,_,_,_ ->
|
||||
getPlayerInventory().let { player -> recipeClicked?.let { recipe ->
|
||||
val mult = spinnerCraftCount.value.toLong()
|
||||
|
||||
// TODO check if player has enough amount of ingredients
|
||||
|
||||
itemListIngredients.getInventory().itemList.forEach { (itm, qty) ->
|
||||
player.remove(itm, qty * mult)
|
||||
// check if player has enough amount of ingredients
|
||||
val itemCraftable = itemListIngredients.getInventory().itemList.all { (itm, qty) ->
|
||||
(player.searchByID(itm)?.qty ?: -1) >= qty * craftMult
|
||||
}
|
||||
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
|
||||
itemListPlayer.rebuild(catAll)
|
||||
itemListCraftable.rebuild(catAll)
|
||||
|
||||
if (itemCraftable) {
|
||||
itemListIngredients.getInventory().itemList.forEach { (itm, qty) ->
|
||||
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
|
||||
// itemListCraftable.gridModeButtons[0].touchDownListener = { _,_,_,_ -> setCompact(false) }
|
||||
@@ -260,7 +283,22 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
itemListCraftable.highlightButton(button)
|
||||
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
|
||||
fun resetUI() {
|
||||
// reset spinner
|
||||
@@ -274,6 +312,8 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
ingredients.clear()
|
||||
itemListPlayer.removeFromForceHighlightList(oldSelectedItems)
|
||||
itemListIngredients.rebuild(catAll)
|
||||
|
||||
refreshCraftButtonStatus()
|
||||
}
|
||||
|
||||
private var openingClickLatched = false
|
||||
|
||||
@@ -4,7 +4,7 @@ import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory
|
||||
|
||||
interface HasInventory {
|
||||
|
||||
fun getNegotiator(): InventoryNegotiator
|
||||
fun getNegotiator(): InventoryTransactionNegotiator
|
||||
fun getFixtureInventory(): FixtureInventory
|
||||
fun getPlayerInventory(): FixtureInventory
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory
|
||||
/**
|
||||
* 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 */
|
||||
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... */
|
||||
@@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion
|
||||
import net.torvald.terrarum.GdxColorMap
|
||||
import net.torvald.terrarum.gameitems.GameItem
|
||||
import net.torvald.terrarum.ui.Toolkit
|
||||
import net.torvald.terrarum.ui.UICanvas
|
||||
import net.torvald.terrarum.ui.UIItem
|
||||
import kotlin.math.absoluteValue
|
||||
@@ -34,6 +35,14 @@ abstract class UIItemInventoryCellBase(
|
||||
abstract override fun update(delta: Float)
|
||||
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
|
||||
/*set(value) {
|
||||
if (field != value) {
|
||||
@@ -85,4 +94,26 @@ object UIItemInventoryCellCommonRes {
|
||||
|
||||
// -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,
|
||||
)
|
||||
@@ -14,6 +14,7 @@ import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
||||
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.modulebasegame.ui.UIItemInventoryCellCommonRes.defaultInventoryCellTheme
|
||||
import net.torvald.terrarum.ui.Toolkit
|
||||
import net.torvald.terrarum.ui.UICanvas
|
||||
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
|
||||
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
|
||||
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) {
|
||||
|
||||
// deal with the moving position
|
||||
@@ -186,7 +188,8 @@ open class UIItemInventoryItemGrid(
|
||||
drawBackOnNull = true,
|
||||
keyDownFun = keyDownFun,
|
||||
touchDownFun = touchDownFun,
|
||||
highlightEquippedItem = highlightEquippedItem
|
||||
highlightEquippedItem = highlightEquippedItem,
|
||||
colourTheme = colourTheme
|
||||
)
|
||||
}
|
||||
// automatically determine how much columns are needed. Minimum Width = 5 grids
|
||||
@@ -205,7 +208,8 @@ open class UIItemInventoryItemGrid(
|
||||
drawBackOnNull = true,
|
||||
keyDownFun = keyDownFun,
|
||||
touchDownFun = touchDownFun,
|
||||
highlightEquippedItem = highlightEquippedItem
|
||||
highlightEquippedItem = highlightEquippedItem,
|
||||
colourTheme = colourTheme
|
||||
)
|
||||
}
|
||||
|
||||
@@ -266,6 +270,16 @@ open class UIItemInventoryItemGrid(
|
||||
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) {
|
||||
itemPage = if (itemPageCount == 0) 0 else (itemPage + relativeAmount).fmod(itemPageCount)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user