From 6f0a923df7547d4cd92b9c123abccbefc8d7684a Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 8 Dec 2018 23:10:48 +0900 Subject: [PATCH] WIP new UI elem --- src/net/torvald/terrarum/GdxColorMap.kt | 36 ++++++ src/net/torvald/terrarum/QNDTreeNode.kt | 28 +++++ src/net/torvald/terrarum/Yaml.kt | 118 ++++++++++++++++++ .../terrarum/modulebasegame/ui/UIRemoCon.kt | 11 +- .../modulebasegame/ui/UITitleRemoConYaml.kt | 89 +------------ .../torvald/terrarum/tests/ColorMapTest.kt | 68 ++++++++++ .../torvald/terrarum/ui/UIItemIntSlider.kt | 16 ++- src/net/torvald/terrarum/ui/UIItemNSMenus.kt | 19 +++ .../graphics/colourmap/pal64_master_ryb.tga | 3 + 9 files changed, 294 insertions(+), 94 deletions(-) create mode 100644 src/net/torvald/terrarum/QNDTreeNode.kt create mode 100644 src/net/torvald/terrarum/Yaml.kt create mode 100644 src/net/torvald/terrarum/tests/ColorMapTest.kt create mode 100644 src/net/torvald/terrarum/ui/UIItemNSMenus.kt create mode 100644 work_files/graphics/colourmap/pal64_master_ryb.tga diff --git a/src/net/torvald/terrarum/GdxColorMap.kt b/src/net/torvald/terrarum/GdxColorMap.kt index 1decaad1a..f6a75f718 100644 --- a/src/net/torvald/terrarum/GdxColorMap.kt +++ b/src/net/torvald/terrarum/GdxColorMap.kt @@ -55,4 +55,40 @@ class GdxColorMap { 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] + 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() + } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/QNDTreeNode.kt b/src/net/torvald/terrarum/QNDTreeNode.kt new file mode 100644 index 000000000..ea81c5b3b --- /dev/null +++ b/src/net/torvald/terrarum/QNDTreeNode.kt @@ -0,0 +1,28 @@ +package net.torvald.terrarum + +import java.util.ArrayList + +class QNDTreeNode(var data: T? = null, var parent: QNDTreeNode? = null) { + var children = ArrayList>() + + + private fun traverse1(node: QNDTreeNode, action: (QNDTreeNode, 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, action: (QNDTreeNode, Int) -> Unit, depth: Int = 0) { + //if (node == null) return + node.children.forEach { traverse2(it, action, depth + 1) } + action(node, depth) + } + + fun traversePreorder(action: (QNDTreeNode, Int) -> Unit) { + this.traverse1(this, action) + } + + fun traversePostorder(action: (QNDTreeNode, Int) -> Unit) { + this.traverse2(this, action) + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/Yaml.kt b/src/net/torvald/terrarum/Yaml.kt new file mode 100644 index 000000000..fb41686eb --- /dev/null +++ b/src/net/torvald/terrarum/Yaml.kt @@ -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 { + var currentIndentLevel = -1 + val root = QNDTreeNode() + var currentNode = root + val nodesStack = Stack>() + + 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 + } + +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt index 9613363a5..9f67c880f 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt @@ -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) : UICanvas() { private val screens = ArrayList>() - private val yamlSep = Regex(" : ") + private val yamlSep = Yaml.SEPARATOR init { remoConTray = generateNewRemoCon(currentRemoConContents) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UITitleRemoConYaml.kt b/src/net/torvald/terrarum/modulebasegame/ui/UITitleRemoConYaml.kt index 6a0356811..e7eae2a2d 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UITitleRemoConYaml.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UITitleRemoConYaml.kt @@ -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 { - var currentIndentLevel = -1 - val root = QNDTreeNode() - var currentNode = root - val nodesStack = Stack>() - - 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(var data: T? = null, var parent: QNDTreeNode? = null) { - var children = ArrayList>() - - - private fun traverse1(node: QNDTreeNode, action: (QNDTreeNode, 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, action: (QNDTreeNode, Int) -> Unit, depth: Int = 0) { - //if (node == null) return - node.children.forEach { traverse2(it, action, depth + 1) } - action(node, depth) - } - - fun traversePreorder(action: (QNDTreeNode, Int) -> Unit) { - this.traverse1(this, action) - } - - fun traversePostorder(action: (QNDTreeNode, Int) -> Unit) { - this.traverse2(this, action) - } -} \ No newline at end of file diff --git a/src/net/torvald/terrarum/tests/ColorMapTest.kt b/src/net/torvald/terrarum/tests/ColorMapTest.kt new file mode 100644 index 000000000..c4bbc7825 --- /dev/null +++ b/src/net/torvald/terrarum/tests/ColorMapTest.kt @@ -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) { + 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) +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/ui/UIItemIntSlider.kt b/src/net/torvald/terrarum/ui/UIItemIntSlider.kt index 993eee759..0457fe6ea 100644 --- a/src/net/torvald/terrarum/ui/UIItemIntSlider.kt +++ b/src/net/torvald/terrarum/ui/UIItemIntSlider.kt @@ -1,8 +1,10 @@ package net.torvald.terrarum.ui import com.badlogic.gdx.graphics.Camera +import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.BlendMode +import net.torvald.terrarum.GdxColorMap import java.awt.Color /** @@ -16,6 +18,9 @@ class UIItemIntSlider( val minValue: Int, val maxValue: Int, val step: Int, + + // BASIC OPTIONS // + /** Show prev- and next values (if any) */ var showNotches: Boolean, var showMinMaxValues: Boolean, @@ -27,8 +32,12 @@ class UIItemIntSlider( val notchCol: Color, val barCol: Color, - val barAndNotchBlend: BlendMode + val barAndNotchBlend: BlendMode, + // EXTENDED OPTIONS // + + val sliderUseColourMap: GdxColorMap? = null, + val sliderUseTexture: Texture? = null ) : UIItem(parent) { constructor( @@ -61,6 +70,11 @@ class UIItemIntSlider( 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 diff --git a/src/net/torvald/terrarum/ui/UIItemNSMenus.kt b/src/net/torvald/terrarum/ui/UIItemNSMenus.kt new file mode 100644 index 000000000..d49ab6d09 --- /dev/null +++ b/src/net/torvald/terrarum/ui/UIItemNSMenus.kt @@ -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") +}*/ \ No newline at end of file diff --git a/work_files/graphics/colourmap/pal64_master_ryb.tga b/work_files/graphics/colourmap/pal64_master_ryb.tga new file mode 100644 index 000000000..9246a30eb --- /dev/null +++ b/work_files/graphics/colourmap/pal64_master_ryb.tga @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9e72560184087f091a407dd951eb98358c1427b7071f2fcc4e4d13ca5828bd56 +size 876