mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-06 08:38:30 +09:00
UINSMenu is made to show how to code 'UI dragging'
This commit is contained in:
@@ -37,8 +37,8 @@ class UITooltip : UICanvas() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun renderUI(batch: SpriteBatch, camera: Camera) {
|
override fun renderUI(batch: SpriteBatch, camera: Camera) {
|
||||||
val mouseX = Terrarum.mouseScreenX.toFloat() + 4
|
val mouseX = 4f
|
||||||
val mouseY = Terrarum.mouseScreenY.toFloat() - 6
|
val mouseY = 6f
|
||||||
|
|
||||||
val tooltipY = mouseY - textures.tileH
|
val tooltipY = mouseY - textures.tileH
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package net.torvald.terrarum.tests
|
package net.torvald.terrarum.tests
|
||||||
|
|
||||||
import com.badlogic.gdx.Game
|
import com.badlogic.gdx.*
|
||||||
import com.badlogic.gdx.Gdx
|
|
||||||
import com.badlogic.gdx.Screen
|
|
||||||
import com.badlogic.gdx.ScreenAdapter
|
|
||||||
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
|
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
|
||||||
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
|
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
|
||||||
import com.badlogic.gdx.graphics.Color
|
import com.badlogic.gdx.graphics.Color
|
||||||
@@ -60,6 +57,8 @@ class UITestPad1 : ScreenAdapter() {
|
|||||||
lateinit var camera: OrthographicCamera
|
lateinit var camera: OrthographicCamera
|
||||||
|
|
||||||
override fun show() {
|
override fun show() {
|
||||||
|
Gdx.input.inputProcessor = UITestPad1Controller(this)
|
||||||
|
|
||||||
nsMenu = UINSMenu(
|
nsMenu = UINSMenu(
|
||||||
"Menu",
|
"Menu",
|
||||||
96,
|
96,
|
||||||
@@ -84,17 +83,31 @@ class UITestPad1 : ScreenAdapter() {
|
|||||||
var _dct = 0f
|
var _dct = 0f
|
||||||
|
|
||||||
override fun render(delta: Float) {
|
override fun render(delta: Float) {
|
||||||
|
// UPDATE
|
||||||
|
|
||||||
|
|
||||||
|
// RENDER
|
||||||
|
|
||||||
batch.inUse {
|
batch.inUse {
|
||||||
batch.color = bgCol
|
batch.color = bgCol
|
||||||
batch.fillRect(0f, 0f, 2048f, 2048f)
|
batch.fillRect(0f, 0f, 2048f, 2048f)
|
||||||
|
|
||||||
nsMenu.render(batch, camera)
|
nsMenu.render(batch, camera)
|
||||||
|
|
||||||
|
batch.color = if (nsMenu.mouseOnTitleBar())
|
||||||
|
Color.LIME
|
||||||
|
else
|
||||||
|
Color.FIREBRICK
|
||||||
|
AppLoader.fontGame.draw(batch, "Mouse: ${Terrarum.mouseScreenX}, ${Terrarum.mouseScreenY}", 8f, 740 - 28f)
|
||||||
}
|
}
|
||||||
|
|
||||||
_dct = (_dct + delta*2) % 10f
|
_dct = (_dct + delta*2) % 10f
|
||||||
//nsMenu.setPosition(_dct.toInt(), _dct.toInt())
|
//nsMenu.setPosition(_dct.toInt(), _dct.toInt())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
override fun pause() {
|
override fun pause() {
|
||||||
super.pause()
|
super.pause()
|
||||||
}
|
}
|
||||||
@@ -114,6 +127,18 @@ class UITestPad1 : ScreenAdapter() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class UITestPad1Controller(val host: UITestPad1) : InputAdapter() {
|
||||||
|
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
|
||||||
|
host.nsMenu.touchDragged(screenX, screenY, pointer)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
||||||
|
host.nsMenu.touchDown(screenX, screenY, pointer, button)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
ShaderProgram.pedantic = false
|
ShaderProgram.pedantic = false
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package net.torvald.terrarum.ui
|
|||||||
import com.badlogic.gdx.Gdx
|
import com.badlogic.gdx.Gdx
|
||||||
import com.badlogic.gdx.graphics.Camera
|
import com.badlogic.gdx.graphics.Camera
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||||
|
import net.torvald.terrarum.AppLoader
|
||||||
import net.torvald.terrarum.Terrarum
|
import net.torvald.terrarum.Terrarum
|
||||||
import net.torvald.terrarum.Second
|
import net.torvald.terrarum.Second
|
||||||
import net.torvald.terrarum.roundInt
|
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. */
|
/** Override this for the actual update. Note that you must update uiItems by yourself. */
|
||||||
abstract fun updateUI(delta: Float)
|
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)
|
abstract fun renderUI(batch: SpriteBatch, camera: Camera)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -109,6 +115,8 @@ abstract class UICanvas(
|
|||||||
uiItems.add(uiItem)
|
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 {
|
open fun mouseMoved(screenX: Int, screenY: Int): Boolean {
|
||||||
if (this.isVisible) {
|
if (this.isVisible) {
|
||||||
uiItems.forEach { it.mouseMoved(screenX, screenY) }
|
uiItems.forEach { it.mouseMoved(screenX, screenY) }
|
||||||
@@ -117,6 +125,10 @@ abstract class UICanvas(
|
|||||||
}
|
}
|
||||||
else return false
|
else return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When implementing this, make sure to use ```mouseInScreen()``` function!
|
||||||
|
*/
|
||||||
open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
|
open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
|
||||||
if (this.isVisible) {
|
if (this.isVisible) {
|
||||||
uiItems.forEach { it.touchDragged(screenX, screenY, pointer) }
|
uiItems.forEach { it.touchDragged(screenX, screenY, pointer) }
|
||||||
@@ -126,7 +138,7 @@ abstract class UICanvas(
|
|||||||
else return false
|
else return false
|
||||||
}
|
}
|
||||||
open fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
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) }
|
uiItems.forEach { it.touchDown(screenX, screenY, pointer, button) }
|
||||||
handler.subUIs.forEach { it.touchDown(screenX, screenY, pointer, button) }
|
handler.subUIs.forEach { it.touchDown(screenX, screenY, pointer, button) }
|
||||||
return true
|
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) {
|
open fun render(batch: SpriteBatch, camera: Camera) {
|
||||||
if (parentUI.isVisible) {
|
if (parentUI.isVisible) {
|
||||||
mouseOverCall?.render(batch, camera)
|
mouseOverCall?.render(batch, camera)
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ class UINSMenu(
|
|||||||
|
|
||||||
val titleBackCol: Color = Color(0f,0f,0f,.77f),
|
val titleBackCol: Color = Color(0f,0f,0f,.77f),
|
||||||
val titleTextCol: Color = Color.WHITE,
|
val titleTextCol: Color = Color.WHITE,
|
||||||
val titleBlendMode: String = BlendMode.NORMAL
|
val titleBlendMode: String = BlendMode.NORMAL,
|
||||||
|
|
||||||
|
val allowDrag: Boolean = true
|
||||||
) : UICanvas() {
|
) : UICanvas() {
|
||||||
|
|
||||||
override var openCloseTime: Second = 0f
|
override var openCloseTime: Second = 0f
|
||||||
@@ -59,11 +60,11 @@ class UINSMenu(
|
|||||||
// draw title bar
|
// draw title bar
|
||||||
batch.color = titleBackCol
|
batch.color = titleBackCol
|
||||||
BlendMode.resolve(titleBlendMode, batch)
|
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
|
batch.color = titleTextCol
|
||||||
blendNormal(batch)
|
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
|
// draw the list
|
||||||
batch.color = Color.WHITE
|
batch.color = Color.WHITE
|
||||||
@@ -74,6 +75,9 @@ class UINSMenu(
|
|||||||
theRealList.dispose()
|
theRealList.dispose()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun mouseOnTitleBar() =
|
||||||
|
relativeMouseX in 0 until width && relativeMouseY in 0 until LINE_HEIGHT
|
||||||
|
|
||||||
override fun doOpening(delta: Float) {
|
override fun doOpening(delta: Float) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,12 +94,34 @@ class UINSMenu(
|
|||||||
return super.mouseMoved(screenX, screenY)
|
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 {
|
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 {
|
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 {
|
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
|
||||||
|
|||||||
Reference in New Issue
Block a user