package com.oxylusflash.book { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.TimerEvent; import flash.utils.Timer; public class ObjectWithTooltip extends Sprite { private var _tooltip:String; protected var tooltipMc:Tooltip; private var tipShowTimer:Timer; private var tipHideTimer:Timer; protected var tipYOffset:Number = 0; protected var hideTipEvent:String = MouseEvent.MOUSE_DOWN; protected var LibTooltipClass:Class = LibTooltip; public function ObjectWithTooltip() { } /** * Tooltip. */ public function get tooltip():String { return _tooltip; } public function set tooltip(value:String):void { if (_tooltip != value) { _tooltip = value; if (tooltipMc == null) { tooltipMc = new LibTooltipClass; this.addChild(tooltipMc); initTooltipPos(); this.addEventListener(MouseEvent.ROLL_OVER, showTooltip, false, 0, true); this.addEventListener(MouseEvent.ROLL_OUT, hideTooltip, false, 0, true); this.addEventListener(MouseEvent.MOUSE_DOWN, hideTooltip, false, 0, true); tipShowTimer = new Timer(400, 1); tipShowTimer.addEventListener(TimerEvent.TIMER_COMPLETE, tipShowTimer_timerCompleteHandler, false, 0, true); tipHideTimer = new Timer(3000, 1); tipHideTimer.addEventListener(TimerEvent.TIMER_COMPLETE, tipHideTimer_timerCompleteHandler, false, 0, true); } tooltipMc.tip = _tooltip; } } /** * Init tooltip position. */ protected function initTooltipPos():void { tooltipMc.x = Math.round(this.width * 0.5); tooltipMc.y = tipYOffset; } /** * Tip show/hide timers event handlers. * @param e */ private function tipShowTimer_timerCompleteHandler(e:TimerEvent):void { tooltipMc.show(); tipHideTimer.start(); } private function tipHideTimer_timerCompleteHandler(e:TimerEvent):void { tooltipMc.hide(); } /** * Show / hide tooltip. */ protected function showTooltip(e:MouseEvent):void { if (!e.buttonDown) { tipShowTimer.reset(); tipHideTimer.reset(); tipShowTimer.start(); } } private function hideTooltip(e:MouseEvent):void { tipShowTimer.reset(); tipHideTimer.reset(); tooltipMc.hide(); } } }