generalised catbar

This commit is contained in:
minjaesong
2024-01-10 16:38:51 +09:00
parent 724a92bc18
commit ed70b16384
16 changed files with 213 additions and 116 deletions

View File

@@ -192,6 +192,9 @@ abstract class UICanvas(
fun addUIitem(uiItem: UIItem) {
if (!uiItems.contains(uiItem)) uiItems.add(uiItem)
}
fun addUIitem(template: UITemplate) {
template.getUIitems().forEach { addUIitem(it) }
}
fun mouseInScreen(x: Int, y: Int) = x in 0 until App.scr.windowW && y in 0 until App.scr.windowH

View File

@@ -0,0 +1,291 @@
package net.torvald.terrarum.ui
import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.*
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import kotlin.math.roundToInt
/**
* Created by minjaesong on 2017-10-20.
*/
class UIItemCatBar(
parentUI: UICanvas,
initialX: Int,
initialY: Int,
uiInternalWidth: Int,
override val width: Int,
val showSideButtons: Boolean = false,
val catIcons: TextureRegionPack = CommonResourcePool.getAsTextureRegionPack("inventory_category"),
private val catArrangement: IntArray, // icon order
internal val catIconsMeaning: List<Array<String>>, // sortedBy: catArrangement
internal val catIconsLabels: List<() -> String>,
val panelTransitionReqFun: (Int) -> Unit = {} // for side buttons; for the selection change, override selectionChangeListener
) : UIItem(parentUI, initialX, initialY) {
companion object {
const val CAT_ALL = "__all__"
}
private val inventoryUI = parentUI
override val height = catIcons.tileH + 5
private val mainButtons: Array<UIItemImageButton>
private val buttonGapSize = (width.toFloat() - (catArrangement.size * catIcons.tileW)) / (catArrangement.size)
/** raw order */
var selectedIndex = 0 // default to ALL
private set
/** re-arranged order */
// val selectedIcon: Int
// get() = catArrangement[selectedIndex]
private val sideButtons: Array<UIItemImageButton>
// set up all the buttons
init {
// place sub UIs: Image Buttons
mainButtons = Array(catArrangement.size) { index ->
val iconPosX = ((buttonGapSize / 2) + index * (catIcons.tileW + buttonGapSize)).roundToInt()
val iconPosY = 0
val iconIndex = catArrangement[index]
val iconIndexX = iconIndex % 1000
val iconIndexY = iconIndex / 1000
UIItemImageButton(
inventoryUI,
catIcons.get(iconIndexX, iconIndexY),
activeBackCol = Color(0),
backgroundCol = Color(0),
highlightBackCol = Color(0),
activeBackBlendMode = BlendMode.NORMAL,
initialX = posX + iconPosX,
initialY = posY + iconPosY,
highlightable = true
)
}
// side buttons
// NOTE: < > arrows must "highlightable = false"; "true" otherwise
// determine gaps: hacky way exploiting that we already know the catbar is always at the c of the ui
val relativeStartX = posX - (uiInternalWidth - width) / 2
val sideButtonsGap = (((uiInternalWidth - width) / 2) - 2f * catIcons.tileW) / 3f
val iconIndex = arrayOf(
catIcons.get(9,1),
catIcons.get(16,0),
catIcons.get(17,0),
catIcons.get(13,0)
)
//println("[UIItemInventoryCatBar] relativeStartX: $relativeStartX")
//println("[UIItemInventoryCatBar] posX: $posX")
sideButtons = Array(iconIndex.size) { index ->
val iconPosX = if (index < 2)
(relativeStartX + sideButtonsGap + (sideButtonsGap + catIcons.tileW) * index).roundToInt()
else
(relativeStartX + width + 2 * sideButtonsGap + (sideButtonsGap + catIcons.tileW) * index).roundToInt()
val iconPosY = 0
UIItemImageButton(
inventoryUI,
iconIndex[index],
activeBackCol = Color(0),
backgroundCol = Color(0),
highlightBackCol = Color(0),
activeBackBlendMode = BlendMode.NORMAL,
initialX = iconPosX,
initialY = posY + iconPosY,
inactiveCol = if (index == 0 || index == 3) Color.WHITE else Color(0xffffff7f.toInt()),
activeCol = if (index == 0 || index == 3) Toolkit.Theme.COL_MOUSE_UP else Color(0xffffff7f.toInt()),
highlightable = (index == 0 || index == 3)
)
}
}
private val underlineIndTex: Texture
private val underlineColour = Color(0xeaeaea_40.toInt())
private val underlineHighlightColour = mainButtons[0].highlightCol
private var highlighterXPos = mainButtons[selectedIndex].posX
private var highlighterXStart = highlighterXPos
private var highlighterXEnd = highlighterXPos
private val highlighterYPos = catIcons.tileH + 4
private var highlighterMoving = false
private val highlighterMoveDuration: Second = 0.15f
private var highlighterMoveTimer: Second = 0f
private var transitionFired = false
/**
* 0: map, 1: inventory caticons, 2: menu
*/
var selectedPanel = 1; private set
fun setSelectedPanel(n: Int) {
if (n !in 0..2) throw IllegalArgumentException("$n")
selectedPanel = n
sideButtons[0].highlighted = (n == 0)
mainButtons[selectedIndex].highlighted = (n == 1)
sideButtons[3].highlighted = (n == 2)
}
// set up underlined indicator
init {
// procedurally generate texture
val pixmap = Pixmap(catIcons.tileW + buttonGapSize.floorToInt(), 1, Pixmap.Format.RGBA8888)
for (x in 0 until pixmap.width.plus(1).ushr(1)) { // eqv. of ceiling the half-int
val col = /*if (x == 0)*/ /*0xffffff_80.toInt()*/
/*else if (x == 1)*/ /*0xffffff_c0.toInt()*/
/*else */ 0xffffff_ff.toInt()
pixmap.drawPixel(x, 0, col)
pixmap.drawPixel(pixmap.width - (x + 1), 0, col)
}
underlineIndTex = Texture(pixmap)
underlineIndTex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
pixmap.dispose()
mainButtons[selectedIndex].highlighted = true
}
/** (oldIndex: Int?, newIndex: Int) -> Unit
* Indices are raw index. That is, not re-arranged. */
var selectionChangeListener: ((Int?, Int) -> Unit)? = null
override fun update(delta: Float) {
super.update(delta)
if (highlighterMoving) {
highlighterMoveTimer += delta
highlighterXPos = Movement.moveQuick(
highlighterXStart.toFloat(),
highlighterXEnd.toFloat(),
highlighterMoveTimer,
highlighterMoveDuration
).roundToInt()
if (highlighterMoveTimer > highlighterMoveDuration) {
highlighterMoveTimer = 0f
highlighterXStart = highlighterXEnd
highlighterXPos = highlighterXEnd
highlighterMoving = false
}
}
mainButtons.forEachIndexed { index, btn ->
btn.update(delta)
if (btn.mousePushed && selectedPanel != 1) {
transitionFired = true
selectedPanel = 1
}
// move selection highlighter
if (btn.mousePushed && index != selectedIndex) {
// normal stuffs
val oldIndex = selectedIndex
highlighterXStart = mainButtons[selectedIndex].posX // using old selectedIndex
selectedIndex = index
highlighterMoving = true
highlighterXEnd = mainButtons[selectedIndex].posX // using new selectedIndex
selectionChangeListener?.invoke(oldIndex, index)
}
if (selectedPanel == 1) {
btn.highlighted = (index == selectedIndex) // forcibly highlight if this.highlighted != null
sideButtons[0].highlighted = false
sideButtons[3].highlighted = false
}
}
if (showSideButtons) {
sideButtons[0].update(delta)
sideButtons[3].update(delta)
// more transition stuffs
if (sideButtons[0].mousePushed) {
if (selectedPanel != 0) transitionFired = true
mainButtons.forEach { it.highlighted = false }
selectedPanel = 0
sideButtons[0].highlighted = true
sideButtons[3].highlighted = false
}
else if (sideButtons[3].mousePushed) {
if (selectedPanel != 2) transitionFired = true
mainButtons.forEach { it.highlighted = false }
selectedPanel = 2
transitionFired = true
sideButtons[0].highlighted = false
sideButtons[3].highlighted = true
}
if (transitionFired) {
transitionFired = false
panelTransitionReqFun(selectedPanel)
}
}
}
override fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) {
super.render(frameDelta, batch, camera)
// button
// colour determined by UI items themselves
mainButtons.forEach { it.render(frameDelta, batch, camera) }
if (showSideButtons) sideButtons.forEach { it.render(frameDelta, batch, camera) }
blendNormalStraightAlpha(batch)
// underline
batch.color = underlineColour
Toolkit.drawStraightLine(batch, posX, posY + height - 1, posX + width, 1, false)
if (selectedPanel == 1) {
// indicator
batch.color = underlineHighlightColour
batch.draw(underlineIndTex, (highlighterXPos - buttonGapSize / 2), posY + highlighterYPos.toFloat())
// label
batch.color = Color.WHITE
catIconsLabels[selectedIndex]().let {
App.fontGame.draw(batch, it, posX + ((width - App.fontGame.getWidth(it)) / 2), posY + highlighterYPos + 4)
}
}
}
override fun dispose() {
underlineIndTex.dispose()
//catIcons.dispose() // disposed of by the AppLoader
//mainButtons.forEach { it.dispose() } // disposed of by the AppLoader
//sideButtons.forEach { it.dispose() } // disposed of by the AppLoader
}
}

