From 88a1beb2719a2d71f03cf2135044fb74cdbd1fce Mon Sep 17 00:00:00 2001 From: minjaesong Date: Wed, 6 Feb 2019 05:18:00 +0900 Subject: [PATCH] player info cell, INVENTORY UI LEAKS MEMORY --- .../gameactors/ActorInventory.kt | 2 +- .../modulebasegame/ui/ItemSlotImageFactory.kt | 4 + .../modulebasegame/ui/UIInventoryFull.kt | 31 ++-- .../ui/UIItemInventoryDynamicList.kt | 5 +- .../ui/UIItemInventoryEquippedView.kt | 8 +- .../modulebasegame/ui/UIItemPlayerInfoCell.kt | 136 ++++++++++++++++++ .../modulebasegame/ui/UITitleModules.kt | 1 + .../terrarum/serialise/ReadWorldInfo.kt | 16 ++- src/net/torvald/terrarum/tests/Base32Test.kt | 21 +++ .../terrarum/ui/UIItemModuleInfoCell.kt | 88 ++++++++++++ work_files/DataFormats/Savegame metadata.txt | 3 +- 11 files changed, 294 insertions(+), 21 deletions(-) create mode 100644 src/net/torvald/terrarum/modulebasegame/ui/UIItemPlayerInfoCell.kt create mode 100644 src/net/torvald/terrarum/tests/Base32Test.kt create mode 100644 src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt index 8a51b4277..b08f01234 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt @@ -39,7 +39,7 @@ class ActorInventory(val actor: Pocketed, var maxCapacity: Int, var capacityMode val itemList = ArrayList() val quickSlot = Array(UIQuickslotBar.SLOT_COUNT) { null } // 0: Slot 1, 9: Slot 10 - var currency = 0 // unified currency for whole civs; Dwarf Fortress approach seems too complicated + var wallet = 0 // unified currency for whole civs; Dwarf Fortress approach seems too complicated init { } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/ItemSlotImageFactory.kt b/src/net/torvald/terrarum/modulebasegame/ui/ItemSlotImageFactory.kt index 3ffef6884..f76735ea6 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/ItemSlotImageFactory.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/ItemSlotImageFactory.kt @@ -20,9 +20,13 @@ object ItemSlotImageFactory { val CELLCOLOUR_BLACK_OPAQUE = Color(0x404040_FF) val CELLCOLOUR_WHITE_OPAQUE = Color(0xC0C0C0_FF.toInt()) + /** Blend mode: normal */ val CELLCOLOUR_BLACK = Color(0x404040_88) val CELLCOLOUR_WHITE = Color(0xC0C0C0_88.toInt()) + /** Blend mode: screen */ + val CELLCOLOUR_BLACK_ACTIVE = Color(0x282828ff) + val slotImage = TextureRegionPack(Gdx.files.internal("./assets/graphics/gui/quickbar/item_slots_atlas.tga"), 38, 38) // must have same w/h as slotLarge fun produce(isBlack: Boolean, number: Int = 10, item: GameItem?): ItemSlotImage { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt index e746506ed..41c07eb9a 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt @@ -7,11 +7,15 @@ import com.badlogic.gdx.graphics.GL20 import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.glutils.ShapeRenderer import net.torvald.terrarum.* +import net.torvald.terrarum.Terrarum.PLAYER_REF_ID +import net.torvald.terrarum.gameactors.Actor import net.torvald.terrarum.gameactors.ActorWBMovable import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.modulebasegame.Ingame import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory.Companion.CAPACITY_MODE_NO_ENCUMBER +import net.torvald.terrarum.modulebasegame.gameactors.IngamePlayer import net.torvald.terrarum.modulebasegame.gameactors.Pocketed +import net.torvald.terrarum.serialise.ReadWorldInfo import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UIItem import net.torvald.terrarum.ui.UIItemTextButtonList @@ -30,6 +34,8 @@ class UIInventoryFull( doNotWarnConstant: Boolean = false ) : UICanvas(toggleKeyLiteral, toggleButtonLiteral, customPositioning, doNotWarnConstant) { + // FIXME something's causing memory leak + private val debugvals = false override var width: Int = Terrarum.WIDTH @@ -108,7 +114,7 @@ class UIInventoryFull( 109 + (Terrarum.HEIGHT - internalHeight) / 2 ) private val gameMenuListWidth = 400 - private val gameMenuListHeight = 40 * 5 + private val gameMenuListHeight = UIItemPlayerInfoCell.HEIGHT private val gameMenuCharInfoHeight = 64 + 40 // no top margin, 40 bottom margin private val gameMenuListTotalHeight = gameMenuListHeight + gameMenuCharInfoHeight private val gameMenuButtons = UIItemTextButtonList( @@ -154,6 +160,8 @@ class UIInventoryFull( } } + private val testPlayerInfoCell: UIItemPlayerInfoCell + init { addItem(categoryBar) itemList.let { addItem(it) } @@ -166,6 +174,15 @@ class UIInventoryFull( itemList.rebuild() // have to manually rebuild, too! } + testPlayerInfoCell = UIItemPlayerInfoCell( + this, + ReadWorldInfo.SaveMetaData("The Yucky Panopticon", 0L,0L,0L,0L,0L, (actor as Actor).referenceID!!, 84873L, 1500000000L, 2000000000L, 2, byteArrayOf(0), byteArrayOf(0), byteArrayOf(0), playerWallet = 13372), + gameMenuListWidth, + Terrarum.WIDTH + ((Terrarum.WIDTH - 400) / 2) + menuScrOffX.toInt(), + (itemList.height - gameMenuListTotalHeight) / 2 + itemList.posY, + false, + Terrarum.ingame!!.getActorByID(PLAYER_REF_ID) as IngamePlayer + ) rebuildList() @@ -174,6 +191,7 @@ class UIInventoryFull( addToTransitionalGroup(itemList) addToTransitionalGroup(equipped) addToTransitionalGroup(gameMenuButtons) + addToTransitionalGroup(testPlayerInfoCell) // make gameMenuButtons work gameMenuButtons.selectionChangeListener = { old, new -> @@ -184,6 +202,7 @@ class UIInventoryFull( Gdx.app.exit() } } + } private var offsetX = ((Terrarum.WIDTH - internalWidth) / 2).toFloat() @@ -341,15 +360,7 @@ class UIInventoryFull( gameMenuButtons.render(batch, camera) // character info window - - // !! DUMMY !! - batch.color = itemList.backColour - batch.fillRect( - ((Terrarum.WIDTH - 400) / 2) + menuScrOffX, - (itemList.height - gameMenuListTotalHeight) / 2 + itemList.posY.toFloat(), - gameMenuListWidth.toFloat(), - 64f - ) + testPlayerInfoCell.render(batch, camera) } private fun renderScreenInventory(batch: SpriteBatch, camera: Camera) { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryDynamicList.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryDynamicList.kt index 3da7d765b..ba7e40771 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryDynamicList.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryDynamicList.kt @@ -11,6 +11,7 @@ import net.torvald.terrarum.modulebasegame.Ingame import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory import net.torvald.terrarum.modulebasegame.gameactors.InventoryPair import net.torvald.terrarum.modulebasegame.ui.ItemSlotImageFactory.CELLCOLOUR_BLACK +import net.torvald.terrarum.modulebasegame.ui.ItemSlotImageFactory.CELLCOLOUR_BLACK_ACTIVE import net.torvald.terrarum.ui.UIItem import net.torvald.terrarum.ui.UIItemImageButton import java.util.* @@ -99,7 +100,7 @@ class UIItemInventoryDynamicList( item = null, amount = UIItemInventoryElem.UNIQUE_ITEM_HAS_NO_AMOUNT, itemImage = null, - mouseoverBackCol = Color(0x282828_ff), + mouseoverBackCol = Color(CELLCOLOUR_BLACK_ACTIVE), mouseoverBackBlendMode = BlendMode.SCREEN, backCol = backColour, backBlendMode = BlendMode.NORMAL, @@ -116,7 +117,7 @@ class UIItemInventoryDynamicList( item = null, amount = UIItemInventoryElem.UNIQUE_ITEM_HAS_NO_AMOUNT, itemImage = null, - mouseoverBackCol = Color(0x282828_ff), + mouseoverBackCol = Color(CELLCOLOUR_BLACK_ACTIVE), mouseoverBackBlendMode = BlendMode.SCREEN, backCol = backColour, backBlendMode = BlendMode.NORMAL, diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt index 9d3ce51bf..6e2d05503 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt @@ -9,6 +9,7 @@ import net.torvald.terrarum.itemproperties.GameItem import net.torvald.terrarum.itemproperties.ItemCodex import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory import net.torvald.terrarum.modulebasegame.ui.ItemSlotImageFactory.CELLCOLOUR_BLACK +import net.torvald.terrarum.modulebasegame.ui.ItemSlotImageFactory.CELLCOLOUR_BLACK_ACTIVE import net.torvald.terrarum.ui.UIItem /** @@ -28,6 +29,7 @@ class UIItemInventoryEquippedView( companion object { val WIDTH = 2 * UIItemInventoryElemSimple.height + UIItemInventoryDynamicList.listGap val HEIGHT = UIItemInventoryDynamicList.HEIGHT + val SPRITE_DRAW_COL = Color(0xddddddff.toInt()) } private val listGap = 8 @@ -48,7 +50,7 @@ class UIItemInventoryEquippedView( item = null, amount = UIItemInventoryElem.UNIQUE_ITEM_HAS_NO_AMOUNT, itemImage = null, - mouseoverBackCol = Color(0x282828_ff), + mouseoverBackCol = Color(CELLCOLOUR_BLACK_ACTIVE), mouseoverBackBlendMode = BlendMode.SCREEN, backCol = CELLCOLOUR_BLACK, backBlendMode = BlendMode.NORMAL, @@ -61,8 +63,6 @@ class UIItemInventoryEquippedView( itemGrid.forEach { it.update(delta) } } - private val spriteDrawCol = Color(0xddddddff.toInt()) - // deal with the moving position override var oldPosX = posX override var oldPosY = posY @@ -86,7 +86,7 @@ class UIItemInventoryEquippedView( sprite?.let { blendNormal(batch) - batch.color = spriteDrawCol + batch.color = SPRITE_DRAW_COL batch.draw( it.textureRegion.get(0, 0), posX + (width - it.cellWidth).div(2).toFloat(), diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemPlayerInfoCell.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemPlayerInfoCell.kt new file mode 100644 index 000000000..255a65b97 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemPlayerInfoCell.kt @@ -0,0 +1,136 @@ +package net.torvald.terrarum.modulebasegame.ui + +import com.badlogic.gdx.graphics.Camera +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import net.torvald.terrarum.Terrarum +import net.torvald.terrarum.blendNormal +import net.torvald.terrarum.blendScreen +import net.torvald.terrarum.fillRect +import net.torvald.terrarum.langpack.Lang +import net.torvald.terrarum.modulebasegame.gameactors.IngamePlayer +import net.torvald.terrarum.serialise.ReadWorldInfo +import net.torvald.terrarum.ui.UICanvas +import net.torvald.terrarum.ui.UIItem +import java.util.* + +/** + * Created by minjaesong on 2019-02-05. + * + * If ingamePlayer is specified, sprite of current ingamePlayer will be drawn, instead of the SaveMetaData's thumbnail. + */ +class UIItemPlayerInfoCell( + parent: UICanvas, + val saveInfo: ReadWorldInfo.SaveMetaData, + override val width: Int, + override var posX: Int, + override var posY: Int, + var highlightable: Boolean, + var ingamePlayer: IngamePlayer? = null +) : UIItem(parent) { + + // deal with the moving position + override var oldPosX = posX + override var oldPosY = posY + + override val height = HEIGHT + + companion object { + const val HEIGHT = 64 + } + + private val spriteAreaWidth = 56 + private val spriteToNameAreaGap = 8 + private val edgeGap = 8 + + private val backColInactive = ItemSlotImageFactory.CELLCOLOUR_BLACK + private val backColActive = ItemSlotImageFactory.CELLCOLOUR_BLACK_ACTIVE + + private val textRow1 = (((height / 2) - Terrarum.fontGame.lineHeight) / 2).toFloat() + private val textRow2 = textRow1 + (height / 2) + + private val creationTimeStr: String + private val modificationTimeStr: String + + private val worldCountStr: String + private val worldCountStrWidth: Int + + init { + val cal = Calendar.getInstance() + + cal.timeInMillis = saveInfo.creationTime * 1000 + creationTimeStr = "${cal[Calendar.YEAR]}-" + + "${cal[Calendar.MONTH].toString().padStart(2,'0')}-" + + "${cal[Calendar.DATE].toString().padStart(2,'0')}" + + cal.timeInMillis = saveInfo.lastPlayTime * 1000 + modificationTimeStr = "${cal[Calendar.YEAR]}-" + + "${cal[Calendar.MONTH].toString().padStart(2,'0')}-" + + "${cal[Calendar.DATE].toString().padStart(2,'0')}" + + + worldCountStr = Lang["CONTEXT_WORLD_COUNT"] + saveInfo.worldCount + worldCountStrWidth = Terrarum.fontGame.getWidth(worldCountStr) + } + + override fun render(batch: SpriteBatch, camera: Camera) { + // background + if (highlightable && mouseUp) { + batch.color = backColActive + blendScreen(batch) + } + else { + batch.color = backColInactive + blendNormal(batch) + } + + batch.fillRect(posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat()) + + + blendNormal(batch) + /*batch.color = SPRITE_DRAW_COL + + // character sprite image + if (ingamePlayer != null) { + val spriteImage = ingamePlayer?.sprite?.textureRegion?.get(0,0) + batch.draw(spriteImage, + ((spriteImage?.regionWidth ?: 2) - spriteAreaWidth).div(2).toFloat(), + ((spriteImage?.regionHeight ?: 2) - height).div(2).toFloat() + ) + } + else { + val spriteImage = saveInfo.thumbnail + batch.draw(spriteImage, + (spriteImage.width - spriteAreaWidth).div(2).toFloat(), + (spriteImage.height - height).div(2).toFloat() + ) + } + + // texts // + + // name + batch.color = Color.WHITE + Terrarum.fontGame.draw(batch, saveInfo.playerName, spriteAreaWidth + spriteToNameAreaGap.toFloat(), textRow1) + // creation and modification time + Terrarum.fontGame.draw(batch, "$creationTimeStr/$modificationTimeStr", spriteAreaWidth + spriteToNameAreaGap.toFloat(), textRow2) + // world count + Terrarum.fontGame.draw(batch, worldCountStr, width - (edgeGap + worldCountStrWidth).toFloat(), textRow1) + // wallet + val walletStr = "¤ " + (ingamePlayer?.inventory?.wallet ?: saveInfo.playerWallet) + val walletStrWidth = Terrarum.fontGame.getWidth(walletStr) + Terrarum.fontGame.draw(batch, walletStr, width - (edgeGap + walletStrWidth).toFloat(), textRow2) + */ + } + + override fun update(delta: Float) { + super.update(delta) + + + + oldPosX = posX + oldPosY = posY + } + + override fun dispose() { + + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UITitleModules.kt b/src/net/torvald/terrarum/modulebasegame/ui/UITitleModules.kt index e0b0bcc15..7adbbfc08 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UITitleModules.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UITitleModules.kt @@ -9,6 +9,7 @@ import net.torvald.terrarum.Terrarum import net.torvald.terrarum.blendNormal import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UIItemList +import net.torvald.terrarum.ui.UIItemModuleInfoCell /** * Created by minjaesong on 2017-08-01. diff --git a/src/net/torvald/terrarum/serialise/ReadWorldInfo.kt b/src/net/torvald/terrarum/serialise/ReadWorldInfo.kt index 87945d43e..0614a4445 100644 --- a/src/net/torvald/terrarum/serialise/ReadWorldInfo.kt +++ b/src/net/torvald/terrarum/serialise/ReadWorldInfo.kt @@ -1,9 +1,11 @@ package net.torvald.terrarum.serialise +import com.badlogic.gdx.graphics.Pixmap +import com.badlogic.gdx.graphics.Texture +import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.DiskSkimmer.Companion.read import java.io.File import java.io.FileInputStream import java.util.* -import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.DiskSkimmer.Companion.read object ReadWorldInfo { @@ -55,7 +57,7 @@ object ReadWorldInfo { } - internal data class SaveMetaData( + data class SaveMetaData( val worldName: String, val terrainSeed: Long, val rngS0: Long, @@ -69,6 +71,14 @@ object ReadWorldInfo { val totalPlayTime: Int, val worldinfo1Hash: ByteArray, val worldInfo2Hash: ByteArray, - val worldInfo3Hash: ByteArray + val worldInfo3Hash: ByteArray, + + // gzipped TGA in meta + val thumbnail: Texture = Texture(2, 2, Pixmap.Format.RGBA8888), + // skim through the virtualdisk entries + val worldCount: Int = 1, + // read from the entry file + val playerName: String = "Savegame", + val playerWallet: Int = 0 ) } \ No newline at end of file diff --git a/src/net/torvald/terrarum/tests/Base32Test.kt b/src/net/torvald/terrarum/tests/Base32Test.kt new file mode 100644 index 000000000..6228915ad --- /dev/null +++ b/src/net/torvald/terrarum/tests/Base32Test.kt @@ -0,0 +1,21 @@ +import net.torvald.terrarum.utils.PasswordBase32 +import java.nio.charset.Charset + +object Base32Test { + + operator fun invoke() { + val testStr = "정 참판 양반댁 규수 혼례 치른 날. 123456709".toByteArray() + val pwd = "béchamel".toByteArray() + + val enc = PasswordBase32.encode(testStr, pwd) + val dec = PasswordBase32.decode(enc, testStr.size, pwd) + + println("Encoded text: $enc") + println("Decoded text: ${dec.toString(Charset.defaultCharset())}") + } + +} + +fun main(args: Array) { + Base32Test.invoke() +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt b/src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt new file mode 100644 index 000000000..bb5916fab --- /dev/null +++ b/src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt @@ -0,0 +1,88 @@ +package net.torvald.terrarum.ui + +import com.badlogic.gdx.graphics.Camera +import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import net.torvald.terrarum.ModMgr +import net.torvald.terrarum.Terrarum +import net.torvald.terrarum.blendNormal +import net.torvald.terrarum.floor + +class UIItemModuleInfoCell( + parent: UICanvas, + var moduleName: String, + override val width: Int, + override var posX: Int, + override var posY: Int +) : UIItem(parent) { + + // deal with the moving position + override var oldPosX = posX + override var oldPosY = posY + + override val height: Int = Terrarum.fontGame.lineHeight.toInt() * 2 + + private val numberAreaWidth = Terrarum.fontSmallNumbers.W * 3 + 4 + + override fun render(batch: SpriteBatch, camera: Camera) { + blendNormal(batch) + + if (ModMgr.moduleInfo.containsKey(moduleName)) { + val modInfo = ModMgr.moduleInfo[moduleName]!! + + // print load order index + batch.color = Color(0xccccccff.toInt()) + var strlen = Terrarum.fontSmallNumbers.getWidth(modInfo.order.toString()) + Terrarum.fontSmallNumbers.draw(batch, + modInfo.order.toString(), + posX + (numberAreaWidth - strlen).div(2f).floor(), + posY + (height - Terrarum.fontSmallNumbers.H).div(2f).floor() + ) + + // print module name + batch.color = Color.WHITE + Terrarum.fontGame.draw(batch, + "${modInfo.properName} (${modInfo.version})", + posX + numberAreaWidth.toFloat(), + posY.toFloat() + ) + + // print author name + strlen = Terrarum.fontGame.getWidth(modInfo.author) + Terrarum.fontGame.draw(batch, + modInfo.author, + posX + width - strlen.toFloat(), + posY.toFloat() + ) + + // print description + Terrarum.fontGame.draw(batch, + modInfo.description, + posX + numberAreaWidth.toFloat(), + posY + Terrarum.fontGame.lineHeight + ) + + // print releasedate + strlen = Terrarum.fontGame.getWidth(modInfo.releaseDate) + Terrarum.fontGame.draw(batch, + modInfo.releaseDate, + posX + width - strlen.toFloat(), + posY + Terrarum.fontGame.lineHeight + ) + + } + else { + batch.color = Color(0xff8080_ff.toInt()) + val str = "InternalError: no such module: '$moduleName'" + val strlen = Terrarum.fontSmallNumbers.getWidth(str) + Terrarum.fontSmallNumbers.draw(batch, + str, + posX + (width - numberAreaWidth - strlen).div(2f).floor() + numberAreaWidth, + posY + (height - Terrarum.fontSmallNumbers.H).div(2f).floor() + ) + } + } + + override fun dispose() { + } +} \ No newline at end of file diff --git a/work_files/DataFormats/Savegame metadata.txt b/work_files/DataFormats/Savegame metadata.txt index d7d15d488..37aaa504c 100644 --- a/work_files/DataFormats/Savegame metadata.txt +++ b/work_files/DataFormats/Savegame metadata.txt @@ -33,5 +33,6 @@ Ord Hex Description ... SHA-256 hash of worldinfo2 (32 bytes) ... SHA-256 hash of worldinfo3 (32 bytes) - +... Gzipped thumbnail image in TGA format + (it's gzipped so that it saves faster, so no Lzma)