Created
February 18, 2011 14:14
-
-
Save lionelB/833696 to your computer and use it in GitHub Desktop.
A simple osx-like activity indicator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ui.utils | |
{ | |
import flash.display.CapsStyle; | |
import flash.display.LineScaleMode; | |
import flash.display.Shape; | |
import flash.events.Event; | |
import flash.utils.clearInterval; | |
import flash.utils.getTimer; | |
import flash.utils.setInterval; | |
import mx.states.AddChild; | |
public class ActivityIndicator extends Shape | |
{ | |
private var _leafSize:Number = 4; | |
private var _color:Number = 0xFFFFFF; //0x808080 | |
private var _minOpacity:Number = .15; | |
private var _nbLeaf:Number = 12; | |
private var _trailSize:Number = 5; | |
private var _radius:Number = 20; | |
private var _innerRadius:Number = 9; | |
private var _speed:int = 40; | |
private var interval:uint; | |
private var offset:int=3; | |
private var _padding:int = 5 | |
public function ActivityIndicator() | |
{ | |
super(); | |
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); | |
addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler); | |
} | |
protected function draw(d:int=0):void | |
{ | |
graphics.clear(); | |
graphics.beginFill(0x000000,.7); | |
graphics.drawRoundRect(0,0,(_padding + _radius)*2,(_padding+_radius)*2,8); | |
graphics.endFill(); | |
const op:Number = (1-_minOpacity)/_trailSize; | |
for(var i:int; i < _nbLeaf; ++i) | |
{ | |
var p:int = (i+d+1)%_nbLeaf; | |
var o:Number = (i >= _nbLeaf - _trailSize)? (op*(i+_trailSize-_nbLeaf))+_minOpacity : _minOpacity; | |
drawLeafStub( Math.cos( p*2*Math.PI/_nbLeaf) * _innerRadius, | |
Math.sin( p*2*Math.PI/_nbLeaf) * _innerRadius, | |
o); | |
} | |
} | |
protected function drawLeafStub (x:Number, y:Number, a:Number):void | |
{ | |
graphics.lineStyle(_leafSize, _color, a, true, LineScaleMode.NONE, CapsStyle.NONE ); | |
graphics.moveTo(_radius + _padding + x, _radius + _padding+ y); | |
graphics.lineTo(_radius+ _padding + x*((_radius)/_innerRadius),_radius + _padding+ y*((_radius)/_innerRadius)); | |
} | |
protected function clear():void | |
{ | |
graphics.clear(); | |
} | |
protected function update():void | |
{ | |
offset %= _nbLeaf; | |
draw(offset); | |
offset++; | |
} | |
protected function addedToStageHandler(event:Event):void | |
{ | |
if (interval == 0) | |
{ | |
interval = setInterval(update,_speed); | |
} | |
} | |
protected function removedFromStageHandler(event:Event):void | |
{ | |
clearInterval(interval); | |
interval = 0; | |
clear(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment