1# See: http://wiki.flightgear.org/MapStructure
2# Class things:
3var name = 'HOLD';
4var parents = [canvas.SymbolLayer.Controller];
5var __self__ = caller(0)[0];
6
7canvas.SymbolLayer.Controller.add(name, __self__);
8canvas.SymbolLayer.add(name, {
9    parents: [MultiSymbolLayer],
10    type: name, # Symbol type
11    df_controller: __self__, # controller to use by default -- this one
12});
13var new = func(layer) {
14    var m = {
15        parents: [__self__],
16        layer: layer,
17        #map: layer.map,
18        listeners: [],
19    };
20    #debug.dump(layer.parents);
21    layer.searcher._equals = func(a,b) a.id == b.id;
22    #append(m.listeners, setlistener(layer.options.fplan_active, func m.layer.update() ));
23    #m.addVisibilityListener();
24
25    return m;
26};
27
28var del = func() {
29    foreach (var l; me.listeners)
30        removelistener(l);
31};
32
33var searchCmd = func {
34    var results = [];
35    var node = props.globals.getNode(me.layer.options.hold_node);
36    var wp_id = node.getValue('wp');
37    if(wp_id == nil or wp_id == '' or wp_id == '---'){
38        return [];
39    }
40    var pointsNode = node.getNode('points');
41    var pointNode = nil;
42    var lat = nil;
43    var lon = nil;
44    if(pointsNode != nil)
45        pointNode = pointsNode.getNode('point', 0);
46    if (pointNode != nil){
47        lat = pointNode.getValue('lat');
48        lon = pointNode.getValue('lon');
49    }
50    if (pointNode != nil and lat != nil and lon != nil){
51        var r = node.getValue('crs');
52        var d = node.getValue('dist');
53        var t = node.getValue('turn');
54        var model = {
55            parents: [geo.Coord],
56            id: wp_id~r~d~t,
57            pos: pointNode,
58            type: 'pattern',
59            latlon: func(){
60                return [
61                    lat,
62                    lon
63                ];
64            },
65            radial: r,
66            dist: d,
67            turn: t,
68            equals: func(o){me.id == o.id and me.radial == o.radial}
69        };
70        append(results, model);
71    } else {
72        var wp_idx = node.getValue('wp_id');
73        var fp = flightplan();
74        var wp = fp.getWP(wp_idx);
75        #print("HOLD AT "~wp.wp_lat~", "~wp.wp_lon);
76        if(wp == nil or wp.id != wp_id)
77            return [];
78        var wp_lat = wp.wp_lat;
79        var wp_lon = wp.wp_lon;
80        var model = {
81            parents: [geo.Coord],
82            id: wp_id~'-'~wp_idx~'-inactive',
83            pos: nil,
84            type: 'hold_symbol',
85            latlon: func(){
86                return [
87                    wp_lat,
88                    wp_lon
89                ];
90            },
91            radial: node.getValue('crs'),
92            dist: node.getValue('dist'),
93            turn: node.getValue('turn'),
94            equals: func(o){me.id == o.id}
95        };
96        append(results, model);
97    }
98
99    return results;
100}
101