From c2bce5dca1ebd5fb3316336eb39d266b9b1d5fd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Sun, 7 Feb 2021 14:17:24 +0100 Subject: [PATCH] [Editor] Refactor brush code #3 --- .../base/editor/model/map/brush/Brush.kt | 28 ++++++++----------- .../editor/render/canvas/map/MapPainter.kt | 28 ++++++++++++------- .../render/canvas/tileset/TileSetSelection.kt | 4 --- .../view/component/tileset/TileSetPane.kt | 11 +------- .../base/editor/view/map/MapToolbarView.kt | 8 +++--- .../base/editor/viewmodel/map/BrushVM.kt | 11 ++++---- 6 files changed, 40 insertions(+), 50 deletions(-) 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 9797f2b3..35180ffe 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 @@ -20,8 +20,8 @@ class Brush { var columns by columnsProperty private set - val brushRangeProperty = SimpleIntegerProperty(1) - var brushRange by brushRangeProperty + val rangeProperty = SimpleIntegerProperty(1) + var range by rangeProperty private set val modeProperty = SimpleObjectProperty(BrushMode.PAINTING_MODE) @@ -49,16 +49,16 @@ class Brush { fun forEach(consumer: (row: Int, column: Int, centerRow: Int, centerColumn: Int, tile: Tile?) -> Unit) { return when { - brushRange > 1 || mode == BrushMode.ERASING_MODE -> forEachInRangedBrush(consumer) + range > 1 || mode == BrushMode.ERASING_MODE -> forEachInRangedBrush(consumer) else -> forEachInRegularBrush(consumer) } } private fun forEachInRangedBrush(consumer: (row: Int, column: Int, centerRow: Int, centerColumn: Int, tile: Tile?) -> Unit) { - val center = brushRange / 2 + val center = range / 2 - (0 until brushRange).forEach { row -> - (0 until brushRange).forEach { column -> + (0 until range).forEach { row -> + (0 until range).forEach { column -> consumer(row, column, center, center, getTileByMode(brush[0])) } } @@ -79,20 +79,16 @@ class Brush { } private fun clone() = Brush(brush, rows, columns).apply { - brushRange = this@Brush.brushRange - mode = this@Brush.mode + this.range = this@Brush.range + this.mode = this@Brush.mode } - fun withBrushRange(range: Int) = clone().apply { - brushRange = range + fun withRange(range: Int) = clone().apply { + this.range = range } - fun withErasingMode() = clone().apply { - mode = BrushMode.ERASING_MODE - } - - fun withPaintingMode() = clone().apply { - mode = BrushMode.PAINTING_MODE + fun withBrushMode(mode: BrushMode) = clone().apply { + this.mode = mode } companion object { 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 a2f770cd..32466aaf 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 @@ -9,6 +9,7 @@ import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM import javafx.scene.canvas.GraphicsContext import javafx.scene.input.MouseButton import javafx.scene.input.MouseEvent +import javafx.scene.paint.Color class MapPainter( private val mapVM: GameMapVM, @@ -28,21 +29,28 @@ class MapPainter( gc.globalAlpha = 0.4 brushVM.forEach { row, column, centerRow, centerColumn, tile -> - tile?.let { - renderTile(gc, it, column, row, centerRow, centerColumn) - } + renderTile(gc, row, column, centerRow, centerColumn, tile) } gc.globalAlpha = alpha - } - private fun renderTile(gc: GraphicsContext, tile: Tile, column: Int, row: Int, centerRow: Int, centerColumn: Int) { - gc.drawImage( - tile.image, - tileWidth * (mouseColumn - centerColumn + column), - tileHeight * (mouseRow - centerRow + row) - ) + private fun renderTile(gc: GraphicsContext, row: Int, column: Int, centerRow: Int, centerColumn: Int, tile: Tile?) { + val x = tileWidth * (mouseColumn - centerColumn + column) + val y = tileHeight * (mouseRow - centerRow + row) + + when { + tile != null -> renderPaintingBrushTile(gc, tile, x, y) + else -> renderEraserTile(gc, x, y) + } + } + + private fun renderPaintingBrushTile(gc: GraphicsContext, tile: Tile, x: Double, y: Double) = + gc.drawImage(tile.image, x, y) + + private fun renderEraserTile(gc: GraphicsContext, x: Double, y: Double) { + gc.fill = Color.WHITE + gc.fillRect(x, y, tileWidth, tileHeight) } override fun handleMouseInput(event: MapMouseEvent) { 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 53cabd25..dac96d2b 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 @@ -21,10 +21,6 @@ class TileSetSelection(private val gameMapVM: GameMapVM, private val brushVM: Br private var height = gameMapVM.tileSet.tileHeight.toDouble() - fun shrinkToTopLeftTile() { - proceed(startRow, startColumn) - } - fun begin(row: Double, column: Double) { startRow = row offsetRow = 0.0 diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/tileset/TileSetPane.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/tileset/TileSetPane.kt index 5b3970d4..68c8edb4 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/tileset/TileSetPane.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/tileset/TileSetPane.kt @@ -22,17 +22,8 @@ class TileSetPane(private val gameMapVM: GameMapVM, brushVM: BrushVM) : Canvas() width = gameMapVM.tileSet.width.toDouble() height = gameMapVM.tileSet.height.toDouble() - // Shrink the selection just one tile (the top left one) - // when brush range (size) is increased to 2 or more - // (because the range-increased brush can only include - // the tile of one type). - brushVM.itemProperty.addListener { _, _, brush -> - if (brush.brushRange > 1) { - selection.shrinkToTopLeftTile() - } - render() - } + brushVM.itemProperty.addListener { _, _, _ -> render() } render() } 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 62f57650..0ca67a28 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 @@ -80,7 +80,7 @@ class MapToolbarView : View() { graphic = FontIcon("fa-paint-brush") action { - brushVM.item = brushVM.withPaintingMode() + brushVM.item = brushVM.withMode(BrushMode.PAINTING_MODE) } } @@ -88,7 +88,7 @@ class MapToolbarView : View() { graphic = FontIcon("fa-eraser") action { - brushVM.item = brushVM.withErasingMode() + brushVM.item = brushVM.withMode(BrushMode.ERASING_MODE) } } @@ -100,11 +100,11 @@ class MapToolbarView : View() { minorTickCount = 0 valueProperty().addListener { _, _, newValue -> - brushVM.item = brushVM.withBrushRange(newValue.toInt()) + brushVM.item = brushVM.withRange(newValue.toInt()) } brushVM.itemProperty.addListener { _, _, brush -> - value = brush.brushRange.toDouble() + value = brush.range.toDouble() } } 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 7942a8a9..83890f14 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 @@ -1,6 +1,7 @@ package com.bartlomiejpluta.base.editor.viewmodel.map import com.bartlomiejpluta.base.editor.model.map.brush.Brush +import com.bartlomiejpluta.base.editor.model.map.brush.BrushMode import com.bartlomiejpluta.base.editor.model.tileset.Tile import tornadofx.ItemViewModel import tornadofx.getValue @@ -14,17 +15,15 @@ class BrushVM : ItemViewModel(Brush.of(arrayOf(arrayOf()))) { val columnsProperty = bind(Brush::columnsProperty) val columns by columnsProperty - val brushRangeProperty = bind(Brush::brushRangeProperty) - val brushRange by brushRangeProperty + val rangeProperty = bind(Brush::rangeProperty) + val range by rangeProperty val modeProperty = bind(Brush::modeProperty) val mode by modeProperty fun forEach(consumer: (row: Int, column: Int, centerRow: Int, centerColumn: Int, tile: Tile?) -> Unit) = item.forEach(consumer) - fun withBrushRange(range: Int) = item.withBrushRange(range) + fun withRange(range: Int) = item.withRange(range) - fun withErasingMode() = item.withErasingMode() - - fun withPaintingMode() = item.withPaintingMode() + fun withMode(mode: BrushMode) = item.withBrushMode(mode) } \ No newline at end of file