action listener on textbutton list

This commit is contained in:
minjaesong
2017-07-20 22:25:40 +09:00
parent 6f2c1e578e
commit 78b09b2790
13 changed files with 215 additions and 53 deletions

View File

@@ -14,7 +14,7 @@ import net.torvald.terrarum.concurrent.ThreadParallel
import net.torvald.terrarum.console.*
import net.torvald.terrarum.gameactors.*
import net.torvald.terrarum.gameactors.physicssolver.CollisionSolver
import net.torvald.terrarum.gamecontroller.GameController
import net.torvald.terrarum.gamecontroller.IngameController
import net.torvald.terrarum.gamecontroller.KeyToggler
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.gameworld.WorldSimulator
@@ -32,7 +32,6 @@ import javax.swing.JOptionPane
import com.badlogic.gdx.graphics.OrthographicCamera
import net.torvald.random.HQRNG
import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.ui.*
import net.torvald.terrarum.worldgenerator.RoguelikeRandomiser
import net.torvald.terrarum.worldgenerator.WorldGenerator
@@ -281,6 +280,8 @@ class Ingame(val batch: SpriteBatch) : Screen {
}
}
private val ingameController = IngameController(this)
/** Load rest of the game with GL context */
fun postInit() {
//LightmapRenderer.world = this.world
@@ -288,7 +289,7 @@ class Ingame(val batch: SpriteBatch) : Screen {
//FeaturesDrawer.world = this.world
Gdx.input.inputProcessor = GameController
Gdx.input.inputProcessor = ingameController
initViewPort(Terrarum.WIDTH, Terrarum.HEIGHT)
@@ -471,7 +472,7 @@ class Ingame(val batch: SpriteBatch) : Screen {
KeyToggler.update()
GameController.update(delta)
ingameController.update(delta)
if (!paused) {
@@ -1529,7 +1530,7 @@ class Ingame(val batch: SpriteBatch) : Screen {
// Set up viewport when window is resized
initViewPort(width, height)
initViewPort(Terrarum.WIDTH, Terrarum.HEIGHT)

View File

@@ -13,9 +13,9 @@ import com.badlogic.gdx.graphics.glutils.ShaderProgram
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
import com.google.gson.JsonArray
import com.google.gson.JsonPrimitive
import net.torvald.random.HQRNG
import net.torvald.terrarum.Terrarum.RENDER_FPS
import net.torvald.terrarum.gamecontroller.GameController
import net.torvald.terrarum.gameactors.floorInt
import net.torvald.terrarum.gamecontroller.IngameController
import net.torvald.terrarum.imagefont.TinyAlphNum
import net.torvald.terrarum.imagefont.Watch7SegMain
import net.torvald.terrarum.imagefont.WatchDotAlph
@@ -24,6 +24,8 @@ import net.torvald.terrarum.ui.ItemSlotImageBuilder
import net.torvald.terrarum.ui.MessageWindow
import net.torvald.terrarum.utils.JsonFetcher
import net.torvald.terrarum.utils.JsonWriter
import net.torvald.terrarum.worlddrawer.FeaturesDrawer
import net.torvald.terrarum.worlddrawer.WorldCamera
import net.torvald.terrarumsansbitmap.gdx.GameFontBase
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import org.lwjgl.input.Controllers
@@ -400,18 +402,18 @@ object Terrarum : Game() {
ingame = Ingame(batch)
//ingame = Ingame(batch)
//ingame!!.gameLoadInfoPayload = Ingame.NewWorldParameters(8192, 2048, HQRNG().nextLong())
// TODO: create world being used by title screen, and serialise it.
ingame!!.gameLoadInfoPayload = Ingame.NewWorldParameters(2400, 800, HQRNG().nextLong())
ingame!!.gameLoadMode = Ingame.GameLoadMode.CREATE_NEW
//ingame!!.gameLoadInfoPayload = Ingame.NewWorldParameters(2400, 800, HQRNG().nextLong())
//ingame!!.gameLoadMode = Ingame.GameLoadMode.CREATE_NEW
LoadScreen.screenToLoad = ingame!!
//LoadScreen.screenToLoad = ingame!!
//super.setScreen(TitleScreen(batch))
super.setScreen(LoadScreen)
super.setScreen(TitleScreen(batch))
//super.setScreen(LoadScreen)
//super.setScreen(ingame)
}
@@ -645,13 +647,13 @@ object Terrarum : Game() {
}
inline val mouseX: Double
get() = GameController.mouseX.toDouble()
get() = WorldCamera.x + Gdx.input.x / (ingame?.screenZoom ?: 1f).toDouble()
inline val mouseY: Double
get() = GameController.mouseY.toDouble()
get() = WorldCamera.y + Gdx.input.y / (ingame?.screenZoom ?: 1f).toDouble()
@JvmStatic inline val mouseTileX: Int
get() = GameController.mouseTileX
get() = (mouseX / FeaturesDrawer.TILE_SIZE).floorInt()
@JvmStatic inline val mouseTileY: Int
get() = GameController.mouseTileY
get() = (mouseY / FeaturesDrawer.TILE_SIZE).floorInt()
inline val mouseScreenX: Int
get() = Gdx.input.x
inline val mouseScreenY: Int

View File

@@ -2,6 +2,7 @@ package net.torvald.terrarum
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.InputAdapter
import com.badlogic.gdx.Screen
import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g2d.SpriteBatch
@@ -67,7 +68,7 @@ class TitleScreen(val batch: SpriteBatch) : Screen {
lateinit var logo: TextureRegion
val uiContainer = ArrayList<UIHandler>()
private lateinit var uiMenu: UIHandler
private fun loadThingsWhileIntroIsVisible() {
@@ -92,6 +93,8 @@ class TitleScreen(val batch: SpriteBatch) : Screen {
uiMenu.setAsOpen()
uiContainer.add(uiMenu)
loadDone = true
}
@@ -104,6 +107,9 @@ class TitleScreen(val batch: SpriteBatch) : Screen {
logo = TextureRegion(Texture(Gdx.files.internal("assets/graphics/logo_placeholder.tga")))
logo.flip(false, true)
Gdx.input.inputProcessor = TitleScreenController(this)
}
private var blurWriteBuffer = lightmapFboA
@@ -156,7 +162,7 @@ class TitleScreen(val batch: SpriteBatch) : Screen {
// update UIs //
uiMenu.update(delta)
uiContainer.forEach { it.update(delta) }
// render and blur lightmap
@@ -217,7 +223,7 @@ class TitleScreen(val batch: SpriteBatch) : Screen {
batch.shader = null
uiMenu.render(batch, camera)
uiContainer.forEach { it.render(batch, camera) }
}
private fun renderOverlayTexts() {
@@ -249,7 +255,14 @@ class TitleScreen(val batch: SpriteBatch) : Screen {
override fun resize(width: Int, height: Int) {
// Set up viewport when window is resized
initViewPort(width, height)
initViewPort(Terrarum.WIDTH, Terrarum.HEIGHT)
if (loadDone) {
// resize UI by re-creating it (!!)
uiMenu.UI.resize(Terrarum.WIDTH, Terrarum.HEIGHT)
uiMenu.setPosition(0, UIStartMenu.menubarOffY)
}
}
override fun dispose() {
@@ -260,6 +273,8 @@ class TitleScreen(val batch: SpriteBatch) : Screen {
uiMenu.dispose()
}
fun setCameraPosition(newX: Float, newY: Float) {
Ingame.setCameraPosition(batch, camera, newX, newY)
}
@@ -360,4 +375,45 @@ class TitleScreen(val batch: SpriteBatch) : Screen {
}
class TitleScreenController(val screen: TitleScreen) : InputAdapter() {
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
screen.uiContainer.forEach { it.touchUp(screenX, screenY, pointer, button) }
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
}
override fun scrolled(amount: Int): Boolean {
screen.uiContainer.forEach { it.scrolled(amount) }
return true
}
override fun keyUp(keycode: Int): Boolean {
screen.uiContainer.forEach { it.keyUp(keycode) }
return true
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
screen.uiContainer.forEach { it.touchDragged(screenX, screenY, pointer) }
return true
}
override fun keyDown(keycode: Int): Boolean {
screen.uiContainer.forEach { it.keyDown(keycode) }
return true
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
screen.uiContainer.forEach { it.touchDown(screenX, screenY, pointer, button) }
return true
}
}
}

View File

@@ -178,4 +178,24 @@ class UIItemInventoryElem(
override fun dispose() {
itemImage?.texture?.dispose()
}
override fun keyUp(keycode: Int): Boolean {
return super.keyUp(keycode)
}
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 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)
}
}

View File

@@ -3,6 +3,7 @@ package net.torvald.terrarum.gamecontroller
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.InputAdapter
import net.torvald.terrarum.Ingame
import net.torvald.terrarum.worlddrawer.FeaturesDrawer
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.*
@@ -12,12 +13,9 @@ import net.torvald.terrarum.worlddrawer.WorldCamera
/**
* Created by minjaesong on 15-12-31.
*/
object GameController : InputAdapter() {
class IngameController(val ingame: Ingame) : InputAdapter() {
private val ingame = Terrarum.ingame!!
// these four values can also be accessed with GameContainer.<varname>
// e.g. gc.mouseTileX
@@ -58,16 +56,16 @@ object GameController : InputAdapter() {
///////////////////
// Use item: assuming the player has only one effective grip (EquipPosition.HAND_GRIP)
if (ingame.player != null && ingame.canPlayerControl) {
if (ingame.canPlayerControl) {
if (Gdx.input.isButtonPressed(Terrarum.getConfigInt("mouseprimary")) || Gdx.input.isButtonPressed(Terrarum.getConfigInt("mousesecondary"))) {
val itemOnGrip = ingame.player!!.inventory.itemEquipped[GameItem.EquipPosition.HAND_GRIP]
val itemOnGrip = ingame.player.inventory.itemEquipped[GameItem.EquipPosition.HAND_GRIP]
itemOnGrip?.let {
if (Gdx.input.isButtonPressed(Terrarum.getConfigInt("mouseprimary"))) {
ingame.player!!.consumePrimary(it)
ingame.player.consumePrimary(it)
}
if (Gdx.input.isButtonPressed(Terrarum.getConfigInt("mousesecondary"))) {
ingame.player!!.consumeSecondary(it)
ingame.player.consumeSecondary(it)
}
}
}
@@ -82,7 +80,7 @@ object GameController : InputAdapter() {
override fun keyDown(keycode: Int): Boolean {
if (ingame.canPlayerControl) {
ingame.player?.keyDown(keycode)
ingame.player.keyDown(keycode)
}
if (Terrarum.getConfigIntArray("keyquickselalt").contains(keycode)
@@ -129,26 +127,23 @@ object GameController : InputAdapter() {
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
if (Terrarum.ingame != null) {
val ingame = Terrarum.ingame!!
// don't separate Player from this! Physics will break, esp. airborne manoeuvre
if (ingame.player != null && ingame.canPlayerControl) {
val itemOnGrip = ingame.player!!.inventory.itemEquipped[GameItem.EquipPosition.HAND_GRIP]
// don't separate Player from this! Physics will break, esp. airborne manoeuvre
if (ingame.canPlayerControl) {
val itemOnGrip = ingame.player.inventory.itemEquipped[GameItem.EquipPosition.HAND_GRIP]
if (itemOnGrip != null) {
if (button == Terrarum.getConfigInt("mouseprimary")) {
itemOnGrip.endPrimaryUse(Gdx.graphics.deltaTime)
}
if (button == Terrarum.getConfigInt("mousesecondary")) {
itemOnGrip.endSecondaryUse(Gdx.graphics.deltaTime)
}
if (itemOnGrip != null) {
if (button == Terrarum.getConfigInt("mouseprimary")) {
itemOnGrip.endPrimaryUse(Gdx.graphics.deltaTime)
}
if (button == Terrarum.getConfigInt("mousesecondary")) {
itemOnGrip.endSecondaryUse(Gdx.graphics.deltaTime)
}
}
ingame.uiContainer.forEach { it.touchUp(screenX, screenY, pointer, button) } // for MouseControlled UIcanvases
}
ingame.uiContainer.forEach { it.touchUp(screenX, screenY, pointer, button) } // for MouseControlled UIcanvases
return true
}

View File

@@ -139,7 +139,7 @@ abstract class GameItem : Comparable<GameItem>, Cloneable {
*
* note: DO NOT super() this!
*
* Consumption function is executed in net.torvald.terrarum.gamecontroller.GameController,
* Consumption function is executed in net.torvald.terrarum.gamecontroller.IngameController,
* in which the function itself is defined in net.torvald.terrarum.gameactors.ActorInventory
*/
open fun primaryUse(delta: Float): Boolean = false

View File

@@ -110,6 +110,11 @@ abstract class UICanvas {
//uiItems.forEach { it.keyT }
}
open fun resize(width: Int, height: Int) {
this.width = width
this.height = height
}
companion object {
const val OPENCLOSE_GENERIC = 0.2f

View File

@@ -114,8 +114,6 @@ 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) {
@@ -123,7 +121,9 @@ abstract class UIItem(var parentUI: UICanvas) { // do not replace parentUI to UI
actionDone = true
}
if (!clickOnceListenerFired && mouseUp) {
if (clickOnceListener != null && !clickOnceListenerFired && mouseUp) {
println("arstineotarsneio")
clickOnceListener!!.invoke(relativeMouseX, relativeMouseY, button)
actionDone = true
}

View File

@@ -53,4 +53,32 @@ open class UIItemImageButton(
override fun dispose() {
image.texture.dispose()
}
override fun keyDown(keycode: Int): Boolean {
return super.keyDown(keycode)
}
override fun keyUp(keycode: Int): Boolean {
return super.keyUp(keycode)
}
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)
}
}

View File

@@ -77,4 +77,32 @@ open class UIItemTextButton(
override fun dispose() {
}
override fun keyDown(keycode: Int): Boolean {
return super.keyDown(keycode)
}
override fun keyUp(keycode: Int): Boolean {
return super.keyUp(keycode)
}
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)
}
}

View File

@@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer
import net.torvald.terrarum.BlendMode
import net.torvald.terrarum.fillRect
import net.torvald.terrarum.gameactors.Second
import net.torvald.terrarum.gameactors.ai.toInt
import net.torvald.terrarum.gameactors.roundInt
import net.torvald.terrarum.inUse
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
@@ -175,4 +176,32 @@ class UIItemTextButtonList(
override fun dispose() {
iconSpriteSheet?.dispose()
}
override fun keyDown(keycode: Int): Boolean {
return super.keyDown(keycode) || buttons.map { it.keyDown(keycode).toInt() }.sum() != 0
}
override fun keyUp(keycode: Int): Boolean {
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
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return super.touchDown(screenX, screenY, pointer, button) || buttons.map { it.touchDown(screenX, screenY, pointer, button).toInt() }.sum() != 0
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return super.touchUp(screenX, screenY, pointer, button) || buttons.map { it.touchUp(screenX, screenY, pointer, button).toInt() }.sum() != 0
}
override fun scrolled(amount: Int): Boolean {
return super.scrolled(amount) || buttons.map { it.scrolled(amount).toInt() }.sum() != 0
}
}

View File

@@ -16,7 +16,7 @@ class UIStartMenu : UICanvas() {
"MENU_LABEL_EXIT"
)
val menubarOffY = Terrarum.HEIGHT - 180 - 40 * menuLabels.size.plus(1)
val menubarOffY: Int; get() = Terrarum.HEIGHT / 2 - (Terrarum.fontGame.lineHeight * 1.5).toInt()
}
@@ -45,7 +45,7 @@ class UIStartMenu : UICanvas() {
// attach listeners
menubar.buttons[3].clickOnceListener = { _, _, _ -> System.exit(0) }
menubar.buttons[menuLabels.indexOf("MENU_LABEL_EXIT")].clickOnceListener = { _, _, _ -> System.exit(0) }
}
override fun update(delta: Float) {

View File

@@ -126,8 +126,6 @@ object WeatherMixer {
val topCol = getGradientColour(skyboxColourMap, 0, timeNow)
val bottomCol = getGradientColour(skyboxColourMap, 1, timeNow)
println("zero pos: $parallaxZeroPos, domain_size: $parallaxDomainSize")
//Terrarum.textureWhiteSquare.bind(0)
Terrarum.shaderBayerSkyboxFill.begin()