WIP new UI elem

This commit is contained in:
minjaesong
2018-12-08 23:10:48 +09:00
committed by Minjae Song
parent 4b04bf3781
commit 6f0a923df7
9 changed files with 294 additions and 94 deletions

View File

@@ -55,4 +55,40 @@ class GdxColorMap {
fun getRaw(x: Int, y: Int): RGBA8888 = data[y * width + x] fun getRaw(x: Int, y: Int): RGBA8888 = data[y * width + x]
fun getRaw(x: Int): RGBA8888 = if (is2D) throw OperationNotSupportedException("This is 2D color map") else data[x] fun getRaw(x: Int): RGBA8888 = if (is2D) throw OperationNotSupportedException("This is 2D color map") else data[x]
override fun toString(): String {
val sb = StringBuilder()
sb.append("ColorMap ${width}x$height:\n")
var yi = 0
var xi = 0
for (y in ((0 until height).take(2) + (0 until height).toList().takeLast(2)).distinct()) {
if (y - yi > 1) {
sb.append(when (width) {
in 1..4 -> ".......... ".repeat(width) + '\n'
else -> ".......... .......... ... .......... .......... \n"
}
)
}
for (x in ((0 until width).take(2) + (0 until width).toList().takeLast(2)).distinct()) {
if (x - xi > 1) {
sb.append("... ")
}
sb.append("0x")
sb.append(getRaw(x, y).toLong().and(0xFFFFFFFF).toString(16).toUpperCase().padStart(8, '0'))
sb.append(' ')
xi = x
}
sb.append('\n')
yi = y
}
return sb.toString()
}
} }

View File

@@ -0,0 +1,28 @@
package net.torvald.terrarum
import java.util.ArrayList
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)
}
}

View File

@@ -0,0 +1,118 @@
package net.torvald.terrarum
import java.util.*
/**
* Simplified version of YAML, only for the representation of a text tree.
*
* Example code:
* ```
* - File
* - New : Ctrl-N
* - Open : Ctrl-O
* - Open Recent
* - yaml_example.yaml
* - Yaml.kt
* - Close : Ctrl-W
* - Settings
* - Line Separators
* - CRLF
* - CR
* - LF
* - Edit
* - Undo : Ctrl-Z
* - Redo : Shift-Ctrl-Z
* - Cut : Ctrl-X
* - Copy : Ctrl-C
* - Paste : Ctrl-V
* - Find
* - Find : Ctrl-F
* - Replace : Shift-Ctrl-F
* - Convert Indents
* - To Spaces
* - Set Project Indentation
* - To Tabs
* - Refactor
* - Refactor This
* - Rename : Shift-Ctrl-R
* - Extract
* - Variable
* - Property
* - Function
* ```
*
* - All lines are indented with one space
* - All entries are preceded by '- ' (dash and a space)
* - All propery are separated by ' : ' (space colon space)
*
* Any deviation to the above rule will cause a parse failure, because it's simple and dumb as that.
*
* Created by minjaesong on 2018-12-08.
*/
inline class Yaml(val text: String) {
companion object {
val SEPARATOR = Regex(" : ")
}
fun parse(): QNDTreeNode<String> {
var currentIndentLevel = -1
val root = QNDTreeNode<String>()
var currentNode = root
val nodesStack = Stack<QNDTreeNode<String>>()
nodesStack.push(currentNode)
text.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
}
}

View File

