package de.nkunited.utils { import flash.display.BlendMode; import flash.display.DisplayObjectContainer; import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.TimerEvent; import flash.geom.ColorTransform; import flash.text.AntiAliasType; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.utils.Timer; import com.greensock.TweenLite; import com.greensock.easing.*; //import org.flashdevelop.utils.FlashConnect; /** * ... * @author Niklas Knaack * @version: 0.12 beta * @link: http://niklasknaack.blogspot.com * @link: http://www.nkunited.de */ public class ToolTip extends Sprite { /** * static const */ private static const MATH:Object = Math; /** * vars */ public var TWEEN_SPEED_IN:Number = .3;//.3 = 0,3 sec public var TWEEN_SPEED_OUT:Number = .15;//.1 = 0,1 sec public var TWEEN_EASING:Function = Linear.easeNone;// Quad.easeInOut; public var TIMER_DELAY:Number = 2000;//1000 = 1 sec public var TEXTFIELD_SHARPNESS:Number = 0;//-400 to 400 /** * vars */ private var toolTipStage:DisplayObjectContainer; private var toolTipAsset:MovieClip; private var toolTipDifX:int; private var toolTipDifY:int; private var toolTipAutoClose:Boolean; private var toolTipAnimated:Boolean; private var toolTipAlign:String; private var toolTipEnabled:Boolean; private var toolTipTimer:Timer; private var toolTipColorTransform:ColorTransform; private var toolTipAlignDif:Number; private var toolTipMovementDelay:Number; private var toolTipNewX:Number; private var toolTipNewY:Number; private var toolTipMask:Sprite; private var txtFormat:TextFormat; private var txtColor:Number; private var txtAlpha:Number; private var txtSize:Number; private var txtAntiAliasType:String; private var txtAnimationDir:Number; private var bgColor:Number; private var bgAlpha:Number; private var bgBitmapFilter:Array; private var bgBlendMode:String; /** * ToolTip * @param toolTipStage for example: this, type: DisplayObjectContainer * @param toolTipAsset for example: new ToolTipAsset(), type: MovieClip, must include TextField ( instance name: "textField" ) and MovieClip ( instance name: "bg" ) * @param toolTipDifX for example: 0 or x as int, type: int * @param toolTipDifY for example: 0 or x as int, type: int * @param toolTipAutoClose for example: true or false, type: Boolean * @param toolTipAnimated for example: true or false, type: Boolean * @param toolTipAlign for example: "Right", "Left", or "Center", type: String * @param txtColor for example: 0xFFFFFF, type: Number * @param txtAlpha for example: 1, type: Number * @param txtSize for example: 1, type: Number * @param txtAntiAliasType for example: AntiAliasType.ADVANCED, type: String, if font rendering method = Bitmap Text set AntiAliasType.NORMAL * @param bgColor for example: 0x000000, type: Number * @param bgAlpha for example: 1, type: Number * @param bgBitmapFilter for example: new Array( new DropShadowFilter(), new BlurFilter( 30,30 ) ),type: Array * @param bgBlendMode for example: BlendMode.INVERT,type: String * @param toolTipMovementDelay for example: 6,type: Number * @usage var toolTipinstance name:ToolTip = new ToolTip( this, new ToolTipAsset() ) // ToolTipAsset must include TextField ( instance name: "textField" ) and MovieClip ( instance name: "bg" ) * @usage var toolTipinstance name:ToolTip = new ToolTip( this, new ToolTipAsset(), 2, 2, true, true, "Right", 0xFFFFFF, 1, 11, "normal", NaN, 1, new Array( new DropShadowFilter() ) ) // ToolTipAsset must include TextField ( instance name: "textField" ) and MovieClip ( instance name: "bg" ) * @usage var toolTipinstance name:ToolTip = new ToolTip( this, new ToolTipAsset(), 5, 5, true, true, "Right", 0x00CCFF, 1, 11, "normal", 0x000000, 1, new Array( new DropShadowFilter(), new BlurFilter( 30,30 ) ) ) // ToolTipAsset must include TextField ( instance name: "textField" ) and MovieClip ( instance name: "bg" ) * @usage var toolTipinstance name:ToolTip = new ToolTip( this, new ToolTipAsset(), 5, 5, true, true, "Right", 0x000000, 1, 11, "normal", NaN, .75, new Array( new DropShadowFilter() ), BlendMode.INVERT ) // ToolTipAsset must include TextField ( instance name: "textField" ) and MovieClip ( instance name: "bg" ) */ public function ToolTip( toolTipStage:DisplayObjectContainer, toolTipAsset:MovieClip, toolTipDifX:int = 0, toolTipDifY:int = 0, toolTipAutoClose:Boolean = false, toolTipAnimated:Boolean = false, toolTipAlign:String = "Right", txtColor:Number = 0xFFFFFF, txtAlpha:Number = 1, txtSize:Number = 11, txtAntiAliasType:String = "normal", bgColor:Number = NaN, bgAlpha:Number = 1, bgBitmapFilter:Array = null, bgBlendMode:String = "", toolTipMovementDelay:Number = 1 ):void { this.toolTipStage = toolTipStage as DisplayObjectContainer; this.toolTipAsset = toolTipAsset as MovieClip; this.setLayout( toolTipDifX, toolTipDifY, toolTipAutoClose, toolTipAnimated, toolTipAlign, txtColor, txtAlpha, txtSize, txtAntiAliasType, bgColor, bgAlpha, bgBitmapFilter, bgBlendMode, toolTipMovementDelay ); this.toolTipAsset.mouseEnabled = false; this.toolTipAsset.mouseChildren = true; this.toolTipAsset.enabled = false; this.mouseEnabled = false; this.mouseChildren = false; this.toolTipAsset.name = "ToolTipAsset"; this.name = "ToolTip"; this.addChild( this.toolTipAsset ); this.toolTipEnabled = true; this.toolTipColorTransform = new ColorTransform; this.toolTipAlignDif = 0; this.toolTipTimer = new Timer( TIMER_DELAY, 0 ) this.toolTipTimer.addEventListener( TimerEvent.TIMER, this.onAutoClose, false, 0, true ); } /** * setLayout * @param toolTipDifX for example: 0 or x as int, type: int * @param toolTipDifY for example: 0 or x as int, type: int * @param toolTipAutoClose for example: true or false, type: Boolean * @param toolTipAnimated for example: true or false, type: Boolean * @param toolTipAlign for example: "Right", "Left", or "Center", type: String * @param txtColor for example: 0xFFFFFF, type: Number * @param txtAlpha for example: 1, type: Number * @param txtSize for example: 1, type: Number * @param txtAntiAliasType for example: AntiAliasType.NORMAL, type: String * @param bgColor for example: 0x000000, type: Number * @param bgAlpha for example: 1, type: Number * @param bgBitmapFilter for example: new Array( new DropShadowFilter(), new BlurFilter( 30,30 ) ),type: Array * @param bgBlendMode for example: BlendMode.INVERT,type: String * @param toolTipMovementDelay for example: 6,type: Number * @usage toolTipInstance.setLayout( 2, 2, true, true, "Right", 0xFFFFFF, 1, 11, "normal", Nan, 1, new Array( new DropShadowFilter() ) ); * @usage toolTipInstance.setLayout( 5, 5, true, true, "Right", 0x00CCFF, 1, 11, "normal", 0x000000, 1, new Array( new DropShadowFilter(), new BlurFilter( 30,30 ) ) ); * @usage toolTipInstance.setLayout( 5, 5, true, true, "Right", 0x000000, 1, 11, "normal", NaN, .75, new Array( new DropShadowFilter() ), BlendMode.INVERT ); */ public function setLayout( toolTipDifX:int = 0, toolTipDifY:int = 0, toolTipAutoClose:Boolean = false, toolTipAnimated:Boolean = false, toolTipAlign:String = "Right", txtColor:Number = 0xFFFFFF, txtAlpha:Number = 1, txtSize:Number = 11, txtAntiAliasType:String = "normal", bgColor:Number = NaN, bgAlpha:Number = 1, bgBitmapFilter:Array = null, bgBlendMode:String = "", toolTipMovementDelay:Number = 1 ):void { this.toolTipDifX = toolTipDifX; this.toolTipDifY = toolTipDifY * -1; this.toolTipAutoClose = toolTipAutoClose; this.toolTipAnimated = toolTipAnimated; this.toolTipAlign = toolTipAlign; this.txtColor = txtColor; this.txtAlpha = txtAlpha; this.txtSize = txtSize; this.txtAntiAliasType = txtAntiAliasType; this.bgColor = bgColor; this.bgAlpha = bgAlpha; this.bgBitmapFilter = bgBitmapFilter; this.bgBlendMode = bgBlendMode; this.toolTipMovementDelay = toolTipMovementDelay; this.txtFormat = new TextFormat(); this.txtFormat.color = this.txtColor; this.txtFormat.size = this.txtSize; this.txtFormat.kerning = true; this.setTextFormat( this.txtFormat ); this.toolTipAsset.textField.selectable = false; this.toolTipAsset.textField.autoSize = TextFieldAutoSize.LEFT; this.toolTipAsset.textField.antiAliasType = this.txtAntiAliasType; this.toolTipAsset.textField.sharpness = TEXTFIELD_SHARPNESS; this.toolTipAsset.textField.alpha = this.txtAlpha; this.toolTipAsset.textField.mouseEnabled = false; this.toolTipAsset.bg.alpha = this.bgAlpha; if ( this.bgColor ) { this.toolTipColorTransform.color = this.bgColor; this.toolTipAsset.bg.transform.colorTransform = this.toolTipColorTransform; } if ( this.bgBitmapFilter ) this.toolTipAsset.bg.filters = this.bgBitmapFilter; else this.toolTipAsset.bg.filters = new Array(); if ( this.bgBlendMode.length > 0 ) this.toolTipAsset.bg.blendMode = this.bgBlendMode; else this.toolTipAsset.bg.blendMode = BlendMode.NORMAL; } /** * setTextFormat * @param textFormat for example: ( null, 33, 0xFF0000, and so on ), type: TextFormat * @usage toolTipInstance.setTextFormat( null, 33, 0xFF0000, and so on ); * @usage toolTipInstance.setTextFormat( new TextFormat( new ToolTipFont().fontName, 33, 0xFF0000, and so on ) ); */ public function setTextFormat( textFormat:TextFormat ):void { this.txtFormat = textFormat; this.toolTipAsset.textField.defaultTextFormat = this.txtFormat; } /** * addTip * @param txt for example: "Hello World", type: String * @usage toolTipInstance.addTip( "Hello World" ); * @usage toolTipInstance.addTip( "Hello World", { width:50, speed:1 } ); */ public function addTip( txt:String, animatedText:Object = null ):void { if ( this.toolTipMask ) { this.toolTipAsset.mask = null; this.toolTipAsset.removeChild( this.toolTipMask ); this.toolTipMask = null; } if ( this.toolTipEnabled ) { if ( !animatedText ) { this.toolTipAsset.textField.x = 0; this.toolTipAsset.textField.text = txt; this.toolTipAsset.bg.width = MATH.round( this.toolTipAsset.textField.width + this.toolTipAsset.textField.x + 3 ); this.toolTipAsset.bg.height = MATH.round( this.toolTipAsset.textField.height ); this.toolTipAsset.bg.y = -this.toolTipAsset.bg.height; this.toolTipAsset.textField.y = this.toolTipAsset.bg.y + 1; } else { this.toolTipAsset.textField.x = 0; this.toolTipAsset.textField.text = txt; this.toolTipAsset.bg.width = MATH.round( animatedText.width + 3 ); this.toolTipAsset.bg.height = MATH.round( this.toolTipAsset.textField.height ); this.toolTipAsset.bg.y = -this.toolTipAsset.bg.height; this.toolTipAsset.textField.y = this.toolTipAsset.bg.y + 1; this.txtAnimationDir = animatedText.speed * -1; this.toolTipMask = new Sprite(); this.toolTipMask.graphics.beginFill( 0x000000 ); this.toolTipMask.graphics.lineStyle ( 0, 0x000000 ); this.toolTipMask.graphics.drawRect( 0, 0, this.toolTipAsset.bg.width, this.toolTipAsset.bg.height ); this.toolTipMask.graphics.endFill(); this.toolTipMask.y = -this.toolTipAsset.bg.height; this.toolTipAsset.addChild( this.toolTipMask ); this.toolTipAsset.mask = this.toolTipMask; } this.toolTipStage.addChild( this ); this.toolTipStage.addEventListener( MouseEvent.MOUSE_MOVE, this.onMouseMoveHandler, false, 0, true ); this.addEventListener( Event.ENTER_FRAME, this.onEnterFrameHandler, false, 0, true ); if ( this.toolTipAlign == "Left" ) { this.toolTipAlignDif = -this.toolTipAsset.bg.width; } else if ( this.toolTipAlign == "Center" ) { this.toolTipAlignDif = MATH.round( this.toolTipAsset.bg.width / 2 ) * -1; } else if ( this.toolTipAlign == "Right" ) { this.toolTipAlignDif = 0; } this.moveTip( this.toolTipStage.stage.mouseX, this.toolTipStage.stage.mouseY ); TweenLite.killTweensOf( this.toolTipAsset ); if ( this.toolTipAnimated ) { this.toolTipAsset.alpha = 0; TweenLite.to( this.toolTipAsset, TWEEN_SPEED_IN, { alpha:1, ease:TWEEN_EASING } ); } else { this.toolTipAsset.alpha = 1; } if ( this.toolTipTimer.running ) this.toolTipTimer.stop(); if ( this.toolTipAutoClose ) this.toolTipTimer.start(); } } /** * removeTip * @param * @usage toolTipInstance.removeTip(); */ public function removeTip():void { if ( this.toolTipAnimated ) { TweenLite.killTweensOf( this.toolTipAsset ); TweenLite.to( this.toolTipAsset, TWEEN_SPEED_OUT, { alpha:0, ease:TWEEN_EASING, onComplete:this.onRemoveTipComplete } ); } else { this.onRemoveTipComplete(); } } /** * onRemoveTipComplete * @param */ private function onRemoveTipComplete():void { if ( this.toolTipStage.getChildByName( this.name ) ) { this.toolTipStage.removeChild( this ); this.toolTipStage.removeEventListener( MouseEvent.MOUSE_MOVE, this.onMouseMoveHandler ); this.removeEventListener( Event.ENTER_FRAME, this.onEnterFrameHandler ); } if ( this.toolTipMask ) { this.toolTipAsset.mask = null; this.toolTipAsset.removeChild( this.toolTipMask ); this.toolTipMask = null; } } /** * moveTip * @param xPos for example: stage.mouseX, type: Number * @param yPos for example: stage.mouseY, type: Number */ private function moveTip( xPos:Number, yPos:Number ):void { var newX:Number = xPos; var newY:Number = yPos; var difX:int = this.toolTipDifX; var difY:int = this.toolTipDifY; var w:int = this.toolTipAsset.bg.width + difX; var h:int = newY - this.toolTipAsset.bg.height + difY; //vertical alignment if ( this.toolTipAlign == "Right" ) { if ( newX + w > this.toolTipStage.stage.stageWidth ) { newX -= this.toolTipAsset.bg.width; difX *= -1; } } if ( this.toolTipAlign == "Left" ) { if ( newX - w < 0 ) { newX += this.toolTipAsset.bg.width; difX *= -1; } newX += this.toolTipAlignDif - difX * 2; } if ( this.toolTipAlign == "Center" ) { if ( newX - w / 2 < 0 ) { newX += MATH.round( this.toolTipAsset.bg.width / 2 ) + difX; difX *= -1; } if ( newX + w / 2 > this.toolTipStage.stage.stageWidth ) { newX -= MATH.round( this.toolTipAsset.bg.width / 2 ) + difX; difX *= -1; } newX += this.toolTipAlignDif - difX; } //horizontal alignment if ( h < 0 ) { newY += this.toolTipAsset.bg.height; difY *= -1; } else if ( h > this.toolTipStage.stage.stageHeight ) { newY += this.toolTipAsset.bg.height; difY *= -1; } this.toolTipNewX = newX + difX; this.toolTipNewY = newY + difY; } /** * onMouseMoveHandler * @param e for example: MouseEvent, type: MouseEvent */ private function onMouseMoveHandler( e:MouseEvent ):void { this.moveTip( e.stageX, e.stageY ); } /** * onEnterFrameHandler * @param e for example: Event, type: Event */ private function onEnterFrameHandler( e:Event ):void { if ( this.toolTipMovementDelay > 1 ) { this.toolTipAsset.x += MATH.round( ( this.toolTipNewX - this.toolTipAsset.x ) / this.toolTipMovementDelay ); this.toolTipAsset.y += MATH.round( ( this.toolTipNewY - this.toolTipAsset.y ) / this.toolTipMovementDelay ); } else { this.toolTipAsset.x = MATH.round( this.toolTipNewX ); this.toolTipAsset.y = MATH.round( this.toolTipNewY ); } if ( this.toolTipMask ) { this.toolTipAsset.textField.x += this.txtAnimationDir; if ( this.toolTipAsset.textField.x + this.toolTipAsset.textField.width < this.toolTipAsset.bg.width ) { this.txtAnimationDir *= -1; } else if ( this.toolTipAsset.textField.x > 0 ) { this.txtAnimationDir *= -1; } } } /** * onAutoClose * @param e for example: TimerEvent, type: TimerEvent */ private function onAutoClose( e:TimerEvent ):void { this.toolTipTimer.stop(); this.toolTipTimer.reset(); this.removeTip(); } /** * enabled * @usage toolTipInstance.enabled = true/false; */ public function set enabled( value:Boolean ):void { this.toolTipEnabled = value; } /** * die * @param * @usage toolTipInstance.die(); */ public function die():void { if ( this.toolTipAnimated ) TweenLite.killTweensOf( this.toolTipAsset ); if ( this.toolTipTimer ) { this.toolTipTimer.stop(); this.toolTipTimer.reset(); this.toolTipTimer.removeEventListener( TimerEvent.TIMER, this.onAutoClose ); } if ( this.toolTipMask ) { this.toolTipAsset.mask = null; this.toolTipAsset.removeChild( this.toolTipMask ); this.toolTipMask = null; } if ( this.toolTipStage.getChildByName( this.name ) ) this.toolTipStage.removeChild( this ); if ( this.toolTipStage.hasEventListener( MouseEvent.MOUSE_MOVE ) ) this.toolTipStage.removeEventListener( MouseEvent.MOUSE_MOVE, this.onMouseMoveHandler ); if ( this.hasEventListener( Event.ENTER_FRAME ) ) this.removeEventListener( Event.ENTER_FRAME, this.onEnterFrameHandler ); this.enabled = false; this.txtFormat = null; this.removeChild( this.toolTipAsset ); this.toolTipAsset = null; this.toolTipStage = null; } } }