some more new world ui thingies/fixed a bug where drawing an inventory background would cause a segfault

This commit is contained in:
minjaesong
2021-10-26 14:12:16 +09:00
parent d8aed8a34c
commit 08518fff06
11 changed files with 105 additions and 90 deletions

View File

@@ -1,10 +1,8 @@
package net.torvald.terrarum.ui
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.utils.Disposable
import net.torvald.terrarum.App
import net.torvald.terrarum.Terrarum
@@ -209,7 +207,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
var actionDone = false
if (parentUI.isVisible) {
if (touchDownListener != null) {
if (touchDownListener != null && mouseUp) {
touchDownListener!!.invoke(relativeMouseX, relativeMouseY, pointer, button)
actionDone = true
}
@@ -225,7 +223,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
open fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
clickOnceListenerFired = false
if (parentUI.isVisible && touchUpListener != null) {
if (parentUI.isVisible && touchUpListener != null && mouseUp) {
touchUpListener!!.invoke(relativeMouseX, relativeMouseY, pointer, button)
return true
}
@@ -233,7 +231,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
return false
}
open fun scrolled(amountX: Float, amountY: Float): Boolean {
if (parentUI.isVisible && scrolledListener != null) {
if (parentUI.isVisible && scrolledListener != null && mouseUp) {
scrolledListener!!.invoke(amountX, amountY)
return true
}

View File

@@ -36,9 +36,8 @@ open class UIItemTextButton(
val highlightBackBlendMode: String = BlendMode.NORMAL,
/** Colour on normal status */
val inactiveCol: Color = Toolkit.Theme.COL_LIST_DEFAULT,
val backgroundCol: Color = UIItemTextButtonList.DEFAULT_BACKGROUNDCOL,
val backgroundBlendMode: String = BlendMode.NORMAL,
val hasBorder: Boolean = false,
val paddingLeft: Int = 0,
val paddingRight: Int = 0,
@@ -51,7 +50,7 @@ open class UIItemTextButton(
companion object {
val font = App.fontGame
val height = font.lineHeight.toInt()
val height = 24
enum class Alignment {
CENTRE, LEFT, RIGHT
@@ -75,6 +74,12 @@ open class UIItemTextButton(
override fun render(batch: SpriteBatch, camera: Camera) {
val textW = font.getWidth(label)
val fontX = when (alignment) {
Alignment.CENTRE -> posX + width.minus(textW).div(2) + (paddingLeft - paddingRight).div(2)
Alignment.LEFT -> posX + paddingLeft
Alignment.RIGHT -> width - paddingRight - textW
}
val fontY = posY + (hitboxSize - font.lineHeight.toInt()) / 2
// draw background
@@ -97,21 +102,29 @@ open class UIItemTextButton(
blendNormal(batch)
if (hasBorder) {
batch.color = Toolkit.Theme.COL_CELL_FILL
Toolkit.fillArea(batch, posX, posY, width, height)
}
batch.color = if (highlighted) highlightCol
else if (mouseUp) activeCol
else inactiveCol
font.draw(batch,
label,
// "$label/H:${highlighted.toInt()}, M:${mouseUp.toInt()}",
when (alignment) {
Alignment.CENTRE -> posX.toFloat() + width.minus(textW).div(2) + (paddingLeft - paddingRight).div(2)
Alignment.LEFT -> posX.toFloat() + paddingLeft
Alignment.RIGHT -> width - paddingRight - textW.toFloat()
},
posY.toFloat() + (hitboxSize - UIItemTextButton.height) / 2f
)
// draw border
if (hasBorder) {
val c = batch.color.cpy()
if (batch.color == inactiveCol) {
batch.color = Toolkit.Theme.COL_INACTIVE
}
Toolkit.drawBoxBorder(batch, posX - 1, posY - 1, width + 2, height + 2)
batch.color = c
}
// draw text
font.draw(batch, label, fontX, fontY)
}
override fun dispose() {

View File

@@ -1,11 +1,12 @@
package net.torvald.terrarum.ui
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.*
import net.torvald.terrarum.App.printdbg
import net.torvald.terrarum.BlendMode
import net.torvald.terrarum.Second
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.toInt
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
@@ -43,11 +44,6 @@ class UIItemTextButtonList(
val highlightBackBlendMode: String = BlendMode.NORMAL,
/** Colour on normal status */
val inactiveCol: Color = Toolkit.Theme.COL_LIST_DEFAULT,
val backgroundCol: Color = UIItemTextButtonList.DEFAULT_BACKGROUNDCOL,
val backgroundBlendMode: String = BlendMode.NORMAL,
val kinematic: Boolean = false,
val leftPadding: Int = 0,
val rightPadding: Int = 0,
@@ -83,7 +79,7 @@ class UIItemTextButtonList(
val lh = itemHitboxSize
val vertOff = lineHeight * i
if (!kinematic) {
// if (!kinematic) {
UIItemTextButton(
parentUI, s,
initialX = posX,
@@ -97,37 +93,34 @@ class UIItemTextButtonList(
highlightBackCol = highlightBackCol,
highlightBackBlendMode = highlightBackBlendMode,
inactiveCol = inactiveCol,
backgroundCol = backgroundCol,
backgroundBlendMode = backgroundBlendMode,
paddingLeft = pregap,
paddingRight = postgap,
alignment = alignment,
hitboxSize = itemHitboxSize,
tags = tagsCollection[i]
)
}
else {
UIItemTextButton(
parentUI, s,
initialX = posX,
initialY = posY + vertOff,
width = width,
readFromLang = readFromLang,
activeCol = activeCol,
activeBackCol = activeBackCol,
activeBackBlendMode = activeBackBlendMode,
highlightCol = highlightCol,
highlightBackCol = activeBackCol, // we are using custom highlighter
highlightBackBlendMode = activeBackBlendMode, // we are using custom highlighter
backgroundCol = Color(0),
inactiveCol = inactiveCol,
paddingLeft = pregap,
paddingRight = postgap,
alignment = alignment,
hitboxSize = itemHitboxSize,
tags = tagsCollection[i]
)
}
// }
// else {
// UIItemTextButton(
// parentUI, s,
// initialX = posX,
// initialY = posY + vertOff,
// width = width,
// readFromLang = readFromLang,
// activeCol = activeCol,
// activeBackCol = activeBackCol,
// activeBackBlendMode = activeBackBlendMode,
// highlightCol = highlightCol,
// highlightBackCol = activeBackCol, // we are using custom highlighter
// highlightBackBlendMode = activeBackBlendMode, // we are using custom highlighter
// inactiveCol = inactiveCol,
// paddingLeft = pregap,
// paddingRight = postgap,
// alignment = alignment,
// hitboxSize = itemHitboxSize,
// tags = tagsCollection[i]
// )
// }
}
@@ -203,16 +196,16 @@ class UIItemTextButtonList(
if (!clickLatched && btn.mousePushed && index != selectedIndex) {
val oldIndex = selectedIndex
if (kinematic) {
selectedIndex = index
highlighterYStart = buttons[selectedIndex!!].posY.toFloat()
highlighterMoving = true
highlighterYEnd = buttons[selectedIndex!!].posY.toFloat()
}
else {
// if (kinematic) {
// selectedIndex = index
// highlighterYStart = buttons[selectedIndex!!].posY.toFloat()
// highlighterMoving = true
// highlighterYEnd = buttons[selectedIndex!!].posY.toFloat()
// }
// else {
selectedIndex = index
highlightY = buttons[selectedIndex!!].posY.toFloat()
}
// }
selectionChangeListener?.invoke(oldIndex, index)
}
@@ -229,7 +222,7 @@ class UIItemTextButtonList(
override fun render(batch: SpriteBatch, camera: Camera) {
if (kinematic) {
/*if (kinematic) {
batch.color = backgroundCol
BlendMode.resolve(backgroundBlendMode, batch)
Toolkit.fillArea(batch, posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat())
@@ -239,7 +232,7 @@ class UIItemTextButtonList(
if (highlightY != null) {
Toolkit.fillArea(batch, posX.toFloat(), highlightY!!.toFloat(), width.toFloat(), itemHitboxSize.toFloat())
}
}
}*/
buttons.forEach { it.render(batch, camera) }
@@ -253,7 +246,7 @@ class UIItemTextButtonList(
}
}
batch.color = backgroundCol
// batch.color = backgroundCol
}
fun select(index: Int) {