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 1e3e14d3..1e1ff531 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 @@ -8,15 +8,22 @@ import tornadofx.setValue class Brush(newBrush: Array>) { val brush = observableListOf() + val rowsProperty = SimpleIntegerProperty(this, "", 0) var rows by rowsProperty + val columnsProperty = SimpleIntegerProperty(0) var columns by columnsProperty + val centerRowProperty = SimpleIntegerProperty(0) var centerRow by centerRowProperty + val centerColumnProperty = SimpleIntegerProperty(0) var centerColumn by centerColumnProperty + val brushRangeProperty = SimpleIntegerProperty(1) + var brushRange by brushRangeProperty + init { rowsProperty.value = newBrush.size @@ -35,4 +42,6 @@ class Brush(newBrush: Array>) { consumer(id / columns, id % columns, tile) } } + + fun withBrushRange(range: Int) = Brush(Array(range) { Array(range) { brush[0] } }).apply { brushRange = range } } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt index 688ca4b3..b1f04950 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt @@ -16,8 +16,6 @@ class MapCanvas(val map: GameMapVM, private val painter: MapPainter) : Renderabl override fun render(gc: GraphicsContext) { - log.debug("vm.dim = ${map.rows}x${map.columns} | map.dim = ${map.item.rows}x${map.item.columns}") - log.debug("vm.size = ${map.width}x${map.height} | map.size = ${map.item.width}x${map.item.height}") gc.clearRect(0.0, 0.0, gc.canvas.width, gc.canvas.height) renderBackground(gc) @@ -72,7 +70,6 @@ class MapCanvas(val map: GameMapVM, private val painter: MapPainter) : Renderabl } companion object { - private val log = LoggerFactory.getLogger(MapCanvas::class.java) private val BACKGROUND_COLOR1 = Color.color(1.0, 1.0, 1.0, 1.0) private val BACKGROUND_COLOR2 = Color.color(0.95, 0.95, 0.95, 0.95) } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/tileset/TileSetCanvas.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/tileset/TileSetCanvas.kt index 044c6c8f..3c3a2c23 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/tileset/TileSetCanvas.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/tileset/TileSetCanvas.kt @@ -10,9 +10,7 @@ import javafx.scene.input.MouseButton import javafx.scene.input.MouseEvent import javafx.scene.paint.Color -class TileSetCanvas(private val gameMapVM: GameMapVM, brushVM: BrushVM) : Renderable, MapMouseEventHandler { - private var selection = TileSetSelection(gameMapVM, brushVM) - +class TileSetCanvas(private val gameMapVM: GameMapVM, private val selection: TileSetSelection) : Renderable, MapMouseEventHandler { private var mouseRow = -1 private var mouseColumn = -1 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 488481b6..4ac9f7da 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 @@ -1,8 +1,6 @@ package com.bartlomiejpluta.base.editor.render.canvas.tileset import com.bartlomiejpluta.base.editor.model.map.brush.Brush -import com.bartlomiejpluta.base.editor.model.tileset.Tile -import com.bartlomiejpluta.base.editor.model.tileset.TileSet import com.bartlomiejpluta.base.editor.render.model.Renderable import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM @@ -23,7 +21,13 @@ 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) { + resetBrushRange() + startRow = row offsetRow = 0.0 startColumn = column @@ -32,6 +36,10 @@ 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 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 afd6031b..0216fde4 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 @@ -2,6 +2,7 @@ package com.bartlomiejpluta.base.editor.view.component.tileset import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent import com.bartlomiejpluta.base.editor.render.canvas.tileset.TileSetCanvas +import com.bartlomiejpluta.base.editor.render.canvas.tileset.TileSetSelection import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM import javafx.event.EventHandler @@ -9,7 +10,8 @@ import javafx.scene.canvas.Canvas import javafx.scene.input.MouseEvent class TileSetPane(private val gameMapVM: GameMapVM, brushVM: BrushVM) : Canvas(), EventHandler { - private val tileSetCanvas = TileSetCanvas(gameMapVM, brushVM) + private val selection = TileSetSelection(gameMapVM, brushVM) + private val tileSetCanvas = TileSetCanvas(gameMapVM, selection) init { onMouseMoved = this @@ -20,6 +22,17 @@ 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.brushRangeProperty.addListener { _, _, newValue -> + if (newValue.toInt() > 1) { + selection.shrinkToTopLeftTile() + render() + } + } + render() } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapFragment.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapFragment.kt index b1e2124f..b6421fde 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapFragment.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapFragment.kt @@ -1,19 +1,14 @@ 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.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() { - private val undoRedoService: UndoRedoService by di() - override val scope = super.scope as UndoableScope + val map: GameMap by param() private val mapVM = find().apply { item = map } @@ -21,57 +16,11 @@ class MapFragment : Fragment() { private val mapView = find() private val layersView = find() private val tileSetView = find() + private val toolbarView = find() + override val root = borderpane { - top = toolbar { - button(graphic = FontIcon("fa-undo")) { - shortcut("Ctrl+Z") - action { - undoRedoService.undo(scope) - fire(RedrawMapRequestEvent) - } - } - - button(graphic = FontIcon("fa-repeat")) { - shortcut("Ctrl+Shift+Z") - action { - undoRedoService.redo(scope) - fire(RedrawMapRequestEvent) - } - } - - button(text = "Rows", graphic = FontIcon("fa-minus")) { - action { - mapVM.rows = max(mapVM.rows - 1, 1) - mapVM.commit() - fire(RedrawMapRequestEvent) - } - } - - button(text = "Columns", graphic = FontIcon("fa-minus")) { - action { - 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) - } - } - } + top = toolbarView.root center = mapView.root 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 new file mode 100755 index 00000000..4e0c5138 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapToolbarView.kt @@ -0,0 +1,97 @@ +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.viewmodel.map.BrushVM +import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM +import javafx.scene.control.ToggleGroup +import org.kordamp.ikonli.javafx.FontIcon +import tornadofx.* +import kotlin.math.max + +class MapToolbarView : View() { + private val undoRedoService: UndoRedoService by di() + + override val scope = super.scope as UndoableScope + + private val mapVM = find() + private val brushVM = find() + + private val tool = ToggleGroup() + + + override val root = toolbar { + button(graphic = FontIcon("fa-undo")) { + shortcut("Ctrl+Z") + action { + undoRedoService.undo(scope) + fire(RedrawMapRequestEvent) + } + } + + button(graphic = FontIcon("fa-repeat")) { + shortcut("Ctrl+Shift+Z") + action { + undoRedoService.redo(scope) + fire(RedrawMapRequestEvent) + } + } + + button(text = "Rows", graphic = FontIcon("fa-minus")) { + action { + mapVM.rows = max(mapVM.rows - 1, 1) + mapVM.commit() + fire(RedrawMapRequestEvent) + } + } + + button(text = "Columns", graphic = FontIcon("fa-minus")) { + action { + 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) + } + } + + togglebutton(tool) { + graphic = FontIcon("fa-paint-brush") + } + + togglebutton(tool) { + graphic = FontIcon("fa-eraser") + } + + this += FontIcon("fa-paint-brush") + + button(graphic = FontIcon("fa-plus")) { + enableWhen(brushVM.brushRangeProperty.lessThan(5)) + action { + brushVM.item = brushVM.withBrushRange(brushVM.brushRange + 1) + } + } + + button(graphic = FontIcon("fa-minus")) { + enableWhen(brushVM.brushRangeProperty.greaterThan(1)) + action { + brushVM.item = brushVM.withBrushRange(brushVM.brushRange - 1) + } + } + } +} \ No newline at end of file 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 bbde958a..13b9231b 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 @@ -2,8 +2,7 @@ package com.bartlomiejpluta.base.editor.viewmodel.map import com.bartlomiejpluta.base.editor.model.map.brush.Brush import com.bartlomiejpluta.base.editor.model.tileset.Tile -import tornadofx.ItemViewModel -import tornadofx.getValue +import tornadofx.* class BrushVM : ItemViewModel(Brush(arrayOf(arrayOf()))) { val brush = bind(Brush::brush) @@ -20,5 +19,10 @@ class BrushVM : ItemViewModel(Brush(arrayOf(arrayOf()))) { val centerColumnProperty = bind(Brush::centerColumnProperty) val centerColumn by centerColumnProperty + val brushRangeProperty = bind(Brush::brushRangeProperty) + var brushRange by brushRangeProperty + fun forEach(consumer: (row: Int, column: Int, tile: Tile) -> Unit) = item.forEach(consumer) + + fun withBrushRange(range: Int) = item.withBrushRange(range) } \ No newline at end of file