/* * * PixelTIp.as * author: Alan Shaw * http://nodename.com * */ import ascb.util.Proxy; import flash.display.BitmapData; class com.nodename.utils.PixelTip { static var version:String = "$Id: PixelTip.as,v 1.5 2006/03/26 20:09:15 ashaw Exp $"; public static function setBoxSize(wd:Number, ht:Number):Void { PixelTip._boxWidth = wd; PixelTip._boxHeight = ht; } public static function showHex(yesno:Boolean):Void { PixelTip._hex = yesno; } public static function showDecimal(yesno:Boolean):Void { PixelTip._decimal = yesno; } /* * * @function register * @param tipTarget the MovieClip over which the tooltip is to be shown * */ public static function register(tipTarget:MovieClip):Void { // fill something in the pixel at (0, 0) to anchor the pixel coordinates: with (tipTarget) { // a transparent black pixel: beginFill(0, 0); moveTo(0, 0); lineTo(1, 0); lineTo(1, 1); lineTo(0, 1); lineTo(0, 0); endFill(); } tipTarget.onRollOver = Proxy.create(PixelTip, PixelTip.showPixelTip, tipTarget); tipTarget.onRollOut = tipTarget.onDragOut = PixelTip.hidePixelTip; } ///////////////////////////////////////// // // // End of public interface // // // ///////////////////////////////////////// private static var theOnlyPixelTip:PixelTip; private static function showPixelTip(tipTarget:MovieClip):Void { if (theOnlyPixelTip == undefined) { theOnlyPixelTip = new PixelTip(); } theOnlyPixelTip.p_showPixelTip(tipTarget); } private static function hidePixelTip():Void { if (theOnlyPixelTip == undefined) { theOnlyPixelTip = new PixelTip(); } theOnlyPixelTip.p_hidePixelTip(); } private var _mc:MovieClip; private var _tf:TextField; private var _tFormat:TextFormat; private static var _boxWidth:Number = 150; private static var _boxHeight:Number = 50; private var _tailLeft:Number; // distance from left edge of box to left edge of tail private var _tailWidth:Number; private var _tailHeight:Number; private var _tailTipOffset:Number; // tip of tail is this far above registration point private var _textSideMargin:Number; private var topY:Number; private var leftX:Number; private function PixelTip() { _tailLeft = .40 * PixelTip._boxHeight; _tailWidth = .40 * PixelTip._boxHeight; _tailHeight = .60 * PixelTip._boxHeight; _tailTipOffset = 5; _textSideMargin = 5; var trueParent:MovieClip = _level0; // it has to live somewhere... this._mc = trueParent.createEmptyMovieClip("nodenamePixelTip", trueParent.getNextHighestDepth()); this._mc._visible = false; leftX = -this._tailLeft; topY = -PixelTip._boxHeight - this._tailHeight - this._tailTipOffset; this.draw(leftX, topY, PixelTip._boxWidth, PixelTip._boxHeight, this._tailWidth, this._tailTipOffset); this.makeTextField(leftX, topY, PixelTip._boxWidth, PixelTip._boxHeight); } private function draw(leftX:Number, topY:Number, boxWidth:Number, boxHeight:Number, tailWidth:Number, tailTipOffset:Number):Void { // TODO: make rounded corners var rightX:Number = leftX + boxWidth; var bottomY:Number = topY + boxHeight; with (this._mc) { moveTo(leftX, topY); lineStyle(1, 0x000000, 100); // black beginGradientFill( "linear", [0xfff3d9, 0xfffbf2], // colors [100, 100], // alphas [0, 255], // ratios { matrixType:"box", x: leftX, y: topY, w: boxWidth, h: boxHeight, r: Math.PI/2 // 90 degrees: vertical gradient } ); lineTo(rightX, topY); lineTo(rightX, bottomY); lineTo(tailWidth, bottomY); lineTo(0, -tailTipOffset); // tip of tail lineTo(0, bottomY); lineTo(leftX, bottomY); lineTo(leftX, topY); endFill(); } } private function makeTextField(leftX:Number, topY:Number, boxWidth:Number, boxHeight:Number):Void { var pointSize:Number = boxWidth/8; this._mc.createTextField("_tf", this._mc.getNextHighestDepth(), leftX, topY, boxWidth, boxHeight); this._tf = this._mc._tf; this._tFormat = new TextFormat("_sans", pointSize, 0x000000); this._tFormat.align = "center"; this._tFormat.leftMargin = this._tFormat.rightMargin = this._textSideMargin; this._tf.setNewTextFormat(this._tFormat); this._tf.wordWrap = true; this._tf.autoSize = "none"; } private var _toolTipIntervalID:Number; private var _toolTipDelay:Number = 500; // milliseconds private function p_showPixelTip(tipTarget:MovieClip):Void { this._toolTipIntervalID = setInterval(this, "reallyShowPixelTip", this._toolTipDelay, tipTarget); } private var _bmp:BitmapData; private function reallyShowPixelTip(tipTarget:MovieClip):Void { clearInterval(this._toolTipIntervalID); this._bmp = new BitmapData(tipTarget._width, tipTarget._height, false, 0); this._bmp.draw(tipTarget); updatePixelTip(tipTarget); this._mc._visible = true; this._mc.onMouseMove = Proxy.create(this, updatePixelTip, tipTarget); } private static var _decimal:Boolean = true; private static var _hex:Boolean = false; private function updatePixelTip(tipTarget:MovieClip):Void { setFlip(); this._mc._x = this._mc._parent._xmouse; this._mc._y = this._mc._parent._ymouse; var loc:Object = {x: Math.floor(this._mc._x), y: Math.floor(this._mc._y)}; tipTarget.globalToLocal(loc); var color:Number = this._bmp.getPixel(loc.x, loc.y); this._tf.text = loc.x + ", " + loc.y + (PixelTip._decimal ? "\n" + PixelTip.triplet(color) : "") + (PixelTip._hex ? "\n" + PixelTip.colorVal(color, 6) : ""); updateAfterEvent(); } private function setFlip():Void { var loc:Object = {x: this._mc._xmouse, y: this._mc._ymouse}; this._mc.localToGlobal(loc); if (loc.y + topY < 5) { flipY(); } else { unflipY(); } if (loc.x + leftX + PixelTip._boxWidth > Stage.width - 5) { flipX(); } else { unflipX(); } } private function flipY():Void { this._mc._yscale = -100; this._tf._yscale = -100; this._tf._y = topY + PixelTip._boxHeight; } private function unflipY():Void { this._mc._yscale = 100; this._tf._yscale = 100; this._tf._y = topY; } private function flipX():Void { this._mc._xscale = -100; this._tf._xscale = -100; this._tf._x = leftX + PixelTip._boxWidth; } private function unflipX():Void { this._mc._xscale = 100; this._tf._xscale = 100; this._tf._x = leftX; } private static function triplet(color:Number):String { var r = color >> 16 & 0xff; var g = color >> 8 & 0xff; var b = color & 0xff; return ("" + r + " " + g + " " + b); } private static function colorVal(color:Number, size:Number):String { var str:String = "00000" + color.toString(16).toUpperCase(); return ("0x" + str.substr(-size)); } private function p_hidePixelTip():Void { clearInterval(this._toolTipIntervalID); this._mc._visible = false; delete this._mc.onMouseMove; this._bmp.dispose(); } }