disposable singletons to an array in loader; single float for both notification and tooltip

This commit is contained in:
minjaesong
2019-05-24 20:38:35 +09:00
parent d08aae5db0
commit 3f692da03c
13 changed files with 116 additions and 69 deletions

View File

@@ -0,0 +1,43 @@
package net.torvald.terrarum.modulebasegame.ui
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.utils.Disposable
import net.torvald.terrarum.AppLoader
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2019-05-24.
*/
object FloatDrawer : Disposable {
val tile = TextureRegionPack("assets/graphics/gui/message_black_tileable.tga", 8, 8)
init {
AppLoader.disposableSingletonsPool.add(this)
}
/**
* Draws the Float at given position in given size. The size is that of the centre area, excluding the edges. Size of the edges are 8x8 pixels.
*/
operator fun invoke(batch: SpriteBatch, x: Float, y: Float, w: Float, h: Float) {
// centre area
batch.draw(tile.get(1, 1), x, y, w, h)
// edges
batch.draw(tile.get(1, 0), x, y - tile.tileH, w, tile.tileH.toFloat())
batch.draw(tile.get(1, 2), x, y + h, w, tile.tileH.toFloat())
batch.draw(tile.get(0, 1), x - tile.tileW, y, tile.tileW.toFloat(), h)
batch.draw(tile.get(2, 1), x + w, y, tile.tileW.toFloat(), h)
// corners
batch.draw(tile.get(0, 0), x - tile.tileW, y - tile.tileH)
batch.draw(tile.get(2, 0), x + w, y - tile.tileH)
batch.draw(tile.get(2, 2), x + w, y + h)
batch.draw(tile.get(0, 2), x - tile.tileW, y + h)
}
override fun dispose() {
tile.dispose()
}
}

View File

@@ -8,15 +8,12 @@ import net.torvald.terrarum.Second
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.blendNormal
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2016-01-23.
*/
class Notification : UICanvas() {
private val segment = SEGMENT_BLACK
private var fontCol: Color = Color.WHITE // assuming alpha of 1.0
override var openCloseTime: Second = OPEN_CLOSE_TIME
@@ -28,14 +25,14 @@ class Notification : UICanvas() {
override var width: Int = 500
override var height: Int = segment.tileH
override var height: Int = 0
private val visibleTime = Math.min(
AppLoader.getConfigInt("notificationshowuptime"),
SHOWUP_MAX
) / 1000f
private var displayTimer = 0f
internal var message: Array<String> = Array(MESSAGES_DISPLAY) { "" }
internal var message: List<String> = listOf("")
init {
@@ -51,9 +48,6 @@ class Notification : UICanvas() {
}
}
private val textAreaHeight = 48f
private val imageToTextAreaDelta = (segment.tileH - textAreaHeight) / 2
private val drawColor = Color(1f,1f,1f,1f)
override fun renderUI(batch: SpriteBatch, camera: Camera) {
@@ -64,26 +58,22 @@ class Notification : UICanvas() {
val realTextWidth = 12 + if (message.size == 1)
Terrarum.fontGame.getWidth(message[0])
else
maxOf(Terrarum.fontGame.getWidth(message[0]), Terrarum.fontGame.getWidth(message[1]))
message.map { Terrarum.fontGame.getWidth(it) }.sorted().last()
val displayedTextWidth = maxOf(240, realTextWidth)
// force the UI to the centre of the screen
this.posX = (Terrarum.WIDTH - displayedTextWidth) / 2
val textHeight = message.size * Terrarum.fontGame.lineHeight
batch.color = drawColor
batch.draw(segment.get(0, 0), -segment.tileW.toFloat(), 0f)
batch.draw(segment.get(1, 0), 0f, 0f, displayedTextWidth.toFloat(), segment.tileH.toFloat())
batch.draw(segment.get(2, 0), displayedTextWidth.toFloat(), 0f)
FloatDrawer(batch, 0f, -textHeight, displayedTextWidth.toFloat(), textHeight)
batch.color = fontCol
message.forEachIndexed { index, s ->
val xoff = 6 + (displayedTextWidth - realTextWidth) / 2
val y = if (message.size == 1)
-2 + imageToTextAreaDelta + 0.5f * (textAreaHeight / 2) + (textAreaHeight / 2 - Terrarum.fontGame.lineHeight) / 2
else
-1 + imageToTextAreaDelta + index * (textAreaHeight / 2) + (textAreaHeight / 2 - Terrarum.fontGame.lineHeight) / 2
val y = -textHeight + Terrarum.fontGame.lineHeight * index
Terrarum.fontGame.draw(batch, s, LRmargin + xoff, y)
}
@@ -109,7 +99,7 @@ class Notification : UICanvas() {
endClosingFade(this)
}
fun sendNotification(message: Array<String>) {
fun sendNotification(message: List<String>) {
this.message = message
handler.openCloseCounter = 0f
handler.opacity = 0f
@@ -121,12 +111,6 @@ class Notification : UICanvas() {
companion object {
// private int messagesShowingIndex = 0;
val MESSAGES_DISPLAY = 2
val OPEN_CLOSE_TIME = 0.16f
// will be disposed by Terrarum (application main instance)
val SEGMENT_BLACK = TextureRegionPack("assets/graphics/gui/message_black.tga", 8, 56)
val SEGMENT_WHITE = TextureRegionPack("assets/graphics/gui/message_white.tga", 8, 56)
}
}

View File

@@ -2,12 +2,10 @@ package net.torvald.terrarum.modulebasegame.ui
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.Second
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2017-11-25.
@@ -22,38 +20,33 @@ class UITooltip : UICanvas() {
msgWidth = font.getWidth(value)
}
private val textures = TextureRegionPack("assets/graphics/gui/tooltip_black.tga", 8, 36)
private val font = Terrarum.fontGame
private var msgWidth = 0
val textMarginX = 4
override var width: Int
get() = msgWidth + (textMarginX + textures.tileW) * 2
get() = msgWidth + (textMarginX + FloatDrawer.tile.tileW) * 2
set(value) { throw Error("You are not supposed to set the width of the tooltip manually.") }
override var height: Int
get() = textures.tileH
get() = FloatDrawer.tile.tileH * 2 + font.lineHeight.toInt()
set(value) { throw Error("You are not supposed to set the height of the tooltip manually.") }
init {
textures.texture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
}
override fun renderUI(batch: SpriteBatch, camera: Camera) {
val mouseX = 4f
val mouseY = 6f
val tooltipY = mouseY - textures.tileH
val tooltipY = mouseY - height
val txtW = msgWidth + 2f * textMarginX
batch.color = Color.WHITE
batch.draw(textures.get(0, 0), mouseX, tooltipY)
batch.draw(textures.get(1, 0), mouseX + textures.tileW, tooltipY, txtW, height.toFloat())
batch.draw(textures.get(2, 0), mouseX + textures.tileW + txtW, tooltipY)
font.draw(batch, message, mouseX + textures.tileW + textMarginX, mouseY - textures.tileH + (textures.tileH - font.lineHeight) / 2)
FloatDrawer(batch, mouseX - textMarginX, tooltipY, txtW, font.lineHeight)
font.draw(batch, message,
mouseX,
mouseY - height
)
}
override fun updateUI(delta: Float) {
@@ -73,7 +66,6 @@ class UITooltip : UICanvas() {
}
override fun dispose() {
textures.dispose()
}
}