[Editor] Create editor options object

This commit is contained in:
2021-02-07 14:44:48 +01:00
parent c2bce5dca1
commit f40bd5ee03
9 changed files with 68 additions and 23 deletions

View File

@@ -0,0 +1,8 @@
package com.bartlomiejpluta.base.editor.model.map.editor
class EditorOptions {
var selectedLayer = 0
var showGrid = true
}

View File

@@ -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.Layer
import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer
import com.bartlomiejpluta.base.editor.render.model.Renderable 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 com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
import javafx.scene.canvas.GraphicsContext import javafx.scene.canvas.GraphicsContext
import javafx.scene.paint.Color 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 var tileSet = map.tileSet
private var tileWidth = map.tileWidth private var tileWidth = map.tileWidth
private var tileHeight = map.tileHeight 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) } map.layers.forEach { dispatchLayerRender(gc, it) }
renderGrid(gc) if (editorOptionsVM.showGrid) {
renderGrid(gc)
}
painter.render(gc) painter.render(gc)
} }

View File

@@ -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.canvas.input.MapMouseEventHandler
import com.bartlomiejpluta.base.editor.render.model.Renderable import com.bartlomiejpluta.base.editor.render.model.Renderable
import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM 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 com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
import javafx.scene.canvas.GraphicsContext import javafx.scene.canvas.GraphicsContext
import javafx.scene.input.MouseButton import javafx.scene.input.MouseButton
@@ -14,6 +15,7 @@ import javafx.scene.paint.Color
class MapPainter( class MapPainter(
private val mapVM: GameMapVM, private val mapVM: GameMapVM,
private val brushVM: BrushVM, private val brushVM: BrushVM,
private val editorOptionsVM: EditorOptionsVM,
private val paintingCallback: (MapPaintingTrace) -> Unit private val paintingCallback: (MapPaintingTrace) -> Unit
) : Renderable, MapMouseEventHandler { ) : Renderable, MapMouseEventHandler {
private val tileWidth = mapVM.tileSet.tileWidth.toDouble() private val tileWidth = mapVM.tileSet.tileWidth.toDouble()
@@ -65,10 +67,10 @@ class MapPainter(
} }
private fun beginTrace(event: MapMouseEvent) { 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 { currentTrace = MapPaintingTrace(mapVM, "Paint trace").apply {
brushVM.forEach { row, column, centerRow, centerColumn, tile -> 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) { if (event.button == MouseButton.PRIMARY) {
currentTrace?.apply { currentTrace?.apply {
brushVM.forEach { row, column, centerRow, centerColumn, tile -> 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)
} }
} }
} }

View File

@@ -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.MapPainter
import com.bartlomiejpluta.base.editor.render.canvas.map.MapPaintingTrace import com.bartlomiejpluta.base.editor.render.canvas.map.MapPaintingTrace
import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM 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 com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
import javafx.event.EventHandler import javafx.event.EventHandler
import javafx.scene.canvas.Canvas import javafx.scene.canvas.Canvas
import javafx.scene.input.MouseEvent import javafx.scene.input.MouseEvent
class MapPane(private val mapVM: GameMapVM, class MapPane(
private val mapVM: GameMapVM,
brushVM: BrushVM, brushVM: BrushVM,
editorOptionsVM: EditorOptionsVM,
paintingCallback: (MapPaintingTrace) -> Unit paintingCallback: (MapPaintingTrace) -> Unit
) : Canvas(), EventHandler<MouseEvent> { ) : Canvas(), EventHandler<MouseEvent> {
private val painter = MapPainter(mapVM, brushVM, paintingCallback) private val painter = MapPainter(mapVM, brushVM, editorOptionsVM, paintingCallback)
private val mapCanvas = MapCanvas(mapVM, painter) private val mapCanvas = MapCanvas(mapVM, editorOptionsVM, painter)
init { init {
onMouseMoved = this onMouseMoved = this
@@ -27,6 +30,8 @@ class MapPane(private val mapVM: GameMapVM,
widthProperty().bind(mapVM.widthProperty) widthProperty().bind(mapVM.widthProperty)
heightProperty().bind(mapVM.heightProperty) heightProperty().bind(mapVM.heightProperty)
editorOptionsVM.showGridProperty.addListener { _, _, _ -> render() }
render() render()
} }

View File

@@ -9,6 +9,7 @@ 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.layer.Layer import com.bartlomiejpluta.base.editor.model.map.layer.Layer
import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer 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 com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
import javafx.scene.control.TableView import javafx.scene.control.TableView
import org.kordamp.ikonli.javafx.FontIcon import org.kordamp.ikonli.javafx.FontIcon
@@ -21,6 +22,8 @@ class MapLayersView : View() {
private val mapVM = find<GameMapVM>() private val mapVM = find<GameMapVM>()
private val editorOptionsVM = find<EditorOptionsVM>()
private var layersPane = TableView(mapVM.layers).apply { private var layersPane = TableView(mapVM.layers).apply {
column("Layer Name", Layer::nameProperty).makeEditable().setOnEditCommit { column("Layer Name", Layer::nameProperty).makeEditable().setOnEditCommit {
val command = RenameLayerCommand(it.rowValue, it.newValue) val command = RenameLayerCommand(it.rowValue, it.newValue)
@@ -28,7 +31,7 @@ class MapLayersView : View() {
undoRedoService.push(command, scope) undoRedoService.push(command, scope)
} }
mapVM.selectedLayerProperty.bind(selectionModel.selectedIndexProperty()) editorOptionsVM.selectedLayerProperty.bind(selectionModel.selectedIndexProperty())
} }
override val root = borderpane { override val root = borderpane {
@@ -46,10 +49,10 @@ class MapLayersView : View() {
} }
button(graphic = FontIcon("fa-chevron-up")) { button(graphic = FontIcon("fa-chevron-up")) {
enableWhen(mapVM.selectedLayerProperty.greaterThan(0)) enableWhen(editorOptionsVM.selectedLayerProperty.greaterThan(0))
action { action {
val newIndex = mapVM.selectedLayer - 1 val newIndex = editorOptionsVM.selectedLayer - 1
val command = MoveLayerCommand(mapVM.item, mapVM.selectedLayer, newIndex) val command = MoveLayerCommand(mapVM.item, editorOptionsVM.selectedLayer, newIndex)
command.execute() command.execute()
layersPane.selectionModel.select(newIndex) layersPane.selectionModel.select(newIndex)
fire(RedrawMapRequestEvent) fire(RedrawMapRequestEvent)
@@ -59,12 +62,12 @@ class MapLayersView : View() {
button(graphic = FontIcon("fa-chevron-down")) { button(graphic = FontIcon("fa-chevron-down")) {
enableWhen( enableWhen(
mapVM.selectedLayerProperty.lessThan(mapVM.layers.sizeProperty().minus(1)) editorOptionsVM.selectedLayerProperty.lessThan(mapVM.layers.sizeProperty().minus(1))
.and(mapVM.selectedLayerProperty.greaterThanOrEqualTo(0)) .and(editorOptionsVM.selectedLayerProperty.greaterThanOrEqualTo(0))
) )
action { action {
val newIndex = mapVM.selectedLayer + 1 val newIndex = editorOptionsVM.selectedLayer + 1
val command = MoveLayerCommand(mapVM.item, mapVM.selectedLayer, newIndex) val command = MoveLayerCommand(mapVM.item, editorOptionsVM.selectedLayer, newIndex)
command.execute() command.execute()
layersPane.selectionModel.select(newIndex) layersPane.selectionModel.select(newIndex)
fire(RedrawMapRequestEvent) fire(RedrawMapRequestEvent)
@@ -73,9 +76,9 @@ class MapLayersView : View() {
} }
button(graphic = FontIcon("fa-trash")) { button(graphic = FontIcon("fa-trash")) {
enableWhen(mapVM.selectedLayerProperty.greaterThanOrEqualTo(0)) enableWhen(editorOptionsVM.selectedLayerProperty.greaterThanOrEqualTo(0))
action { action {
var index = mapVM.selectedLayer var index = editorOptionsVM.selectedLayer
val command = RemoveLayerCommand(mapVM.item, index) val command = RemoveLayerCommand(mapVM.item, index)
command.execute() command.execute()

View File

@@ -5,6 +5,7 @@ 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.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.EditorOptionsVM
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
import org.kordamp.ikonli.javafx.FontIcon import org.kordamp.ikonli.javafx.FontIcon
@@ -20,6 +21,7 @@ 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 editorOptionsVM = find<EditorOptionsVM>()
private val brushMode = ToggleGroup().apply { private val brushMode = ToggleGroup().apply {
brushVM.itemProperty.addListener { _, _, brush -> 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) { togglebutton(value = BrushMode.PAINTING_MODE, group = brushMode) {
graphic = FontIcon("fa-paint-brush") graphic = FontIcon("fa-paint-brush")

View File

@@ -5,6 +5,7 @@ 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.view.component.map.MapPane import com.bartlomiejpluta.base.editor.view.component.map.MapPane
import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM 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 com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
import javafx.beans.property.SimpleDoubleProperty import javafx.beans.property.SimpleDoubleProperty
import javafx.scene.input.MouseButton import javafx.scene.input.MouseButton
@@ -27,11 +28,14 @@ class MapView : View() {
xProperty().bind(zoomProperty) xProperty().bind(zoomProperty)
yProperty().bind(zoomProperty) yProperty().bind(zoomProperty)
} }
private val mapVM = find<GameMapVM>() private val mapVM = find<GameMapVM>()
private val brushVM = find<BrushVM>() private val brushVM = find<BrushVM>()
private val mapPane = MapPane(mapVM, brushVM) { undoRedoService.push(it, scope) } private val editorOptionsVM = find<EditorOptionsVM>()
private val mapPane = MapPane(mapVM, brushVM, editorOptionsVM) { undoRedoService.push(it, scope) }
init { init {
brushVM.item = mapVM.tileSet.baseBrush brushVM.item = mapVM.tileSet.baseBrush

View File

@@ -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>(EditorOptions()) {
val selectedLayerProperty = bind(EditorOptions::selectedLayer) as IntegerProperty
var selectedLayer by selectedLayerProperty
val showGridProperty = bind(EditorOptions::showGrid)
var showGrid by showGridProperty
}

View File

@@ -31,9 +31,6 @@ class GameMapVM : ItemViewModel<GameMap>() {
val heightProperty = bind(GameMap::heightProperty) val heightProperty = bind(GameMap::heightProperty)
val height by heightProperty val height by heightProperty
val selectedLayerProperty = SimpleIntegerProperty(0)
val selectedLayer by selectedLayerProperty
} }