trying to write a new ingamecontroller so that its update can be manually controlled

This commit is contained in:
minjaesong
2021-08-21 21:03:39 +09:00
parent bb95444067
commit ca72a6fbe5
14 changed files with 214 additions and 111 deletions

View File

@@ -325,11 +325,6 @@ class TitleScreen(batch: SpriteBatch) : IngameInstance(batch) {
return true
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
screen.uiContainer.forEach { it?.mouseMoved(screenX, screenY) }
return true
}
override fun keyTyped(character: Char): Boolean {
screen.uiContainer.forEach { it?.keyTyped(character) }
return true

View File

@@ -2,13 +2,13 @@ package net.torvald.terrarum.gamecontroller
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.Input.Keys
import com.badlogic.gdx.InputAdapter
import com.badlogic.gdx.controllers.Controllers
import com.badlogic.gdx.utils.GdxRuntimeException
import net.torvald.terrarum.AppLoader
import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.AppLoader.printdbgerr
import net.torvald.terrarum.TerrarumAppConfiguration
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE
import net.torvald.terrarum.controller.TerrarumController
import net.torvald.terrarum.floorInt
@@ -17,9 +17,8 @@ import net.torvald.terrarum.gameitem.GameItem
import net.torvald.terrarum.gameworld.fmod
import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.gameactors.FixtureBase
import net.torvald.terrarum.worlddrawer.CreateTileAtlas
import net.torvald.terrarum.worlddrawer.WorldCamera
import java.util.*
/**
* Created by minjaesong on 2015-12-31.
@@ -61,6 +60,12 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() {
private var worldPrimaryClickLatched = false
private val keyStatus = BitSet(256)
private var inputMouseX = -1
private var inputMouseY = -1
private val mouseStatus = BitSet(8)
private val controllerButtonStatus = BitSet(64)
fun update(delta: Float) {
///////////////////
@@ -107,11 +112,45 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() {
//KeyToggler.update(terrarumIngame.canPlayerControl)
//printdbg(this, terrarumIngame.canPlayerControl)
// control key events
var noKeyHeldDown = true
for (key in 1..Input.Keys.MAX_KEYCODE) {
val keyDown = Gdx.input.isKeyPressed(key)
noKeyHeldDown = noKeyHeldDown and keyDown
if (keyDown && !keyStatus[key])
tKeyDown(key)
else if (!keyDown && keyStatus[key])
tKeyUp(key)
keyStatus[key] = keyDown
}
// control mouse/touch events
val newmx = Gdx.input.x
val newmy = Gdx.input.y
for (touch in 0..4) {
val touchDown = Gdx.input.isButtonPressed(touch)
if (touchDown && !mouseStatus[touch])
tTouchDown(newmx, newmy, 0, touch)
else if (!touchDown && keyStatus[touch])
tTouchUp(newmx, newmy, 0, touch)
if (touchDown && mouseStatus.bitCount() != 0) {
tTouchDragged(newmx, newmy, 0)
}
mouseStatus[touch] = touchDown
}
inputMouseX = newmx
inputMouseY = newmy
}
private var f12Down = false
override fun keyDown(keycode: Int): Boolean {
private fun tKeyDown(keycode: Int): Boolean {
if (!terrarumIngame.paused) {
terrarumIngame.actorNowPlaying?.keyDown(keycode)
@@ -148,7 +187,7 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() {
return true
}
override fun keyUp(keycode: Int): Boolean {
private fun tKeyUp(keycode: Int): Boolean {
if (AppLoader.getConfigIntArray("config_keyquickselalt").contains(keycode)
|| keycode == AppLoader.getConfigInt("config_keyquicksel")) {
terrarumIngame.uiPieMenu.setAsClose()
@@ -169,12 +208,7 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() {
return true
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
terrarumIngame.uiContainer.forEach { it?.mouseMoved(screenX, screenY) }
return true
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
private fun tTouchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
// don't separate Player from this! Physics will break, esp. airborne manoeuvre
if (!terrarumIngame.paused) {
// fire world click events; the event is defined as Ingame's (or any others') WorldClick event
@@ -216,12 +250,12 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() {
return true
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
private fun tTouchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
terrarumIngame.uiContainer.forEach { it?.touchDragged(screenX, screenY, pointer) }
return true
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
private fun tTouchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
terrarumIngame.uiContainer.forEach { it?.touchDown(screenX, screenY, pointer, button) }
// pie menu
@@ -233,5 +267,163 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() {
return true
}
companion object {
const val KEY_DELAY = 0.2f
const val KEY_REPEAT = 1f / 40f
val KEYCODE_TO_CHAR = hashMapOf<Int, Char>(
Keys.NUM_1 to '1',
Keys.NUM_2 to '2',
Keys.NUM_3 to '3',
Keys.NUM_4 to '4',
Keys.NUM_5 to '5',
Keys.NUM_6 to '6',
Keys.NUM_7 to '7',
Keys.NUM_8 to '8',
Keys.NUM_9 to '9',
Keys.NUM_0 to '0',
}
Keys.A to 'a',
Keys.B to 'b',
Keys.C to 'c',
Keys.D to 'd',
Keys.E to 'e',
Keys.F to 'f',
Keys.G to 'g',
Keys.H to 'h',
Keys.I to 'i',
Keys.J to 'j',
Keys.K to 'k',
Keys.L to 'l',
Keys.M to 'm',
Keys.N to 'n',
Keys.O to 'o',
Keys.P to 'p',
Keys.Q to 'q',
Keys.R to 'r',
Keys.S to 's',
Keys.T to 't',
Keys.U to 'u',
Keys.V to 'v',
Keys.W to 'w',
Keys.X to 'x',
Keys.Y to 'y',
Keys.Z to 'z',
Keys.GRAVE to '`',
Keys.MINUS to '-',
Keys.EQUALS to '=',
Keys.BACKSPACE to 8.toChar(),
Keys.LEFT_BRACKET to '[',
Keys.RIGHT_BRACKET to ']',
Keys.BACKSLASH to '\\',
Keys.SEMICOLON to ';',
Keys.APOSTROPHE to '\'',
Keys.ENTER to 10.toChar(),
Keys.COMMA to ',',
Keys.PERIOD to '.',
Keys.SLASH to '/',
Keys.SPACE to ' ',
Keys.NUMPAD_0 to '0',
Keys.NUMPAD_1 to '1',
Keys.NUMPAD_2 to '2',
Keys.NUMPAD_3 to '3',
Keys.NUMPAD_4 to '4',
Keys.NUMPAD_5 to '5',
Keys.NUMPAD_6 to '6',
Keys.NUMPAD_7 to '7',
Keys.NUMPAD_8 to '8',
Keys.NUMPAD_9 to '9',
Keys.NUMPAD_DIVIDE to '/',
Keys.NUMPAD_MULTIPLY to '*',
Keys.NUMPAD_SUBTRACT to '-',
Keys.NUMPAD_ADD to '+',
Keys.NUMPAD_DOT to '.',
Keys.NUMPAD_ENTER to 10.toChar()
)
val KEYCODE_TO_CHAR_SHIFT = hashMapOf<Int, Char>(
Keys.NUM_1 to '!',
Keys.NUM_2 to '@',
Keys.NUM_3 to '#',
Keys.NUM_4 to '$',
Keys.NUM_5 to '%',
Keys.NUM_6 to '^',
Keys.NUM_7 to '&',
Keys.NUM_8 to '*',
Keys.NUM_9 to '(',
Keys.NUM_0 to ')',
Keys.A to 'A',
Keys.B to 'B',
Keys.C to 'C',
Keys.D to 'D',
Keys.E to 'E',
Keys.F to 'F',
Keys.G to 'G',
Keys.H to 'H',
Keys.I to 'I',
Keys.J to 'J',
Keys.K to 'K',
Keys.L to 'L',
Keys.M to 'M',
Keys.N to 'N',
Keys.O to 'O',
Keys.P to 'P',
Keys.Q to 'Q',
Keys.R to 'R',
Keys.S to 'S',
Keys.T to 'T',
Keys.U to 'U',
Keys.V to 'V',
Keys.W to 'W',
Keys.X to 'X',
Keys.Y to 'Y',
Keys.Z to 'Z',
Keys.GRAVE to '~',
Keys.MINUS to '_',
Keys.EQUALS to '+',
Keys.BACKSPACE to 8.toChar(),
Keys.LEFT_BRACKET to '{',
Keys.RIGHT_BRACKET to '}',
Keys.BACKSLASH to '|',
Keys.SEMICOLON to ':',
Keys.APOSTROPHE to '"',
Keys.ENTER to 10.toChar(),
Keys.COMMA to '<',
Keys.PERIOD to '>',
Keys.SLASH to '?',
Keys.SPACE to ' ',
Keys.NUMPAD_0 to '0',
Keys.NUMPAD_1 to '1',
Keys.NUMPAD_2 to '2',
Keys.NUMPAD_3 to '3',
Keys.NUMPAD_4 to '4',
Keys.NUMPAD_5 to '5',
Keys.NUMPAD_6 to '6',
Keys.NUMPAD_7 to '7',
Keys.NUMPAD_8 to '8',
Keys.NUMPAD_9 to '9',
Keys.NUMPAD_DIVIDE to '/',
Keys.NUMPAD_MULTIPLY to '*',
Keys.NUMPAD_SUBTRACT to '-',
Keys.NUMPAD_ADD to '+',
Keys.NUMPAD_DOT to '.',
Keys.NUMPAD_ENTER to 10.toChar()
)
}
private inline fun BitSet.bitCount() = this.cardinality()
}

View File

@@ -33,7 +33,7 @@ object KeyToggler {
* Set ```toggleGameKeys = true``` to make toggling work for keys like Q, W, E, ...; otherwise only F1-F12 keys will be toggled
*/
fun update(toggleGameKeys: Boolean) {
for (it in 0..255) {
for (it in 1..255) {
if (!toggleGameKeys && gameKeys.contains(it)) {
continue
}

View File

@@ -505,11 +505,6 @@ class BuildingMakerController(val screen: BuildingMaker) : InputAdapter() {
return true
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
screen.uiContainer.forEach { it?.mouseMoved(screenX, screenY) }
return true
}
override fun keyTyped(character: Char): Boolean {
screen.uiContainer.forEach { it?.keyTyped(character) }
return true

View File

@@ -564,6 +564,10 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
particlesActive = 0
// synchronised Ingame Input Updater
ingameController.update(delta)
if (!paused) {
//hypothetical_input_capturing_function_if_you_finally_decided_to_forgo_gdx_input_processor_and_implement_your_own_to_synchronise_everything()
@@ -611,9 +615,6 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
}
// world click events (e.g. opening the UI that a fixture has) must go here
ingameController.update(delta)
/*if (!paused) {
// completely consume block change queues because why not
terrainChangeQueue.clear()

View File

@@ -188,14 +188,6 @@ open class UIRemoCon(treeRepresentation: QNDTreeNode<String>) : UICanvas() {
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
screens.forEach {
it.second.mouseMoved(screenX, screenY) // again, underlying handler will block unnecessary renders
}
return true
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
screens.forEach {
it.second.touchDragged(screenX, screenY, pointer) // again, underlying handler will block unnecessary renders

View File

@@ -14,10 +14,10 @@ import kotlin.math.roundToInt
* ## UI Items
*
* UI can contain one or more UI elements (called UIItem). Each UIItem can have one or more events programmed to it.
* Events have their own listener are governed by their GDX event handlers (e.g. mouseMoved).
* Events have their own listener are governed by their GDX event handlers (e.g. touchDragged).
* These GDX handlers are what makes the our own handler to work.
*
* UIItems have following event handlers: updateLister, keyDownListener, mouseMovedListener, touchDraggedListener, touchDownListener, touchUpListener, scrolledListener, and clickOnceListener.
* UIItems have following event handlers: updateLister, keyDownListener, touchDraggedListener, touchDownListener, touchUpListener, scrolledListener, and clickOnceListener.
* (perhaps clickOnceListener is the one most useful)
*
* To make them work without any hassle on your part,
@@ -150,15 +150,6 @@ abstract class UICanvas(
fun mouseInScreen(x: Int, y: Int) = x in 0 until AppLoader.screenSize.screenW && y in 0 until AppLoader.screenSize.screenH
open fun mouseMoved(screenX: Int, screenY: Int): Boolean {
if (this.isVisible) {
uiItems.forEach { it.mouseMoved(screenX, screenY) }
handler.subUIs.forEach { it.mouseMoved(screenX, screenY) }
return true
}
else return false
}
/**
* Called by the screen's InputProcessor
*

View File

@@ -346,16 +346,6 @@ void main() {
TerrarumIngame.setCameraPosition(batch, camera, newX, newY)
}
fun mouseMoved(uiItems: List<UIItem>, screenX: Int, screenY: Int): Boolean {
if (isVisible) {
uiItems.forEach { it.mouseMoved(screenX, screenY) }
subUIs.forEach { it.mouseMoved(screenX, screenY) }
return true
}
else {
return false
}
}
fun touchDragged(uiItems: List<UIItem>, screenX: Int, screenY: Int, pointer: Int): Boolean {
if (isVisible) {
uiItems.forEach { it.touchDragged(screenX, screenY, pointer) }

View File

@@ -16,8 +16,7 @@ import net.torvald.terrarum.Terrarum
* - updateListener
* - keyDownListener
* - keyUpListener
* - mouseMovedListene
* - touchDraggedListe
* - touchDraggedLister
* - touchDownListener
* - touchUpListener
* - scrolledListener
@@ -104,7 +103,6 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
open var keyDownListener: ((Int) -> Unit)? = null
/** Parametre: keycode */
open var keyUpListener: ((Int) -> Unit)? = null
open var mouseMovedListener: ((Int, Int) -> Unit)? = null
open var touchDraggedListener: ((Int, Int, Int) -> Unit)? = null
/** Parameters: screenX, screenY, pointer, button */
open var touchDownListener: ((Int, Int, Int, Int) -> Unit)? = null
@@ -177,14 +175,6 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
}
// mouse controlled
open fun mouseMoved(screenX: Int, screenY: Int): Boolean {
if (parentUI.isVisible && mouseMovedListener != null) {
mouseMovedListener!!.invoke(relativeMouseX, relativeMouseY)
return true
}
return false
}
open fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
if (parentUI.isVisible && touchDraggedListener != null) {
touchDraggedListener!!.invoke(relativeMouseX, relativeMouseY, pointer)

View File

@@ -43,34 +43,6 @@ class UIItemImageGallery(
}
}
override fun keyDown(keycode: Int): Boolean {
TODO("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(screenX: Int, screenY: Int): Boolean {
TODO("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 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 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 scrolled(amountX: Float, amountY: Float): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun dispose() {
imageList.forEach { it.dispose() }
}

View File

@@ -103,9 +103,6 @@ class UIItemIntSlider(
override var keyUpListener: ((Int) -> Unit)?
get() = super.keyUpListener
set(_) {}
override var mouseMovedListener: ((Int, Int) -> Unit)?
get() = super.mouseMovedListener
set(_) {}
override var touchDraggedListener: ((Int, Int, Int) -> Unit)?
get() = super.touchDraggedListener
set(_) {}

View File

@@ -257,10 +257,6 @@ class UIItemTextButtonList(
return super.keyUp(keycode) || buttons.map { it.keyUp(keycode).toInt() }.sum() != 0
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
return super.mouseMoved(screenX, screenY) || buttons.map { it.mouseMoved(screenX, screenY).toInt() }.sum() != 0
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
return super.touchDragged(screenX, screenY, pointer) || buttons.map { it.touchDragged(screenX, screenY, pointer).toInt() }.sum() != 0
}

View File

@@ -96,11 +96,6 @@ open class UIItemTransitionContainer(
return true
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
uis.forEachIndexed { index, ui -> if (timeToUpdate(index)) ui.mouseMoved(screenX, screenY) }
return true
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
uis.forEachIndexed { index, ui -> if (timeToUpdate(index)) ui.touchDragged(screenX, screenY, pointer) }
return true

View File

@@ -202,9 +202,6 @@ class UINSMenu(
override fun endClosing(delta: Float) {
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
return super.mouseMoved(screenX, screenY)
}
private var dragOriginX = 0 // relative mousepos
private var dragOriginY = 0 // relative mousepos