1 module unecht.core.components.editor.commands;
2 
3 version (UEIncludeEditor)  : import core.thread : Fiber;
4 import unecht.core.components.sceneNode;
5 import unecht.core.components._editor;
6 import unecht.core.fibers;
7 
8 ///
9 abstract class UEEditorCommand
10 {
11 	///
12 	void execute();
13 	///
14 	void undo();
15 }
16 
17 ///
18 final class UECmdDelete : UEEditorCommand
19 {
20 private:
21 	import std.uuid : UUID;
22 
23 	string itemData;
24 	UUID parentId;
25 
26 	///
27 	public override void execute()
28 	{
29 		import unecht.core.entity : UEEntity;
30 		import unecht.core.serialization.serializer : UESerializer;
31 
32 		assert(EditorRootComponent.currentEntity);
33 
34 		UESerializer s;
35 		EditorRootComponent.currentEntity.sceneNode.serialize(s);
36 
37 		itemData = s.toString();
38 
39 		parentId = EditorRootComponent.currentEntity.sceneNode.parent.instanceId;
40 
41 		UEEntity.destroy(EditorRootComponent.currentEntity);
42 
43 		EditorRootComponent.selectEntity(null);
44 	}
45 
46 	///
47 	public override void undo()
48 	{
49 		import unecht.core.serialization.serializer : UEDeserializer;
50 		import unecht.ue : ue;
51 
52 		auto d = UEDeserializer(itemData);
53 		auto node = d.deserializeFirst!UESceneNode;
54 
55 		auto obj = cast(UESceneNode) ue.scene.findObject(parentId);
56 
57 		assert(obj);
58 
59 		node.parent = obj;
60 		node.onCreate();
61 
62 		EditorRootComponent.selectEntity(node.entity);
63 	}
64 }
65 
66 ///
67 struct UECommands
68 {
69 	static UEEditorCommand[] history;
70 
71 	///
72 	static void execute(UEEditorCommand cmd)
73 	{
74 		UEFibers.startFiber({ Fiber.yield(); cmd.execute(); history ~= cmd; });
75 	}
76 
77 	///
78 	static void undo()
79 	{
80 		if (history.length > 0)
81 		{
82 			UEFibers.startFiber({
83 				Fiber.yield();
84 				auto cmd = history[$ - 1];
85 				cmd.undo();
86 				history.length = history.length - 1;
87 			});
88 		}
89 	}
90 }