UINSMenu is working again

This commit is contained in:
minjaesong
2023-10-14 20:56:03 +09:00
parent 228c9b8127
commit d5074e30eb
8 changed files with 107 additions and 72 deletions

View File

@@ -92,6 +92,8 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
/** If mouse is hovering over it and mouse is down */
val mousePushed: Boolean
get() = mouseUp && Terrarum.mouseDown
val mouseJustPushed: Boolean
get() = mouseUp && Terrarum.mouseJustDown
val mouseDown: Boolean
get() = Terrarum.mouseDown

View File

@@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.BlendMode
import net.torvald.terrarum.Second
import net.torvald.terrarum.Terrarum
/**
@@ -57,8 +58,18 @@ class UIItemList<Item: UIItem>(
/** (oldIndex: Int?, newIndex: Int) -> Unit */
var selectionChangeListener: ((Int?, Int) -> Unit)? = null
private var clickLatched = false
override fun show() {
clickLatched = true
}
override fun update(delta: Float) {
val posXDelta = posX - oldPosX
itemList.forEach { it.posX += posXDelta }
if (highlighterMoving) {
highlighterMoveTimer += delta
@@ -83,7 +94,7 @@ class UIItemList<Item: UIItem>(
item.update(delta)
if (item.mousePushed && index != selectedIndex) {
if (!clickLatched && item.mousePushed) {
val oldIndex = selectedIndex
if (kinematic) {
@@ -102,6 +113,12 @@ class UIItemList<Item: UIItem>(
//item.highlighted = (index == selectedIndex) // forcibly highlight if this.highlighted != null
}
if (!Terrarum.mouseDown) {
clickLatched = false
}
oldPosX = posX
}
override fun render(batch: SpriteBatch, camera: OrthographicCamera) {

View File

@@ -197,7 +197,8 @@ class UIItemTextButtonList(
btn.update(delta)
if (!clickLatched && btn.mousePushed && index != selectedIndex) {
if (!clickLatched && btn.mousePushed) {
clickLatched = true
val oldIndex = selectedIndex
// if (kinematic) {
@@ -214,10 +215,9 @@ class UIItemTextButtonList(
selectionChangeListener?.invoke(oldIndex, index)
}
btn.highlighted = (index == selectedIndex) // forcibly highlight if this.highlighted != null
}
if (!Terrarum.mouseDown) {
if (clickLatched && !Terrarum.mouseDown) {
clickLatched = false
}

View File

@@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.*
import kotlin.math.max
import kotlin.math.roundToInt
/**
* Nextstep-themed menu bar with mandatory title line
@@ -102,14 +103,14 @@ class UINSMenu(
val list = UIItemTextButtonList(
this,
UIItemTextButtonList.DEFAULT_LINE_HEIGHT,
LINE_HEIGHT,
stringsFromTree,
width, LINE_HEIGHT,
uiWidth, listHeight,
textAreaWidth = listWidth,
alignment = UIItemTextButton.Companion.Alignment.LEFT,
inactiveCol = Color(.94f, .94f, .94f, 1f),
itemHitboxSize = LINE_HEIGHT,
itemHitboxSize = LINE_HEIGHT - 2,
backgroundCol = UIItemTextButtonList.DEFAULT_BACKGROUNDCOL
)
@@ -129,13 +130,18 @@ class UINSMenu(
}
// 2. push the new menu
if (tree.children[new].children.isNotEmpty()) {
addSubMenu(tree.children[new])
}
if (old != new) {
if (tree.children[new].children.isNotEmpty()) {
addSubMenu(tree.children[new])
}
// invoke whatever command there is
//printdbg(this, "Selected: ${tree.children[new].data?.second}")
tree.children[new].data?.second?.invoke(invocationArgument)
// invoke whatever command there is
//printdbg(this, "Selected: ${tree.children[new].data?.second}")
tree.children[new].data?.second?.invoke(invocationArgument)
}
else {
list.selectedIndex = null // deselect if old == new
}
}
// END List selection change listener
@@ -216,8 +222,10 @@ class UINSMenu(
if (mouseInScreen(screenX, screenY)) {
if (dragForReal) {
handler.setPosition(screenX - dragOriginX, screenY - dragOriginY)
//println("drag $screenX, $screenY")
handler.setPosition(
(screenX / App.scr.magn - dragOriginX).roundToInt(),
(screenY / App.scr.magn - dragOriginY).roundToInt()
)
}
}