Skip to content

Commit

Permalink
undo (ctrl z) is complete for main editor actions, #651
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Nov 14, 2021
1 parent 11e333e commit eb41e26
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 473,15 @@ class DialoguePane(graph: DialogueGraph = DialogueGraph()) : Pane() {
val startLayoutY = nodeView.properties["startLayoutY"] as Double

if (startLayoutX != nodeView.layoutX || startLayoutY != nodeView.layoutY) {
performUIAction(MoveNodeAction(nodeView, startLayoutX, startLayoutY, nodeView.layoutX, nodeView.layoutY))
performUIAction(MoveNodeAction(nodeView.node, this::getNodeView, startLayoutX, startLayoutY, nodeView.layoutX, nodeView.layoutY))
}
}

nodeView.cursor = Cursor.MOVE
nodeView.closeButton.cursor = Cursor.HAND

nodeView.closeButton.setOnMouseClicked {
graph.removeNode(nodeView.node)
performUIAction(RemoveNodeAction(graph, nodeView.node, nodeView.layoutX, nodeView.layoutY, this::getNodeView))
}

nodeView.outPoints.forEach { outPoint ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 28,27 @@ interface EditorAction {
// * remove node (and its incident edges)
// * add edge
// * remove edge
// * TODO: node text editing

class MoveNodeAction(
private val nodeView: NodeView,
private val node: DialogueNode,
private val newNodeViewGetter: (DialogueNode) -> NodeView,
private val startX: Double,
private val startY: Double,
private val endX: Double,
private val endY: Double
) : EditorAction {

override fun run() {
val nodeView = newNodeViewGetter(node)

nodeView.layoutX = endX
nodeView.layoutY = endY
}

override fun undo() {
val nodeView = newNodeViewGetter(node)

nodeView.layoutX = startX
nodeView.layoutY = startY
}
Expand All @@ -66,32 72,37 @@ class AddNodeAction(
}
}

//class RemoveNodeAction(
// private val graph: DialogueGraph,
// private val node: DialogueNode
//) : EditorAction {
//
// private val edges = arrayListOf<DialogueEdge>()
//
// override fun run() {
// graph.edges.filter { it.source === node || it.target === node }
// .forEach { edges = it }
//
// graph.removeNode(node)
// }
//
// override fun undo() {
// graph.addNode(node)
//
// edges.forEach {
// if (it.target === node) {
// graph.addEdge()
//
// graph.addChoiceEdge()
// }
// }
// }
//}
class RemoveNodeAction(
private val graph: DialogueGraph,
private val node: DialogueNode,

// where the node was during removal
private val layoutX: Double,
private val layoutY: Double,

// the newly created one (via undo) can be accessed through this
private val newNodeViewGetter: (DialogueNode) -> NodeView
) : EditorAction {

private val edges = arrayListOf<DialogueEdge>()

override fun run() {
graph.edges.filter { it.source === node || it.target === node }
.forEach { edges = it }

graph.removeNode(node)
}

override fun undo() {
graph.addNode(node)

newNodeViewGetter(node).relocate(layoutX, layoutY)

edges.forEach {
graph.addEdge(it)
}
}
}

class AddEdgeAction(
private val graph: DialogueGraph,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 64,7 @@ class MainUI : BorderPane() {
contextMenuFile.addItem("Exit") { getGameController().exit() }

val contextMenuEdit = FXGLContextMenu()
//contextMenuEdit.addItem("Undo (CTRL Z)") { undo() }
contextMenuEdit.addItem("Undo (CTRL Z)") { undo() }
//contextMenuEdit.addItem("Redo") { redo() }
//contextMenuEdit.addItem("Copy (CTRL C)") { }
//contextMenuEdit.addItem("Paste (CTRL V)") { }
Expand Down

0 comments on commit eb41e26

Please sign in to comment.