mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-09 18:14:06 +09:00
minimap test: zoom and pan of the image
This commit is contained in:
@@ -21,6 +21,7 @@ import com.google.gson.JsonObject;
|
|||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
import net.torvald.dataclass.ArrayListMap;
|
import net.torvald.dataclass.ArrayListMap;
|
||||||
import net.torvald.getcpuname.GetCpuName;
|
import net.torvald.getcpuname.GetCpuName;
|
||||||
|
import net.torvald.terrarum.blockstats.MinimapComposer;
|
||||||
import net.torvald.terrarum.controller.GdxControllerAdapter;
|
import net.torvald.terrarum.controller.GdxControllerAdapter;
|
||||||
import net.torvald.terrarum.controller.TerrarumController;
|
import net.torvald.terrarum.controller.TerrarumController;
|
||||||
import net.torvald.terrarum.controller.XinputControllerAdapter;
|
import net.torvald.terrarum.controller.XinputControllerAdapter;
|
||||||
@@ -523,6 +524,7 @@ public class AppLoader implements ApplicationListener {
|
|||||||
|
|
||||||
IngameRenderer.INSTANCE.dispose();
|
IngameRenderer.INSTANCE.dispose();
|
||||||
PostProcessor.INSTANCE.dispose();
|
PostProcessor.INSTANCE.dispose();
|
||||||
|
MinimapComposer.INSTANCE.dispose();
|
||||||
|
|
||||||
Terrarum.INSTANCE.dispose();
|
Terrarum.INSTANCE.dispose();
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ import net.torvald.terrarum.gameactors.ActorID
|
|||||||
import net.torvald.terrarum.imagefont.TinyAlphNum
|
import net.torvald.terrarum.imagefont.TinyAlphNum
|
||||||
import net.torvald.terrarum.itemproperties.ItemCodex
|
import net.torvald.terrarum.itemproperties.ItemCodex
|
||||||
import net.torvald.terrarum.worlddrawer.CreateTileAtlas
|
import net.torvald.terrarum.worlddrawer.CreateTileAtlas
|
||||||
import net.torvald.terrarum.worlddrawer.FeaturesDrawer
|
|
||||||
import net.torvald.terrarum.worlddrawer.WorldCamera
|
import net.torvald.terrarum.worlddrawer.WorldCamera
|
||||||
import net.torvald.terrarumsansbitmap.gdx.GameFontBase
|
import net.torvald.terrarumsansbitmap.gdx.GameFontBase
|
||||||
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
|
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
|
||||||
@@ -491,6 +490,10 @@ object Terrarum : Screen {
|
|||||||
get() = Gdx.input.x
|
get() = Gdx.input.x
|
||||||
inline val mouseScreenY: Int
|
inline val mouseScreenY: Int
|
||||||
get() = Gdx.input.y
|
get() = Gdx.input.y
|
||||||
|
inline val mouseDeltaX: Int
|
||||||
|
get() = Gdx.input.deltaX
|
||||||
|
inline val mouseDeltaY: Int
|
||||||
|
get() = Gdx.input.deltaY
|
||||||
/** Delta converted as it it was a FPS */
|
/** Delta converted as it it was a FPS */
|
||||||
inline val updateRate: Double
|
inline val updateRate: Double
|
||||||
get() = 1.0 / Gdx.graphics.deltaTime
|
get() = 1.0 / Gdx.graphics.deltaTime
|
||||||
|
|||||||
50
src/net/torvald/terrarum/blockstats/MinimapComposer.kt
Normal file
50
src/net/torvald/terrarum/blockstats/MinimapComposer.kt
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package net.torvald.terrarum.blockstats
|
||||||
|
|
||||||
|
import com.badlogic.gdx.Gdx
|
||||||
|
import com.badlogic.gdx.graphics.Pixmap
|
||||||
|
import com.badlogic.gdx.graphics.Texture
|
||||||
|
import net.torvald.terrarum.AppLoader
|
||||||
|
import net.torvald.terrarum.gameworld.GameWorld
|
||||||
|
|
||||||
|
object MinimapComposer {
|
||||||
|
|
||||||
|
// strategy: mosaic the textures, maximum texture size is 4 096.
|
||||||
|
|
||||||
|
|
||||||
|
private var world: GameWorld = GameWorld.makeNullWorld()
|
||||||
|
|
||||||
|
fun setWorld(world: GameWorld) {
|
||||||
|
try {
|
||||||
|
if (this.world != world) {
|
||||||
|
AppLoader.printdbg(this, "World change detected -- old world: ${this.world.hashCode()}, new world: ${world.hashCode()}")
|
||||||
|
|
||||||
|
// TODO, also set totalWidth/Height
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (e: UninitializedPropertyAccessException) {
|
||||||
|
// new init, do nothing
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
this.world = world
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val tempTex = Array(4) { Texture(1,1,Pixmap.Format.RGBA8888) }
|
||||||
|
// total size of the minimap. Remember: textures can be mosaic-ed to display full map.
|
||||||
|
var totalWidth = 0
|
||||||
|
var totalHeight = 0
|
||||||
|
|
||||||
|
init {
|
||||||
|
repeat(4) {
|
||||||
|
tempTex[it] = Texture(Gdx.files.internal("./assets/testimage.png"))
|
||||||
|
}
|
||||||
|
totalWidth = tempTex[0].width * 2
|
||||||
|
totalHeight = tempTex[0].height * 2
|
||||||
|
}
|
||||||
|
|
||||||
|
fun dispose() {
|
||||||
|
// tempTex.forEach { it.dispose }
|
||||||
|
// minimapPixmaps.forEach { it.dispose }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import net.torvald.terrarum.*
|
|||||||
import net.torvald.terrarum.AppLoader.printdbg
|
import net.torvald.terrarum.AppLoader.printdbg
|
||||||
import net.torvald.terrarum.blockproperties.BlockPropUtil
|
import net.torvald.terrarum.blockproperties.BlockPropUtil
|
||||||
import net.torvald.terrarum.blockstats.BlockStats
|
import net.torvald.terrarum.blockstats.BlockStats
|
||||||
|
import net.torvald.terrarum.blockstats.MinimapComposer
|
||||||
import net.torvald.terrarum.concurrent.ThreadParallel
|
import net.torvald.terrarum.concurrent.ThreadParallel
|
||||||
import net.torvald.terrarum.console.Authenticator
|
import net.torvald.terrarum.console.Authenticator
|
||||||
import net.torvald.terrarum.gameactors.Actor
|
import net.torvald.terrarum.gameactors.Actor
|
||||||
@@ -273,6 +274,7 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
|
|||||||
|
|
||||||
|
|
||||||
MegaRainGovernor // invoke MegaRain Governor
|
MegaRainGovernor // invoke MegaRain Governor
|
||||||
|
MinimapComposer // invoke MinimapComposer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package net.torvald.terrarum.modulebasegame.ui
|
package net.torvald.terrarum.modulebasegame.ui
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx
|
import com.badlogic.gdx.Gdx
|
||||||
import com.badlogic.gdx.graphics.Camera
|
import com.badlogic.gdx.Input
|
||||||
import com.badlogic.gdx.graphics.Color
|
import com.badlogic.gdx.graphics.*
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||||
|
import com.badlogic.gdx.graphics.glutils.FrameBuffer
|
||||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
|
||||||
|
import com.jme3.math.Vector2f
|
||||||
import net.torvald.terrarum.*
|
import net.torvald.terrarum.*
|
||||||
import net.torvald.terrarum.AppLoader.printdbg
|
import net.torvald.terrarum.AppLoader.printdbg
|
||||||
import net.torvald.terrarum.Terrarum.gamepadLabelEast
|
import net.torvald.terrarum.Terrarum.gamepadLabelEast
|
||||||
@@ -15,6 +17,7 @@ import net.torvald.terrarum.Terrarum.gamepadLabelRStick
|
|||||||
import net.torvald.terrarum.Terrarum.gamepadLabelRT
|
import net.torvald.terrarum.Terrarum.gamepadLabelRT
|
||||||
import net.torvald.terrarum.Terrarum.gamepadLabelStart
|
import net.torvald.terrarum.Terrarum.gamepadLabelStart
|
||||||
import net.torvald.terrarum.Terrarum.gamepadLabelWest
|
import net.torvald.terrarum.Terrarum.gamepadLabelWest
|
||||||
|
import net.torvald.terrarum.blockstats.MinimapComposer
|
||||||
import net.torvald.terrarum.gameactors.ActorWBMovable
|
import net.torvald.terrarum.gameactors.ActorWBMovable
|
||||||
import net.torvald.terrarum.langpack.Lang
|
import net.torvald.terrarum.langpack.Lang
|
||||||
import net.torvald.terrarum.modulebasegame.Ingame
|
import net.torvald.terrarum.modulebasegame.Ingame
|
||||||
@@ -39,8 +42,6 @@ class UIInventoryFull(
|
|||||||
doNotWarnConstant: Boolean = false
|
doNotWarnConstant: Boolean = false
|
||||||
) : UICanvas(toggleKeyLiteral, toggleButtonLiteral, customPositioning, doNotWarnConstant) {
|
) : UICanvas(toggleKeyLiteral, toggleButtonLiteral, customPositioning, doNotWarnConstant) {
|
||||||
|
|
||||||
// FIXME something's causing memory leak
|
|
||||||
|
|
||||||
private val debugvals = false
|
private val debugvals = false
|
||||||
|
|
||||||
override var width: Int = Terrarum.WIDTH
|
override var width: Int = Terrarum.WIDTH
|
||||||
@@ -343,20 +344,105 @@ class UIInventoryFull(
|
|||||||
private val MINIMAP_WIDTH = 800f
|
private val MINIMAP_WIDTH = 800f
|
||||||
private val MINIMAP_HEIGHT = UIItemInventoryDynamicList.HEIGHT.toFloat()
|
private val MINIMAP_HEIGHT = UIItemInventoryDynamicList.HEIGHT.toFloat()
|
||||||
private val MINIMAP_SKYCOL = Color(0x88bbddff.toInt())
|
private val MINIMAP_SKYCOL = Color(0x88bbddff.toInt())
|
||||||
|
private var minimapZoom = 1f
|
||||||
|
private var minimapPanX = -MinimapComposer.totalWidth / 2f
|
||||||
|
private var minimapPanY = -MinimapComposer.totalHeight / 2f
|
||||||
|
private val MINIMAP_ZOOM_MIN = 0.25f
|
||||||
|
private val MINIMAP_ZOOM_MAX = 8f
|
||||||
|
private val minimapFBO = FrameBuffer(Pixmap.Format.RGBA8888, MINIMAP_WIDTH.toInt(), MINIMAP_HEIGHT.toInt(), false)
|
||||||
|
private val minimapCamera = OrthographicCamera(MINIMAP_WIDTH, MINIMAP_HEIGHT)
|
||||||
|
|
||||||
private fun renderScreenMinimap(batch: SpriteBatch, camera: Camera) {
|
private fun renderScreenMinimap(batch: SpriteBatch, camera: Camera) {
|
||||||
blendNormal(batch)
|
blendNormal(batch)
|
||||||
|
|
||||||
|
// update map panning
|
||||||
|
if (currentScreen >= 2f - epsilon) {
|
||||||
|
// if left click is down and cursor is in the map area
|
||||||
|
if (Gdx.input.isButtonPressed(AppLoader.getConfigInt("mouseprimary")) &&
|
||||||
|
Terrarum.mouseScreenY in itemList.posY..itemList.posY + itemList.height) {
|
||||||
|
minimapPanX += Terrarum.mouseDeltaX / minimapZoom
|
||||||
|
minimapPanY += Terrarum.mouseDeltaY / minimapZoom
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.NUM_1)) {
|
||||||
|
minimapZoom *= (1f / 1.02f)
|
||||||
|
}
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.NUM_2)) {
|
||||||
|
minimapZoom *= 1.02f
|
||||||
|
}
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.NUM_3)) {
|
||||||
|
minimapZoom = 1f
|
||||||
|
minimapPanX = -MinimapComposer.totalWidth / 2f
|
||||||
|
minimapPanY = -MinimapComposer.totalHeight / 2f
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
//minimapPanX = minimapPanX.coerceIn(-(MinimapComposer.totalWidth * minimapZoom) + MINIMAP_WIDTH, 0f) // un-comment this line to constain the panning over x-axis
|
||||||
|
} catch (e: IllegalArgumentException) { }
|
||||||
|
try {
|
||||||
|
//minimapPanY = minimapPanY.coerceIn(-(MinimapComposer.totalHeight * minimapZoom) + MINIMAP_HEIGHT, 0f)
|
||||||
|
} catch (e: IllegalArgumentException) { }
|
||||||
|
minimapZoom = minimapZoom.coerceIn(MINIMAP_ZOOM_MIN, MINIMAP_ZOOM_MAX)
|
||||||
|
|
||||||
|
|
||||||
|
// make image to roll over for x-axis. This is for the ROUNDWORLD implementation, feel free to remove below.
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// render minimap
|
||||||
|
batch.end()
|
||||||
|
minimapFBO.inAction(minimapCamera, batch) {
|
||||||
|
// whatever.
|
||||||
|
val t = MinimapComposer.tempTex
|
||||||
|
t.forEach { it.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat) }
|
||||||
|
|
||||||
|
|
||||||
|
batch.inUse {
|
||||||
|
|
||||||
|
// [ 1 0 0 ] [ s 0 0 ] [ s 0 0 ]
|
||||||
|
// [ 0 1 0 ] x [ 0 s 0 ] = [ 0 s 0 ]
|
||||||
|
// [ px py 1 ] [ w/2 h/2 1 ] [ tx ty 1 ]
|
||||||
|
//
|
||||||
|
// https://www.wolframalpha.com/input/?i=%7B%7B1,0,0%7D,%7B0,1,0%7D,%7Bp_x,p_y,1%7D%7D+*+%7B%7Bs,0,0%7D,%7B0,s,0%7D,%7Bw%2F2,h%2F2,1%7D%7D
|
||||||
|
|
||||||
|
val halfWindow = Vector2f(0.5f * MINIMAP_WIDTH, 0.5f * MINIMAP_HEIGHT)
|
||||||
|
val p = arrayOf(
|
||||||
|
Vector2f(minimapPanX, minimapPanY).mult(minimapZoom).add(halfWindow),
|
||||||
|
Vector2f(minimapPanX + t[0].width, minimapPanY).mult(minimapZoom).add(halfWindow),
|
||||||
|
Vector2f(minimapPanX, minimapPanY + t[0].height).mult(minimapZoom).add(halfWindow),
|
||||||
|
Vector2f(minimapPanX + t[0].width, minimapPanY + t[0].height).mult(minimapZoom).add(halfWindow)
|
||||||
|
)
|
||||||
|
|
||||||
|
// sky background
|
||||||
|
batch.color = MINIMAP_SKYCOL
|
||||||
|
batch.fillRect(0f, 0f, MINIMAP_WIDTH, MINIMAP_HEIGHT)
|
||||||
|
// the actual image
|
||||||
|
batch.color = Color.WHITE
|
||||||
|
repeat(4) {
|
||||||
|
batch.draw(t[it], p[it].x, p[it].y + t[it].height * minimapZoom, t[it].width * minimapZoom, -t[it].height * minimapZoom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
batch.begin()
|
||||||
|
|
||||||
|
|
||||||
|
//Terrarum.fontSmallNumbers.draw(batch, "$minimapPanX, $minimapPanY; x$minimapZoom", 10f, 10f)
|
||||||
|
|
||||||
|
|
||||||
|
batch.projectionMatrix = camera.combined
|
||||||
// 1px stroke
|
// 1px stroke
|
||||||
batch.color = Color.WHITE
|
batch.color = Color.WHITE
|
||||||
batch.fillRect(-1 + minimapScrOffX + (Terrarum.WIDTH - MINIMAP_WIDTH) / 2, -1 + itemList.posY.toFloat(), 2 + MINIMAP_WIDTH, 2 + MINIMAP_HEIGHT)
|
batch.fillRect(-1 + minimapScrOffX + (Terrarum.WIDTH - MINIMAP_WIDTH) / 2, -1 + itemList.posY.toFloat(), 2 + MINIMAP_WIDTH, 2 + MINIMAP_HEIGHT)
|
||||||
// sky background
|
|
||||||
batch.color = MINIMAP_SKYCOL
|
|
||||||
batch.fillRect(minimapScrOffX + (Terrarum.WIDTH - MINIMAP_WIDTH) / 2, itemList.posY.toFloat(), MINIMAP_WIDTH, MINIMAP_HEIGHT)
|
|
||||||
|
|
||||||
// control hints
|
// control hints
|
||||||
batch.color = Color.WHITE
|
batch.color = Color.WHITE
|
||||||
Terrarum.fontGame.draw(batch, minimapControlHelp, offsetX + minimapScrOffX, yEnd - 20)
|
Terrarum.fontGame.draw(batch, minimapControlHelp, offsetX + minimapScrOffX, yEnd - 20)
|
||||||
|
|
||||||
|
// the minimap
|
||||||
|
batch.draw(minimapFBO.colorBufferTexture, minimapScrOffX + (Terrarum.WIDTH - MINIMAP_WIDTH) / 2, itemList.posY.toFloat())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun renderScreenGamemenu(batch: SpriteBatch, camera: Camera) {
|
private fun renderScreenGamemenu(batch: SpriteBatch, camera: Camera) {
|
||||||
@@ -508,5 +594,5 @@ class UIInventoryFull(
|
|||||||
return super.scrolled(amount)
|
return super.scrolled(amount)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user