Debian Codegraph @

Nasal/canvas/api.nas

raw
1 #
2 # FlightGear canvas API
3 # Namespace: canvas
4 #
5 # Classes included:
6 # Transform
7 # Element
8 # Group
9 # Map
10 # Text
11 # Path
12 # Image
13 # Canvas
14 #
15 # see also gui.nas
16 var include_path = "Nasal/canvas/api/";
17
18 # log level for debug output
19 var _API_dbg_level = DEV_WARN;
20
21 io.include(include_path~"colors.nas");
22 io.include(include_path~"helpers.nas");
23 io.include(include_path~"transform.nas");
24 io.include(include_path~"element.nas");
25 io.include(include_path~"group.nas");
26 io.include(include_path~"map.nas");
27 io.include(include_path~"text.nas");
28 io.include(include_path~"path.nas");
29 io.include(include_path~"image.nas");
30 io.include(include_path~"svgcanvas.nas");
31
32 # Element factories used by #Group elements to create children
33 Group._element_factories = {
34 "group": Group.new,
35 "map": Map.new,
36 "text": Text.new,
37 "path": Path.new,
38 "image": Image.new
39 };
40
41 io.include(include_path~"canvas.nas");
42
43 # @param g Canvas ghost
44 var wrapCanvas = func(g) {
45 if (g != nil and g._impl == nil) {
46 g._impl = Canvas._new(g);
47 }
48 return g;
49 }
50
51 # Create a new canvas. Pass parameters as hash, eg:
52 #
53 # var my_canvas = canvas.new({
54 # "name": "PFD-Test",
55 # "size": [512, 512],
56 # "view": [768, 1024],
57 # "mipmapping": 1
58 # });
59 var new = func(vals)
60 {
61 var m = wrapCanvas(_newCanvasGhost());
62 m._node.setValues(vals);
63 return m;
64 };
65
66 # Get the first existing canvas with the given name
67 #
68 # @param name Name of the canvas
69 # @return #Canvas, if canvas with #name exists
70 # nil, otherwise
71 var get = func(arg)
72 {
73 if( isa(arg, props.Node) )
74 var node = arg;
75 else if (ishash(arg))
76 var node = props.Node.new(arg);
77 else {
78 die("canvas.get: Invalid argument.");
79 }
80
81 return wrapCanvas(_getCanvasGhost(node._g));
82 };
83
84 # subclass of Group representing the desktop
85 var Desktop = {
86 new: func(ghost) {
87 var obj = {
88 parents: [Desktop, Group.new(ghost)],
89 _sizeChangedCallbacks: [],
90 _xsizeNode: nil,
91 _ysizeNode: nil
92 };
93
94 obj._xsizeNode = props.globals.getNode("/sim/startup/xsize", 1);
95 obj._ysizeNode = props.globals.getNode("/sim/startup/ysize", 1);
96
97 setlistener(obj._xsizeNode, func { obj._callSizeChangedCallbacks(); }, 1);
98 setlistener(obj._ysizeNode, func { obj._callSizeChangedCallbacks(); }, 1);
99 return obj;
100 },
101
102 addResizedCallback: func(cb)
103 {
104 if (!isfunc(cb)) {
105 die("Cannot add callback for main window size changes: callback is not callable !");
106 }
107
108 append(me._sizeChangedCallbacks, cb);
109 cb(me._xsizeNode.getValue(), me._ysizeNode.getValue());
110 },
111
112 removeResizedCallback: func(cb)
113 {
114 remove(me._sizeChangedCallbacks, cb);
115 },
116
117 # @private
118 # @description Helper function that calls all callbacks in @m _sizeChangedCallbacks in the order they were added.
119 _callSizeChangedCallbacks: func {
120 var newXSize = me._xsizeNode.getValue();
121 var newYSize = me._ysizeNode.getValue();
122 foreach (var cb; me._sizeChangedCallbacks) {
123 cb(newXSize, newYSize);
124 }
125 }
126 };
127
128 var getDesktop = func()
129 {
130 return Desktop.new(_getDesktopGhost());
131 };
132
133
134 var unload = func
135 {
136 unloadTooltips();
137 unloadErrorNotification();
138 unloadGUI();
139 logprint(LOG_INFO, "Unloaded canvas Nasal module");
140 };