click on palette item to select it

This commit is contained in:
minjaesong
2021-11-18 11:39:25 +09:00
parent 3f88e7965a
commit 63d7009dcf
5 changed files with 59 additions and 33 deletions

View File

@@ -78,15 +78,15 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
protected var oldPosY: Int = initialY
/** Position of mouse relative to this item */
protected val relativeMouseX: Int
protected val itemRelativeMouseX: Int
get() = (Terrarum.mouseScreenX - (parentUI.posX) - this.posX)
/** Position of mouse relative to this item */
protected val relativeMouseY: Int
protected val itemRelativeMouseY: Int
get() = (Terrarum.mouseScreenY - (parentUI.posY) - this.posY)
/** If mouse is hovering over it */
open val mouseUp: Boolean
get() = relativeMouseX in 0 until width && relativeMouseY in 0 until height
get() = itemRelativeMouseX in 0 until width && itemRelativeMouseY in 0 until height
/** If mouse is hovering over it and mouse is down */
open val mousePushed: Boolean
get() = mouseUp && Terrarum.mouseDown
@@ -198,7 +198,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 {
if (parentUI.isVisible && touchDraggedListener != null) {
touchDraggedListener!!.invoke(relativeMouseX, relativeMouseY, pointer)
touchDraggedListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer)
return true
}
@@ -209,12 +209,12 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
if (parentUI.isVisible) {
if (touchDownListener != null && mouseUp) {
touchDownListener!!.invoke(relativeMouseX, relativeMouseY, pointer, button)
touchDownListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer, button)
actionDone = true
}
if (clickOnceListener != null && !clickOnceListenerFired && mouseUp) {
clickOnceListener!!.invoke(relativeMouseX, relativeMouseY, button)
clickOnceListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, button)
actionDone = true
}
}
@@ -225,7 +225,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
clickOnceListenerFired = false
if (parentUI.isVisible && touchUpListener != null && mouseUp) {
touchUpListener!!.invoke(relativeMouseX, relativeMouseY, pointer, button)
touchUpListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer, button)
return true
}

View File

