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.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<String> = listOf("")
internal val messageQueue = Queue<List<String>>()
private var messageDisplaying: List<String>? = 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<String>) {
this.message = message
handler.openCloseCounter = 0f
handler.opacity = 0f
handler.setAsOpen()
messageQueue.addLast(message)
}
override fun dispose() {

View File

@@ -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
}
}