|
-
stage and static, coding nasties?
Ok, here's an example of using the stage directly for a static (singleton for slackers) TootlTipManager class. The short story is ToolTipManager checks every MOUSE_OVER event target to see if it implements IToolTippable. It also listens for explicit TIP_ITEM_OVER, TIP_ITEM_OUT events which are used when the ITooltippable object does not directly dispatch mouse events, i.e. they are being forwarded by IToolTippable from a composit object.
ToolTipManager does not need to know anything about the document class, or the display hierarchy it just needs a reference to the stage so it can wait for IToolTippable objects. Atleast in my mind, there can only ever be one tooltip and it must aways be on top within the active display area, hence static and direct stage access.
It has been a few years since I wrote this and it has worked well with no modifications but I look at it now and then and think it is closer to procedural then OO. Do you think there is anything wrong with that, both in general and as implemented? Let me have it if you think it's just crap.
Actionscript Code:
package com.company.ui { import flash.text.*; import flash.display.*; import flash.events.*; import flash.utils.*; import fl.transitions.*; import fl.transitions.easing.*; public class ToolTipManager { static public var toolTip:ToolTip = new ToolTip(); static public var fadeTween:Tween = new Tween( toolTip, "alpha", None.easeNone, 0.0, 1.0, 0.25, true ); static public var timer:Timer = new Timer( 500 ); static public var defaultTipOffsetX:Number = 0; static public var defaultTipOffsetY:Number = 21; static public var xml:XMLList; static public const TIP_ITEM_OVER:String = "tipItemOver"; static public const TIP_ITEM_OUT:String = "tipItemOut"; static private var tipItem:IToolTippable; static private var stage:Stage; static private var _enabled:Boolean; static public function init( _stage:Stage, _xml:XMLList, state:Boolean = true ){ stage = _stage; xml = _xml; if( xml.hasOwnProperty( "@delay" ) ) timer.delay = Number( xml.@delay ); enabled = state; } static public function set enabled( state:Boolean ):void { _enabled = state; if( _enabled ){ timer.addEventListener( TimerEvent.TIMER, showToolTip ); stage.addEventListener( MouseEvent.MOUSE_OVER, onItemOver ); stage.addEventListener( MouseEvent.MOUSE_OUT, onItemOut ); stage.addEventListener( TIP_ITEM_OVER, onItemOver ); stage.addEventListener( TIP_ITEM_OUT, onItemOut ); }else{ timer.removeEventListener( TimerEvent.TIMER, showToolTip ); stage.removeEventListener( MouseEvent.MOUSE_OVER, onItemOver ); stage.removeEventListener( MouseEvent.MOUSE_OUT, onItemOut ); stage.removeEventListener( TIP_ITEM_OVER, onItemOver ); stage.removeEventListener( TIP_ITEM_OUT, onItemOut ); onItemOut( null ); } } static public function get enabled():Boolean { return _enabled; } static private function onItemOver( e:MouseEvent ):void { var newTipItem:IToolTippable = e.target as IToolTippable; if( !newTipItem ) return; if( newTipItem != tipItem && e.buttonDown ) return; tipItem = newTipItem; stage.addEventListener( MouseEvent.MOUSE_MOVE, restartTimer ); stage.addEventListener( MouseEvent.MOUSE_DOWN, restartTimer ); stage.addEventListener( MouseEvent.MOUSE_UP, restartTimer ); timer.start(); } static private function onItemOut( e:Event ):void { stage.removeEventListener( MouseEvent.MOUSE_MOVE, restartTimer ); stage.removeEventListener( MouseEvent.MOUSE_DOWN, restartTimer ); stage.removeEventListener( MouseEvent.MOUSE_UP, restartTimer ); hideToolTip(); } static private function restartTimer( e:MouseEvent ):void { hideToolTip(); timer.start(); } static private function showToolTip( e:TimerEvent ):void { var tipDisplay:DisplayObject = tipItem as DisplayObject; if( !tipDisplay.stage || !tipDisplay.hitTestPoint( tipDisplay.stage.mouseX, tipDisplay.stage.mouseY, true ) ){ onItemOut( e ); return; } timer.stop(); var tipName:String = tipItem.tipName ? tipItem.tipName : getQualifiedClassName( tipItem ).split( "::" ).pop(); var tipString:String = tipItem.constructTip( xml.child( tipName ) ); if( !tipString ) return; toolTip.setText( tipString ); var toolTipLeft:Number = isNaN( tipItem.tipOffsetX ) ? defaultTipOffsetX : tipItem.tipOffsetX; toolTipLeft += stage.mouseX; var toolTipTop:Number = isNaN( tipItem.tipOffsetY ) ? defaultTipOffsetY : tipItem.tipOffsetY; toolTipTop += stage.mouseY; var toolTipRight:int = toolTipLeft + toolTip.width; var toolTipBottom:int = toolTipTop + toolTip.height; toolTip.x = Math.max( 0, toolTipRight < stage.stageWidth ? toolTipLeft : stage.stageWidth - toolTip.width ); toolTip.y = Math.max( 0, toolTipBottom < stage.stageHeight ? toolTipTop : stage.mouseY - toolTip.height ); fadeTween.start(); stage.addChild( toolTip ); } static private function hideToolTip():void { timer.reset(); if( toolTip.parent ) toolTip.parent.removeChild( toolTip ); } } }
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|