mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-13 03:54:06 +09:00
UINSMenu is made to show how to code 'UI dragging'
This commit is contained in:
@@ -3,6 +3,7 @@ package net.torvald.terrarum.ui
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.graphics.Camera
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import net.torvald.terrarum.AppLoader
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.Second
|
||||
import net.torvald.terrarum.roundInt
|
||||
@@ -80,7 +81,12 @@ abstract class UICanvas(
|
||||
|
||||
/** Override this for the actual update. Note that you must update uiItems by yourself. */
|
||||
abstract fun updateUI(delta: Float)
|
||||
/** Override this for the actual render. Note that you must render uiItems by yourself. */
|
||||
/**
|
||||
* Override this for the actual render. Note that you must render uiItems by yourself.
|
||||
*
|
||||
* Under normal circumstances, draws are automatically translated as per the handler's X/Y position.
|
||||
* This means, don't write like: ```draw(posX + 4, posY + 32)```, do instead: ```draw(4, 32)``` unless you have a good reason to do so.
|
||||
*/
|
||||
abstract fun renderUI(batch: SpriteBatch, camera: Camera)
|
||||
|
||||
/**
|
||||
@@ -109,6 +115,8 @@ abstract class UICanvas(
|
||||
uiItems.add(uiItem)
|
||||
}
|
||||
|
||||
fun mouseInScreen(x: Int, y: Int) = x in 0 until AppLoader.appConfig.width && y in 0 until AppLoader.appConfig.height
|
||||
|
||||
open fun mouseMoved(screenX: Int, screenY: Int): Boolean {
|
||||
if (this.isVisible) {
|
||||
uiItems.forEach { it.mouseMoved(screenX, screenY) }
|
||||
@@ -117,6 +125,10 @@ abstract class UICanvas(
|
||||
}
|
||||
else return false
|
||||
}
|
||||
|
||||
/**
|
||||
* When implementing this, make sure to use ```mouseInScreen()``` function!
|
||||
*/
|
||||
open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
|
||||
if (this.isVisible) {
|
||||
uiItems.forEach { it.touchDragged(screenX, screenY, pointer) }
|
||||
@@ -126,7 +138,7 @@ abstract class UICanvas(
|
||||
else return false
|
||||
}
|
||||
open fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
||||
if (this.isVisible) {
|
||||
if (this.isVisible && mouseInScreen(screenX, screenY)) {
|
||||
uiItems.forEach { it.touchDown(screenX, screenY, pointer, button) }
|
||||
handler.subUIs.forEach { it.touchDown(screenX, screenY, pointer, button) }
|
||||
return true
|
||||
|
||||
@@ -107,6 +107,10 @@ abstract class UIItem(var parentUI: UICanvas) { // do not replace parentUI to UI
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In this time, you do write like: ```draw(posX + 4, posY + 32)```, unlike UICanvas, because posX/posY comes from the parent UI.
|
||||
*/
|
||||
open fun render(batch: SpriteBatch, camera: Camera) {
|
||||
if (parentUI.isVisible) {
|
||||
mouseOverCall?.render(batch, camera)
|
||||
|
||||
@@ -17,8 +17,9 @@ class UINSMenu(
|
||||
|
||||
val titleBackCol: Color = Color(0f,0f,0f,.77f),
|
||||
val titleTextCol: Color = Color.WHITE,
|
||||
val titleBlendMode: String = BlendMode.NORMAL
|
||||
val titleBlendMode: String = BlendMode.NORMAL,
|
||||
|
||||
val allowDrag: Boolean = true
|
||||
) : UICanvas() {
|
||||
|
||||
override var openCloseTime: Second = 0f
|
||||
@@ -59,11 +60,11 @@ class UINSMenu(
|
||||
// draw title bar
|
||||
batch.color = titleBackCol
|
||||
BlendMode.resolve(titleBlendMode, batch)
|
||||
batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), LINE_HEIGHT.toFloat())
|
||||
batch.fillRect(0f, 0f, width.toFloat(), LINE_HEIGHT.toFloat())
|
||||
|
||||
batch.color = titleTextCol
|
||||
blendNormal(batch)
|
||||
Terrarum.fontGame.draw(batch, title, posX + TEXT_OFFSETX, posY + TEXT_OFFSETY)
|
||||
Terrarum.fontGame.draw(batch, title, TEXT_OFFSETX, TEXT_OFFSETY)
|
||||
|
||||
// draw the list
|
||||
batch.color = Color.WHITE
|
||||
@@ -74,6 +75,9 @@ class UINSMenu(
|
||||
theRealList.dispose()
|
||||
}
|
||||
|
||||
fun mouseOnTitleBar() =
|
||||
relativeMouseX in 0 until width && relativeMouseY in 0 until LINE_HEIGHT
|
||||
|
||||
override fun doOpening(delta: Float) {
|
||||
}
|
||||
|
||||
@@ -90,12 +94,34 @@ class UINSMenu(
|
||||
return super.mouseMoved(screenX, screenY)
|
||||
}
|
||||
|
||||
private var dragOriginX = 0 // relative mousepos
|
||||
private var dragOriginY = 0 // relative mousepos
|
||||
private var dragForReal = false
|
||||
|
||||
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
|
||||
return super.touchDragged(screenX, screenY, pointer)
|
||||
if (!allowDrag) return false
|
||||
|
||||
if (mouseInScreen(screenX, screenY)) {
|
||||
if (dragForReal) {
|
||||
handler.setPosition(screenX - dragOriginX, screenY - dragOriginY)
|
||||
println("drag $screenX, $screenY")
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
||||
return super.touchDown(screenX, screenY, pointer, button)
|
||||
if (mouseOnTitleBar()) {
|
||||
dragOriginX = relativeMouseX
|
||||
dragOriginY = relativeMouseY
|
||||
dragForReal = true
|
||||
}
|
||||
else {
|
||||
dragForReal = false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
||||
|
||||
Reference in New Issue
Block a user