floating alerts
[potlatch2.git] / net / systemeD / controls / FloatingAlert.as
1 package net.systemeD.controls {
2
3         import flash.display.Sprite;
4         import flash.display.Stage;
5         import flash.events.*;
6         import flash.text.*;
7         import flash.utils.Timer;
8
9         public class FloatingAlert extends Sprite {
10
11                 private var textfield:TextField;
12                 private var h:int;
13                 private var w:int;
14                 private var timer:Timer;
15                 
16                 public function FloatingAlert(message:String) {
17                         super();
18
19                         textfield=new TextField();
20                         textfield.defaultTextFormat=new TextFormat("_sans", 15, 0xFFFFFF, true);
21                         textfield.autoSize=TextFieldAutoSize.LEFT;
22                         textfield.text=message;
23                         textfield.x=10;
24                         textfield.y=3;
25                         addChild(textfield);
26
27                         w=textfield.textWidth+20;
28                         h=textfield.textHeight+10;
29
30                         graphics.lineStyle(2,0);
31             graphics.beginFill(0x6495ED,100); 
32             graphics.drawRoundRect(0,0,w,h,10);
33             graphics.endFill();
34
35                         textfield.alpha=alpha=0;
36                         addEventListener(Event.ADDED_TO_STAGE, start);
37                 }
38                 
39                 private function start(event:Event):void {
40                         removeEventListener(Event.ADDED_TO_STAGE, start);
41                         x=(stage.stageWidth-w)/2;
42                         y=stage.stageHeight;
43
44                         timer=new Timer(20,30);
45                         timer.addEventListener(TimerEvent.TIMER, floatUpwards);
46                         timer.addEventListener(TimerEvent.TIMER_COMPLETE, floatFinished);
47                         timer.start();
48                 }
49                 
50                 private function floatUpwards(event:TimerEvent):void {
51                         y-=2;
52                         alpha+=0.035;
53                         textfield.alpha=alpha;
54                 }
55                 
56                 private function floatFinished(event:TimerEvent):void {
57                         timer.removeEventListener(TimerEvent.TIMER, floatUpwards);
58                         timer.removeEventListener(TimerEvent.TIMER_COMPLETE, floatFinished);
59                         stage.addEventListener(MouseEvent.MOUSE_DOWN, clear);
60                         stage.addEventListener(KeyboardEvent.KEY_DOWN, clear);
61                 }
62                 
63                 private function clear(event:Event):void {
64                         stage.removeEventListener(MouseEvent.MOUSE_DOWN, clear);
65                         stage.removeEventListener(KeyboardEvent.KEY_DOWN, clear);
66                         parent.removeChild(this);
67                 }
68         }
69 }