[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 {
var name: String
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 javafx.beans.property.SimpleStringProperty
import tornadofx.*
import tornadofx.getValue
import tornadofx.setValue
class TileLayer(name: String, val layer: Array<Array<Tile?>>) : 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<Tile?>> = 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)
}
}
}
}

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.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<Layer>()
val mapProperties = observableListOf<MapProperty>()
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) }
}
}
}

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 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)
}

View File

@@ -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)

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.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<GameMap>() {
val layers: SimpleListProperty<Layer> = bind(GameMap::layers)
val mapProperties: SimpleListProperty<MapProperty> = bind(GameMap::mapProperties)
val tileSetProperty = bind(GameMap::tileSet)
val tileSet by tileSetProperty
@@ -33,11 +31,6 @@ class GameMapVM : ItemViewModel<GameMap>() {
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