mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-12 19:44:05 +09:00
uiitem and template refactoring; new smelter ui wip
This commit is contained in:
@@ -97,7 +97,7 @@ abstract class UICanvas(
|
||||
open var openCloseTime: Second = OPENCLOSE_GENERIC
|
||||
|
||||
|
||||
protected val uiItems = ArrayList<UIItem>()
|
||||
protected val uiItems = ArrayList<UIItemisable>()
|
||||
|
||||
|
||||
val relativeMouseX: Int
|
||||
@@ -189,12 +189,9 @@ abstract class UICanvas(
|
||||
|
||||
abstract override fun dispose()
|
||||
|
||||
fun addUIitem(uiItem: UIItem) {
|
||||
fun addUIitem(uiItem: UIItemisable) {
|
||||
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
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ import net.torvald.terrarum.gamecontroller.TerrarumKeyboardEvent
|
||||
*
|
||||
* Created by minjaesong on 2015-12-31.
|
||||
*/
|
||||
abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: Int): Disposable { // do not replace parentUI to UIHandler!
|
||||
abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: Int): UIItemisable() { // do not replace parentUI to UIHandler!
|
||||
|
||||
// X/Y Position relative to the containing canvas
|
||||
var posX: Int = initialX
|
||||
@@ -145,11 +145,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
||||
open var isActive = true
|
||||
|
||||
|
||||
open fun show() {}
|
||||
open fun hide() {}
|
||||
|
||||
|
||||
open fun update(delta: Float) {
|
||||
override fun update(delta: Float) {
|
||||
if (parentUI.isVisible) {
|
||||
if (isActive) {
|
||||
updateListener.invoke(delta)
|
||||
@@ -177,7 +173,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
||||
/**
|
||||
* In this time, you do write like: ```draw(posX + 4, posY + 32)```, unlike UICanvas, because posX/posY comes from the parent UI.
|
||||
*/
|
||||
open fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) {
|
||||
override fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) {
|
||||
if (parentUI.isVisible) {
|
||||
// if (isActive) {
|
||||
mouseOverCall?.render(frameDelta, batch, camera)
|
||||
@@ -190,7 +186,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
||||
}
|
||||
|
||||
// keyboard controlled
|
||||
open fun keyDown(keycode: Int): Boolean {
|
||||
override fun keyDown(keycode: Int): Boolean {
|
||||
if (parentUI.isVisible && isEnabled) {
|
||||
keyDownListener.invoke(keycode)
|
||||
return true
|
||||
@@ -198,7 +194,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
||||
|
||||
return false
|
||||
}
|
||||
open fun keyUp(keycode: Int): Boolean {
|
||||
override fun keyUp(keycode: Int): Boolean {
|
||||
if (parentUI.isVisible && isEnabled) {
|
||||
keyUpListener.invoke(keycode)
|
||||
return true
|
||||
@@ -206,7 +202,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
||||
|
||||
return false
|
||||
}
|
||||
open fun keyTyped(character: Char): Boolean {
|
||||
override fun keyTyped(character: Char): Boolean {
|
||||
if (parentUI.isVisible && isEnabled) {
|
||||
keyTypedListener.invoke(character)
|
||||
return true
|
||||
@@ -216,7 +212,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
||||
}
|
||||
|
||||
// mouse controlled
|
||||
open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
|
||||
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
|
||||
if (parentUI.isVisible && isEnabled) {
|
||||
touchDraggedListener.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer)
|
||||
return true
|
||||
@@ -224,7 +220,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
||||
|
||||
return false
|
||||
}
|
||||
open fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
||||
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
||||
var actionDone = false
|
||||
|
||||
if (parentUI.isVisible && isEnabled) {
|
||||
@@ -241,7 +237,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
||||
|
||||
return actionDone
|
||||
}
|
||||
open fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
||||
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
||||
clickOnceListenerFired = false
|
||||
|
||||
if (parentUI.isVisible && mouseUp) {
|
||||
@@ -251,7 +247,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
||||
|
||||
return false
|
||||
}
|
||||
open fun scrolled(amountX: Float, amountY: Float): Boolean {
|
||||
override fun scrolled(amountX: Float, amountY: Float): Boolean {
|
||||
if (parentUI.isVisible && mouseUp && isEnabled) {
|
||||
scrolledListener.invoke(amountX, amountY)
|
||||
return true
|
||||
@@ -259,9 +255,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
||||
|
||||
return false
|
||||
}
|
||||
open fun inputStrobed(e: TerrarumKeyboardEvent) {
|
||||
|
||||
}
|
||||
|
||||
abstract override fun dispose()
|
||||
|
||||
|
||||
@@ -35,6 +35,8 @@ class UIItemInventoryElemSimple(
|
||||
highlightEquippedItem: Boolean = true, // for some UIs that only cares about getting equipped slot number but not highlighting
|
||||
colourTheme: InventoryCellColourTheme = defaultInventoryCellTheme,
|
||||
var showItemCount: Boolean = true,
|
||||
var emptyCellIcon: TextureRegion? = null, // icon to draw when the cell is empty
|
||||
var emptyCellIconColour: Color = Color(0xdddddd7f.toInt()),
|
||||
) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot, keyDownFun, touchDownFun, extraInfo, highlightEquippedItem, colourTheme) {
|
||||
|
||||
companion object {
|
||||
@@ -52,6 +54,9 @@ class UIItemInventoryElemSimple(
|
||||
private val imgOffsetX: Float
|
||||
get() = (this.height - itemImageOrDefault.regionWidth).div(2).toFloat() // to snap to the pixel grid
|
||||
|
||||
private val emptyCellIconOffsetX = (this.height - (emptyCellIcon?.regionWidth ?: 0)).div(2).toFloat()
|
||||
private val emptyCellIconOffsetY = (this.height - (emptyCellIcon?.regionHeight ?: 0)).div(2).toFloat()
|
||||
|
||||
override fun update(delta: Float) {
|
||||
|
||||
}
|
||||
@@ -123,6 +128,15 @@ class UIItemInventoryElemSimple(
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
// empty cell image
|
||||
emptyCellIcon?.let {
|
||||
batch.color = emptyCellIconColour
|
||||
batch.draw(it, posX + emptyCellIconOffsetX, posY + emptyCellIconOffsetY)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// see IFs above?
|
||||
|
||||
@@ -1,10 +1,44 @@
|
||||
package net.torvald.terrarum.ui
|
||||
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import com.badlogic.gdx.utils.Disposable
|
||||
import net.torvald.terrarum.gamecontroller.TerrarumKeyboardEvent
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2024-01-10.
|
||||
*/
|
||||
abstract class UITemplate(val parent: UICanvas) {
|
||||
abstract class UITemplate(val parent: UICanvas) : UIItemisable() {
|
||||
|
||||
abstract fun getUIitems(): List<UIItem>
|
||||
|
||||
override fun show() { getUIitems().forEach { it.show() } }
|
||||
override fun hide() { getUIitems().forEach { it.hide() } }
|
||||
override fun inputStrobed(e: TerrarumKeyboardEvent) { getUIitems().forEach { it.inputStrobed(e) } }
|
||||
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean { getUIitems().forEach { it.touchDragged(screenX, screenY, pointer) }; return true }
|
||||
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { getUIitems().forEach { it.touchDown(screenX, screenY, pointer, button) }; return true }
|
||||
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { getUIitems().forEach { it.touchUp(screenX, screenY, pointer, button) }; return true }
|
||||
override fun scrolled(amountX: Float, amountY: Float): Boolean { getUIitems().forEach { it.scrolled(amountX, amountY) }; return true }
|
||||
override fun keyDown(keycode: Int): Boolean { getUIitems().forEach { it.keyDown(keycode) }; return true }
|
||||
override fun keyUp(keycode: Int): Boolean { getUIitems().forEach { it.keyUp(keycode) }; return true }
|
||||
override fun keyTyped(char: Char): Boolean { getUIitems().forEach { it.keyTyped(char) }; return true }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2024-01-29.
|
||||
*/
|
||||
abstract class UIItemisable : Disposable {
|
||||
abstract fun update(delta: Float)
|
||||
abstract fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera)
|
||||
open fun show() {}
|
||||
open fun hide() {}
|
||||
open fun inputStrobed(e: TerrarumKeyboardEvent) {}
|
||||
open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean = false
|
||||
open fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean = false
|
||||
open fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean = false
|
||||
open fun scrolled(amountX: Float, amountY: Float): Boolean = false
|
||||
open fun keyDown(keycode: Int): Boolean = false
|
||||
open fun keyUp(keycode: Int): Boolean = false
|
||||
open fun keyTyped(char: Char): Boolean = false
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package net.torvald.terrarum.ui
|
||||
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import net.torvald.terrarum.App
|
||||
import net.torvald.terrarum.CommonResourcePool
|
||||
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull
|
||||
@@ -34,4 +36,8 @@ class UITemplateCatBar(
|
||||
override fun getUIitems(): List<UIItem> {
|
||||
return listOf(catBar)
|
||||
}
|
||||
|
||||
override fun update(delta: Float) = catBar.update(delta)
|
||||
override fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) = catBar.render(frameDelta, batch, camera)
|
||||
override fun dispose() = catBar.dispose()
|
||||
}
|
||||
Reference in New Issue
Block a user