Nasal library: Difference between revisions

Jump to navigation Jump to search
Line 148: Line 148:
|param1text = Optional integer specifying the stack level to return a result from.  Defaults to 1 (i.e. the caller of the currently executing function).
|param1text = Optional integer specifying the stack level to return a result from.  Defaults to 1 (i.e. the caller of the currently executing function).
|example1 =
|example1 =
var myFunction = func(a, b){
var myFunction = func(a, b) {
     debug.dump(caller(0)[0]); # prints a hash of local variables, including arguments a and b
     debug.dump(caller(0)[0]); # prints a hash of local variables, including arguments a and b
     return 2 * 2;
     return 2 * 2;
Line 155: Line 155:
print("2 x 2 = ", myFunction(2, 2));
print("2 x 2 = ", myFunction(2, 2));
|example2 =
|example2 =
var get_arg_value = func(){
var get_arg_value = func() {
     print("Argument to myFunc = ", caller(1)[0]['a']); # print the value of myFunc's single argument, using caller()
     print("Argument to myFunc = ", caller(1)[0]['a']); # print the value of myFunc's single argument, using caller()
};
};


var myFunc = func(a){
var myFunc = func(a) {
     get_arg_value();
     get_arg_value();
};
};


myFunc(3);
myFunc(3);
|example3text = This is a real example taken from {{fgdata file|Nasal/canvas/MapStructure.nas}}.  Function <code>r()</code> (above the TODOs) returns a hash with the key/value pairs as per its arguments. For example, something like this is returned: <code>{ name: "<name>", vis: 1, zindex: nil }</code>.
|example3text = This is a real example taken from {{fgdata file|Nasal/canvas/MapStructure.nas}}.  Function <code>r()</code> (above the TODOs) returns a hash with the key/value pairs as per its arguments. For example, something like this is returned: <code>{ name: "<name>", vis: true, zindex: nil }</code>.
|example3 =
|example3 =
var MapStructure_selfTest = func() {
var MapStructure_selfTest = func() {
var temp = {};
    var temp = {};
temp.dlg = canvas.Window.new([600,400],"dialog");
    temp.dlg = canvas.Window.new([600, 400], "dialog");
temp.canvas = temp.dlg.createCanvas().setColorBackground(1,1,1,0.5);
    temp.canvas = temp.dlg.createCanvas().setColorBackground(1, 1, 1, 0.5);
temp.root = temp.canvas.createGroup();
    temp.root = temp.canvas.createGroup();
var TestMap = temp.root.createChild("map");
 
TestMap.setController("Aircraft position");
    var testMap = temp.root.createChild("map");
TestMap.setRange(25); # TODO: implement zooming/panning via mouse/wheel here, for lack of buttons :-/
    testMap.setController("Aircraft position");
TestMap.setTranslation(
    testMap.setRange(25); # TODO: implement zooming/panning via mouse/wheel here, for lack of buttons :-/
temp.canvas.get("view[0]")/2,
    testMap.setTranslation(
temp.canvas.get("view[1]")/2
        temp.canvas.get("view[0]") / 2,
);
        temp.canvas.get("view[1]") / 2,
var r = func(name,vis=1,zindex=nil) return caller(0)[0];
    );
# TODO: we'll need some z-indexing here, right now it's just random
 
# TODO: use foreach/keys to show all layers in this case by traversing SymbolLayer.registry direclty ?
    var r = func(name, vis = true, zindex = nil) {
# maybe encode implicit z-indexing for each lcontroller ctor call ? - i.e. preferred above/below order ?
        return caller(0)[0];
foreach(var type; [r('TFC',0),r('APT'),r('DME'),r('VOR'),r('NDB'),r('FIX',0),r('RTE'),r('WPT'),r('FLT'),r('WXR'),r('APS'), ] )  
    }
TestMap.addLayer(factory: canvas.SymbolLayer, type_arg: type.name,
 
visible: type.vis, priority: type.zindex,
    # TODO: we'll need some z-indexing here, right now it's just random
);
    # TODO: use foreach/keys to show all layers in this case by traversing SymbolLayer.registry directly ?
    # maybe encode implicit z-indexing for each lcontroller ctor call ? - i.e. preferred above/below order ?
    var types = [
        r('TFC', false),
        r('APT'),
        r('DME'),
        r('VOR'),
        r('NDB'),
        r('FIX', false),
        r('RTE'),
        r('WPT'),
        r('FLT'),
        r('WXR'),
        r('APS'),
    ];
 
    foreach (var type; types) {
        testMap.addLayer(
            factory: canvas.SymbolLayer,
            type_arg: type.name,
            visible: type.vis,
            priority: type.zindex,
        );
    }
}; # MapStructure_selfTest
}; # MapStructure_selfTest
}}
}}
1,376

edits

Navigation menu