keyboard control ported to GDX (at least as much as I can right now)

This commit is contained in:
minjaesong
2017-07-01 18:31:00 +09:00
parent 54c643b35e
commit e2da14da8a
25 changed files with 298 additions and 327 deletions

View File

@@ -1,6 +1,7 @@
package net.torvald.terrarum.ui
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.dataclass.HistoryArray
@@ -9,7 +10,6 @@ import net.torvald.terrarum.TerrarumGDX
import net.torvald.terrarum.console.Authenticator
import net.torvald.terrarum.console.CommandInterpreter
import net.torvald.terrarum.fillRect
import net.torvald.terrarum.gamecontroller.Key
/**
@@ -80,60 +80,59 @@ class ConsoleWindow : UICanvas, KeyControlled {
}
override fun keyPressed(key: Int, c: Char) {
override fun keyDown(key: Int): Boolean {
// history
if (key == Key.UP && historyIndex < commandHistory.history.size)
if (key == Input.Keys.UP && historyIndex < commandHistory.history.size)
historyIndex++
else if (key == Key.DOWN && historyIndex >= 0)
else if (key == Input.Keys.DOWN && historyIndex >= 0)
historyIndex--
else if (key != Key.UP && key != Key.DOWN)
else if (key != Input.Keys.UP && key != Input.Keys.DOWN)
historyIndex = -1
// execute
if (key == Key.RETURN && commandInputPool!!.isNotEmpty()) {
if (key == Input.Keys.ENTER && commandInputPool!!.isNotEmpty()) {
commandHistory.add(commandInputPool!!.toString())
executeCommand()
commandInputPool = StringBuilder()
}
// erase last letter
else if (key == Key.BACKSPACE && commandInputPool!!.isNotEmpty()) {
else if (key == Input.Keys.BACKSPACE && commandInputPool!!.isNotEmpty()) {
commandInputPool!!.deleteCharAt(commandInputPool!!.length - 1)
}
// append acceptable letter
else if (key >= 2 && key <= 13
|| key >= 16 && key <= 27
|| key >= 30 && key <= 40
|| key >= 44 && key <= 53
|| commandInputPool!!.length > 0 && key == 57) {
commandInputPool!!.append(c)
inputCursorPos += 1
}
// scroll
else if (key == Key.UP || key == Key.DOWN) {
else if (key == Input.Keys.UP || key == Input.Keys.DOWN) {
// create new stringbuilder
commandInputPool = StringBuilder()
if (historyIndex >= 0) // just leave blank if index is -1
commandInputPool!!.append(commandHistory[historyIndex] ?: "")
}
// delete input
else if (key == Key.DELETE) {
else if (key == Input.Keys.BACKSPACE) {
commandInputPool = StringBuilder()
}
// message scroll up
else if (key == Key.PGUP) {
else if (key == Input.Keys.PAGE_UP) {
setDisplayPos(-MESSAGES_DISPLAY_COUNT + 1)
}
// message scroll down
else if (key == Key.PGDN) {
else if (key == Input.Keys.PAGE_DOWN) {
setDisplayPos(MESSAGES_DISPLAY_COUNT - 1)
}
return true
}
override fun keyReleased(key: Int, c: Char) {
override fun keyTyped(character: Char): Boolean {
commandInputPool!!.append(character)
inputCursorPos += 1
return true
}
override fun keyUp(keycode: Int): Boolean {
return false
}
private fun executeCommand() {
CommandInterpreter.execute(commandInputPool!!.toString())
@@ -205,12 +204,6 @@ class ConsoleWindow : UICanvas, KeyControlled {
openingTimeCounter = 0f
}
override fun controllerButtonPressed(controller: Int, button: Int) {
}
override fun controllerButtonReleased(controller: Int, button: Int) {
}
override fun processInput(delta: Float) {
}
}

View File

@@ -4,8 +4,7 @@ package net.torvald.terrarum.ui
* Created by minjaesong on 16-03-06.
*/
interface KeyControlled {
fun keyPressed(key: Int, c: Char)
fun keyReleased(key: Int, c: Char)
fun controllerButtonPressed(controller: Int, button: Int)
fun controllerButtonReleased(controller: Int, button: Int)
fun keyDown(keycode: Int): Boolean
fun keyUp(keycode: Int): Boolean
fun keyTyped(character: Char): Boolean
}

View File

@@ -4,9 +4,9 @@ package net.torvald.terrarum.ui
* Created by minjaesong on 16-03-06.
*/
interface MouseControlled {
fun mouseMoved(oldx: Int, oldy: Int, newx: Int, newy: Int)
fun mouseDragged(oldx: Int, oldy: Int, newx: Int, newy: Int)
fun mousePressed(button: Int, x: Int, y: Int)
fun mouseReleased(button: Int, x: Int, y: Int)
fun mouseWheelMoved(change: Int)
fun mouseMoved(screenX: Int, screenY: Int): Boolean
fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean
fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean
fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean
fun scrolled(amount: Int): Boolean
}

