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,35 +70,46 @@ 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])
else
message.map { App.fontGame.getWidth(it) }.sorted().last()
val displayedTextWidth = max(240, realTextWidth)
// force the UI to the centre of the screen if (messageDisplaying != null) {
this.posX = (App.scr.width - displayedTextWidth) / 2
val textHeight = message.size * App.fontGame.lineHeight 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 // draw time gauge
if (displayTimer != 0f) { if (displayTimer != 0f) {
batch.color = timeGaugeCol batch.color = timeGaugeCol
val time = 1f - (displayTimer / visibleTime) val time = 1f - (displayTimer / visibleTime)
val bw = displayedTextWidth * time val bw = displayedTextWidth * time
val bx = (displayedTextWidth - bw) / 2 val bx = (displayedTextWidth - bw) / 2
Toolkit.drawStraightLine(batch, bx, 2f, bx + bw, 2f, false) Toolkit.drawStraightLine(batch, bx, 2f, bx + bw, 2f, false)
} }
// 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)
}
} }
@@ -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
} }
} }