Merge branch 'master' of github.com:systemed/potlatch2
[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         /**
10         * A FloatingAlert displays a message and stays on screen until the user next presses the mouse or a key.
11         * It's an alternative to showing a modal dialog that would simply be dismissed. The alert appears at the bottom
12         * centre of the screen and fades in as it moves up into view.
13         */
14
15         public class FloatingAlert extends Sprite {
16
17                 private var textfield:TextField;
18                 private var h:int;
19                 private var w:int;
20                 private var timer:Timer;
21
22                 /**
23                 * Create a new FloatingAlert
24                 *
25                 * @param message The message to display to the user
26                 */
27                 public function FloatingAlert(message:String) {
28                         super();
29
30                         textfield=new TextField();
31                         textfield.defaultTextFormat=new TextFormat("_sans", 15, 0xFFFFFF, true);
32                         textfield.autoSize=TextFieldAutoSize.LEFT;
33                         textfield.text=message;
34                         textfield.x=10;
35                         textfield.y=3;
36                         addChild(textfield);
37
38                         w=textfield.textWidth+20;
39                         h=textfield.textHeight+10;
40
41                         graphics.lineStyle(2,0);
42             graphics.beginFill(0x6495ED,100); 
43             graphics.drawRoundRect(0,0,w,h,10);
44             graphics.endFill();
45
46                         textfield.alpha=alpha=0;
47                         addEventListener(Event.ADDED_TO_STAGE, start);
48                 }
49                 
50                 private function start(event:Event):void {
51                         removeEventListener(Event.ADDED_TO_STAGE, start);
52                         x=(stage.stageWidth-w)/2;
53                         y=stage.stageHeight;
54
55                         timer=new Timer(20,30);
56                         timer.addEventListener(TimerEvent.TIMER, floatUpwards);
57                         timer.addEventListener(TimerEvent.TIMER_COMPLETE, floatFinished);
58                         timer.start();
59                 }
60                 
61                 private function floatUpwards(event:TimerEvent):void {
62                         y-=2;
63                         alpha+=0.035;
64                         textfield.alpha=alpha;
65                 }
66                 
67                 private function floatFinished(event:TimerEvent):void {
68                         timer.removeEventListener(TimerEvent.TIMER, floatUpwards);
69                         timer.removeEventListener(TimerEvent.TIMER_COMPLETE, floatFinished);
70                         stage.addEventListener(MouseEvent.MOUSE_DOWN, clear);
71                         stage.addEventListener(KeyboardEvent.KEY_DOWN, clear);
72                 }
73                 
74                 private function clear(event:Event):void {
75                         stage.removeEventListener(MouseEvent.MOUSE_DOWN, clear);
76                         stage.removeEventListener(KeyboardEvent.KEY_DOWN, clear);
77                         parent.removeChild(this);
78                 }
79         }
80 }