1 /*
2     Copyright © 2020, Inochi2D Project
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 module creator.actions;
8 public import creator.actions.node;
9 public import creator.actions.parameter;
10 public import creator.actions.binding;
11 public import creator.actions.mesheditor;
12 public import creator.actions.drawable;
13 
14 /**
15     An undo/redo-able action
16 */
17 interface Action {
18     /**
19         Roll back the action that was done
20     */
21     void rollback();
22 
23     /**
24         Redo the action that was done
25     */
26     void redo();
27 
28     /**
29         Describes the action
30     */
31     string describe();
32 
33     /**
34         Describes the action
35     */
36     string describeUndo();
37 
38     /**
39         Gets the name of the action
40     */
41     string getName();
42 
43     /**
44         Merge action with other action (if possible)
45 
46         returns true if merge was successful
47     */
48     bool merge(Action other);
49 
50     /**
51         Gets whether this action can merge with an other
52     */
53     bool canMerge(Action other);
54 }
55 
56 /**
57    Special case of actions which captures the status of the target to implement undo/redo.
58    Action is instantiated before executing any change to the target. status is captured by
59    Action implementation. Later, updateState is called after change is applied to the target.
60    New status is captured by Action implementation then.
61 */
62 interface LazyBoundAction : Action {
63     /** 
64      * Confirm 'redo' state from the current status of the target.
65      */
66     void updateNewState();
67 }
68 
69 
70 
71 /**
72     Grouping several actions into one undo/redo action.
73 */
74 class GroupAction : Action {
75 public:
76     Action[] actions;
77 
78     this(Action[] actions = []) {
79         this.actions = actions;
80     }
81 
82     void addAction(Action action) {
83         this.actions ~= action;
84     }
85 
86     /**
87         Rollback
88     */
89     void rollback() {
90         foreach_reverse (action; actions) {
91             action.rollback();
92         }
93     }
94 
95     /**
96         Redo
97     */
98     void redo() {
99         foreach (action; actions) {
100             action.redo();
101         }
102     }
103 
104     /**
105         Describe the action
106     */
107     string describe() {
108         string result;
109         foreach (action; actions) {
110             result ~= action.describe();
111         }
112         return result;
113     }
114 
115     /**
116         Describe the action
117     */
118     string describeUndo() {
119         string result;
120         foreach_reverse (action; actions) {
121             result ~= action.describeUndo();
122         }
123         return result;
124     }
125 
126     /**
127         Gets name of this action
128     */
129     string getName() {
130         return this.stringof;
131     }
132     
133     bool merge(Action other) { return false; }
134     bool canMerge(Action other) { return false; }
135 }