diff --git a/assets/graphics/gui/inventory/category.tga b/assets/graphics/gui/inventory/category.tga index 6682c5c3e..64b16a2f1 100755 --- a/assets/graphics/gui/inventory/category.tga +++ b/assets/graphics/gui/inventory/category.tga @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e9c9e1f2387328bdb20908556f63a49821b80f43c2eca8fdf8f8860536f9f9e9 +oid sha256:b2baa9b6caabc974a0e5c9bb45d4d35b3ea21434d33127e98c4412298acb02ba size 240018 diff --git a/src/net/torvald/terrarum/gamecontroller/IME.kt b/src/net/torvald/terrarum/gamecontroller/IME.kt index 5603f1f48..2eeec5cb1 100644 --- a/src/net/torvald/terrarum/gamecontroller/IME.kt +++ b/src/net/torvald/terrarum/gamecontroller/IME.kt @@ -33,6 +33,8 @@ data class TerrarumInputMethod( */ object IME { + class LayoutNotFound(id: String): NullPointerException("Keyboard layout not found: $id") + const val KEYLAYOUT_DIR = "assets/keylayout/" const val KEYLAYOUT_EXTENSION = "key" const val IME_EXTENSION = "ime" diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIKeyboardControlPanel.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIKeyboardControlPanel.kt index e6402c0b5..2bce8108b 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIKeyboardControlPanel.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIKeyboardControlPanel.kt @@ -14,7 +14,6 @@ import net.torvald.terrarum.gamecontroller.IME import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.linearSearch import net.torvald.terrarum.ui.* -import net.torvald.terrarum.utils.RandomWordsName import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack /** @@ -136,12 +135,12 @@ class UIKeyboardControlPanel(remoCon: UIRemoCon?) : UICanvas() { private val lowLayerCodes = IME.getAllLowLayers() private val lowLayerNames = lowLayerCodes.map { { IME.getLowLayerByName(it).name } } - private val keyboardLayoutSelection = UIItemTextSelector(this, drawX + width - textSelWidth - 3, 400, lowLayerNames, lowLayerCodes.linearSearch { it == App.getConfigString("basekeyboardlayout") }!!, textSelWidth) + private val keyboardLayoutSelection = UIItemTextSelector(this, drawX + width - textSelWidth - 3, 400, lowLayerNames, lowLayerCodes.linearSearch { it == App.getConfigString("basekeyboardlayout") } ?: throw IME.LayoutNotFound(App.getConfigString("basekeyboardlayout")), textSelWidth) private val imeCodes0 = IME.getAllHighLayers() private val imeCodes = listOf("none") + IME.getAllHighLayers() private val imeNames = listOf({"$EMDASH"}) + imeCodes0.map { { IME.getHighLayerByName(it).name } } - private val imeSelection = UIItemTextSelector(this, drawX + width - textSelWidth - 3, 440, imeNames, imeCodes.linearSearch { it == App.getConfigString("inputmethod") }!!, textSelWidth) + private val imeSelection = UIItemTextSelector(this, drawX + width - textSelWidth - 3, 440, imeNames, imeCodes.linearSearch { it == App.getConfigString("inputmethod") } ?: throw IME.LayoutNotFound(App.getConfigString("inputmethod")), textSelWidth) diff --git a/src/net/torvald/terrarum/ui/UIItemSpinner.kt b/src/net/torvald/terrarum/ui/UIItemSpinner.kt new file mode 100644 index 000000000..a3c6e3c9c --- /dev/null +++ b/src/net/torvald/terrarum/ui/UIItemSpinner.kt @@ -0,0 +1,131 @@ +package net.torvald.terrarum.ui + +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.* +import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack + +/** + * Created by minjaesong on 2021-10-23. + */ + +class UIItemSpinner( + parentUI: UICanvas, + initialX: Int, initialY: Int, + initialValue: Int, + val min: Int, + val max: Int, + val step: Int, + override val width: Int, + private val drawBorder: Boolean = true +) : UIItem(parentUI, initialX, initialY) { + + init { + CommonResourcePool.addToLoadingList("inventory_category") { + TextureRegionPack("assets/graphics/gui/inventory/category.tga", 20, 20) + } + CommonResourcePool.loadAll() + } + + private val labels = CommonResourcePool.getAsTextureRegionPack("inventory_category") + + override val height = 24 + private val buttonW = 30 + + private val fbo = FrameBuffer(Pixmap.Format.RGBA8888, width - 2*buttonW - 6, height - 4, true) + + var value = initialValue.coerceIn(min, max) + private var fboUpdateLatch = true + + private var mouseOnButton = 0 // 0: nothing, 1: left, 2: right + + var selectionChangeListener: (Int) -> Unit = {} + + override fun update(delta: Float) { + super.update(delta) + + mouseOnButton = + if (relativeMouseX in 0..buttonW && relativeMouseY in 0..height) + 1 + else if (relativeMouseX in width - buttonW..width && relativeMouseY in 0..height) + 2 + else + 0 + + if (!mouseLatched && Terrarum.mouseDown && mouseOnButton != 0) { + mouseLatched = true + value = (value + step * ((mouseOnButton * 2) - 3)).coerceIn(min, max) + fboUpdateLatch = true + selectionChangeListener(value) + } + else if (!Terrarum.mouseDown) mouseLatched = false + } + + 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 + val t = "$value" + val tw = App.fontGame.getWidth(t) + App.fontGameFBO.draw(it, t, (fbo.width - tw) / 2, 0) + } } + } + + batch.begin() + + if (drawBorder) { + batch.color = UIItemTextLineInput.TEXTINPUT_COL_BACKGROUND + // left button cell back + Toolkit.fillArea(batch, posX, posY, buttonW, height) + // text area cell back + Toolkit.fillArea(batch, posX + buttonW + 3, posY, width - 2*buttonW - 6, height) + // right button cell back + Toolkit.fillArea(batch, posX + width - buttonW, posY, buttonW, height) + + // text area border + batch.color = Toolkit.Theme.COL_INACTIVE + Toolkit.drawBoxBorder(batch, posX - 1, posY - 1, width + 2, height + 2) + + // left button border + batch.color = if (mouseOnButton == 1 && mousePushed) Toolkit.Theme.COL_HIGHLIGHT + else if (mouseOnButton == 1) Toolkit.Theme.COL_ACTIVE else Toolkit.Theme.COL_INACTIVE + Toolkit.drawBoxBorder(batch, posX - 1, posY - 1, buttonW + 2, height + 2) + + // right button border + batch.color = if (mouseOnButton == 2 && mousePushed) Toolkit.Theme.COL_HIGHLIGHT + else if (mouseOnButton == 2) Toolkit.Theme.COL_ACTIVE else Toolkit.Theme.COL_INACTIVE + Toolkit.drawBoxBorder(batch, posX + width - buttonW - 1, posY - 1, buttonW + 2, height + 2) + } + + // left button icon + batch.color = if (mouseOnButton == 1 && mousePushed) Toolkit.Theme.COL_HIGHLIGHT + else if (mouseOnButton == 1) Toolkit.Theme.COL_ACTIVE else UIItemTextLineInput.TEXTINPUT_COL_TEXT + batch.draw(labels.get(9,2), posX + (buttonW - labels.tileW) / 2f, posY + (height - labels.tileH) / 2f) + + // right button icon + batch.color = if (mouseOnButton == 2 && mousePushed) Toolkit.Theme.COL_HIGHLIGHT + else if (mouseOnButton == 2) Toolkit.Theme.COL_ACTIVE else UIItemTextLineInput.TEXTINPUT_COL_TEXT + batch.draw(labels.get(10,2), posX + width - buttonW + (buttonW - labels.tileW) / 2f, posY + (height - labels.tileH) / 2f) + + // draw text + batch.color = UIItemTextLineInput.TEXTINPUT_COL_TEXT + batch.draw(fbo.colorBufferTexture, posX + buttonW + 3f, posY + 2f, fbo.width.toFloat(), fbo.height.toFloat()) + + + super.render(batch, camera) + } + + override fun dispose() { + fbo.dispose() + } +} diff --git a/src/net/torvald/terrarum/ui/UIItemTextSelector.kt b/src/net/torvald/terrarum/ui/UIItemTextSelector.kt index 4f2c53445..0c73b43a2 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextSelector.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextSelector.kt @@ -37,7 +37,7 @@ class UIItemTextSelector( override val height = 24 private val buttonW = 30 - private val fbo = FrameBuffer(Pixmap.Format.RGBA8888, width - 54 - 4, height - 4, true) + private val fbo = FrameBuffer(Pixmap.Format.RGBA8888, width - 2*buttonW - 6, height - 4, true) var selection = intialSelection private var fboUpdateLatch = true @@ -120,7 +120,7 @@ class UIItemTextSelector( // draw text batch.color = UIItemTextLineInput.TEXTINPUT_COL_TEXT - batch.draw(fbo.colorBufferTexture, posX + buttonW + 5f, posY + 2f, fbo.width.toFloat(), fbo.height.toFloat()) + batch.draw(fbo.colorBufferTexture, posX + buttonW + 3f, posY + 2f, fbo.width.toFloat(), fbo.height.toFloat()) super.render(batch, camera) @@ -130,29 +130,3 @@ class UIItemTextSelector( fbo.dispose() } } - -class UIItemSpinner( - parentUI: UICanvas, - initialX: Int, initialY: Int, - intialValue: Int, - val min: Int, - val max: Int, - val step: Int, - override val width: Int -) : UIItem(parentUI, initialX, initialY) { - - init { - CommonResourcePool.addToLoadingList("inventory_category") { - TextureRegionPack("assets/graphics/gui/inventory/category.tga", 20, 20) - } - CommonResourcePool.loadAll() - } - - private val labels = CommonResourcePool.getAsTextureRegionPack("inventory_category") - - override val height = 24 - - override fun dispose() { - - } -}