NSMenu WIP, but also caught a source of the old bug

This commit is contained in:
minjaesong
2018-12-09 04:40:46 +09:00
parent 1c1ae37f41
commit 4c2b73197d
5 changed files with 254 additions and 25 deletions

View File

@@ -0,0 +1,130 @@
package net.torvald.terrarum.tests
import com.badlogic.gdx.Game
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Screen
import com.badlogic.gdx.ScreenAdapter
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.ShaderProgram
import net.torvald.terrarum.*
import net.torvald.terrarum.ui.UINSMenu
/**
* Created by minjaesong on 2018-12-09.
*/
class UITestPad1 : ScreenAdapter() {
val treeStr = """
- 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
"""
lateinit var nsMenu: UINSMenu
lateinit var batch: SpriteBatch
lateinit var camera: OrthographicCamera
override fun show() {
nsMenu = UINSMenu(
"Menu",
160,
Yaml(treeStr)
)
batch = SpriteBatch()
camera = OrthographicCamera(AppLoader.appConfig.width.toFloat(), AppLoader.appConfig.height.toFloat())
camera.setToOrtho(true, AppLoader.appConfig.width.toFloat(), AppLoader.appConfig.height.toFloat())
camera.update()
Gdx.gl20.glViewport(0, 0, AppLoader.appConfig.width, AppLoader.appConfig.height)
resize(AppLoader.appConfig.width, AppLoader.appConfig.height)
nsMenu.setPosition(10, 10)
nsMenu.setAsAlwaysVisible()
}
val bgCol = Color(.62f,.79f,1f,1f)
override fun render(delta: Float) {
batch.inUse {
batch.color = bgCol
batch.fillRect(0f, 0f, 2048f, 2048f)
nsMenu.render(batch, camera)
}
//nsMenu.setPosition(20, 20) // FIXME the prolonged bug, "the entire screen is shifted!" is caused by these kind of operations
}
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(AppLoader(appConfig, UITestPad1()), appConfig)
}

View File

@@ -1,20 +0,0 @@
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 treeRepresentation: Yaml
) : UIItem(parent) {
val tree = treeRepresentation.parse()
override val height = -1
}*/

View File

@@ -61,17 +61,17 @@ open class UIItemTextButton(
if (highlighted) {
BlendMode.resolve(highlightBackBlendMode)
BlendMode.resolve(highlightBackBlendMode, batch)
batch.color = highlightBackCol
batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat())
}
else if (mouseUp) {
BlendMode.resolve(activeBackBlendMode)
BlendMode.resolve(activeBackBlendMode, batch)
batch.color = activeBackCol
batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat())
}
blendNormal()
blendNormal(batch)
batch.color = if (highlighted) highlightCol

View File

@@ -181,11 +181,11 @@ class UIItemTextButtonList(
override fun render(batch: SpriteBatch, camera: Camera) {
batch.color = backgroundCol
BlendMode.resolve(backgroundBlendMode)
BlendMode.resolve(backgroundBlendMode, batch)
batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat())
batch.color = highlightBackCol
BlendMode.resolve(highlightBackBlendMode)
BlendMode.resolve(highlightBackBlendMode, batch)
if (highlightY != null) {
batch.fillRect(posX.toFloat(), highlightY!!.toFloat(), width.toFloat(), UIItemTextButton.height.toFloat())
}

View File

@@ -0,0 +1,119 @@
package net.torvald.terrarum.ui
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.*
/**
* Nextstep-themed menu bar with mandatory title line
*
* Created by minjaesong on 2018-12-08.
*/
class UINSMenu(
var title: String = "",
val minimumWidth: Int,
treeRepresentation: Yaml,
val titleBackCol: Color = Color(0f,0f,0f,.77f),
val titleTextCol: Color = Color.WHITE,
val titleBlendMode: String = BlendMode.NORMAL
) : UICanvas() {
override var openCloseTime: Second = 0f
val LINE_HEIGHT = 24
val TEXT_OFFSETX = 3f
val TEXT_OFFSETY = (LINE_HEIGHT - Terrarum.fontGame.lineHeight) / 2f
val CHILD_ARROW = "${0x2023.toChar()}"
val tree = treeRepresentation.parse()
override var width = minOf(minimumWidth, tree.getLevelData(1).map { Terrarum.fontGame.getWidth(it ?: "") }.max() ?: 0)
override var height = LINE_HEIGHT * (tree.children.size + 1)
private val treeChildrenLabels = Array<String>(tree.children.size) {
tree.children[it].toString() + if (tree.children[it].children.isNotEmpty()) " $CHILD_ARROW" else ""
}
private val theRealList = UIItemTextButtonList(
this,
treeChildrenLabels,
posX + TEXT_OFFSETX.toInt(), posY + LINE_HEIGHT,
width - (2 * TEXT_OFFSETX.toInt()), height - LINE_HEIGHT,
TEXT_OFFSETY.toInt(),
textAreaWidth = width - (2 * TEXT_OFFSETX.toInt()),
alignment = UIItemTextButton.Companion.Alignment.LEFT
)
val selectedIndex: Int?
get() = theRealList.selectedIndex
override fun updateUI(delta: Float) {
theRealList.update(delta)
}
override fun renderUI(batch: SpriteBatch, camera: Camera) {
// draw title bar
batch.color = titleBackCol
BlendMode.resolve(titleBlendMode, batch)
batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), LINE_HEIGHT.toFloat())
batch.color = titleTextCol
blendNormal(batch)
Terrarum.fontGame.draw(batch, title, posX + TEXT_OFFSETX, TEXT_OFFSETY)
// draw the list
theRealList.render(batch, camera)
}
override fun dispose() {
theRealList.dispose()
}
override fun doOpening(delta: Float) {
}
override fun doClosing(delta: Float) {
}
override fun endOpening(delta: Float) {
}
override fun endClosing(delta: Float) {
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
return super.mouseMoved(screenX, screenY)
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
return super.touchDragged(screenX, screenY, pointer)
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return super.touchDown(screenX, screenY, pointer, button)
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return super.touchUp(screenX, screenY, pointer, button)
}
override fun scrolled(amount: Int): Boolean {
return super.scrolled(amount)
}
override fun keyDown(keycode: Int): Boolean {
return super.keyDown(keycode)
}
override fun keyUp(keycode: Int): Boolean {
return super.keyUp(keycode)
}
override fun keyTyped(character: Char): Boolean {
return super.keyTyped(character)
}
override fun resize(width: Int, height: Int) {
}
}