From 0651e774f364032185df2b51445a62bc889d707a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Sat, 6 Feb 2021 12:59:21 +0100 Subject: [PATCH] [Editor] Map resizability - attempt III --- .../base/editor/model/map/layer/Layer.kt | 2 ++ .../base/editor/model/map/layer/TileLayer.kt | 18 +++++++------ .../base/editor/model/map/map/GameMap.kt | 16 ++++++------ .../base/editor/model/map/map/MapProperty.kt | 13 ---------- .../base/editor/view/map/MapFragment.kt | 25 ++++++++++++++++--- .../base/editor/view/map/MapLayersView.kt | 2 +- .../base/editor/view/map/MapPropertiesView.kt | 14 ----------- .../base/editor/viewmodel/map/GameMapVM.kt | 13 +++------- 8 files changed, 47 insertions(+), 56 deletions(-) delete mode 100755 editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/map/MapProperty.kt delete mode 100755 editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapPropertiesView.kt diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/layer/Layer.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/layer/Layer.kt index 164c98e7..7037fc17 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/layer/Layer.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/layer/Layer.kt @@ -5,4 +5,6 @@ import javafx.beans.property.StringProperty interface Layer { var name: String val nameProperty: StringProperty + + fun resize(rows: Int, columns: Int) } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/layer/TileLayer.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/layer/TileLayer.kt index e4d1c645..ddabbf04 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/layer/TileLayer.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/layer/TileLayer.kt @@ -2,17 +2,21 @@ package com.bartlomiejpluta.base.editor.model.map.layer import com.bartlomiejpluta.base.editor.model.tileset.Tile import javafx.beans.property.SimpleStringProperty -import tornadofx.* +import tornadofx.getValue +import tornadofx.setValue -class TileLayer(name: String, val layer: Array>) : Layer { - - fun setTile(row: Int, column: Int, tile: Tile?) = apply { layer[row][column] = tile } +class TileLayer(name: String, rows: Int, columns: Int) : Layer { + var layer: Array> = Array(rows) { Array(columns) { null } } + private set override val nameProperty = SimpleStringProperty(name) - override var name: String by nameProperty - companion object { - fun empty(name: String, rows: Int ,columns: Int) = TileLayer(name, Array(rows) { Array(columns) { null } }) + override fun resize(rows: Int, columns: Int) { + layer = Array(rows) { row -> + Array(columns) { column -> + layer.getOrNull(row) ?. getOrNull(column) + } + } } } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/map/GameMap.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/map/GameMap.kt index d049ce17..ff1fbd98 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/map/GameMap.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/map/GameMap.kt @@ -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.tileset.TileSet -import javafx.beans.property.ReadOnlyDoubleWrapper -import javafx.beans.property.ReadOnlyFloatProperty import javafx.beans.property.SimpleDoubleProperty 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) { val layers = observableListOf() - val mapProperties = observableListOf() - val tileWidth = tileSet.tileWidth.toDouble() val tileHeight = tileSet.tileHeight.toDouble() @@ -33,11 +31,15 @@ class GameMap(val tileSet: TileSet, initialColumns: Int, initialRows: Int) { init { rowsProperty.addListener { _, _, newValue -> - height = newValue.toInt() * tileWidth + val newRows = newValue.toInt() + height = newRows * tileWidth + layers.forEach { it.resize(newRows, columns) } } columnsProperty.addListener { _, _, newValue -> - width = newValue.toInt() * tileWidth + val newColumns = newValue.toInt() + width = newColumns * tileWidth + layers.forEach { it.resize(rows, newColumns) } } } } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/map/MapProperty.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/map/MapProperty.kt deleted file mode 100755 index 87c3eb22..00000000 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/map/MapProperty.kt +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapFragment.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapFragment.kt index 6e0bebf4..b1e2124f 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapFragment.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapFragment.kt @@ -7,6 +7,7 @@ import com.bartlomiejpluta.base.editor.model.map.map.GameMap import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM import org.kordamp.ikonli.javafx.FontIcon import tornadofx.* +import kotlin.math.max class MapFragment : Fragment() { @@ -39,17 +40,33 @@ class MapFragment : Fragment() { } } - button(graphic = FontIcon("fa-plus")) { + button(text = "Rows", graphic = FontIcon("fa-minus")) { action { - mapVM.rows = mapVM.rows + 1 + mapVM.rows = max(mapVM.rows - 1, 1) mapVM.commit() fire(RedrawMapRequestEvent) } } - button(graphic = FontIcon("fa-plus")) { + button(text = "Columns", graphic = FontIcon("fa-minus")) { 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() fire(RedrawMapRequestEvent) } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapLayersView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapLayersView.kt index 4f452d8c..1fe1f225 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapLayersView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapLayersView.kt @@ -37,7 +37,7 @@ class MapLayersView : View() { bottom = toolbar { button(graphic = FontIcon("fa-plus")) { 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) command.execute() layersPane.selectionModel.select(mapVM.layers.size - 1) diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapPropertiesView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapPropertiesView.kt deleted file mode 100755 index cf0cea85..00000000 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapPropertiesView.kt +++ /dev/null @@ -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() - - override val root = tableview(mapVM.mapProperties) { - column("Key", MapProperty::keyProperty) - column("Value", MapProperty::valueProperty) - } -} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/GameMapVM.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/GameMapVM.kt index a6b86e86..f6d65bcd 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/GameMapVM.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/GameMapVM.kt @@ -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.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.SimpleListProperty -import tornadofx.* +import tornadofx.ItemViewModel +import tornadofx.getValue +import tornadofx.setValue class GameMapVM : ItemViewModel() { val layers: SimpleListProperty = bind(GameMap::layers) - val mapProperties: SimpleListProperty = bind(GameMap::mapProperties) val tileSetProperty = bind(GameMap::tileSet) val tileSet by tileSetProperty @@ -33,11 +31,6 @@ class GameMapVM : ItemViewModel() { val heightProperty = bind(GameMap::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 selectedLayer by selectedLayerProperty