@@ -30,8 +30,8 @@ class UIItemInlineRadioButtons(
private fun getCellX(i: Int) = posX - 3 + cellWidth * i + 3 * (i + 1)
override fun update(delta: Float) {
val mx = relativeMouseX
val my = relativeMouseY
val mx = itemRelativeMouseX
val my = itemRelativeMouseY
mouseOnSelection = -1
val oldSelection = selection
if (my in 0 until height) {

View File

@@ -49,11 +49,11 @@ class UIItemSpinner(
super.update(delta)
mouseOnButton =
if (relativeMouseX in 0 until buttonW && relativeMouseY in 0 until height)
if (itemRelativeMouseX in 0 until buttonW && itemRelativeMouseY in 0 until height)
1
else if (relativeMouseX in width - buttonW until width && relativeMouseY in 0 until height)
else if (itemRelativeMouseX in width - buttonW until width && itemRelativeMouseY in 0 until height)
2
else if (relativeMouseX in buttonW + 3 until width - buttonW - 3 && relativeMouseY in 0 until height)
else if (itemRelativeMouseX in buttonW + 3 until width - buttonW - 3 && itemRelativeMouseY in 0 until height)
3
else
0

View File

@@ -9,7 +9,6 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.FrameBuffer
import com.jme3.math.FastMath
import net.torvald.terrarum.*
import net.torvald.terrarum.App.printdbg
import net.torvald.terrarum.gamecontroller.*
import net.torvald.terrarum.utils.Clipboard
import net.torvald.terrarumsansbitmap.gdx.CodepointSequence
@@ -138,11 +137,11 @@ class UIItemTextLineInput(
}
private val mouseUpOnTextArea: Boolean
get() = mouseoverUpdateLatch && relativeMouseX in 0 until fbo.width + 2* UI_TEXT_MARGIN && relativeMouseY in 0 until height
get() = mouseoverUpdateLatch && itemRelativeMouseX in 0 until fbo.width + 2 * UI_TEXT_MARGIN && itemRelativeMouseY in 0 until height
private val mouseUpOnButton1
get() = mouseoverUpdateLatch && buttonsShown > 1 && relativeMouseX in btn1PosX - posX until btn1PosX - posX + WIDTH_ONEBUTTON && relativeMouseY in 0 until height
get() = mouseoverUpdateLatch && buttonsShown > 1 && itemRelativeMouseX in btn1PosX - posX until btn1PosX - posX + WIDTH_ONEBUTTON && itemRelativeMouseY in 0 until height
private val mouseUpOnButton2
get() = mouseoverUpdateLatch && buttonsShown > 0 && relativeMouseX in btn2PosX - posX until btn2PosX - posX + WIDTH_ONEBUTTON && relativeMouseY in 0 until height
get() = mouseoverUpdateLatch && buttonsShown > 0 && itemRelativeMouseX in btn2PosX - posX until btn2PosX - posX + WIDTH_ONEBUTTON && itemRelativeMouseY in 0 until height
private var imeOn = false
private var candidates: List<CodepointSequence> = listOf()

View File

@@ -34,9 +34,9 @@ class UIItemTextSelector(
}
override val mouseUp: Boolean
get() = (relativeMouseX in 0 until width && relativeMouseY in 0 until height) or (if (paletteShowing)
(relativeMouseX in buttonW+3 until buttonW+3 + palW &&
relativeMouseY in palY - posY until palY + palH - posY)
get() = (itemRelativeMouseX in 0 until width && itemRelativeMouseY in 0 until height) or (if (paletteShowing)
(itemRelativeMouseX in buttonW + 3 until buttonW + 3 + palW &&
itemRelativeMouseY in palY - posY until palY + palH - posY)
else false)
private val labels = CommonResourcePool.getAsTextureRegionPack("inventory_category")
@@ -53,7 +53,7 @@ class UIItemTextSelector(
var selectionChangeListener: (Int) -> Unit = {}
private var mouseOnPaletteItem: Int? = null
var paletteShowing = false; private set
private val palCursorCol = Toolkit.Theme.COL_INACTIVE.cpy().mul(1f,1f,1f,0.5f)
@@ -64,23 +64,44 @@ class UIItemTextSelector(
private var palW = width - 2*buttonW - 6
private var palH = palCellHeight * labelfuns.size + 2*palCursorGap
private var labelCache: List<String> = listOf()
override fun update(delta: Float) {
super.update(delta)
mouseOnButton =
if (relativeMouseX in 0 until buttonW && relativeMouseY in 0 until height)
if (itemRelativeMouseX in 0 until buttonW && itemRelativeMouseY in 0 until height)
1
else if (relativeMouseX in width - buttonW until width && relativeMouseY in 0 until height)
else if (itemRelativeMouseX in width - buttonW until width && itemRelativeMouseY in 0 until height)
2
else if (relativeMouseX in buttonW + 3 until width - buttonW - 3 && relativeMouseY in 0 until height)
else if (itemRelativeMouseX in buttonW + 3 until width - buttonW - 3 && itemRelativeMouseY in 0 until height)
3
else
0
mouseOnPaletteItem = null // serves as the fallback value
if (paletteShowing) {
for (i in 0 until labelfuns.size) {
val ys = getPalItemPosY(i) - 2 // text cell height is hardcoded as 24; text height is 20
val ye = ys + palCellHeight
// cannot use mouseOnButton == 3 as it also takes Y-position into account
if (parentUI.relativeMouseY in ys until ye && itemRelativeMouseX in buttonW + 3 until width - buttonW - 3) {
mouseOnPaletteItem = i
break
}
}
}
if (!mouseLatched && Terrarum.mouseDown) {
// TODO if (mouse on palette items)
if (mouseOnButton in 1..2) {
if (paletteShowing && mouseOnPaletteItem != null ) {
selection = mouseOnPaletteItem!!
fboUpdateLatch = true
selectionChangeListener(selection)
paletteShowing = false
}
else if (mouseOnButton in 1..2) {
selection = (selection + (mouseOnButton * 2) - 3) fmod labelfuns.size
fboUpdateLatch = true
selectionChangeListener(selection)
@@ -106,8 +127,7 @@ class UIItemTextSelector(
}
override fun render(batch: SpriteBatch, camera: Camera) {
val labelCache = labelfuns.map { it() }
labelCache = labelfuns.map { it() }
batch.end()
@@ -180,25 +200,26 @@ class UIItemTextSelector(
// palette background
batch.color = UIItemTextLineInput.TEXTINPUT_COL_BACKGROUND
Toolkit.fillArea(batch, palX, palY, palW, palH)
Toolkit.fillArea(batch, palX, palY, palW, palH)
Toolkit.fillArea(batch, palX-1, palY-1, palW+2, palH+2)
Toolkit.fillArea(batch, palX-1, palY-1, palW+2, palH+2)
// cursor
batch.color = palCursorCol
Toolkit.drawBoxBorder(batch, posX + buttonW + 2, posY - 1, width - 2*buttonW - 4, height + 2)
// palette border
batch.color = Toolkit.Theme.COL_ACTIVE
Toolkit.drawBoxBorder(batch, palX, palY, palW, palH)
Toolkit.drawBoxBorder(batch, palX-1, palY-1, palW+2, palH+2)
// palette items
labelCache.forEachIndexed { index, s ->
batch.color = if (index == selection) Toolkit.Theme.COL_HIGHLIGHT else UIItemTextLineInput.TEXTINPUT_COL_TEXT
batch.color = if (index == selection) Toolkit.Theme.COL_HIGHLIGHT
else if (index == mouseOnPaletteItem) Toolkit.Theme.COL_ACTIVE
else UIItemTextLineInput.TEXTINPUT_COL_TEXT
val t = labelCache[index]
val tw = App.fontGame.getWidth(t)
App.fontGame.draw(batch, t,
palX + (palW - tw) / 2,
palY + 2 + palCellHeight * index +
(if (index > selection) 1 else if (index == selection) 0 else -1) * palCursorGap + palCursorGap
getPalItemPosY(index)
)
}
}
@@ -207,6 +228,12 @@ class UIItemTextSelector(
}
/**
* posY, palY and selection must be updated beforehand!
*/
private fun getPalItemPosY(index: Int) = palY + 2 + palCellHeight * index +
(if (index > selection) 1 else if (index == selection) 0 else -1) * palCursorGap + palCursorGap
override fun scrolled(amountX: Float, amountY: Float): Boolean {
if (mouseUp) {