Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max height for heightmap #182

Merged
merged 6 commits into from
Jun 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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