1 /*
2     Copyright © 2020, Inochi2D Project
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 /// Extensions to Inochi2D only used in Inochi Creator
8 module creator.ext;
9 public import creator.ext.nodes;
10 public import creator.ext.param;
11 import inochi2d;
12 
13 class ExPuppet : Puppet {
14 private:
15 
16 public:
17     this() { super(); }
18     this(Node root) { super(root); }
19 
20     /**
21         Returns a parameter by UUID
22     */
23     override
24     Parameter findParameter(uint uuid) {
25         foreach(ref parameter; parameters) {
26             if (auto group = cast(ExParameterGroup)parameter) {
27                 foreach(ref child; group.children) {
28                     if (child.uuid == uuid) return child;
29                 }
30             } else if (parameter.uuid == uuid) return parameter;
31         }
32         return null;
33     }
34     
35 
36     /**
37         Returns a parameter by UUID
38     */
39     Parameter findParameter(string name) {
40         foreach(ref parameter; parameters) {
41             if (auto group = cast(ExParameterGroup)parameter) {
42                 foreach(ref child; group.children) {
43                     if (child.name == name) return child;
44                 }
45             } else if (parameter.name == name) return parameter;
46         }
47         return null;
48     }
49     
50 
51     /**
52         Gets if a node is bound to ANY parameter.
53     */
54     override
55     bool getIsNodeBound(Node n) {
56         foreach(ref parameter; parameters) {
57             if (auto group = cast(ExParameterGroup)parameter) {
58                 foreach(ref child; group.children) {
59                     if (child.hasAnyBinding(n)) return true;
60                 }
61             } else if (parameter.hasAnyBinding(n)) return true;
62         }
63         return false;
64     }
65     
66 
67     /**
68         Removes a parameter from this puppet
69     */
70     override
71     void removeParameter(Parameter param) {
72         import std.algorithm.searching : countUntil;
73         import std.algorithm.mutation : remove;
74 
75         // First attempt to remove from root
76         ptrdiff_t idx = parameters.countUntil(param);
77         if (idx >= 0) {
78             parameters = parameters.remove(idx);
79             return;
80         }
81 
82         // Next attempt to remove from groups
83         foreach(ref parameter; parameters) {
84             if (auto group = cast(ExParameterGroup)parameter) {
85                 idx = group.children.countUntil(param);
86                 if (idx >= 0) {
87                     group.children = group.children.remove(idx);
88                     return;
89                 }
90             }
91         }
92     }
93 }
94 
95 void incInitExt() {
96     incInitExtNodes();
97     incRegisterExParameter();
98 }