mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-11 19:14:05 +09:00
new module info ui
This commit is contained in:
@@ -1,72 +1,235 @@
|
||||
package net.torvald.terrarum.modulebasegame.ui
|
||||
|
||||
import com.badlogic.gdx.graphics.Camera
|
||||
import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.Input
|
||||
import com.badlogic.gdx.graphics.*
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import net.torvald.terrarum.App
|
||||
import net.torvald.terrarum.ModMgr
|
||||
import net.torvald.terrarum.Second
|
||||
import net.torvald.terrarum.blendNormal
|
||||
import com.badlogic.gdx.graphics.glutils.FrameBuffer
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
|
||||
import net.torvald.getKeycapConsole
|
||||
import net.torvald.getKeycapPC
|
||||
import net.torvald.terrarum.*
|
||||
import net.torvald.terrarum.langpack.Lang
|
||||
import net.torvald.terrarum.ui.Movement
|
||||
import net.torvald.terrarum.ui.Toolkit
|
||||
import net.torvald.terrarum.ui.UICanvas
|
||||
import net.torvald.terrarum.ui.UIItemList
|
||||
import net.torvald.terrarum.ui.UIItemModuleInfoCell
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
val MODULEINFO_CELL_WIDTH = 480
|
||||
val MODULEINFO_CELL_HEIGHT = 48
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2017-08-01.
|
||||
*/
|
||||
class UITitleModules(val remoCon: UIRemoCon) : UICanvas() {
|
||||
|
||||
override var width: Int
|
||||
get() = Toolkit.drawWidth
|
||||
set(value) {}
|
||||
override var height: Int
|
||||
get() = App.scr.height
|
||||
set(value) {}
|
||||
override var openCloseTime: Second = 0f
|
||||
|
||||
private val moduleAreaHMargin = 48
|
||||
private val moduleAreaBorder = 8
|
||||
|
||||
override var width = 600//App.scr.width - UIRemoCon.remoConWidth - moduleAreaHMargin
|
||||
override var height = App.scr.height - moduleAreaHMargin * 2
|
||||
private val shapeRenderer = ShapeRenderer()
|
||||
|
||||
|
||||
private val moduleInfoCells = ArrayList<UIItemModuleInfoCell>()
|
||||
// build module list
|
||||
internal val uiWidth = MODULEINFO_CELL_WIDTH
|
||||
internal val uiX = (width - uiWidth) / 2
|
||||
|
||||
internal val textH = App.fontGame.lineHeight.toInt()
|
||||
|
||||
internal val cellGap = 20
|
||||
internal val cellInterval = cellGap + MODULEINFO_CELL_HEIGHT
|
||||
internal val gradAreaHeight = 32
|
||||
|
||||
internal val titleTextPosY: Int = App.scr.tvSafeGraphicsHeight + 10
|
||||
internal val titleTopGradStart: Int = titleTextPosY + textH
|
||||
internal val titleTopGradEnd: Int = titleTopGradStart + gradAreaHeight
|
||||
internal val titleBottomGradStart: Int = height - App.scr.tvSafeGraphicsHeight - gradAreaHeight
|
||||
internal val titleBottomGradEnd: Int = titleBottomGradStart + gradAreaHeight
|
||||
internal val controlHelperY: Int = titleBottomGradStart + gradAreaHeight - textH
|
||||
|
||||
|
||||
private val controlHelp: String
|
||||
get() = if (App.environment == RunningEnvironment.PC)
|
||||
"${getKeycapPC(App.getConfigInt("control_key_up"))}${getKeycapPC(App.getConfigInt("control_key_down"))}" +
|
||||
" ${Lang["MENU_CONTROLS_SCROLL"]}"
|
||||
else
|
||||
"${getKeycapConsole('R')} ${Lang["MENU_CONTROLS_SCROLL"]}"
|
||||
|
||||
|
||||
private var scrollAreaHeight = height - 2 * App.scr.tvSafeGraphicsHeight - 64
|
||||
private var listScroll = 0 // only update when animation is finished
|
||||
private var savesVisible = (scrollAreaHeight + cellGap) / cellInterval
|
||||
|
||||
private var uiScroll = 0f
|
||||
private var scrollFrom = 0
|
||||
private var scrollTarget = 0
|
||||
private var scrollAnimCounter = 0f
|
||||
private val scrollAnimLen = 0.1f
|
||||
|
||||
private var sliderFBO = FrameBuffer(Pixmap.Format.RGBA8888, uiWidth + 10, height, true)
|
||||
|
||||
private val moduleCells = ArrayList<UIItemModuleInfoCell>()
|
||||
|
||||
|
||||
private var showSpinner = false
|
||||
|
||||
init {
|
||||
ModMgr.moduleInfo.toList().sortedBy { it.second.order }.forEachIndexed { index, it ->
|
||||
moduleInfoCells.add(UIItemModuleInfoCell(
|
||||
this,
|
||||
it.first,
|
||||
width - 2 * moduleAreaBorder,
|
||||
0, 0 // placeholder
|
||||
))
|
||||
try {
|
||||
// read savegames
|
||||
var savegamesCount = 0
|
||||
ModMgr.loadOrder.forEachIndexed { index, s ->
|
||||
val x = uiX + if (App.getConfigBoolean("fx_streamerslayout")) App.scr.chatWidth / 2 else 0
|
||||
val y = titleTopGradEnd + cellInterval * savegamesCount
|
||||
try {
|
||||
moduleCells.add(UIItemModuleInfoCell(this, index, x, y))
|
||||
savegamesCount += 1
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
System.err.println("[UILoadDemoSavefiles] Error while loading module info for '$s'")
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val moduleArea = UIItemList<UIItemModuleInfoCell>(
|
||||
this,
|
||||
moduleInfoCells,
|
||||
(App.scr.width - this.width) / 2, moduleAreaHMargin,
|
||||
width,
|
||||
height,
|
||||
inactiveCol = Color.WHITE,
|
||||
border = moduleAreaBorder
|
||||
)
|
||||
|
||||
|
||||
init {
|
||||
uiItems.add(moduleArea)
|
||||
catch (e: UninitializedPropertyAccessException) {}
|
||||
}
|
||||
|
||||
override fun updateUI(delta: Float) {
|
||||
moduleArea.update(delta)
|
||||
if (scrollTarget != listScroll) {
|
||||
if (scrollAnimCounter < scrollAnimLen) {
|
||||
scrollAnimCounter += delta
|
||||
uiScroll = Movement.fastPullOut(
|
||||
scrollAnimCounter / scrollAnimLen,
|
||||
listScroll * cellInterval.toFloat(),
|
||||
scrollTarget * cellInterval.toFloat()
|
||||
)
|
||||
}
|
||||
else {
|
||||
scrollAnimCounter = 0f
|
||||
listScroll = scrollTarget
|
||||
uiScroll = cellInterval.toFloat() * scrollTarget
|
||||
}
|
||||
}
|
||||
|
||||
for (index in 0 until moduleCells.size) {
|
||||
val it = moduleCells[index]
|
||||
if (index in listScroll - 2 until listScroll + savesVisible + 2) {
|
||||
// re-position
|
||||
it.posY = (it.initialY - uiScroll).roundToInt()
|
||||
it.update(delta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun hide() {
|
||||
moduleCells.forEach { it.dispose() }
|
||||
moduleCells.clear()
|
||||
}
|
||||
|
||||
override fun renderUI(batch: SpriteBatch, camera: Camera) {
|
||||
batch.color = Color.WHITE
|
||||
blendNormal(batch)
|
||||
Toolkit.drawBoxBorder(batch, moduleArea.posX, moduleArea.posY, width, height)
|
||||
moduleArea.render(batch, camera)
|
||||
batch.end()
|
||||
|
||||
lateinit var savePixmap: Pixmap
|
||||
sliderFBO.inAction(camera as OrthographicCamera, batch) {
|
||||
gdxClearAndSetBlend(0f, 0f, 0f, 0f)
|
||||
|
||||
setCameraPosition(batch, camera, 0f, 0f)
|
||||
batch.color = Color.WHITE
|
||||
batch.inUse {
|
||||
for (index in 0 until moduleCells.size) {
|
||||
val it = moduleCells[index]
|
||||
if (index in listScroll - 2 until listScroll + savesVisible + 2) {
|
||||
it.render(batch, camera)
|
||||
}
|
||||
}
|
||||
}
|
||||
savePixmap = Pixmap.createFromFrameBuffer(0, 0, sliderFBO.width, sliderFBO.height)
|
||||
savePixmap.blending = Pixmap.Blending.None
|
||||
}
|
||||
|
||||
|
||||
// implement "wipe-out" by CPU-rendering (*deep exhale*)
|
||||
//savePixmap.setColor(1f,1f,1f,0f)
|
||||
savePixmap.setColor(0f, 0f, 0f, 0f)
|
||||
savePixmap.fillRectangle(0, savePixmap.height - titleTopGradStart, savePixmap.width, titleTopGradStart)
|
||||
// top grad
|
||||
for (y in titleTopGradStart until titleTopGradEnd) {
|
||||
val alpha = (y - titleTopGradStart).toFloat() / gradAreaHeight
|
||||
for (x in 0 until savePixmap.width) {
|
||||
val col = savePixmap.getPixel(x, savePixmap.height - y)
|
||||
val blendAlpha = (col.and(0xFF) * alpha).roundToInt()
|
||||
savePixmap.drawPixel(x, savePixmap.height - y, col.and(0xFFFFFF00.toInt()) or blendAlpha)
|
||||
}
|
||||
}
|
||||
// bottom grad
|
||||
for (y in titleBottomGradStart until titleBottomGradEnd) {
|
||||
val alpha = 1f - ((y - titleBottomGradStart).toFloat() / gradAreaHeight)
|
||||
for (x in 0 until savePixmap.width) {
|
||||
val col = savePixmap.getPixel(x, savePixmap.height - y)
|
||||
val blendAlpha = (col.and(0xFF) * alpha).roundToInt()
|
||||
savePixmap.drawPixel(x, savePixmap.height - y, col.and(0xFFFFFF00.toInt()) or blendAlpha)
|
||||
}
|
||||
}
|
||||
savePixmap.setColor(0f, 0f, 0f, 0f)
|
||||
savePixmap.fillRectangle(0, 0, savePixmap.width, height - titleBottomGradEnd + 1)
|
||||
|
||||
|
||||
|
||||
setCameraPosition(batch, camera, 0f, 0f)
|
||||
val saveTex = Texture(savePixmap)
|
||||
batch.inUse {
|
||||
batch.color = Color.WHITE
|
||||
batch.draw(saveTex, (width - uiWidth - 10) / 2f, 0f)
|
||||
// draw texts
|
||||
val loadGameTitleStr = Lang["MENU_MODULES"]// + "$EMDASH$hash"
|
||||
// "Game Load"
|
||||
App.fontUITitle.draw(batch, loadGameTitleStr, (width - App.fontGame.getWidth(loadGameTitleStr)).div(2).toFloat(), titleTextPosY.toFloat())
|
||||
// Control help
|
||||
App.fontGame.draw(batch, controlHelp, uiX.toFloat(), controlHelperY.toFloat())
|
||||
}
|
||||
|
||||
saveTex.dispose()
|
||||
savePixmap.dispose()
|
||||
|
||||
batch.begin()
|
||||
|
||||
}
|
||||
|
||||
override fun doOpening(delta: Float) {
|
||||
override fun keyDown(keycode: Int): Boolean {
|
||||
if (this.isVisible) {
|
||||
if ((keycode == Input.Keys.UP || keycode == App.getConfigInt("control_key_up")) && scrollTarget > 0) {
|
||||
scrollFrom = listScroll
|
||||
scrollTarget -= 1
|
||||
scrollAnimCounter = 0f
|
||||
}
|
||||
else if ((keycode == Input.Keys.DOWN || keycode == App.getConfigInt("control_key_down")) && scrollTarget < moduleCells.size - savesVisible) {
|
||||
scrollFrom = listScroll
|
||||
scrollTarget += 1
|
||||
scrollAnimCounter = 0f
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun scrolled(amountX: Float, amountY: Float): Boolean {
|
||||
if (this.isVisible) {
|
||||
if (amountY <= -1f && scrollTarget > 0) {
|
||||
scrollFrom = listScroll
|
||||
scrollTarget -= 1
|
||||
scrollAnimCounter = 0f
|
||||
}
|
||||
else if (amountY >= 1f && scrollTarget < moduleCells.size - savesVisible) {
|
||||
scrollFrom = listScroll
|
||||
scrollTarget += 1
|
||||
scrollAnimCounter = 0f
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
override fun doOpening(delta: Float) {
|
||||
}
|
||||
|
||||
override fun doClosing(delta: Float) {
|
||||
@@ -76,9 +239,32 @@ class UITitleModules(val remoCon: UIRemoCon) : UICanvas() {
|
||||
}
|
||||
|
||||
override fun endClosing(delta: Float) {
|
||||
listScroll = 0
|
||||
scrollTarget = 0
|
||||
uiScroll = 0f
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
try { shapeRenderer.dispose() } catch (e: IllegalArgumentException) {}
|
||||
try { sliderFBO.dispose() } catch (e: IllegalArgumentException) {}
|
||||
}
|
||||
|
||||
override fun resize(width: Int, height: Int) {
|
||||
super.resize(width, height)
|
||||
scrollAreaHeight = height - 2 * App.scr.tvSafeGraphicsHeight - 64
|
||||
savesVisible = (scrollAreaHeight + cellInterval) / (cellInterval + MODULEINFO_CELL_HEIGHT)
|
||||
|
||||
listScroll = 0
|
||||
scrollTarget = 0
|
||||
uiScroll = 0f
|
||||
|
||||
sliderFBO.dispose()
|
||||
sliderFBO = FrameBuffer(Pixmap.Format.RGBA8888, uiWidth + 10, height, true)
|
||||
}
|
||||
|
||||
private fun setCameraPosition(batch: SpriteBatch, camera: Camera, newX: Float, newY: Float) {
|
||||
camera.position.set((-newX + App.scr.halfw).round(), (-newY + App.scr.halfh).round(), 0f)
|
||||
camera.update()
|
||||
batch.projectionMatrix = camera.combined
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user