1
0
Fork 0

Nasal/canvas/tooltip.nas: handle multiple properties.

Allows sprintf string to contain multiple %'s, which are mapped to multiple
<property>...</property> tags.
This commit is contained in:
Julian Smith 2019-08-29 11:08:08 +01:00
parent c280ff3fc4
commit e41c0f099b

View file

@ -8,8 +8,8 @@ var Tooltip = {
{ {
var m = { var m = {
parents: [Tooltip, PropertyElement.new(["/sim/gui/canvas", "window"], id)], parents: [Tooltip, PropertyElement.new(["/sim/gui/canvas", "window"], id)],
_listener: nil, _listeners: [],
_property: nil, _properties: [],
_mapping: "", _mapping: "",
_mappingFunc: nil, _mappingFunc: nil,
_width: 0, _width: 0,
@ -96,15 +96,19 @@ var Tooltip = {
me._updateText(); me._updateText();
}, },
setProperty: func(prop) setProperties: func(properties)
{ {
if (me._property != nil) foreach(var l; me._listeners) {
removelistener(me._listener); removelistener(l);
}
me._property = prop; me._listeners = [];
if (me._property != nil) me._properties = properties;
me._listener = setlistener(me._property, func { me._updateText(); }); foreach(var p; me._properties) {
if (p != nil) {
var l = setlistener(p, func { me._updateText(); });
append(me._listeners, l);
}
}
me._updateText(); me._updateText();
}, },
@ -119,21 +123,28 @@ var Tooltip = {
_updateText: func _updateText: func
{ {
var msg = me._label; var msg = '';
if (me._property != nil and me._label != nil) { if (me._label != nil) {
var val = me._property.getValue() or 0; var args = [me._label];
foreach(var p; me._properties) {
var val = '';
if (p != nil) val = p.getValue() or 0;
# https://code.google.com/p/flightgear-bugs/issues/detail?id=1454 # https://code.google.com/p/flightgear-bugs/issues/detail?id=1454
# wrap mapping in 'call' to catch conversion errors # wrap mapping in 'call' to catch conversion errors
var val_mapped = call(me._remapValue, [val], me, nil, var err = []); var val_mapped = call(me._remapValue, [val], me, nil, var err = []);
if( size(err) ) if( size(err) ) {
printlog( printlog(
"warn", "warn",
"Tooltip: failed to remap " ~ debug.string(me._property, 0) ~ ":\n" "Tooltip: failed to remap " ~ debug.string(p, 0) ~ ":\n"
~ debug.string(err, 0) ~ debug.string(err, 0)
); );
val_mapped = '';
}
append(args, val_mapped);
}
msg = sprintf(me._label, val_mapped or val); msg = call( sprintf, args);
} }
me._text.setText(msg); me._text.setText(msg);
@ -309,14 +320,14 @@ tooltip.createCanvas();
var innerSetTooltip = func(node) var innerSetTooltip = func(node)
{ {
tooltip.setLabel(nil); tooltip.setLabel(nil);
var propPaths0 = cmdarg().getChildren('property');
var propPath = cmdarg().getNode('property'); var propPaths = [];
if (propPath != nil) { foreach(var p; propPaths0) {
var n = props.globals.getNode(propPath.getValue()); var v = p.getValue();
tooltip.setProperty(n); if (v != nil) v = props.globals.getNode(v);
} else { append(propPaths, v);
tooltip.setProperty(nil);
} }
tooltip.setProperties(propPaths);
tooltip.setLabel(cmdarg().getNode('label').getValue()); tooltip.setLabel(cmdarg().getNode('label').getValue());