[Editor] Map resizability - attempt III

This commit is contained in:
2021-02-06 12:59:21 +01:00
parent df53527230
commit 0651e774f3
8 changed files with 47 additions and 56 deletions

View File

@@ -5,4 +5,6 @@ import javafx.beans.property.StringProperty
interface Layer { interface Layer {
var name: String var name: String
val nameProperty: StringProperty val nameProperty: StringProperty
fun resize(rows: Int, columns: Int)
} }

View File

@@ -2,17 +2,21 @@ package com.bartlomiejpluta.base.editor.model.map.layer
import com.bartlomiejpluta.base.editor.model.tileset.Tile import com.bartlomiejpluta.base.editor.model.tileset.Tile
import javafx.beans.property.SimpleStringProperty import javafx.beans.property.SimpleStringProperty
import tornadofx.* import tornadofx.getValue
import tornadofx.setValue
class TileLayer(name: String, val layer: Array<Array<Tile?>>) : Layer { class TileLayer(name: String, rows: Int, columns: Int) : Layer {
var layer: Array<Array<Tile?>> = Array(rows) { Array(columns) { null } }
fun setTile(row: Int, column: Int, tile: Tile?) = apply { layer[row][column] = tile } private set
override val nameProperty = SimpleStringProperty(name) override val nameProperty = SimpleStringProperty(name)
override var name: String by nameProperty override var name: String by nameProperty
companion object { override fun resize(rows: Int, columns: Int) {
fun empty(name: String, rows: Int ,columns: Int) = TileLayer(name, Array(rows) { Array(columns) { null } }) layer = Array(rows) { row ->
Array(columns) { column ->
layer.getOrNull(row) ?. getOrNull(column)
}
}
} }
} }

View File

