Skip to content

Commit

Permalink
Add working undo/redo for drag actions
Browse files Browse the repository at this point in the history
  • Loading branch information
meh2481 committed Dec 22, 2023
1 parent 53c67b9 commit 7af7fe0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/com/WooGLEFX/Engine/FXCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.WooGLEFX.Structures.SimpleStructures.LevelTab;
import com.WooGLEFX.Structures.SimpleStructures.MetaEditorAttribute;
import com.WooGLEFX.Structures.UserActions.AttributeChangeAction;
import com.WooGLEFX.Structures.UserActions.UserAction;
import com.WooGLEFX.Structures.UserActions.HierarchyDragAction;
import com.WorldOfGoo.Ball.Part;
import com.WorldOfGoo.Resrc.ResrcImage;

Expand Down Expand Up @@ -783,7 +783,7 @@ protected void updateItem(String item, boolean empty) {
if (!row.isEmpty()) {
int dropIndex = row.getIndex();
int curIndex = Main.getOldDropIndex();
if (dropIndex > Main.getOldDropIndex()) {
if (dropIndex > curIndex) {
// Dragged the item downwards; shift all of the items up
while (curIndex < dropIndex) {
hierarchy.getTreeItem(curIndex).setValue(hierarchy.getTreeItem(curIndex + 1).getValue());
Expand All @@ -801,7 +801,8 @@ protected void updateItem(String item, boolean empty) {

success = true;

// TODO: Create new undo action
Main.registerChange(new HierarchyDragAction(Main.getMoving(), Main.getOldDropIndex(), dropIndex));
Main.clearRedoActions();
}
}
event.setDropCompleted(success);
Expand Down Expand Up @@ -1012,6 +1013,7 @@ public void commitEdit(String s) {
// Push an attribute change to the undo buffer.
Main.registerChange(new AttributeChangeAction(Main.getSelected(), attribute.getName(), oldValue,
attribute.getValue()));
Main.clearRedoActions();

// If we have edited the name or ID of the object, change the object's "Name or
// ID" value.
Expand Down Expand Up @@ -1450,6 +1452,7 @@ public static ContextMenu possibleAttributeValues(TextFieldTreeTableCell<EditorA
setImageItem.setOnAction(event -> {
Main.registerChange(new AttributeChangeAction(Main.getSelected(), attribute.getName(),
attribute.getValue(), resource.getAttribute("id")));
Main.clearRedoActions();
attribute.setValue(resource.getAttribute("REALid"));
if (contextMenu.isFocused()) {
cell.commitEdit(attribute.getValue());
Expand All @@ -1471,6 +1474,7 @@ public static ContextMenu possibleAttributeValues(TextFieldTreeTableCell<EditorA
setImageItem.setOnAction(event -> {
Main.registerChange(new AttributeChangeAction(Main.getSelected(), attribute.getName(),
attribute.getValue(), ballFile.getName()));
Main.clearRedoActions();
attribute.setValue(ballFile.getName());
if (contextMenu.isFocused()) {
cell.commitEdit(attribute.getValue());
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/WooGLEFX/Engine/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.WooGLEFX.Structures.SimpleStructures.LevelTab;
import com.WooGLEFX.Structures.SimpleStructures.WoGAnimation;
import com.WooGLEFX.Structures.UserActions.AttributeChangeAction;
import com.WooGLEFX.Structures.UserActions.HierarchyDragAction;
import com.WooGLEFX.Structures.UserActions.ImportResourceAction;
import com.WooGLEFX.Structures.UserActions.ObjectCreationAction;
import com.WooGLEFX.Structures.UserActions.ObjectDestructionAction;
Expand Down Expand Up @@ -747,6 +748,26 @@ public static void undo() {
} else if (change instanceof ImportResourceAction) {
deleteResource(change.getObject().getAttribute("path"));
deleteItem(change.getObject(), false);
} else if (change instanceof HierarchyDragAction) {
HierarchyDragAction dragAction = (HierarchyDragAction) change;
int toIndex = dragAction.getToPosition();
int fromIndex = dragAction.getFromPosition();
// Shift all the items opposite of the direction the original item was dragged
if (toIndex > fromIndex) {
// Dragged the item downwards; shift all of the items down
while (toIndex > fromIndex) {
hierarchy.getTreeItem(toIndex).setValue(hierarchy.getTreeItem(toIndex - 1).getValue());
toIndex--;
}
} else {
// Dragged the item upwards; shift all of the items up
while (fromIndex > toIndex) {
hierarchy.getTreeItem(toIndex).setValue(hierarchy.getTreeItem(toIndex + 1).getValue());
toIndex++;
}
}
hierarchy.getTreeItem(dragAction.getFromPosition()).setValue(dragAction.getObject());
hierarchy.getSelectionModel().select(dragAction.getFromPosition());
}
hierarchy.refresh();
}
Expand All @@ -758,6 +779,10 @@ public static void undo() {

private static final Stack<UserAction[]> redoActions = new Stack<>();

public static void clearRedoActions() {
redoActions.clear();
}

public static void redo() {
if (redoActions.size() != 0) {
UserAction[] changes = redoActions.pop();
Expand All @@ -778,6 +803,25 @@ public static void redo() {
// TODO make this work with loopsounds instead of just music
importMusic(new File(((ImportResourceAction) change).getPath()), false);
}
} else if (change instanceof HierarchyDragAction) {
HierarchyDragAction dragAction = (HierarchyDragAction) change;
int toIndex = dragAction.getToPosition();
int fromIndex = dragAction.getFromPosition();
if (toIndex > fromIndex) {
// Dragged the item downwards; shift all of the items up
while (fromIndex < toIndex) {
hierarchy.getTreeItem(fromIndex).setValue(hierarchy.getTreeItem(fromIndex + 1).getValue());
fromIndex++;
}
} else {
// Dragged the item upwards; shift all of the items down
while (fromIndex > toIndex) {
hierarchy.getTreeItem(fromIndex).setValue(hierarchy.getTreeItem(fromIndex - 1).getValue());
fromIndex--;
}
}
hierarchy.getTreeItem(dragAction.getToPosition()).setValue(dragAction.getObject());
hierarchy.getSelectionModel().select(dragAction.getToPosition());
}
hierarchy.refresh();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.WooGLEFX.Structures.UserActions;

import com.WooGLEFX.Structures.EditorObject;

public class HierarchyDragAction extends UserAction {

private int toPosition;
private int fromPosition;

public int getToPosition() {
return toPosition;
}

public int getFromPosition() {
return fromPosition;
}

public void setToPosition(int toPosition) {
this.toPosition = toPosition;
}

public void setFromPosition(int fromPosition) {
this.fromPosition = fromPosition;
}

public HierarchyDragAction(EditorObject object, int fromPosition, int toPosition) {
super(object);
this.fromPosition = fromPosition;
this.toPosition = toPosition;
}

}

0 comments on commit 7af7fe0

Please sign in to comment.