queueing for notifications

This commit is contained in:
minjaesong
2023-10-21 21:29:14 +09:00
parent 6e90d3521b
commit 69345eab57
2 changed files with 58 additions and 29 deletions

View File

@@ -4,7 +4,9 @@ import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.utils.Queue
import net.torvald.terrarum.App import net.torvald.terrarum.App
import net.torvald.terrarum.App.printdbg
import net.torvald.terrarum.Second import net.torvald.terrarum.Second
import net.torvald.terrarum.blendNormalStraightAlpha import net.torvald.terrarum.blendNormalStraightAlpha
import net.torvald.terrarum.ui.Toolkit import net.torvald.terrarum.ui.Toolkit
@@ -12,6 +14,8 @@ import net.torvald.terrarum.ui.UICanvas
import kotlin.math.max 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. * Created by minjaesong on 2016-01-23.
*/ */
class Notification : UICanvas() { class Notification : UICanvas() {
@@ -34,20 +38,31 @@ class Notification : UICanvas() {
) / 1000f ) / 1000f
private var displayTimer = 0f private var displayTimer = 0f
internal var message: List<String> = listOf("") internal val messageQueue = Queue<List<String>>()
private var messageDisplaying: List<String>? = null
private val timeGaugeCol = Color(0x707070ff) private val timeGaugeCol = Color(0x707070ff)
init { init {
handler.alwaysUpdate = true
} }
override fun updateUI(delta: Float) { 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) if (handler.isOpened)
displayTimer += delta displayTimer += delta
if (displayTimer >= visibleTime) { if (displayTimer >= visibleTime) {
handler.setAsClose() handler.setAsClose()
displayTimer = 0f displayTimer = 0f
messageDisplaying = null
} }
} }
@@ -55,19 +70,29 @@ class Notification : UICanvas() {
blendNormalStraightAlpha(batch) blendNormalStraightAlpha(batch)
fontCol.a = handler.opacity * OPACITY fontCol.a = handler.opacity * OPACITY
val realTextWidth = 12 + if (message.size == 1)
App.fontGame.getWidth(message[0]) if (messageDisplaying != null) {
val realTextWidth = 12 + if (messageDisplaying!!.size == 1)
App.fontGame.getWidth(messageDisplaying!![0])
else else
message.map { App.fontGame.getWidth(it) }.sorted().last() messageDisplaying!!.map { App.fontGame.getWidth(it) }.sorted().last()
val displayedTextWidth = max(240, realTextWidth) val displayedTextWidth = max(240, realTextWidth)
// force the UI to the centre of the screen // force the UI to the centre of the screen
this.posX = (App.scr.width - displayedTextWidth) / 2 this.posX = (App.scr.width - displayedTextWidth) / 2
val textHeight = message.size * App.fontGame.lineHeight 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 // draw time gauge
if (displayTimer != 0f) { if (displayTimer != 0f) {
@@ -80,11 +105,12 @@ class Notification : UICanvas() {
// draw texts // draw texts
batch.color = fontCol batch.color = fontCol
message.forEachIndexed { index, s -> messageDisplaying!!.forEachIndexed { index, s ->
val xoff = 6 + (displayedTextWidth - realTextWidth) / 2 val xoff = 6 + (displayedTextWidth - realTextWidth) / 2
val y = -textHeight + App.fontGame.lineHeight * index val y = -textHeight + App.fontGame.lineHeight * index
App.fontGame.draw(batch, s, LRmargin + xoff, y - 1) App.fontGame.draw(batch, s, LRmargin + xoff, y - 1)
} }
}
// dunno why, it doesn't work without this. // dunno why, it doesn't work without this.
@@ -108,10 +134,7 @@ class Notification : UICanvas() {
} }
fun sendNotification(message: List<String>) { fun sendNotification(message: List<String>) {
this.message = message messageQueue.addLast(message)
handler.openCloseCounter = 0f
handler.opacity = 0f
handler.setAsOpen()
} }
override fun dispose() { override fun dispose() {

View File

@@ -33,6 +33,8 @@ class UIHandler(//var UI: UICanvas,
var uiTogglerFunctionDefault: ((UIHandler) -> Unit)? = null var uiTogglerFunctionDefault: ((UIHandler) -> Unit)? = null
): Disposable { ): Disposable {
var alwaysUpdate = false
companion object { companion object {
private val SHADER_PROG_FRAG = """ private val SHADER_PROG_FRAG = """
#ifdef GL_ES #ifdef GL_ES
@@ -224,7 +226,7 @@ void main() {
//if (closeFired && openCloseCounter > 9) closeFired = false //if (closeFired && openCloseCounter > 9) closeFired = false
if (isVisible) { if (isVisible || alwaysUpdate) {
ui.updateUI(delta) ui.updateUI(delta)
} }
@@ -344,6 +346,8 @@ void main() {
isVisible = true isVisible = true
openFired = true openFired = true
openCloseCounter = 0f
} }
} }
@@ -360,6 +364,8 @@ void main() {
isOpening = false isOpening = false
closeFired = true closeFired = true
openCloseCounter = 0f
} }
} }