Merge branch 'master' into history
[potlatch2.git] / net / systemeD / controls / Spinner.as
1 package net.systemeD.controls {
2         import flash.events.TimerEvent;
3         import flash.events.Event;
4         import flash.display.Sprite;
5         import flash.display.Shape;
6         import flash.utils.Timer;
7         import flash.filters.GlowFilter;
8         
9         // Adapted from http://www.stevensacks.net/2008/10/01/as3-apple-style-preloader/
10
11         public class Spinner extends Sprite {
12                 private var timer:Timer;
13                 private var slices:int;
14                 private var radius:int;
15
16                 public function Spinner(slices:int = 12, radius:int = 6) {
17                         super();
18                         this.slices = slices;
19                         this.radius = radius;
20                         draw();
21
22                         var filter:GlowFilter=new GlowFilter(0xFFFFFF,1,4,4,6);
23                         this.filters = [filter];
24                 }
25
26                 public function start():void {
27                         visible=true;
28                         if (timer) return;
29                         timer = new Timer(65);
30                         timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
31                         timer.start();
32                 }
33
34                 public function stop():void {
35                         visible=false;
36                         if (!timer) return;
37                         timer.reset();
38                         timer.removeEventListener(TimerEvent.TIMER, onTimer);
39                         timer = null;
40                 }
41
42                 private function onTimer(event:TimerEvent):void {
43                         rotation = (rotation + (360 / slices)) % 360;
44                 }
45                 
46                 private function draw():void {
47                         var i:int = slices;
48                         var degrees:int = 360 / slices;
49                         while (i--) {
50                                 var slice:Shape = getSlice();
51                                 slice.alpha = Math.max(0.2, 1 - (0.1 * i));
52                                 var radianAngle:Number = (degrees * i) * Math.PI / 180;
53                                 slice.rotation = -degrees * i;
54                                 slice.x = Math.sin(radianAngle) * radius;
55                                 slice.y = Math.cos(radianAngle) * radius;
56                                 addChild(slice);
57                         }
58                 }
59
60                 private function getSlice():Shape {
61                         var slice:Shape = new Shape();
62                         slice.graphics.beginFill(0x666666);
63                         slice.graphics.drawRoundRect(-1, 0, 2, 6, 12, 12);
64                         slice.graphics.endFill();
65                         return slice;
66                 }
67         }
68 }