View File

@@ -209,58 +209,66 @@ class UIHandler(val UI: UICanvas,
}
}
fun keyPressed(key: Int, c: Char) {
fun keyDown(keycode: Int): Boolean {
if (isVisible && UI is KeyControlled) {
UI.keyPressed(key, c)
return UI.keyDown(keycode)
}
return false
}
fun keyReleased(key: Int, c: Char) {
fun keyUp(keycode: Int): Boolean {
if (isVisible && UI is KeyControlled) {
UI.keyReleased(key, c)
return UI.keyUp(keycode)
}
return false
}
fun mouseMoved(oldx: Int, oldy: Int, newx: Int, newy: Int) {
if (isVisible && UI is MouseControlled) {
UI.mouseMoved(oldx, oldy, newx, newy)
}
}
fun mouseDragged(oldx: Int, oldy: Int, newx: Int, newy: Int) {
if (isVisible && UI is MouseControlled) {
UI.mouseDragged(oldx, oldy, newx, newy)
}
}
fun mousePressed(button: Int, x: Int, y: Int) {
if (isVisible && UI is MouseControlled) {
UI.mousePressed(button, x, y)
}
}
fun mouseReleased(button: Int, x: Int, y: Int) {
if (isVisible && UI is MouseControlled) {
UI.mouseReleased(button, x, y)
}
}
fun mouseWheelMoved(change: Int) {
if (isVisible && UI is MouseControlled) {
UI.mouseWheelMoved(change)
}
}
fun controllerButtonPressed(controller: Int, button: Int) {
fun keyTyped(char: Char): Boolean {
if (isVisible && UI is KeyControlled) {
UI.controllerButtonPressed(controller, button)
return UI.keyTyped(char)
}
return false
}
fun mouseMoved(screenX: Int, screenY: Int) {
if (isVisible && UI is MouseControlled) {
UI.mouseMoved(screenX, screenY)
}
}
fun controllerButtonReleased(controller: Int, button: Int) {
if (isVisible && UI is KeyControlled) {
UI.controllerButtonReleased(controller, button)
fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
if (isVisible && UI is MouseControlled) {
UI.touchDragged(screenX, screenY, pointer)
}
return false
}
fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
if (isVisible && UI is MouseControlled) {
UI.touchDown(screenX, screenY, pointer, button)
}
return false
}
fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
if (isVisible && UI is MouseControlled) {
UI.touchUp(screenX, screenY, pointer, button)
}
return false
}
fun scrolled(amount: Int): Boolean {
if (isVisible && UI is MouseControlled) {
UI.scrolled(amount)
}
return false
}
// constant UI can't take control

View File

@@ -315,40 +315,47 @@ class UIInventory(
UICanvas.endClosingPopOut(handler, UICanvas.Companion.Position.LEFT)
}
override fun keyPressed(key: Int, c: Char) {
items.forEach { if (it.mouseUp) it.keyPressed(key, c) }
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun keyDown(keycode: Int): Boolean {
items.forEach { if (it.mouseUp) it.keyDown(keycode) }
shutUpAndRebuild()
return true
}
override fun mouseMoved(oldx: Int, oldy: Int, newx: Int, newy: Int) {
}
override fun keyReleased(key: Int, c: Char) {
items.forEach { if (it.mouseUp) it.keyReleased(key, c) }
override fun keyUp(keycode: Int): Boolean {
items.forEach { if (it.mouseUp) it.keyUp(keycode) }
shutUpAndRebuild()
return true
}
override fun mouseDragged(oldx: Int, oldy: Int, newx: Int, newy: Int) {
override fun keyTyped(character: Char): Boolean {
return false
}
override fun controllerButtonPressed(controller: Int, button: Int) {
items.forEach { if (it.mouseUp) it.controllerButtonPressed(controller, button) }
shutUpAndRebuild()
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
return false
}
override fun mousePressed(button: Int, x: Int, y: Int) {
items.forEach { if (it.mouseUp) it.mousePressed(button, x, y) }
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
items.forEach { if (it.mouseUp) it.touchDown(screenX, screenY, pointer, button) }
return true
}
override fun controllerButtonReleased(controller: Int, button: Int) {
items.forEach { if (it.mouseUp) it.controllerButtonReleased(controller, button) }
shutUpAndRebuild()
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
items.forEach { if (it.mouseUp) it.touchUp(screenX, screenY, pointer, button) }
return true
}
override fun mouseReleased(button: Int, x: Int, y: Int) {
items.forEach { if (it.mouseUp) it.mouseReleased(button, x, y) }
override fun scrolled(amount: Int): Boolean {
return false
}
override fun mouseWheelMoved(change: Int) {
}
}

View File

@@ -32,17 +32,14 @@ abstract class UIItem(var parentUI: UICanvas) { // do not replace parentUI to UI
abstract fun render(batch: SpriteBatch)
// keyboard controlled
abstract fun keyPressed(key: Int, c: Char)
abstract fun keyReleased(key: Int, c: Char)
abstract fun keyDown(keycode: Int): Boolean
abstract fun keyUp(keycode: Int): Boolean
// mouse controlled
abstract fun mouseMoved(oldx: Int, oldy: Int, newx: Int, newy: Int)
abstract fun mouseDragged(oldx: Int, oldy: Int, newx: Int, newy: Int)
abstract fun mousePressed(button: Int, x: Int, y: Int)
abstract fun mouseReleased(button: Int, x: Int, y: Int)
abstract fun mouseWheelMoved(change: Int)
abstract fun mouseMoved(screenX: Int, screenY: Int): Boolean
abstract fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean
abstract fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean
abstract fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean
abstract fun scrolled(amount: Int): Boolean
// gamepad controlled
abstract fun controllerButtonPressed(controller: Int, button: Int)
abstract fun controllerButtonReleased(controller: Int, button: Int)
}

View File

@@ -42,39 +42,31 @@ class UIItemImageGallery(
}
}
override fun keyPressed(key: Int, c: Char) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun keyDown(keycode: Int): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun keyReleased(key: Int, c: Char) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun keyUp(keycode: Int): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun mouseMoved(oldx: Int, oldy: Int, newx: Int, newy: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun mouseDragged(oldx: Int, oldy: Int, newx: Int, newy: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun mousePressed(button: Int, x: Int, y: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun mouseReleased(button: Int, x: Int, y: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun mouseWheelMoved(change: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun controllerButtonPressed(controller: Int, button: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun controllerButtonReleased(controller: Int, button: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun scrolled(amount: Int): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}

View File

@@ -82,30 +82,31 @@ class UIItemTextButton(
}
}
override fun keyPressed(key: Int, c: Char) {
override fun keyDown(keycode: Int): Boolean {
return false
}
override fun keyReleased(key: Int, c: Char) {
override fun keyUp(keycode: Int): Boolean {
return false
}
override fun mouseMoved(oldx: Int, oldy: Int, newx: Int, newy: Int) {
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
return false
}
override fun mouseDragged(oldx: Int, oldy: Int, newx: Int, newy: Int) {
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
return false
}
override fun mousePressed(button: Int, x: Int, y: Int) {
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return false
}
override fun mouseReleased(button: Int, x: Int, y: Int) {
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return false
}
override fun mouseWheelMoved(change: Int) {
}
override fun controllerButtonPressed(controller: Int, button: Int) {
}
override fun controllerButtonReleased(controller: Int, button: Int) {
override fun scrolled(amount: Int): Boolean {
return false
}
}

View File

@@ -169,30 +169,31 @@ class UIItemTextButtonList(
}
}
override fun keyPressed(key: Int, c: Char) {
override fun keyDown(keycode: Int): Boolean {
return false
}
override fun keyReleased(key: Int, c: Char) {
override fun keyUp(keycode: Int): Boolean {
return false
}
override fun mouseMoved(oldx: Int, oldy: Int, newx: Int, newy: Int) {
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
return false
}
override fun mouseDragged(oldx: Int, oldy: Int, newx: Int, newy: Int) {
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
return false
}
override fun mousePressed(button: Int, x: Int, y: Int) {
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return false
}
override fun mouseReleased(button: Int, x: Int, y: Int) {
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return false
}
override fun mouseWheelMoved(change: Int) {
}
override fun controllerButtonPressed(controller: Int, button: Int) {
}
override fun controllerButtonReleased(controller: Int, button: Int) {
override fun scrolled(amount: Int): Boolean {
return false
}
}

View File

@@ -89,22 +89,29 @@ class UIQuickBar : UICanvas, MouseControlled {
handler!!.opacity = 0f
}
override fun mouseMoved(oldx: Int, oldy: Int, newx: Int, newy: Int) {
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
return false
}
override fun mouseDragged(oldx: Int, oldy: Int, newx: Int, newy: Int) {
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
return false
}
override fun mousePressed(button: Int, x: Int, y: Int) {
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return false
}
override fun mouseReleased(button: Int, x: Int, y: Int) {
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return false
}
override fun mouseWheelMoved(change: Int) {
selection = selection.plus(if (change > 1) 1 else if (change < -1) -1 else 0).fmod(SLOT_COUNT)
override fun scrolled(amount: Int): Boolean {
selection = selection.plus(if (amount > 1) 1 else if (amount < -1) -1 else 0).fmod(SLOT_COUNT)
return true
}
companion object {
val finalOpacity = 0.8f