Not sure what to call the title, but the question goes as such:
I have a class Tooltip that displays a tooltip at a given x and y coordinate and a message to go along with it. Because the tooltip can be displayed anywhere on the desktop, the coordinates have to be converted to the Screen coordinates and then the function has to be called. It's a static-initialized/singleton since there should be only one Tooltip at a time, and also because it should be accessible from all locations (which I figure shouldn't be too harmful since it's more or less a passive class that doesn't change global state)
Since all the code to display the tooltip (converting to screen coordinates, etc.) is the same, I figure it would be easier to create a manager class ToolTipMessageManager that stores all the messages for each object in a Dictionary, and the displayTooltip() is defined within it to display the tooltip at a given location (which it converts to screen coordinates). It's worth noting that this manager class is not static, and each class should have an instance of this class.
(This class does not automatically register event listeners, though, so the class that uses tooltips should add listeners for it. I figured this was because tooltips could appear in a number of ways, not just through mouse / key input, so the TTMM class is more akin to a Dictionary + a function to display a tooltip at a given Screen location)
Now, I have a bunch of classes PanelA, PanelB, PanelC,...PanelN. All the Panel classes that use tooltips (not all do) have the interface IUsesTooltip, with a displayTooltip(evt:MouseEvent) function defined within. What I do is at runtime, I go through all the panelA...N instances, and see if they implement IUsesTooltip, and if so, then I add an event listener to display a tooltip on mouseover the target.
It works fine, but I wonder if I'm making a few design mistakes here. I have a few options in front of me:
* Keep it the same. Each panel that wants to implement a tooltip can implement a displayTooltip(evt:MouseEvent) function, and either let the manager do the displaying, or display it themselves. The problem is that the displayTooltip function becomes public, and it shouldn't be, really (there aren't any protected interfaces in AS3 sadly.)
* Gut the manager class and make it a singleton as well. Whenever an object is added to the manager class, e.g. ToolTipMessageManager.set(object, "message") then I add a listener to that object (internally), and have the tooltip display every time the object is hovered over. This way, there's no (imo unnecessarily) public displayToolTip(evt:MouseEvent) function in each class that uses tooltips, nor any IUsesToolTip interface either.
* Any other option?
Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp
"Sit look rub panda" - Alan Davies