2013-03-07 19:51:06 +00:00
|
|
|
var Tooltip = {
|
|
|
|
# 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: "",
|
|
|
|
_width: 0,
|
|
|
|
_height: 0,
|
2013-03-09 12:48:33 +00:00
|
|
|
_tipId: nil,
|
2013-03-10 10:13:47 +00:00
|
|
|
_slice: 17,
|
|
|
|
_measureText: nil
|
2013-03-07 19:51:06 +00:00
|
|
|
};
|
|
|
|
m.setInt("size[0]", size[0]);
|
|
|
|
m.setInt("size[1]", size[1]);
|
|
|
|
m.setBool("visible", 0);
|
|
|
|
|
|
|
|
# arg = [child, listener_node, mode, is_child_event]
|
|
|
|
# setlistener(m._node, func m._propCallback(arg[0], arg[2]), 0, 2);
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
});
|
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")
|
|
|
|
.set("file", "gui/images/tooltip.png")
|
|
|
|
.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-10 10:13:47 +00:00
|
|
|
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
|
|
|
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)
|
|
|
|
me._listener = setlistener(me._property, func { me._propertyChanged(); });
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
me._updateText();
|
|
|
|
},
|
2013-03-10 10:13:47 +00:00
|
|
|
|
|
|
|
# specify a string used to compute the width of the tooltip
|
|
|
|
setWidthText: func(txt)
|
|
|
|
{
|
|
|
|
me._measureText = txt;
|
|
|
|
me._updateBounds();
|
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
_propertyChanged: func
|
|
|
|
{
|
|
|
|
me._updateText();
|
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
_updateText: func
|
|
|
|
{
|
|
|
|
var msg = me._label;
|
|
|
|
if (me._property != nil) {
|
|
|
|
var val = me._remapValue(me._property.getValue());
|
|
|
|
msg = sprintf(me._label, val);
|
|
|
|
}
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
me._text.setText(msg);
|
2013-03-10 10:13:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_updateBounds: func
|
|
|
|
{
|
|
|
|
var width = me.get("size[0]");
|
|
|
|
me._text.setMaxWidth(width);
|
|
|
|
|
|
|
|
if (me._measureText == nil)
|
|
|
|
me._updateText();
|
|
|
|
else
|
|
|
|
me._text.setText(me._measureText);
|
|
|
|
|
|
|
|
me._text.update(); # compute the bounds
|
|
|
|
var bounds = me._text.getBoundingBox();
|
|
|
|
me._updateText(); # restore real text after measurement
|
|
|
|
|
|
|
|
if ((bounds[2] == me._width) and (bounds[3] == me._height))
|
|
|
|
return;
|
|
|
|
|
|
|
|
me._width = bounds[2] + 2 * me._slice;
|
|
|
|
me._height = bounds[3] + 2 * me._slice;
|
|
|
|
me._frame.setSize(me._width, me._height);
|
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;
|
|
|
|
if (me._mapping == "percent") return int(val * 100);
|
|
|
|
# TODO - translate me!
|
|
|
|
if (me._mapping == "on-off") return (val == 1) ? "ON" : "OFF";
|
|
|
|
if (me._mapping == "arm-disarm") return (val == 1) ? "ARMED" : "DISARMED";
|
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";
|
|
|
|
if (me._mapping == "down-up") return (val == 1) ? "DOWN" : "UP";
|
2013-03-10 10:13:47 +00:00
|
|
|
if (me._mapping == "heading") return geo.normdeg(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-07 19:51:06 +00:00
|
|
|
setMapping: func(mapping)
|
|
|
|
{
|
|
|
|
me._mapping = mapping;
|
|
|
|
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-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-10 10:13:47 +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
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
show: func()
|
|
|
|
{
|
|
|
|
# don't show if undefined
|
|
|
|
if (me._tipId == nil) return;
|
|
|
|
me.setBool("visible", 1);
|
|
|
|
},
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
hide: func()
|
|
|
|
{
|
|
|
|
me.setBool("visible", 0);
|
|
|
|
},
|
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-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
# private:
|
2013-03-09 12:48:33 +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-07 19:51:06 +00:00
|
|
|
tooltip.createCanvas();
|
|
|
|
|
|
|
|
var setTooltip = func(node)
|
|
|
|
{
|
|
|
|
var tipId = cmdarg().getNode('tooltip-id').getValue();
|
|
|
|
if (tooltip.getTooltipId() == tipId) {
|
|
|
|
return; # nothing more to do
|
|
|
|
}
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
var x = cmdarg().getNode('x').getValue();
|
|
|
|
var y = cmdarg().getNode('y').getValue();
|
|
|
|
|
|
|
|
var screenHeight = getprop('/sim/startup/ysize');
|
|
|
|
tooltip.setPosition(x, screenHeight - y);
|
|
|
|
tooltip.setTooltipId(tipId);
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
tooltip.setLabel(cmdarg().getNode('label').getValue());
|
2013-03-10 10:13:47 +00:00
|
|
|
var measure = cmdarg().getNode('measure-text');
|
|
|
|
if (measure != nil) {
|
|
|
|
tooltip.setWidthText(measure.getValue());
|
|
|
|
} else {
|
|
|
|
tooltip.setWidthText(nil);
|
|
|
|
}
|
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
var nodePath = cmdarg().getNode('property');
|
|
|
|
if (nodePath != nil) {
|
|
|
|
var n = props.globals.getNode(nodePath.getValue());
|
|
|
|
tooltip.setProperty(n);
|
2013-03-09 12:48:33 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
# mapping modes allow some standard conversion of the property
|
|
|
|
# value to a human readable form.
|
|
|
|
var mapping = cmdarg().getNode('mapping');
|
|
|
|
tooltip.setMapping(mapping == nil ? "" : mapping.getValue());
|
|
|
|
} else {
|
2013-03-09 12:48:33 +00:00
|
|
|
tooltip.setProperty(nil);
|
2013-03-11 17:19:32 +00:00
|
|
|
tooltip.setMapping(nil);
|
2013-03-07 19:51:06 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
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-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 17:19:32 +00:00
|
|
|
|
2013-03-07 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addcommand("update-hover", updateHover);
|
|
|
|
addcommand("set-tooltip", setTooltip);
|
|
|
|
addcommand("tooltip-timeout", showTooltip);
|