From 5b5534bcb91cb5318d7d2be1071fed1c19bd2e23 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 5 Mar 2024 05:21:17 +0900 Subject: [PATCH] LR click behav change/mouse clicks were sticky and causes unwanted behaviour on some fixture UIs --- src/net/torvald/terrarum/IngameInstance.kt | 2 +- .../torvald/terrarum/gameactors/WireActor.kt | 11 +- .../gamecontroller/IngameController.kt | 32 ++- .../torvald/terrarum/gameitems/GameItem.kt | 6 +- .../terrarum/modulebasegame/TerrarumIngame.kt | 206 ++++++++++-------- .../gameactors/FixtureMusicalTurntable.kt | 2 +- .../gameactors/FixtureSignalSwitchManual.kt | 2 +- .../gameitems/FixtureItemBase.kt | 5 + .../modulebasegame/ui/UICraftingWorkbench.kt | 13 +- .../modulebasegame/ui/UIInventoryCells.kt | 1 + .../modulebasegame/ui/UIInventoryEscMenu.kt | 1 + .../modulebasegame/ui/UIInventoryFull.kt | 6 +- .../ui/UIItemInventoryCellBase.kt | 2 +- .../terrarum/modulebasegame/ui/UIJukebox.kt | 12 +- .../terrarum/modulebasegame/ui/UILoadList.kt | 3 + .../modulebasegame/ui/UILoadSavegame.kt | 2 + .../terrarum/modulebasegame/ui/UIShare.kt | 2 + .../modulebasegame/ui/UIStorageChest.kt | 14 +- .../ui/UITemplateHalfInventory.kt | 1 + .../modulebasegame/ui/UITitleLanguage.kt | 2 + .../modulebasegame/ui/UIWorldPortal.kt | 7 +- .../modulebasegame/ui/UIWorldPortalCargo.kt | 12 +- .../modulebasegame/ui/UIWorldPortalListing.kt | 16 +- .../modulebasegame/ui/UIWorldPortalShare.kt | 2 + src/net/torvald/terrarum/ui/UICanvas.kt | 9 +- src/net/torvald/terrarum/ui/UIHandler.kt | 11 + src/net/torvald/terrarum/ui/UIItem.kt | 5 + 27 files changed, 206 insertions(+), 181 deletions(-) diff --git a/src/net/torvald/terrarum/IngameInstance.kt b/src/net/torvald/terrarum/IngameInstance.kt index 5f1926cb1..9275b8bce 100644 --- a/src/net/torvald/terrarum/IngameInstance.kt +++ b/src/net/torvald/terrarum/IngameInstance.kt @@ -533,7 +533,7 @@ open class IngameInstance(val batch: FlippingSpriteBatch, val isMultiplayer: Boo fun getActorsAt(worldX: Double, worldY: Double): List { val outList = ArrayList() try { - actorsRTree.find(worldX, worldY, worldX, worldY, outList) + actorsRTree.find(worldX - 0.5, worldY - 0.5, worldX + 0.5, worldY + 0.5, outList) } catch (e: NullPointerException) {} return outList diff --git a/src/net/torvald/terrarum/gameactors/WireActor.kt b/src/net/torvald/terrarum/gameactors/WireActor.kt index 52ef5f131..8d875db8f 100644 --- a/src/net/torvald/terrarum/gameactors/WireActor.kt +++ b/src/net/torvald/terrarum/gameactors/WireActor.kt @@ -7,11 +7,16 @@ import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE import net.torvald.terrarum.gameitems.ItemID /** - * FIXME Constructor is super expensive - * + * Created by minjaesong on 2024-03-05. + */ +interface InternalActor { + +} + +/** * Created by minjaesong on 2021-07-30. */ -class WireActor : ActorWithBody, NoSerialise { +class WireActor : ActorWithBody, NoSerialise, InternalActor { companion object { val WIRE_NEARBY = arrayOf( diff --git a/src/net/torvald/terrarum/gamecontroller/IngameController.kt b/src/net/torvald/terrarum/gamecontroller/IngameController.kt index 2fd23895d..c24e837ef 100644 --- a/src/net/torvald/terrarum/gamecontroller/IngameController.kt +++ b/src/net/torvald/terrarum/gamecontroller/IngameController.kt @@ -208,7 +208,7 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() { } } - terrarumIngame.uiContainer.forEach { it?.keyDown(keycode) } // for KeyboardControlled UIcanvases + terrarumIngame.uiContainer.forEach { if (it?.justOpened == false) it.keyDown(keycode) } // for KeyboardControlled UIcanvases // Debug UIs if (keycode == Input.Keys.GRAVE) { @@ -233,7 +233,7 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() { terrarumIngame.uiQuickBar.setAsOpen() } - terrarumIngame.uiContainer.forEach { it?.keyUp(keycode) } // for KeyboardControlled UIcanvases + terrarumIngame.uiContainer.forEach { if (it?.justOpened == false) it.keyUp(keycode) } // for KeyboardControlled UIcanvases // screenshot key if (keycode == Input.Keys.F12) f12Down = false @@ -243,27 +243,25 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() { } override fun keyTyped(character: Char): Boolean { - terrarumIngame.uiContainer.forEach { if (it?.isVisible == true) it.keyTyped(character) } + terrarumIngame.uiContainer.forEach { if (it?.justOpened == false && it.isVisible) it.keyTyped(character) } return true } 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 && !terrarumIngame.playerControlDisabled) { - // fire world click events; the event is defined as Ingame's (or any others') WorldClick event - if (terrarumIngame.uiContainer.map { if ((it?.isOpening == true || it?.isOpened == true) && it.mouseUp) 1 else 0 }.sum() == 0) { // no UI on the mouse, right? + // disable the IFs: the "unlatching" must happen no matter what, even if a UI is been opened - if ( - terrarumIngame.actorNowPlaying != null && - (button == App.getConfigInt("config_mouseprimary") || - button == App.getConfigInt("config_mousesecondary"))) { +// if (!terrarumIngame.paused && !terrarumIngame.playerControlDisabled) { + // fire world click events; the event is defined as Ingame's (or any others') WorldClick event +// if (terrarumIngame.uiContainer.map { if ((it?.isOpening == true || it?.isOpened == true) && it.mouseUp) 1 else 0 }.sum() == 0) { // no UI on the mouse, right? + + if (button == App.getConfigInt("config_mouseprimary")) { terrarumIngame.worldPrimaryClickEnd(terrarumIngame.actorNowPlaying!!, App.UPDATE_RATE) } if (button == App.getConfigInt("config_mousesecondary")) { terrarumIngame.worldSecondaryClickEnd(terrarumIngame.actorNowPlaying!!, App.UPDATE_RATE) } - } - } +// } +// } // pie menu if (button == App.getConfigInt("control_mouse_quicksel")) { @@ -271,7 +269,7 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() { terrarumIngame.uiQuickBar.setAsOpen() } - terrarumIngame.uiContainer.forEach { it?.touchUp(screenX, screenY, pointer, button) } // for MouseControlled UIcanvases + terrarumIngame.uiContainer.forEach { if (it?.justOpened == false) it.touchUp(screenX, screenY, pointer, button) } // for MouseControlled UIcanvases return true } @@ -289,17 +287,17 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() { it.actorValue.set(AVKey.__PLAYER_QUICKSLOTSEL, selection fmod UIQuickslotBar.SLOT_COUNT) } } - terrarumIngame.uiContainer.forEach { it?.scrolled(amountX, amountY) } + terrarumIngame.uiContainer.forEach { if (it?.justOpened == false) it?.scrolled(amountX, amountY) } return true } private fun tTouchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean { - terrarumIngame.uiContainer.forEach { it?.touchDragged(screenX, screenY, pointer) } + terrarumIngame.uiContainer.forEach { if (it?.justOpened == false) it.touchDragged(screenX, screenY, pointer) } return true } private fun tTouchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { - terrarumIngame.uiContainer.forEach { it?.touchDown(screenX, screenY, pointer, button) } + terrarumIngame.uiContainer.forEach { if (it?.justOpened == false) it.touchDown(screenX, screenY, pointer, button) } // pie menu if (button == App.getConfigInt("control_mouse_quicksel")) { diff --git a/src/net/torvald/terrarum/gameitems/GameItem.kt b/src/net/torvald/terrarum/gameitems/GameItem.kt index abf166f7f..d171aeaf8 100644 --- a/src/net/torvald/terrarum/gameitems/GameItem.kt +++ b/src/net/torvald/terrarum/gameitems/GameItem.kt @@ -256,8 +256,6 @@ abstract class GameItem(val originalID: ItemID) : Comparable, Cloneabl open fun startPrimaryUse(actor: ActorWithBody, delta: Float): Long = -1 /** - * I have decided that left and right clicks must do the same thing, so no secondary use from now on. --Torvald on 2019-05-26 - * * Apply effects (continuously or not) while secondary button is down * The item will NOT be consumed, so you will want to consume it yourself by inventory.consumeItem(item) * @@ -267,10 +265,10 @@ abstract class GameItem(val originalID: ItemID) : Comparable, Cloneabl * * note: DO NOT super() this! */ - //open fun startSecondaryUse(delta: Float): Boolean = false + open fun startSecondaryUse(actor: ActorWithBody, delta: Float): Long = -1 open fun endPrimaryUse(actor: ActorWithBody, delta: Float): Boolean = false - //open fun endSecondaryUse(actor: ActorWithBody, delta: Float): Boolean = false + open fun endSecondaryUse(actor: ActorWithBody, delta: Float): Boolean = false /** * Effects applied immediately only once when thrown (discarded) from pocket diff --git a/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt b/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt index e3e237feb..6e9b4b381 100644 --- a/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt +++ b/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt @@ -691,6 +691,7 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) { + private var worldPrimaryClickLatch = false // left click: use held item, attack, pick up fixture if i'm holding a pickaxe or hammer (aka tool), do 'bare hand action' if holding nothing override fun worldPrimaryClickStart(actor: ActorWithBody, delta: Float) { //println("[Ingame] worldPrimaryClickStart $delta") @@ -699,6 +700,12 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) { val itemOnGrip = ItemCodex[(actor as Pocketed).inventory.itemEquipped.get(GameItem.EquipPosition.HAND_GRIP)] // bring up the UIs of the fixtures (e.g. crafting menu from a crafting table) + var uiOpened = false + val fixtureUnderMouse0: List = getActorsUnderMouse(Terrarum.mouseX, Terrarum.mouseY).filterIsInstance() + if (fixtureUnderMouse0.size > 1) { + App.printdbgerr(this, "Multiple fixtures at world coord ${Terrarum.mouseX}, ${Terrarum.mouseY}") + } + val fixtureUnderMouse = fixtureUnderMouse0.firstOrNull() //////////////////////////////// @@ -708,12 +715,41 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) { val consumptionSuccessful = itemOnGrip.startPrimaryUse(actor, delta) if (consumptionSuccessful > -1) (actor as Pocketed).inventory.consumeItem(itemOnGrip, consumptionSuccessful) + + worldPrimaryClickLatch = true } - // #2. If I'm not holding any item and I can do barehandaction (size big enough that barehandactionminheight check passes), perform it - else if (itemOnGrip == null) { + else { mouseInInteractableRange(actor) { mwx, mwy, mtx, mty -> - performBarehandAction(actor, delta, mwx, mwy, mtx, mty) - 0L + // #2. interact with the fixture + // scan for the one with non-null UI. + // what if there's multiple of such fixtures? whatever, you are supposed to DISALLOW such situation. + if (fixtureUnderMouse != null) { + if (!worldPrimaryClickLatch) { + worldPrimaryClickLatch = true + fixtureUnderMouse.let { fixture -> + fixture.mainUI?.let { ui -> + uiOpened = true + + // property 'uiFixture' is a dedicated property that the TerrarumIngame recognises. + // when it's not null, the UI will be updated and rendered + // when the UI is closed, it'll be replaced with a null value + uiFixture = ui + ui.setPosition( + (Toolkit.drawWidth - ui.width) / 4, + (App.scr.height - ui.height) / 4 // what the fuck? + ) + ui.setAsOpen() + } + } + } + 0L + } + // #3. If not holding any item and can do barehandaction (size big enough that barehandactionminheight check passes), do it + else { + performBarehandAction(actor, delta, mwx, mwy, mtx, mty) + 0L + } + } } } @@ -728,46 +764,26 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) { if (canPerformBarehandAction) { endPerformBarehandAction(actor) } + + worldPrimaryClickLatch = false } - // right click: use fixture override fun worldSecondaryClickStart(actor: ActorWithBody, delta: Float) { val itemOnGrip = ItemCodex[(actor as Pocketed).inventory.itemEquipped.get(GameItem.EquipPosition.HAND_GRIP)] - var uiOpened = false - val actorsUnderMouse: List = getActorsAt(Terrarum.mouseX, Terrarum.mouseY).filterIsInstance() - if (actorsUnderMouse.size > 1) { - App.printdbgerr(this, "Multiple fixtures at world coord ${Terrarum.mouseX}, ${Terrarum.mouseY}") + + // #1. If ~~there is no UI under and~~ I'm holding an item, use it + // don't want to open the UI and use the item at the same time, would ya? + if (itemOnGrip != null) { + val consumptionSuccessful = itemOnGrip.startSecondaryUse(actor, delta) + if (consumptionSuccessful > -1) + (actor as Pocketed).inventory.consumeItem(itemOnGrip, consumptionSuccessful) } - - // #1. Try to open a UI under the cursor - // scan for the one with non-null UI. - // what if there's multiple of such fixtures? whatever, you are supposed to DISALLOW such situation. - for (kk in actorsUnderMouse.indices) { - if (mouseInInteractableRange(actor) { _, _, _, _ -> - actorsUnderMouse[kk].let { fixture -> - fixture.mainUI?.let { ui -> - uiOpened = true - - // property 'uiFixture' is a dedicated property that the TerrarumIngame recognises. - // when it's not null, the UI will be updated and rendered - // when the UI is closed, it'll be replaced with a null value - uiFixture = ui - ui.setPosition( - (Toolkit.drawWidth - ui.width) / 4, - (App.scr.height - ui.height) / 4 // what the fuck? - ) -// if (fixture.mainUIopenFun == null) - ui.setAsOpen() -// else -// fixture.mainUIopenFun!!.invoke(ui) - } - } + // #2. Try to pick up the fixture + else { + mouseInInteractableRange(actor) { mwx, mwy, mtx, mty -> + pickupFixture(actor, delta, mwx, mwy, mtx, mty) 0L - } == 0L) break - } - - if (!uiOpened) { - //... + } } } @@ -1546,8 +1562,20 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) { } } - fun performBarehandAction(actor: ActorWithBody, delta: Float, mwx: Double, mwy: Double, mtx: Int, mty: Int) { + private fun getActorsAtVicinity(worldX: Double, worldY: Double, radius: Double): List { + val outList = java.util.ArrayList() + try { + actorsRTree.find(worldX - radius, worldY - radius, worldX + radius, worldY + radius, outList) + } + catch (e: NullPointerException) { + } + return outList + } + private fun getPunchSize(actor: ActorWithBody) = actor.scale * actor.actorValue.getAsDouble(AVKey.BAREHAND_BASE_DIGSIZE)!! + + + fun performBarehandAction(actor: ActorWithBody, delta: Float, mwx: Double, mwy: Double, mtx: Int, mty: Int) { // for giant actors punching every structure pickaxe can dig out val canAttackOrDig = actor.scale * actor.baseHitboxH >= (actor.actorValue.getAsDouble(AVKey.BAREHAND_MINHEIGHT) ?: 4294967296.0) @@ -1556,62 +1584,15 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) { val canDigSoftTileOnly = actor is ActorHumanoid && (actor.baseHitboxH * actor.scale) >= 32f - fun getActorsAtVicinity(worldX: Double, worldY: Double, radius: Double): List { - val outList = java.util.ArrayList() - try { - actorsRTree.find(worldX - radius, worldY - radius, worldX + radius, worldY + radius, outList) - } - catch (e: NullPointerException) { - } - return outList - } - - val punchSize = actor.scale * actor.actorValue.getAsDouble(AVKey.BAREHAND_BASE_DIGSIZE)!! + val punchSize = getPunchSize(actor) val punchBlockSize = punchSize.div(TILE_SIZED).floorToInt() val mouseUnderPunchableTree = BlockCodex[world.getTileFromTerrain(mtx, mty)].hasAnyTagOf("LEAVES", "TREESMALL") - // if there are attackable actor or fixtures - val actorsUnderMouse: List = getActorsAtVicinity(mwx, mwy, punchSize / 2.0).sortedBy { - (mwx - it.hitbox.centeredX).sqr() + (mwy - it.hitbox.centeredY).sqr() - } // sorted by the distance from the mouse - // prioritise actors - val fixturesUnderHand = ArrayList() - val mobsUnderHand = ArrayList() - actorsUnderMouse.forEach { - if (it is FixtureBase) // && it.mainUI == null) // pickup avail check must be done against fixture.canBeDespawned - fixturesUnderHand.add(it) - else if (it !is FixtureBase) - mobsUnderHand.add(it) - } - - // pickup a fixture - if (fixturesUnderHand.size > 0 && fixturesUnderHand[0].canBeDespawned && - System.nanoTime() - fixturesUnderHand[0].spawnRequestedTime > 500000000) { // don't pick up the fixture if it was recently placed (0.5 seconds) - val fixture = fixturesUnderHand[0] - val fixtureItem = ItemCodex.fixtureToItemID(fixture) - printdbg(this, "Fixture pickup at F${WORLD_UPDATE_TIMER}: ${fixture.javaClass.canonicalName} -> $fixtureItem") - // 0. hide tooltips - setTooltipMessage(null) - // 1. put the fixture to the inventory - fixture.flagDespawn() - // 2. register this item(fixture) to the quickslot so that the player sprite would be actually lifting the fixture - if (actor is Pocketed) { - actor.inventory.add(fixtureItem) - actor.equipItem(fixtureItem) - actor.inventory.setQuickslotItemAtSelected(fixtureItem) - // 2-1. unregister if other slot has the same item - for (k in 0..9) { - if (actor.inventory.getQuickslotItem(k)?.itm == fixtureItem && k != actor.actorValue.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL)) { - actor.inventory.setQuickslotItem(k, null) - } - } - } - } // punch a small tree/shrub - else if (mouseUnderPunchableTree) { + if (mouseUnderPunchableTree) { barehandAxeInUse = true AxeCore.startPrimaryUse(actor, delta, null, mtx, mty, punchBlockSize.coerceAtLeast(1), punchBlockSize.coerceAtLeast(1), listOf("TREESMALL")) } @@ -1634,6 +1615,55 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) { } } + private fun getActorsUnderMouse(mwx: Double, mwy: Double): List { + val actorsUnderMouse: List = getActorsAt(mwx, mwy).filter { it !is InternalActor }.sortedBy { + (mwx - it.hitbox.centeredX).sqr() + (mwy - it.hitbox.centeredY).sqr() + } // sorted by the distance from the mouse + return actorsUnderMouse + } + + fun pickupFixture(actor: ActorWithBody, delta: Float, mwx: Double, mwy: Double, mtx: Int, mty: Int, assignToQuickslot: Boolean = true) { + printdbg(this, "Pickup fixture fired") + + val nearestActorUnderMouse = getActorsUnderMouse(mwx, mwy).firstOrNull() + + // pickup a fixture + if (nearestActorUnderMouse != null && nearestActorUnderMouse is FixtureBase && nearestActorUnderMouse.canBeDespawned && + System.nanoTime() - nearestActorUnderMouse.spawnRequestedTime > 500000000) { // don't pick up the fixture if it was recently placed (0.5 seconds) + val fixture = nearestActorUnderMouse + val fixtureItem = ItemCodex.fixtureToItemID(fixture) + printdbg(this, "Fixture pickup at F${WORLD_UPDATE_TIMER}: ${fixture.javaClass.canonicalName} -> $fixtureItem") + // 0. hide tooltips + setTooltipMessage(null) + if (!fixture.flagDespawn) { + // 1. put the fixture to the inventory + fixture.flagDespawn() + // 2. register this item(fixture) to the quickslot so that the player sprite would be actually lifting the fixture + if (actor is Pocketed) { + actor.inventory.add(fixtureItem) + + if (assignToQuickslot) { + actor.equipItem(fixtureItem) + actor.inventory.setQuickslotItemAtSelected(fixtureItem) + // 2-1. unregister if other slot has the same item + for (k in 0..9) { + if (actor.inventory.getQuickslotItem(k)?.itm == fixtureItem && k != actor.actorValue.getAsInt( + AVKey.__PLAYER_QUICKSLOTSEL + ) + ) { + actor.inventory.setQuickslotItem(k, null) + } + } + } + } + } + } + else if (nearestActorUnderMouse != null && nearestActorUnderMouse.canBeDespawned) { + + } + // TODO pickup a mob + } + override fun hide() { uiContainer.forEach { it?.handler?.dispose() } } diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureMusicalTurntable.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureMusicalTurntable.kt index bfd309a0f..857e0b9c2 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureMusicalTurntable.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureMusicalTurntable.kt @@ -65,7 +65,7 @@ class FixtureMusicalTurntable : Electric, PlaysMusic { internal var disc: ItemID? = null - @Transient private val clickLatch = MouseLatch(listOf(App.getConfigInt("config_mousesecondary"))) + @Transient private val clickLatch = MouseLatch() override val canBeDespawned: Boolean get() = disc == null diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalSwitchManual.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalSwitchManual.kt index de0e1e12c..0bdcca266 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalSwitchManual.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalSwitchManual.kt @@ -59,7 +59,7 @@ class FixtureSignalSwitchManual : Electric { setWireEmissionAt(0, 0, Vector2(state.toInt().toDouble(), 0.0)) } - @Transient private val clickLatch = MouseLatch(listOf(App.getConfigInt("config_mousesecondary"))) + @Transient private val clickLatch = MouseLatch() override fun updateImpl(delta: Float) { super.updateImpl(delta) diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/FixtureItemBase.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/FixtureItemBase.kt index 69dd54f3f..20f12765d 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameitems/FixtureItemBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/FixtureItemBase.kt @@ -104,6 +104,11 @@ open class FixtureItemBase(originalID: ItemID, val fixtureClassName: String) : G // return true when placed, false when cannot be placed } + override fun startSecondaryUse(actor: ActorWithBody, delta: Float) = mouseInInteractableRange(actor) { mwx, mwy, mtx, mty -> + (INGAME as TerrarumIngame).pickupFixture(actor, delta, mwx, mwy, mtx, mty, false) + -1 + } + /** * Also see: [net.torvald.terrarum.modulebasegame.gameactors.FixtureBase.Companion] */ diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UICraftingWorkbench.kt b/src/net/torvald/terrarum/modulebasegame/ui/UICraftingWorkbench.kt index 2cf0e5ca0..a666d1750 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UICraftingWorkbench.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UICraftingWorkbench.kt @@ -446,8 +446,6 @@ class UICraftingWorkbench(val inventoryUI: UIInventoryFull?, val parentContainer itemListCraftable.numberMultiplier = 1L } - private var openingClickLatched = false - override fun show() { nearbyCraftingStations = getCraftingStationsWithinReach() // printdbg(this, "Nearby crafting stations: $nearbyCraftingStations") @@ -455,7 +453,7 @@ class UICraftingWorkbench(val inventoryUI: UIInventoryFull?, val parentContainer playerThings.setGetInventoryFun { INGAME.actorNowPlaying!!.inventory } itemListUpdate() - openingClickLatched = Terrarum.mouseDown + super.show() tooltipShowing.clear() INGAME.setTooltipMessage(null) @@ -472,18 +470,9 @@ class UICraftingWorkbench(val inventoryUI: UIInventoryFull?, val parentContainer encumbrancePerc = getPlayerInventory().encumberment.toFloat() } - override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { - if (!openingClickLatched) { - return super.touchDown(screenX, screenY, pointer, button) - } - return false - } - override fun updateImpl(delta: Float) { // NO super.update due to an infinite recursion this.uiItems.forEach { it.update(delta) } - - if (openingClickLatched && !Terrarum.mouseDown) openingClickLatched = false } override fun renderImpl(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt index e71d7edf3..e3f3ec2e8 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt @@ -180,6 +180,7 @@ internal class UIInventoryCells( } override fun show() { + super.show() tooltipShowing.clear() INGAME.setTooltipMessage(null) } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt index f9bb0c1b8..8c2359c1c 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt @@ -304,6 +304,7 @@ class UIInventoryEscMenu(val full: UIInventoryFull) : UICanvas() { ) override fun show() { + super.show() tooltipShowing.clear() INGAME.setTooltipMessage(null) toInitScreen() diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt index 46571da64..b2dd93ce5 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt @@ -323,15 +323,11 @@ class UIInventoryFull( } override fun show() { - transitionPanel.show() + super.show() tooltipShowing.clear() INGAME.setTooltipMessage(null) } - override fun hide() { - transitionPanel.hide() - } - internal var offsetX = ((width - internalWidth) / 2).toFloat() private set internal var offsetY = ((App.scr.height - internalHeight) / 2).toFloat() diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt index 142dd1279..4c88dcb0b 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryCellBase.kt @@ -65,7 +65,7 @@ abstract class UIItemInventoryCellBase( } override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { - if (mouseUp) { + if (mouseUp && !parentUI.openingClickLatched) { touchDownFun(item, amount, button, extraInfo, this) super.touchDown(screenX, screenY, pointer, button) } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIJukebox.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIJukebox.kt index 8582644ea..710da968b 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIJukebox.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIJukebox.kt @@ -74,23 +74,20 @@ class UIJukebox : UICanvas( addUIitem(transitionPanel) } - private var openingClickLatched = false - override fun show() { - openingClickLatched = Terrarum.mouseDown + super.show() transitionPanel.show() tooltipShowing.clear() INGAME.setTooltipMessage(null) } override fun hide() { + super.hide() transitionPanel.hide() } override fun updateImpl(delta: Float) { uiItems.forEach { it.update(delta) } - - if (openingClickLatched && !Terrarum.mouseDown) openingClickLatched = false } override fun renderImpl(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera) { @@ -108,10 +105,7 @@ class UIJukebox : UICanvas( override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { - if (!openingClickLatched) { - return super.touchDown(screenX, screenY, pointer, button) - } - return false + return super.touchDown(screenX, screenY, pointer, button) } override fun doOpening(delta: Float) { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UILoadList.kt b/src/net/torvald/terrarum/modulebasegame/ui/UILoadList.kt index 1ede80f92..8f5bd06f1 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UILoadList.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UILoadList.kt @@ -141,6 +141,8 @@ class UILoadList(val full: UILoadSavegame) : UICanvas() { } catch (e: UninitializedPropertyAccessException) { } + + super.show() } } @@ -299,6 +301,7 @@ class UILoadList(val full: UILoadSavegame) : UICanvas() { } override fun hide() { + super.hide() showCalled = false cellLoadThread.interrupt() } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UILoadSavegame.kt b/src/net/torvald/terrarum/modulebasegame/ui/UILoadSavegame.kt index 71fe5decc..b4d972842 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UILoadSavegame.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UILoadSavegame.kt @@ -132,6 +132,7 @@ class UILoadSavegame(val remoCon: UIRemoCon) : Advanceable() { override fun show() { // takeAutosaveSelectorDown() + super.show() transitionPanel.show() nodesForListing.parent = remoCon.treeRoot @@ -140,6 +141,7 @@ class UILoadSavegame(val remoCon: UIRemoCon) : Advanceable() { } override fun hide() { + super.hide() transitionPanel.hide() } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIShare.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIShare.kt index c6fb9d067..79a602b3e 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIShare.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIShare.kt @@ -32,6 +32,8 @@ class UIShare : UICanvas() { private var shareCode = "" override fun show() { + super.show() + shareCode = Common.encodeUUID(INGAME.world.worldIndex) App.printdbg(this, shareCode) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt index 4e347624c..3cada5921 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIStorageChest.kt @@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.* +import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.gameactors.AVKey import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.langpack.Lang @@ -183,15 +184,13 @@ internal class UIStorageChest : UICanvas( addUIitem(itemListPlayer) } - private var openingClickLatched = false - override fun show() { + super.show() + itemListPlayer.itemList.getInventory = { INGAME.actorNowPlaying!!.inventory } itemListUpdate() - openingClickLatched = Terrarum.mouseDown - tooltipShowing.clear() INGAME.setTooltipMessage(null) } @@ -221,18 +220,13 @@ internal class UIStorageChest : UICanvas( } override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { - if (!openingClickLatched) { - return super.touchDown(screenX, screenY, pointer, button) - } - return false + return super.touchDown(screenX, screenY, pointer, button) } override fun updateImpl(delta: Float) { catBar.update(delta) itemListChest.update(delta) itemListPlayer.update(delta) - - if (openingClickLatched && !Terrarum.mouseDown) openingClickLatched = false } private val thisOffsetX = Toolkit.hdrawWidth - getWidthOfCells(6) - halfSlotOffset diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt b/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt index 8cba6b301..fe2223cb0 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UITemplateHalfInventory.kt @@ -70,6 +70,7 @@ class UITemplateHalfInventory( } + fun rebuild(category: Array) { itemList.rebuild(category) } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UITitleLanguage.kt b/src/net/torvald/terrarum/modulebasegame/ui/UITitleLanguage.kt index d9c68bd32..84dfca4a5 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UITitleLanguage.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UITitleLanguage.kt @@ -119,10 +119,12 @@ class UITitleLanguage(remoCon: UIRemoCon?) : UICanvas() { override fun show() { initialMouseBlock = true + super.show() } override fun hide() { initialMouseBlock = true + super.hide() } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortal.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortal.kt index 2297cf203..d37131a5a 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortal.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortal.kt @@ -156,9 +156,8 @@ class UIWorldPortal : UICanvas( } override fun show() { - super.show() transitionPanel.forcePosition(0) - transitionPanel.show() + super.show() INGAME.setTooltipMessage(null) // add current world to the player's worldportaldict @@ -166,10 +165,6 @@ class UIWorldPortal : UICanvas( cleanUpWorldDict() } - override fun hide() { - transitionPanel.hide() - } - override fun dispose() { transitionPanel.dispose() } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt index a4b7d314f..0fac61ea2 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalCargo.kt @@ -139,15 +139,13 @@ class UIWorldPortalCargo(val full: UIWorldPortal) : UICanvas(), HasInventory { addUIitem(itemListPlayer) } - private var openingClickLatched = false - override fun show() { + super.show() + itemListPlayer.getInventory = { INGAME.actorNowPlaying!!.inventory } itemListUpdate() - openingClickLatched = Terrarum.mouseDown - tooltipShowing.clear() INGAME.setTooltipMessage(null) } @@ -177,9 +175,7 @@ class UIWorldPortalCargo(val full: UIWorldPortal) : UICanvas(), HasInventory { } override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { - if (!openingClickLatched) { - return super.touchDown(screenX, screenY, pointer, button) - } + return super.touchDown(screenX, screenY, pointer, button) return false } @@ -187,8 +183,6 @@ class UIWorldPortalCargo(val full: UIWorldPortal) : UICanvas(), HasInventory { catBar.update(delta) itemListChest.update(delta) itemListPlayer.update(delta) - - if (openingClickLatched && !Terrarum.mouseDown) openingClickLatched = false } private val thisOffsetX = Toolkit.hdrawWidth - UIInventoryFull.getWidthOfCells(6) - halfSlotOffset diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalListing.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalListing.kt index 00e4c6533..3756f02e6 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalListing.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalListing.kt @@ -304,6 +304,8 @@ class UIWorldPortalListing(val full: UIWorldPortal) : UICanvas() { private var threadFired = false override fun show() { + super.show() + listPage = 0 showSpinner = true @@ -464,7 +466,7 @@ class UIWorldPortalListing(val full: UIWorldPortal) : UICanvas() { } override fun hide() { - uiItems.forEach { it.hide() } + super.hide() if (::worldCells.isInitialized) worldCells.forEach { it.hide() } if (::worldCells.isInitialized) worldCells.forEach { it.tryDispose() } @@ -554,18 +556,6 @@ class UIItemWorldCellsSimple( var highlighted = false - override fun show() { - super.show() - } - - override fun hide() { - super.hide() - } - - override fun update(delta: Float) { - super.update(delta) - } - fun render(frameDelta: Float, batch: SpriteBatch, camera: OrthographicCamera, offX: Int, offY: Int) { super.render(frameDelta, batch, camera) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalShare.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalShare.kt index 147ccf5f0..7716313ea 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalShare.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalShare.kt @@ -50,6 +50,8 @@ class UIWorldPortalShare(private val full: UIWorldPortal) : UICanvas() { private var shareCode = "" override fun show() { + super.show() + shareCode = Common.encodeUUID(INGAME.world.worldIndex) diff --git a/src/net/torvald/terrarum/ui/UICanvas.kt b/src/net/torvald/terrarum/ui/UICanvas.kt index 607ce82a5..19336570a 100644 --- a/src/net/torvald/terrarum/ui/UICanvas.kt +++ b/src/net/torvald/terrarum/ui/UICanvas.kt @@ -65,6 +65,11 @@ abstract class UICanvas( doNotWarnConstant: Boolean = false ): Disposable { + internal var openingClickLatched = false + + val justOpened: Boolean + get() = handler.justOpened + abstract var width: Int abstract var height: Int @@ -130,6 +135,7 @@ abstract class UICanvas( /** A function that is run ONCE when the UI is requested to be opened; will work identical to [endOpening] if [openCloseTime] is zero */ open fun show() { + openingClickLatched = true uiItems.forEach { it.show() } handler.subUIs.forEach { it.show() } } @@ -137,6 +143,7 @@ abstract class UICanvas( open fun hide() { uiItems.forEach { it.hide() } handler.subUIs.forEach { it.hide() } + openingClickLatched = true // just in case `justOpened` detection fails } @@ -208,7 +215,7 @@ abstract class UICanvas( } /** Called by the screen's InputProcessor */ open fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { - if (this.isVisible && mouseInScreen(screenX, screenY)) { + if (this.isVisible && mouseInScreen(screenX, screenY) && !openingClickLatched) { uiItems.forEach { it.touchDown(screenX, screenY, pointer, button) } handler.subUIs.forEach { it.touchDown(screenX, screenY, pointer, button) } return true diff --git a/src/net/torvald/terrarum/ui/UIHandler.kt b/src/net/torvald/terrarum/ui/UIHandler.kt index f032f4493..431494c58 100644 --- a/src/net/torvald/terrarum/ui/UIHandler.kt +++ b/src/net/torvald/terrarum/ui/UIHandler.kt @@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.utils.Disposable import net.torvald.terrarum.* +import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.gamecontroller.KeyToggler import net.torvald.terrarum.modulebasegame.TerrarumIngame @@ -109,6 +110,8 @@ void main() { var openFired = false var closeFired = false + internal var justOpened = true + var opacity = 1f /*set(value) { field = value @@ -227,6 +230,10 @@ void main() { if (isVisible || alwaysUpdate) { ui.updateImpl(delta) + if (ui.openingClickLatched && !Terrarum.mouseDown) { + ui.openingClickLatched = false +// printdbg(this, "UIHandler.update Unlatching openingClick") + } } if (isOpening) { @@ -241,11 +248,13 @@ void main() { if (openCloseCounter < ui.openCloseTime) { ui.doOpening(delta) + justOpened = false // println("UIHandler.opening ${UI.javaClass.simpleName}") } else { ui.doOpening(0f) ui.endOpening(delta) + justOpened = false isOpening = false isClosing = false isOpened = true @@ -347,6 +356,8 @@ void main() { openFired = true openCloseCounter = 0f + + justOpened = true } } diff --git a/src/net/torvald/terrarum/ui/UIItem.kt b/src/net/torvald/terrarum/ui/UIItem.kt index c816b519b..03148c45a 100644 --- a/src/net/torvald/terrarum/ui/UIItem.kt +++ b/src/net/torvald/terrarum/ui/UIItem.kt @@ -155,6 +155,11 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I } mouseOverCall?.updateImpl(delta) + if (mouseOverCall?.openingClickLatched == true && !Terrarum.mouseDown) { + mouseOverCall?.openingClickLatched = false +// App.printdbg(this, "UIItem.update Unlatching openingClick of mouseOverCall") + } + mouseUpListener.invoke(itemRelativeMouseX, itemRelativeMouseY) } else {