Files
Terrarum/src/net/torvald/terrarum/UIItemInventoryElemWide.kt
2021-12-03 16:39:46 +09:00

152 lines
5.7 KiB
Kotlin

package net.torvald.terrarum
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.gameitems.GameItem
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.INVEN_DEBUG_MODE
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellBase
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.toItemCountText
import net.torvald.terrarum.ui.Toolkit
import net.torvald.terrarum.ui.UICanvas
import kotlin.math.roundToInt
/***
* Note that the UI will not render if either item or itemImage is null.
*
* Created by minjaesong on 2017-03-16.
*/
class UIItemInventoryElemWide(
parentUI: UICanvas,
initialX: Int,
initialY: Int,
override val width: Int,
override var item: GameItem?,
override var amount: Int,
override var itemImage: TextureRegion?,
override var quickslot: Int? = null,
override var equippedSlot: Int? = null,
val drawBackOnNull: Boolean = true,
keyDownFun: (GameItem?, Int, Int) -> Unit,
touchDownFun: (GameItem?, Int, Int) -> Unit
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun) {
companion object {
val height = 48
val UNIQUE_ITEM_HAS_NO_AMOUNT = -1
internal val durabilityBarThickness = 3
}
override val height = UIItemInventoryElemWide.height
private val imgOffsetY: Float
get() = (this.height - itemImage!!.regionHeight).div(2).toFloat() // to snap to the pixel grid
private val imgOffsetX: Float
get() = (this.height - itemImage!!.regionWidth).div(2).toFloat() // NOTE we're using this.height to get horizontal value; this is absofreakinlutely intentional (otherwise images would draw center of this wide cell which is not something we want)
private val textOffsetX = 50f
private val textOffsetY = 8f
private val durabilityBarOffY = 35
override fun update(delta: Float) {
if (item != null) {
}
}
private val fwsp = 0x3000.toChar()
override fun render(batch: SpriteBatch, camera: Camera) {
blendNormal(batch)
// cell background
if (item != null || drawBackOnNull) {
batch.color = Toolkit.Theme.COL_CELL_FILL
Toolkit.fillArea(batch, posX, posY, width, height)
}
// cell border
batch.color = if (equippedSlot != null) Toolkit.Theme.COL_HIGHLIGHT
else if (mouseUp) Toolkit.Theme.COL_ACTIVE
else Toolkit.Theme.COL_INVENTORY_CELL_BORDER
Toolkit.drawBoxBorder(batch, posX, posY, width, height)
if (item != null && itemImage != null) {
val amountString = amount.toItemCountText()
blendNormal(batch)
// item image
batch.color = Color.WHITE
batch.draw(itemImage, posX + imgOffsetX, posY + imgOffsetY)
// if mouse is over, text lights up
// highlight item name and count (blocks/walls) if the item is equipped
batch.color = item!!.nameColour mul (
if (equippedSlot != null) Toolkit.Theme.COL_HIGHLIGHT
else if (mouseUp) Toolkit.Theme.COL_ACTIVE
else Color.WHITE
)
// draw name of the item
if (INVEN_DEBUG_MODE) {
App.fontGame.draw(batch,
// print static id, dynamic id, and count
"${item!!.originalID}/${item!!.dynamicID}" + (if (amount > 0 && item!!.stackable) "$fwsp($amountString)" else if (amount != 1) "$fwsp!!$amountString!!" else ""),
posX + textOffsetX,
posY + textOffsetY
)
}
else {
App.fontGame.draw(batch,
// print name and amount in parens
item!!.name + (if (amount > 0 && item!!.stackable) "$fwsp($amountString)" else if (amount != 1) "$fwsp!!$amountString!!" else ""),
posX + textOffsetX,
posY + textOffsetY
)
}
// durability metre
val barFullLen = (width - 8) - textOffsetX.toInt()
val barOffset = posX + textOffsetX.toInt()
val percentage = if (item!!.maxDurability < 0.00001f) 0f else item!!.durability / item!!.maxDurability
val durabilityCol = UIItemInventoryCellCommonRes.getHealthMeterColour(percentage, 0f, 1f)
val durabilityBack = durabilityCol mul UIItemInventoryCellCommonRes.meterBackDarkening
if (item!!.maxDurability > 0.0) {
batch.color = durabilityBack
Toolkit.drawStraightLine(batch, barOffset, posY + durabilityBarOffY, barOffset + barFullLen, durabilityBarThickness, false)
batch.color = durabilityCol
Toolkit.drawStraightLine(batch, barOffset, posY + durabilityBarOffY, barOffset + (barFullLen * percentage).roundToInt(), durabilityBarThickness, false)
}
// quickslot marker (TEMPORARY UNTIL WE GET BETTER DESIGN)
batch.color = Color.WHITE
if (quickslot != null) {
val label = quickslot!!.plus(0xE010).toChar()
val labelW = App.fontGame.getWidth("$label")
App.fontGame.draw(batch, "$label", barOffset + barFullLen - labelW.toFloat(), posY + textOffsetY)
}
}
// see IFs above?
batch.color = Color.WHITE
}
override fun dispose() {
}
}