implementation of Pie Menu

Former-commit-id: 0ff65782d25f0bf73435d297cf70d93e80882090
Former-commit-id: c31e1675890bb098addd2467f1137cb20038ec7d
This commit is contained in:
Song Minjae
2016-07-20 21:40:26 +09:00
parent 222bf0811c
commit 00dd5e99bb
30 changed files with 513 additions and 192 deletions

View File

@@ -25,6 +25,9 @@ class BasicDebugInfoWindow:UICanvas {
override var height: Int = Terrarum.HEIGHT
override var openCloseTime: Int = 0
override var openCloseTimer: Int = 0
override var handler: UIHandler? = null
private var prevPlayerX = 0.0
private var prevPlayerY = 0.0

View File

@@ -34,11 +34,13 @@ class ConsoleWindow : UICanvas, UITypable {
override var height: Int = LINE_HEIGHT * (MESSAGES_DISPLAY_COUNT + 1)
override var openCloseTime: Int = 0
override var openCloseTimer: Int = 0
private var drawOffX: Float = 0f
private var drawOffY: Float = -height.toFloat()
private var openingTimeCounter = 0
override var handler: UIHandler? = null
private var historyIndex = -1
@@ -170,7 +172,10 @@ class ConsoleWindow : UICanvas, UITypable {
commandHistory = HistoryArray<String>(COMMAND_HISTORY_MAX)
commandInputPool = StringBuilder()
if (Terrarum.ingame.auth.b()) sendMessage(Lang["DEV_MESSAGE_CONSOLE_CODEX"])
if (Terrarum.ingame.auth.b()) {
sendMessage("${Terrarum.NAME} ${Terrarum.VERSION_STRING}")
sendMessage(Lang["DEV_MESSAGE_CONSOLE_CODEX"])
}
}
override fun doOpening(gc: GameContainer, delta: Int) {

View File

@@ -0,0 +1,52 @@
package net.torvald.terrarum.ui
import net.torvald.terrarum.setBlendNormal
import org.newdawn.slick.Color
import org.newdawn.slick.Image
import org.newdawn.slick.SpriteSheet
import org.newdawn.slick.SpriteSheetFont
/**
* Make item slot image with number on bottom-right
*
* Created by minjaesong on 16-07-20.
*/
object ItemSlotImageBuilder {
const val COLOR_BLACK = 1
const val COLOR_WHITE = 2
private val colourBlack = Color(0x404040)
private val colourWhite = Color(0xC0C0C0)
private val numberFont = SpriteSheetFont(
SpriteSheet("./res/graphics/fonts/numeric_small.png", 5, 8),
'0'
)
private val slotImage = Image("./res/graphics/gui/quickbar/item_slot.png")
private val canvas = Image(slotImage.width, slotImage.height)
fun produce(color: Int, number: Int = -1): Image {
if (color == COLOR_BLACK)
canvas.graphics.drawImage(slotImage, 0f, 0f, colourBlack)
else if (color == COLOR_WHITE)
canvas.graphics.drawImage(slotImage, 0f, 0f, colourWhite)
if (number >= 0) {
canvas.graphics.font = numberFont
if (color == COLOR_BLACK)
canvas.graphics.color = colourWhite
else if (color == COLOR_WHITE)
canvas.graphics.color = colourBlack
canvas.graphics.drawString(number.mod(UIQuickBar.SLOT_COUNT).toString(),
slotImage.width - 6f,
slotImage.height - 10f
)
}
return canvas
}
}

View File

@@ -28,7 +28,9 @@ constructor(override var width: Int, isBlackVariant: Boolean) : UICanvas {
override var openCloseTime: Int = OPEN_CLOSE_TIME
internal var opacity = 0f
internal var openCloseCounter = 0
override var openCloseTimer = 0
override var handler: UIHandler? = null
private lateinit var uidrawCanvas: Image // render all the images and fonts here; will be faded
@@ -85,27 +87,27 @@ constructor(override var width: Int, isBlackVariant: Boolean) : UICanvas {
}
override fun doOpening(gc: GameContainer, delta: Int) {
openCloseCounter += delta
opacity = FastMath.interpolateLinear(openCloseCounter.toFloat() / openCloseTime.toFloat(),
openCloseTimer += delta
opacity = FastMath.interpolateLinear(openCloseTimer.toFloat() / openCloseTime.toFloat(),
0f, 1f
)
}
override fun doClosing(gc: GameContainer, delta: Int) {
openCloseCounter += delta
opacity = FastMath.interpolateLinear(openCloseCounter.toFloat() / openCloseTime.toFloat(),
openCloseTimer += delta
opacity = FastMath.interpolateLinear(openCloseTimer.toFloat() / openCloseTime.toFloat(),
1f, 0f
)
}
override fun endOpening(gc: GameContainer, delta: Int) {
opacity = 1f
openCloseCounter = 0
openCloseTimer = 0
}
override fun endClosing(gc: GameContainer, delta: Int) {
opacity = 0f
openCloseCounter = 0
openCloseTimer = 0
}
private fun drawSegments(g: Graphics) {

View File

@@ -15,7 +15,7 @@ constructor() : UICanvas {
override var width: Int = 0
override var height: Int = 0
internal var visibleTime: Int
internal var showupTimeConuter = 0
override var openCloseTimer = 0
internal var isShowing = false
internal var message: Array<String> = Array(MessageWindow.MESSAGES_DISPLAY, { i -> ""})
@@ -24,6 +24,8 @@ constructor() : UICanvas {
override var openCloseTime: Int = MessageWindow.OPEN_CLOSE_TIME
override var handler: UIHandler? = null
private val SHOWUP_MAX = 15000
init {
@@ -37,7 +39,7 @@ constructor() : UICanvas {
}
override fun update(gc: GameContainer, delta: Int) {
if (showupTimeConuter >= visibleTime && isShowing) {
if (openCloseTimer >= visibleTime && isShowing) {
// invoke closing mode
doClosing(gc, delta)
// check if msgUI is fully fade out
@@ -48,7 +50,7 @@ constructor() : UICanvas {
}
if (isShowing) {
showupTimeConuter += delta
openCloseTimer += delta
}
}
@@ -82,6 +84,6 @@ constructor() : UICanvas {
isShowing = true
this.message = message
msgUI.setMessage(this.message)
showupTimeConuter = 0
openCloseTimer = 0
}
}

View File

@@ -15,6 +15,9 @@ interface UICanvas {
* In milliseconds
*/
var openCloseTime: Int
var openCloseTimer: Int
var handler: UIHandler?
fun update(gc: GameContainer, delta: Int)

View File

@@ -35,10 +35,10 @@ constructor(val UI: UICanvas) {
private val UIGraphicInstance: Graphics
private val UIDrawnCanvas: Image
private var opening = false
private var closing = false
private var opened = false // fully opened
var visible: Boolean = false
var isOpening = false
var isClosing = false
var isOpened = false // fully opened
var isVisible: Boolean = false
get() = if (alwaysVisible) true
else field
set(value) {
@@ -48,6 +48,9 @@ constructor(val UI: UICanvas) {
field = value
}
var opacity = 1f
var scale = 1f
var openCloseCounter = 0
init {
@@ -56,18 +59,19 @@ constructor(val UI: UICanvas) {
UIDrawnCanvas = Image(
//FastMath.nearestPowerOfTwo(UI.width), FastMath.nearestPowerOfTwo(UI.height))
UI.width, UI.height)
UIDrawnCanvas.filter = Image.FILTER_LINEAR
UIGraphicInstance = UIDrawnCanvas.graphics
}
fun update(gc: GameContainer, delta: Int) {
if (visible || alwaysVisible) {
if (isVisible || alwaysVisible) {
UI.update(gc, delta)
}
if (opening) {
visible = true
if (isOpening) {
isVisible = true
openCloseCounter += delta
// println("UI ${UI.javaClass.simpleName} (open)")
@@ -79,12 +83,12 @@ constructor(val UI: UICanvas) {
}
else {
UI.endOpening(gc, delta)
opening = false
opened = true
isOpening = false
isOpened = true
openCloseCounter = 0
}
}
else if (closing) {
else if (isClosing) {
openCloseCounter += delta
// println("UI ${UI.javaClass.simpleName} (close)")
@@ -96,32 +100,37 @@ constructor(val UI: UICanvas) {
}
else {
UI.endClosing(gc, delta)
closing = false
opened = false
visible = false
isClosing = false
isOpened = false
isVisible = false
openCloseCounter = 0
}
}
}
fun render(gc: GameContainer, sbg: StateBasedGame, gameGraphicInstance: Graphics) {
if (visible || alwaysVisible) {
fun render(gc: GameContainer, sbg: StateBasedGame, ingameGraphics: Graphics) {
if (isVisible || alwaysVisible) {
UIGraphicInstance.clear()
UIGraphicInstance.font = Terrarum.gameFont
UIGraphicInstance.setAntiAlias(true)
UI.render(gc, UIGraphicInstance)
if (sbg.currentStateID == Terrarum.SCENE_ID_GAME) {
gameGraphicInstance.drawImage(UIDrawnCanvas,
posX + MapCamera.cameraX * Terrarum.ingame.screenZoom,
posY + MapCamera.cameraY * Terrarum.ingame.screenZoom
ingameGraphics.drawImage(UIDrawnCanvas.getScaledCopy(scale),
posX + MapCamera.cameraX * Terrarum.ingame.screenZoom - (UI.width / 2f * scale.minus(1)),
posY + MapCamera.cameraY * Terrarum.ingame.screenZoom - (UI.height / 2f * scale.minus(1)),
Color(1f, 1f, 1f, opacity)
)// compensate for screenZoom AND camera translation
// (see Game.render -> g.translate())
}
else {
gameGraphicInstance.drawImage(UIDrawnCanvas,
posX.toFloat(),
posY.toFloat()
ingameGraphics.drawImage(UIDrawnCanvas.getScaledCopy(scale),
posX.toFloat() - (UI.width / 2f * scale.minus(1)),
posY.toFloat() - (UI.height / 2f * scale.minus(1)),
Color(1f, 1f, 1f, opacity)
)
}
}
}
@@ -133,10 +142,10 @@ constructor(val UI: UICanvas) {
fun setAsAlwaysVisible() {
alwaysVisible = true
visible = true
opened = true
opening = false
closing = false
isVisible = true
isOpened = true
isOpening = false
isClosing = false
}
/**
@@ -146,8 +155,10 @@ constructor(val UI: UICanvas) {
if (alwaysVisible) {
throw RuntimeException("[UIHandler] Tried to 'open' constant UI")
}
opened = false
opening = true
if (!isOpened && !isVisible) {
isOpened = false
isOpening = true
}
}
/**
@@ -157,82 +168,82 @@ constructor(val UI: UICanvas) {
if (alwaysVisible) {
throw RuntimeException("[UIHandler] Tried to 'close' constant UI")
}
opened = false
closing = true
isOpened = false
isClosing = true
}
fun toggleOpening() {
if (alwaysVisible) {
throw RuntimeException("[UIHandler] Tried to 'toggle opening of' constant UI")
}
if (visible) {
if (!closing) {
if (isVisible) {
if (!isClosing) {
setAsClosing()
}
}
else {
if (!opening) {
if (!isOpening) {
setAsOpening()
}
}
}
fun processInput(input: Input) {
if (visible) {
if (isVisible) {
UI.processInput(input)
}
}
fun keyPressed(key: Int, c: Char) {
if (visible && UI is UITypable) {
if (isVisible && UI is UITypable) {
UI.keyPressed(key, c)
}
}
fun keyReleased(key: Int, c: Char) {
if (visible && UI is UITypable) {
if (isVisible && UI is UITypable) {
UI.keyReleased(key, c)
}
}
fun mouseMoved(oldx: Int, oldy: Int, newx: Int, newy: Int) {
if (visible && UI is UIClickable) {
if (isVisible && UI is UIClickable) {
UI.mouseMoved(oldx, oldy, newx, newy)
}
}
fun mouseDragged(oldx: Int, oldy: Int, newx: Int, newy: Int) {
if (visible && UI is UIClickable) {
if (isVisible && UI is UIClickable) {
UI.mouseDragged(oldx, oldy, newx, newy)
}
}
fun mousePressed(button: Int, x: Int, y: Int) {
if (visible && UI is UIClickable) {
if (isVisible && UI is UIClickable) {
UI.mousePressed(button, x, y)
}
}
fun mouseReleased(button: Int, x: Int, y: Int) {
if (visible && UI is UIClickable) {
if (isVisible && UI is UIClickable) {
UI.mouseReleased(button, x, y)
}
}
fun mouseWheelMoved(change: Int) {
if (visible && UI is UIClickable) {
if (isVisible && UI is UIClickable) {
UI.mouseWheelMoved(change)
}
}
fun controllerButtonPressed(controller: Int, button: Int) {
if (visible && UI is UIClickable) {
if (isVisible && UI is UIClickable) {
UI.controllerButtonPressed(controller, button)
}
}
fun controllerButtonReleased(controller: Int, button: Int) {
if (visible && UI is UIClickable) {
if (isVisible && UI is UIClickable) {
UI.controllerButtonReleased(controller, button)
}
}
@@ -243,6 +254,6 @@ constructor(val UI: UICanvas) {
if (alwaysVisible) {
return false
}
return visible && !opening
return isVisible && !isOpening
}
}

View File

@@ -0,0 +1,108 @@
package net.torvald.terrarum.ui
import com.jme3.math.FastMath
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.AVKey
import org.dyn4j.geometry.Vector2
import org.newdawn.slick.Color
import org.newdawn.slick.GameContainer
import org.newdawn.slick.Graphics
import org.newdawn.slick.Input
/**
* Created by minjaesong on 16-07-20.
*/
class UIPieMenu : UICanvas {
private val cellSize = 32
private val slotCount = UIQuickBar.SLOT_COUNT
private val roundRectRadius = 6
override var width: Int = cellSize * 7
override var height: Int = width
/**
* In milliseconds
*/
override var openCloseTime: Int = 160
override var openCloseTimer: Int = 0
override var handler: UIHandler? = null
private val smallenSize = 0.93f
var menuSelection: Int = -1
override fun update(gc: GameContainer, delta: Int) {
if (menuSelection >= 0)
Terrarum.ingame.player.actorValue[AVKey._PLAYER_QUICKBARSEL] =
menuSelection % quickbarSlots
}
override fun render(gc: GameContainer, g: Graphics) {
val centrePoint = Vector2(width / 2.0, height / 2.0)
// draw radial thingies
for (i in 0..slotCount - 1) {
// set position
val angle = Math.PI * 2.0 * (i.toDouble() / slotCount) + Math.PI // 180 deg monitor-wise
val slotCentrePoint = Vector2(0.0, cellSize * 3.0).setDirection(angle) + centrePoint
// draw cells
g.color = if (menuSelection == i) Color(0xC0C0C0) else Color(0x404040)
g.drawImage(ItemSlotImageBuilder.produce(
if (menuSelection == i)
ItemSlotImageBuilder.COLOR_WHITE
else
ItemSlotImageBuilder.COLOR_BLACK,
i + 1
),
slotCentrePoint.x.toFloat() - (cellSize / 2f),
slotCentrePoint.y.toFloat() - (cellSize / 2f)
)
// TODO draw item
}
}
override fun processInput(input: Input) {
if (handler!!.isOpened || handler!!.isOpening) {
val cursorPos = Vector2(input.mouseX.toDouble(), input.mouseY.toDouble())
val centre = Vector2(Terrarum.WIDTH / 2.0, Terrarum.HEIGHT / 2.0)
val deg = (centre - cursorPos).direction.toFloat()
menuSelection = Math.round(deg * slotCount / FastMath.TWO_PI)
if (menuSelection < 0) menuSelection += 10
}
}
override fun doOpening(gc: GameContainer, delta: Int) {
if (openCloseTimer < openCloseTime) {
openCloseTimer += delta
handler!!.opacity = openCloseTimer.toFloat() / openCloseTime
handler!!.scale = smallenSize + (1f.minus(smallenSize) * handler!!.opacity)
}
}
override fun doClosing(gc: GameContainer, delta: Int) {
if (openCloseTimer < openCloseTime) {
openCloseTimer += delta
handler!!.isOpened = false
handler!!.opacity = (openCloseTime - openCloseTimer.toFloat()) / openCloseTime
handler!!.scale = smallenSize + (1f.minus(smallenSize) * handler!!.opacity)
}
}
override fun endOpening(gc: GameContainer, delta: Int) {
openCloseTimer = 0
handler!!.opacity = 1f
handler!!.scale = 1f
}
override fun endClosing(gc: GameContainer, delta: Int) {
openCloseTimer = 0
handler!!.opacity = 0f
handler!!.scale = 1f
}
}

View File

@@ -0,0 +1,64 @@
package net.torvald.terrarum.ui
import org.newdawn.slick.GameContainer
import org.newdawn.slick.Graphics
import org.newdawn.slick.Input
/**
* Created by minjaesong on 16-07-20.
*/
class UIQuickBar : UICanvas {
override var width: Int
get() = throw UnsupportedOperationException()
set(value) {
}
override var height: Int
get() = throw UnsupportedOperationException()
set(value) {
}
/**
* In milliseconds
*/
override var openCloseTime: Int
get() = throw UnsupportedOperationException()
set(value) {
}
override var openCloseTimer: Int
get() = throw UnsupportedOperationException()
set(value) {
}
override var handler: UIHandler? = null
override fun update(gc: GameContainer, delta: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun render(gc: GameContainer, g: Graphics) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun processInput(input: Input) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun doOpening(gc: GameContainer, delta: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun doClosing(gc: GameContainer, delta: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun endOpening(gc: GameContainer, delta: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun endClosing(gc: GameContainer, delta: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
companion object {
const val SLOT_COUNT = 10
}
}