diff --git a/src/net/torvald/terrarum/Terrarum.kt b/src/net/torvald/terrarum/Terrarum.kt index 4496862b1..db06451a1 100644 --- a/src/net/torvald/terrarum/Terrarum.kt +++ b/src/net/torvald/terrarum/Terrarum.kt @@ -399,6 +399,8 @@ inline fun FrameBuffer.inAction(camera: OrthographicCamera?, batch: SpriteBatch? //this.begin() FrameBufferManager.begin(this) + val oldCamPos = camera?.position?.cpy() + camera?.setToOrtho(true, this.width.toFloat(), this.height.toFloat()) camera?.position?.set((this.width / 2f).round(), (this.height / 2f).round(), 0f) // TODO floor? ceil? round? camera?.update() @@ -410,6 +412,7 @@ inline fun FrameBuffer.inAction(camera: OrthographicCamera?, batch: SpriteBatch? FrameBufferManager.end() camera?.setToOrtho(true, App.scr.wf, App.scr.hf) + camera?.position?.set(oldCamPos) camera?.update() batch?.projectionMatrix = camera?.combined } @@ -421,6 +424,8 @@ inline fun FrameBuffer.inActionF(camera: OrthographicCamera?, batch: SpriteBatch //this.begin() FrameBufferManager.begin(this) + val oldCamPos = camera?.position?.cpy() + camera?.setToOrtho(false, this.width.toFloat(), this.height.toFloat()) camera?.position?.set((this.width / 2f).round(), (this.height / 2f).round(), 0f) // TODO floor? ceil? round? camera?.update() @@ -432,6 +437,7 @@ inline fun FrameBuffer.inActionF(camera: OrthographicCamera?, batch: SpriteBatch FrameBufferManager.end() camera?.setToOrtho(true, App.scr.wf, App.scr.hf) + camera?.position?.set(oldCamPos) camera?.update() batch?.projectionMatrix = camera?.combined } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortal.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortal.kt index a96c1c6c8..9b0b9333c 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortal.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortal.kt @@ -56,6 +56,8 @@ class UIWorldPortal : UICanvas( val transitionalSearch = UIWorldPortalSearch(this) val transitionalListing = UIWorldPortalListing(this) + val transitionalDelete = UIWorldPortalDelete(this) + val transitionalRename = UIWorldPortalRename(this) // val transitionalCargo = UIWorldPortalCargo(this) private val transitionPanel = UIItemHorizontalFadeSlide( this, @@ -65,10 +67,21 @@ class UIWorldPortal : UICanvas( App.scr.height, 0f, listOf(transitionalListing), - listOf(transitionalSearch), + listOf(transitionalSearch, transitionalDelete, transitionalRename), listOf() ) + internal var selectedButton: UIItemWorldCellsSimple? = null + + internal fun queueUpSearchScr() { transitionPanel.setCentreUIto(0) } + internal fun queueUpDeleteScr() { transitionPanel.setCentreUIto(1) } + internal fun queueUpRenameScr() { transitionPanel.setCentreUIto(2) } + + internal fun changePanelTo(index: Int) { + transitionPanel.requestTransition(index) + } + + /** * Called by: * - "Search" button on UIWorldPortalListing @@ -100,7 +113,7 @@ class UIWorldPortal : UICanvas( transitionPanel.render(batch, camera) } - private fun addWorldToPlayersDict(uuid: UUID) { + internal fun addWorldToPlayersDict(uuid: UUID) { val uuidstr = uuid.toAscii85() INGAME.actorNowPlaying?.let { val avList = (it.actorValue.getAsString(AVKey.WORLD_PORTAL_DICT) ?: "").split(',').filter { it.isNotBlank() }.toMutableList() @@ -111,7 +124,16 @@ class UIWorldPortal : UICanvas( } } - private fun cleanUpWorldDict() { + internal fun removeWorldfromDict(uuid: UUID) { + val uuidstr = uuid.toAscii85() + INGAME.actorNowPlaying?.let { + val avList = (it.actorValue.getAsString(AVKey.WORLD_PORTAL_DICT) ?: "").split(',').filter { it.isNotBlank() }.toMutableList() + avList.remove(uuidstr) + it.actorValue[AVKey.WORLD_PORTAL_DICT] = avList.joinToString(",") + } + } + + internal fun cleanUpWorldDict() { // remove dupes, etc INGAME.actorNowPlaying?.let { val avList = (it.actorValue.getAsString(AVKey.WORLD_PORTAL_DICT) ?: "").split(',').filter { it.isNotBlank() }.toSet() diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalDelete.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalDelete.kt new file mode 100644 index 000000000..0f9684016 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalDelete.kt @@ -0,0 +1,77 @@ +package net.torvald.terrarum.modulebasegame.ui + +import com.badlogic.gdx.graphics.Camera +import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import net.torvald.terrarum.App +import net.torvald.terrarum.langpack.Lang +import net.torvald.terrarum.ui.Toolkit +import net.torvald.terrarum.ui.UICanvas +import net.torvald.terrarum.ui.UIItemTextButton + +class UIWorldPortalDelete(private val full: UIWorldPortal) : UICanvas() { + + override var width = 480 + override var height = 480 + + private val drawX = (Toolkit.drawWidth - width) / 2 + private val drawY = (App.scr.height - height) / 2 + private val goButtonWidth = 180 + private val buttonY = drawY + height - 24 + + private val deleteButton = UIItemTextButton(this, + { Lang["MENU_LABEL_DELETE"] }, drawX + (width/2 - goButtonWidth) / 2, buttonY, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true, inactiveCol = Toolkit.Theme.COL_RED, activeCol = Toolkit.Theme.COL_REDD).also { + it.clickOnceListener = { _,_ -> + full.removeWorldfromDict(full.selectedButton!!.worldInfo!!.uuid) + full.changePanelTo(0) + } + } + private val cancelButton = UIItemTextButton(this, + { Lang["MENU_LABEL_CANCEL"] }, drawX + width/2 + (width/2 - goButtonWidth) / 2, buttonY, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also { + it.clickOnceListener = { _,_ -> + full.changePanelTo(0) + } + } + + init { + addUIitem(deleteButton) + addUIitem(cancelButton) + } + + + override fun updateUI(delta: Float) { + uiItems.forEach { it.update(delta) } + } + + + override fun renderUI(batch: SpriteBatch, camera: Camera) { + full.selectedButton?.let { + val buttonYdelta = (App.scr.tvSafeGraphicsHeight + 172 + 36) - it.posY + val buttonXdelta = (Toolkit.drawWidth - it.width) / 2 - it.posX + it.render(batch, camera, buttonXdelta, buttonYdelta) + } + + uiItems.forEach { it.render(batch, camera) } + + batch.color = Color.WHITE + // ui title + val titlestr = Lang["MENU_LABEL_DELETE_WORLD"] + App.fontUITitle.draw(batch, titlestr, drawX + (width - App.fontUITitle.getWidth(titlestr)).div(2).toFloat(), UIInventoryFull.INVENTORY_CELLS_OFFSET_Y() - 36f) + + +// Toolkit.drawTextCentered(batch, App.fontGame, Lang["MENU_LABEL_SAVE_WILL_BE_DELETED"], Toolkit.drawWidth, 0, (App.scr.tvSafeGraphicsHeight + 172 + 36) - 46) + Toolkit.drawTextCentered(batch, App.fontGame, Lang["MENU_LABEL_ARE_YOU_SURE"], Toolkit.drawWidth, 0, (App.scr.tvSafeGraphicsHeight + 172 + 36) + UIItemWorldCellsSimple.height + 36) + } + + override fun dispose() { + } + + override fun doOpening(delta: Float) { + full.selectedButton?.forceMouseDown = true + } + + override fun doClosing(delta: Float) { + full.selectedButton?.forceMouseDown = false + } + +} diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalListing.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalListing.kt index f2a83f07b..6a7fe90aa 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalListing.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalListing.kt @@ -76,6 +76,7 @@ class UIWorldPortalListing(val full: UIWorldPortal) : UICanvas() { alignment = UIItemTextButton.Companion.Alignment.CENTRE ).also { it.clickOnceListener = { _,_ -> + full.queueUpSearchScr() full.requestTransition(1) } } @@ -104,15 +105,27 @@ class UIWorldPortalListing(val full: UIWorldPortal) : UICanvas() { deleteButtonWidth, hasBorder = true, alignment = UIItemTextButton.Companion.Alignment.CENTRE - ) + ).also { + it.clickOnceListener = { _,_ -> + full.queueUpRenameScr() + full.changePanelTo(1) + } + } private val buttonDelete = UIItemTextButton(this, - { Lang["MENU_LABEL_DELETE"] }, + { Lang["MENU_LABEL_DELETE_WORLD"] }, hx + gridGap/2 + deleteButtonWidth + gridGap, buttonsY, deleteButtonWidth, hasBorder = true, - alignment = UIItemTextButton.Companion.Alignment.CENTRE - ) + alignment = UIItemTextButton.Companion.Alignment.CENTRE, + inactiveCol = Toolkit.Theme.COL_RED, activeCol = Toolkit.Theme.COL_REDD + ).also { + it.clickOnceListener = { _,_ -> + full.queueUpDeleteScr() + full.changePanelTo(1) + } + } + private val navRemoCon = UIItemListNavBarVertical(full, hx + 6 + UIItemWorldCellsSimple.width, y + 7, listHeight + 2, false) @@ -251,6 +264,7 @@ class UIWorldPortalListing(val full: UIWorldPortal) : UICanvas() { worldList.getOrNull(it)?.diskSkimmer?.getDiskName(Common.CHARSET) ).also { button -> button.clickOnceListener = { _, _ -> + full.selectedButton = button selected = button selectedIndex = it highlightListEditButtons(worldList.getOrNull(it)) @@ -475,9 +489,11 @@ class UIItemWorldCellsSimple( initialX: Int, initialY: Int, internal val worldInfo: UIWorldPortalListing.WorldInfo? = null, - internal val worldName: String? = null, + internal var worldName: String? = null, ) : UIItem(parent, initialX, initialY) { + var forceMouseDown = false + companion object { const val width = 378 const val height = 46 @@ -502,20 +518,22 @@ class UIItemWorldCellsSimple( super.update(delta) } - override fun render(batch: SpriteBatch, camera: Camera) { + fun render(batch: SpriteBatch, camera: Camera, offX: Int, offY: Int) { super.render(batch, camera) + val posX = posX + offX + val posY = posY + offY // draw background batch.color = UIInventoryFull.CELL_COL Toolkit.fillArea(batch, posX, posY, width, height) - val mouseUp = mouseUp && worldInfo != null + val mouseUp = mouseUp && worldInfo != null && !forceMouseDown - val bcol = if (highlighted || mouseUp && mousePushed) Toolkit.Theme.COL_SELECTED - else if (mouseUp) Toolkit.Theme.COL_MOUSE_UP else (if (worldInfo == null) Toolkit.Theme.COL_INVENTORY_CELL_BORDER else Toolkit.Theme.COL_INACTIVE) - val tcol = if (highlighted || mouseUp && mousePushed) Toolkit.Theme.COL_SELECTED - else if (mouseUp) Toolkit.Theme.COL_MOUSE_UP else (if (worldInfo == null) Toolkit.Theme.COL_INACTIVE else Toolkit.Theme.COL_LIST_DEFAULT) + val bcol = if (highlighted && !forceMouseDown || mouseUp && mousePushed) Toolkit.Theme.COL_SELECTED + else if (mouseUp) Toolkit.Theme.COL_MOUSE_UP else (if (worldInfo == null && !forceMouseDown) Toolkit.Theme.COL_INVENTORY_CELL_BORDER else Toolkit.Theme.COL_INACTIVE) + val tcol = if (highlighted && !forceMouseDown || mouseUp && mousePushed) Toolkit.Theme.COL_SELECTED + else if (mouseUp) Toolkit.Theme.COL_MOUSE_UP else (if (worldInfo == null && !forceMouseDown) Toolkit.Theme.COL_INACTIVE else Toolkit.Theme.COL_LIST_DEFAULT) // draw border batch.color = bcol @@ -529,7 +547,11 @@ class UIItemWorldCellsSimple( // text separator batch.color = bcol.cpy().sub(0f,0f,0f,0.65f) Toolkit.fillArea(batch, posX + 2, posY + 23, width - 4, 1) + } + + override fun render(batch: SpriteBatch, camera: Camera) { + render(batch, camera, 0, 0) } override fun scrolled(amountX: Float, amountY: Float): Boolean { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalRename.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalRename.kt new file mode 100644 index 000000000..9330a9508 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalRename.kt @@ -0,0 +1,100 @@ +package net.torvald.terrarum.modulebasegame.ui + +import com.badlogic.gdx.graphics.Camera +import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import net.torvald.terrarum.App +import net.torvald.terrarum.langpack.Lang +import net.torvald.terrarum.ui.* +import net.torvald.unicode.EMDASH + +class UIWorldPortalRename(private val full: UIWorldPortal) : UICanvas() { + + override var width = 480 + override var height = 480 + + private val drawX = (Toolkit.drawWidth - width) / 2 + private val drawY = (App.scr.height - height) / 2 + private val goButtonWidth = 180 + private val buttonY = drawY + height - 24 + + private val inputWidth = UIItemWorldCellsSimple.width + + + private val nameInput = UIItemTextLineInput(this, + (Toolkit.drawWidth - inputWidth) / 2, drawY + 300, inputWidth, + { "" }, InputLenCap(256, InputLenCap.CharLenUnit.CODEPOINTS) + ) + + private val renameButton = UIItemTextButton(this, + { Lang["MENU_LABEL_RENAME"] }, drawX + (width/2 - goButtonWidth) / 2, buttonY, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also { + it.clickOnceListener = { _,_ -> + val newName = nameInput.getText().trim() + if (newName.isNotBlank()) { + full.selectedButton!!.worldInfo!!.uuid.let { uuid -> + App.savegameWorldsName[uuid] = newName + App.savegameWorlds[uuid]!!.renameWorld(newName) + full.selectedButton!!.worldName = newName + } + } + + full.changePanelTo(0) + } + } + private val cancelButton = UIItemTextButton(this, + { Lang["MENU_LABEL_CANCEL"] }, drawX + width/2 + (width/2 - goButtonWidth) / 2, buttonY, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also { + it.clickOnceListener = { _,_ -> + + + full.changePanelTo(0) + } + } + + init { + addUIitem(renameButton) + addUIitem(cancelButton) + addUIitem(nameInput) + } + + + override fun updateUI(delta: Float) { + uiItems.forEach { it.update(delta) } + } + + private var oldPosX = full.posX + + override fun renderUI(batch: SpriteBatch, camera: Camera) { + val posXDelta = posX - oldPosX + + // ugh why won't you just scroll along?? +// nameInput.posX += posXDelta // is it fixed now? + + batch.color = Color.WHITE + // ui title + val titlestr = Lang["MENU_LABEL_RENAME"] + App.fontUITitle.draw(batch, titlestr, drawX + (width - App.fontUITitle.getWidth(titlestr)).div(2).toFloat(), UIInventoryFull.INVENTORY_CELLS_OFFSET_Y() - 36f) + + full.selectedButton?.let { + val buttonYdelta = (App.scr.tvSafeGraphicsHeight + 172 + 36) - it.posY + val buttonXdelta = (Toolkit.drawWidth - it.width) / 2 - it.posX + it.render(batch, camera, buttonXdelta, buttonYdelta) + } + + uiItems.forEach { it.render(batch, camera) } + + + oldPosX = posX + } + + override fun dispose() { + } + + override fun doOpening(delta: Float) { + full.selectedButton?.forceMouseDown = true + } + + override fun doClosing(delta: Float) { + full.selectedButton?.forceMouseDown = false + } + +} diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalSearch.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalSearch.kt index 1f311692e..ef6a6b981 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalSearch.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalSearch.kt @@ -51,7 +51,6 @@ class UIWorldPortalSearch(val full: UIWorldPortal) : UICanvas() { private val radioCellWidth = 116 private val inputWidth = 340 private val radioX = (width - (radioCellWidth * tex.size + 9)) / 2 - private val inputX = width - inputWidth private val sizeSelY = 186 + 40 @@ -78,10 +77,12 @@ class UIWorldPortalSearch(val full: UIWorldPortal) : UICanvas() { ) private val goButtonWidth = 180 + private val buttonY = drawY + height - 24 + private val backButton = UIItemTextButton(this, - { Lang["MENU_LABEL_BACK"] }, drawX + (width/2 - goButtonWidth) / 2, drawY + height - 24, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true) + { Lang["MENU_LABEL_BACK"] }, drawX + (width/2 - goButtonWidth) / 2, buttonY, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true) private val goButton = UIItemTextButton(this, - { Lang["MENU_LABEL_CONFIRM_BUTTON"] }, drawX + width/2 + (width/2 - goButtonWidth) / 2, drawY + height - 24, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true) + { Lang["MENU_LABEL_CONFIRM_BUTTON"] }, drawX + width/2 + (width/2 - goButtonWidth) / 2, buttonY, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true) init { goButton.clickOnceListener = { _, _ -> @@ -101,10 +102,10 @@ class UIWorldPortalSearch(val full: UIWorldPortal) : UICanvas() { } addUIitem(sizeSelector) - addUIitem(seedInput) // order is important - addUIitem(nameInput) // because of the IME candidates overlay addUIitem(goButton) addUIitem(backButton) + addUIitem(seedInput) // order is important + addUIitem(nameInput) // because of the IME candidates overlay } @@ -126,17 +127,15 @@ class UIWorldPortalSearch(val full: UIWorldPortal) : UICanvas() { val posXDelta = posX - oldPosX // ugh why won't you just scroll along?? - seedInput.posX += posXDelta - nameInput.posX += posXDelta - goButton.posX += posXDelta - backButton.posX += posXDelta - +// seedInput.posX += posXDelta +// nameInput.posX += posXDelta // is it fixed now? + // TODO teleporter memory usage and whatnot batch.color = Color.WHITE // ui title val titlestr = Lang["CONTEXT_WORLD_NEW"] - App.fontUITitle.draw(batch, titlestr, drawX + (width - App.fontUITitle.getWidth(titlestr)).div(2).toFloat(), INVENTORY_CELLS_OFFSET_Y() - 72f) + App.fontUITitle.draw(batch, titlestr, drawX + (width - App.fontUITitle.getWidth(titlestr)).div(2).toFloat(), INVENTORY_CELLS_OFFSET_Y() - 36f) // draw size previews val texture = tex[sizeSelector.selection.coerceAtMost(tex.lastIndex)] diff --git a/src/net/torvald/terrarum/savegame/VDUtil.kt b/src/net/torvald/terrarum/savegame/VDUtil.kt index 59305b902..de31617db 100644 --- a/src/net/torvald/terrarum/savegame/VDUtil.kt +++ b/src/net/torvald/terrarum/savegame/VDUtil.kt @@ -665,10 +665,10 @@ fun magicMismatch(magic: ByteArray, array: ByteArray): Boolean { return !Arrays.equals(array, magic) } fun String.toEntryName(length: Int, charset: Charset): ByteArray { - val buffer = ByteArray64(length.toLong()) + val buf = ByteArray(length) val stringByteArray = this.toByteArray(charset) - buffer.appendBytes(stringByteArray.sliceArray(0 until minOf(length, stringByteArray.size))) - return buffer.toByteArray() + System.arraycopy(stringByteArray, 0, buf, 0, minOf(length, stringByteArray.size)) + return buf } fun ByteArray.toCanonicalString(charset: Charset): String { var lastIndexOfRealStr = 0 diff --git a/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt b/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt index 26a1b4ff4..9d108ca34 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt @@ -507,6 +507,7 @@ class UIItemTextLineInput( private var textDrawOffset = 0 override fun render(batch: SpriteBatch, camera: Camera) { + val posXDelta = posX - oldPosX @@ -515,6 +516,7 @@ class UIItemTextLineInput( batch.end() + // FIXME any subsequent UIItems after this function are not moved even if their parent UI is moving if (true || fboUpdateLatch) { fboUpdateLatch = false fbo.inAction(camera as OrthographicCamera, batch) { batch.inUse {