From 9326b8790974270a98c2dc731342769c734d72f7 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Wed, 20 Oct 2021 23:00:25 +0900 Subject: [PATCH] text input pane wip --- assets/keylayout/us_colemak.key | 4 +- assets/keylayout/us_qwerty.key | 4 +- src/net/torvald/terrarum/App.java | 12 -- .../torvald/terrarum/FrameBufferManager.java | 1 + src/net/torvald/terrarum/SanicLoadScreen.kt | 7 +- src/net/torvald/terrarum/Terrarum.kt | 17 +-- src/net/torvald/terrarum/UIFakeGradOverlay.kt | 2 +- .../torvald/terrarum/UIItemInventoryCatBar.kt | 7 +- .../terrarum/UIItemInventoryElemSimple.kt | 7 +- .../terrarum/UIItemInventoryElemWide.kt | 15 +-- .../torvald/terrarum/gamecontroller/IME.kt | 20 ++- .../gamecontroller/IngameController.kt | 15 ++- .../terrarum/modulebasegame/IngameRenderer.kt | 9 +- .../gameactors/FixtureStorageChest.kt | 6 +- .../ui/UIBuildingMakerBlockChooser.kt | 16 +-- .../ui/UIBuildingMakerPenMenu.kt | 20 +-- .../modulebasegame/ui/UICheatDetected.kt | 2 +- .../terrarum/modulebasegame/ui/UIInventory.kt | 8 +- .../modulebasegame/ui/UIInventoryCells.kt | 4 +- .../modulebasegame/ui/UIInventoryMinimap.kt | 10 +- .../ui/UIKeyboardControlPanel.kt | 4 + .../modulebasegame/ui/UIPaletteSelector.kt | 8 +- src/net/torvald/terrarum/tests/UIElemTest.kt | 119 ++++++++++++++---- src/net/torvald/terrarum/tests/UITestPad1.kt | 8 +- .../terrarum/ui/BasicDebugInfoWindow.kt | 4 +- src/net/torvald/terrarum/ui/ConsoleWindow.kt | 13 +- src/net/torvald/terrarum/ui/Toolkit.kt | 22 +++- .../torvald/terrarum/ui/UIItemImageButton.kt | 7 +- src/net/torvald/terrarum/ui/UIItemList.kt | 5 +- .../torvald/terrarum/ui/UIItemTextButton.kt | 6 +- .../terrarum/ui/UIItemTextButtonList.kt | 4 +- .../terrarum/ui/UIItemTextLineInput.kt | 82 ++++++++++-- src/net/torvald/terrarum/ui/UINSMenu.kt | 2 +- .../terrarum/worlddrawer/FeaturesDrawer.kt | 13 +- 34 files changed, 320 insertions(+), 163 deletions(-) diff --git a/assets/keylayout/us_colemak.key b/assets/keylayout/us_colemak.key index 0c6c98a82..7378cd097 100644 --- a/assets/keylayout/us_colemak.key +++ b/assets/keylayout/us_colemak.key @@ -1,4 +1,4 @@ -[[""],[undefined], +{"n":"US Colemak","t":[[""],[undefined], [undefined], [""], [undefined], @@ -253,4 +253,4 @@ [undefined], [undefined], ["<:CIRCLE:>"] -] +]} diff --git a/assets/keylayout/us_qwerty.key b/assets/keylayout/us_qwerty.key index 5b0f3f3de..f8d98a552 100644 --- a/assets/keylayout/us_qwerty.key +++ b/assets/keylayout/us_qwerty.key @@ -1,4 +1,4 @@ -[[""],[undefined], +{"n":"US Qwerty","t":[[""],[undefined], [undefined], [""], [undefined], @@ -253,4 +253,4 @@ [undefined], [undefined], ["<:CIRCLE:>"] -] +]} diff --git a/src/net/torvald/terrarum/App.java b/src/net/torvald/terrarum/App.java index 23b457864..61cb149e7 100644 --- a/src/net/torvald/terrarum/App.java +++ b/src/net/torvald/terrarum/App.java @@ -234,9 +234,6 @@ public class App implements ApplicationListener { private static Screen currentScreen; private static LoadScreenBase currentSetLoadScreen; - public static Texture textureWhiteSquare; - public static Texture textureWhiteCircle; - private void initViewPort(int width, int height) { // Set Y to point downwards camera.setToOrtho(true, width, height); // some elements are pre-flipped, while some are not. The statement itself is absolutely necessary to make edge of the screen as the origin @@ -748,8 +745,6 @@ public class App implements ApplicationListener { fontSmallNumbers.dispose(); ItemSlotImageFactory.INSTANCE.dispose(); - textureWhiteSquare.dispose(); - textureWhiteCircle.dispose(); logo.getTexture().dispose(); disposables.forEach((it) -> { @@ -830,13 +825,6 @@ public class App implements ApplicationListener { private void postInit() { Terrarum.initialise(); - - textureWhiteSquare = new Texture(Gdx.files.internal("assets/graphics/ortho_line_tex_2px.tga")); - textureWhiteSquare.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest); - - textureWhiteCircle = new Texture(Gdx.files.internal("assets/graphics/circle_512.tga")); - textureWhiteCircle.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); - TextureRegionPack.Companion.setGlobalFlipY(true); fontSmallNumbers = TinyAlphNum.INSTANCE; diff --git a/src/net/torvald/terrarum/FrameBufferManager.java b/src/net/torvald/terrarum/FrameBufferManager.java index 99d1ebc2f..ce79778f2 100644 --- a/src/net/torvald/terrarum/FrameBufferManager.java +++ b/src/net/torvald/terrarum/FrameBufferManager.java @@ -27,4 +27,5 @@ public class FrameBufferManager { stack.peek().begin(); } } + } diff --git a/src/net/torvald/terrarum/SanicLoadScreen.kt b/src/net/torvald/terrarum/SanicLoadScreen.kt index 39baea595..48e14a377 100644 --- a/src/net/torvald/terrarum/SanicLoadScreen.kt +++ b/src/net/torvald/terrarum/SanicLoadScreen.kt @@ -8,6 +8,7 @@ import com.badlogic.gdx.graphics.glutils.FrameBuffer import com.jme3.math.FastMath import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.modulebasegame.TerrarumIngame +import net.torvald.terrarum.ui.Toolkit /** * Created by minjaesong on 2017-07-13. @@ -141,7 +142,7 @@ object SanicLoadScreen : LoadScreenBase() { // almost black background it.color = Color(0x181818ff) - it.fillRect(0f, 0f, App.scr.wf, App.scr.hf) + Toolkit.fillArea(it, 0, 0, App.scr.width, App.scr.height) it.color = Color.WHITE @@ -184,7 +185,7 @@ object SanicLoadScreen : LoadScreenBase() { // message backgrounds it.color = messageBackgroundColour - it.fillRect(0f, 60f, App.scr.wf, 40f + (messages.size) * App.fontGame.lineHeight) + Toolkit.fillArea(it, 0, 60, App.scr.width, 40 + (messages.size) * App.fontGame.lineHeight.toInt()) // log messages it.color = messageForegroundColour @@ -209,7 +210,7 @@ object SanicLoadScreen : LoadScreenBase() { // message backgrounds it.color = messageBackgroundColour - it.fillRect(0f, 60f, App.scr.wf, 40f + (messages.size) * App.fontGame.lineHeight) + Toolkit.fillArea(it, 0, 60, App.scr.width, 40 + (messages.size) * App.fontGame.lineHeight.toInt()) // log messages it.color = messageForegroundColour diff --git a/src/net/torvald/terrarum/Terrarum.kt b/src/net/torvald/terrarum/Terrarum.kt index 0e054f63d..184aef6f6 100644 --- a/src/net/torvald/terrarum/Terrarum.kt +++ b/src/net/torvald/terrarum/Terrarum.kt @@ -26,9 +26,9 @@ import net.torvald.terrarum.gamecontroller.IngameController import net.torvald.terrarum.gameworld.fmod import net.torvald.terrarum.itemproperties.ItemCodex import net.torvald.terrarum.itemproperties.MaterialCodex -import net.torvald.terrarum.serialise.Common import net.torvald.terrarum.savegame.ByteArray64Reader import net.torvald.terrarum.savegame.DiskSkimmer +import net.torvald.terrarum.serialise.Common import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.worlddrawer.WorldCamera import net.torvald.terrarumsansbitmap.gdx.GameFontBase @@ -358,21 +358,6 @@ inline fun FrameBuffer.inAction(camera: OrthographicCamera?, batch: SpriteBatch? } -// ShapeRenderer alternative for rects -fun SpriteBatch.fillRect(x: Float, y: Float, w: Float, h: Float) { - this.draw(App.textureWhiteSquare, x, y, w, h) -} -fun SpriteBatch.fillCircle(x: Float, y: Float, w: Float, h: Float) { - this.draw(App.textureWhiteCircle, x, y, w, h) -} -fun SpriteBatch.drawStraightLine(x: Float, y: Float, otherEnd: Float, thickness: Float, isVertical: Boolean) { - if (!isVertical) - this.fillRect(x, y, otherEnd - x, thickness) - else - this.fillRect(x, y, thickness, otherEnd - y) -} - - infix fun Color.mul(other: Color): Color = this.cpy().mul(other) infix fun Color.mulAndAssign(other: Color): Color { diff --git a/src/net/torvald/terrarum/UIFakeGradOverlay.kt b/src/net/torvald/terrarum/UIFakeGradOverlay.kt index 9df20ddda..40106323b 100644 --- a/src/net/torvald/terrarum/UIFakeGradOverlay.kt +++ b/src/net/torvald/terrarum/UIFakeGradOverlay.kt @@ -89,7 +89,7 @@ class UIFakeBlurOverlay(val blurRadius: Float, val nodarken: Boolean) : UICanvas if (!nodarken) { blendMul(batch) batch.color = darken - batch.fillRect(0f, 0f, width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, 0, 0, width, height) blendNormal(batch) } diff --git a/src/net/torvald/terrarum/UIItemInventoryCatBar.kt b/src/net/torvald/terrarum/UIItemInventoryCatBar.kt index b068d4635..6a0f60b53 100644 --- a/src/net/torvald/terrarum/UIItemInventoryCatBar.kt +++ b/src/net/torvald/terrarum/UIItemInventoryCatBar.kt @@ -6,10 +6,7 @@ import com.badlogic.gdx.graphics.Pixmap import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.gameitem.GameItem -import net.torvald.terrarum.ui.Movement -import net.torvald.terrarum.ui.UICanvas -import net.torvald.terrarum.ui.UIItem -import net.torvald.terrarum.ui.UIItemImageButton +import net.torvald.terrarum.ui.* import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack import kotlin.math.roundToInt @@ -270,7 +267,7 @@ class UIItemInventoryCatBar( // underline batch.color = underlineColour - batch.drawStraightLine(posX.toFloat(), posY + height - 1f, posX + width.toFloat(), 1f, false) + Toolkit.drawStraightLine(batch, posX, posY + height - 1, posX + width, 1, false) // indicator if (selectedPanel == 1) { diff --git a/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt b/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt index 0f874f7d0..e1029ecec 100644 --- a/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt +++ b/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt @@ -12,6 +12,7 @@ import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.Toolkit.DEFAULT_BOX_BORDER_COL import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UIItemTextButton +import kotlin.math.roundToInt /** * Created by minjaesong on 2017-10-20. @@ -92,16 +93,16 @@ class UIItemInventoryElemSimple( if (item!!.maxDurability > 0.0) { // draw durability metre val barFullLen = width - val barOffset = posX.toFloat() + val barOffset = posX val thickness = UIItemInventoryElemWide.durabilityBarThickness val percentage = item!!.durability / item!!.maxDurability val durabilityCol = UIItemInventoryCellCommonRes.getHealthMeterColour(percentage, 0f, 1f) val durabilityBack = durabilityCol mul UIItemInventoryCellCommonRes.meterBackDarkening if (item!!.maxDurability > 0.0) { batch.color = durabilityBack - batch.drawStraightLine(barOffset, posY + height - thickness, barOffset + barFullLen, thickness, false) + Toolkit.drawStraightLine(batch, barOffset, posY + height - thickness, barOffset + barFullLen, thickness, false) batch.color = durabilityCol - batch.drawStraightLine(barOffset, posY + height - thickness, barOffset + barFullLen * percentage, thickness, false) + Toolkit.drawStraightLine(batch, barOffset, posY + height - thickness, barOffset + (barFullLen * percentage).roundToInt(), thickness, false) } } // draw item count when applicable diff --git a/src/net/torvald/terrarum/UIItemInventoryElemWide.kt b/src/net/torvald/terrarum/UIItemInventoryElemWide.kt index 2cc183d00..eca96a076 100644 --- a/src/net/torvald/terrarum/UIItemInventoryElemWide.kt +++ b/src/net/torvald/terrarum/UIItemInventoryElemWide.kt @@ -13,6 +13,7 @@ import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.Toolkit.DEFAULT_BOX_BORDER_COL import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UIItemTextButton +import kotlin.math.roundToInt /*** * Note that the UI will not render if either item or itemImage is null. @@ -44,7 +45,7 @@ class UIItemInventoryElemWide( val height = 48 val UNIQUE_ITEM_HAS_NO_AMOUNT = -1 - internal val durabilityBarThickness = 3f + internal val durabilityBarThickness = 3 } override val height = UIItemInventoryElemWide.height @@ -59,7 +60,7 @@ class UIItemInventoryElemWide( - private val durabilityBarOffY = 35f + private val durabilityBarOffY = 35 @@ -125,16 +126,16 @@ class UIItemInventoryElemWide( // durability metre - val barFullLen = (width - 8f) - textOffsetX - val barOffset = posX + textOffsetX + val barFullLen = (width - 8) - textOffsetX.toInt() + val barOffset = posX + textOffsetX.toInt() val percentage = if (item!!.maxDurability < 0.00001f) 0f else item!!.durability / item!!.maxDurability val durabilityCol = UIItemInventoryCellCommonRes.getHealthMeterColour(percentage, 0f, 1f) val durabilityBack = durabilityCol mul UIItemInventoryCellCommonRes.meterBackDarkening if (item!!.maxDurability > 0.0) { batch.color = durabilityBack - batch.drawStraightLine(barOffset, posY + durabilityBarOffY, barOffset + barFullLen, durabilityBarThickness, false) + Toolkit.drawStraightLine(batch, barOffset, posY + durabilityBarOffY, barOffset + barFullLen, durabilityBarThickness, false) batch.color = durabilityCol - batch.drawStraightLine(barOffset, posY + durabilityBarOffY, barOffset + barFullLen * percentage, durabilityBarThickness, false) + Toolkit.drawStraightLine(batch, barOffset, posY + durabilityBarOffY, barOffset + (barFullLen * percentage).roundToInt(), durabilityBarThickness, false) } @@ -144,7 +145,7 @@ class UIItemInventoryElemWide( if (quickslot != null) { val label = quickslot!!.plus(0xE010).toChar() val labelW = App.fontGame.getWidth("$label") - App.fontGame.draw(batch, "$label", barOffset + barFullLen - labelW, posY + textOffsetY) + App.fontGame.draw(batch, "$label", barOffset + barFullLen - labelW.toFloat(), posY + textOffsetY) } } diff --git a/src/net/torvald/terrarum/gamecontroller/IME.kt b/src/net/torvald/terrarum/gamecontroller/IME.kt index 9f9adfc5e..f78c678f0 100644 --- a/src/net/torvald/terrarum/gamecontroller/IME.kt +++ b/src/net/torvald/terrarum/gamecontroller/IME.kt @@ -1,11 +1,18 @@ package net.torvald.terrarum.gamecontroller import com.badlogic.gdx.Gdx -import net.torvald.terrarum.App.printdbg -typealias TerrarumKeyLayout = Array> +data class TerrarumKeyLayout( + val name: String, + val symbols: Array>?, + val acceptChar: ((Int) -> String?)? = null +) /** + * Key Layout File Structure for Low Layer: + * - n: Displayed name of the keyboard layout + * - t: Key symbols in 256R4C string array (C1: unshifted, C2: Shift, C3: AltGr, C4: Shift-AltGr) + * * IME consists of two keyboard layers: * - Low layer: "english" keyboard (qwerty, colemak, etc), stateless * - High layer: chinese/japanese/korean/etc. keyboard, stateful @@ -36,10 +43,11 @@ object IME { private fun parseKeylayoutFile(path: String): TerrarumKeyLayout { val file = Gdx.files.internal(path) val src = file.readString("UTF-8") - val jsval = context.eval("js", src) + val jsval = context.eval("js", "let t=$src;Object.freeze(t)") + val name = jsval.getMember("n").asString() val out = Array(256) { Array(4) { null } } for (keycode in 0L until 256L) { - val a = jsval.getArrayElement(keycode) + val a = jsval.getMember("t").getArrayElement(keycode) if (!a.isNull) { for (layer in 0L until 4L) { if (a.arraySize > layer) { @@ -52,9 +60,9 @@ object IME { } } - //println("[IME] Test Keymap print:"); for (keycode in 0 until 256) { print("$keycode:\t"); println(out[keycode].joinToString("\t")) } +// println("[IME] Test Keymap print for $name:"); for (keycode in 0 until 256) { print("$keycode:\t"); println(out[keycode].joinToString("\t")) } - return out + return TerrarumKeyLayout(name, out) } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/gamecontroller/IngameController.kt b/src/net/torvald/terrarum/gamecontroller/IngameController.kt index 32a27e7c7..6f70b4241 100644 --- a/src/net/torvald/terrarum/gamecontroller/IngameController.kt +++ b/src/net/torvald/terrarum/gamecontroller/IngameController.kt @@ -283,13 +283,19 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() { private const val KEY_DOWN = 0 private const val KEY_CHANGE = 1 const val N_KEY_ROLLOVER = 8 - var KEYBOARD_DELAYS = floatArrayOf(0.25f, 0.025f) + var KEYBOARD_DELAYS = longArrayOf(0L,250000000L,0L,25000000L,0L) private var stroboTime = 0L private var stroboStatus = 0 private var repeatCount = 0 private var oldKeys = IntArray(N_KEY_ROLLOVER) { 0 } + /** always Low Layer */ private var keymap = IME.getLowLayerByName(App.getConfigString("basekeyboardlayout")) + fun resetKeyboardStrobo() { + stroboStatus = 0 + repeatCount = 0 + } + // code proudly stolen from tsvm's TVDOS.SYS fun withKeyboardEvent(callback: (TerrarumKeyboardEvent) -> Unit) { val keys = strobeKeys() @@ -337,8 +343,9 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() { } private fun keysToStr(keys: IntArray): Array? { + if (keys.size == 0) return null val headkey = keys[0] - return if (keymap[headkey] == null) null else keymap[headkey] + return if (keymap.symbols!![headkey] == null) null else keymap.symbols!![headkey] } private fun strobeKeys(): IntArray { @@ -356,8 +363,8 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() { } private fun arrayEq(a: IntArray, b: IntArray): Boolean { - for (i in 0..a.size) { - if (a[i] != b[i]) return false + for (i in 0 until a.size) { + if (a[i] != b.getOrNull(i)) return false } return true } diff --git a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt index 77b611853..53535d1dc 100644 --- a/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt +++ b/src/net/torvald/terrarum/modulebasegame/IngameRenderer.kt @@ -18,6 +18,7 @@ import net.torvald.terrarum.gamecontroller.KeyToggler import net.torvald.terrarum.gameparticles.ParticleBase import net.torvald.terrarum.gameworld.GameWorld import net.torvald.terrarum.gameworld.fmod +import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.weather.WeatherMixer import net.torvald.terrarum.worlddrawer.BlocksDrawer import net.torvald.terrarum.worlddrawer.FeaturesDrawer @@ -295,11 +296,11 @@ object IngameRenderer : Disposable { // indicator batch.color = Color.RED - batch.fillRect(0f, 0f, 6f, 10f) + Toolkit.fillArea(batch, 0, 0, 6, 10) batch.color = Color.LIME - batch.fillRect(6f, 0f, 6f, 10f) + Toolkit.fillArea(batch, 6, 0, 6, 10) batch.color = Color.ROYAL - batch.fillRect(12f, 0f, 6f, 10f) + Toolkit.fillArea(batch, 12, 0, 6, 10) batch.color = Color.WHITE } @@ -322,7 +323,7 @@ object IngameRenderer : Disposable { // indicator batch.color = Color.WHITE - batch.fillRect(18f, 0f, 18f, 10f) + Toolkit.fillArea(batch, 18, 0, 18, 10) } // works as intended diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt index 6c0b5670e..ee46ce093 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt @@ -9,7 +9,6 @@ import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE import net.torvald.terrarum.gameactors.AVKey import net.torvald.terrarum.gameitem.GameItem import net.torvald.terrarum.langpack.Lang -import net.torvald.terrarum.modulebasegame.TerrarumIngame import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory.Companion.CAPACITY_MODE_COUNT import net.torvald.terrarum.modulebasegame.ui.* import net.torvald.terrarum.modulebasegame.ui.UIInventoryCells.Companion.weightBarWidth @@ -21,6 +20,7 @@ import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.controlH import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.internalHeight import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull.Companion.internalWidth import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.listGap +import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack @@ -225,13 +225,13 @@ internal class UIStorageChest : UICanvas( // encumbrance bar background batch.color = encumbBack - batch.fillRect( + Toolkit.fillArea(batch, encumbBarXPos, encumbBarYPos, weightBarWidth, controlHelpHeight - 6f ) // encumbrance bar batch.color = encumbCol - batch.fillRect( + Toolkit.fillArea(batch, encumbBarXPos, encumbBarYPos, if (getPlayerInventory().capacityMode == FixtureInventory.CAPACITY_MODE_NO_ENCUMBER) 1f diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIBuildingMakerBlockChooser.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIBuildingMakerBlockChooser.kt index 9e7f57e4e..d58341dbb 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIBuildingMakerBlockChooser.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIBuildingMakerBlockChooser.kt @@ -1,20 +1,20 @@ package net.torvald.terrarum.modulebasegame.ui -import com.badlogic.gdx.Gdx import com.badlogic.gdx.graphics.Camera import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.g2d.SpriteBatch -import net.torvald.terrarum.App +import net.torvald.terrarum.BlockCodex +import net.torvald.terrarum.ItemCodex +import net.torvald.terrarum.Terrarum import net.torvald.terrarum.blendNormal -import net.torvald.terrarum.fillRect import net.torvald.terrarum.modulebasegame.BuildingMaker import net.torvald.terrarum.modulebasegame.ui.ItemSlotImageFactory.CELLCOLOUR_WHITE +import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UIItemImageButton import net.torvald.terrarum.ui.UIItemTextButtonList import net.torvald.terrarum.ui.UIItemTextButtonList.Companion.DEFAULT_BACKGROUNDCOL import kotlin.math.roundToInt -import net.torvald.terrarum.* /** * Created by minjaesong on 2019-02-14. @@ -132,15 +132,15 @@ class UIBuildingMakerBlockChooser(val parent: BuildingMaker): UICanvas() { // gaps between tabs and close button batch.color = DEFAULT_BACKGROUNDCOL - batch.fillRect(0f, tabs.height.toFloat(), MENUBAR_SIZE.toFloat(), height.toFloat() - (tabs.height + closeButton.height)) + Toolkit.fillArea(batch, 0f, tabs.height.toFloat(), MENUBAR_SIZE.toFloat(), height.toFloat() - (tabs.height + closeButton.height)) // scrollbar back batch.color = DEFAULT_BACKGROUNDCOL - batch.fillRect(width - SCROLLBAR_SIZE.toFloat(), 0f, SCROLLBAR_SIZE.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, width - SCROLLBAR_SIZE.toFloat(), 0f, SCROLLBAR_SIZE.toFloat(), height.toFloat()) batch.color = scrollbarBackCol - batch.fillRect(width - SCROLLBAR_SIZE.toFloat(), 0f, SCROLLBAR_SIZE.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, width - SCROLLBAR_SIZE.toFloat(), 0f, SCROLLBAR_SIZE.toFloat(), height.toFloat()) // scrollbar batch.color = CELLCOLOUR_WHITE - batch.fillRect(width - SCROLLBAR_SIZE.toFloat(), scrollBarPos.toFloat(), SCROLLBAR_SIZE.toFloat(), scrollBarHeight) + Toolkit.fillArea(batch, width - SCROLLBAR_SIZE.toFloat(), scrollBarPos.toFloat(), SCROLLBAR_SIZE.toFloat(), scrollBarHeight) // the actual buttons tabs.render(batch, camera) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIBuildingMakerPenMenu.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIBuildingMakerPenMenu.kt index b51436577..c35d7f5f7 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIBuildingMakerPenMenu.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIBuildingMakerPenMenu.kt @@ -8,6 +8,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.* import net.torvald.terrarum.blockproperties.Block import net.torvald.terrarum.modulebasegame.BuildingMaker +import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UIItemImageButton import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack @@ -22,18 +23,19 @@ class UIBuildingMakerPenMenu(val parent: BuildingMaker): UICanvas() { companion object { const val SIZE = 330 const val RADIUS = SIZE / 2.0 + const val RADIUSI = RADIUS.toInt() const val RADIUSF = RADIUS.toFloat() const val BLOCKS_ROW_RADIUS = 120.0 const val TOOLS_ROW_RADIUS = 56.0 const val BLOCK_BACK_SIZE = 72 - const val BLOCK_BACK_RADIUS = BLOCK_BACK_SIZE / 2f + const val BLOCK_BACK_RADIUS = BLOCK_BACK_SIZE / 2 const val ICON_SIZE = 38 const val ICON_SIZEH = ICON_SIZE / 2f const val CLOSE_BUTTON_SIZE = 48 - const val CLOSE_BUTTON_RADIUS = CLOSE_BUTTON_SIZE / 2f + const val CLOSE_BUTTON_RADIUS = CLOSE_BUTTON_SIZE / 2 const val PALETTE_SIZE = 10 const val TOOLS_SIZE = 5 @@ -135,7 +137,7 @@ class UIBuildingMakerPenMenu(val parent: BuildingMaker): UICanvas() { // primary click if (Terrarum.mouseDown) { // close by clicking close button or out-of-boud - if (mouseVec.distanceSquared(RADIUS, RADIUS) !in CLOSE_BUTTON_RADIUS.sqr()..RADIUSF.sqr()) { + if (mouseVec.distanceSquared(RADIUS, RADIUS) !in CLOSE_BUTTON_RADIUS.toFloat().sqr()..RADIUSF.sqr()) { closeGracefully() } } @@ -149,7 +151,7 @@ class UIBuildingMakerPenMenu(val parent: BuildingMaker): UICanvas() { override fun renderUI(batch: SpriteBatch, camera: Camera) { // draw back batch.color = backCol - batch.fillCircle(0f, 0f, SIZE.toFloat(), SIZE.toFloat()) + Toolkit.fillCircle(batch,0, 0, SIZE, SIZE) // draw blocks slot batch.color = blockCellCol @@ -167,12 +169,12 @@ class UIBuildingMakerPenMenu(val parent: BuildingMaker): UICanvas() { "wall@" + Block.GLASS_CRUDE )//AppLoader.getConfigStringArray("buildingmakerfavs") for (i in 0 until PALETTE_SIZE) { - val x = blockCellPos[i].x.roundToInt().toFloat() - val y = blockCellPos[i].y.roundToInt().toFloat() + val x = blockCellPos[i].x.roundToInt() + val y = blockCellPos[i].y.roundToInt() batch.color = blockCellCol - repeat((i == mouseOnBlocksSlot).toInt() + 1) { batch.fillCircle(x - BLOCK_BACK_RADIUS, y - BLOCK_BACK_RADIUS, BLOCK_BACK_SIZE.toFloat(), BLOCK_BACK_SIZE.toFloat()) } + repeat((i == mouseOnBlocksSlot).toInt() + 1) { Toolkit.fillCircle(batch, x - BLOCK_BACK_RADIUS, y - BLOCK_BACK_RADIUS, BLOCK_BACK_SIZE, BLOCK_BACK_SIZE) } batch.color = Color.WHITE - batch.draw(ItemCodex.getItemImage(slotConfig[i]), x - 16, y - 16, 32f, 32f) + batch.draw(ItemCodex.getItemImage(slotConfig[i]), x - 16f, y - 16f, 32f, 32f) // update as well while looping if (i == mouseOnBlocksSlot && Terrarum.mouseDown) { @@ -183,7 +185,7 @@ class UIBuildingMakerPenMenu(val parent: BuildingMaker): UICanvas() { // draw close button batch.color = blockCellCol - repeat(mouseOnCloseButton.toInt() + 1) { batch.fillCircle(RADIUSF - CLOSE_BUTTON_RADIUS, RADIUSF - CLOSE_BUTTON_RADIUS, CLOSE_BUTTON_SIZE.toFloat(), CLOSE_BUTTON_SIZE.toFloat()) } + repeat(mouseOnCloseButton.toInt() + 1) { Toolkit.fillCircle(batch, RADIUSI - CLOSE_BUTTON_RADIUS, RADIUSI - CLOSE_BUTTON_RADIUS, CLOSE_BUTTON_SIZE, CLOSE_BUTTON_SIZE) } batch.color = if (mouseOnCloseButton) toolButtons[0].activeCol else toolButtons[0].inactiveCol batch.draw(toolIcons.get(5, 0), RADIUSF - ICON_SIZEH, RADIUSF - ICON_SIZEH) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UICheatDetected.kt b/src/net/torvald/terrarum/modulebasegame/ui/UICheatDetected.kt index 2e39a5be9..c05f0705b 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UICheatDetected.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UICheatDetected.kt @@ -34,7 +34,7 @@ class UICheatDetected : UICanvas() { Toolkit.blurEntireScreen(batch, camera as OrthographicCamera, 2f, 0, 0, width, height) batch.color = backgroundCol - batch.fillRect(0f, 0f, width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, 0f, 0f, width.toFloat(), height.toFloat()) batch.color = Color.WHITE val txt = Lang["ERROR_GENERIC_CHEATING"] diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventory.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventory.kt index 6c3a44bf2..99b649396 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventory.kt @@ -217,13 +217,13 @@ package net.torvald.terrarum.modulebasegame.ui // background blendNormal() batch.color = backgroundColour - batch.fillRect(0f, 0f, width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, 0f, 0f, width.toFloat(), height.toFloat()) // cat bar background blendMul() batch.color = Color(0xcccccc_ff.toInt()) - batch.fillRect(0f, 0f, catButtons.width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, 0f, 0f, catButtons.width.toFloat(), height.toFloat()) catButtons.render(batch, camera) @@ -255,7 +255,7 @@ package net.torvald.terrarum.modulebasegame.ui // encumbrance bar background blendMul() batch.color = Color(0xa0a0a0_ff.toInt()) - batch.fillRect( + Toolkit.fillArea(batch, width - 3 - weightBarWidth, height - controlHelpHeight + 3f, weightBarWidth, @@ -264,7 +264,7 @@ package net.torvald.terrarum.modulebasegame.ui // encumbrance bar blendNormal() batch.color = if (isEncumbered) Color(0xff0000_cc.toInt()) else Color(0x00ff00_cc.toInt()) - batch.fillRect( + Toolkit.fillArea(batch, width - 3 - weightBarWidth, height - controlHelpHeight + 3f, if (actor?.inventory?.capacityMode == CAPACITY_MODE_NO_ENCUMBER) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt index 3d9e911bf..8ca988315 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt @@ -120,13 +120,13 @@ internal class UIInventoryCells( val encumbCol = UIItemInventoryCellCommonRes.getHealthMeterColour(1f - encumbrancePerc, 0f, 1f) val encumbBack = encumbCol mul UIItemInventoryCellCommonRes.meterBackDarkening batch.color = encumbBack - batch.fillRect( + Toolkit.fillArea(batch, encumbBarXPos, encumbBarYPos, weightBarWidth, controlHelpHeight - 6f ) // encumbrance bar batch.color = encumbCol - batch.fillRect( + Toolkit.fillArea(batch, encumbBarXPos, encumbBarYPos, if (full.actor.inventory.capacityMode == FixtureInventory.CAPACITY_MODE_NO_ENCUMBER) 1f diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryMinimap.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryMinimap.kt index dcd7f6a5b..037de4c00 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryMinimap.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryMinimap.kt @@ -108,7 +108,7 @@ class UIInventoryMinimap(val full: UIInventoryFull) : UICanvas() { // sky background batch.color = MINIMAP_SKYCOL - batch.fillRect(0f, 0f, MINIMAP_WIDTH, MINIMAP_HEIGHT) + Toolkit.fillArea(batch, 0f, 0f, MINIMAP_WIDTH, MINIMAP_HEIGHT) // the actual image batch.color = Color.WHITE batch.draw(MinimapComposer.tempTex, tx, ty + MinimapComposer.totalHeight * minimapZoom, MinimapComposer.totalWidth * minimapZoom, -MinimapComposer.totalHeight * minimapZoom) @@ -124,10 +124,10 @@ class UIInventoryMinimap(val full: UIInventoryFull) : UICanvas() { batch.projectionMatrix = camera.combined // 1px stroke batch.color = Color.WHITE - batch.fillRect((width - MINIMAP_WIDTH) / 2, -1 + cellOffY.toFloat(), MINIMAP_WIDTH, 1f) - batch.fillRect((width - MINIMAP_WIDTH) / 2, cellOffY + MINIMAP_HEIGHT, MINIMAP_WIDTH, 1f) - batch.fillRect(-1 + (width - MINIMAP_WIDTH) / 2, cellOffY.toFloat(), 1f, MINIMAP_HEIGHT) - batch.fillRect((width - MINIMAP_WIDTH) / 2 + MINIMAP_WIDTH, cellOffY.toFloat(), 1f, MINIMAP_HEIGHT) + Toolkit.fillArea(batch, (width - MINIMAP_WIDTH) / 2, -1 + cellOffY.toFloat(), MINIMAP_WIDTH, 1f) + Toolkit.fillArea(batch, (width - MINIMAP_WIDTH) / 2, cellOffY + MINIMAP_HEIGHT, MINIMAP_WIDTH, 1f) + Toolkit.fillArea(batch, -1 + (width - MINIMAP_WIDTH) / 2, cellOffY.toFloat(), 1f, MINIMAP_HEIGHT) + Toolkit.fillArea(batch, (width - MINIMAP_WIDTH) / 2 + MINIMAP_WIDTH, cellOffY.toFloat(), 1f, MINIMAP_HEIGHT) // control hints batch.color = Color.WHITE diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIKeyboardControlPanel.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIKeyboardControlPanel.kt index bc492082f..0c01ff717 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIKeyboardControlPanel.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIKeyboardControlPanel.kt @@ -130,6 +130,8 @@ class UIKeyboardControlPanel(remoCon: UIRemoCon?) : UICanvas() { private val controlPalette = UIItemControlPaletteBaloon(this, (Toolkit.drawWidth - 480) / 2, kby + 219) + private val textInputPanel = UIItemTextLineInput(this, drawX, 360, width) + init { keycaps.values.forEach { addUIitem(it) } updateKeycaps() @@ -139,6 +141,8 @@ class UIKeyboardControlPanel(remoCon: UIRemoCon?) : UICanvas() { resetKeyConfig() updateKeycaps() } + + addUIitem(textInputPanel) } private fun resetKeyConfig() { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIPaletteSelector.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIPaletteSelector.kt index 98650f8d6..8867e5238 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIPaletteSelector.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIPaletteSelector.kt @@ -6,15 +6,15 @@ import com.badlogic.gdx.graphics.Pixmap import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.App +import net.torvald.terrarum.ItemCodex import net.torvald.terrarum.blendNormal import net.torvald.terrarum.blockproperties.Block -import net.torvald.terrarum.fillRect import net.torvald.terrarum.gameitem.ItemID import net.torvald.terrarum.modulebasegame.BuildingMaker import net.torvald.terrarum.modulebasegame.ui.ItemSlotImageFactory.CELLCOLOUR_BLACK +import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UINSMenu -import net.torvald.terrarum.* /** * Created by minjaesong on 2019-02-03. @@ -75,7 +75,7 @@ class UIPaletteSelector(val parent: BuildingMaker) : UICanvas() { // draw title bar batch.color = UINSMenu.DEFAULT_TITLEBACKCOL blendNormal(batch) - batch.fillRect(0f, 0f, width.toFloat(), LINE_HEIGHT.toFloat()) + Toolkit.fillArea(batch, 0, 0, width, LINE_HEIGHT) // draw "Pal." batch.color = UINSMenu.DEFAULT_TITLETEXTCOL @@ -83,7 +83,7 @@ class UIPaletteSelector(val parent: BuildingMaker) : UICanvas() { // draw background batch.color = CELLCOLOUR_BLACK - batch.fillRect(0f, LINE_HEIGHT.toFloat(), 36f, 48f) + Toolkit.fillArea(batch, 0, LINE_HEIGHT, 36, 48) // draw back and fore selection batch.color = Color.WHITE diff --git a/src/net/torvald/terrarum/tests/UIElemTest.kt b/src/net/torvald/terrarum/tests/UIElemTest.kt index cb845e0e3..e25d93c49 100644 --- a/src/net/torvald/terrarum/tests/UIElemTest.kt +++ b/src/net/torvald/terrarum/tests/UIElemTest.kt @@ -2,7 +2,7 @@ package net.torvald.terrarum.tests import com.badlogic.gdx.ApplicationAdapter import com.badlogic.gdx.Gdx -import com.badlogic.gdx.Input +import com.badlogic.gdx.InputAdapter import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration import com.badlogic.gdx.graphics.Camera @@ -11,11 +11,13 @@ import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.glutils.ShaderProgram -import com.badlogic.gdx.graphics.glutils.ShapeRenderer -import com.jme3.math.FastMath +import net.torvald.EMDASH import net.torvald.terrarum.* -import net.torvald.terrarum.ui.* -import kotlin.math.roundToInt +import net.torvald.terrarum.ui.Toolkit +import net.torvald.terrarum.ui.UICanvas +import net.torvald.terrarum.ui.UIItemTextLineInput +import net.torvald.terrarum.ui.UIItemToggleButton +import net.torvald.terrarumsansbitmap.gdx.GameFontBase /** * Created by Torvald on 2019-10-16. @@ -29,18 +31,66 @@ class UIElemTest : ApplicationAdapter() { private lateinit var ui: UICanvas override fun create() { + App.fontGame = GameFontBase(App.FONT_DIR, false, true, false, + Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest, false, + 256, false, 0.5f, false + ) + batch = SpriteBatch() camera = OrthographicCamera() - camera.setToOrtho(false, 800f, 600f) + camera.setToOrtho(true, 800f, 600f) camera.update() + Gdx.gl20.glViewport(0, 0, 800, 600) + ui = DummyTogglePane() ui.isVisible = true + + Gdx.input.inputProcessor = object : InputAdapter() { + override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { + ui.touchUp(screenX, screenY, pointer, button) + return true + } + + override fun keyTyped(character: Char): Boolean { + ui.keyTyped(character) + return true + } + + override fun scrolled(amountX: Float, amountY: Float): Boolean { + ui.scrolled(amountX, amountY) + return true + } + + override fun keyUp(keycode: Int): Boolean { + ui.keyUp(keycode) + return true + } + + override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean { + ui.touchDragged(screenX, screenY, pointer) + return true + } + + override fun keyDown(keycode: Int): Boolean { + ui.keyDown(keycode) + return true + } + + override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { + ui.touchDown(screenX, screenY, pointer, button) + return true + } + } } override fun render() { + gdxClearAndSetBlend(0.1f, 0.1f, 0.1f, 1f) + ui.update(Gdx.graphics.deltaTime) ui.render(batch, camera) + + Gdx.graphics.setTitle("Terrarum UIElemTest $EMDASH F: ${Gdx.graphics.framesPerSecond}") } @@ -51,46 +101,63 @@ class UIElemTest : ApplicationAdapter() { } class DummyTogglePane : UICanvas() { - private val button1 = UIItemToggleButton(this, 0, 0) + private val button1 = UIItemToggleButton(this, 400, 100) + private val textin = UIItemTextLineInput(this, 400, 160, 400) - private val key1 = UIItemConfigKeycap(this, 0, 20, 4, Input.Keys.A) - private val key2 = UIItemConfigKeycap(this, 36, 20, 4, Input.Keys.S) - private val key3 = UIItemConfigKeycap(this, 36*2, 20, 4, Input.Keys.D) - private val key4 = UIItemConfigKeycap(this, 36*3, 20, 4, Input.Keys.F) - - override var width = 100 - override var height = 25 + override var width = 800 + override var height = 600 override var openCloseTime: Second = 0f private var timer = 0f init { + button1.clickOnceListener = { _,_,_ -> + button1.toggle() + } uiItems.add(button1) - uiItems.add(key1) - uiItems.add(key2) - uiItems.add(key3) - uiItems.add(key4) + uiItems.add(textin) } override fun updateUI(delta: Float) { - timer += delta - - if (timer >= 1f) { - timer -= 1f - button1.toggle() - } - uiItems.forEach { it.update(delta) } } override fun renderUI(batch: SpriteBatch, camera: Camera) { batch.inUse { + batch.color = Color.CORAL + Toolkit.fillArea(batch, 0f, 0f, 800f, 600f) + batch.color = Color.WHITE uiItems.forEach { it.render(batch, camera) } } } + override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { + uiItems.forEach { it.touchDown(screenX, screenY, pointer, button) } + return true + } + + override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { + uiItems.forEach { it.touchUp(screenX, screenY, pointer, button) } + return true + } + + override fun keyDown(keycode: Int): Boolean { + uiItems.forEach { it.keyDown(keycode) } + return true + } + + override fun keyUp(keycode: Int): Boolean { + uiItems.forEach { it.keyUp(keycode) } + return true + } + + override fun keyTyped(character: Char): Boolean { + uiItems.forEach { it.keyTyped(character) } + return true + } + override fun doOpening(delta: Float) { } @@ -115,6 +182,8 @@ fun main(args: Array) { appConfig.setResizable(false) appConfig.setWindowedMode(800, 600) appConfig.setForegroundFPS(60) + App.scr = TerrarumScreenSize(800, 600) + Lwjgl3Application(UIElemTest(), appConfig) } \ No newline at end of file diff --git a/src/net/torvald/terrarum/tests/UITestPad1.kt b/src/net/torvald/terrarum/tests/UITestPad1.kt index a67522d31..91c38ff4e 100644 --- a/src/net/torvald/terrarum/tests/UITestPad1.kt +++ b/src/net/torvald/terrarum/tests/UITestPad1.kt @@ -9,8 +9,12 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.glutils.ShaderProgram -import net.torvald.terrarum.* +import net.torvald.terrarum.App +import net.torvald.terrarum.Terrarum +import net.torvald.terrarum.Yaml +import net.torvald.terrarum.inUse import net.torvald.terrarum.modulebasegame.TerrarumIngame +import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.UINSMenu val UITEST1_WIDTH = 1280 @@ -98,7 +102,7 @@ class UITestPad1 : ScreenAdapter() { batch.inUse { batch.color = bgCol - batch.fillRect(0f, 0f, 2048f, 2048f) + Toolkit.fillArea(batch, 0, 0, 2048, 2048) nsMenu.render(batch, camera) diff --git a/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt b/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt index 9588fabd4..0a77cfa5c 100644 --- a/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt +++ b/src/net/torvald/terrarum/ui/BasicDebugInfoWindow.kt @@ -300,7 +300,7 @@ class BasicDebugInfoWindow : UICanvas() { val histogramMax = histogram.screen_tiles.toFloat() batch.color = uiColour - batch.fillRect(x.toFloat(), y.toFloat(), w.plus(1), h) + Toolkit.fillArea(batch, x.toFloat(), y.toFloat(), w.plus(1), h) batch.color = Color.GRAY App.fontSmallNumbers.draw(batch, "0", x.toFloat(), y.toFloat() + h + 2) App.fontSmallNumbers.draw(batch, "255", x.toFloat() + w + 1 - 8 * 3, y.toFloat() + h + 2) @@ -322,7 +322,7 @@ class BasicDebugInfoWindow : UICanvas() { val bar_w = 1f batch.color = barColour[c] - batch.fillRect(bar_x, bar_y, bar_w, -bar_h) + Toolkit.fillArea(batch, bar_x, bar_y, bar_w, -bar_h) } } blendNormal(batch) diff --git a/src/net/torvald/terrarum/ui/ConsoleWindow.kt b/src/net/torvald/terrarum/ui/ConsoleWindow.kt index b6e102043..df141d4c7 100644 --- a/src/net/torvald/terrarum/ui/ConsoleWindow.kt +++ b/src/net/torvald/terrarum/ui/ConsoleWindow.kt @@ -5,7 +5,10 @@ import com.badlogic.gdx.graphics.Camera import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.EMDASH -import net.torvald.terrarum.* +import net.torvald.terrarum.App +import net.torvald.terrarum.Terrarum +import net.torvald.terrarum.TerrarumAppConfiguration +import net.torvald.terrarum.ccE import net.torvald.terrarum.console.Authenticator import net.torvald.terrarum.console.CommandInterpreter import net.torvald.terrarum.gameactors.AVKey @@ -73,8 +76,8 @@ class ConsoleWindow : UICanvas() { override fun renderUI(batch: SpriteBatch, camera: Camera) { // background batch.color = UIColour - batch.fillRect(drawOffX, drawOffY, width.toFloat(), height.toFloat()) - batch.fillRect(drawOffX, drawOffY, width.toFloat(), LINE_HEIGHT.toFloat()) + Toolkit.fillArea(batch, drawOffX, drawOffY, width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, drawOffX, drawOffY, width.toFloat(), LINE_HEIGHT.toFloat()) val input = commandInputPool!!.toString() val inputDrawWidth = App.fontGame.getWidth(input) @@ -85,9 +88,9 @@ class ConsoleWindow : UICanvas() { App.fontGame.draw(batch, input, 1f + drawOffX, drawOffY) batch.color = Color(0x7f7f7f_ff) - batch.fillRect(inputDrawWidth.toFloat() + drawOffX + 1, drawOffY, 2f, inputDrawHeight) + Toolkit.fillArea(batch, inputDrawWidth.toFloat() + drawOffX + 1, drawOffY, 2f, inputDrawHeight) batch.color = Color.WHITE - batch.fillRect(inputDrawWidth.toFloat() + drawOffX + 1, drawOffY, 1f, inputDrawHeight - 1) + Toolkit.fillArea(batch, inputDrawWidth.toFloat() + drawOffX + 1, drawOffY, 1f, inputDrawHeight - 1) // messages diff --git a/src/net/torvald/terrarum/ui/Toolkit.kt b/src/net/torvald/terrarum/ui/Toolkit.kt index 8082a283c..3b434474d 100644 --- a/src/net/torvald/terrarum/ui/Toolkit.kt +++ b/src/net/torvald/terrarum/ui/Toolkit.kt @@ -12,7 +12,6 @@ import com.badlogic.gdx.utils.Disposable import net.torvald.random.HQRNG import net.torvald.terrarum.App import net.torvald.terrarum.CommonResourcePool -import net.torvald.terrarum.fillRect import net.torvald.terrarum.modulebasegame.IngameRenderer import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack import org.lwjgl.opengl.GL20 @@ -30,10 +29,15 @@ object Toolkit : Disposable { val baloonTile = TextureRegionPack("assets/graphics/gui/message_black_tileable.tga", 36, 36, flipY = true) + val textureWhiteSquare = Texture(Gdx.files.internal("assets/graphics/ortho_line_tex_2px.tga")) + val textureWhiteCircle = Texture(Gdx.files.internal("assets/graphics/circle_512.tga")) init { App.disposables.add(this) + textureWhiteSquare.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest) + textureWhiteCircle.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) + CommonResourcePool.addToLoadingList("toolkit_box_border") { TextureRegionPack(Gdx.files.internal("./assets/graphics/gui/box_border_flat_tileable.tga"), 1, 1) } @@ -44,6 +48,8 @@ object Toolkit : Disposable { override fun dispose() { baloonTile.dispose() + textureWhiteSquare.dispose() + textureWhiteCircle.dispose() } val drawWidth: Int @@ -72,7 +78,19 @@ object Toolkit : Disposable { } fun fillArea(batch: SpriteBatch, x: Int, y: Int, w: Int, h: Int) { - batch.fillRect(x.toFloat(), y.toFloat(), w.toFloat(), h.toFloat()) + batch.draw(textureWhiteSquare, x.toFloat(), y.toFloat(), w.toFloat(), h.toFloat()) + } + fun fillArea(batch: SpriteBatch, x: Float, y: Float, w: Float, h: Float) { + batch.draw(textureWhiteSquare, x, y, w, h) + } + fun fillCircle(batch: SpriteBatch, x: Int, y: Int, w: Int, h: Int) { + batch.draw(textureWhiteCircle, x.toFloat(), y.toFloat(), w.toFloat(), h.toFloat()) + } + fun drawStraightLine(batch: SpriteBatch, x: Int, y: Int, otherEnd: Int, thickness: Int, isVertical: Boolean) { + if (!isVertical) + fillArea(batch, x, y, otherEnd - x, thickness) + else + fillArea(batch, x, y, thickness, otherEnd - y) } /** diff --git a/src/net/torvald/terrarum/ui/UIItemImageButton.kt b/src/net/torvald/terrarum/ui/UIItemImageButton.kt index 68d0dc800..be78ed98e 100644 --- a/src/net/torvald/terrarum/ui/UIItemImageButton.kt +++ b/src/net/torvald/terrarum/ui/UIItemImageButton.kt @@ -6,7 +6,6 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.TextureRegion import net.torvald.terrarum.BlendMode import net.torvald.terrarum.blendNormal -import net.torvald.terrarum.fillRect /** * Created by minjaesong on 2017-07-16. @@ -48,17 +47,17 @@ open class UIItemImageButton( if (highlighted) { BlendMode.resolve(highlightBackBlendMode, batch) batch.color = highlightBackCol - batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) } else if (mouseUp) { BlendMode.resolve(activeBackBlendMode, batch) batch.color = activeBackCol - batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) } else { batch.color = backgroundCol BlendMode.resolve(backgroundBlendMode, batch) - batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) } diff --git a/src/net/torvald/terrarum/ui/UIItemList.kt b/src/net/torvald/terrarum/ui/UIItemList.kt index b686452bc..a9ad31130 100644 --- a/src/net/torvald/terrarum/ui/UIItemList.kt +++ b/src/net/torvald/terrarum/ui/UIItemList.kt @@ -5,7 +5,6 @@ import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.BlendMode import net.torvald.terrarum.Second -import net.torvald.terrarum.fillRect /** @@ -107,12 +106,12 @@ class UIItemList( override fun render(batch: SpriteBatch, camera: Camera) { batch.color = backgroundCol BlendMode.resolve(backgroundBlendMode, batch) - batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) batch.color = highlightBackCol BlendMode.resolve(highlightBackBlendMode, batch) if (highlightY != null) { - batch.fillRect(posX.toFloat(), highlightY!!.toFloat(), width.toFloat(), UIItemTextButton.height.toFloat()) + Toolkit.fillArea(batch, posX.toFloat(), highlightY!!.toFloat(), width.toFloat(), UIItemTextButton.height.toFloat()) } itemList.forEach { it.render(batch, camera) } diff --git a/src/net/torvald/terrarum/ui/UIItemTextButton.kt b/src/net/torvald/terrarum/ui/UIItemTextButton.kt index 71aa273da..99ba43966 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextButton.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextButton.kt @@ -84,17 +84,17 @@ open class UIItemTextButton( /*if (highlighted) { BlendMode.resolve(highlightBackBlendMode, batch) batch.color = highlightBackCol - batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) } else if (mouseUp) { BlendMode.resolve(activeBackBlendMode, batch) batch.color = activeBackCol - batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) } else { batch.color = backgroundCol BlendMode.resolve(backgroundBlendMode, batch) - batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) }*/ diff --git a/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt b/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt index ad05675cb..6eb2fdf5d 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt @@ -232,12 +232,12 @@ class UIItemTextButtonList( if (kinematic) { batch.color = backgroundCol BlendMode.resolve(backgroundBlendMode, batch) - batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) + Toolkit.fillArea(batch, posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) batch.color = highlightBackCol BlendMode.resolve(highlightBackBlendMode, batch) if (highlightY != null) { - batch.fillRect(posX.toFloat(), highlightY!!.toFloat(), width.toFloat(), itemHitboxSize.toFloat()) + Toolkit.fillArea(batch, posX.toFloat(), highlightY!!.toFloat(), width.toFloat(), itemHitboxSize.toFloat()) } } diff --git a/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt b/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt index 8edf45ade..ae777e82a 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt @@ -1,13 +1,14 @@ package net.torvald.terrarum.ui -import com.badlogic.gdx.Gdx +import com.badlogic.gdx.Input import com.badlogic.gdx.graphics.Camera +import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.Pixmap import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.glutils.FrameBuffer -import net.torvald.terrarum.App -import net.torvald.terrarum.Terrarum -import java.awt.Color +import net.torvald.terrarum.* +import net.torvald.terrarum.gamecontroller.IngameController /** * @param width width of the text input where the text gets drawn, not the entire item @@ -19,7 +20,7 @@ class UIItemTextLineInput( parentUI: UICanvas, initialX: Int, initialY: Int, override val width: Int, - override val height: Int, + override val height: Int = 24, var placeholder: String? = null, val enablePasteButton: Boolean = true, val enableLanguageButton: Boolean = false @@ -28,33 +29,96 @@ class UIItemTextLineInput( companion object { val TEXTINPUT_COL_TEXT = Color.WHITE val TEXTINPUT_COL_GREY = Color.GRAY - + val TEXTINPUT_COL_BACKGROUND = Color(0x28282888) + const val CURSOR_BLINK_TIME = 1f / 3f } - private val fbo = FrameBuffer(Pixmap.Format.RGBA8888, width, height, true) + private val fbo = FrameBuffer(Pixmap.Format.RGBA8888, width - 4, height - 4, true) var isActive = true var isGreyedOut = false - val cursorX = 0 + var cursorX = 0 // 1 per code point + var cursorDrawX = 0 // pixelwise point + var cursorBlinkCounter = 0f + var cursorOn = true + val keybuf = StringBuilder() + private var fboUpdateLatch = false override fun update(delta: Float) { super.update(delta) if (Terrarum.mouseDown) { - isActive = mouseUp + //isActive = mouseUp } // process keypresses if (isActive) { + IngameController.withKeyboardEvent { (_, char, _, keycodes) -> + fboUpdateLatch = true + if (keycodes.contains(Input.Keys.BACKSPACE) && cursorX > 0) { + cursorX -= 1 + val charLen = Character.charCount(keybuf.codePointAt(cursorX)) + keybuf.delete(keybuf.length - charLen, keybuf.length) + cursorDrawX = App.fontGame.getWidth("$keybuf") + } + // accept: + // - literal "<" + // - keysymbol that does not start with "<" (not always has length of 1 because UTF-16) + else if (char != null && char[0].code >= 32 && (char == "<" || !char.startsWith("<"))) { + keybuf.append(char) + cursorDrawX = App.fontGame.getWidth("$keybuf") + cursorX += 1 + } + } + + cursorBlinkCounter += delta + + while (cursorBlinkCounter >= CURSOR_BLINK_TIME) { + cursorBlinkCounter -= CURSOR_BLINK_TIME + cursorOn = !cursorOn + } } } override fun render(batch: SpriteBatch, camera: Camera) { + + batch.end() + + if (fboUpdateLatch) { + fboUpdateLatch = false + fbo.inAction(camera as OrthographicCamera, batch) { batch.inUse { + gdxClearAndSetBlend(0f, 0f, 0f, 0f) + + it.color = Color.WHITE + App.fontGame.draw(it, "$keybuf", 0f, 0f) + } } + } + + batch.begin() + + batch.color = TEXTINPUT_COL_BACKGROUND + Toolkit.fillArea(batch, posX, posY, width, height) + + batch.color = if (isActive) TEXTINPUT_COL_TEXT else TEXTINPUT_COL_GREY + Toolkit.drawBoxBorder(batch, posX - 1, posY - 1, width + 2, height + 2) + batch.draw(fbo.colorBufferTexture, posX + 2f, posY + 2f, fbo.width.toFloat(), fbo.height.toFloat()) + + if (cursorOn) { + val oldBatchCol = batch.color.cpy() + + batch.color = batch.color.mul(0.5f,0.5f,0.5f,1f) + Toolkit.fillArea(batch, posX + cursorDrawX + 3, posY, 2, 24) + + batch.color = oldBatchCol + Toolkit.fillArea(batch, posX + cursorDrawX + 3, posY, 1, 23) + } + + super.render(batch, camera) } diff --git a/src/net/torvald/terrarum/ui/UINSMenu.kt b/src/net/torvald/terrarum/ui/UINSMenu.kt index 6d024f2e2..a19999f2d 100644 --- a/src/net/torvald/terrarum/ui/UINSMenu.kt +++ b/src/net/torvald/terrarum/ui/UINSMenu.kt @@ -171,7 +171,7 @@ class UINSMenu( // draw title bar batch.color = titleBackCol BlendMode.resolve(titleBlendMode, batch) - batch.fillRect(it.ui.posX.toFloat(), it.ui.posY.toFloat() - LINE_HEIGHT, it.ui.width.toFloat(), LINE_HEIGHT.toFloat()) + Toolkit.fillArea(batch, it.ui.posX.toFloat(), it.ui.posY.toFloat() - LINE_HEIGHT, it.ui.width.toFloat(), LINE_HEIGHT.toFloat()) batch.color = titleTextCol blendNormal(batch) diff --git a/src/net/torvald/terrarum/worlddrawer/FeaturesDrawer.kt b/src/net/torvald/terrarum/worlddrawer/FeaturesDrawer.kt index f356ab662..4e1acf629 100644 --- a/src/net/torvald/terrarum/worlddrawer/FeaturesDrawer.kt +++ b/src/net/torvald/terrarum/worlddrawer/FeaturesDrawer.kt @@ -3,11 +3,16 @@ package net.torvald.terrarum.worlddrawer import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.jme3.math.FastMath import net.torvald.colourutil.ColourTemp -import net.torvald.terrarum.* +import net.torvald.terrarum.App +import net.torvald.terrarum.Terrarum import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZEF +import net.torvald.terrarum.blendMul +import net.torvald.terrarum.blendNormal import net.torvald.terrarum.blockproperties.Block import net.torvald.terrarum.blockstats.BlockStats import net.torvald.terrarum.gameworld.GameWorld +import net.torvald.terrarum.ui.Toolkit +import kotlin.math.roundToInt /** * Created by minjaesong on 2015-12-31. @@ -58,9 +63,9 @@ object FeaturesDrawer { blendMul(batch) batch.color = ColourTemp(colTemp) - batch.fillRect(0f, 0f, - App.scr.width * if (zoom < 1) 1f / zoom else zoom, - App.scr.height * if (zoom < 1) 1f / zoom else zoom + Toolkit.fillArea(batch, 0, 0, + (App.scr.width * if (zoom < 1) 1f / zoom else zoom).roundToInt(), + (App.scr.height * if (zoom < 1) 1f / zoom else zoom).roundToInt() ) blendNormal(batch)