Debian Codegraph @

Nasal/canvas/api/group.nas

raw
1 #-------------------------------------------------------------------------------
2 # canvas.Group
3 #-------------------------------------------------------------------------------
4 # Class for a group element on a canvas
5 #
6 var Group = {
7 new: func(ghost) {
8 var obj = {
9 parents: [Group, Element.new(ghost)],
10 };
11 return obj;
12 },
13
14 # Create a child of given type with specified id.
15 # type can be group, text
16 createChild: func(type, id = nil) {
17 var ghost = me._createChild(type, id);
18 var factory = me._getFactory(type);
19 if (factory == nil) {
20 return ghost;
21 }
22 return factory(ghost);
23 },
24
25 # Create multiple children of given type
26 createChildren: func(type, count) {
27 var factory = me._getFactory(type);
28 if (factory == nil) {
29 return [];
30 }
31 var nodes = props._addChildren(me._node._g, [type, count, 0, 0]);
32 for (var i = 0; i < count; i += 1) {
33 nodes[i] = factory(me._getChild(nodes[i]));
34 }
35 return nodes;
36 },
37
38 # Create a path child drawing a (rounded) rectangle
39 #
40 # @param x Position of left border
41 # @param y Position of top border
42 # @param w Width
43 # @param h Height
44 # @param cfg Optional settings (eg. {"border-top-radius": 5})
45 rect: func(x, y, w, h, cfg = nil) {
46 return me.createChild("path").rect(x, y, w, h, cfg);
47 },
48
49 # Get a vector of all child elements
50 getChildren: func() {
51 var children = [];
52 foreach(var c; me._node.getChildren()) {
53 if (me._isElementNode(c)) {
54 append(children, me._wrapElement(c));
55 }
56 }
57 return children;
58 },
59
60 # Recursively get all children of class specified by first param
61 getChildrenOfType: func(type, array = nil) {
62 var children = array;
63 if (children == nil) {
64 children = [];
65 }
66 var my_children = me.getChildren();
67 if (!isvec(type)) {
68 type = [type];
69 }
70 foreach(var c; my_children) {
71 foreach(var t; type) {
72 if (isa(c, t)) {
73 append(children, c);
74 }
75 }
76 if (isa(c, canvas.Group)) {
77 c.getChildrenOfType(type, children);
78 }
79 }
80 return children;
81 },
82
83 # Set color to children of type Path and Text. It is possible to optionally
84 # specify which types of children should be affected by passing a vector as
85 # the last agrument, ie. my_group.setColor(1,1,1,[Path]);
86 setColor: func() {
87 var color = arg;
88 var types = [Path, Text];
89 var arg_c = size(color);
90 if (arg_c > 1 and isvec(color[-1])) {
91 types = color[-1];
92 color = subvec(color, 0, arg_c - 1);
93 }
94 var children = me.getChildrenOfType(types);
95 if (isvec(color)) {
96 var first = color[0];
97 if (isvec(first)) {
98 color = first;
99 }
100 }
101 foreach(var c; children) {
102 c.setColor(color);
103 }
104 },
105
106 getWidth: func() {
107 var bb = me.getTightBoundingBox();
108 return bb[2] - bb[0];
109 },
110
111 getHeight: func() {
112 var bb = me.getTightBoundingBox();
113 return bb[3] - bb[1];
114 },
115
116 # Get first child with given id (breadth-first search)
117 #
118 # @note Use with care as it can take several miliseconds (for me eg. ~2ms).
119 # TODO check with new C++ implementation
120 getElementById: func(id) {
121 var ghost = me._getElementById(id);
122 if (ghost == nil) { return nil; }
123
124 var node = props.wrapNode(ghost._node_ghost);
125 var factory = me._getFactory(node.getName());
126 if (factory == nil) { return ghost; }
127 return factory(ghost);
128 },
129
130 # Remove all children
131 removeAllChildren: func() {
132 foreach(var type; keys(me._element_factories)) {
133 me._node.removeChildren(type, 0);
134 }
135 return me;
136 },
137
138 # private:
139 # element nodes have type NONE and valid element names (those in the factory
140 # list)
141 _isElementNode: func(el) {
142 return el.getType() == "NONE" and me._element_factories[el.getName()] != nil;
143 },
144
145 # Create element from existing node
146 _wrapElement: func(node) {
147 var factory = me._getFactory(node.getName());
148 return factory(me._getChild(node._g));
149 },
150
151 _getFactory: func(type) {
152 var factory = me._element_factories[type];
153 if (factory == nil) {
154 logprint(DEV_ALERT, "canvas.Group.createChild(): unknown type ("~type~")");
155 }
156 return factory;
157 }
158 };