1
0
Fork 0

- setlistener(): add wrapper function to turn ghost arguments into props.Nodes

- "modernization": use more named function arguments and "var" keywords
This commit is contained in:
mfranz 2007-10-15 16:30:37 +00:00
parent cdc2b360d7
commit 0d3c7cec6f
2 changed files with 21 additions and 17 deletions

View file

@ -2,8 +2,7 @@
# Returns true if the first object is an instance of the second
# (class) object. Example: isa(someObject, props.Node)
#
isa = func {
obj = arg[0]; class = arg[1];
var isa = func(obj, class) {
if(obj == nil or !contains(obj, "parents")) { return 0; }
foreach(c; obj.parents) {
if(c == class) { return 1; }
@ -19,7 +18,7 @@ isa = func {
# string, in which case it specifies a path in the global property
# tree.
#
fgcommand = func(cmd, node=nil) {
var fgcommand = func(cmd, node=nil) {
if(isa(node, props.Node)) node = node._g;
_fgcommand(cmd, node);
}
@ -30,12 +29,12 @@ fgcommand = func(cmd, node=nil) {
# the ghost handle to the argument and wraps it in a
# props.Node object.
#
cmdarg = func { props.wrapNode(_cmdarg()) }
var cmdarg = func { props.wrapNode(_cmdarg()) }
##
# Utility. Does what it you think it does.
#
abs = func { if(arg[0] < 0) { -arg[0] } else { arg[0] } }
var abs = func(v) { return v < 0 ? -v : v }
##
# Convenience wrapper for the _interpolate function. Takes a
@ -56,10 +55,10 @@ abs = func { if(arg[0] < 0) { -arg[0] } else { arg[0] } }
# 16 seconds. Note the use of zero-time interpolation between 360 and
# 0 to wrap the interpolated value properly.
#
interpolate = func {
if(isa(arg[0], props.Node)) { arg[0] = arg[0]._g; }
elsif(typeof(arg[0]) != "scalar") { return; }
_interpolate(arg[0], size(arg) == 1 ? [] : subvec(arg, 1));
var interpolate = func(node, val...) {
if(isa(node, props.Node)) node = node._g;
elsif(typeof(node) != "scalar") return;
_interpolate(node, val);
}
@ -69,10 +68,15 @@ interpolate = func {
# listened to property, a function in arg[1], and an optional
# bool in arg[2], which triggers the function initially if true.
#
setlistener = func {
if(isa(arg[0], props.Node)) { arg[0] = arg[0]._g; }
var id = _setlistener(arg[0], arg[1], size(arg) > 2 ? arg[2] : 0,
size(arg) > 3 ? arg[3] : 1);
var setlistener = func(node, fun, init=0, runtime=1) {
if(isa(node, props.Node)) node = node._g;
var propghost = ghosttype(node);
var id = _setlistener(node, func {
forindex (var i; arg)
if(ghosttype(arg[i]) == propghost)
arg[i] = props.wrapNode(arg[i], nil);
call(fun, arg);
}, init, runtime);
if(__.log_level <= 2) {
var c = caller(1);
print(sprintf("setting listener #%d in %s, line %s", id, c[2], c[3]))
@ -87,7 +91,7 @@ setlistener = func {
# you can use varname in an expression without a undefined symbol
# error.
#
defined = func(sym) {
var defined = func(sym) {
var fn = 1;
while((var frame = caller(fn)) != nil) {
if(contains(frame[0], sym)) { return 1; }
@ -106,7 +110,7 @@ defined = func(sym) {
__ = {};
__.dbg_types = { none:0, bulk:1, debug:2, info:3, warn:4, alert:5 };
__.log_level = __.dbg_types[getprop("/sim/logging/priority")];
printlog = func(level, args...) {
var printlog = func(level, args...) {
if(__.dbg_types[level] >= __.log_level) { call(print, args); }
}

View file

@ -85,8 +85,8 @@ settimer(INIT, 1);
##
# Show/hide the fps display dialog.
#
fpsDisplay = func {
var w = (caller(0)[0]["arg"] == nil) ? cmdarg().getBoolValue() : arg[0];
var fpsDisplay = func(n) {
var w = isa(n, props.Node) ? n.getValue() : n;
fgcommand(w ? "dialog-show" : "dialog-close", props.Node.new({"dialog-name": "fps"}));
}