1,376
edits
| 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: | |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 = {}; | |||
temp.dlg = canvas.Window.new([600, 400], "dialog"); | |||
temp.canvas = temp.dlg.createCanvas().setColorBackground(1, 1, 1, 0.5); | |||
temp.root = temp.canvas.createGroup(); | |||
var testMap = temp.root.createChild("map"); | |||
testMap.setController("Aircraft position"); | |||
testMap.setRange(25); # TODO: implement zooming/panning via mouse/wheel here, for lack of buttons :-/ | |||
testMap.setTranslation( | |||
temp.canvas.get("view[0]") / 2, | |||
temp.canvas.get("view[1]") / 2, | |||
); | |||
var r = func(name, vis = true, 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 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 | ||
}} | }} | ||
edits