Canvas MapStructure: Difference between revisions

Jump to navigation Jump to search
m
m (→‎Example: Tutorial Layer (TUT: http://forum.flightgear.org/viewtopic.php?f=71&t=27591#p259306)
Line 1,178: Line 1,178:
if (tutorial != nil) {
if (tutorial != nil) {
  allTargets = tutorial.getNode("targets");
  allTargets = tutorial.getNode("targets");
props.dump(allTargets);
}
}


props.dump(allTargets);
</syntaxhighlight>
</syntaxhighlight>


Line 1,205: Line 1,205:




Now, let's turn the whole thing into a searchCmd() function that we can us in our MapStructure layer:


<syntaxhighlight lang="nasal">
var getTutorialNode = func(name) {
var tutorialN = nil;
        foreach (var c; props.globals.getNode("/sim/tutorials").getChildren("tutorial")) {
                if (c.getNode("name").getValue() == name) {
                        tutorialN = c;
                        return tutorialN;
                }
        }
if (tutorialN == nil) {
                screen.log.write('Unable to find tutorial "' ~ name ~ '"');
                return nil;
        }
}
var getTargets = func(node) {
var results = [];
foreach (var t; node.getChildren()) {
                var lon = t.getNode("longitude-deg");
                var lat = t.getNode("latitude-deg");
                if (lon == nil or lat == nil)
                        die("target coords undefined");
                var target = geo.Coord.new().set_latlon(lat.getValue(), lon.getValue());
append(results,target);
}
return results;
}


var searchCmd=func() {
var tutorial = getTutorialNode("Taxiing");
var allTargets = nil;
if (tutorial != nil) {
allTargets = tutorial.getNode("targets");
#props.dump(allTargets);
return getTargets(allTargets);
}
} # searchCmd
debug.dump( searchCmd() );
</syntaxhighlight>


Next, we need to come up with some boilerplate code for creating a new MapStructure layer, and then populate the searchCmd() method using our two helper fnctions
Next, we need to come up with some boilerplate code for creating a new MapStructure layer, and then populate the searchCmd() method using our two helper fnctions

Navigation menu