1 /*
2     Copyright © 2020,2022 Inochi2D Project
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4 */
5 module creator.actions.drawable;
6 import creator.core.actionstack;
7 import creator.actions;
8 import creator;
9 import inochi2d;
10 import std.format;
11 import i18n;
12 
13 /**
14     Action to add parameter to active puppet.
15 */
16 class DrawableChangeAction : GroupAction, LazyBoundAction {
17 private:
18     void copy(ref MeshData src, ref MeshData dst) {
19         dst.vertices = src.vertices.dup;
20         dst.uvs      = src.uvs.dup;
21         dst.indices  = src.indices.dup;
22         dst.origin   = src.origin;
23     }
24 public:
25     Drawable self;
26     string name;
27 
28     MeshData mesh;
29     bool     undoable;
30 
31     this(string name, Drawable self) {
32         super();
33         this.name = name;
34         this.self = self;
35         this.undoable = true;
36         copy(self.getMesh(), mesh);
37     }
38 
39     override
40     void updateNewState() {
41     }
42 
43     void addBinding(Parameter param, ParameterBinding binding) {
44         addAction(new ParameterBindingRemoveAction(param, binding));
45     }
46 
47     /**
48         Rollback
49     */
50     override
51     void rollback() {
52         if (undoable) {
53             MeshData tmpMesh;
54             copy(self.getMesh(), tmpMesh);
55             self.rebuffer(mesh);
56             mesh = tmpMesh;
57             undoable = false;
58         }
59         super.rollback();
60     }
61 
62     /**
63         Redo
64     */
65     override
66     void redo() {
67         if (!undoable) {
68             MeshData tmpMesh;
69             copy(self.getMesh(), tmpMesh);
70             self.rebuffer(mesh);
71             mesh = tmpMesh;
72             undoable = true;
73         }
74         super.redo();
75     }
76 
77     /**
78         Describe the action
79     */
80     override
81     string describe() {
82         return _("Changed drawable mesh of %s").format(self.name);
83     }
84 
85     /**
86         Describe the action
87     */
88     override
89     string describeUndo() {
90         return _("Drawable %s was changed").format(self.name);
91     }
92 
93     /**
94         Gets name of this action
95     */
96     override
97     string getName() {
98         return this.stringof;
99     }
100     
101     override bool merge(Action other) { return false; }
102     override bool canMerge(Action other) { return false; }
103 }