Sport-Tooltip is a super fast and clean Tooltip class I developed with the Haxe community in mind, and is aimed at Haxe/Flash. It is :
- Extremely simple to use
- Very compact
- Using a clean and transparent approach (not messing with your Flash like some other libs!)
- Stylish & easily customizable
- Released in Public domain (you can do whatever you want with it)
(Note: it will probably be converted to ActionScript 3 soon).
Kick-off
This demo will illustrate most of the features provided by Sport-Tooltip.
Notice the three different white, red and black areas on the right. Those show the 3 different overflow styles you can get in Sport-Tooltip. Also the yellow area is using a callback for the tooltip text instead of a String. As a result, it returns a random animal from the Chinese zodiac everytime.
Move mouse over the coloured areas to get some tooltips to appear:
This demo’s code is available at the end of this post.
How to use it?
Like so:
1 | Tooltip.attach(spriteMario, "My name is Super Mario!"); |
This will create tooltips containing “My name is Super Mario!” when hovering the sprite.
But what if my tooltip content is not static?
Provide a callback instead
Use Tooltip.attachF() to attach some callback instead of a String:
1 2 3 4 | Tooltip.attachF(mySprite, function() { return "Today is " + ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"][Std.random(7)]; }); |
You can also refer to functions and static methods with their name:
1 | Tooltip.attachF(mySprite, getTextForTooltip); |
What about styling?
Sport-Tooltip uses the following typedef for defining styles:
1 2 3 4 5 6 7 8 9 10 11 | /** * This is the structure to configure the tooltip. */ typedef TooltipStyle = { var overflow: OverflowStyle; var fg: UInt; // text color var bg: UInt; // background color var bgAlpha: Float; // background alpha var fontFamily: String; // font-family var fontSize: Int; // font-size } |
Overflow defines what happens when a tooltip content overflows out of stage. It can have 3 values, as defined by the enum OverflowStyle:
- Lock: tooltip will be locked on the border, forbidden to cross it,
- Serious: tooltip will go in the opposite direction,
- None: tooltip will be let overflowing off-stage.
For an illustration of overflow, you can have a look at the demo, especially the 3 white, red and black areas.
The other parameters you’re going to be dealing with if restyling the tooltip are easy to figure out:
- fg is the text color (eg. 0xffffff for white)
- bg is the background color (eg. 0×000000 for black)
- bgAlpha is the alpha opacity for the background only (eg. 0.5 for semi-opaque)
- fontFamily is a string defining the font(s) to use, separated by comas (eg. “Arial, sans-serif”)
- fontSize is the font size in pixels (eg. 15)
This is nice, but how to actually use it?
Overwriting the default style
The default style is defined in Tooltip.FACTORYSTYLE. But usually you’ll want to change it dynamically at the beginning of your program, like so:
1 2 3 4 5 6 7 8 | Tooltip.setDefaultStyle({ overflow: Lock, bgAlpha: 1.0, bg: 0x0, fg: 0xffffff, fontFamily: "Trebuchet MS", fontSize: 15 }); |
What about those cases where I want a specific style for a tooltip?
Attaching a specific style to a tooltip
Tooltip.attach() admits an optionnal third argument, of the type TooltipStyle. You can write something like this:
1 2 3 4 5 6 7 8 | Tooltip.attach(text, "<b>Sport-Tooltip</b> is clean.", { overflow: Serious, fg: 0xffffff, bg: 0x0000ff, bgAlpha: .9, fontFamily: "Trebuchet MS", fontSize: 15 }); |
Is that it?
That’s about it!! Two other public methods actually exist: unattach() and unattachF().
They are used in those cases where you dynamically create and delete a lot of elements that have tooltips attached to them, so the listeners can get properly unregistered. It’s normally good practice to always call unattach() and unattachF() every time you destroy an element having previously been attached a tooltip.
public API
They are only 5 methods publicly available. Also I haven’t written the returns for the sake of simplicity, all of those 5 methods actually return the class Tooltip, so they can be chained if you want:
- static function setDefaultStyle(s:TooltipStyle);
- static function attach(d:DisplayObject, s:String, st:TooltipStyle=null);
- static function attachF(d:DisplayObject, f:Void->String, st:TooltipStyle=null);
- static function unattach(d:DisplayObject, s:String, st:TooltipStyle=null);
- static function unattachD(d:DisplayObject, f:Void->String, st:TooltipStyle=null);
Source code
The source code can be get here:
Enjoy!
Sport-Tooltip, super fast and clean tooltip for Haxe/Flash.