View File

@@ -0,0 +1,130 @@
package net.torvald.terrarum.ui
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.App
import net.torvald.terrarum.blendNormalStraightAlpha
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.UIItemInventoryCellCommonRes
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.defaultInventoryCellTheme
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.toItemCountText
import net.torvald.terrarum.mul
import kotlin.math.roundToInt
/**
* Created by minjaesong on 2017-10-20.
*/
class UIItemInventoryElemSimple(
parentUI: UICanvas,
initialX: Int,
initialY: Int,
override var item: GameItem?,
override var amount: Long,
override var itemImage: TextureRegion?,
override var quickslot: Int? = null,
override var equippedSlot: Int? = null, // remnants of wide cell displaying slot number and highlighting; in this style of cell this field only determines highlightedness at render
val drawBackOnNull: Boolean = true,
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
extraInfo: Any? = null,
highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting
colourTheme: InventoryCellColourTheme = defaultInventoryCellTheme
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem, colourTheme) {
companion object {
val height = UIItemInventoryElemWide.height
}
override val width = Companion.height
override val height = Companion.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() // to snap to the pixel grid
override fun update(delta: Float) {
}
private var highlightToMainCol = false
private var highlightToSubCol = false
override fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) {
blendNormalStraightAlpha(batch)
highlightToMainCol = customHighlightRuleMain?.invoke(this) ?: (equippedSlot != null && highlightEquippedItem) || forceHighlighted
highlightToSubCol = customHighlightRule2?.invoke(this) ?: false
// cell background
if (item != null || drawBackOnNull) {
batch.color = colourTheme.cellBackgroundCol
Toolkit.fillArea(batch, posX, posY, width, height)
}
// cell border
batch.color = if (highlightToMainCol) colourTheme.cellHighlightMainCol
else if (highlightToSubCol) colourTheme.cellHighlightSubCol
else if (mouseUp && item != null) colourTheme.cellHighlightMouseUpCol
else colourTheme.cellHighlightNormalCol
Toolkit.drawBoxBorder(batch, posX, posY, width, height)
// quickslot and equipped slot indicator is not needed as it's intended for blocks and walls
// and you can clearly see the quickslot UI anyway
if (item != null && itemImage != null) {
// item image
batch.color = Color.WHITE
batch.draw(itemImage, posX + imgOffsetX, posY + imgOffsetY)
// if item has durability, draw that and don't draw count; durability and itemCount cannot coexist
if (item!!.maxDurability > 0.0) {
// draw durability metre
val barFullLen = width
val barOffset = posX
val thickness = UIItemInventoryElemWide.durabilityBarThickness
val percentage = item!!.durability / item!!.maxDurability
val durabilityCol = UIItemInventoryCellCommonRes.getHealthMeterColour(percentage, 0f, 1f)
val durabilityBack = durabilityCol mul UIItemInventoryCellCommonRes.meterBackDarkening
batch.color = durabilityBack
Toolkit.drawStraightLine(batch, barOffset, posY + height - thickness, barOffset + barFullLen, thickness, false)
batch.color = durabilityCol
Toolkit.drawStraightLine(batch, barOffset, posY + height - thickness, barOffset + (barFullLen * percentage).roundToInt(), thickness, false)
}
// draw item count when applicable
else if (item!!.stackable) {
val amountString = amount.toItemCountText()
// if mouse is over, text lights up
// highlight item count (blocks/walls) if the item is equipped
batch.color = item!!.nameColour mul (
if (highlightToMainCol) colourTheme.textHighlightMainCol
else if (highlightToSubCol) colourTheme.textHighlightSubCol
else if (mouseUp && item != null) colourTheme.textHighlightMouseUpCol
else colourTheme.textHighlightNormalCol)
App.fontSmallNumbers.draw(batch,
amountString,
posX + (width - App.fontSmallNumbers.getWidth(amountString)).toFloat(),
posY + (height - App.fontSmallNumbers.H).toFloat()
)
}
}
// see IFs above?
batch.color = Color.WHITE
}
override fun dispose() {
}
}

