Debian Codegraph @

Nasal/canvas/gui/Overlay.nas

raw
1 # SPDX-FileCopyrightText: (C) 2022 Frederic Croix <[email protected]>
2 # SPDX-License-Identifier: GPL-2.0-or-later
3
4 gui.Overlay = {
5 # Constructor
6 #
7 # @param size ([width, height])
8 new: func(size_, id = nil) {
9 var ghost = _newWindowGhost(id);
10 var m = {
11 parents: [gui.Overlay, PropertyElement, ghost],
12 _ghost: ghost,
13 _node: props.wrapNode(ghost._node_ghost),
14 _widgets: [],
15 _canvas: nil,
16 _focused: 1,
17 };
18
19 m.setSize(size_);
20 m._updateDecoration();
21
22 # arg = [child, listener_node, mode, is_child_event]
23 setlistener(m._node, func m._propCallback(arg[0], arg[2]), 0, 2);
24
25 return m;
26 },
27 # Destructor
28 del: func {
29 if (me["_canvas"] != nil) {
30 var placements = me._canvas._node.getChildren("placement");
31 # Do not remove canvas if other placements exist
32 if (size(placements) > 1) {
33 foreach (var p; placements) {
34 if (p.getValue("type") == "window" and p.getValue("id") == me.get("id")) {
35 p.remove();
36 }
37 }
38 } else {
39 me._canvas.del();
40 }
41 me._canvas = nil;
42 }
43 if (me._node != nil) {
44 me._node.remove();
45 me._node = nil;
46 }
47 },
48 # Create the canvas to be used for this Window
49 #
50 # @return The new canvas
51 createCanvas: func() {
52 var size = [
53 me.get("content-size[0]"),
54 me.get("content-size[1]")
55 ];
56
57 me._canvas = new({
58 size: [size[0], size[1]],
59 view: size,
60 placement: {
61 type: "window",
62 id: me.get("id")
63 },
64
65 # Standard alpha blending
66 "blend-source-rgb": "src-alpha",
67 "blend-destination-rgb": "one-minus-src-alpha",
68
69 # Just keep current alpha (TODO allow using rgb textures instead of rgba?)
70 "blend-source-alpha": "zero",
71 "blend-destination-alpha": "one"
72 });
73
74 me._canvas._focused_widget = nil;
75 me._canvas.data("focused", me._focused);
76
77 return me._canvas;
78 },
79 # Set an existing canvas to be used for this Window
80 setCanvas: func(canvas_) {
81 if (ghosttype(canvas_) != "Canvas") {
82 return debug.warn("Not a Canvas");
83 }
84
85 canvas_.addPlacement({type: "window", "id": me.get("id")});
86 me['_canvas'] = canvas_;
87
88 canvas_._focused_widget = nil;
89 canvas_.data("focused", me._focused);
90
91 return me;
92 },
93 # Get the displayed canvas
94 getCanvas: func(create = 0) {
95 if (me['_canvas'] == nil and create) {
96 me.createCanvas();
97 }
98
99 return me['_canvas'];
100 },
101 setLayout: func(l) {
102 if (me['_canvas'] == nil) {
103 me.createCanvas();
104 }
105
106 me._canvas.update(); # Ensure placement is applied
107 me._ghost.setLayout(l);
108 return me;
109 },
110 setPosition: func {
111 if (size(arg) == 1) {
112 var arg = arg[0];
113 }
114 var (x, y) = arg;
115
116 me.setInt("tf/t[0]", x);
117 me.setInt("tf/t[1]", y);
118 return me;
119 },
120 setSize: func {
121 if (size(arg) == 1) {
122 var arg = arg[0];
123 }
124 var (w, h) = arg;
125
126 me.set("content-size[0]", w);
127 me.set("content-size[1]", h);
128
129 if (me["_canvas"] == nil) {
130 print("canvas is nil");
131 return;
132 }
133
134 for (var i = 0; i < 2; i += 1) {
135 var size = me.get("content-size[" ~ i ~ "]");
136 me._canvas.set("size[" ~ i ~ "]", size);
137 me._canvas.set("view[" ~ i ~ "]", size);
138 }
139
140 return me;
141 },
142 getSize: func {
143 var w = me.get("content-size[0]");
144 var h = me.get("content-size[1]");
145 return [w,h];
146 },
147 # Raise to top of window stack
148 raise: func() {
149 # on writing the z-index the window always is moved to the top of all other
150 # windows with the same z-index.
151 me.setInt("z-index", me.get("z-index", gui.STACK_INDEX["always-on-top"]));
152 },
153 hide: func(parents = 0) {
154 me._ghost.hide();
155 },
156 show: func() {
157 me._ghost.show();
158 me.raise();
159 if (me._canvas != nil) {
160 me._canvas.update();
161 }
162 },
163 # Hide / show the window based on whether it's currently visible
164 toggle: func() {
165 if (me.isVisible()) {
166 me.hide();
167 } else {
168 me.show();
169 }
170 },
171 _onStateChange: func {
172 var event = canvas.CustomEvent.new("wm.focus-" ~ (me._focused ? "in" : "out"));
173
174 if (me.getCanvas() != nil) {
175 me.getCanvas().data("focused", me._focused).dispatchEvent(event);
176 }
177 },
178 #mode 0 = value changed, +-1 add/remove node
179 _propCallback: func(child, mode) {
180 if (!me._node.equals(child.getParent())) {
181 return;
182 }
183 var name = child.getName();
184
185 # support for CSS like position: absolute; with right and/or bottom margin
186 if (name == "right") {
187 me._handlePositionAbsolute(child, mode, name, 0);
188 } elsif (name == "bottom") {
189 me._handlePositionAbsolute(child, mode, name, 1);
190 }
191
192 if (mode == 0) {
193 if (name == "size") {
194 me._resizeDecoration();
195 }
196 }
197 },
198 _handlePositionAbsolute: func(child, mode, name, index) {
199 # mode
200 # -1 child removed
201 # 0 value changed
202 # 1 child added
203
204 if (mode == 0) {
205 me._updatePos(index, name);
206 } elsif (mode == 1) {
207 me["_listener_" ~ name] = [
208 setlistener(
209 "/sim/gui/canvas/size[" ~ index ~ "]",
210 func me._updatePos(index, name)
211 ),
212 setlistener(
213 me._node.getNode("content-size[" ~ index ~ "]"),
214 func me._updatePos(index, name)
215 )
216 ];
217 } elsif (mode == -1) {
218 for (var i = 0; i < 2; i += 1) {
219 removelistener(me["_listener_" ~ name][i]);
220 }
221 }
222 },
223 _updatePos: func(index, name) {
224 me.setInt(
225 "tf/t[" ~ index ~ "]",
226 getprop("/sim/gui/canvas/size[" ~ index ~ "]") - me.get(name) - me.get("content-size[" ~ index ~ "]")
227 );
228 },
229 getCanvasDecoration: func() {
230 return wrapCanvas(me._getCanvasDecoration());
231 },
232 _updateDecoration: func() {
233 me.setBool("update", 1);
234
235 #var canvas_deco = me.getCanvasDecoration();
236 #var group_deco = canvas_deco.getGroup("decoration");
237
238 me._resizeDecoration();
239 me._onStateChange();
240 },
241 _resizeDecoration: func() {
242 }
243 };
244
245