Saturday, November 3, 2012

Creating EasyTooltip class: Part 2

Today we'll start creating the base of our class.

First thing we'll do is add 2 new variables - private "par" and public "listeners".

private var par:DisplayObject;
public var listeners:Array;

Receive a new parameter in the constructor - parent. This will be a reference to the object which is the parent of all the listeners. By listeners I mean the object, which will invoke tooltips on roll over. This parent entity is needed for a number of reasons - the tooltips will be added to it, its width and height will be considered the boundaries that the tooltips may not cross, mouse coordinates will be calculated inside this object only.

/**
 * Create a Tooltip manager. Needed for creating and managing tooltips.
 * @paramparent Reference to the parent of tooltips - the container, which will contain the objects that will be rolled over.
 */

public function EasyTooltip(parent:DisplayObject) 
{
par = parent;
listeners = [];
trace("Tooltip manager created!");
}

Now let's create a new class Tooltip. This will be the class of the objects that will later actually be displayed to the user - these are the boxes that will contain the text.

For now, just let it store a public msg value, and receive a message String in the constructor:

package com.kircode.EasyTooltip 
{
import flash.display.MovieClip;
/**
 * Basic tooltip.
 * @author Kirill Poletaev
 */
public class Tooltip extends MovieClip
{
public var msg:String;

public function Tooltip(message:String) 
{
msg = message;
}

}

}

Now let's go back to EasyTooltip.as and add a function addListener(). Add 2 parameters - listener and tooltip. Make it return a Tooltip object. Inside of it, create a Tooltip object with the provided message text and return this Tooltip:

/**
 * Add a Tooltip listener.
 * @paramlistener Object, which invokes the tooltip on roll over.
 * @paramtooltip Message to be displayed.
 * @returnNewly created Tooltip object. Can be used to dynamically change its properties in real-time.
 */

public function addListener(listener:DisplayObject, tooltip:String):Tooltip {
var tip:Tooltip = new Tooltip(tooltip);
return tip;
}

Full EasyTooltip.as code so far:

package com.kircode.EasyTooltip 
{
import flash.display.DisplayObject;
/**
 * Utility for creation of tooltips.
 * @author Kirill Poletaev
 */
public class EasyTooltip 
{

private var par:DisplayObject;
public var listeners:Array;

/**
 * Create a Tooltip manager. Needed for creating and managing tooltips.
 * @paramparent Reference to the parent of tooltips - the container, which will contain the objects that will be rolled over.
 */

public function EasyTooltip(parent:DisplayObject) 
{
par = parent;
listeners = [];
trace("Tooltip manager created!");
}

/**
 * Add a Tooltip listener.
 * @paramlistener Object, which invokes the tooltip on roll over.
 * @paramtooltip Message to be displayed.
 * @returnNewly created Tooltip object. Can be used to dynamically change its properties in real-time.
 */

public function addListener(listener:DisplayObject, tooltip:String):Tooltip {
var tip:Tooltip = new Tooltip(tooltip);
return tip;
}

}

}

Now, back in the example project, you can create a display object, such as MovieClip, call it something like "object1" and set it as a listener for a tooltip. In this case, I create a MovieClip in Flash IDE and place it on stage, and set it as a listener using this code in main.as:

package  
{
import com.kircode.EasyTooltip.EasyTooltip;
import flash.display.MovieClip;
import flash.events.Event;
/**
 * Example of EasyTooltip library usage.
 * @author Kirill Poletaev
 */
public class main extends MovieClip
{
var tooltip:EasyTooltip;

public function main() 
{
addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(evt:Event):void {
tooltip = new EasyTooltip(stage);
tooltip.addListener(object1, "This is an object.");
}

}

}

It doesn't do much yet, but it's a start.

Thanks for reading!

No comments:

Post a Comment