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

@@ -14,6 +14,7 @@ import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.JsonValue;
import com.github.strikerx3.jxinput.XInputDevice;
import net.torvald.gdx.graphics.PixmapIO2;
@@ -752,7 +753,7 @@ public class App implements ApplicationListener {
try {
it.dispose();
}
catch (NullPointerException | IllegalArgumentException e) { }
catch (NullPointerException | IllegalArgumentException | GdxRuntimeException e) { }
});
ModMgr.INSTANCE.disposeMods();

View File

@@ -33,13 +33,13 @@ class EntryPoint : ModuleEntryPoint() {
// load common resources to the AssetsManager
CommonResourcePool.addToLoadingList("$moduleName.items16") {
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items.tga"), 16, 16, flipY = true)
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items.tga"), 16, 16, flipY = false)
}
CommonResourcePool.addToLoadingList("$moduleName.items24") {
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items24.tga"), 24, 24, flipY = true)
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items24.tga"), 24, 24, flipY = false)
}
CommonResourcePool.addToLoadingList("$moduleName.items48") {
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items48.tga"), 48, 48, flipY = true)
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items48.tga"), 48, 48, flipY = false)
}

View File

@@ -78,8 +78,6 @@ internal class UIStorageChest : UICanvas(
override var height = App.scr.height
override var openCloseTime: Second = 0.0f
private val shapeRenderer = ShapeRenderer()
private val negotiator = object : InventoryNegotiator() {
override fun accept(player: FixtureInventory, fixture: FixtureInventory, item: GameItem, amount: Int) {
player.remove(item, amount)
@@ -201,7 +199,7 @@ internal class UIStorageChest : UICanvas(
override fun renderUI(batch: SpriteBatch, camera: Camera) {
// background fill
UIInventoryFull.drawBackground(batch, shapeRenderer)
UIInventoryFull.drawBackground(batch)
// UI items
batch.color = Color.WHITE
@@ -271,6 +269,5 @@ internal class UIStorageChest : UICanvas(
override fun dispose() {
try { shapeRenderer.dispose() } catch (e: IllegalArgumentException) {}
}
}

View File

@@ -42,7 +42,6 @@ class UIInventoryEscMenu(val full: UIInventoryFull) : UICanvas() {
textAreaWidth = gameMenuListWidth,
activeBackCol = Color(0),
highlightBackCol = Color(0),
backgroundCol = Color(0),
inactiveCol = Color.WHITE,
defaultSelection = null
)
@@ -55,7 +54,6 @@ class UIInventoryEscMenu(val full: UIInventoryFull) : UICanvas() {
textAreaWidth = gameMenuListWidth,
activeBackCol = Color(0),
highlightBackCol = Color(0),
backgroundCol = Color(0),
inactiveCol = Color.WHITE,
defaultSelection = null
)
@@ -68,7 +66,6 @@ class UIInventoryEscMenu(val full: UIInventoryFull) : UICanvas() {
textAreaWidth = gameMenuListWidth,
activeBackCol = Color(0),
highlightBackCol = Color(0),
backgroundCol = Color(0),
inactiveCol = Color.WHITE,
defaultSelection = null
)

View File

@@ -34,6 +34,9 @@ class UIInventoryFull(
override var openCloseTime: Second = 0.0f
companion object {
private var shapeRenderer: ShapeRenderer? = null
val CELL_COL = Toolkit.Theme.COL_CELL_FILL
const val INVEN_DEBUG_MODE = false
@@ -62,24 +65,28 @@ class UIInventoryFull(
val controlHelpHeight = App.fontGame.lineHeight
fun drawBackground(batch: SpriteBatch, shapeRenderer: ShapeRenderer) {
fun drawBackground(batch: SpriteBatch) {
batch.end()
gdxSetBlendNormal()
if (shapeRenderer == null) {
shapeRenderer = ShapeRenderer()
App.disposables.add(shapeRenderer)
}
val gradTopStart = (App.scr.height - internalHeight).div(2).toFloat()
val gradBottomEnd = App.scr.height - gradTopStart
shapeRenderer.inUse {
shapeRenderer!!.inUse {
// shaperender starts at bottom-left!
it.rect(0f, gradTopStart, App.scr.wf, gradHeight, gradStartCol, gradStartCol, gradEndCol, gradEndCol)
it.rect(0f, gradBottomEnd, App.scr.wf, -gradHeight, gradStartCol, gradStartCol, gradEndCol, gradEndCol)
shapeRenderer.rect(0f, gradTopStart, App.scr.wf, gradHeight, gradStartCol, gradStartCol, gradEndCol, gradEndCol)
shapeRenderer.rect(0f, gradBottomEnd, App.scr.wf, -gradHeight, gradStartCol, gradStartCol, gradEndCol, gradEndCol)
it.rect(0f, gradTopStart + gradHeight, App.scr.wf, internalHeight - (2 * gradHeight), gradEndCol, gradEndCol, gradEndCol, gradEndCol)
shapeRenderer.rect(0f, gradTopStart + gradHeight, App.scr.wf, internalHeight - (2 * gradHeight), gradEndCol, gradEndCol, gradEndCol, gradEndCol)
shapeRenderer.rect(0f, 0f, App.scr.wf, gradTopStart, gradStartCol, gradStartCol, gradStartCol, gradStartCol)
shapeRenderer.rect(0f, App.scr.hf, App.scr.wf, -(App.scr.hf - gradBottomEnd), gradStartCol, gradStartCol, gradStartCol, gradStartCol)
it.rect(0f, 0f, App.scr.wf, gradTopStart, gradStartCol, gradStartCol, gradStartCol, gradStartCol)
it.rect(0f, App.scr.hf, App.scr.wf, -(App.scr.hf - gradBottomEnd), gradStartCol, gradStartCol, gradStartCol, gradStartCol)
}
batch.begin()
@@ -235,7 +242,6 @@ class UIInventoryFull(
//private val gradStartCol = Color(0x404040_60)
//private val gradEndCol = Color(0x000000_70)
//private val gradHeight = 48f
private val shapeRenderer = ShapeRenderer()
internal var xEnd = (width + internalWidth).div(2).toFloat()
private set
@@ -244,7 +250,7 @@ class UIInventoryFull(
override fun renderUI(batch: SpriteBatch, camera: Camera) {
drawBackground(batch, shapeRenderer)
drawBackground(batch)
// UI items
catBar.render(batch, camera)
@@ -274,7 +280,6 @@ class UIInventoryFull(
override fun dispose() {
catBar.dispose()
transitionPanel.dispose()
}

View File

@@ -5,7 +5,9 @@ import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.random.HQRNG
import net.torvald.terrarum.App
import net.torvald.terrarum.App.printdbg
import net.torvald.terrarum.ModMgr
import net.torvald.terrarum.Second
import net.torvald.terrarum.langpack.Lang
@@ -50,23 +52,35 @@ class UINewWorld(val remoCon: UIRemoCon) : UICanvas() {
{ Lang["CONTEXT_DESCRIPTION_HUGE"] }
))
private val rng = HQRNG()
private val nameInput = UIItemTextLineInput(this,
drawX + width - inputWidth, drawY + sizeSelY + 80, inputWidth,
{ RandomWordsName(4) }, InputLenCap(VirtualDisk.NAME_LENGTH, InputLenCap.CharLenUnit.UTF8_BYTES))
private val seedInput = UIItemTextLineInput(this,
drawX + width - inputWidth, drawY + sizeSelY + 120, inputWidth,
{ RandomWordsName(4) }, InputLenCap(256, InputLenCap.CharLenUnit.CODEPOINTS))
{ rng.nextLong().toString() }, InputLenCap(256, InputLenCap.CharLenUnit.CODEPOINTS))
private val goButtonWidth = 180
private val backButton = UIItemTextButton(this, "MENU_LABEL_BACK", drawX + (width/2 - goButtonWidth) / 2, drawY + height - 24, goButtonWidth, true, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true)
private val goButton = UIItemTextButton(this, "MENU_LABEL_CONFIRM_BUTTON", drawX + width/2 + (width/2 - goButtonWidth) / 2, drawY + height - 24, goButtonWidth, true, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true)
init {
tex.forEach { it.flip(false, true) }
sizeSelector.selectionChangeListener = {}
goButton.touchDownListener = { _, _, _, _ ->
printdbg(this, "generate!")
}
backButton.touchDownListener = { _, _, _, _ ->
printdbg(this, "back!")
}
addUIitem(sizeSelector)
addUIitem(nameInput)
addUIitem(seedInput)
addUIitem(goButton)
addUIitem(backButton)
}

View File

@@ -245,7 +245,6 @@ open class UIRemoCon(val parent: TitleScreen, treeRepresentation: QNDTreeNode<St
readFromLang = true,
activeBackCol = Color(0),//Color(1f,0f,.75f,1f),
highlightBackCol = Color(0),
backgroundCol = Color(0),
inactiveCol = Color.WHITE,
defaultSelection = null,
itemHitboxSize = lineHeight - 2,

View File

@@ -39,7 +39,6 @@ class UITitleLanguage(val remoCon: UIRemoCon) : UICanvas() {
readFromLang = false,
activeBackCol = Color(0),
highlightBackCol = Color(0),
backgroundCol = Color(0),
inactiveCol = Color.WHITE,
defaultSelection = null
)
@@ -52,7 +51,6 @@ class UITitleLanguage(val remoCon: UIRemoCon) : UICanvas() {
readFromLang = false,
activeBackCol = Color(0),
highlightBackCol = Color(0),
backgroundCol = Color(0),
inactiveCol = Color.WHITE,
defaultSelection = null
)

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) {