Fix hair, dick around with new game screen.
[18plus-7leafadventure.git] / src / org / sevenchan / dongs / ui / Statistic.as
blob16e865e41784d43a15717874916ab60c22d0b98d
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;
10 /**
11 * ...
12 * @author Harbinger
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;
40 // Blah: 100 (+)(-)
41 // [================]
42 public function Statistic(label:String,hovertext:String,showMax:Boolean=false,callback:Function=null)
44 this.onValueChanged = callback;
45 this.showMax = showMax;
46 this.label = label;
47 this.hovertext = hovertext;
48 txtLabel = new TextField();
49 txtLabel.htmlText = label + ": -";
50 addChild(txtLabel);
51 txtLabel.x = txtLabel.y = 0;
53 var tf:TextFormat = new TextFormat();
54 tf.font = "Verdana";
55 tf.bold = true;
56 tf.color = normalColor;
57 txtLabel.setTextFormat(tf);
58 txtLabel.defaultTextFormat = tf;
59 txtLabel.autoSize = "left";
61 this.btnAdd = new TinyButton("+");
62 addChild(btnAdd);
63 this.btnRemove = new TinyButton("-");
64 addChild(btnRemove);
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;
74 btnAdd.draw();
75 btnRemove.draw();
76 btnAdd.alpha = btnRemove.alpha = btnAdd.alphaNormal;
78 lcdProgress = new LCDProgressBar();
79 addChild(lcdProgress);
80 lcdProgress.y = txtLabel.textHeight + 3;
81 lcdProgress.x = 0;
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 {
109 this.max = max;
111 if (this.val > max)
112 this.val = max;
114 doAnimation(showAnimation);
117 public function setValue(val:Number, showAnimation:Boolean = true, notify:Boolean=false):void {
118 //trace(val);
119 this.val = val;
120 if (this.val > max)
121 this.val = max;
122 if (this.val < 0)
123 this.val = 0;
124 if (notify && onValueChanged!=null)
125 onValueChanged(val);
126 doAnimation(false);
129 public function getValue():Number {
130 return val;
133 public function setMaxAndValue(max:Number, val:Number, showAnimation:Boolean = true):void {
134 setMax(max, false);
135 setValue(val, showAnimation);
138 private function doAnimation(showAnimation:Boolean):void {
139 if (showAnimation) {
140 step = 0;
141 this.addEventListener(Event.ENTER_FRAME, this.frameListener);
142 } else {
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;
147 if (showMax)
148 txtLabel.appendText("/" + shown_max);
152 private function frameListener(e:Event):void {
153 return;
154 if (step == 100) {
155 step = -1;
156 this.removeEventListener(Event.ENTER_FRAME, this.frameListener);
157 doAnimation(false);
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;
162 if (max > d_max) {
163 if(upIsGood)
164 origColor = goodColor;
165 else
166 origColor = badColor;
167 } else if(max < d_max) {
168 if(!upIsGood)
169 origColor = goodColor;
170 else
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;