From d94e810977eb0bcd63c6271c06072ae779808c9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Thu, 4 Feb 2021 09:22:41 +0100 Subject: [PATCH] [Editor] Create basic undo/redo system --- .../base/editor/command/model/Command.kt | 5 ++ .../editor/command/model/SimpleCommand.kt | 21 +++++++ .../base/editor/command/model/Undoable.kt | 7 +++ .../command/service/DefaultUndoRedoService.kt | 61 +++++++++++++++++++ .../editor/command/service/UndoRedoService.kt | 15 +++++ 5 files changed, 109 insertions(+) create mode 100755 editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/Command.kt create mode 100755 editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/SimpleCommand.kt create mode 100755 editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/Undoable.kt create mode 100755 editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/DefaultUndoRedoService.kt create mode 100755 editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/UndoRedoService.kt diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/Command.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/Command.kt new file mode 100755 index 00000000..998f5033 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/Command.kt @@ -0,0 +1,5 @@ +package com.bartlomiejpluta.base.editor.command.model + +interface Command { + fun execute() +} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/SimpleCommand.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/SimpleCommand.kt new file mode 100755 index 00000000..633608e6 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/SimpleCommand.kt @@ -0,0 +1,21 @@ +package com.bartlomiejpluta.base.editor.command.model + +class SimpleCommand( + override val commandName: String, + private val formerValue: T, + private val value: T, + private val execute: (T) -> Unit +) : Undoable, Command { + + override fun undo() { + execute(formerValue) + } + + override fun redo() { + execute() + } + + override fun execute() { + execute(value) + } +} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/Undoable.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/Undoable.kt new file mode 100755 index 00000000..c9470f59 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/model/Undoable.kt @@ -0,0 +1,7 @@ +package com.bartlomiejpluta.base.editor.command.model + +interface Undoable { + fun undo() + fun redo() + val commandName: String +} \ No newline at end of file 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 new file mode 100755 index 00000000..4da0b061 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/DefaultUndoRedoService.kt @@ -0,0 +1,61 @@ +package com.bartlomiejpluta.base.editor.command.service + +import com.bartlomiejpluta.base.editor.command.model.Undoable +import org.springframework.stereotype.Service +import java.util.* + +@Service +class DefaultUndoRedoService : UndoRedoService { + private val undo: Deque = ArrayDeque() + private val redo: Deque = ArrayDeque() + + override var sizeMax = 30 + set(value) { + if(value >= 0) { + for(i in 0 until undo.size - value) { + undo.removeLast() + } + + field = value + } + } + + override fun push(undoable: Undoable) { + if(undo.size == sizeMax) { + undo.removeLast() + } + + undo.push(undoable) + redo.clear() + } + + override fun undo() { + if(undo.isNotEmpty()) { + undo.pop().let { + it.undo() + redo.push(it) + } + } + } + + override fun redo() { + if(redo.isNotEmpty()) { + redo.pop().let { + it.redo() + undo.push(it) + } + } + } + + override val lastUndoable: Undoable? + get() = undo.last + + override val lastRedoable: Undoable? + get() = redo.last + + override val undoCommandName: String + get() = undo.last?.commandName ?: "" + + override val redoCommandName: String + get() = redo.last?.commandName ?: "" +} \ No newline at end of file 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 new file mode 100755 index 00000000..cd896473 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/command/service/UndoRedoService.kt @@ -0,0 +1,15 @@ +package com.bartlomiejpluta.base.editor.command.service + +import com.bartlomiejpluta.base.editor.command.model.Undoable + +interface UndoRedoService { + fun push(undoable: Undoable) + fun undo() + fun redo() + + val lastUndoable: Undoable? + val lastRedoable: Undoable? + val undoCommandName: String + val redoCommandName: String + var sizeMax: Int +} \ No newline at end of file