mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-09 10:04:05 +09:00
click on palette item to select it
This commit is contained in:
@@ -78,15 +78,15 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
|||||||
protected var oldPosY: Int = initialY
|
protected var oldPosY: Int = initialY
|
||||||
|
|
||||||
/** Position of mouse relative to this item */
|
/** Position of mouse relative to this item */
|
||||||
protected val relativeMouseX: Int
|
protected val itemRelativeMouseX: Int
|
||||||
get() = (Terrarum.mouseScreenX - (parentUI.posX) - this.posX)
|
get() = (Terrarum.mouseScreenX - (parentUI.posX) - this.posX)
|
||||||
/** Position of mouse relative to this item */
|
/** Position of mouse relative to this item */
|
||||||
protected val relativeMouseY: Int
|
protected val itemRelativeMouseY: Int
|
||||||
get() = (Terrarum.mouseScreenY - (parentUI.posY) - this.posY)
|
get() = (Terrarum.mouseScreenY - (parentUI.posY) - this.posY)
|
||||||
|
|
||||||
/** If mouse is hovering over it */
|
/** If mouse is hovering over it */
|
||||||
open val mouseUp: Boolean
|
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 */
|
/** If mouse is hovering over it and mouse is down */
|
||||||
open val mousePushed: Boolean
|
open val mousePushed: Boolean
|
||||||
get() = mouseUp && Terrarum.mouseDown
|
get() = mouseUp && Terrarum.mouseDown
|
||||||
@@ -198,7 +198,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
|||||||
// mouse controlled
|
// mouse controlled
|
||||||
open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
|
open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
|
||||||
if (parentUI.isVisible && touchDraggedListener != null) {
|
if (parentUI.isVisible && touchDraggedListener != null) {
|
||||||
touchDraggedListener!!.invoke(relativeMouseX, relativeMouseY, pointer)
|
touchDraggedListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,12 +209,12 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
|||||||
|
|
||||||
if (parentUI.isVisible) {
|
if (parentUI.isVisible) {
|
||||||
if (touchDownListener != null && mouseUp) {
|
if (touchDownListener != null && mouseUp) {
|
||||||
touchDownListener!!.invoke(relativeMouseX, relativeMouseY, pointer, button)
|
touchDownListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer, button)
|
||||||
actionDone = true
|
actionDone = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clickOnceListener != null && !clickOnceListenerFired && mouseUp) {
|
if (clickOnceListener != null && !clickOnceListenerFired && mouseUp) {
|
||||||
clickOnceListener!!.invoke(relativeMouseX, relativeMouseY, button)
|
clickOnceListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, button)
|
||||||
actionDone = true
|
actionDone = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -225,7 +225,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
|
|||||||
clickOnceListenerFired = false
|
clickOnceListenerFired = false
|
||||||
|
|
||||||
if (parentUI.isVisible && touchUpListener != null && mouseUp) {
|
if (parentUI.isVisible && touchUpListener != null && mouseUp) {
|
||||||
touchUpListener!!.invoke(relativeMouseX, relativeMouseY, pointer, button)
|
touchUpListener!!.invoke(itemRelativeMouseX, itemRelativeMouseY, pointer, button)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,8 +30,8 @@ class UIItemInlineRadioButtons(
|
|||||||
private fun getCellX(i: Int) = posX - 3 + cellWidth * i + 3 * (i + 1)
|
private fun getCellX(i: Int) = posX - 3 + cellWidth * i + 3 * (i + 1)
|
||||||
|
|
||||||
override fun update(delta: Float) {
|
override fun update(delta: Float) {
|
||||||
val mx = relativeMouseX
|
val mx = itemRelativeMouseX
|
||||||
val my = relativeMouseY
|
val my = itemRelativeMouseY
|
||||||
mouseOnSelection = -1
|
mouseOnSelection = -1
|
||||||
val oldSelection = selection
|
val oldSelection = selection
|
||||||
if (my in 0 until height) {
|
if (my in 0 until height) {
|
||||||
|
|||||||
@@ -49,11 +49,11 @@ class UIItemSpinner(
|
|||||||
super.update(delta)
|
super.update(delta)
|
||||||
|
|
||||||
mouseOnButton =
|
mouseOnButton =
|
||||||
if (relativeMouseX in 0 until buttonW && relativeMouseY in 0 until height)
|
if (itemRelativeMouseX in 0 until buttonW && itemRelativeMouseY in 0 until height)
|
||||||
1
|
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
|
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
|
3
|
||||||
else
|
else
|
||||||
0
|
0
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
|||||||
import com.badlogic.gdx.graphics.glutils.FrameBuffer
|
import com.badlogic.gdx.graphics.glutils.FrameBuffer
|
||||||
import com.jme3.math.FastMath
|
import com.jme3.math.FastMath
|
||||||
import net.torvald.terrarum.*
|
import net.torvald.terrarum.*
|
||||||
import net.torvald.terrarum.App.printdbg
|
|
||||||
import net.torvald.terrarum.gamecontroller.*
|
import net.torvald.terrarum.gamecontroller.*
|
||||||
import net.torvald.terrarum.utils.Clipboard
|
import net.torvald.terrarum.utils.Clipboard
|
||||||
import net.torvald.terrarumsansbitmap.gdx.CodepointSequence
|
import net.torvald.terrarumsansbitmap.gdx.CodepointSequence
|
||||||
@@ -138,11 +137,11 @@ class UIItemTextLineInput(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val mouseUpOnTextArea: Boolean
|
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
|
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
|
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 imeOn = false
|
||||||
private var candidates: List<CodepointSequence> = listOf()
|
private var candidates: List<CodepointSequence> = listOf()
|
||||||
|
|||||||
@@ -34,9 +34,9 @@ class UIItemTextSelector(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override val mouseUp: Boolean
|
override val mouseUp: Boolean
|
||||||
get() = (relativeMouseX in 0 until width && relativeMouseY in 0 until height) or (if (paletteShowing)
|
get() = (itemRelativeMouseX in 0 until width && itemRelativeMouseY in 0 until height) or (if (paletteShowing)
|
||||||
(relativeMouseX in buttonW+3 until buttonW+3 + palW &&
|
(itemRelativeMouseX in buttonW + 3 until buttonW + 3 + palW &&
|
||||||
relativeMouseY in palY - posY until palY + palH - posY)
|
itemRelativeMouseY in palY - posY until palY + palH - posY)
|
||||||
else false)
|
else false)
|
||||||
|
|
||||||
private val labels = CommonResourcePool.getAsTextureRegionPack("inventory_category")
|
private val labels = CommonResourcePool.getAsTextureRegionPack("inventory_category")
|
||||||
@@ -53,7 +53,7 @@ class UIItemTextSelector(
|
|||||||
|
|
||||||
var selectionChangeListener: (Int) -> Unit = {}
|
var selectionChangeListener: (Int) -> Unit = {}
|
||||||
|
|
||||||
|
private var mouseOnPaletteItem: Int? = null
|
||||||
|
|
||||||
var paletteShowing = false; private set
|
var paletteShowing = false; private set
|
||||||
private val palCursorCol = Toolkit.Theme.COL_INACTIVE.cpy().mul(1f,1f,1f,0.5f)
|
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 palW = width - 2*buttonW - 6
|
||||||
private var palH = palCellHeight * labelfuns.size + 2*palCursorGap
|
private var palH = palCellHeight * labelfuns.size + 2*palCursorGap
|
||||||
|
|
||||||
|
private var labelCache: List<String> = listOf()
|
||||||
|
|
||||||
override fun update(delta: Float) {
|
override fun update(delta: Float) {
|
||||||
super.update(delta)
|
super.update(delta)
|
||||||
|
|
||||||
mouseOnButton =
|
mouseOnButton =
|
||||||
if (relativeMouseX in 0 until buttonW && relativeMouseY in 0 until height)
|
if (itemRelativeMouseX in 0 until buttonW && itemRelativeMouseY in 0 until height)
|
||||||
1
|
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
|
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
|
3
|
||||||
else
|
else
|
||||||
0
|
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) {
|
if (!mouseLatched && Terrarum.mouseDown) {
|
||||||
// TODO if (mouse on palette items)
|
if (paletteShowing && mouseOnPaletteItem != null ) {
|
||||||
if (mouseOnButton in 1..2) {
|
selection = mouseOnPaletteItem!!
|
||||||
|
fboUpdateLatch = true
|
||||||
|
selectionChangeListener(selection)
|
||||||
|
|
||||||
|
paletteShowing = false
|
||||||
|
}
|
||||||
|
else if (mouseOnButton in 1..2) {
|
||||||
selection = (selection + (mouseOnButton * 2) - 3) fmod labelfuns.size
|
selection = (selection + (mouseOnButton * 2) - 3) fmod labelfuns.size
|
||||||
fboUpdateLatch = true
|
fboUpdateLatch = true
|
||||||
selectionChangeListener(selection)
|
selectionChangeListener(selection)
|
||||||
@@ -106,8 +127,7 @@ class UIItemTextSelector(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun render(batch: SpriteBatch, camera: Camera) {
|
override fun render(batch: SpriteBatch, camera: Camera) {
|
||||||
val labelCache = labelfuns.map { it() }
|
labelCache = labelfuns.map { it() }
|
||||||
|
|
||||||
|
|
||||||
batch.end()
|
batch.end()
|
||||||
|
|
||||||
@@ -180,25 +200,26 @@ class UIItemTextSelector(
|
|||||||
|
|
||||||
// palette background
|
// palette background
|
||||||
batch.color = UIItemTextLineInput.TEXTINPUT_COL_BACKGROUND
|
batch.color = UIItemTextLineInput.TEXTINPUT_COL_BACKGROUND
|
||||||
Toolkit.fillArea(batch, palX, palY, palW, palH)
|
Toolkit.fillArea(batch, palX-1, palY-1, palW+2, palH+2)
|
||||||
Toolkit.fillArea(batch, palX, palY, palW, palH)
|
Toolkit.fillArea(batch, palX-1, palY-1, palW+2, palH+2)
|
||||||
|
|
||||||
// cursor
|
// cursor
|
||||||
batch.color = palCursorCol
|
batch.color = palCursorCol
|
||||||
Toolkit.drawBoxBorder(batch, posX + buttonW + 2, posY - 1, width - 2*buttonW - 4, height + 2)
|
Toolkit.drawBoxBorder(batch, posX + buttonW + 2, posY - 1, width - 2*buttonW - 4, height + 2)
|
||||||
// palette border
|
// palette border
|
||||||
batch.color = Toolkit.Theme.COL_ACTIVE
|
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
|
// palette items
|
||||||
labelCache.forEachIndexed { index, s ->
|
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 t = labelCache[index]
|
||||||
val tw = App.fontGame.getWidth(t)
|
val tw = App.fontGame.getWidth(t)
|
||||||
App.fontGame.draw(batch, t,
|
App.fontGame.draw(batch, t,
|
||||||
palX + (palW - tw) / 2,
|
palX + (palW - tw) / 2,
|
||||||
palY + 2 + palCellHeight * index +
|
getPalItemPosY(index)
|
||||||
(if (index > selection) 1 else if (index == selection) 0 else -1) * palCursorGap + palCursorGap
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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 {
|
override fun scrolled(amountX: Float, amountY: Float): Boolean {
|
||||||
if (mouseUp) {
|
if (mouseUp) {
|
||||||
|
|||||||
Reference in New Issue
Block a user