[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.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)
}

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.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)
}
}
}

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.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<MouseEvent> {
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()
}

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.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<GameMapVM>()
private val editorOptionsVM = find<EditorOptionsVM>()
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()

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.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<GameMapVM>()
private val brushVM = find<BrushVM>()
private val editorOptionsVM = find<EditorOptionsVM>()
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")

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.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<GameMapVM>()
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 {
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 height by heightProperty
val selectedLayerProperty = SimpleIntegerProperty(0)
val selectedLayer by selectedLayerProperty
}