mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-11 02:54:04 +09:00
WIP new UI elem
This commit is contained in:
@@ -5,18 +5,13 @@ 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.random.HQRNG
|
||||
import net.torvald.terrarum.AppLoader.printdbg
|
||||
import net.torvald.terrarum.AppLoader.printdbgerr
|
||||
import net.torvald.terrarum.LoadScreen
|
||||
import net.torvald.terrarum.Second
|
||||
import net.torvald.terrarum.QNDTreeNode
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.modulebasegame.Ingame
|
||||
import net.torvald.terrarum.modulebasegame.gameactors.PlayerBuilderSigrid
|
||||
import net.torvald.terrarum.Yaml
|
||||
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
|
||||
|
||||
/**
|
||||
@@ -39,7 +34,7 @@ open class UIRemoCon(treeRepresentation: QNDTreeNode<String>) : UICanvas() {
|
||||
|
||||
private val screens = ArrayList<Pair<String, UICanvas>>()
|
||||
|
||||
private val yamlSep = Regex(" : ")
|
||||
private val yamlSep = Yaml.SEPARATOR
|
||||
|
||||
init {
|
||||
remoConTray = generateNewRemoCon(currentRemoConContents)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.torvald.terrarum.modulebasegame.ui
|
||||
|
||||
import net.torvald.terrarum.AppLoader
|
||||
import net.torvald.terrarum.QNDTreeNode
|
||||
import net.torvald.terrarum.Yaml
|
||||
import java.util.*
|
||||
|
||||
|
||||
@@ -43,91 +45,8 @@ object UITitleRemoConYaml {
|
||||
""".trimIndent()
|
||||
|
||||
operator fun invoke() = if (AppLoader.IS_DEVELOPMENT_BUILD)
|
||||
parseYamlList(menus + "\n" + debugTools)
|
||||
Yaml(menus + "\n" + debugTools).parse()
|
||||
else
|
||||
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
|
||||
/*root.traversePreorder { node, depth ->
|
||||
repeat(depth + 1) { print("-") }
|
||||
println("${node.data} -> ${node.parent}")
|
||||
}*/
|
||||
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
private fun String.countSpaces(): Int {
|
||||
var c = 0
|
||||
while (c <= this.length) {
|
||||
if (this[c] == ' ')
|
||||
c++
|
||||
else
|
||||
break
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
Yaml(menus).parse()
|
||||
}
|
||||
|
||||
class QNDTreeNode<T>(var data: T? = null, var parent: QNDTreeNode<T>? = null) {
|
||||
var children = ArrayList<QNDTreeNode<T>>()
|
||||
|
||||
|
||||
private fun traverse1(node: QNDTreeNode<T>, action: (QNDTreeNode<T>, Int) -> Unit, depth: Int = 0) {
|
||||
//if (node == null) return
|
||||
action(node, depth)
|
||||
node.children.forEach { traverse1(it, action, depth + 1) }
|
||||
}
|
||||
|
||||
private fun traverse2(node: QNDTreeNode<T>, action: (QNDTreeNode<T>, Int) -> Unit, depth: Int = 0) {
|
||||
//if (node == null) return
|
||||
node.children.forEach { traverse2(it, action, depth + 1) }
|
||||
action(node, depth)
|
||||
}
|
||||
|
||||
fun traversePreorder(action: (QNDTreeNode<T>, Int) -> Unit) {
|
||||
this.traverse1(this, action)
|
||||
}
|
||||
|
||||
fun traversePostorder(action: (QNDTreeNode<T>, Int) -> Unit) {
|
||||
this.traverse2(this, action)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user