1 package org
.sevenchan
.dongs
.ui
3 import flash
.display
.Sprite;
4 import flash
.events
.Event;
5 import flash
.events
.MouseEvent;
6 import flash
.text
.TextField;
7 import flash
.text
.TextFormat;
8 import org
.sevenchan
.dongs
.ui
.LCDProgressBar
;
9 import org
.sevenchan
.dongs
.ui
.TinyButton
;
14 public class Statistic
extends Sprite implements IHovertextRecipient
16 private var onValueChanged
:Function = null;
17 private var label
:String;
18 private var hovertext
:String;
19 private var max
:Number=100;
20 private var val
:Number = 0;
21 private var d_max
:Number = 0;
22 private var d_val
:Number = 0;
23 public var showMax
:Boolean = false;
24 private var shown_max
:Number = 0;
25 private var shown_val
:Number = 0;
26 private var step
:int = -1;
28 private var lcdProgress
:LCDProgressBar
;
29 private var txtLabel
:TextField;
30 private var btnAdd
:TinyButton
;
31 private var btnRemove
:TinyButton
;
33 private const goodColor
:uint = 0x006600;
34 private const normalColor
:uint = 0x000000;
35 private const badColor
:uint = 0x660000;
36 private const PROGRESS_HEIGHT
:Number = 15;
38 public var upIsGood
:Boolean = true;
42 public function Statistic
(label
:String,hovertext
:String,showMax
:Boolean=false,callback
:Function=null)
44 this.onValueChanged
= callback
;
45 this.showMax
= showMax
;
47 this.hovertext
= hovertext
;
48 txtLabel
= new TextField();
49 txtLabel
.htmlText
= label
+ ": -";
51 txtLabel
.x
= txtLabel
.y
= 0;
53 var tf
:TextFormat = new TextFormat();
56 tf
.color
= normalColor
;
57 txtLabel
.setTextFormat
(tf
);
58 txtLabel
.defaultTextFormat
= tf
;
59 txtLabel
.autoSize
= "left";
61 this.btnAdd
= new TinyButton
("+");
63 this.btnRemove
= new TinyButton
("-");
65 btnAdd
.addEventListener
(MouseEvent.CLICK
, this.addWhatever
);
66 btnRemove
.addEventListener
(MouseEvent.CLICK
, this.removeWhatever
);
67 btnAdd
.y
= btnRemove
.y
= 0;
68 btnRemove
.x
= 124 - btnRemove
.width
;
69 btnAdd
.x
= 124 - btnRemove
.width
- btnAdd
.width
- 3;
70 btnAdd
.bgColor
= 0x003300;
71 btnRemove
.bgColor
= 0x330000;
72 btnAdd
.alphaNormal
= btnRemove
.alphaNormal
= 0.25;
73 btnAdd
.alphaHover
= btnRemove
.alphaHover
= 0.90;
76 btnAdd
.alpha
= btnRemove
.alpha
= btnAdd
.alphaNormal
;
78 lcdProgress
= new LCDProgressBar
();
79 addChild
(lcdProgress
);
80 lcdProgress
.y
= txtLabel
.textHeight
+ 3;
82 lcdProgress
.draw
(0, 100, PROGRESS_HEIGHT
, 125);
84 showCheatButtons
(false);
87 private function addWhatever
(e
:MouseEvent):void {
88 this.setValue
(this.getValue
() + 5,true);
91 private function removeWhatever
(e
:MouseEvent):void {
92 this.setValue
(this.getValue
() - 5,true);
95 public function setHoverText
(hovertext
:String):void {
96 this.hovertext
= hovertext
;
99 public function getHoverText
():String {
100 return this.hovertext
;
103 public function showCheatButtons
(show
:Boolean):void {
104 this.btnAdd
.visible
= (onValueChanged
!= null && show
);
105 this.btnRemove
.visible
= (onValueChanged
!= null && show
);
108 public function setMax
(max
:Number, showAnimation
:Boolean = true):void {
114 doAnimation
(showAnimation
);
117 public function setValue
(val
:Number, showAnimation
:Boolean = true, notify
:Boolean=false):void {
124 if (notify
&& onValueChanged
!=null)
129 public function getValue
():Number {
133 public function setMaxAndValue
(max
:Number, val
:Number, showAnimation
:Boolean = true):void {
135 setValue
(val
, showAnimation
);
138 private function doAnimation
(showAnimation
:Boolean):void {
141 this.addEventListener
(Event.ENTER_FRAME
, this.frameListener
);
143 d_max
= shown_max
= max
;
144 d_val
= shown_val
= val
;
145 lcdProgress
.draw
(shown_val
,shown_max
, PROGRESS_HEIGHT
, 125);
146 txtLabel
.text
= label
+ ": " + shown_val
;
148 txtLabel
.appendText
("/" + shown_max
);
152 private function frameListener
(e
:Event):void {
156 this.removeEventListener
(Event.ENTER_FRAME
, this.frameListener
);
159 shown_max
= MathUtils
.lerp
((step
/ 100), d_max
, max
);
160 shown_val
= MathUtils
.lerp
((step
/ 100), d_val
, val
);
161 var origColor
:uint=normalColor
;
164 origColor
= goodColor
;
166 origColor
= badColor
;
167 } else if(max
< d_max
) {
169 origColor
= goodColor
;
171 origColor
= badColor
;
173 lcdProgress
.color
=MathUtils
.lerpColor
((step
/ 100), origColor
, normalColor
);
174 lcdProgress
.draw
(shown_val
, shown_max
,PROGRESS_HEIGHT
,125);
175 txtLabel
.text
= label
+ ": " + shown_val
;