mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-17 00:56:07 +09:00
new toggler design
This commit is contained in:
BIN
assets/graphics/gui/toggler_icons.tga
LFS
Normal file
BIN
assets/graphics/gui/toggler_icons.tga
LFS
Normal file
Binary file not shown.
@@ -79,7 +79,7 @@ class UIGraphicsControlPanel(remoCon: UIRemoCon?) : UICanvas() {
|
|||||||
}) to { _, _ -> }
|
}) to { _, _ -> }
|
||||||
}
|
}
|
||||||
else if (args.startsWith("toggle")) {
|
else if (args.startsWith("toggle")) {
|
||||||
UIItemToggleButton(this, x, y, App.getConfigBoolean(optionName)) to { it: UIItem, optionStr: String ->
|
UIItemToggleButton(this, x, y, spinnerWidth, App.getConfigBoolean(optionName)) to { it: UIItem, optionStr: String ->
|
||||||
(it as UIItemToggleButton).clickOnceListener = { _, _, _ ->
|
(it as UIItemToggleButton).clickOnceListener = { _, _, _ ->
|
||||||
it.toggle()
|
it.toggle()
|
||||||
App.setConfig(optionStr, it.getStatus())
|
App.setConfig(optionStr, it.getStatus())
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ class UIElemTest : ApplicationAdapter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class DummyTogglePane : UICanvas() {
|
class DummyTogglePane : UICanvas() {
|
||||||
private val button1 = UIItemToggleButton(this, 400, 100)
|
private val button1 = UIItemToggleButton(this, 400, 100, 80)
|
||||||
private val textin = UIItemTextLineInput(this, 400, 160, 400)
|
private val textin = UIItemTextLineInput(this, 400, 160, 400)
|
||||||
|
|
||||||
override var width = 800
|
override var width = 800
|
||||||
|
|||||||
@@ -5,41 +5,37 @@ import com.badlogic.gdx.graphics.Camera
|
|||||||
import com.badlogic.gdx.graphics.Color
|
import com.badlogic.gdx.graphics.Color
|
||||||
import com.badlogic.gdx.graphics.Texture
|
import com.badlogic.gdx.graphics.Texture
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||||
import com.badlogic.gdx.graphics.g2d.TextureRegion
|
|
||||||
import net.torvald.terrarum.CommonResourcePool
|
import net.torvald.terrarum.CommonResourcePool
|
||||||
import net.torvald.terrarum.blendNormalStraightAlpha
|
import net.torvald.terrarum.blendNormalStraightAlpha
|
||||||
import net.torvald.terrarum.toInt
|
import net.torvald.terrarum.toInt
|
||||||
|
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Torvald on 2019-10-17.
|
* Created by Torvald on 2019-10-17.
|
||||||
*/
|
*/
|
||||||
class UIItemToggleButton(
|
class UIItemToggleButton(
|
||||||
parent: UICanvas,
|
parent: UICanvas,
|
||||||
initialX: Int,
|
initialX: Int,
|
||||||
initialY: Int,
|
initialY: Int,
|
||||||
private var status: Boolean = false
|
override val width: Int,
|
||||||
|
private var status: Boolean = false,
|
||||||
) : UIItem(parent, initialX, initialY) {
|
) : UIItem(parent, initialX, initialY) {
|
||||||
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
CommonResourcePool.addToLoadingList("ui_item_toggler_base") {
|
CommonResourcePool.addToLoadingList("gui_toggler_icons") {
|
||||||
TextureRegion(Texture(Gdx.files.internal("./assets/graphics/gui/toggler_back.tga")))
|
TextureRegionPack(Texture(Gdx.files.internal("assets/graphics/gui/toggler_icons.tga")), 14, 14)
|
||||||
}
|
|
||||||
CommonResourcePool.addToLoadingList("ui_item_toggler_handle") {
|
|
||||||
TextureRegion(Texture(Gdx.files.internal("./assets/graphics/gui/toggler_switch.tga")))
|
|
||||||
}
|
}
|
||||||
CommonResourcePool.loadAll()
|
CommonResourcePool.loadAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
override val width: Int
|
|
||||||
get() = togglerBase.regionWidth
|
|
||||||
override val height: Int
|
|
||||||
get() = togglerBase.regionHeight
|
|
||||||
|
|
||||||
private var togglerBase = CommonResourcePool.getAsTextureRegion("ui_item_toggler_base")
|
override val height = 24
|
||||||
private var togglerHandle = CommonResourcePool.getAsTextureRegion("ui_item_toggler_handle")
|
|
||||||
|
|
||||||
private var handleTravelDist = togglerBase.regionWidth - togglerHandle.regionWidth
|
private var handleWidth = 30
|
||||||
|
|
||||||
|
private var handleTravelDist = width - handleWidth
|
||||||
private var handlePos = handleTravelDist * status.toInt()
|
private var handlePos = handleTravelDist * status.toInt()
|
||||||
|
|
||||||
private var animTimer = 0f
|
private var animTimer = 0f
|
||||||
@@ -67,6 +63,8 @@ class UIItemToggleButton(
|
|||||||
|
|
||||||
// define clickOnceListener by yourself!
|
// define clickOnceListener by yourself!
|
||||||
|
|
||||||
|
private var mouseOnHandle = false
|
||||||
|
|
||||||
override fun update(delta: Float) {
|
override fun update(delta: Float) {
|
||||||
super.update(delta)
|
super.update(delta)
|
||||||
|
|
||||||
@@ -85,16 +83,64 @@ class UIItemToggleButton(
|
|||||||
animCalled = false
|
animCalled = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
mouseOnHandle = itemRelativeMouseX in handlePos until handlePos + handleWidth && itemRelativeMouseY in 0 until height
|
||||||
|
|
||||||
|
|
||||||
oldPosX = posX
|
oldPosX = posX
|
||||||
oldPosY = posY
|
oldPosY = posY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val togglerIcon = CommonResourcePool.getAsTextureRegionPack("gui_toggler_icons")
|
||||||
|
|
||||||
|
private val renderJobs = arrayOf(
|
||||||
|
// trough fill
|
||||||
|
{ batch: SpriteBatch ->
|
||||||
|
batch.color = UIItemTextLineInput.TEXTINPUT_COL_BACKGROUND
|
||||||
|
Toolkit.fillArea(batch, posX, posY, width, height)
|
||||||
|
batch.color = togglerIconCol[1]
|
||||||
|
batch.draw(togglerIcon.get(1,0), posX.toFloat() + (handleWidth - 14)/2, posY.toFloat() + (height - 14)/2)
|
||||||
|
batch.color = togglerIconCol[0]
|
||||||
|
batch.draw(togglerIcon.get(0,0), posX.toFloat() + width - handleWidth + (handleWidth - 14)/2, posY.toFloat() + (height - 14)/2)
|
||||||
|
},
|
||||||
|
// trough border
|
||||||
|
{ batch: SpriteBatch ->
|
||||||
|
batch.color = troughBorderCol
|
||||||
|
Toolkit.drawBoxBorder(batch, posX - 1, posY - 1, width + 2, height + 2)
|
||||||
|
},
|
||||||
|
// handle fill
|
||||||
|
{ batch: SpriteBatch ->
|
||||||
|
batch.color = handleCol.cpy().mul(Color.LIGHT_GRAY)
|
||||||
|
Toolkit.fillArea(batch, posX + handlePos, posY, handleWidth, height)
|
||||||
|
},
|
||||||
|
// handle border
|
||||||
|
{ batch: SpriteBatch ->
|
||||||
|
batch.color = handleCol
|
||||||
|
Toolkit.drawBoxBorder(batch, posX + handlePos - 1, posY - 1, handleWidth + 2, height + 2)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
val troughBorderCol: Color; get() = if (mouseUp && !mouseOnHandle && mousePushed) Toolkit.Theme.COL_SELECTED
|
||||||
|
else if (mouseUp && !mouseOnHandle) Toolkit.Theme.COL_MOUSE_UP else Toolkit.Theme.COL_INACTIVE
|
||||||
|
val handleCol: Color; get() = if (mouseOnHandle && mousePushed) Toolkit.Theme.COL_SELECTED
|
||||||
|
else if (mouseOnHandle) Toolkit.Theme.COL_MOUSE_UP else Color.WHITE
|
||||||
|
|
||||||
|
private val renderOrderMouseUp = arrayOf(0,2,3,1).map { renderJobs[it] }
|
||||||
|
private val renderOrderNormal = arrayOf(0,1,2,3).map { renderJobs[it] }
|
||||||
|
|
||||||
|
private val togglerIconCol = arrayOf(
|
||||||
|
Color(0xFF8888FF.toInt()),
|
||||||
|
Color(0x00E800FF)
|
||||||
|
)
|
||||||
|
|
||||||
override fun render(batch: SpriteBatch, camera: Camera) {
|
override fun render(batch: SpriteBatch, camera: Camera) {
|
||||||
batch.color = Color.WHITE
|
|
||||||
blendNormalStraightAlpha(batch)
|
blendNormalStraightAlpha(batch)
|
||||||
|
|
||||||
batch.draw(togglerBase, posX.toFloat(), posY.toFloat())
|
|
||||||
batch.draw(togglerHandle, (posX + handlePos).toFloat(), posY.toFloat())
|
if (mouseUp && !mouseOnHandle)
|
||||||
|
renderOrderMouseUp.forEach { it(batch) }
|
||||||
|
else
|
||||||
|
renderOrderNormal.forEach { it(batch) }
|
||||||
|
|
||||||
|
|
||||||
super.render(batch, camera)
|
super.render(batch, camera)
|
||||||
|
|||||||
Reference in New Issue
Block a user