Debian Codegraph @

Nasal/canvas/MFD_Generic.nas

raw
1 # Generic Page switching cockpit display device.
2 # ---------------------------
3 # Richard Harrison: 2015-10-17 : [email protected]
4 # ---------------------------
5 # I'm calling this a PFD as in Programmable Function Display.
6 # ---------------------------
7 # documentation: see http://wiki.flightgear.org/Canvas_MFD_Framework
8 # See FGAddon/Aircraft/F-15/Nasal/MPCD/MPCD_main.nas for an example usage
9 # ---------------------------
10 # This is but a straightforwards wrapper to provide the core logic that page switching displays require.
11 # Examples of Page switching displays
12 # * MFD
13 # * PFD
14 # * FMCS
15 # * F-15 MPCD
16
17 #
18 # Menu Item. There is a list of these for each page changing button per display page
19 # Parameters:
20 # menu_id : page change event id for this menu item. e.g. button number
21 # title : Title Text (for display on the device)
22 # page : Instance of page usually returned from PFD.addPage
23 # callbackfn : Function to call when menu item is selected
24 # displayfn : Function to call when the menu item is displayed. Used to enable
25 # highlighting of menu items, for example.
26
27 var PFD_MenuItem =
28 {
29 new : func (menu_id, title, page, callbackfn=nil, displayfn=nil)
30 {
31 var obj = {parents : [PFD_MenuItem] };
32 obj.page = page;
33 obj.menu_id = menu_id;
34 obj.callbackfn = callbackfn;
35 obj.displayfn = displayfn;
36 obj.title = title;
37 return obj;
38 },
39 };
40
41 #
42 #
43 # Create a new PFD Page
44 # - related svg
45 # - Title: Page title
46 # - SVG element for the page
47 # - Device to attach the page to
48
49 var PFD_Page =
50 {
51 new : func (svg, title, layer_id, device)
52 {
53 var obj = {parents : [PFD_Page] };
54 obj.title = title;
55 obj.device = device;
56 obj.layer_id = layer_id;
57 obj.menus = [];
58 obj.svg = svg.getElementById(layer_id);
59 if(obj.svg == nil)
60 printf("PFD_Device: Error loading %s: svg layer %s ",title, layer_id);
61
62 return obj;
63 },
64
65 #
66 # Makes a page visible.
67 # It is the responsibility of the caller to manage the visibility of pages - i.e. to
68 # make a page that is currenty visible not visible before making a new page visible,
69 # however more than one page could be visible - but only one set of menu buttons can be active
70 # so if two pages are visible (e.g. an overlay) then when the overlay removed it would be necessary
71 # to call setVisible on the base page to ensure that the menus are setup
72 setVisible : func(vis)
73 {
74 if(me.svg != nil)
75 me.svg.setVisible(vis);
76
77 if (vis)
78 me.ondisplay();
79 else
80 me.offdisplay();
81 },
82
83 # Standard callback for buttons, causing the appropriate page to be displayed
84 std_callbackfn : func (device, me, mi)
85 {
86 device.selectPage(mi.page);
87 },
88
89 # Standard display function for buttons, displaying the text and making visible
90 std_displayfn : func(svg_element, menuitem)
91 {
92 svg_element.setText(menuitem.title);
93 svg_element.setVisible(1);
94 #me.buttons[mi.menu_id].setText(mi.title);
95 #me.buttons[mi.menu_id].setVisible(1);
96 },
97
98 #
99 # Perform action when button is pushed
100 notifyButton : func(button_id)
101 { foreach(var mi; me.menus)
102 {
103 if (mi.menu_id == button_id)
104 {
105 if (mi.callbackfn != nil) mi.callbackfn(me.device, me, mi);
106 break;
107 }
108 }
109 },
110
111 #
112 # Add an item to a menu
113 # Params:
114 # menu button id (that is set in controls/PFD/button-pressed by the model)
115 # title of the menu for the label
116 # page that will be selected when pressed
117 #
118 # The corresponding menu for the selected page will automatically be loaded
119 addMenuItem : func(menu_id, title, page, callbackfn=nil, displayfn=nil)
120 {
121 if (callbackfn == nil) callbackfn = me.std_callbackfn;
122 if (displayfn == nil) displayfn = me.std_displayfn;
123 var nm = PFD_MenuItem.new(menu_id, title, page, callbackfn, displayfn);
124 append(me.menus, nm);
125 return nm;
126 },
127
128 #
129 # Clear all items from the menu. Use-case is where they may be a hierarchy
130 # of menus within the same page.
131 #
132 clearMenu : func()
133 {
134 me.menus = [];
135 },
136
137 # base method for update; this can be overridden per page instance to provide update of the
138 # elements on display (e.g. to display updated properties)
139 update : func(notification=nil)
140 {
141 },
142
143 #
144 # notify the page that it is being displayed. use to load any static framework or perform one
145 # time initialisation
146 ondisplay : func
147 {
148 },
149
150 #
151 # notify the page that it is going off display; use to clean up any created elements or perform
152 # any other required functions
153 offdisplay : func
154 {
155 },
156 };
157
158
159 #
160 # Container device for pages.
161 var PFD_Device =
162 {
163 # - svg is the page elements from the svg.
164 # - num_menu_buttons is the Number of menu buttons; starting from the bottom left then right, then top, then left.
165 # - button prefix (e.g MI_) is the prefix of the labels in the SVG for the menu boxes.
166 # - _canvas is the canvas group.
167 # - designation (optional) is used for Emesary designation
168 #NOTE:
169 # This does not actually create the canvas elements, or parse the SVG, that would typically be done in
170 # a higher level class that contains an instance of this class.
171 # see: http://wiki.flightgear.org/Canvas_MFD_Framework
172 new : func(svg, num_menu_buttons, button_prefix, _canvas, designation="MFD")
173 {
174 var obj = {parents : [PFD_Device] };
175 obj.svg = svg;
176 obj.canvas = _canvas;
177 obj.current_page = nil;
178 obj.pages = [];
179 obj.page_index = {};
180 obj.buttons = setsize([], num_menu_buttons);
181 obj.transmitter = nil;
182
183 # change after creation if required
184 obj.device_id = 1;
185 obj.designation = designation;
186
187 for(var idx = 0; idx < num_menu_buttons; idx += 1)
188 {
189 var label_name = sprintf(button_prefix~"%d",idx);
190 var msvg = obj.svg.getElementById(label_name);
191 if (msvg == nil)
192 printf("PFD_Device: Failed to load %s",label_name);
193 else
194 {
195 obj.buttons[idx] = msvg;
196 obj.buttons[idx].setText(sprintf("M%d",idx));
197 }
198 }
199 obj.Recipient = nil;
200 return obj;
201 },
202 #
203 # instead of using the direct call method this allows the use of Emesary (via a specified or default global transmitter)
204 # example to notify that a softkey has been used. The "1" in the line below is the device ID
205 # var notification = notifications.PFDEventNotification.new(me.designation, me.DeviceId, notifications.PFDEventNotification.SoftKeyPushed, me.mpcd_button_pushed);
206 # emesary.GlobalTransmitter.NotifyAll(notification);
207 # - currently supported is
208 # 1. setting menu text directly (after page has been loaded)
209 # notifications.PFDEventNotification.new(me.designation, 1, notifications.PFDEventNotification.ChangeMenuText, [{ Id: 1, Text: "NNN"}]);
210 # 2. SoftKey selection.
211 #
212 # the device ID must match this device ID (to allow for multiple devices).
213 RegisterWithEmesary : func(transmitter = nil){
214 if (transmitter == nil)
215 transmitter = emesary.GlobalTransmitter;
216
217 if (me.Recipient == nil){
218 me.Recipient = emesary.Recipient.new("PFD_"~me.designation);
219 var pfd_obj = me;
220 me.Recipient.Receive = func(notification)
221 {
222 if (notification.NotificationType == notifications.PFDEventNotification.DefaultType and
223 notification.Device_Id == pfd_obj.device_id) {
224 if (notification.Event_Id == notifications.PFDEventNotification.SoftKeyPushed
225 and notification.EventParameter != nil)
226 {
227 #printf("Button pressed " ~ notification.EventParameter);
228 pfd_obj.notifyButton(notification.EventParameter);
229 }
230 else if (notification.Event_Id == notifications.PFDEventNotification.ChangeMenuText
231 and notification.EventParameter != nil)
232 {
233 foreach(var eventMenu; notification.EventParameter) {
234 #printf("Menu Text changed : " ~ eventMenu.Text);
235 foreach (var mi ; pfd_obj.current_page.menus) {
236 if (pfd_obj.buttons[eventMenu.Id] != nil) {
237 pfd_obj.buttons[eventMenu.Id].setText(eventMenu.Text);
238 }
239 else
240 printf("PFD_device: Menu for button not found. Menu ID '%s'",mi.menu_id);
241 }
242 }
243 }
244 return emesary.Transmitter.ReceiptStatus_OK;
245 }
246 return emesary.Transmitter.ReceiptStatus_NotProcessed;
247 };
248 transmitter.Register(me.Recipient);
249 me.transmitter = transmitter;
250 }
251 },
252 DeRegisterWithEmesary : func(transmitter = nil){
253 # remove registration from transmitter; but keep the recipient once it is created.
254 if (me.transmitter != nil)
255 me.transmitter.DeRegister(me.Recipient);
256 me.transmitter = nil;
257 },
258 #
259 # called when a button is pushed - connecting the property to this method is implemented in the outer class
260 notifyButton : func(button_id)
261 {
262 #
263 # by convention the buttons we have are 0 based; however externally 0 is used
264 # to indicate no button pushed.
265 if (button_id > 0)
266 {
267 button_id = button_id - 1;
268 if (me.current_page != nil)
269 {
270 me.current_page.notifyButton(button_id);
271 }
272 else
273 printf("PFD_Device: Could not locate page for button ",button_id);
274 }
275 },
276 #
277 #
278 # add a page to the device.
279 # - page title.
280 # - svg element id
281 addPage : func(title, layer_id)
282 {
283 var np = PFD_Page.new(me.svg, title, layer_id, me);
284 append(me.pages, np);
285 me.page_index[layer_id] = np;
286 np.setVisible(0);
287 return np;
288 },
289 #
290 # Get a named page
291 #
292 getPage : func(title)
293 {
294 foreach(var p; me.pages) {
295 if (p.title == title) return p;
296 }
297
298 return nil;
299 },
300 #
301 # manage the update of the currently selected page
302 update : func(notification=nil)
303 {
304 if (me.current_page != nil)
305 me.current_page.update(notification);
306 },
307 #
308 # Change to display the selected page.
309 # - the page object method controls the visibility
310 selectPage : func(p)
311 {
312 if (me.current_page == p) return;
313
314 if (me.current_page != nil)
315 me.current_page.setVisible(0);
316 if (me.buttons != nil)
317 {
318 foreach(var mb ; me.buttons)
319 if (mb != nil)
320 mb.setVisible(0);
321
322 foreach(var mi ; p.menus)
323 {
324 if (me.buttons[mi.menu_id] != nil)
325 {
326 mi.displayfn(me.buttons[mi.menu_id], mi);
327 }
328 else
329 printf("PFD_device: Menu for button not found. Menu ID '%s'",mi.menu_id);
330 }
331 }
332 p.setVisible(1);
333 me.current_page = p;
334 },
335
336 # Return the current selected page.
337 getCurrentPage : func()
338 {
339 return me.current_page;
340 },
341
342 #
343 # ensure that the menus are display correctly for the current page.
344 updateMenus : func
345 {
346 foreach(var mb ; me.buttons)
347 if (mb != nil)
348 mb.setVisible(0);
349
350 if (me.current_page == nil) return;
351
352 foreach(var mi ; me.current_page.menus)
353 {
354 if (me.buttons[mi.menu_id] != nil)
355 {
356 mi.displayfn(me.buttons[mi.menu_id], mi);
357 }
358 else
359 printf("No corresponding item '%s'",mi.menu_id);
360 }
361 },
362 };
363
364 var PFD_NavDisplay =
365 {
366 #
367 # Instantiate parameters:
368 # 1. pfd_device (instance of PFD_Device)
369 # 2. instrument display ident (e.g. mfd-map, or mfd-map-left mfd-map-right for multiple displays)
370 # (this is used to map to the property tree)
371 # 3. layer_id: main layer in the SVG
372 # 4. nd_group_ident : group (usually within the main layer) to place the NavDisplay
373 # 5. switches - used to connect the property tree to the nav display. see the canvas nav display
374 # documentation
375 new : func (pfd_device, title, instrument_ident, layer_id, nd_group_ident, switches=nil, map_style="Boeing")
376 {
377 var obj = pfd_device.addPage(title, layer_id);
378
379 # if no switches given then use a default set.
380 if (switches != nil)
381 obj.switches = switches;
382 else
383 obj.switches = {
384 'toggle_range': { path: '/inputs/range-nm', value: 50, type: 'INT' },
385 'toggle_weather': { path: '/inputs/wxr', value: 0, type: 'BOOL' },
386 'toggle_airports': { path: '/inputs/arpt', value: 1, type: 'BOOL' },
387 'toggle_stations': { path: '/inputs/sta', value: 1, type: 'BOOL' },
388 'toggle_waypoints': { path: '/inputs/wpt', value: 1, type: 'BOOL' },
389 'toggle_position': { path: '/inputs/pos', value: 0, type: 'BOOL' },
390 'toggle_data': { path: '/inputs/data', value: 0, type: 'BOOL' },
391 'toggle_terrain': { path: '/inputs/terr', value: 0, type: 'BOOL' },
392 'toggle_traffic': { path: '/inputs/tfc', value: 1, type: 'BOOL' },
393 'toggle_centered': { path: '/inputs/nd-centered', value: 1, type: 'BOOL' },
394 'toggle_lh_vor_adf': { path: '/inputs/lh-vor-adf', value: 1, type: 'INT' },
395 'toggle_rh_vor_adf': { path: '/inputs/rh-vor-adf', value: 1, type: 'INT' },
396 'toggle_display_mode': { path: '/mfd/display-mode', value: 'MAP', type: 'STRING' },
397 'toggle_display_type': { path: '/mfd/display-type', value: 'LCD', type: 'STRING' },
398 'toggle_true_north': { path: '/mfd/true-north', value: 0, type: 'BOOL' },
399 'toggle_rangearc': { path: '/mfd/rangearc', value: 0, type: 'BOOL' },
400 'toggle_track_heading': { path: '/hdg-trk-selected', value: 1, type: 'BOOL' },
401 };
402
403 obj.nd_initialised = 0;
404 obj.nd_placeholder_ident = nd_group_ident;
405 obj.nd_ident = instrument_ident;
406 obj.pfd_device = pfd_device;
407
408 obj.nd_init = func
409 {
410 me.ND = canvas.NavDisplay;
411 if (!me.nd_initialised)
412 {
413 me.nd_initialised = 1;
414
415 me.NDCpt = me.ND.new("instrumentation/"~me.nd_ident, me.switches,map_style);
416
417 me.group = me.pfd_device.svg.getElementById(me.nd_placeholder_ident);
418 me.group.setScale(0.39,0.45);
419 me.group.setTranslation(45,0);
420 me.NDCpt.newMFD(me.group, pfd_device.canvas);
421 }
422 me.NDCpt.update();
423 };
424 #
425 # Method overrides
426 #-----------------------------------------------
427 # Called when the page goes on display - need to delay initialization of the NavDisplay until later (it fails
428 # if done too early).
429 # NOTE: This causes a display "wobble" the first time on display as resizing happens. I've seen similar things
430 # happen on real avionics (when switched on) so it's not necessarily unrealistic -)
431 obj.ondisplay = func
432 {
433 if (!me.nd_initialised)
434 me.nd_init();
435 #2018.2 - manage the timer so that the nav display is only updated when visibile
436 me.NDCpt.onDisplay();
437 };
438 obj.offdisplay = func
439 {
440 #2018.2 - manage the timer so that the nav display is only updated when visibile
441 if (me.nd_initialised)
442 me.NDCpt.offDisplay();
443 };
444 #
445 # most updates performed by the canvas nav display directly.
446 obj.update = func
447 {
448 };
449 return obj;
450 },
451 };