@@ -2,17 +2,15 @@ package com.bartlomiejpluta.base.editor.model.map.map
import com.bartlomiejpluta.base.editor.model.map.layer.Layer import com.bartlomiejpluta.base.editor.model.map.layer.Layer
import com.bartlomiejpluta.base.editor.model.tileset.TileSet import com.bartlomiejpluta.base.editor.model.tileset.TileSet
import javafx.beans.property.ReadOnlyDoubleWrapper
import javafx.beans.property.ReadOnlyFloatProperty
import javafx.beans.property.SimpleDoubleProperty import javafx.beans.property.SimpleDoubleProperty
import javafx.beans.property.SimpleIntegerProperty import javafx.beans.property.SimpleIntegerProperty
import tornadofx.* import tornadofx.getValue
import tornadofx.observableListOf
import tornadofx.setValue
class GameMap(val tileSet: TileSet, initialColumns: Int, initialRows: Int) { class GameMap(val tileSet: TileSet, initialColumns: Int, initialRows: Int) {
val layers = observableListOf<Layer>() val layers = observableListOf<Layer>()
val mapProperties = observableListOf<MapProperty>()
val tileWidth = tileSet.tileWidth.toDouble() val tileWidth = tileSet.tileWidth.toDouble()
val tileHeight = tileSet.tileHeight.toDouble() val tileHeight = tileSet.tileHeight.toDouble()
@@ -33,11 +31,15 @@ class GameMap(val tileSet: TileSet, initialColumns: Int, initialRows: Int) {
init { init {
rowsProperty.addListener { _, _, newValue -> rowsProperty.addListener { _, _, newValue ->
height = newValue.toInt() * tileWidth val newRows = newValue.toInt()
height = newRows * tileWidth
layers.forEach { it.resize(newRows, columns) }
} }
columnsProperty.addListener { _, _, newValue -> columnsProperty.addListener { _, _, newValue ->
width = newValue.toInt() * tileWidth val newColumns = newValue.toInt()
width = newColumns * tileWidth
layers.forEach { it.resize(rows, newColumns) }
} }
} }
} }

View File

@@ -1,13 +0,0 @@
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

@@ -7,6 +7,7 @@ import com.bartlomiejpluta.base.editor.model.map.map.GameMap
import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
import org.kordamp.ikonli.javafx.FontIcon import org.kordamp.ikonli.javafx.FontIcon
import tornadofx.* import tornadofx.*
import kotlin.math.max
class MapFragment : Fragment() { class MapFragment : Fragment() {
@@ -39,17 +40,33 @@ class MapFragment : Fragment() {
} }
} }
button(graphic = FontIcon("fa-plus")) { button(text = "Rows", graphic = FontIcon("fa-minus")) {
action { action {
mapVM.rows = mapVM.rows + 1 mapVM.rows = max(mapVM.rows - 1, 1)
mapVM.commit() mapVM.commit()
fire(RedrawMapRequestEvent) fire(RedrawMapRequestEvent)
} }
} }
button(graphic = FontIcon("fa-plus")) { button(text = "Columns", graphic = FontIcon("fa-minus")) {
action { action {
mapVM.columns = mapVM.columns + 1 mapVM.columns = max(mapVM.columns - 1, 1)
mapVM.commit()
fire(RedrawMapRequestEvent)
}
}
button(text = "Rows", graphic = FontIcon("fa-plus")) {
action {
++mapVM.rows
mapVM.commit()
fire(RedrawMapRequestEvent)
}
}
button(text = "Columns", graphic = FontIcon("fa-plus")) {
action {
++mapVM.columns
mapVM.commit() mapVM.commit()
fire(RedrawMapRequestEvent) fire(RedrawMapRequestEvent)
} }

View File

@@ -37,7 +37,7 @@ class MapLayersView : View() {
bottom = toolbar { bottom = toolbar {
button(graphic = FontIcon("fa-plus")) { button(graphic = FontIcon("fa-plus")) {
action { action {
val tileLayer = TileLayer.empty("Layer ${mapVM.layers.size + 1}", mapVM.rows, mapVM.columns) val tileLayer = TileLayer("Layer ${mapVM.layers.size + 1}", mapVM.rows, mapVM.columns)
val command = CreateLayerCommand(mapVM.item, tileLayer) val command = CreateLayerCommand(mapVM.item, tileLayer)
command.execute() command.execute()
layersPane.selectionModel.select(mapVM.layers.size - 1) layersPane.selectionModel.select(mapVM.layers.size - 1)

View File

@@ -1,14 +0,0 @@
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,16 +2,14 @@ package com.bartlomiejpluta.base.editor.viewmodel.map
import com.bartlomiejpluta.base.editor.model.map.layer.Layer 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.GameMap
import com.bartlomiejpluta.base.editor.model.map.map.MapProperty
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
import javafx.beans.property.ReadOnlyDoubleWrapper
import javafx.beans.property.SimpleIntegerProperty import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.SimpleListProperty import javafx.beans.property.SimpleListProperty
import tornadofx.* import tornadofx.ItemViewModel
import tornadofx.getValue
import tornadofx.setValue
class GameMapVM : ItemViewModel<GameMap>() { class GameMapVM : ItemViewModel<GameMap>() {
val layers: SimpleListProperty<Layer> = bind(GameMap::layers) val layers: SimpleListProperty<Layer> = bind(GameMap::layers)
val mapProperties: SimpleListProperty<MapProperty> = bind(GameMap::mapProperties)
val tileSetProperty = bind(GameMap::tileSet) val tileSetProperty = bind(GameMap::tileSet)
val tileSet by tileSetProperty val tileSet by tileSetProperty
@@ -33,11 +31,6 @@ class GameMapVM : ItemViewModel<GameMap>() {
val heightProperty = bind(GameMap::heightProperty) val heightProperty = bind(GameMap::heightProperty)
val height by heightProperty val height by heightProperty
// val widthProperty = ReadOnlyDoubleWrapper().apply { bind(tileSetProperty.getProperty(TileSet::tileWidthProperty)) }
// val width by widthProperty
//
// val heightProperty = ReadOnlyDoubleWrapper().apply { bind(rowsProperty.multiply(32.0)) }
// val height by heightProperty
val selectedLayerProperty = SimpleIntegerProperty(0) val selectedLayerProperty = SimpleIntegerProperty(0)
val selectedLayer by selectedLayerProperty val selectedLayer by selectedLayerProperty