1
0
Fork 0

Nasal/*.nas: named arguments

Instead of var (name) = arg[0..n], which doesn't give a "missing
argument" error.
This commit is contained in:
Philosopher 2013-10-19 22:34:56 -05:00
parent 51c66c3df4
commit 36305939f3
4 changed files with 43 additions and 53 deletions

View file

@ -196,10 +196,9 @@ var stepSlats = func(step) {
# magnetos, for instance), work similarly but not compatibly, and # magnetos, for instance), work similarly but not compatibly, and
# could be integrated. # could be integrated.
# #
var stepProps = func { var stepProps = func(dst, array, delta) {
var dst = props.globals.getNode(arg[0]); dst = props.globals.getNode(dst);
var array = props.globals.getNode(arg[1]); array = props.globals.getNode(array);
var delta = arg[2];
if(dst == nil or array == nil) { return; } if(dst == nil or array == nil) { return; }
var sets = array.getChildren("setting"); var sets = array.getChildren("setting");
@ -238,30 +237,30 @@ var TRIM_RATE = 0.045;
# events. They are *not* good for binding to the keyboard, since (at # events. They are *not* good for binding to the keyboard, since (at
# least) X11 synthesizes its own key repeats. # least) X11 synthesizes its own key repeats.
# #
var elevatorTrim = func { var elevatorTrim = func(speed) {
slewProp("/controls/flight/elevator-trim", arg[0] * TRIM_RATE); } slewProp("/controls/flight/elevator-trim", speed * TRIM_RATE); }
var aileronTrim = func { var aileronTrim = func(speed) {
slewProp("/controls/flight/aileron-trim", arg[0] * TRIM_RATE); } slewProp("/controls/flight/aileron-trim", speed * TRIM_RATE); }
var rudderTrim = func { var rudderTrim = func(speed) {
slewProp("/controls/flight/rudder-trim", arg[0] * TRIM_RATE); } slewProp("/controls/flight/rudder-trim", speed * TRIM_RATE); }
var THROTTLE_RATE = 0.33; var THROTTLE_RATE = 0.33;
var adjThrottle = func { var adjThrottle = func(speed) {
adjEngControl("throttle", arg[0]); } adjEngControl("throttle", speed); }
var adjMixture = func { var adjMixture = func(speed) {
adjEngControl("mixture", arg[0]); } adjEngControl("mixture", speed); }
var adjCondition = func { var adjCondition = func(speed) {
adjEngControl("condition", arg[0]); } adjEngControl("condition", speed); }
var adjPropeller = func { var adjPropeller = func(speed) {
adjEngControl("propeller-pitch", arg[0]); } adjEngControl("propeller-pitch", speed); }
var adjEngControl = func { var adjEngControl = func(prop, speed) {
var delta = arg[1] * THROTTLE_RATE * getprop("/sim/time/delta-realtime-sec"); var delta = speed * THROTTLE_RATE * getprop("/sim/time/delta-realtime-sec");
var (value, count) = (0, 0); var (value, count) = (0, 0);
foreach(var e; engines) { foreach(var e; engines) {
if(e.selected.getValue()) { if(e.selected.getValue()) {
var node = e.controls.getNode(arg[0], 1); var node = e.controls.getNode(prop, 1);
node.setValue(node.getValue() + delta); node.setValue(node.getValue() + delta);
value += node.getValue(); # must read again because of clamping value += node.getValue(); # must read again because of clamping
count += 1; count += 1;

View file

@ -242,11 +242,10 @@ var currTimer = 0;
# API. Note especially the slightly tricky addChild() method. # API. Note especially the slightly tricky addChild() method.
# #
var Widget = { var Widget = {
set : func { me.node.getNode(arg[0], 1).setValue(arg[1]); }, set : func(name, val) { me.node.getNode(name, 1).setValue(val); },
prop : func { return me.node; }, prop : func { return me.node; },
new : func { return { parents : [Widget], node : props.Node.new() } }, new : func { return { parents : [Widget], node : props.Node.new() } },
addChild : func { addChild : func(type) {
var type = arg[0];
var idx = size(me.node.getChildren(type)); var idx = size(me.node.getChildren(type));
var name = type ~ "[" ~ idx ~ "]"; var name = type ~ "[" ~ idx ~ "]";
var newnode = me.node.getNode(name, 1); var newnode = me.node.getNode(name, 1);
@ -1134,9 +1133,9 @@ var showWeightDialog = func {
# </text> # </text>
# </help> # </help>
# #
var showHelpDialog = func { var showHelpDialog = func(path, toggle=0) {
var node = props.globals.getNode(arg[0]); var node = props.globals.getNode(path);
if (arg[0] == "/sim/help" and size(node.getChildren()) < 4) { if (path == "/sim/help" and size(node.getChildren()) < 4) {
node = node.getChild("common"); node = node.getChild("common");
} }
@ -1147,7 +1146,7 @@ var showHelpDialog = func {
name = getprop("/sim/aircraft"); name = getprop("/sim/aircraft");
} }
} }
var toggle = size(arg) > 1 and arg[1] != nil and arg[1] > 0; var toggle = toggle > 0;
if (toggle and contains(dialog, name)) { if (toggle and contains(dialog, name)) {
fgcommand("dialog-close", props.Node.new({ "dialog-name": name })); fgcommand("dialog-close", props.Node.new({ "dialog-name": name }));
delete(dialog, name); delete(dialog, name);

View file

@ -50,17 +50,14 @@
var dialog = nil; var dialog = nil;
var colorgroup = func { var colorgroup = func(parent, name, base) {
var parent = arg[0]; # pui parent var undef = func { props.globals.getNode(base ~ name ~ "/" ~ parent) == nil };
var name = arg[1]; # "diffuse"
var base = arg[2];
var undef = func { props.globals.getNode(base ~ name ~ "/" ~ arg[0]) == nil };
if (undef("red") and undef("green") and undef("blue")) { if (undef("red") and undef("green") and undef("blue")) {
return 0; return 0;
} }
if (arg[3] != nil) { if (base != nil) {
parent.addChild("hrule").setColor(1, 1, 1, 0.5); parent.addChild("hrule").setColor(1, 1, 1, 0.5);
} }
@ -76,11 +73,7 @@ var colorgroup = func {
} }
var mat = func { var mat = func(parent, name, path, format, min=nil, max=nil) {
var parent = arg[0];
var name = arg[1];
var path = arg[2];
var format = arg[3];
if (props.globals.getNode(path) != nil) { if (props.globals.getNode(path) != nil) {
var grp = parent.addChild("group"); var grp = parent.addChild("group");
grp.set("layout", "hbox"); grp.set("layout", "hbox");
@ -91,9 +84,9 @@ var mat = func {
var slider = grp.addChild("slider"); var slider = grp.addChild("slider");
slider.set("property", path); slider.set("property", path);
slider.set("live", 1); slider.set("live", 1);
if (size(arg) == 6) { if (min != nil and max != nil) {
slider.set("min", arg[4]); slider.set("min", min);
slider.set("max", arg[5]); slider.set("max", max);
} }
slider.setBinding("dialog-apply"); slider.setBinding("dialog-apply");
@ -107,8 +100,7 @@ var mat = func {
} }
var showDialog = func { var showDialog = func(base, title=nil, x=nil, y=nil) {
var base = arg[0];
while (size(base) and substr(base, size(base) - 1, 1) == "/") { while (size(base) and substr(base, size(base) - 1, 1) == "/") {
base = substr(base, 0, size(base) - 1); base = substr(base, 0, size(base) - 1);
} }
@ -121,14 +113,14 @@ var showDialog = func {
parentdir = c ~ parentdir; parentdir = c ~ parentdir;
} }
var title = if (size(arg) > 1 and arg[1] != nil) { arg[1] } else { parentdir }; if (title == nil) var title = parentdir;
var name = "material-" ~ parentdir; var name = "material-" ~ parentdir;
base = base ~ "/"; base = base ~ "/";
dialog = gui.Widget.new(); dialog = gui.Widget.new();
dialog.set("name", name); dialog.set("name", name);
if (size(arg) > 2 and arg[2] != nil) { dialog.set("x", arg[2]) } if (x != nil) dialog.set("x", x);
if (size(arg) > 3 and arg[3] != nil) { dialog.set("y", arg[3]) } if (y != nil) dialog.set("y", y);
dialog.set("layout", "vbox"); dialog.set("layout", "vbox");
var titlebar = dialog.addChild("group"); var titlebar = dialog.addChild("group");
@ -152,7 +144,7 @@ var showDialog = func {
h += colorgroup(dialog, "emission", base, h); h += colorgroup(dialog, "emission", base, h);
h += colorgroup(dialog, "specular", base, h); h += colorgroup(dialog, "specular", base, h);
var undef = func { props.globals.getNode(base ~ arg[0]) == nil }; var undef = func(prop) { props.globals.getNode(base ~ prop) == nil };
if (!(undef("shininess") and undef("transparency/alpha") and undef("threshold"))) { if (!(undef("shininess") and undef("transparency/alpha") and undef("threshold"))) {
if (h) { if (h) {
dialog.addChild("hrule").setColor(1, 1, 1, 0.5); dialog.addChild("hrule").setColor(1, 1, 1, 0.5);