[Editor] Create basic undo/redo system
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package com.bartlomiejpluta.base.editor.command.model
|
||||
|
||||
interface Command {
|
||||
fun execute()
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.bartlomiejpluta.base.editor.command.model
|
||||
|
||||
class SimpleCommand<T>(
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.bartlomiejpluta.base.editor.command.model
|
||||
|
||||
interface Undoable {
|
||||
fun undo()
|
||||
fun redo()
|
||||
val commandName: String
|
||||
}
|
||||
@@ -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<Undoable> = ArrayDeque()
|
||||
private val redo: Deque<Undoable> = 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 ?: ""
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user