diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/brush/Brush.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/brush/Brush.kt index 1e1ff531..c541b657 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/brush/Brush.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/brush/Brush.kt @@ -1,13 +1,17 @@ package com.bartlomiejpluta.base.editor.model.map.brush import com.bartlomiejpluta.base.editor.model.tileset.Tile +import javafx.beans.property.SimpleBooleanProperty import javafx.beans.property.SimpleIntegerProperty +import javafx.collections.FXCollections +import javafx.collections.ObservableList +import tornadofx.asObservable import tornadofx.getValue import tornadofx.observableListOf import tornadofx.setValue -class Brush(newBrush: Array>) { - val brush = observableListOf() +class Brush { + val brush: ObservableList val rowsProperty = SimpleIntegerProperty(this, "", 0) var rows by rowsProperty @@ -24,10 +28,15 @@ class Brush(newBrush: Array>) { val brushRangeProperty = SimpleIntegerProperty(1) var brushRange by brushRangeProperty - init { - rowsProperty.value = newBrush.size + val erasingProperty = SimpleBooleanProperty(false) + var erasing by erasingProperty - newBrush.forEach { brush.addAll(it) } + private constructor(brushArray: Array>) { + rowsProperty.value = brushArray.size + + brush = observableListOf() + + brushArray.forEach { brush.addAll(it) } if (rowsProperty.value > 0) { columns = brush.size / rowsProperty.value @@ -37,11 +46,38 @@ class Brush(newBrush: Array>) { centerColumn = columns / 2 } - fun forEach(consumer: (row: Int, column: Int, tile: Tile) -> Unit) { + private constructor(brush: List, rows: Int, columns: Int) { + this.rows = rows + this.columns = columns + + this.brush = brush.asObservable() + + centerRow = rows / 2 + centerColumn = columns / 2 + } + + fun forEach(consumer: (row: Int, column: Int, tile: Tile?) -> Unit) { brush.forEachIndexed { id, tile -> - consumer(id / columns, id % columns, tile) + consumer(id / columns, id % columns, if(erasing) null else tile) } } - fun withBrushRange(range: Int) = Brush(Array(range) { Array(range) { brush[0] } }).apply { brushRange = range } + fun withBrushRange(range: Int) = Brush(Array(range) { Array(range) { brush[0] } }).apply { + brushRange = range + erasing = this@Brush.erasing + } + + fun withErasingMode() = Brush(brush, rows, columns).apply { + brushRange = this@Brush.brushRange + erasing = true + } + + fun withPaintingMode() = Brush(brush, rows, columns).apply { + brushRange = this@Brush.brushRange + erasing = false + } + + companion object { + fun of(brushArray: Array>) = Brush(brushArray) + } } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/tileset/TileSet.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/tileset/TileSet.kt index 5a37bb6d..2637f1fd 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/tileset/TileSet.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/tileset/TileSet.kt @@ -32,7 +32,7 @@ class TileSet(private val image: Image, rows: Int, columns: Int) { val tiles = (0 until rows * columns).map { cropTile(it / columns, it % columns) }.toObservable() val baseBrush: Brush - get() = Brush(arrayOf(arrayOf(tiles[0]))) + get() = Brush.of(arrayOf(arrayOf(tiles[0]))) private fun cropTile(row: Int, column: Int): Tile { val reader = image.pixelReader diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapPainter.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapPainter.kt index 3bb3d6df..a85cd021 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapPainter.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapPainter.kt @@ -27,7 +27,7 @@ class MapPainter( val alpha = gc.globalAlpha gc.globalAlpha = 0.4 - brushVM.forEach { row, column, tile -> renderTile(gc, tile, column, row) } + brushVM.forEach { row, column, tile -> tile?.let { renderTile(gc, it, column, row) } } gc.globalAlpha = alpha diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/tileset/TileSetSelection.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/tileset/TileSetSelection.kt index 4ac9f7da..aa820a11 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/tileset/TileSetSelection.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/tileset/TileSetSelection.kt @@ -71,7 +71,7 @@ class TileSetSelection(private val gameMapVM: GameMapVM, private val brushVM: Br } } - brushVM.item = Brush(brushArray) + brushVM.item = Brush.of(brushArray) } override fun render(gc: GraphicsContext) { diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapToolbarView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapToolbarView.kt index 318e8158..7fb7077a 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapToolbarView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapToolbarView.kt @@ -18,7 +18,11 @@ class MapToolbarView : View() { private val mapVM = find() private val brushVM = find() - private val tool = ToggleGroup() + private val tool = ToggleGroup().apply { + brushVM.erasingProperty.addListener { observable, oldValue, newValue -> + selectedValueProperty().value = newValue + } + } override val root = toolbar { button(graphic = FontIcon("fa-undo")) { @@ -69,12 +73,20 @@ class MapToolbarView : View() { } } - togglebutton(tool) { + togglebutton(value = false, group = tool) { graphic = FontIcon("fa-paint-brush") + + action { + brushVM.item = brushVM.withPaintingMode() + } } - togglebutton(tool) { + togglebutton(value = true, group = tool) { graphic = FontIcon("fa-eraser") + + action { + brushVM.item = brushVM.withErasingMode() + } } this += FontIcon("fa-paint-brush").apply { iconSize = 10 } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/BrushVM.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/BrushVM.kt index 13b9231b..963ca4e5 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/BrushVM.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/BrushVM.kt @@ -4,7 +4,7 @@ import com.bartlomiejpluta.base.editor.model.map.brush.Brush import com.bartlomiejpluta.base.editor.model.tileset.Tile import tornadofx.* -class BrushVM : ItemViewModel(Brush(arrayOf(arrayOf()))) { +class BrushVM : ItemViewModel(Brush.of(arrayOf(arrayOf()))) { val brush = bind(Brush::brush) val rowsProperty = bind(Brush::rowsProperty) @@ -22,7 +22,14 @@ class BrushVM : ItemViewModel(Brush(arrayOf(arrayOf()))) { val brushRangeProperty = bind(Brush::brushRangeProperty) var brushRange by brushRangeProperty - fun forEach(consumer: (row: Int, column: Int, tile: Tile) -> Unit) = item.forEach(consumer) + val erasingProperty = bind(Brush::erasingProperty) + var erasing by erasingProperty + + fun forEach(consumer: (row: Int, column: Int, tile: Tile?) -> Unit) = item.forEach(consumer) fun withBrushRange(range: Int) = item.withBrushRange(range) + + fun withErasingMode() = item.withErasingMode() + + fun withPaintingMode() = item.withPaintingMode() } \ No newline at end of file