limitedly successful attempt to create a title screen

This commit is contained in:
minjaesong
2017-07-20 00:36:41 +09:00
parent 3a1379e376
commit 15dbd16766
34 changed files with 1168 additions and 410 deletions

View File

@@ -8,6 +8,8 @@ import net.torvald.terrarum.gameactors.roundInt
/**
* UIItems must be added manually at the init!
*
* Created by minjaesong on 15-12-31.
*/
abstract class UICanvas {

View File

@@ -1,16 +1,19 @@
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.Ingame
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gamecontroller.KeyToggler
import net.torvald.terrarum.round
/**
* UIHandler is a handler for UICanvas. It opens/closes the attached UI, moves the "window" (or "canvas")
* to the coordinate of displayed cartesian coords, and update and render the UI.
* It also process game inputs and send control events to the UI so that the UI can handle them.
*
* Newly created UI is invisible by default.
* New UIs are NORMALLY HIDDEN; set it visible as you need!
*
* Created by minjaesong on 15-12-31.
*/
@@ -129,7 +132,7 @@ class UIHandler(var UI: UICanvas,
}
}
fun render(batch: SpriteBatch) {
fun render(batch: SpriteBatch, camera: Camera) {
if (isVisible || alwaysVisible) {
// camera SHOULD BE CENTERED to HALFX and HALFY (see StateInGame) //
@@ -141,8 +144,9 @@ class UIHandler(var UI: UICanvas,
if (!customPositioning)
Terrarum.ingame?.setCameraPosition(posX.toFloat(), posY.toFloat())
if (!customPositioning) {
setCameraPosition(batch, camera, posX.toFloat(), posY.toFloat())
}
batch.color = Color.WHITE
UI.render(batch)
@@ -289,4 +293,8 @@ class UIHandler(var UI: UICanvas,
fun dispose() {
UI.dispose()
}
fun setCameraPosition(batch: SpriteBatch, camera: Camera, newX: Float, newY: Float) {
Ingame.setCameraPosition(batch, camera, newX, newY)
}
}

View File

@@ -159,7 +159,7 @@ class UIInventory(
else
"${0xe069.toChar()} ${Lang["GAME_ACTION_CLOSE"]}"
private var oldCatSelect = -1
private var oldCatSelect: Int? = null
private var encumbrancePerc = 0f
private var isEncumbered = false
@@ -294,70 +294,72 @@ class UIInventory(
fun shutUpAndRebuild() {
val filter = catButtonsToCatIdent[catButtons.selectedButton.labelText]
if (catButtons.selectedButton != null) {
val filter = catButtonsToCatIdent[catButtons.selectedButton!!.labelText]
// encumbrance
encumbrancePerc = inventory!!.capacity.toFloat() / inventory!!.maxCapacity
isEncumbered = inventory!!.isEncumbered
// encumbrance
encumbrancePerc = inventory!!.capacity.toFloat() / inventory!!.maxCapacity
isEncumbered = inventory!!.isEncumbered
inventorySortList = ArrayList<InventoryPair>()
inventorySortList = ArrayList<InventoryPair>()
// filter items
inventory?.forEach {
if (it.item.inventoryCategory == filter || filter == "__all__")
inventorySortList.add(it)
}
// filter items
inventory?.forEach {
if (it.item.inventoryCategory == filter || filter == "__all__")
inventorySortList.add(it)
}
rebuildList = false
rebuildList = false
// sort if needed
// test sort by name
inventorySortList.sortBy { it.item.name }
// sort if needed
// test sort by name
inventorySortList.sortBy { it.item.name }
// map sortList to item list
for (k in 0..items.size - 1) {
// we have an item
try {
val sortListItem = inventorySortList[k + itemPage * items.size]
items[k].item = sortListItem.item
items[k].amount = sortListItem.amount
items[k].itemImage = ItemCodex.getItemImage(sortListItem.item)
// map sortList to item list
for (k in 0..items.size - 1) {
// we have an item
try {
val sortListItem = inventorySortList[k + itemPage * items.size]
items[k].item = sortListItem.item
items[k].amount = sortListItem.amount
items[k].itemImage = ItemCodex.getItemImage(sortListItem.item)
// set quickslot number
for (qs in 1..UIQuickBar.SLOT_COUNT) {
if (sortListItem.item == actor?.inventory?.getQuickBar(qs - 1)?.item) {
items[k].quickslot = qs % 10 // 10 -> 0, 1..9 -> 1..9
break
}
else
items[k].quickslot = null
}
// set equippedslot number
for (eq in 0..actor!!.inventory.itemEquipped.size - 1) {
if (eq < actor!!.inventory.itemEquipped.size) {
if (actor!!.inventory.itemEquipped[eq] == items[k].item) {
items[k].equippedSlot = eq
// set quickslot number
for (qs in 1..UIQuickBar.SLOT_COUNT) {
if (sortListItem.item == actor?.inventory?.getQuickBar(qs - 1)?.item) {
items[k].quickslot = qs % 10 // 10 -> 0, 1..9 -> 1..9
break
}
else
items[k].equippedSlot = null
items[k].quickslot = null
}
// set equippedslot number
for (eq in 0..actor!!.inventory.itemEquipped.size - 1) {
if (eq < actor!!.inventory.itemEquipped.size) {
if (actor!!.inventory.itemEquipped[eq] == items[k].item) {
items[k].equippedSlot = eq
break
}
else
items[k].equippedSlot = null
}
}
}
// we do not have an item, empty the slot
catch (e: IndexOutOfBoundsException) {
items[k].item = null
items[k].amount = 0
items[k].itemImage = null
items[k].quickslot = null
}
}
// we do not have an item, empty the slot
catch (e: IndexOutOfBoundsException) {
items[k].item = null
items[k].amount = 0
items[k].itemImage = null
items[k].quickslot = null
}
itemPageCount = maxOf(1, 1 + (inventorySortList.size.minus(1) / items.size))
}
itemPageCount = maxOf(1, 1 + (inventorySortList.size.minus(1) / items.size))
}

View File

@@ -6,6 +6,32 @@ import net.torvald.terrarum.Terrarum
/**
* ## Attaching Input Listeners
*
* UIItem provides following listeners:
*
* - updateListener
* - keyDownListener
* - keyUpListener
* - mouseMovedListene
* - touchDraggedListe
* - touchDownListener
* - touchUpListener
* - scrolledListener
* - clickOnceListener
*
* Each listeners are implemented using _functions_, instead of traditional listener _classes_.
* What you should do is just override one or more of these variables which has 'function' as their type.
* For example:
*
* ```
* <<some_name>>.clickOnceListener = { mouseX, mouseY, button ->
* println("Bo-ing!")
* }
* ```
*
* This listener will print out 'Bo-ing!' whenever it's clicked.
*
* Created by minjaesong on 15-12-31.
*/
abstract class UIItem(var parentUI: UICanvas) { // do not replace parentUI to UIHandler!
@@ -88,6 +114,8 @@ abstract class UIItem(var parentUI: UICanvas) { // do not replace parentUI to UI
return false
}
open fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
println("trsaneirsatneioarsteniotrsaneioarstineoarstneio")
var actionDone = false
if (touchDownListener != null) {

View File

@@ -16,11 +16,11 @@ import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
class UIItemTextButtonList(
parentUI: UICanvas,
labelsList: Array<String>,
override val width: Int,
override val height: Int,
override var width: Int,
override var height: Int,
val verticalGutter: Int = 0,
val readFromLang: Boolean = false,
val defaultSelection: Int = 0,
val defaultSelection: Int? = null, // negative: INVALID, positive: valid, null: no select
// icons
val textAreaWidth: Int,
@@ -36,7 +36,7 @@ class UIItemTextButtonList(
val highlightBackCol: Color = Color(0xb0b0b0_ff.toInt()),
val highlightBackBlendMode: String = BlendMode.MULTIPLY,
val inactiveCol: Color = Color(0xc0c0c0_ff.toInt()),
val backgroundCol: Color = Color(0),
val backgroundCol: Color = Color(0x242424_80),
val backgroundBlendMode: String = BlendMode.NORMAL,
val kinematic: Boolean = false
) : UIItem(parentUI) {
@@ -93,10 +93,10 @@ class UIItemTextButtonList(
override var posX = 0
override var posY = 0
var selectedIndex = defaultSelection
val selectedButton: UIItemTextButton
get() = buttons[selectedIndex]
private var highlightY = buttons[selectedIndex].posY.toDouble()
var selectedIndex: Int? = defaultSelection
val selectedButton: UIItemTextButton?
get() = if (selectedIndex != null) buttons[selectedIndex!!] else null
private var highlightY: Double? = if (selectedIndex != null) buttons[selectedIndex!!].posY.toDouble() else null
private val highlighterMoveDuration: Second = 0.1f
private var highlighterMoveTimer: Second = 0f
private var highlighterMoving = false
@@ -107,12 +107,15 @@ class UIItemTextButtonList(
override fun update(delta: Float) {
if (highlighterMoving) {
highlighterMoveTimer += delta
highlightY = UIUtils.moveQuick(
highlighterYStart,
highlighterYEnd,
highlighterMoveTimer.toDouble(),
highlighterMoveDuration.toDouble()
)
if (selectedIndex != null) {
highlightY = UIUtils.moveQuick(
highlighterYStart!!,
highlighterYEnd!!,
highlighterMoveTimer.toDouble(),
highlighterMoveDuration.toDouble()
)
}
if (highlighterMoveTimer > highlighterMoveDuration) {
highlighterMoveTimer = 0f
@@ -128,14 +131,14 @@ class UIItemTextButtonList(
if (btn.mousePushed && index != selectedIndex) {
if (kinematic) {
highlighterYStart = buttons[selectedIndex].posY.toDouble()
highlighterYStart = buttons[selectedIndex!!].posY.toDouble()
selectedIndex = index
highlighterMoving = true
highlighterYEnd = buttons[selectedIndex].posY.toDouble()
highlighterYEnd = buttons[selectedIndex!!].posY.toDouble()
}
else {
selectedIndex = index
highlightY = buttons[selectedIndex].posY.toDouble()
highlightY = buttons[selectedIndex!!].posY.toDouble()
}
}
btn.highlighted = (index == selectedIndex) // forcibly highlight if this.highlighted != null
@@ -150,7 +153,9 @@ class UIItemTextButtonList(
batch.color = highlightBackCol
BlendMode.resolve(highlightBackBlendMode)
batch.fillRect(posX.toFloat(), highlightY.toFloat(), width.toFloat(), UIItemTextButton.height.toFloat())
if (highlightY != null) {
batch.fillRect(posX.toFloat(), highlightY!!.toFloat(), width.toFloat(), UIItemTextButton.height.toFloat())
}
buttons.forEach { it.render(batch) }

View File

@@ -0,0 +1,107 @@
package net.torvald.terrarum.ui
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.Terrarum
class UIStartMenu : UICanvas() {
companion object {
/** Contains STRING_IDs */
val menuLabels = arrayOf(
"MENU_MODE_SINGLEPLAYER",
"MENU_OPTIONS",
"MENU_MODULES",
"MENU_LABEL_EXIT"
)
val menubarOffY = Terrarum.HEIGHT - 180 - 40 * menuLabels.size.plus(1)
}
override var width: Int = 240
override var height: Int = 40 * menuLabels.size.plus(1)
override var handler: UIHandler? = null
override var openCloseTime = 0f
private val menubar = UIItemTextButtonList(
this,
menuLabels,
240, this.height,
textAreaWidth = 240,
readFromLang = true,
activeBackCol = Color(0),
highlightBackCol = Color(0),
defaultSelection = null
)
init {
uiItems.add(menubar)
// attach listeners
menubar.buttons[3].clickOnceListener = { _, _, _ -> System.exit(0) }
}
override fun update(delta: Float) {
menubar.update(delta)
}
override fun render(batch: SpriteBatch) {
menubar.render(batch)
}
override fun doOpening(delta: Float) {
}
override fun doClosing(delta: Float) {
}
override fun endOpening(delta: Float) {
}
override fun endClosing(delta: Float) {
}
override fun dispose() {
}
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)
}
}