mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-11 02:54:04 +09:00
UI Remote Controller reworked (now 90% less stupidity)
This commit is contained in:
@@ -60,16 +60,16 @@ class UIInventoryFull(
|
||||
|
||||
|
||||
val catBarWidth = 330
|
||||
val catBar = UIItemInventoryCatBar(
|
||||
val categoryBar = UIItemInventoryCatBar(
|
||||
this,
|
||||
(Terrarum.WIDTH - catBarWidth) / 2,
|
||||
66 + (Terrarum.HEIGHT - internalHeight) / 2,
|
||||
catBarWidth
|
||||
)
|
||||
val catSelection: Int
|
||||
get() = catBar.selectedIndex
|
||||
get() = categoryBar.selectedIndex
|
||||
val catSelectedIcon: Int
|
||||
get() = catBar.selectedIcon
|
||||
get() = categoryBar.selectedIcon
|
||||
|
||||
override var openCloseTime: Second = 0.0f
|
||||
|
||||
@@ -101,12 +101,16 @@ class UIInventoryFull(
|
||||
|
||||
|
||||
init {
|
||||
addItem(catBar)
|
||||
addItem(categoryBar)
|
||||
itemList?.let { addItem(it) }
|
||||
equipped?.let { addItem(it) }
|
||||
|
||||
|
||||
catBar.selectionChangeListener = { old, new -> rebuildList() }
|
||||
categoryBar.selectionChangeListener = { old, new ->
|
||||
rebuildList()
|
||||
itemList?.itemPage = 0 // set scroll to zero
|
||||
itemList?.rebuild() // have to manually rebuild, too!
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -125,7 +129,7 @@ class UIInventoryFull(
|
||||
}
|
||||
|
||||
|
||||
catBar.update(delta)
|
||||
categoryBar.update(delta)
|
||||
itemList?.update(delta)
|
||||
equipped?.update(delta)
|
||||
}
|
||||
@@ -163,7 +167,7 @@ class UIInventoryFull(
|
||||
batch.begin()
|
||||
|
||||
// UI items
|
||||
catBar.render(batch, camera)
|
||||
categoryBar.render(batch, camera)
|
||||
itemList?.render(batch, camera)
|
||||
equipped?.render(batch, camera)
|
||||
|
||||
@@ -221,7 +225,7 @@ class UIInventoryFull(
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
catBar.dispose()
|
||||
categoryBar.dispose()
|
||||
itemList?.dispose()
|
||||
equipped?.dispose()
|
||||
}
|
||||
|
||||
@@ -183,12 +183,12 @@ class UIItemInventoryDynamicList(
|
||||
}
|
||||
|
||||
scrollUpButton.clickOnceListener = { _, _, _ ->
|
||||
itemPage = (itemPage - 1).fmod(itemPageCount)
|
||||
itemPage = if (itemPageCount == 0) 0 else (itemPage - 1).fmod(itemPageCount)
|
||||
scrollUpButton.highlighted = false
|
||||
rebuild()
|
||||
}
|
||||
scrollDownButton.clickOnceListener = { _, _, _ ->
|
||||
itemPage = (itemPage + 1).fmod(itemPageCount)
|
||||
itemPage = if (itemPageCount == 0) 0 else (itemPage + 1).fmod(itemPageCount)
|
||||
scrollDownButton.highlighted = false
|
||||
rebuild()
|
||||
}
|
||||
|
||||
161
src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt
Normal file
161
src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt
Normal file
@@ -0,0 +1,161 @@
|
||||
package net.torvald.terrarum.modulebasegame.ui
|
||||
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.Input
|
||||
import com.badlogic.gdx.graphics.Camera
|
||||
import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import net.torvald.terrarum.Second
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.ui.UICanvas
|
||||
import net.torvald.terrarum.ui.UIItemTextButton
|
||||
import net.torvald.terrarum.ui.UIItemTextButtonList
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2018-08-29.
|
||||
*/
|
||||
class UIRemoCon(treeRepresentation: QNDTreeNode<String>) : UICanvas() {
|
||||
|
||||
override var openCloseTime = 0f
|
||||
|
||||
private var remoConTray: UIRemoConElement // this remocon is dynamically generated
|
||||
private var currentRemoConContents = treeRepresentation
|
||||
|
||||
override var width = remoConWidth
|
||||
override var height: Int
|
||||
get() = remoConTray.height
|
||||
set(value) {}
|
||||
|
||||
init {
|
||||
remoConTray = generateNewRemoCon(currentRemoConContents)
|
||||
}
|
||||
|
||||
private var mouseActionAvailable = true
|
||||
|
||||
private fun generateNewRemoCon(node: QNDTreeNode<String>): UIRemoConElement {
|
||||
val dynamicStrArray = Array(node.children.size, { node.children[it].data ?: "(null)" })
|
||||
return UIRemoConElement(this, dynamicStrArray)
|
||||
}
|
||||
|
||||
override fun updateUI(delta: Float) {
|
||||
if (mouseActionAvailable) {
|
||||
remoConTray.update(delta)
|
||||
|
||||
mouseActionAvailable = false
|
||||
}
|
||||
|
||||
val selectedItem = remoConTray.selectedItem
|
||||
val selectedIndex = remoConTray.selectedIndex
|
||||
|
||||
selectedItem?.let {
|
||||
if (it.labelText == "MENU_LABEL_RETURN") {
|
||||
if (currentRemoConContents.parent != null) {
|
||||
remoConTray.consume()
|
||||
|
||||
currentRemoConContents = currentRemoConContents.parent!!
|
||||
remoConTray = generateNewRemoCon(currentRemoConContents)
|
||||
}
|
||||
else {
|
||||
throw NullPointerException("No parent node to return")
|
||||
}
|
||||
}
|
||||
else {
|
||||
// check if target exists
|
||||
//println("current node: ${currentRemoConContents.data}")
|
||||
//currentRemoConContents.children.forEach { println("- ${it.data}") }
|
||||
|
||||
if (currentRemoConContents.children.size > selectedIndex ?: 0x7FFFFFFF) {
|
||||
|
||||
// only go deeper if that node has child to navigate
|
||||
if (currentRemoConContents.children[selectedIndex!!].children.size != 0) {
|
||||
remoConTray.consume()
|
||||
currentRemoConContents = currentRemoConContents.children[selectedIndex!!]
|
||||
remoConTray = generateNewRemoCon(currentRemoConContents)
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw RuntimeException("Index: $selectedIndex, Size: ${currentRemoConContents.children.size}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
|
||||
mouseActionAvailable = true
|
||||
}
|
||||
}
|
||||
|
||||
override fun renderUI(batch: SpriteBatch, camera: Camera) {
|
||||
remoConTray.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() {
|
||||
|
||||
}
|
||||
|
||||
class UIRemoConElement(uiRemoCon: UIRemoCon, val labels: Array<String>) {
|
||||
|
||||
|
||||
private val menubar = UIItemTextButtonList(
|
||||
uiRemoCon,
|
||||
labels,
|
||||
0, menubarOffY,
|
||||
uiRemoCon.width, getRemoConHeight(labels),
|
||||
textAreaWidth = uiRemoCon.width,
|
||||
readFromLang = true,
|
||||
activeBackCol = Color(0),
|
||||
highlightBackCol = Color(0),
|
||||
backgroundCol = Color(0),
|
||||
inactiveCol = Color.WHITE,
|
||||
defaultSelection = null
|
||||
)
|
||||
|
||||
fun update(delta: Float) {
|
||||
menubar.update(delta)
|
||||
}
|
||||
|
||||
fun render(batch: SpriteBatch, camera: Camera) {
|
||||
menubar.render(batch, camera)
|
||||
}
|
||||
|
||||
// nullifies currently selected item
|
||||
fun consume() {
|
||||
menubar.selectedIndex = null
|
||||
}
|
||||
|
||||
/** null if none are selected */
|
||||
val selectedItem: UIItemTextButton?
|
||||
get() = menubar.selectedButton
|
||||
/** null if none are selected */
|
||||
val selectedIndex: Int?
|
||||
get() = menubar.selectedIndex
|
||||
|
||||
val height = getRemoConHeight(labels)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val remoConWidth = 280
|
||||
fun getRemoConHeight(menu: ArrayList<String>) = 36 * menu.size.plus(1)
|
||||
fun getRemoConHeight(menu: Array<String>) = 36 * menu.size.plus(1)
|
||||
val menubarOffY: Int; get() = Terrarum.HEIGHT / 2 - (Terrarum.fontGame.lineHeight * 1.5).toInt()
|
||||
}
|
||||
}
|
||||
106
src/net/torvald/terrarum/modulebasegame/ui/UITitleRemoConYaml.kt
Normal file
106
src/net/torvald/terrarum/modulebasegame/ui/UITitleRemoConYaml.kt
Normal file
@@ -0,0 +1,106 @@
|
||||
package net.torvald.terrarum.modulebasegame.ui
|
||||
|
||||
import java.util.*
|
||||
|
||||
|
||||
|
||||
object UITitleRemoConYaml {
|
||||
|
||||
// YAML indent with a space!
|
||||
|
||||
val menus = """
|
||||
- MENU_MODE_SINGLEPLAYER
|
||||
- MENU_LABEL_RETURN
|
||||
- MENU_MODE_MULTIPLAYER
|
||||
- MENU_LABEL_RETURN
|
||||
- MENU_OPTIONS
|
||||
- MENU_OPTIONS_GRAPHICS
|
||||
- MENU_OPTIONS_CONTROLS
|
||||
- MENU_OPTIONS_SOUND
|
||||
- MENU_LABEL_RETURN
|
||||
- MENU_MODULES
|
||||
- MENU_LABEL_RETURN
|
||||
- MENU_LABEL_LANGUAGE
|
||||
- MENU_LABEL_RETURN
|
||||
- MENU_LABEL_CREDITS
|
||||
- MENU_LABEL_CREDITS
|
||||
- MENU_CREDIT_GPL_DNT
|
||||
- MENU_LABEL_RETURN
|
||||
- MENU_LABEL_QUIT
|
||||
""".trimIndent()
|
||||
|
||||
val debugTools = """
|
||||
- Development Tools $
|
||||
- Building Maker
|
||||
""".trimIndent()
|
||||
|
||||
operator fun invoke() = parseYamlList(menus)
|
||||
|
||||
fun parseYamlList(yaml: String): QNDTreeNode<String> {
|
||||
var currentIndentLevel = -1
|
||||
val root = QNDTreeNode<String>()
|
||||
var currentNode = root
|
||||
val nodesStack = Stack<QNDTreeNode<String>>()
|
||||
|
||||
nodesStack.push(currentNode)
|
||||
|
||||
yaml.split('\n') .forEach {
|
||||
val indentLevel = it.countSpaces()
|
||||
val it = it.trimIndent()
|
||||
if (it.startsWith("- ")) {
|
||||
val nodeName = it.drop(2)
|
||||
|
||||
if (indentLevel == currentIndentLevel) {
|
||||
val sibling = QNDTreeNode(nodeName, currentNode.parent)
|
||||
currentNode.parent!!.children.add(sibling)
|
||||
currentNode = sibling
|
||||
}
|
||||
else if (indentLevel > currentIndentLevel) {
|
||||
val childNode = QNDTreeNode(nodeName, currentNode)
|
||||
currentNode.children.add(childNode)
|
||||
nodesStack.push(currentNode)
|
||||
currentNode = childNode
|
||||
currentIndentLevel = indentLevel
|
||||
}
|
||||
else {
|
||||
repeat(currentIndentLevel - indentLevel) { currentNode = nodesStack.pop() }
|
||||
currentIndentLevel = indentLevel
|
||||
val sibling = QNDTreeNode(nodeName, currentNode.parent)
|
||||
currentNode.parent!!.children.add(sibling)
|
||||
currentNode = sibling
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// test traverse resulting tree
|
||||
fun preorder(node: QNDTreeNode<String>, depth: Int = 1) {
|
||||
//if (node == null) return
|
||||
|
||||
repeat(depth) { print("-") }
|
||||
println("${node.data} -> ${node.parent}")
|
||||
|
||||
node.children.forEach { preorder(it, depth + 1) }
|
||||
}
|
||||
|
||||
preorder(root)
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
private fun String.countSpaces(): Int {
|
||||
var c = 0
|
||||
while (c <= this.length) {
|
||||
if (this[c] == ' ')
|
||||
c++
|
||||
else
|
||||
break
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
}
|
||||
|
||||
class QNDTreeNode<T>(var data: T? = null, var parent: QNDTreeNode<T>? = null) {
|
||||
var children = ArrayList<QNDTreeNode<T>>()
|
||||
}
|
||||
Reference in New Issue
Block a user