UI Remote Controller reworked (now 90% less stupidity)

This commit is contained in:
minjaesong
2018-08-30 17:24:53 +09:00
parent e7a1a8ca85
commit 6c4c0214a1
25 changed files with 348 additions and 27 deletions

View File

@@ -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()
}

View File

@@ -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()
}

View 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()
}
}

View 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>>()
}