diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/editor/EditorOptions.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/editor/EditorOptions.kt new file mode 100755 index 00000000..3f7c6130 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/model/map/editor/EditorOptions.kt @@ -0,0 +1,8 @@ +package com.bartlomiejpluta.base.editor.model.map.editor + +class EditorOptions { + var selectedLayer = 0 + var showGrid = true +} + + 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 b1f04950..dee4fa98 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 @@ -3,13 +3,13 @@ package com.bartlomiejpluta.base.editor.render.canvas.map import com.bartlomiejpluta.base.editor.model.map.layer.Layer import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer import com.bartlomiejpluta.base.editor.render.model.Renderable +import com.bartlomiejpluta.base.editor.viewmodel.map.EditorOptionsVM import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM import javafx.scene.canvas.GraphicsContext import javafx.scene.paint.Color -import org.slf4j.LoggerFactory -class MapCanvas(val map: GameMapVM, private val painter: MapPainter) : Renderable { +class MapCanvas(val map: GameMapVM, private val editorOptionsVM: EditorOptionsVM, private val painter: MapPainter) : Renderable { var tileSet = map.tileSet private var tileWidth = map.tileWidth private var tileHeight = map.tileHeight @@ -22,7 +22,9 @@ class MapCanvas(val map: GameMapVM, private val painter: MapPainter) : Renderabl map.layers.forEach { dispatchLayerRender(gc, it) } - renderGrid(gc) + if (editorOptionsVM.showGrid) { + renderGrid(gc) + } painter.render(gc) } 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 32466aaf..55ee224b 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 @@ -5,6 +5,7 @@ import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEventHandler import com.bartlomiejpluta.base.editor.render.model.Renderable import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM +import com.bartlomiejpluta.base.editor.viewmodel.map.EditorOptionsVM import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM import javafx.scene.canvas.GraphicsContext import javafx.scene.input.MouseButton @@ -14,6 +15,7 @@ import javafx.scene.paint.Color class MapPainter( private val mapVM: GameMapVM, private val brushVM: BrushVM, + private val editorOptionsVM: EditorOptionsVM, private val paintingCallback: (MapPaintingTrace) -> Unit ) : Renderable, MapMouseEventHandler { private val tileWidth = mapVM.tileSet.tileWidth.toDouble() @@ -65,10 +67,10 @@ class MapPainter( } private fun beginTrace(event: MapMouseEvent) { - if (event.button == MouseButton.PRIMARY && mapVM.selectedLayer >= 0) { + if (event.button == MouseButton.PRIMARY && editorOptionsVM.selectedLayer >= 0) { currentTrace = MapPaintingTrace(mapVM, "Paint trace").apply { brushVM.forEach { row, column, centerRow, centerColumn, tile -> - paint(map.selectedLayer, mouseRow - centerRow + row, mouseColumn - centerColumn + column, tile) + paint(editorOptionsVM.selectedLayer, mouseRow - centerRow + row, mouseColumn - centerColumn + column, tile) } } } @@ -78,7 +80,7 @@ class MapPainter( if (event.button == MouseButton.PRIMARY) { currentTrace?.apply { brushVM.forEach { row, column, centerRow, centerColumn, tile -> - paint(map.selectedLayer, mouseRow - centerRow + row, mouseColumn - centerColumn + column, tile) + paint(editorOptionsVM.selectedLayer, mouseRow - centerRow + row, mouseColumn - centerColumn + column, tile) } } } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt index e8b4d9d3..47231d10 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt @@ -5,18 +5,21 @@ import com.bartlomiejpluta.base.editor.render.canvas.map.MapCanvas import com.bartlomiejpluta.base.editor.render.canvas.map.MapPainter import com.bartlomiejpluta.base.editor.render.canvas.map.MapPaintingTrace import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM +import com.bartlomiejpluta.base.editor.viewmodel.map.EditorOptionsVM import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM import javafx.event.EventHandler import javafx.scene.canvas.Canvas import javafx.scene.input.MouseEvent -class MapPane(private val mapVM: GameMapVM, +class MapPane( + private val mapVM: GameMapVM, brushVM: BrushVM, + editorOptionsVM: EditorOptionsVM, paintingCallback: (MapPaintingTrace) -> Unit ) : Canvas(), EventHandler { - private val painter = MapPainter(mapVM, brushVM, paintingCallback) - private val mapCanvas = MapCanvas(mapVM, painter) + private val painter = MapPainter(mapVM, brushVM, editorOptionsVM, paintingCallback) + private val mapCanvas = MapCanvas(mapVM, editorOptionsVM, painter) init { onMouseMoved = this @@ -27,6 +30,8 @@ class MapPane(private val mapVM: GameMapVM, widthProperty().bind(mapVM.widthProperty) heightProperty().bind(mapVM.heightProperty) + editorOptionsVM.showGridProperty.addListener { _, _, _ -> render() } + render() } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapLayersView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapLayersView.kt index 1fe1f225..250a8bec 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapLayersView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapLayersView.kt @@ -9,6 +9,7 @@ import com.bartlomiejpluta.base.editor.command.service.UndoRedoService import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent import com.bartlomiejpluta.base.editor.model.map.layer.Layer import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer +import com.bartlomiejpluta.base.editor.viewmodel.map.EditorOptionsVM import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM import javafx.scene.control.TableView import org.kordamp.ikonli.javafx.FontIcon @@ -21,6 +22,8 @@ class MapLayersView : View() { private val mapVM = find() + private val editorOptionsVM = find() + private var layersPane = TableView(mapVM.layers).apply { column("Layer Name", Layer::nameProperty).makeEditable().setOnEditCommit { val command = RenameLayerCommand(it.rowValue, it.newValue) @@ -28,7 +31,7 @@ class MapLayersView : View() { undoRedoService.push(command, scope) } - mapVM.selectedLayerProperty.bind(selectionModel.selectedIndexProperty()) + editorOptionsVM.selectedLayerProperty.bind(selectionModel.selectedIndexProperty()) } override val root = borderpane { @@ -46,10 +49,10 @@ class MapLayersView : View() { } button(graphic = FontIcon("fa-chevron-up")) { - enableWhen(mapVM.selectedLayerProperty.greaterThan(0)) + enableWhen(editorOptionsVM.selectedLayerProperty.greaterThan(0)) action { - val newIndex = mapVM.selectedLayer - 1 - val command = MoveLayerCommand(mapVM.item, mapVM.selectedLayer, newIndex) + val newIndex = editorOptionsVM.selectedLayer - 1 + val command = MoveLayerCommand(mapVM.item, editorOptionsVM.selectedLayer, newIndex) command.execute() layersPane.selectionModel.select(newIndex) fire(RedrawMapRequestEvent) @@ -59,12 +62,12 @@ class MapLayersView : View() { button(graphic = FontIcon("fa-chevron-down")) { enableWhen( - mapVM.selectedLayerProperty.lessThan(mapVM.layers.sizeProperty().minus(1)) - .and(mapVM.selectedLayerProperty.greaterThanOrEqualTo(0)) + editorOptionsVM.selectedLayerProperty.lessThan(mapVM.layers.sizeProperty().minus(1)) + .and(editorOptionsVM.selectedLayerProperty.greaterThanOrEqualTo(0)) ) action { - val newIndex = mapVM.selectedLayer + 1 - val command = MoveLayerCommand(mapVM.item, mapVM.selectedLayer, newIndex) + val newIndex = editorOptionsVM.selectedLayer + 1 + val command = MoveLayerCommand(mapVM.item, editorOptionsVM.selectedLayer, newIndex) command.execute() layersPane.selectionModel.select(newIndex) fire(RedrawMapRequestEvent) @@ -73,9 +76,9 @@ class MapLayersView : View() { } button(graphic = FontIcon("fa-trash")) { - enableWhen(mapVM.selectedLayerProperty.greaterThanOrEqualTo(0)) + enableWhen(editorOptionsVM.selectedLayerProperty.greaterThanOrEqualTo(0)) action { - var index = mapVM.selectedLayer + var index = editorOptionsVM.selectedLayer val command = RemoveLayerCommand(mapVM.item, index) command.execute() 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 0ca67a28..419db54f 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 @@ -5,6 +5,7 @@ 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.EditorOptionsVM import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM import javafx.scene.control.ToggleGroup import org.kordamp.ikonli.javafx.FontIcon @@ -20,6 +21,7 @@ class MapToolbarView : View() { private val mapVM = find() private val brushVM = find() + private val editorOptionsVM = find() private val brushMode = ToggleGroup().apply { brushVM.itemProperty.addListener { _, _, brush -> @@ -76,6 +78,15 @@ class MapToolbarView : View() { } } + togglebutton { + graphic = FontIcon("fa-th") + + action { + editorOptionsVM.showGrid = isSelected + editorOptionsVM.commit() + } + } + togglebutton(value = BrushMode.PAINTING_MODE, group = brushMode) { graphic = FontIcon("fa-paint-brush") diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapView.kt index 241f3bca..acef6685 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/map/MapView.kt @@ -5,6 +5,7 @@ import com.bartlomiejpluta.base.editor.command.service.UndoRedoService import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent import com.bartlomiejpluta.base.editor.view.component.map.MapPane import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM +import com.bartlomiejpluta.base.editor.viewmodel.map.EditorOptionsVM import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM import javafx.beans.property.SimpleDoubleProperty import javafx.scene.input.MouseButton @@ -27,11 +28,14 @@ class MapView : View() { xProperty().bind(zoomProperty) yProperty().bind(zoomProperty) } + private val mapVM = find() private val brushVM = find() - private val mapPane = MapPane(mapVM, brushVM) { undoRedoService.push(it, scope) } + private val editorOptionsVM = find() + + private val mapPane = MapPane(mapVM, brushVM, editorOptionsVM) { undoRedoService.push(it, scope) } init { brushVM.item = mapVM.tileSet.baseBrush diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/EditorOptionsVM.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/EditorOptionsVM.kt new file mode 100755 index 00000000..ba7e06f5 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/EditorOptionsVM.kt @@ -0,0 +1,13 @@ +package com.bartlomiejpluta.base.editor.viewmodel.map + +import com.bartlomiejpluta.base.editor.model.map.editor.EditorOptions +import javafx.beans.property.IntegerProperty +import tornadofx.* + +class EditorOptionsVM : ItemViewModel(EditorOptions()) { + val selectedLayerProperty = bind(EditorOptions::selectedLayer) as IntegerProperty + var selectedLayer by selectedLayerProperty + + val showGridProperty = bind(EditorOptions::showGrid) + var showGrid by showGridProperty +} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/GameMapVM.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/GameMapVM.kt index f6d65bcd..437dfee8 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/GameMapVM.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/viewmodel/map/GameMapVM.kt @@ -31,9 +31,6 @@ class GameMapVM : ItemViewModel() { val heightProperty = bind(GameMap::heightProperty) val height by heightProperty - - val selectedLayerProperty = SimpleIntegerProperty(0) - val selectedLayer by selectedLayerProperty }