@@ -5,18 +5,13 @@ import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.Camera import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch 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.AppLoader.printdbgerr
import net.torvald.terrarum.LoadScreen import net.torvald.terrarum.QNDTreeNode
import net.torvald.terrarum.Second
import net.torvald.terrarum.Terrarum import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.modulebasegame.Ingame import net.torvald.terrarum.Yaml
import net.torvald.terrarum.modulebasegame.gameactors.PlayerBuilderSigrid
import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.ui.UIItemTextButton import net.torvald.terrarum.ui.UIItemTextButton
import net.torvald.terrarum.ui.UIItemTextButtonList import net.torvald.terrarum.ui.UIItemTextButtonList
import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
/** /**
@@ -39,7 +34,7 @@ open class UIRemoCon(treeRepresentation: QNDTreeNode<String>) : UICanvas() {
private val screens = ArrayList<Pair<String, UICanvas>>() private val screens = ArrayList<Pair<String, UICanvas>>()
private val yamlSep = Regex(" : ") private val yamlSep = Yaml.SEPARATOR
init { init {
remoConTray = generateNewRemoCon(currentRemoConContents) remoConTray = generateNewRemoCon(currentRemoConContents)

View File

@@ -1,6 +1,8 @@
package net.torvald.terrarum.modulebasegame.ui package net.torvald.terrarum.modulebasegame.ui
import net.torvald.terrarum.AppLoader import net.torvald.terrarum.AppLoader
import net.torvald.terrarum.QNDTreeNode
import net.torvald.terrarum.Yaml
import java.util.* import java.util.*
@@ -43,91 +45,8 @@ object UITitleRemoConYaml {
""".trimIndent() """.trimIndent()
operator fun invoke() = if (AppLoader.IS_DEVELOPMENT_BUILD) operator fun invoke() = if (AppLoader.IS_DEVELOPMENT_BUILD)
parseYamlList(menus + "\n" + debugTools) Yaml(menus + "\n" + debugTools).parse()
else else
parseYamlList(menus) Yaml(menus).parse()
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
}
} }
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)
}
}

View File

@@ -0,0 +1,68 @@
package net.torvald.terrarum.tests
import com.badlogic.gdx.Game
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Screen
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
import com.badlogic.gdx.graphics.glutils.ShaderProgram
import net.torvald.terrarum.GdxColorMap
/**
* Created by minjaesong on 2018-12-08.
*/
class ColorMapTest : Game() {
override fun create() {
}
override fun getScreen(): Screen {
return super.getScreen()
}
override fun setScreen(screen: Screen?) {
super.setScreen(screen)
}
override fun render() {
val colormap = GdxColorMap(Gdx.files.internal("assets/testimage_resized.png"))
println(colormap)
System.exit(0)
}
override fun pause() {
super.pause()
}
override fun resume() {
super.resume()
}
override fun resize(width: Int, height: Int) {
super.resize(width, height)
}
override fun dispose() {
super.dispose()
}
}
fun main(args: Array<String>) {
ShaderProgram.pedantic = false
val appConfig = LwjglApplicationConfiguration()
appConfig.vSyncEnabled = false
appConfig.resizable = false//true;
//appConfig.width = 1072; // IMAX ratio
//appConfig.height = 742; // IMAX ratio
appConfig.width = 1110 // photographic ratio (1.5:1)
appConfig.height = 740 // photographic ratio (1.5:1)
appConfig.backgroundFPS = 9999
appConfig.foregroundFPS = 9999
appConfig.forceExit = false
LwjglApplication(ColorMapTest(), appConfig)
}

View File

@@ -1,8 +1,10 @@
package net.torvald.terrarum.ui package net.torvald.terrarum.ui
import com.badlogic.gdx.graphics.Camera import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.BlendMode import net.torvald.terrarum.BlendMode
import net.torvald.terrarum.GdxColorMap
import java.awt.Color import java.awt.Color
/** /**
@@ -16,6 +18,9 @@ class UIItemIntSlider(
val minValue: Int, val minValue: Int,
val maxValue: Int, val maxValue: Int,
val step: Int, val step: Int,
// BASIC OPTIONS //
/** Show prev- and next values (if any) */ /** Show prev- and next values (if any) */
var showNotches: Boolean, var showNotches: Boolean,
var showMinMaxValues: Boolean, var showMinMaxValues: Boolean,
@@ -27,8 +32,12 @@ class UIItemIntSlider(
val notchCol: Color, val notchCol: Color,
val barCol: Color, val barCol: Color,
val barAndNotchBlend: BlendMode val barAndNotchBlend: BlendMode,
// EXTENDED OPTIONS //
val sliderUseColourMap: GdxColorMap? = null,
val sliderUseTexture: Texture? = null
) : UIItem(parent) { ) : UIItem(parent) {
constructor( constructor(
@@ -61,6 +70,11 @@ class UIItemIntSlider(
var value = initValue var value = initValue
init {
if (sliderUseColourMap != null && sliderUseTexture != null) {
throw IllegalArgumentException("Can't use colour map and texture at the same time -- ColorMap: $sliderUseColourMap, Texture: $sliderUseTexture")
}
}
// TODO unimplemented // TODO unimplemented

View File

@@ -0,0 +1,19 @@
package net.torvald.terrarum.ui
import net.torvald.terrarum.Yaml
/**
* Created by minjaesong on 2018-12-08.
*/
/*class UIItemNSMenus(
parent: UICanvas,
var title: String? = null,
override var posX: Int,
override var posY: Int,
override var width: Int,
val tree: Yaml
) : UIItem(parent) {
override val height: Int
get() = TODO("not implemented")
}*/

Binary file not shown.