[Editor] Refactor brush code

This commit is contained in:
2021-02-07 12:41:33 +01:00
parent 24437d9760
commit 6028ae27fb
5 changed files with 23 additions and 20 deletions

View File

@@ -1,9 +1,8 @@
package com.bartlomiejpluta.base.editor.model.map.brush package com.bartlomiejpluta.base.editor.model.map.brush
import com.bartlomiejpluta.base.editor.model.tileset.Tile import com.bartlomiejpluta.base.editor.model.tileset.Tile
import javafx.beans.property.SimpleBooleanProperty
import javafx.beans.property.SimpleIntegerProperty import javafx.beans.property.SimpleIntegerProperty
import javafx.collections.FXCollections import javafx.beans.property.SimpleObjectProperty
import javafx.collections.ObservableList import javafx.collections.ObservableList
import tornadofx.asObservable import tornadofx.asObservable
import tornadofx.getValue import tornadofx.getValue
@@ -28,8 +27,8 @@ class Brush {
val brushRangeProperty = SimpleIntegerProperty(1) val brushRangeProperty = SimpleIntegerProperty(1)
var brushRange by brushRangeProperty var brushRange by brushRangeProperty
val erasingProperty = SimpleBooleanProperty(false) val modeProperty = SimpleObjectProperty(BrushMode.PAINTING_MODE)
var erasing by erasingProperty var mode by modeProperty
private constructor(brushArray: Array<Array<Tile>>) { private constructor(brushArray: Array<Array<Tile>>) {
rowsProperty.value = brushArray.size rowsProperty.value = brushArray.size
@@ -58,23 +57,26 @@ class Brush {
fun forEach(consumer: (row: Int, column: Int, tile: Tile?) -> Unit) { fun forEach(consumer: (row: Int, column: Int, tile: Tile?) -> Unit) {
brush.forEachIndexed { id, tile -> brush.forEachIndexed { id, tile ->
consumer(id / columns, id % columns, if(erasing) null else tile) consumer(id / columns, id % columns, when(mode) {
BrushMode.PAINTING_MODE -> tile
BrushMode.ERASING_MODE -> null
})
} }
} }
fun withBrushRange(range: Int) = Brush(Array(range) { Array(range) { brush[0] } }).apply { fun withBrushRange(range: Int) = Brush(Array(range) { Array(range) { brush[0] } }).apply {
brushRange = range brushRange = range
erasing = this@Brush.erasing mode = this@Brush.mode
} }
fun withErasingMode() = Brush(brush, rows, columns).apply { fun withErasingMode() = Brush(brush, rows, columns).apply {
brushRange = this@Brush.brushRange brushRange = this@Brush.brushRange
erasing = true mode = BrushMode.ERASING_MODE
} }
fun withPaintingMode() = Brush(brush, rows, columns).apply { fun withPaintingMode() = Brush(brush, rows, columns).apply {
brushRange = this@Brush.brushRange brushRange = this@Brush.brushRange
erasing = false mode = BrushMode.PAINTING_MODE
} }
companion object { companion object {

View File

@@ -0,0 +1,6 @@
package com.bartlomiejpluta.base.editor.model.map.brush
enum class BrushMode {
PAINTING_MODE,
ERASING_MODE
}

View File

@@ -26,8 +26,6 @@ class TileSetSelection(private val gameMapVM: GameMapVM, private val brushVM: Br
} }
fun begin(row: Double, column: Double) { fun begin(row: Double, column: Double) {
resetBrushRange()
startRow = row startRow = row
offsetRow = 0.0 offsetRow = 0.0
startColumn = column startColumn = column
@@ -36,10 +34,6 @@ class TileSetSelection(private val gameMapVM: GameMapVM, private val brushVM: Br
updateRect(row, column) updateRect(row, column)
} }
private fun resetBrushRange() {
brushVM.brushRange = 1
}
private fun updateRect(row: Double, column: Double) { private fun updateRect(row: Double, column: Double) {
x = min(column, startColumn) * gameMapVM.tileSet.tileWidth x = min(column, startColumn) * gameMapVM.tileSet.tileWidth
y = min(row, startRow) * gameMapVM.tileSet.tileHeight y = min(row, startRow) * gameMapVM.tileSet.tileHeight

View File

@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.editor.view.map
import com.bartlomiejpluta.base.editor.command.context.UndoableScope import com.bartlomiejpluta.base.editor.command.context.UndoableScope
import com.bartlomiejpluta.base.editor.command.service.UndoRedoService import com.bartlomiejpluta.base.editor.command.service.UndoRedoService
import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent
import com.bartlomiejpluta.base.editor.model.map.brush.BrushMode
import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM
import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
import javafx.scene.control.ToggleGroup import javafx.scene.control.ToggleGroup
@@ -18,9 +19,9 @@ class MapToolbarView : View() {
private val mapVM = find<GameMapVM>() private val mapVM = find<GameMapVM>()
private val brushVM = find<BrushVM>() private val brushVM = find<BrushVM>()
private val tool = ToggleGroup().apply { private val brushMode = ToggleGroup().apply {
brushVM.erasingProperty.addListener { observable, oldValue, newValue -> brushVM.erasingProperty.addListener { _, _, newValue ->
selectedValueProperty<Boolean>().value = newValue selectedValueProperty<BrushMode>().value = newValue
} }
} }
@@ -73,7 +74,7 @@ class MapToolbarView : View() {
} }
} }
togglebutton(value = false, group = tool) { togglebutton(value = BrushMode.PAINTING_MODE, group = brushMode) {
graphic = FontIcon("fa-paint-brush") graphic = FontIcon("fa-paint-brush")
action { action {
@@ -81,7 +82,7 @@ class MapToolbarView : View() {
} }
} }
togglebutton(value = true, group = tool) { togglebutton(value = BrushMode.ERASING_MODE, group = brushMode) {
graphic = FontIcon("fa-eraser") graphic = FontIcon("fa-eraser")
action { action {

View File

@@ -22,7 +22,7 @@ class BrushVM : ItemViewModel<Brush>(Brush.of(arrayOf(arrayOf()))) {
val brushRangeProperty = bind(Brush::brushRangeProperty) val brushRangeProperty = bind(Brush::brushRangeProperty)
var brushRange by brushRangeProperty var brushRange by brushRangeProperty
val erasingProperty = bind(Brush::erasingProperty) val erasingProperty = bind(Brush::modeProperty)
var erasing by erasingProperty var erasing by erasingProperty
fun forEach(consumer: (row: Int, column: Int, tile: Tile?) -> Unit) = item.forEach(consumer) fun forEach(consumer: (row: Int, column: Int, tile: Tile?) -> Unit) = item.forEach(consumer)