Skip to content

Commit

Permalink
Merge pull request #182 from antzGames/min.max-heightmap
Browse files Browse the repository at this point in the history
Max height for heightmap
  • Loading branch information
JamesTKhan authored Jun 14, 2023
2 parents 288ce4d + 39c1d92 commit f96f2db
Showing 1 changed file with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener
import com.badlogic.gdx.utils.Align
import com.kotcrab.vis.ui.util.dialog.Dialogs
import com.kotcrab.vis.ui.widget.VisLabel
import com.kotcrab.vis.ui.widget.VisTable
import com.kotcrab.vis.ui.widget.VisTextButton
import com.kotcrab.vis.ui.widget.tabbedpane.Tab
Expand All @@ -18,6 +19,7 @@ import com.mbrlabs.mundus.editor.history.commands.TerrainHeightCommand
import com.mbrlabs.mundus.editor.terrain.Terraformer
import com.mbrlabs.mundus.editor.ui.UI
import com.mbrlabs.mundus.editor.ui.widgets.FileChooserField
import com.mbrlabs.mundus.editor.ui.widgets.FloatFieldWithLabel
import com.mbrlabs.mundus.editor.utils.isImage

class HeightmapTab(private val terrainComponent: TerrainComponent) : Tab(false, false) {
Expand All @@ -26,12 +28,28 @@ class HeightmapTab(private val terrainComponent: TerrainComponent) : Tab(false,

private val hmInput = FileChooserField()
private val loadHeightMapBtn = VisTextButton("Load heightmap")
private val loadHeightMapMaxHeight = FloatFieldWithLabel("Maximum height:", -1, true)

private val history: CommandHistory = Mundus.inject()
private val projectManager: ProjectManager = Mundus.inject()

private val description = """
Generate terrain using a heightmap image.
Terrain height range is from 0 to the maximum height.
Maximum height must be a positive value.
Maximum height must be greater than 0.
""".trimIndent()

init {
loadHeightMapMaxHeight.text = "100"

root.align(Align.left)
root.add(VisLabel(description)).pad(5f).left().fillX().row()
root.add(loadHeightMapMaxHeight).pad(5f).left().fillX().expandX().row()

root.add(VisLabel("\n\nSelect heightmap image:")).pad(5f).left().fillX().row()

root.add(hmInput).pad(5f).left().expandX().fillX().row()
root.add(loadHeightMapBtn).pad(5f).right().row()
Expand All @@ -47,6 +65,11 @@ class HeightmapTab(private val terrainComponent: TerrainComponent) : Tab(false,
loadHeightMapBtn.addListener(object : ClickListener() {
override fun clicked(event: InputEvent?, x: Float, y: Float) {
val hm = hmInput.file
val max = loadHeightMapMaxHeight.float

if (max == 0f) loadHeightMapMaxHeight.text = "100" // if max height left blank or zero, set to 100
if (max < 0f) loadHeightMapMaxHeight.text = "" + -max // if max is negative, then set to max to positive value

if (hm != null && hm.exists() && isImage(hm)) {
loadHeightMap(hm)
projectManager.current().assetManager.addModifiedAsset(terrainComponent.terrainAsset)
Expand All @@ -62,6 +85,7 @@ class HeightmapTab(private val terrainComponent: TerrainComponent) : Tab(false,
val command = TerrainHeightCommand(terrain)
command.setHeightDataBefore(terrain.heightData)

val minMax = loadHeightMapMaxHeight.float
val originalMap = Pixmap(heightMap)

// scale pixmap if it doesn't fit the terrainAsset
Expand All @@ -70,12 +94,11 @@ class HeightmapTab(private val terrainComponent: TerrainComponent) : Tab(false,
originalMap.format)
scaledPixmap.drawPixmap(originalMap, 0, 0, originalMap.width, originalMap.height, 0, 0,
scaledPixmap.width, scaledPixmap.height)

originalMap.dispose()
Terraformer.heightMap(terrainComponent).maxHeight(terrain.terrainWidth * 0.17f).map(scaledPixmap).terraform()
Terraformer.heightMap(terrainComponent).maxHeight(minMax).map(scaledPixmap).terraform()
scaledPixmap.dispose()
} else {
Terraformer.heightMap(terrainComponent).maxHeight(terrain.terrainWidth * 0.17f).map(originalMap).terraform()
Terraformer.heightMap(terrainComponent).maxHeight(minMax).map(originalMap).terraform()
originalMap.dispose()
}

Expand Down

0 comments on commit f96f2db

Please sign in to comment.