crafting ui working (visuals only)

This commit is contained in:
minjaesong
2022-06-28 22:51:09 +09:00
parent 8ee2a2f56d
commit b34502ebd3
16 changed files with 271 additions and 94 deletions

View File

@@ -0,0 +1,105 @@
package net.torvald.terrarum.modulebasegame.ui
import net.torvald.terrarum.CraftingRecipeCodex
import net.torvald.terrarum.ItemCodex
import net.torvald.terrarum.UIItemInventoryCatBar
import net.torvald.terrarum.ceilInt
import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.itemproperties.CraftingCodex
import net.torvald.terrarum.modulebasegame.gameactors.UICrafting
/**
* Created by minjaesong on 2022-06-28.
*/
class UIItemCraftingCandidateGrid(
parentUI: UICrafting, catBar: UIItemInventoryCatBar,
initialX: Int, initialY: Int,
horizontalCells: Int, verticalCells: Int,
drawScrollOnRightside: Boolean = false,
keyDownFun: (GameItem?, Long, Int, Any?) -> Unit, // Item, Amount, Keycode, extra info
touchDownFun: (GameItem?, Long, Int, Any?) -> Unit // Item, Amount, Button, extra info
) : UIItemInventoryItemGrid(
parentUI, catBar,
{ TODO() /* UNUSED and MUST NOT BE USED! */ },
initialX, initialY,
horizontalCells, verticalCells,
drawScrollOnRightside,
drawWallet = false,
hideSidebar = false,
keyDownFun = keyDownFun,
touchDownFun = touchDownFun
) {
val craftingRecipes = ArrayList<CraftingCodex.CraftingRecipe>()
init {
}
internal val recipesSortList = ArrayList<CraftingCodex.CraftingRecipe>() // a dual to the [inventorySortList] which contains the actual recipes instead of crafting recipes
override fun rebuild(filter: Array<String>) {
// test fill craftingRecipes with every possible recipes in the game
craftingRecipes.clear()
CraftingRecipeCodex.props.forEach { (_, recipes) -> craftingRecipes.addAll(recipes) }
recipesSortList.clear() // kinda like the output list
craftingRecipes.forEach {
if ((filter.contains((ItemCodex[it.product]?.inventoryCategory ?: throw IllegalArgumentException("Unknown item: ${it.product}"))) || filter[0] == UIItemInventoryCatBar.CAT_ALL))
recipesSortList.add(it)
}
// map sortList to item list
for (k in items.indices) {
// we have an item
try {
val sortListItem = recipesSortList[k + itemPage * items.size]
items[k].item = ItemCodex[sortListItem.product]
items[k].amount = sortListItem.moq
items[k].itemImage = ItemCodex.getItemImage(sortListItem.product)
items[k].extraInfo = sortListItem
// set quickslot number
/*if (getInventory() is ActorInventory) {
val ainv = getInventory() as ActorInventory
for (qs in 1..UIQuickslotBar.SLOT_COUNT) {
if (sortListItem.product == ainv.getQuickslotItem(qs - 1)?.itm) {
items[k].quickslot = qs % 10 // 10 -> 0, 1..9 -> 1..9
break
}
else
items[k].quickslot = null
}
// set equippedslot number
for (eq in ainv.itemEquipped.indices) {
if (eq < ainv.itemEquipped.size) {
if (ainv.itemEquipped[eq] == items[k].item?.dynamicID) {
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
items[k].equippedSlot = null
items[k].extraInfo = null
}
}
itemPageCount = (recipesSortList.size.toFloat() / items.size.toFloat()).ceilInt()
rebuildList = false
}
}

View File

@@ -6,7 +6,6 @@ import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.GdxColorMap
import net.torvald.terrarum.abs
import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.ui.UIItem
@@ -27,20 +26,21 @@ abstract class UIItemInventoryCellBase(
open var itemImage: TextureRegion?,
open var quickslot: Int? = null,
open var equippedSlot: Int? = null,
val keyDownFun: (GameItem?, Long, Int) -> Unit, // Item, Amount, Keycode
val touchDownFun: (GameItem?, Long, Int) -> Unit // Item, Amount, Button
val keyDownFun: (GameItem?, Long, Int, Any?) -> Unit, // Item, Amount, Keycode, extra info
val touchDownFun: (GameItem?, Long, Int, Any?) -> Unit, // Item, Amount, Button, extra info
open var extraInfo: Any?
) : 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, amount, keycode)
keyDownFun(item, amount, keycode, extraInfo)
super.keyDown(keycode)
return true
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
touchDownFun(item, amount, button)
touchDownFun(item, amount, button, extraInfo)
super.touchDown(screenX, screenY, pointer, button)
return true
}

View File

@@ -31,7 +31,7 @@ import kotlin.math.floor
*
* Created by minjaesong on 2017-10-21.
*/
class UIItemInventoryItemGrid(
open class UIItemInventoryItemGrid(
parentUI: UICanvas,
val catBar: UIItemInventoryCatBar,
var getInventory: () -> FixtureInventory, // when you're going to display List of Craftables, you could implement a Delegator...? Or just build a virtual inventory
@@ -42,8 +42,8 @@ class UIItemInventoryItemGrid(
val drawScrollOnRightside: Boolean = false,
val drawWallet: Boolean = true,
val hideSidebar: Boolean = false,
keyDownFun: (GameItem?, Long, Int) -> Unit, // Item, Amount, Keycode
touchDownFun: (GameItem?, Long, Int) -> Unit // Item, Amount, Button
keyDownFun: (GameItem?, Long, Int, Any?) -> Unit, // Item, Amount, Keycode, extra info
touchDownFun: (GameItem?, Long, Int, Any?) -> Unit // Item, Amount, Button, extra info
) : UIItem(parentUI, initialX, initialY) {
// deal with the moving position
@@ -85,13 +85,13 @@ class UIItemInventoryItemGrid(
rebuild(currentFilter)
}
var itemPageCount = 1 // TODO total size of current category / items.size
private set
protected set
var inventorySortList = ArrayList<InventoryPair>()
private var rebuildList = true
protected var rebuildList = true
private val walletFont = TextureRegionPack("./assets/graphics/fonts/inventory_wallet_numbers.tga", 20, 9)
private var walletText = ""
protected val walletFont = TextureRegionPack("./assets/graphics/fonts/inventory_wallet_numbers.tga", 20, 9)
protected var walletText = ""
companion object {
@@ -101,8 +101,8 @@ class UIItemInventoryItemGrid(
fun getEstimatedW(horizontalCells: Int) = horizontalCells * UIItemInventoryElemSimple.height + (horizontalCells - 1) * listGap
fun getEstimatedH(verticalCells: Int) = verticalCells * UIItemInventoryElemSimple.height + (verticalCells - 1) * listGap
fun createInvCellGenericKeyDownFun(): (GameItem?, Long, Int) -> Unit {
return { item: GameItem?, amount: Long, keycode: Int ->
fun createInvCellGenericKeyDownFun(): (GameItem?, Long, Int, Any?) -> Unit {
return { item: GameItem?, amount: Long, 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) {
@@ -131,8 +131,8 @@ class UIItemInventoryItemGrid(
}
}
fun createInvCellGenericTouchDownFun(listRebuildFun: () -> Unit): (GameItem?, Long, Int) -> Unit {
return { item: GameItem?, amount: Long, button: Int ->
fun createInvCellGenericTouchDownFun(listRebuildFun: () -> Unit): (GameItem?, Long, Int, Any?) -> Unit {
return { item: GameItem?, amount: Long, button: Int, _ ->
if (item != null && Terrarum.ingame != null) {
// equip da shit
val itemEquipSlot = item.equipPosition
@@ -202,7 +202,7 @@ class UIItemInventoryItemGrid(
)
}
private var items: Array<UIItemInventoryCellBase> = itemList
protected var items: Array<UIItemInventoryCellBase> = itemList
var isCompactMode = false // this is INIT code
set(value) {
@@ -324,19 +324,20 @@ class UIItemInventoryItemGrid(
gridModeButtons.forEach { it.render(batch, camera) }
scrollUpButton.render(batch, camera)
scrollDownButton.render(batch, camera)
// draw scroll dots
for (i in 0 until itemPageCount) {
val colour = if (i == itemPage) Color.WHITE else Color(0xffffff7f.toInt())
batch.color = colour
batch.draw(
catBar.catIcons.get(if (i == itemPage) 20 else 21, 0),
scrollUpButton.posX.toFloat(),
getScrollDotYHeight(i).toFloat()
)
}
}
// draw scroll dots
for (i in 0 until itemPageCount) {
val colour = if (i == itemPage) Color.WHITE else Color(0xffffff7f.toInt())
batch.color = colour
batch.draw(
catBar.catIcons.get(if (i == itemPage) 20 else 21, 0),
scrollUpButton.posX.toFloat(),
getScrollDotYHeight(i).toFloat()
)
}
// draw wallet text
if (drawWallet) {
@@ -398,7 +399,7 @@ class UIItemInventoryItemGrid(
}
internal fun rebuild(filter: Array<String>) {
open internal fun rebuild(filter: Array<String>) {
//println("Rebuilt inventory")
//println("rebuild: actual itempage: $itemPage")
@@ -406,7 +407,7 @@ class UIItemInventoryItemGrid(
//val filter = catIconsMeaning[selectedIcon]
currentFilter = filter
inventorySortList = ArrayList<InventoryPair>()
inventorySortList.clear()
// filter items
getInventory().forEach {