View File

@@ -0,0 +1,160 @@
package net.torvald.terrarum.ui
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.App
import net.torvald.terrarum.blendNormalStraightAlpha
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.UIItemInventoryCellCommonRes
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.toItemCountText
import net.torvald.terrarum.mul
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: Long,
override var itemImage: TextureRegion?,
override var quickslot: Int? = null,
override var equippedSlot: Int? = null,
val drawBackOnNull: Boolean = true,
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
extraInfo: Any? = null,
highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting
colourTheme: InventoryCellColourTheme = UIItemInventoryCellCommonRes.defaultInventoryCellTheme
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem, colourTheme) {
companion object {
val height = 48
val UNIQUE_ITEM_HAS_NO_AMOUNT = -1L
val durabilityBarThickness = 3
}
override val height = Companion.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 var highlightToMainCol = false
private var highlightToSubCol = false
var cellHighlightMainCol = Toolkit.Theme.COL_SELECTED
var cellHighlightSubCol = Toolkit.Theme.COL_LIST_DEFAULT
var cellHighlightMouseUpCol = Toolkit.Theme.COL_MOUSE_UP
var cellHighlightNormalCol = Toolkit.Theme.COL_INVENTORY_CELL_BORDER
var textHighlightMainCol = Toolkit.Theme.COL_SELECTED
var textHighlightSubCol = Color.WHITE
var textHighlightMouseUpCol = Toolkit.Theme.COL_MOUSE_UP
var textHighlightNormalCol = Color.WHITE
override fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) {
blendNormalStraightAlpha(batch)
highlightToMainCol = customHighlightRuleMain?.invoke(this) ?: (equippedSlot != null && highlightEquippedItem) || forceHighlighted
highlightToSubCol = customHighlightRule2?.invoke(this) ?: false
// cell background
if (item != null || drawBackOnNull) {
batch.color = colourTheme.cellBackgroundCol
Toolkit.fillArea(batch, posX, posY, width, height)
}
// cell border
batch.color = if (highlightToMainCol) colourTheme.cellHighlightMainCol
else if (highlightToSubCol) colourTheme.cellHighlightSubCol
else if (mouseUp && item != null) colourTheme.cellHighlightMouseUpCol
else colourTheme.cellHighlightNormalCol
Toolkit.drawBoxBorder(batch, posX, posY, width, height)
if (item != null && itemImage != null) {
val amountString = amount.toItemCountText()
blendNormalStraightAlpha(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 (highlightToMainCol) colourTheme.textHighlightMainCol
else if (highlightToSubCol) colourTheme.textHighlightSubCol
else if (mouseUp && item != null) colourTheme.textHighlightMouseUpCol
else colourTheme.textHighlightNormalCol)
// draw name of the item
App.fontGame.draw(batch,
// print name and amount in parens
item!!.name + (if (amount > 0 && item!!.stackable) "\u3000($amountString)" else if (amount != 1L) "\u3000!!$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() {
}
}

View File

@@ -0,0 +1,10 @@
package net.torvald.terrarum.ui
/**
* Created by minjaesong on 2024-01-10.
*/
abstract class UITemplate(val parent: UICanvas) {
abstract fun getUIitems(): List<UIItem>
}

View File

@@ -0,0 +1,36 @@
package net.torvald.terrarum.ui
import net.torvald.terrarum.App
import net.torvald.terrarum.CommonResourcePool
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Basically a UIItemInventoryCatBar placed on a set position for your convenience
*
* Created by minjaesong on 2024-01-10.
*/
class UITemplateCatBar(
parent: UICanvas,
catIcons: TextureRegionPack = CommonResourcePool.getAsTextureRegionPack("inventory_category"),
catArrangement: IntArray, // icon order
catIconsMeaning: List<Array<String>>, // sortedBy: catArrangement
catIconsLabels: List<() -> String>,
) : UITemplate(parent) {
val catBar = UIItemCatBar(
parent,
(parent.width - UIInventoryFull.catBarWidth) / 2,
42 - UIInventoryFull.YPOS_CORRECTION + (App.scr.height - UIInventoryFull.internalHeight) / 2,
UIInventoryFull.internalWidth,
UIInventoryFull.catBarWidth,
true,
catIcons, catArrangement, catIconsMeaning, catIconsLabels
)
override fun getUIitems(): List<UIItem> {
return listOf(catBar)
}
}