From e9fbb3e42c893647537b282e3d77f38b3f5b06f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Sun, 7 Feb 2021 23:09:56 +0100 Subject: [PATCH] [Editor] Enable clearing undo/redo stack on map settings update --- .../command/service/DefaultUndoRedoService.kt | 12 ++++++++++++ .../editor/command/service/UndoRedoService.kt | 2 ++ .../base/editor/map/canvas/MapCanvas.kt | 2 +- .../base/editor/map/model/layer/Layer.kt | 2 ++ .../base/editor/map/model/layer/TileLayer.kt | 4 ++++ .../base/editor/map/view/MapSettingsFragment.kt | 16 +++++++++++++--- 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/DefaultUndoRedoService.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/DefaultUndoRedoService.kt index daba2c6e..b879bc9e 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/DefaultUndoRedoService.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/DefaultUndoRedoService.kt @@ -80,6 +80,18 @@ class DefaultUndoRedoService : UndoRedoService { } } + override fun clear() { + log.debug("Clearing [undo] and [redo] stacks") + undo.clear() + redo.clear() + } + + override fun clear(context: UndoableContext) { + log.debug("Clearing [undo] and [redo] stacks (ctx: ${toHexString(context.hashCode())})") + undo.removeIf { it.second == context } + redo.removeIf { it.second == context } + } + override val lastUndoable: Undoable? get() = undo.first?.first diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/UndoRedoService.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/UndoRedoService.kt index a10f860d..bf7d50d4 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/UndoRedoService.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/UndoRedoService.kt @@ -7,10 +7,12 @@ interface UndoRedoService { fun push(undoable: Undoable) fun undo() fun redo() + fun clear() fun push(undoable: Undoable, context: UndoableContext) fun undo(context: UndoableContext) fun redo(context: UndoableContext) + fun clear(context: UndoableContext) val lastUndoable: Undoable? val lastRedoable: Undoable? diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/MapCanvas.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/MapCanvas.kt index aa8b05e9..34bed6fb 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/MapCanvas.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/MapCanvas.kt @@ -40,7 +40,7 @@ class MapCanvas(val map: GameMapVM, private val editorStateVM: EditorStateVM, pr } private fun renderUnderlyingLayers(gc: GraphicsContext) { - for(layer in map.layers.dropLast( map.layers.size - editorStateVM.selectedLayer)) { + for(layer in map.layers.dropLast(if(editorStateVM.selectedLayer < 0) 0 else map.layers.size - editorStateVM.selectedLayer)) { dispatchLayerRender(gc, layer) } } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/layer/Layer.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/layer/Layer.kt index 0eb1b5d7..09011cf0 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/layer/Layer.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/layer/Layer.kt @@ -7,4 +7,6 @@ interface Layer { val nameProperty: StringProperty fun resize(rows: Int, columns: Int) + + fun clone(): Layer } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/layer/TileLayer.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/layer/TileLayer.kt index b98740ab..a0341083 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/layer/TileLayer.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/layer/TileLayer.kt @@ -19,4 +19,8 @@ class TileLayer(name: String, rows: Int, columns: Int) : Layer { } } } + + override fun clone() = TileLayer(name, 0, 0).apply { + layer = this@TileLayer.layer + } } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/MapSettingsFragment.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/MapSettingsFragment.kt index 3aed7167..a34a4417 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/MapSettingsFragment.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/MapSettingsFragment.kt @@ -1,10 +1,15 @@ package com.bartlomiejpluta.base.editor.map.view +import com.bartlomiejpluta.base.editor.command.context.UndoableScope +import com.bartlomiejpluta.base.editor.command.service.UndoRedoService import com.bartlomiejpluta.base.editor.map.viewmodel.GameMapVM import org.kordamp.ikonli.javafx.FontIcon import tornadofx.* class MapSettingsFragment : Fragment("Map Settings") { + override val scope = super.scope as UndoableScope + private val undoRedoService: UndoRedoService by di() + private val mapVM = find() var result: Boolean = false @@ -49,6 +54,8 @@ class MapSettingsFragment : Fragment("Map Settings") { } } } + + label("Warning: Submitting the form will clear related undo/redo stacks!") } buttonbar { @@ -56,9 +63,12 @@ class MapSettingsFragment : Fragment("Map Settings") { shortcut("Enter") action { - mapVM.commit { - result = true - close() + if(mapVM.valid.value) { + mapVM.commit { + result = true + undoRedoService.clear(scope) + close() + } } } }