Debian Codegraph @

Nasal/canvas/gui.nas

raw
1 #
2 # FlightGear canvas gui
3 # Namespace: canvas
4 #
5 # Classes:
6 # WindowButton
7 # Window
8 #
9 # see also api.nas
10
11
12 var gui = {
13 widgets: {},
14 focused_window: nil,
15 open_popups: [],
16 region_highlight: nil,
17
18 # Window/dialog stacking order
19 STACK_INDEX: {
20 "default": 0,
21 "always-on-top": 1,
22 "tooltip": 2
23 }
24 };
25
26 var gui_dir = getprop("/sim/fg-root") ~ "/Nasal/canvas/gui/";
27 var loadGUIFile = func(file) io.load_nasal(gui_dir ~ file, "canvas");
28 var loadWidget = func(name) loadGUIFile("widgets/" ~ name ~ ".nas");
29 var loadDialog = func(name) loadGUIFile("dialogs/" ~ name ~ ".nas");
30
31 loadGUIFile("Config.nas");
32 loadGUIFile("Menu.nas");
33 loadGUIFile("MenuBar.nas");
34 loadGUIFile("Overlay.nas");
35 loadGUIFile("Popup.nas");
36 loadGUIFile("Style.nas");
37 loadGUIFile("Widget.nas");
38 loadGUIFile("styles/DefaultStyle.nas");
39
40 # widgets
41 loadWidget("Button");
42 loadWidget("CheckBox");
43 loadWidget("Dial");
44 loadWidget("Label");
45 loadWidget("LineEdit");
46 loadWidget("List");
47 loadWidget("MenuBar");
48 loadWidget("PropertyTree");
49 loadWidget("PropertyWidgets");
50 loadWidget("ScrollArea");
51 loadWidget("RadioButton");
52 loadWidget("Rule");
53 loadWidget("Slider");
54 loadWidget("Switch");
55 loadWidget("TabWidget");
56 loadWidget("ComboBox");
57
58 # standard dialogs
59 loadDialog("InputDialog");
60 loadDialog("MessageBox");
61 loadDialog("WidgetsFactoryDialog");
62 loadDialog("PropertyTreeBrowser");
63
64 var style = DefaultStyle.new("AmbianceClassic", "Humanity");
65 var WindowButton = {
66 new: func(parent, name)
67 {
68 var m = {
69 parents: [WindowButton, gui.widgets.Button.new(parent, nil, {"flat": 1})],
70 _name: name
71 };
72 m._focus_policy = m.NoFocus;
73 m._setView({_root: parent.createChild("image", "WindowButton-" ~ name)});
74 return m;
75 },
76 # protected:
77 _onStateChange: func
78 {
79 var file = style._dir_decoration ~ "/" ~ me._name;
80 var window_focus = me._windowFocus();
81 file ~= window_focus ? "_focused" : "_unfocused";
82
83 if( me._down )
84 file ~= "_pressed";
85 else if( me._hover )
86 file ~= "_prelight";
87 else if( window_focus )
88 file ~= "_normal";
89
90 me._view._root.set("src", file ~ ".png");
91 }
92 };
93
94 var Window = {
95 # Constructor
96 #
97 # @param size ([width, height])
98 new: func(size, type = nil, id = nil, allowfocus = 1, destroy_on_close = 1)
99 {
100 var ghost = _newWindowGhost(id);
101 var m = {
102 parents: [Window, PropertyElement, ghost],
103 _allowfocus: allowfocus,
104 _destroy_on_close: destroy_on_close,
105 _ghost: ghost,
106 _node: props.wrapNode(ghost._node_ghost),
107 _focused: 0,
108 _widgets: [],
109 _frame_width: 4,
110 _title_bar_height: 25,
111 _title: nil,
112 };
113
114 m.setInt("content-size[0]", size[0]);
115 m.setInt("content-size[1]", size[1]);
116 m.setDouble("aspect-ratio", size[0]/size[1]);
117 m.setBool("lock-aspect-ratio", 0);
118 m.centerOnScreen();
119
120 if (destroy_on_close) {
121 m.setFocus();
122 } else {
123 m._ghost.hide();
124 }
125
126 # arg = [child, listener_node, mode, is_child_event]
127 setlistener(m._node, func m._propCallback(arg[0], arg[2]), 0, 2);
128 if( type )
129 m.set("type", type);
130
131 return m;
132 },
133 # Destructor
134 del: func
135 {
136 if (me["_title"] != nil)
137 me._title.del();
138 me.clearFocus();
139
140 if( me["_canvas"] != nil )
141 {
142 var placements = me._canvas._node.getChildren("placement");
143 # Do not remove canvas if other placements exist
144 if( size(placements) > 1 ) {
145 foreach(var p; placements)
146 {
147 if( p.getValue("type") == "window"
148 and p.getValue("id") == me.get("id") )
149 p.remove();
150 }
151 }
152 else {
153 me._canvas.del();
154 }
155 me._canvas = nil;
156 }
157 if (me._node != nil) {
158 me._node.remove();
159 me._node = nil;
160 }
161 },
162 setTitle: func(title)
163 {
164 return me.set("title", title);
165 },
166 # Create the canvas to be used for this Window
167 #
168 # @return The new canvas
169 createCanvas: func()
170 {
171 var size = [
172 me.get("content-size[0]"),
173 me.get("content-size[1]")
174 ];
175
176 me._canvas = new({
177 size: [size[0], size[1]],
178 view: size,
179 placement: {
180 type: "window",
181 id: me.get("id")
182 },
183
184 # Standard alpha blending
185 "blend-source-rgb": "src-alpha",
186 "blend-destination-rgb": "one-minus-src-alpha",
187
188 # Just keep current alpha (TODO allow using rgb textures instead of rgba?)
189 "blend-source-alpha": "zero",
190 "blend-destination-alpha": "one"
191 });
192
193 me._canvas._focused_widget = nil;
194 me._canvas.data("focused", me._focused);
195 me._canvas.addEventListener("mousedown", func me.raise());
196
197 return me._canvas;
198 },
199 # Set an existing canvas to be used for this Window
200 setCanvas: func(canvas_)
201 {
202 if( ghosttype(canvas_) != "Canvas" )
203 return debug.warn("Not a Canvas");
204
205 canvas_.addPlacement({type: "window", "id": me.get("id")});
206 me['_canvas'] = canvas_;
207
208 canvas_._focused_widget = nil;
209 canvas_.data("focused", me._focused);
210
211 # prevent resizing if canvas is placed from somewhere else
212 me.onResize = nil;
213 return me;
214 },
215 # Get the displayed canvas
216 getCanvas: func(create = 0)
217 {
218 if( me['_canvas'] == nil and create )
219 me.createCanvas();
220
221 return me['_canvas'];
222 },
223 getCanvasDecoration: func()
224 {
225 return wrapCanvas(me._getCanvasDecoration());
226 },
227 setLayout: func(l)
228 {
229 if( me['_canvas'] == nil )
230 me.createCanvas();
231
232 me._canvas.update(); # Ensure placement is applied
233 me._ghost.setLayout(l);
234 return me;
235 },
236 #
237 setFocus: func
238 {
239 if( me._focused )
240 return me;
241
242 if( gui.focused_window != nil )
243 gui.focused_window.clearFocus();
244
245 if (!me._allowfocus) return me; # Make the window not take focus, so focus returns to FGFS
246
247 me._focused = 1;
248 # me.onFocusIn();
249 me._onStateChange();
250 gui.focused_window = me;
251 setInputFocus(me);
252 return me;
253 },
254 #
255 clearFocus: func
256 {
257 if( !me._focused )
258 return me;
259
260 me._focused = 0;
261 # me.onFocusOut();
262 me._onStateChange();
263 gui.focused_window = nil;
264 setInputFocus(nil);
265 return me;
266 },
267 setPosition: func(left=nil, top=nil, right=nil, bottom=nil)
268 {
269 if (typeof(left) == "vector") {
270 if (size(left) == 2) {
271 var (left, top) = left;
272 } elsif (size(left) == 4) {
273 var (left, top, right, bottom) = left;
274 } else {
275 die("canvas.Window.setPosition called with a vector as first argument, which is of unsupported length " ~ size(left));
276 }
277 }
278 if (typeof(top) == "vector") {
279 if (size(top) == 2) {
280 var (right, bottom) = top;
281 } else {
282 die("canvas.Window.setPosition called with a vector as second argument, which is of unsupported length " ~ size(top));
283 }
284 }
285
286 if (left != nil) {
287 me.setInt("tf/t[0]", left);
288 }
289 if (top != nil) {
290 me.setInt("tf/t[1]", top);
291 }
292 if (right != nil) {
293 me.setInt("right", right);
294 }
295 if (bottom != nil) {
296 me.setInt("bottom", bottom);
297 }
298 return me;
299 },
300 getPosition: func {
301 return [me.get("tf/t[0]"), me.get("tf/t[1]")];
302 },
303 getCSSPosition: func {
304 var top = me.get("tf/t[0]");
305 var left = me.get("tf/t[1]");
306 var right = me.get("right");
307 var bottom = me.get("bottom");
308 return [top, left, bottom, right];
309 },
310 centerOnScreen: func {
311 var desktopSize = [props.globals.getValue("/sim/gui/canvas/size[0]"), props.globals.getValue("/sim/gui/canvas/size[1]")];
312 var pos = [desktopSize[0] / 2 - me.getSize()[0] / 2 - 1, desktopSize[1] / 2 - me.getSize()[1] / 2 - 12];
313 me.setPosition(pos);
314 },
315 setSize: func
316 {
317 if( size(arg) == 1 )
318 var arg = arg[0];
319 var (w, h) = arg;
320
321 me.set("content-size[0]", w);
322 me.set("content-size[1]", h);
323
324 if( me.onResize != nil )
325 me.onResize();
326
327 return me;
328 },
329 getSize: func
330 {
331 var w = me.get("content-size[0]");
332 var h = me.get("content-size[1]");
333 return [w,h];
334 },
335 move: func
336 {
337 if( size(arg) == 1 )
338 var arg = arg[0];
339 var (x, y) = arg;
340
341 me.setInt("tf/t[0]", me.get("tf/t[0]", 10) + x);
342 me.setInt("tf/t[1]", me.get("tf/t[1]", 30) + y);
343 return me;
344 },
345 # Raise to top of window stack
346 raise: func()
347 {
348 # on writing the z-index the window always is moved to the top of all other
349 # windows with the same z-index.
350 me.setInt("z-index", me.get("z-index", gui.STACK_INDEX["default"]));
351
352 me.setFocus();
353
354 foreach(var p; gui.open_popups) {
355 p.hide();
356 }
357 },
358 hide: func()
359 {
360 me.clearFocus();
361 me._ghost.hide();
362 },
363 show: func()
364 {
365 me._ghost.show();
366 me.raise();
367 me._canvas.update();
368 },
369 # Hide / show the window based on whether it's currently visible
370 toggle: func()
371 {
372 if (me.isVisible()) {
373 me.hide();
374 } else {
375 me.show();
376 me.raise();
377 }
378 },
379 # function to be executed when the close button is pressed
380 onClose: func()
381 {
382 if (me._destroy_on_close)
383 {
384 me.del();
385 } else {
386 me.hide();
387 }
388 },
389 onResize: func()
390 {
391 if( me['_canvas'] == nil )
392 return;
393
394 for(var i = 0; i < 2; i += 1)
395 {
396 var size = me.get("content-size[" ~ i ~ "]");
397 me._canvas.set("size[" ~ i ~ "]", size);
398 me._canvas.set("view[" ~ i ~ "]", size);
399 }
400 },
401 lockAspectRatio: func (lock=1) {
402 me.setBool("lock-aspect-ratio", lock);
403 },
404 # protected:
405 _onStateChange: func
406 {
407 var event = canvas.CustomEvent.new("wm.focus-" ~ (me._focused ? "in" : "out"));
408
409 if( me._getCanvasDecoration() != nil )
410 {
411 # Stronger shadow for focused windows
412 me.getCanvasDecoration()
413 .set("image[1]/fill", me._focused ? "#000000" : "rgba(0,0,0,0.5)");
414
415 var suffix = me._focused ? "" : "-unfocused";
416 me._title_bar_bg.set("fill", style.getColor("title" ~ suffix));
417 me._frame.setStroke(style.getColor("title" ~ suffix));
418 me._title.setColor(style.getColor("title-text" ~ suffix));
419 me._top_line.setStroke(style.getColor("title-highlight" ~ suffix));
420
421 me.getCanvasDecoration()
422 .data("focused", me._focused)
423 .dispatchEvent(event);
424 }
425
426 if( me.getCanvas() != nil )
427 me.getCanvas()
428 .data("focused", me._focused)
429 .dispatchEvent(event);
430 },
431 # private:
432 #mode 0 = value changed, +-1 add/remove node
433 _propCallback: func(child, mode)
434 {
435 if( !me._node.equals(child.getParent()) )
436 return;
437 var name = child.getName();
438
439 # support for CSS like position: absolute; with right and/or bottom margin
440 if( name == "right" )
441 me._handlePositionAbsolute(child, mode, name, 0);
442 elsif( name == "bottom" )
443 me._handlePositionAbsolute(child, mode, name, 1);
444
445 if (mode == 0) {
446 if (name == "type") me._updateDecoration();
447 elsif (name.starts_with("resize-")) me._handleResize(child, name);
448 elsif (name == "size") me._resizeDecoration();
449 }
450 },
451 _handlePositionAbsolute: func(child, mode, name, index)
452 {
453 # mode
454 # -1 child removed
455 # 0 value changed
456 # 1 child added
457
458 if( mode == 0 )
459 me._updatePos(index, name);
460 else if( mode == 1 )
461 me["_listener_" ~ name] = [
462 setlistener
463 (
464 "/sim/gui/canvas/size[" ~ index ~ "]",
465 func me._updatePos(index, name)
466 ),
467 setlistener
468 (
469 me._node.getNode("content-size[" ~ index ~ "]"),
470 func me._updatePos(index, name)
471 )
472 ];
473 else if( mode == -1 )
474 for(var i = 0; i < 2; i += 1)
475 removelistener(me["_listener_" ~ name][i]);
476 },
477 _updatePos: func(index, name)
478 {
479 me.setInt
480 (
481 "tf/t[" ~ index ~ "]",
482 getprop("/sim/gui/canvas/size[" ~ index ~ "]")
483 - me.get(name)
484 - me.get("content-size[" ~ index ~ "]")
485 );
486 },
487 _handleResize: func(child, name)
488 {
489 var is_status = name == "resize-status";
490 if( !is_status and !me["_resize"] )
491 return;
492
493 var min_size = [75, 100];
494
495 var x = me.get("tf/t[0]");
496 var y = me.get("tf/t[1]");
497 var old_size = [me.get("size[0]"), me.get("size[1]")];
498 if (me.get("lock-aspect-ratio"))
499 {
500 var old_csize = [me.get("content-size[0]"), me.get("content-size[1]")];
501 var dx = old_size[0] - old_csize[0];
502 var dy = old_size[1] - old_csize[1];
503 var ar = me.get("aspect-ratio");
504
505 if (name == "resize-right")
506 me.set("resize-bottom", (me.get("resize-right") - dx) / ar + dy);
507 if (name == "resize-bottom")
508 me.set("resize-right", (me.get("resize-bottom") - dy)* ar + dx);
509
510 if (name == "resize-left")
511 me.set("resize-top", (me.get("resize-left"))/ ar );
512 if (name == "resize-top")
513 me.set("resize-left", (me.get("resize-top"))* ar );
514 }
515
516 var l = x + math.min(me.get("resize-left"), old_size[0] - min_size[0]);
517 var t = y + math.min(me.get("resize-top"), old_size[1] - min_size[1]);
518 var r = x + math.max(me.get("resize-right"), min_size[0]);
519 var b = y + math.max(me.get("resize-bottom"), min_size[1]);
520 if( is_status )
521 {
522 me._resize = child.getValue();
523
524 if( me._resize and gui.region_highlight == nil )
525 gui.region_highlight =
526 getDesktop().createChild("path", "highlight")
527 .set("stroke", "#ffa500")
528 .set("stroke-width", 2)
529 .set("fill", "rgba(255, 165, 0, 0.15)")
530 .set("z-index", 100);
531 else if( !me._resize and gui.region_highlight != nil )
532 {
533 gui.region_highlight.hide();
534 me.setPosition(l, t);
535 me.setSize
536 (
537 me.get("content-size[0]") + (r - l) - old_size[0],
538 me.get("content-size[1]") + (b - t) - old_size[1],
539 );
540 if( me.onResize != nil )
541 me.onResize();
542 return;
543 }
544 }
545 else if( !me["_resize"] )
546 return;
547
548 gui.region_highlight.reset()
549 .moveTo(l, t)
550 .horizTo(r)
551 .vertTo(b)
552 .horizTo(l)
553 .close()
554 .update()
555 .show();
556 },
557 _updateDecoration: func()
558 {
559 var border_radius = 9;
560 me.set("decoration-border", "25 1 1");
561 me.set("shadow-inset", int((1 - math.cos(45 * D2R)) * border_radius + 0.5));
562 me.set("shadow-radius", 5);
563 me.setBool("update", 1);
564
565 var canvas_deco = me.getCanvasDecoration();
566 canvas_deco.addEventListener("mousedown", func me.raise());
567 canvas_deco.set("blend-source-rgb", "src-alpha");
568 canvas_deco.set("blend-destination-rgb", "one-minus-src-alpha");
569 canvas_deco.set("blend-source-alpha", "one");
570 canvas_deco.set("blend-destination-alpha", "one");
571
572 var group_deco = canvas_deco.getGroup("decoration");
573 var title_bar = group_deco.createChild("group", "title_bar");
574 me._title_bar_bg = title_bar.createChild("path");
575 me._top_line = title_bar.createChild("path", "top-line");
576 me._frame = title_bar.createChild("path");
577 me._frame.set("fill", "none");
578 me._frame.set("stroke-width", me._frame_width);
579
580 # close icon
581 var x = 10;
582 var y = 3;
583 var w = 19;
584 var h = 19;
585
586 var button_close = WindowButton.new(title_bar, "close")
587 .move(x, y);
588 button_close.listen("clicked", func me.onClose());
589
590 # title
591 var title = me.get("title", "Canvas Dialog");
592 me._title = title_bar.createChild("text", "title")
593 .setText(title)
594 .setAlignment("left-center")
595 .set("character-size", 14)
596 .setFont("LiberationFonts/LiberationSans-Bold.ttf")
597 .setTranslation(int(x + 1.5 * w + 0.5), int(y + 0.5 * h + 0.5));
598
599 me._node.getNode("title", 1).alias(me._title._node.getPath() ~ "/text");
600 title_bar.addEventListener("drag", func(e) me.move(e.deltaX, e.deltaY));
601
602 me._resizeDecoration();
603 me._onStateChange();
604 },
605 _resizeDecoration: func()
606 {
607 if( me["_title_bar_bg"] == nil )
608 return;
609
610 var border_radius = 9;
611 me._title_bar_bg
612 .reset()
613 .rect( 0, 0,
614 me.get("size[0]"), me._title_bar_height,
615 {"border-top-radius": border_radius} );
616 me._frame
617 .reset()
618 .rect( 0, 0,
619 me.get("size[0]"), me.get("size[1]"),
620 {"border-top-radius": border_radius} );
621
622 me._top_line
623 .reset()
624 .moveTo(border_radius - 2, 2)
625 .lineTo(me.get("size[0]") - border_radius + 2, 2);
626 }
627 }; #Window
628
629 # Clear focus on click outside any window
630 getDesktop().addEventListener("mousedown", func {
631 if (gui.focused_window != nil) {
632 gui.focused_window.clearFocus();
633 }
634
635 foreach (var p; gui.open_popups) {
636 p.hide();
637 }
638 });
639
640 # Provide old 'Dialog' for backwards compatiblity (should be removed for 3.0)
641 var Dialog = {
642 new: func(size, type = nil, id = nil)
643 {
644 debug.warn("'canvas.Dialog' is deprecated! (use canvas.Window instead)");
645 return Window.new(size, type, id);
646 }
647 };
648
649 var unloadGUI = func() {
650 }