[Editor] Map resizability - attempt I

This commit is contained in:
2021-02-06 12:25:27 +01:00
parent f6fcfdbd81
commit 2bd7687088
9 changed files with 83 additions and 33 deletions

View File

@@ -1,6 +1,7 @@
package com.bartlomiejpluta.base.editor.event
import tornadofx.EventBus.RunOn.ApplicationThread
import tornadofx.EventBus.RunOn.BackgroundThread
import tornadofx.FXEvent
object RedrawMapRequestEvent : FXEvent(BackgroundThread)
object RedrawMapRequestEvent : FXEvent(ApplicationThread)

View File

@@ -2,13 +2,18 @@ package com.bartlomiejpluta.base.editor.model.map.map
import com.bartlomiejpluta.base.editor.model.map.layer.Layer
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
import tornadofx.observableListOf
import javafx.beans.property.ReadOnlyDoubleWrapper
import javafx.beans.property.SimpleIntegerProperty
import tornadofx.*
class GameMap(val tileSet: TileSet, val rows: Int, val columns: Int) {
class GameMap(val tileSet: TileSet, initialColumns: Int, initialRows: Int) {
val layers = observableListOf<Layer>()
val mapProperties = observableListOf<MapProperty>()
val width = columns * tileSet.tileWidth
val rowsProperty = SimpleIntegerProperty(initialRows)
val rows by rowsProperty
val height = columns * tileSet.tileWidth
val columnsProperty = SimpleIntegerProperty(initialColumns)
val columns by columnsProperty
}

View File

@@ -0,0 +1,13 @@
package com.bartlomiejpluta.base.editor.model.map.map
import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.SimpleStringProperty
import tornadofx.*
class MapProperty(key: String, value: Int) {
val keyProperty = SimpleStringProperty(key)
val key by keyProperty
val valueProperty = SimpleIntegerProperty(value)
val value by valueProperty
}

View File

@@ -6,25 +6,23 @@ import com.bartlomiejpluta.base.editor.render.model.Renderable
import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
import javafx.scene.canvas.GraphicsContext
import javafx.scene.paint.Color
import org.slf4j.LoggerFactory
class MapCanvas(val map: GameMapVM, private val painter: MapPainter) : Renderable {
var tileSet = map.tileSet
private var layers = map.layers
private var rows = map.rows
private var columns = map.columns
private var tileWidth = tileSet.tileWidth.toDouble()
private var tileHeight = tileSet.tileHeight.toDouble()
private var mapWidth = map.width.toDouble()
private var mapHeight = map.height.toDouble()
override fun render(gc: GraphicsContext) {
log.debug("vm.dim = ${map.rows}x${map.columns} | map.dim = ${map.item.rows}x${map.item.columns}")
// log.debug("vm.size = ${map.width}x${map.height} | map.size = ${map.item.width}x${map.item.height}")
gc.clearRect(0.0, 0.0, gc.canvas.width, gc.canvas.height)
renderBackground(gc)
renderBackground(gc)
layers.forEach { dispatchLayerRender(gc, it) }
map.layers.forEach { dispatchLayerRender(gc, it) }
renderGrid(gc)
@@ -38,8 +36,8 @@ class MapCanvas(val map: GameMapVM, private val painter: MapPainter) : Renderabl
}
private fun renderBackground(gc: GraphicsContext) {
for (row in 0 until rows) {
for (column in 0 until columns) {
for (row in 0 until map.rows) {
for (column in 0 until map.columns) {
gc.fill = if ((row + column) % 2 == 0) BACKGROUND_COLOR1 else BACKGROUND_COLOR2
gc.fillRect(column * tileWidth, row * tileHeight, tileWidth, tileHeight)
}
@@ -59,21 +57,22 @@ class MapCanvas(val map: GameMapVM, private val painter: MapPainter) : Renderabl
private fun renderGrid(gc: GraphicsContext) {
gc.lineWidth = 1.5
gc.strokeLine(0.0, 0.0, mapWidth, 0.0)
gc.strokeLine(0.0, 0.0, 0.0, mapHeight)
gc.strokeLine(mapWidth, 0.0, mapWidth, mapHeight)
gc.strokeLine(0.0, mapHeight, mapWidth, mapHeight)
gc.strokeLine(0.0, 0.0, map.width, 0.0)
gc.strokeLine(0.0, 0.0, 0.0, map.height)
gc.strokeLine(map.width, 0.0, map.width, map.height)
gc.strokeLine(0.0, map.height, map.width, map.height)
for (row in 0 until rows) {
gc.strokeLine(0.0, row * tileHeight, mapWidth, row * tileHeight)
for (row in 0 until map.rows) {
gc.strokeLine(0.0, row * tileHeight, map.width, row * tileHeight)
}
for (column in 0 until columns) {
gc.strokeLine(column * tileWidth, 0.0, column * tileWidth, mapHeight)
for (column in 0 until map.columns) {
gc.strokeLine(column * tileWidth, 0.0, column * tileWidth, map.height)
}
}
companion object {
private val log = LoggerFactory.getLogger(MapCanvas::class.java)
private val BACKGROUND_COLOR1 = Color.color(1.0, 1.0, 1.0, 1.0)
private val BACKGROUND_COLOR2 = Color.color(0.95, 0.95, 0.95, 0.95)
}

View File

@@ -24,8 +24,8 @@ class MapPane(private val mapVM: GameMapVM,
onMousePressed = this
onMouseReleased = this
width = mapVM.width.toDouble()
height = mapVM.height.toDouble()
widthProperty().bind(mapVM.widthProperty)
heightProperty().bind(mapVM.heightProperty)
render()
}

View File

@@ -19,7 +19,7 @@ class MainView : View() {
button(graphic = FontIcon("fa-file-o")) {
action {
val tileSet = tileSetController.tileset
val map = mapController.createMap(tileSet, 25, 25)
val map = mapController.createMap(tileSet, 5, 5)
tabPane += find<MapFragment>(UndoableScope(), MapFragment::map to map).apply {
title = "Map 1"
}

View File

@@ -38,6 +38,22 @@ class MapFragment : Fragment() {
fire(RedrawMapRequestEvent)
}
}
button(graphic = FontIcon("fa-plus")) {
action {
mapVM.rows = mapVM.rows + 1
mapVM.commit()
fire(RedrawMapRequestEvent)
}
}
button(graphic = FontIcon("fa-plus")) {
action {
mapVM.columns = mapVM.columns + 1
mapVM.commit()
fire(RedrawMapRequestEvent)
}
}
}
center = mapView.root

View File

@@ -0,0 +1,14 @@
package com.bartlomiejpluta.base.editor.view.map
import com.bartlomiejpluta.base.editor.model.map.map.MapProperty
import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
import tornadofx.*
class MapPropertiesView : View() {
private val mapVM = find<GameMapVM>()
override val root = tableview(mapVM.mapProperties) {
column("Key", MapProperty::keyProperty)
column("Value", MapProperty::valueProperty)
}
}

View File

@@ -2,27 +2,29 @@ package com.bartlomiejpluta.base.editor.viewmodel.map
import com.bartlomiejpluta.base.editor.model.map.layer.Layer
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
import com.bartlomiejpluta.base.editor.model.map.map.MapProperty
import javafx.beans.property.ReadOnlyDoubleWrapper
import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.SimpleListProperty
import tornadofx.ItemViewModel
import tornadofx.getValue
import tornadofx.*
class GameMapVM : ItemViewModel<GameMap>() {
val layers: SimpleListProperty<Layer> = bind(GameMap::layers)
val mapProperties: SimpleListProperty<MapProperty> = bind(GameMap::mapProperties)
val tileSetProperty = bind(GameMap::tileSet)
val tileSet by tileSetProperty
val rowsProperty = bind(GameMap::rows)
val rows by rowsProperty
val rowsProperty = bind(GameMap::rowsProperty)
var rows by rowsProperty
val columnsProperty = bind(GameMap::columns)
val columns by columnsProperty
val columnsProperty = bind(GameMap::columnsProperty)
var columns by columnsProperty
val widthProperty = bind(GameMap::width)
val widthProperty = ReadOnlyDoubleWrapper().apply { bind(columnsProperty.multiply(32.0)) }
val width by widthProperty
val heightProperty = bind(GameMap::height)
val heightProperty = ReadOnlyDoubleWrapper().apply { bind(rowsProperty.multiply(32.0)) }
val height by heightProperty
val selectedLayerProperty = SimpleIntegerProperty(0)