2013-03-07 19:51:06 +00:00
|
|
|
var Tooltip = {
|
2013-08-26 19:05:01 +00:00
|
|
|
# default delay (in seconds)
|
|
|
|
DELAY: 4.0,
|
2013-03-07 19:51:06 +00:00
|
|
|
# Constructor
|
|
|
|
#
|
|
|
|
# @param size ([width, height])
|
|
|
|
new: func(size, id = nil)
|
|
|
|
{
|
|
|
|
var m = {
|
|
|
|
parents: [Tooltip, PropertyElement.new(["/sim/gui/canvas", "window"], id)],
|
|
|
|
_listener: nil,
|
|
|
|
_property: nil,
|
|
|
|
_mapping: "",
|
2013-03-11 18:09:18 +00:00
|
|
|
_mappingFunc: nil,
|
2013-03-07 19:51:06 +00:00
|
|
|
_width: 0,
|
|
|
|
_height: 0,
|
2013-03-09 12:48:33 +00:00
|
|
|
_tipId: nil,
|
2017-05-29 23:20:14 +00:00
|
|
|
_isMessage: 0,
|
2013-03-10 10:13:47 +00:00
|
|
|
_slice: 17,
|
2013-03-11 18:25:30 +00:00
|
|
|
_measureText: nil,
|
2013-03-16 12:46:13 +00:00
|
|
|
_measureBB: nil,
|
2013-03-11 18:09:18 +00:00
|
|
|
_hideTimer: nil,
|
|
|
|
_hiding: nil
|
2013-03-07 19:51:06 +00:00
|
|
|
};
|
2014-06-21 10:03:35 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
m.setInt("size[0]", size[0]);
|
|
|
|
m.setInt("size[1]", size[1]);
|
|
|
|
m.setBool("visible", 0);
|
2014-06-24 13:34:03 +00:00
|
|
|
m.setInt("z-index", gui.STACK_INDEX["tooltip"]);
|
2013-03-07 19:51:06 +00:00
|
|
|
|
2013-08-26 19:05:01 +00:00
|
|
|
m._hideTimer = maketimer(m.DELAY, m, Tooltip._hideTimeout);
|
2013-03-16 12:46:13 +00:00
|
|
|
m._hideTimer.singleShot = 1;
|
2014-06-21 10:03:35 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
return m;
|
|
|
|
},
|
|
|
|
# Destructor
|
|
|
|
del: func
|
|
|
|
{
|
|
|
|
me.parents[1].del();
|
|
|
|
if( me["_canvas"] != nil )
|
|
|
|
me._canvas.del();
|
|
|
|
},
|
|
|
|
# Create the canvas to be used for this Tooltip
|
|
|
|
#
|
|
|
|
# @return The new canvas
|
|
|
|
createCanvas: func()
|
|
|
|
{
|
|
|
|
var size = [
|
|
|
|
me.get("size[0]"),
|
|
|
|
me.get("size[1]")
|
|
|
|
];
|
|
|
|
|
|
|
|
me._canvas = new({
|
|
|
|
size: [2 * size[0], 2 * size[1]],
|
|
|
|
view: size,
|
|
|
|
placement: {
|
|
|
|
type: "window",
|
|
|
|
index: me._node.getIndex()
|
2017-02-20 18:22:53 +00:00
|
|
|
},
|
|
|
|
name: "Tooltip"
|
2013-03-07 19:51:06 +00:00
|
|
|
});
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-08 16:47:00 +00:00
|
|
|
# don't do anything with mouse events ourselves
|
|
|
|
me.set("capture-events", 0);
|
|
|
|
me.set("fill", "rgba(255,255,255,0.8)");
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
# transparent background
|
|
|
|
me._canvas.setColorBackground(0.0, 0.0, 0.0, 0.0);
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
var root = me._canvas.createGroup();
|
|
|
|
me._root = root;
|
2013-03-09 12:48:33 +00:00
|
|
|
|
|
|
|
me._frame =
|
|
|
|
root.createChild("image", "background")
|
2014-03-20 10:46:48 +00:00
|
|
|
.set("src", "gui/images/tooltip.png")
|
2013-03-09 12:48:33 +00:00
|
|
|
.set("slice", me._slice ~ " fill")
|
|
|
|
.setSize(size);
|
|
|
|
|
|
|
|
me._text =
|
|
|
|
root.createChild("text", "tooltip-caption")
|
|
|
|
.setText("Aircraft Help")
|
|
|
|
.setAlignment("left-top")
|
|
|
|
.setFontSize(14)
|
|
|
|
.setFont("LiberationFonts/LiberationSans-Bold.ttf")
|
|
|
|
.setColor(1,1,1)
|
|
|
|
.setDrawMode(Text.TEXT)
|
|
|
|
.setTranslation(me._slice, me._slice)
|
|
|
|
.setMaxWidth(size[0]);
|
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
return me._canvas;
|
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
setLabel: func(msg)
|
|
|
|
{
|
|
|
|
me._label = msg;
|
|
|
|
me._updateText();
|
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
setProperty: func(prop)
|
|
|
|
{
|
|
|
|
if (me._property != nil)
|
|
|
|
removelistener(me._listener);
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
me._property = prop;
|
|
|
|
if (me._property != nil)
|
2013-03-11 18:25:30 +00:00
|
|
|
me._listener = setlistener(me._property, func { me._updateText(); });
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
me._updateText();
|
|
|
|
},
|
2013-03-11 18:25:30 +00:00
|
|
|
|
2013-03-10 10:13:47 +00:00
|
|
|
# specify a string used to compute the width of the tooltip
|
|
|
|
setWidthText: func(txt)
|
|
|
|
{
|
2013-03-11 18:25:30 +00:00
|
|
|
me._measureBB = me._text.setText(txt)
|
|
|
|
.update()
|
|
|
|
.getBoundingBox();
|
|
|
|
me._updateBounds();
|
2013-03-07 19:51:06 +00:00
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
_updateText: func
|
|
|
|
{
|
|
|
|
var msg = me._label;
|
2018-10-03 06:49:50 +00:00
|
|
|
if (me._property != nil and me._label != nil) {
|
2014-06-21 10:03:35 +00:00
|
|
|
var val = me._property.getValue() or 0;
|
|
|
|
|
|
|
|
# https://code.google.com/p/flightgear-bugs/issues/detail?id=1454
|
|
|
|
# wrap mapping in 'call' to catch conversion errors
|
|
|
|
var val_mapped = call(me._remapValue, [val], me, nil, var err = []);
|
|
|
|
if( size(err) )
|
|
|
|
printlog(
|
|
|
|
"warn",
|
|
|
|
"Tooltip: failed to remap " ~ debug.string(me._property, 0) ~ ":\n"
|
|
|
|
~ debug.string(err, 0)
|
|
|
|
);
|
|
|
|
|
|
|
|
msg = sprintf(me._label, val_mapped or val);
|
2013-03-07 19:51:06 +00:00
|
|
|
}
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
me._text.setText(msg);
|
2013-03-11 18:25:30 +00:00
|
|
|
me._updateBounds();
|
2013-03-10 10:13:47 +00:00
|
|
|
},
|
2013-03-11 18:25:30 +00:00
|
|
|
|
2013-03-10 10:13:47 +00:00
|
|
|
_updateBounds: func
|
|
|
|
{
|
2013-03-11 18:25:30 +00:00
|
|
|
var max_width = me.get("size[0]") - 2 * me._slice;
|
|
|
|
me._text.setMaxWidth(max_width);
|
|
|
|
|
|
|
|
# compute the bounds
|
|
|
|
var text_bb = me._text.update().getBoundingBox();
|
|
|
|
var width = text_bb[2];
|
|
|
|
var height = text_bb[3];
|
|
|
|
|
|
|
|
if( me._measureBB != nil )
|
|
|
|
{
|
|
|
|
width = math.max(width, me._measureBB[2]);
|
|
|
|
height = math.max(height, me._measureBB[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( width > max_width )
|
|
|
|
width = max_width;
|
|
|
|
|
|
|
|
me._width = width + 2 * me._slice;
|
|
|
|
me._height = height + 2 * me._slice;
|
|
|
|
me._frame.setSize(me._width, me._height)
|
|
|
|
.update();
|
2013-03-07 19:51:06 +00:00
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
_remapValue: func(val)
|
|
|
|
{
|
|
|
|
if (me._mapping == "") return val;
|
2016-01-16 01:35:48 +00:00
|
|
|
if (me._mapping == "percent") return math.round(val * 100);
|
2014-06-21 10:03:35 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
# TODO - translate me!
|
|
|
|
if (me._mapping == "on-off") return (val == 1) ? "ON" : "OFF";
|
|
|
|
if (me._mapping == "arm-disarm") return (val == 1) ? "ARMED" : "DISARMED";
|
2014-06-21 10:03:35 +00:00
|
|
|
|
2013-03-11 17:19:32 +00:00
|
|
|
# provide both 'senses' of the flag here
|
|
|
|
if (me._mapping == "up-down") return (val == 1) ? "UP" : "DOWN";
|
2014-06-21 10:03:35 +00:00
|
|
|
if (me._mapping == "down-up") return (val == 1) ? "DOWN" : "UP";
|
2013-03-11 18:09:18 +00:00
|
|
|
if (me._mapping == "open-close") return (val == 1) ? "OPEN" : "CLOSED";
|
|
|
|
if (me._mapping == "close-open") return (val == 1) ? "CLOSED" : "OPEN";
|
2014-06-21 10:03:35 +00:00
|
|
|
|
2013-03-10 10:13:47 +00:00
|
|
|
if (me._mapping == "heading") return geo.normdeg(val);
|
2013-03-11 18:09:18 +00:00
|
|
|
if (me._mapping == "nasal") return me._mappingFunc(val);
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
return val;
|
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-11 18:09:18 +00:00
|
|
|
setMapping: func(mapping, f = nil)
|
2013-03-07 19:51:06 +00:00
|
|
|
{
|
|
|
|
me._mapping = mapping;
|
2013-03-11 18:09:18 +00:00
|
|
|
me._mappingFunc = f;
|
2013-03-07 19:51:06 +00:00
|
|
|
me._updateText();
|
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
setTooltipId: func(tipId)
|
|
|
|
{
|
|
|
|
me._tipId = tipId;
|
2013-03-11 18:09:18 +00:00
|
|
|
if ((tipId != nil) and me._hiding) {
|
|
|
|
me._hideTimer.stop();
|
|
|
|
me._hiding = 0;
|
|
|
|
}
|
2013-03-07 19:51:06 +00:00
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
getTooltipId: func { me._tipId; },
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
# Get the displayed canvas
|
|
|
|
getCanvas: func()
|
|
|
|
{
|
|
|
|
return me['_canvas'];
|
|
|
|
},
|
2013-03-11 18:25:30 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
setPosition: func(x, y)
|
|
|
|
{
|
|
|
|
me.setInt("x", x + 10);
|
|
|
|
me.setInt("y", y + 10);
|
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2017-05-29 23:20:14 +00:00
|
|
|
isMessage: func()
|
|
|
|
{
|
|
|
|
return me._isMessage;
|
|
|
|
},
|
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
show: func()
|
|
|
|
{
|
|
|
|
# don't show if undefined
|
|
|
|
if (me._tipId == nil) return;
|
2014-06-21 10:03:35 +00:00
|
|
|
|
2013-03-11 18:09:18 +00:00
|
|
|
if (me._hiding) {
|
|
|
|
me._hideTimer.stop();
|
|
|
|
me._hiding = 0;
|
|
|
|
}
|
2014-06-21 10:03:35 +00:00
|
|
|
|
2013-03-11 18:09:18 +00:00
|
|
|
if (!me.isVisible()) {
|
2014-06-21 10:03:35 +00:00
|
|
|
me.setBool("visible", 1);
|
2013-03-11 18:09:18 +00:00
|
|
|
}
|
2013-03-07 19:51:06 +00:00
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2014-12-16 06:26:09 +00:00
|
|
|
showMessage: func(timeout = nil, node = nil)
|
2013-03-16 12:46:13 +00:00
|
|
|
{
|
2014-12-16 06:26:09 +00:00
|
|
|
if(var y = me._haveNode(node, 'y') != nil ) {
|
|
|
|
me.setInt("y", y);
|
|
|
|
} else {
|
|
|
|
me.setInt("y", getprop('/sim/startup/ysize') * 0.2);
|
|
|
|
}
|
|
|
|
if(var x = me._haveNode(node, 'x') != nil) {
|
|
|
|
me.setInt("x", x);
|
|
|
|
} else {
|
|
|
|
var screenW = getprop('/sim/startup/xsize');
|
|
|
|
me.setInt("x", (screenW - me._width) * 0.5);
|
|
|
|
}
|
2017-05-29 23:20:14 +00:00
|
|
|
me._isMessage = 1;
|
2013-03-16 12:46:13 +00:00
|
|
|
me.show();
|
2013-12-09 22:02:06 +00:00
|
|
|
# https://code.google.com/p/flightgear-bugs/issues/detail?id=1273
|
|
|
|
# when tooltip is shown for some other reason, ensure it stays for
|
|
|
|
# the full delay (unless replaced). Don't allow the update-hover
|
|
|
|
# code path to hide() with the shorter delay.
|
|
|
|
me._hiding = 1;
|
2013-08-26 19:05:01 +00:00
|
|
|
me._hideTimer.restart(timeout or me.DELAY);
|
2013-03-16 12:46:13 +00:00
|
|
|
},
|
|
|
|
|
2014-12-16 06:26:09 +00:00
|
|
|
_haveNode: func(node, key) {
|
|
|
|
if(node == nil ) return nil;
|
|
|
|
var value = num(node.getValue(key) );
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
hide: func()
|
|
|
|
{
|
2013-03-11 18:09:18 +00:00
|
|
|
# this gets run repeatedly during mouse-moves
|
|
|
|
if (me._hiding) return;
|
|
|
|
me._hiding = 1;
|
2014-06-21 10:03:35 +00:00
|
|
|
|
2013-03-11 18:09:18 +00:00
|
|
|
me._hideTimer.restart(0.5);
|
2013-03-16 12:46:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
hideNow: func()
|
|
|
|
{
|
|
|
|
me._hideTimer.stop();
|
|
|
|
me._hideTimeout();
|
2013-03-07 19:51:06 +00:00
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
isVisible: func
|
|
|
|
{
|
|
|
|
return me.getBool("visible");
|
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
fadeIn: func()
|
|
|
|
{
|
|
|
|
me.show();
|
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
fadeOut: func()
|
|
|
|
{
|
|
|
|
me.hide();
|
2013-03-16 12:46:13 +00:00
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
# private:
|
2013-03-16 12:46:13 +00:00
|
|
|
_hideTimeout: func()
|
|
|
|
{
|
|
|
|
me.setBool("visible", 0);
|
2013-03-11 18:09:18 +00:00
|
|
|
me._hiding = 0;
|
2013-03-16 12:46:13 +00:00
|
|
|
}
|
2013-03-07 19:51:06 +00:00
|
|
|
};
|
|
|
|
|
2013-03-10 10:13:47 +00:00
|
|
|
var tooltip = canvas.Tooltip.new([300, 100]);
|
2013-03-16 12:46:13 +00:00
|
|
|
tooltip.createCanvas();
|
2013-03-07 19:51:06 +00:00
|
|
|
|
2013-03-16 12:46:13 +00:00
|
|
|
var innerSetTooltip = func(node)
|
2013-03-07 19:51:06 +00:00
|
|
|
{
|
2018-10-03 06:49:50 +00:00
|
|
|
tooltip.setLabel(nil);
|
2013-03-11 18:25:30 +00:00
|
|
|
|
2013-03-11 18:09:18 +00:00
|
|
|
var propPath = cmdarg().getNode('property');
|
|
|
|
if (propPath != nil) {
|
|
|
|
var n = props.globals.getNode(propPath.getValue());
|
2013-03-07 19:51:06 +00:00
|
|
|
tooltip.setProperty(n);
|
2013-03-11 18:09:18 +00:00
|
|
|
} else {
|
|
|
|
tooltip.setProperty(nil);
|
|
|
|
}
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2018-10-03 06:49:50 +00:00
|
|
|
tooltip.setLabel(cmdarg().getNode('label').getValue());
|
|
|
|
|
|
|
|
var measure = cmdarg().getNode('measure-text');
|
|
|
|
if (measure != nil) {
|
|
|
|
tooltip.setWidthText(measure.getValue());
|
|
|
|
} else {
|
|
|
|
tooltip.setWidthText(nil);
|
|
|
|
}
|
|
|
|
|
2013-03-11 18:09:18 +00:00
|
|
|
var mapping = cmdarg().getNode('mapping');
|
|
|
|
if (mapping != nil) {
|
|
|
|
var m = mapping.getValue();
|
|
|
|
var f = nil;
|
|
|
|
if (m == 'nasal') {
|
|
|
|
f = compile(cmdarg().getNode('script').getValue());
|
|
|
|
}
|
2014-06-21 10:03:35 +00:00
|
|
|
|
2013-03-11 18:09:18 +00:00
|
|
|
tooltip.setMapping(m, f);
|
2013-03-07 19:51:06 +00:00
|
|
|
} else {
|
2013-03-11 17:19:32 +00:00
|
|
|
tooltip.setMapping(nil);
|
2013-03-07 19:51:06 +00:00
|
|
|
}
|
2013-03-16 12:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var setTooltip = func(node)
|
|
|
|
{
|
|
|
|
var tipId = cmdarg().getNode('tooltip-id').getValue();
|
|
|
|
if (tooltip.getTooltipId() == tipId) {
|
|
|
|
return; # nothing more to do
|
|
|
|
}
|
|
|
|
|
|
|
|
var x = cmdarg().getNode('x').getValue();
|
|
|
|
var y = cmdarg().getNode('y').getValue();
|
|
|
|
|
2013-03-11 18:09:18 +00:00
|
|
|
var screenHeight = getprop('/sim/startup/ysize');
|
|
|
|
tooltip.setPosition(x, screenHeight - y);
|
|
|
|
tooltip.setTooltipId(tipId);
|
2017-05-29 23:20:14 +00:00
|
|
|
tooltip._isMessage = 0;
|
|
|
|
|
2013-03-11 18:09:18 +00:00
|
|
|
innerSetTooltip(node);
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
# don't actually show here, we do that response to tooltip-timeout
|
|
|
|
# so this is just getting ready
|
|
|
|
}
|
|
|
|
|
|
|
|
var showTooltip = func(node)
|
|
|
|
{
|
2017-05-29 23:20:14 +00:00
|
|
|
# this is driven by the mouse-move timeout from native code,
|
|
|
|
# which is irrelevant for message tips
|
|
|
|
if (tooltip.isMessage())
|
|
|
|
return;
|
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
var r = node.getNode("reason");
|
|
|
|
if ((r != nil) and (r.getValue() == "click")) {
|
|
|
|
# click triggering tooltip, show immediately
|
|
|
|
tooltip.show();
|
|
|
|
} else {
|
|
|
|
tooltip.fadeIn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var updateHover = func(node)
|
|
|
|
{
|
2013-03-11 17:19:32 +00:00
|
|
|
tooltip.setTooltipId(nil);
|
2013-03-11 18:25:30 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
# if not shown, nothing to do here
|
|
|
|
if (!tooltip.isVisible()) return;
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
# reset cursor to standard
|
|
|
|
tooltip.fadeOut();
|
2013-03-11 18:25:30 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-16 12:46:13 +00:00
|
|
|
var showMessage = func(node)
|
|
|
|
{
|
|
|
|
var msgId = node.getNode("id");
|
|
|
|
tooltip.setTooltipId((msgId == nil) ? 'msg' : msgId.getValue());
|
|
|
|
innerSetTooltip(node);
|
2013-08-26 19:05:01 +00:00
|
|
|
|
|
|
|
var timeout = node.getNode("delay");
|
2014-12-16 06:26:09 +00:00
|
|
|
tooltip.showMessage( timeout != nil ? timeout.getValue() : nil, node);
|
2013-03-16 12:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var clearMessage = func(node)
|
|
|
|
{
|
|
|
|
var msgId = node.getNode("id");
|
|
|
|
if (tooltip.getTooltipId() != msgId.getValue()) return;
|
|
|
|
tooltip.hideNow();
|
|
|
|
}
|
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
addcommand("update-hover", updateHover);
|
|
|
|
addcommand("set-tooltip", setTooltip);
|
|
|
|
addcommand("tooltip-timeout", showTooltip);
|
2013-03-16 12:46:13 +00:00
|
|
|
addcommand("show-message", showMessage);
|
2013-03-11 18:09:18 +00:00
|
|
|
addcommand("clear-message", clearMessage);
|