Debian Codegraph @

Nasal/canvas/map/VOR.symbol

raw
1 # See: http://wiki.flightgear.org/MapStructure
2 # Class things:
3 var name = 'VOR';
4 var parents = [DotSym];
5 var __self__ = caller(0)[0];
6 DotSym.makeinstance( name, __self__ );
7
8 SymbolLayer.get(name).df_style = { # style to use by default
9 line_width: 3,
10 range_line_width: 3,
11 radial_line_width: 3,
12 range_dash_array: [5, 15, 5, 15, 5],
13 radial_dash_array: [15, 5, 15, 5, 15],
14 scale_factor: 1,
15 active_color: [0, 1, 0],
16 inactive_color: [0, 0.6, 0.85],
17 };
18
19 var element_type = "group"; # we want a group, becomes "me.element"
20 var icon_vor = nil; # a handle to the cached icon
21 var range_vor = nil; # two elements that get drawn when needed
22 var radial_vor = nil; # if one is nil, the other has to be nil
23 var active = -1;
24
25 ##
26 # Callback for drawing a new VOR based on styling
27 #
28 # NOTE: callbacks that create symbols for caching MUST render them using a
29 # transparent background !!
30 #
31 var drawVOR = func(group) {
32 group.createChild("path")
33 .moveTo(-15,0)
34 .lineTo(-7.5,12.5)
35 .lineTo(7.5,12.5)
36 .lineTo(15,0)
37 .lineTo(7.5,-12.5)
38 .lineTo(-7.5,-12.5)
39 .close()
40 .setStrokeLineWidth(line_width)
41 .setColor(color)
42 .set("z-index",-2);
43 };
44
45 var cache = StyleableCacheable.new(
46 name:name, draw_func: drawVOR,
47 cache: SymbolCache32x32,
48 draw_mode: SymbolCache.DRAW_CENTERED,
49 relevant_keys: ["line_width", "color"],
50 );
51
52 ##
53 # TODO: make this a part of the framework, for use by other layers/symbols etc
54 #
55 var controller_check = func(layer, controller, arg...) {
56 var ret = call(compile("call(layer.controller."~controller~", arg, layer.controller)", "<test>"), arg, var err=[]);
57
58 if (size(err))
59 if (err[0] == "No such member: "~controller)
60 print("MapStructure Warning: Required controller not found: ", name, ":", controller);
61 else die(err[0]); # rethrow
62
63 return ret; # nil if not found
64 }
65
66 var draw = func {
67 var active = controller_check(me.layer, 'isActive', me.model);
68 if (active != me.active) {
69 if (me.icon_vor != nil) me.icon_vor.del();
70 # look up the correct symbol from the cache and render it into the group as a raster image
71 #me.icon_vor = icon_vor_cached[active or 0].render(me.element);
72 me.style.color = active ? me.style.active_color : me.style.inactive_color;
73 me.icon_vor = cache.render(me.element, me.style).setScale(me.style.scale_factor);
74
75 me.active = active; # update me.active flag
76 }
77 # Update (also handles non-cached stuff, such as text labels or animations)
78 # TODO: we can use a func generator to pre-create the callback/data structure for this
79 if (active) {
80 if (me.range_vor == nil) {
81 # initialize me.range and me.radial_vor
82 var rangeNm = me.map.getRange();
83 # print("VOR is tuned:", me.model.id);
84 var radius = (me.model.range_nm/rangeNm)*345;
85 me.range_vor = me.element.createChild("path")
86 .moveTo(-radius,0)
87 .arcSmallCW(radius,radius,0,2*radius,0)
88 .arcSmallCW(radius,radius,0,-2*radius,0)
89 .setStrokeLineWidth(me.style.range_line_width)
90 .setStrokeDashArray(me.style.range_dash_array)
91 .setColor(me.style.active_color)
92 .set("z-index",-2);
93
94 var course = me.map.controller.get_tuned_course(me.model.frequency/100);
95 me.radial_vor = me.element.createChild("path")
96 .moveTo(0,-radius)
97 .vert(2*radius)
98 .setStrokeLineWidth(me.style.radial_line_width)
99 .setStrokeDashArray(me.style.radial_dash_array)
100 .setColor(me.style.active_color)
101 .setRotation(course*D2R)
102 .set("z-index",-2);
103 }
104 me.range_vor.show();
105 me.radial_vor.show();
106 } else { # inactive station (not tuned)
107 if (me.range_vor != nil) {
108 me.range_vor.hide();
109 me.radial_vor.hide();
110 }
111 }
112 };
113