UINSMenu is made to show how to code 'UI dragging'

This commit is contained in:
Minjae Song
2018-12-09 20:00:52 +09:00
parent 0c20ed3418
commit b44699ae04
5 changed files with 80 additions and 13 deletions

View File

@@ -37,8 +37,8 @@ class UITooltip : UICanvas() {
}
override fun renderUI(batch: SpriteBatch, camera: Camera) {
val mouseX = Terrarum.mouseScreenX.toFloat() + 4
val mouseY = Terrarum.mouseScreenY.toFloat() - 6
val mouseX = 4f
val mouseY = 6f
val tooltipY = mouseY - textures.tileH

View File

@@ -1,9 +1,6 @@
package net.torvald.terrarum.tests
import com.badlogic.gdx.Game
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Screen
import com.badlogic.gdx.ScreenAdapter
import com.badlogic.gdx.*
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
import com.badlogic.gdx.graphics.Color
@@ -60,6 +57,8 @@ class UITestPad1 : ScreenAdapter() {
lateinit var camera: OrthographicCamera
override fun show() {
Gdx.input.inputProcessor = UITestPad1Controller(this)
nsMenu = UINSMenu(
"Menu",
96,
@@ -84,17 +83,31 @@ class UITestPad1 : ScreenAdapter() {
var _dct = 0f
override fun render(delta: Float) {
// UPDATE
// RENDER
batch.inUse {
batch.color = bgCol
batch.fillRect(0f, 0f, 2048f, 2048f)
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
//nsMenu.setPosition(_dct.toInt(), _dct.toInt())
}
override fun 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>) {
ShaderProgram.pedantic = false

View File

@@ -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

View File

@@ -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)

View File

@@ -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 {