From 69345eab57ef16683565ac1cd6b8ec60e8e7879f Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 21 Oct 2023 21:29:14 +0900 Subject: [PATCH] queueing for notifications --- .../modulebasegame/ui/Notification.kt | 79 ++++++++++++------- src/net/torvald/terrarum/ui/UIHandler.kt | 8 +- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/Notification.kt b/src/net/torvald/terrarum/modulebasegame/ui/Notification.kt index 27978d76d..573df96ab 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/Notification.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/Notification.kt @@ -4,7 +4,9 @@ import com.badlogic.gdx.graphics.Camera import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.g2d.SpriteBatch +import com.badlogic.gdx.utils.Queue import net.torvald.terrarum.App +import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.Second import net.torvald.terrarum.blendNormalStraightAlpha import net.torvald.terrarum.ui.Toolkit @@ -12,6 +14,8 @@ import net.torvald.terrarum.ui.UICanvas import kotlin.math.max /** + * This UI must be always updated; the easiest way is to set `handler.alwaysUpdate` to `true` + * * Created by minjaesong on 2016-01-23. */ class Notification : UICanvas() { @@ -34,20 +38,31 @@ class Notification : UICanvas() { ) / 1000f private var displayTimer = 0f - internal var message: List = listOf("") + internal val messageQueue = Queue>() + private var messageDisplaying: List? = null private val timeGaugeCol = Color(0x707070ff) init { + handler.alwaysUpdate = true } override fun updateUI(delta: Float) { + if (messageDisplaying == null && messageQueue.notEmpty() && handler.isClosed) { + messageDisplaying = messageQueue.removeFirst() + displayTimer = 0f + handler.openCloseCounter = 0f + handler.opacity = 0f + handler.setAsOpen() + } + if (handler.isOpened) displayTimer += delta if (displayTimer >= visibleTime) { handler.setAsClose() displayTimer = 0f + messageDisplaying = null } } @@ -55,35 +70,46 @@ class Notification : UICanvas() { blendNormalStraightAlpha(batch) fontCol.a = handler.opacity * OPACITY - val realTextWidth = 12 + if (message.size == 1) - App.fontGame.getWidth(message[0]) - else - message.map { App.fontGame.getWidth(it) }.sorted().last() - val displayedTextWidth = max(240, realTextWidth) - // force the UI to the centre of the screen - this.posX = (App.scr.width - displayedTextWidth) / 2 - val textHeight = message.size * App.fontGame.lineHeight + if (messageDisplaying != null) { + + val realTextWidth = 12 + if (messageDisplaying!!.size == 1) + App.fontGame.getWidth(messageDisplaying!![0]) + else + messageDisplaying!!.map { App.fontGame.getWidth(it) }.sorted().last() + val displayedTextWidth = max(240, realTextWidth) + + // force the UI to the centre of the screen + this.posX = (App.scr.width - displayedTextWidth) / 2 + val textHeight = messageDisplaying!!.size * App.fontGame.lineHeight - Toolkit.drawBaloon(batch, 0f, -textHeight, displayedTextWidth.toFloat(), textHeight, handler.opacity * OPACITY) + Toolkit.drawBaloon( + batch, + 0f, + -textHeight, + displayedTextWidth.toFloat(), + textHeight, + handler.opacity * OPACITY + ) - // draw time gauge - if (displayTimer != 0f) { - batch.color = timeGaugeCol - val time = 1f - (displayTimer / visibleTime) - val bw = displayedTextWidth * time - val bx = (displayedTextWidth - bw) / 2 - Toolkit.drawStraightLine(batch, bx, 2f, bx + bw, 2f, false) - } + // draw time gauge + if (displayTimer != 0f) { + batch.color = timeGaugeCol + val time = 1f - (displayTimer / visibleTime) + val bw = displayedTextWidth * time + val bx = (displayedTextWidth - bw) / 2 + Toolkit.drawStraightLine(batch, bx, 2f, bx + bw, 2f, false) + } - // draw texts - batch.color = fontCol - message.forEachIndexed { index, s -> - val xoff = 6 + (displayedTextWidth - realTextWidth) / 2 - val y = -textHeight + App.fontGame.lineHeight * index - App.fontGame.draw(batch, s, LRmargin + xoff, y - 1) + // draw texts + batch.color = fontCol + messageDisplaying!!.forEachIndexed { index, s -> + val xoff = 6 + (displayedTextWidth - realTextWidth) / 2 + val y = -textHeight + App.fontGame.lineHeight * index + App.fontGame.draw(batch, s, LRmargin + xoff, y - 1) + } } @@ -108,10 +134,7 @@ class Notification : UICanvas() { } fun sendNotification(message: List) { - this.message = message - handler.openCloseCounter = 0f - handler.opacity = 0f - handler.setAsOpen() + messageQueue.addLast(message) } override fun dispose() { diff --git a/src/net/torvald/terrarum/ui/UIHandler.kt b/src/net/torvald/terrarum/ui/UIHandler.kt index 08a8898b3..67ac2fb73 100644 --- a/src/net/torvald/terrarum/ui/UIHandler.kt +++ b/src/net/torvald/terrarum/ui/UIHandler.kt @@ -33,6 +33,8 @@ class UIHandler(//var UI: UICanvas, var uiTogglerFunctionDefault: ((UIHandler) -> Unit)? = null ): Disposable { + var alwaysUpdate = false + companion object { private val SHADER_PROG_FRAG = """ #ifdef GL_ES @@ -224,7 +226,7 @@ void main() { //if (closeFired && openCloseCounter > 9) closeFired = false - if (isVisible) { + if (isVisible || alwaysUpdate) { ui.updateUI(delta) } @@ -344,6 +346,8 @@ void main() { isVisible = true openFired = true + + openCloseCounter = 0f } } @@ -360,6 +364,8 @@ void main() { isOpening = false closeFired = true + + openCloseCounter = 0f } }