From 48ef757f23678d4d83d68f43d406ec765c25a51d Mon Sep 17 00:00:00 2001 From: fgnm Date: Sat, 23 Sep 2023 11:19:36 +0200 Subject: [PATCH] Fix undo/redo for some commands --- .../editor/controller/BootstrapCommand.java | 2 +- .../commands/AddComponentToItemCommand.java | 12 +++++--- ...on.java => AddToLibraryActionCommand.java} | 4 +-- .../commands/CenterOriginPointCommand.java | 30 ++++++++++++------- .../commands/UpdatePolygonDataCommand.java | 6 ++-- .../UpdatePolygonVerticesCommand.java | 3 +- .../resource/DuplicateLibraryAction.java | 4 +-- .../view/ui/dialog/NodeEditorDialog.java | 4 +-- 8 files changed, 40 insertions(+), 25 deletions(-) rename src/main/java/games/rednblack/editor/controller/commands/{AddToLibraryAction.java => AddToLibraryActionCommand.java} (86%) diff --git a/src/main/java/games/rednblack/editor/controller/BootstrapCommand.java b/src/main/java/games/rednblack/editor/controller/BootstrapCommand.java index e9c4f62a..b64867ff 100644 --- a/src/main/java/games/rednblack/editor/controller/BootstrapCommand.java +++ b/src/main/java/games/rednblack/editor/controller/BootstrapCommand.java @@ -68,7 +68,7 @@ public class BootstrapCommand extends SimpleCommand { facade.registerCommand(MsgAPI.ACTION_REPLACE_SPRITE_ANIMATION_DATA, ReplaceSpriteAnimationCommand.class); facade.registerCommand(MsgAPI.ACTION_REPLACE_SPINE_ANIMATION_DATA, ReplaceSpineCommand.class); facade.registerCommand(MsgAPI.ACTION_ADD_TO_LIBRARY, AddToLibraryCommand.class); - facade.registerCommand(MsgAPI.ACTION_ADD_TO_LIBRARY_ACTION, AddToLibraryAction.class); + facade.registerCommand(MsgAPI.ACTION_ADD_TO_LIBRARY_ACTION, AddToLibraryActionCommand.class); facade.registerCommand(MsgAPI.ACTION_CHANGE_LIBRARY_ACTION, ChangeLibraryActionCommand.class); facade.registerCommand(MsgAPI.ACTION_CONVERT_TO_BUTTON, ConvertToButtonCommand.class); facade.registerCommand(MsgAPI.ACTION_GROUP_ITEMS, ConvertToCompositeCommand.class); diff --git a/src/main/java/games/rednblack/editor/controller/commands/AddComponentToItemCommand.java b/src/main/java/games/rednblack/editor/controller/commands/AddComponentToItemCommand.java index 84df254f..ba8eab8d 100644 --- a/src/main/java/games/rednblack/editor/controller/commands/AddComponentToItemCommand.java +++ b/src/main/java/games/rednblack/editor/controller/commands/AddComponentToItemCommand.java @@ -1,6 +1,7 @@ package games.rednblack.editor.controller.commands; import com.artemis.Component; +import games.rednblack.editor.utils.runtime.EntityUtils; import games.rednblack.editor.view.stage.Sandbox; import games.rednblack.h2d.common.MsgAPI; import games.rednblack.puremvc.Facade; @@ -11,14 +12,14 @@ import games.rednblack.puremvc.Facade; public class AddComponentToItemCommand extends EntityModifyRevertibleCommand { private static final String CLASS_NAME = "games.rednblack.editor.controller.commands.AddComponentToItemCommand"; - public static final String DONE = CLASS_NAME + "DONE"; + public static final String DONE = CLASS_NAME + ".DONE"; - private int entity; + private String entityId; private Class component; private void collectData() { Object[] payload = getNotification().getBody(); - entity = (int) payload[0]; + entityId = (String) payload[0]; component = (Class) payload[1]; } @@ -26,6 +27,7 @@ public class AddComponentToItemCommand extends EntityModifyRevertibleCommand { public void doAction() { collectData(); + int entity = EntityUtils.getByUniqueId(entityId); Component newComponent = Sandbox.getInstance().getEngine().edit(entity).create(component); sandbox.getEngine().inject(newComponent); @@ -35,6 +37,8 @@ public class AddComponentToItemCommand extends EntityModifyRevertibleCommand { @Override public void undoAction() { + int entity = EntityUtils.getByUniqueId(entityId); + Sandbox.getInstance().getEngine().edit(entity).remove(component); Sandbox.getInstance().getEngine().process(); @@ -44,7 +48,7 @@ public class AddComponentToItemCommand extends EntityModifyRevertibleCommand { public static Object[] payload(int entity, Class component) { Object[] payload = new Object[2]; - payload[0] = entity; + payload[0] = EntityUtils.getEntityId(entity); payload[1] = component; return payload; } diff --git a/src/main/java/games/rednblack/editor/controller/commands/AddToLibraryAction.java b/src/main/java/games/rednblack/editor/controller/commands/AddToLibraryActionCommand.java similarity index 86% rename from src/main/java/games/rednblack/editor/controller/commands/AddToLibraryAction.java rename to src/main/java/games/rednblack/editor/controller/commands/AddToLibraryActionCommand.java index 837454c9..d46503e3 100644 --- a/src/main/java/games/rednblack/editor/controller/commands/AddToLibraryAction.java +++ b/src/main/java/games/rednblack/editor/controller/commands/AddToLibraryActionCommand.java @@ -4,9 +4,9 @@ import games.rednblack.editor.renderer.data.GraphVO; import games.rednblack.h2d.common.MsgAPI; import games.rednblack.puremvc.Facade; -public class AddToLibraryAction extends NonRevertibleCommand { +public class AddToLibraryActionCommand extends NonRevertibleCommand { - public AddToLibraryAction() { + public AddToLibraryActionCommand() { setShowConfirmDialog(false); } diff --git a/src/main/java/games/rednblack/editor/controller/commands/CenterOriginPointCommand.java b/src/main/java/games/rednblack/editor/controller/commands/CenterOriginPointCommand.java index b125d116..90d60050 100644 --- a/src/main/java/games/rednblack/editor/controller/commands/CenterOriginPointCommand.java +++ b/src/main/java/games/rednblack/editor/controller/commands/CenterOriginPointCommand.java @@ -3,41 +3,49 @@ package games.rednblack.editor.controller.commands; import com.badlogic.gdx.math.Vector2; import games.rednblack.editor.renderer.components.DimensionsComponent; import games.rednblack.editor.renderer.components.TransformComponent; +import games.rednblack.editor.utils.runtime.EntityUtils; import games.rednblack.editor.utils.runtime.SandboxComponentRetriever; -import games.rednblack.h2d.common.command.TransformCommandBuilder; +import games.rednblack.h2d.common.MsgAPI; import games.rednblack.puremvc.Facade; public class CenterOriginPointCommand extends EntityModifyRevertibleCommand { - private int entity; + private String entityId; private final Vector2 backupOrigin = new Vector2(); @Override public void doAction() { - entity = notification.getBody(); + if (entityId == null) entityId = EntityUtils.getEntityId((int) notification.getBody()); + int entity = EntityUtils.getByUniqueId(entityId); TransformComponent transformComponent = SandboxComponentRetriever.get(entity, TransformComponent.class); backupOrigin.set(transformComponent.originX, transformComponent.originY); DimensionsComponent dimensionsComponent = SandboxComponentRetriever.get(entity, DimensionsComponent.class); - TransformCommandBuilder commandBuilder = new TransformCommandBuilder(); - commandBuilder.begin(entity, sandbox.getEngine()); float originX = dimensionsComponent.width * 0.5f; float originY = dimensionsComponent.height * 0.5f; if (dimensionsComponent.polygon != null) { originX = dimensionsComponent.polygon.getBoundingRectangle().width * 0.5f; originY = dimensionsComponent.polygon.getBoundingRectangle().height * 0.5f; } - commandBuilder.setOrigin(originX, originY); - commandBuilder.execute(Facade.getInstance()); + transformComponent.originX = originX; + transformComponent.originY = originY; + + EntityUtils.refreshComponents(entity); + Facade.getInstance().sendNotification(MsgAPI.ITEM_DATA_UPDATED, entity); } @Override public void undoAction() { - TransformCommandBuilder commandBuilder = new TransformCommandBuilder(); - commandBuilder.begin(entity, sandbox.getEngine()); - commandBuilder.setOrigin(backupOrigin.x, backupOrigin.y); - commandBuilder.execute(Facade.getInstance()); + int entity = EntityUtils.getByUniqueId(entityId); + + TransformComponent transformComponent = SandboxComponentRetriever.get(entity, TransformComponent.class); + + transformComponent.originX = backupOrigin.x; + transformComponent.originY = backupOrigin.y; + + EntityUtils.refreshComponents(entity); + Facade.getInstance().sendNotification(MsgAPI.ITEM_DATA_UPDATED, entity); } } diff --git a/src/main/java/games/rednblack/editor/controller/commands/UpdatePolygonDataCommand.java b/src/main/java/games/rednblack/editor/controller/commands/UpdatePolygonDataCommand.java index 9ea377f9..96861a62 100644 --- a/src/main/java/games/rednblack/editor/controller/commands/UpdatePolygonDataCommand.java +++ b/src/main/java/games/rednblack/editor/controller/commands/UpdatePolygonDataCommand.java @@ -17,12 +17,14 @@ public class UpdatePolygonDataCommand extends EntityModifyRevertibleCommand { @Override public void doAction() { Object[] payload = notification.getBody(); - int entity = (int) payload[0]; + if (entityId == null) + entityId = EntityUtils.getEntityId((int) payload[0]); + int entity = EntityUtils.getByUniqueId(entityId); boolean openPath = (boolean) payload[1]; PolygonShapeComponent polygonShapeComponent = SandboxComponentRetriever.get(entity, PolygonShapeComponent.class); - entityId = EntityUtils.getEntityId(entity); + openPathBackup = polygonShapeComponent.openEnded; polygonShapeComponent.openEnded = openPath; diff --git a/src/main/java/games/rednblack/editor/controller/commands/component/UpdatePolygonVerticesCommand.java b/src/main/java/games/rednblack/editor/controller/commands/component/UpdatePolygonVerticesCommand.java index c240312a..c2298efa 100644 --- a/src/main/java/games/rednblack/editor/controller/commands/component/UpdatePolygonVerticesCommand.java +++ b/src/main/java/games/rednblack/editor/controller/commands/component/UpdatePolygonVerticesCommand.java @@ -20,7 +20,8 @@ public class UpdatePolygonVerticesCommand extends EntityModifyRevertibleCommand private void collectData() { Object[] payload = getNotification().getBody(); - entityId = EntityUtils.getEntityId((int) payload[0]); + if (entityId == null) + entityId = EntityUtils.getEntityId((int) payload[0]); polygonizedDataFrom = (Vector2[][]) payload[1]; dataFrom = (Array) payload[2]; polygonizedDataTo = (Vector2[][]) payload[3]; diff --git a/src/main/java/games/rednblack/editor/controller/commands/resource/DuplicateLibraryAction.java b/src/main/java/games/rednblack/editor/controller/commands/resource/DuplicateLibraryAction.java index cea9acff..0f961385 100644 --- a/src/main/java/games/rednblack/editor/controller/commands/resource/DuplicateLibraryAction.java +++ b/src/main/java/games/rednblack/editor/controller/commands/resource/DuplicateLibraryAction.java @@ -3,7 +3,7 @@ package games.rednblack.editor.controller.commands.resource; import com.badlogic.gdx.utils.Json; import com.kotcrab.vis.ui.util.dialog.Dialogs; import com.kotcrab.vis.ui.util.dialog.InputDialogListener; -import games.rednblack.editor.controller.commands.AddToLibraryAction; +import games.rednblack.editor.controller.commands.AddToLibraryActionCommand; import games.rednblack.editor.controller.commands.NonRevertibleCommand; import games.rednblack.editor.proxy.ProjectManager; import games.rednblack.editor.renderer.data.GraphVO; @@ -40,7 +40,7 @@ public class DuplicateLibraryAction extends NonRevertibleCommand { Json json = HyperJson.getJson(); GraphVO duplicated = json.fromJson(GraphVO.class, json.toJson(actionToDuplicate)); - Object[] payload = AddToLibraryAction.getPayload(input, duplicated); + Object[] payload = AddToLibraryActionCommand.getPayload(input, duplicated); Facade.getInstance().sendNotification(MsgAPI.ACTION_ADD_TO_LIBRARY_ACTION, payload); } diff --git a/src/main/java/games/rednblack/editor/view/ui/dialog/NodeEditorDialog.java b/src/main/java/games/rednblack/editor/view/ui/dialog/NodeEditorDialog.java index 5a6c88ba..1c95b99e 100644 --- a/src/main/java/games/rednblack/editor/view/ui/dialog/NodeEditorDialog.java +++ b/src/main/java/games/rednblack/editor/view/ui/dialog/NodeEditorDialog.java @@ -8,7 +8,7 @@ import com.kotcrab.vis.ui.VisUI; import com.kotcrab.vis.ui.widget.MenuItem; import com.kotcrab.vis.ui.widget.PopupMenu; import com.kotcrab.vis.ui.widget.VisTextButton; -import games.rednblack.editor.controller.commands.AddToLibraryAction; +import games.rednblack.editor.controller.commands.AddToLibraryActionCommand; import games.rednblack.editor.graph.*; import games.rednblack.editor.graph.actions.ActionFieldType; import games.rednblack.editor.graph.actions.config.*; @@ -143,7 +143,7 @@ public class NodeEditorDialog extends H2DDialog implements Graph