[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
import com.bartlomiejpluta.base.editor.model.tileset.Tile
import javafx.beans.property.SimpleBooleanProperty
import javafx.beans.property.SimpleIntegerProperty
import javafx.collections.FXCollections
import javafx.beans.property.SimpleObjectProperty
import javafx.collections.ObservableList
import tornadofx.asObservable
import tornadofx.getValue
@@ -28,8 +27,8 @@ class Brush {
val brushRangeProperty = SimpleIntegerProperty(1)
var brushRange by brushRangeProperty
val erasingProperty = SimpleBooleanProperty(false)
var erasing by erasingProperty
val modeProperty = SimpleObjectProperty(BrushMode.PAINTING_MODE)
var mode by modeProperty
private constructor(brushArray: Array<Array<Tile>>) {
rowsProperty.value = brushArray.size
@@ -58,23 +57,26 @@ class Brush {
fun forEach(consumer: (row: Int, column: Int, tile: Tile?) -> Unit) {
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 {
brushRange = range
erasing = this@Brush.erasing
mode = this@Brush.mode
}
fun withErasingMode() = Brush(brush, rows, columns).apply {
brushRange = this@Brush.brushRange
erasing = true
mode = BrushMode.ERASING_MODE
}
fun withPaintingMode() = Brush(brush, rows, columns).apply {
brushRange = this@Brush.brushRange
erasing = false
mode = BrushMode.PAINTING_MODE
}
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) {
resetBrushRange()
startRow = row
offsetRow = 0.0
startColumn = column
@@ -36,10 +34,6 @@ class TileSetSelection(private val gameMapVM: GameMapVM, private val brushVM: Br
updateRect(row, column)
}
private fun resetBrushRange() {
brushVM.brushRange = 1
}
private fun updateRect(row: Double, column: Double) {
x = min(column, startColumn) * gameMapVM.tileSet.tileWidth
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.service.UndoRedoService
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.GameMapVM
import javafx.scene.control.ToggleGroup
@@ -18,9 +19,9 @@ class MapToolbarView : View() {
private val mapVM = find<GameMapVM>()
private val brushVM = find<BrushVM>()
private val tool = ToggleGroup().apply {
brushVM.erasingProperty.addListener { observable, oldValue, newValue ->
selectedValueProperty<Boolean>().value = newValue
private val brushMode = ToggleGroup().apply {
brushVM.erasingProperty.addListener { _, _, 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")
action {
@@ -81,7 +82,7 @@ class MapToolbarView : View() {
}
}
togglebutton(value = true, group = tool) {
togglebutton(value = BrushMode.ERASING_MODE, group = brushMode) {
graphic = FontIcon("fa-eraser")
action {

View File

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