mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-08 04:41:51 +09:00
uiitem toggle button impl
This commit is contained in:
@@ -17,7 +17,7 @@ object CommonResourcePool {
|
||||
private val poolKillFun = HashMap<String, (() -> Unit)?>()
|
||||
//private val typesMap = HashMap<String, Class<*>>()
|
||||
private var loadCounter = -1 // using counters so that the loading can be done on separate thread (gg if the asset requires GL context to be loaded)
|
||||
val loaded: Boolean
|
||||
val loaded: Boolean // see if there's a thing to load
|
||||
get() = loadCounter == 0
|
||||
|
||||
init {
|
||||
@@ -93,7 +93,7 @@ object CommonResourcePool {
|
||||
* Consumes the loading list. After the load, the list will be empty
|
||||
*/
|
||||
fun loadAll() {
|
||||
if (loaded) return //throw IllegalStateException("Assets are already loaded and shipped out :p")
|
||||
if (loaded) return
|
||||
|
||||
while (!loadingList.isEmpty) {
|
||||
val (name, loadfun, killfun) = loadingList.removeFirst()
|
||||
|
||||
@@ -509,8 +509,8 @@ fun Float.ceilInt() = FastMath.ceil(this)
|
||||
fun Double.round() = Math.round(this).toDouble()
|
||||
fun Double.floor() = Math.floor(this)
|
||||
fun Double.ceil() = this.floor() + 1.0
|
||||
fun Double.roundInt(): Int = Math.round(this).toInt()
|
||||
fun Float.roundInt(): Int = Math.round(this)
|
||||
@Deprecated("Use kotlin.roundToInt") fun Double.roundInt(): Int = Math.round(this).toInt()
|
||||
@Deprecated("Use kotlin.roundToInt") fun Float.roundInt(): Int = Math.round(this)
|
||||
fun Double.abs() = Math.abs(this)
|
||||
fun Double.sqr() = this * this
|
||||
fun Float.sqr() = this * this
|
||||
|
||||
112
src/net/torvald/terrarum/tests/UIElemTest.kt
Normal file
112
src/net/torvald/terrarum/tests/UIElemTest.kt
Normal file
@@ -0,0 +1,112 @@
|
||||
package net.torvald.terrarum.tests
|
||||
|
||||
import com.badlogic.gdx.ApplicationAdapter
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
|
||||
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
|
||||
import com.badlogic.gdx.graphics.Camera
|
||||
import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera
|
||||
import com.badlogic.gdx.graphics.Texture
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import com.badlogic.gdx.graphics.glutils.ShaderProgram
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
|
||||
import com.jme3.math.FastMath
|
||||
import net.torvald.terrarum.*
|
||||
import net.torvald.terrarum.ui.UICanvas
|
||||
import net.torvald.terrarum.ui.UIItem
|
||||
import net.torvald.terrarum.ui.UIItemTextButton
|
||||
import net.torvald.terrarum.ui.UIItemToggleButton
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* Created by Torvald on 2019-10-16.
|
||||
*/
|
||||
|
||||
class UIElemTest : ApplicationAdapter() {
|
||||
|
||||
|
||||
private lateinit var batch: SpriteBatch
|
||||
private lateinit var camera: OrthographicCamera
|
||||
private lateinit var ui: UICanvas
|
||||
|
||||
override fun create() {
|
||||
batch = SpriteBatch()
|
||||
camera = OrthographicCamera()
|
||||
camera.setToOrtho(false, 800f, 600f)
|
||||
camera.update()
|
||||
ui = DummyTogglePane()
|
||||
ui.isVisible = true
|
||||
}
|
||||
|
||||
|
||||
override fun render() {
|
||||
ui.update(Gdx.graphics.rawDeltaTime)
|
||||
ui.render(batch, camera)
|
||||
}
|
||||
|
||||
|
||||
|
||||
override fun dispose() {
|
||||
batch.dispose()
|
||||
}
|
||||
}
|
||||
|
||||
class DummyTogglePane : UICanvas() {
|
||||
private val button1 = UIItemToggleButton(this, 10, 10)
|
||||
|
||||
override var width = 100
|
||||
override var height = 25
|
||||
|
||||
override var openCloseTime: Second = 0f
|
||||
|
||||
private var timer = 0f
|
||||
|
||||
override fun updateUI(delta: Float) {
|
||||
timer += delta
|
||||
|
||||
if (timer >= 1.5f) {
|
||||
timer -= 1.5f
|
||||
button1.toggle()
|
||||
}
|
||||
|
||||
button1.update(delta)
|
||||
}
|
||||
|
||||
override fun renderUI(batch: SpriteBatch, camera: Camera) {
|
||||
batch.inUse {
|
||||
batch.color = Color(0x404040ff)
|
||||
button1.render(batch, camera)
|
||||
}
|
||||
}
|
||||
|
||||
override fun doOpening(delta: Float) {
|
||||
}
|
||||
|
||||
override fun doClosing(delta: Float) {
|
||||
}
|
||||
|
||||
override fun endOpening(delta: Float) {
|
||||
}
|
||||
|
||||
override fun endClosing(delta: Float) {
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
ShaderProgram.pedantic = false
|
||||
|
||||
val appConfig = LwjglApplicationConfiguration()
|
||||
appConfig.vSyncEnabled = false
|
||||
appConfig.resizable = false
|
||||
appConfig.width = 800
|
||||
appConfig.height = 600
|
||||
appConfig.backgroundFPS = 60
|
||||
appConfig.foregroundFPS = 60
|
||||
appConfig.forceExit = false
|
||||
|
||||
LwjglApplication(UIElemTest(), appConfig)
|
||||
}
|
||||
104
src/net/torvald/terrarum/ui/UIItemToggleButton.kt
Normal file
104
src/net/torvald/terrarum/ui/UIItemToggleButton.kt
Normal file
@@ -0,0 +1,104 @@
|
||||
package net.torvald.terrarum.ui
|
||||
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.graphics.Camera
|
||||
import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.graphics.Texture
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import net.torvald.terrarum.CommonResourcePool
|
||||
import net.torvald.terrarum.blendNormal
|
||||
import net.torvald.terrarum.toInt
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* Created by Torvald on 2019-10-17.
|
||||
*/
|
||||
class UIItemToggleButton(
|
||||
parent: UICanvas,
|
||||
override var posX: Int,
|
||||
override var posY: Int,
|
||||
private var status: Boolean = false
|
||||
) : UIItem(parent) {
|
||||
|
||||
init {
|
||||
CommonResourcePool.addToLoadingList("ui_item_toggler_base") {
|
||||
Texture(Gdx.files.internal("./assets/graphics/gui/toggler_back.tga"))
|
||||
}
|
||||
CommonResourcePool.addToLoadingList("ui_item_toggler_handle") {
|
||||
Texture(Gdx.files.internal("./assets/graphics/gui/toggler_switch.tga"))
|
||||
}
|
||||
CommonResourcePool.loadAll()
|
||||
}
|
||||
|
||||
override val width: Int
|
||||
get() = togglerBase.width
|
||||
override val height: Int
|
||||
get() = togglerBase.height
|
||||
override var oldPosX = posX
|
||||
override var oldPosY = posY
|
||||
|
||||
private var togglerBase = CommonResourcePool.getAsTexture("ui_item_toggler_base")
|
||||
private var togglerHandle = CommonResourcePool.getAsTexture("ui_item_toggler_handle")
|
||||
|
||||
private var handleTravelDist = togglerBase.width - togglerHandle.width
|
||||
private var handlePos = handleTravelDist * status.toInt()
|
||||
|
||||
private var animTimer = 0f
|
||||
private var animLength = 0.1f
|
||||
|
||||
private var animCalled = false
|
||||
|
||||
fun getStatus() = status
|
||||
|
||||
fun setAsTrue() {
|
||||
animCalled = true
|
||||
animTimer = 0f
|
||||
status = true
|
||||
}
|
||||
|
||||
fun setAsFalse() {
|
||||
animCalled = true
|
||||
animTimer = 0f
|
||||
status = false
|
||||
}
|
||||
|
||||
fun toggle() {
|
||||
if (status) setAsFalse() else setAsTrue()
|
||||
}
|
||||
|
||||
override fun update(delta: Float) {
|
||||
super.update(delta)
|
||||
|
||||
// make things move
|
||||
if (animCalled) {
|
||||
handlePos = if (status)
|
||||
((animTimer / animLength) * handleTravelDist).roundToInt()
|
||||
else
|
||||
handleTravelDist - ((animTimer / animLength) * handleTravelDist).roundToInt()
|
||||
|
||||
animTimer += delta
|
||||
}
|
||||
|
||||
if (animTimer >= animLength) {
|
||||
handlePos = handleTravelDist * status.toInt()
|
||||
animCalled = false
|
||||
}
|
||||
|
||||
oldPosX = posX
|
||||
oldPosY = posY
|
||||
}
|
||||
|
||||
override fun render(batch: SpriteBatch, camera: Camera) {
|
||||
batch.color = Color.WHITE
|
||||
blendNormal(batch)
|
||||
|
||||
batch.draw(togglerBase, posX.toFloat(), posY.toFloat())
|
||||
batch.draw(togglerHandle, (posX + handlePos).toFloat(), posY.toFloat())
|
||||
|
||||
|
||||
super.render(batch, camera)
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user