├── .actionScriptProperties
├── .project
├── .settings
└── org.eclipse.core.resources.prefs
├── 123.p12
├── README.md
├── bin-debug
└── AiEditor-app.xml
├── lib
└── Greensock.swc
├── psd
├── alert.png
├── alert.psd
├── boss.png
├── boss.psd
├── leaf.png
├── leaf.psd
├── player.png
├── player.psd
├── trunk.png
├── trunk.psd
├── what.png
└── what.psd
├── src
├── AIBrowser.as
├── AiClientTest.as
├── AiEditor-app.xml
├── AiEditor.as
├── Alert.as
├── AlertInput.as
├── AppBG.as
├── AppMover.as
├── Arrow.as
├── Box.as
├── ColorUtil.as
├── DirList.as
├── DragBorder.as
├── DragSprite.as
├── FileX.as
├── FlashBox.as
├── InRect.as
├── LableX.as
├── ListItemX.as
├── Node.as
├── NodeContainer.as
├── ServerState.as
├── com
│ └── bit101
│ │ ├── charts
│ │ ├── BarChart.as
│ │ ├── Chart.as
│ │ ├── LineChart.as
│ │ └── PieChart.as
│ │ ├── components
│ │ ├── Accordion.as
│ │ ├── Calendar.as
│ │ ├── CheckBox.as
│ │ ├── ColorChooser.as
│ │ ├── ComboBox.as
│ │ ├── Component.as
│ │ ├── FPSMeter.as
│ │ ├── HBox.as
│ │ ├── HRangeSlider.as
│ │ ├── HScrollBar.as
│ │ ├── HSlider.as
│ │ ├── HUISlider.as
│ │ ├── IndicatorLight.as
│ │ ├── InputText.as
│ │ ├── Knob.as
│ │ ├── Label.as
│ │ ├── List.as
│ │ ├── ListItem.as
│ │ ├── Meter.as
│ │ ├── NumericStepper.as
│ │ ├── Panel.as
│ │ ├── ProgressBar.as
│ │ ├── PushButton.as
│ │ ├── RadioButton.as
│ │ ├── RangeSlider.as
│ │ ├── RotarySelector.as
│ │ ├── ScrollBar.as
│ │ ├── ScrollPane.as
│ │ ├── Slider.as
│ │ ├── Style.as
│ │ ├── Text.as
│ │ ├── TextArea.as
│ │ ├── UISlider.as
│ │ ├── VBox.as
│ │ ├── VRangeSlider.as
│ │ ├── VScrollBar.as
│ │ ├── VSlider.as
│ │ ├── VUISlider.as
│ │ ├── WheelMenu.as
│ │ └── Window.as
│ │ └── utils
│ │ └── MinimalConfigurator.as
├── comm
│ └── EventData.as
├── net
│ ├── SimpleSocketClient.as
│ ├── SimpleSocketMessageEvent.as
│ └── SimpleSocketServer.as
├── props
│ ├── PropertyPanel.as
│ ├── WindowX.as
│ ├── comm
│ │ ├── PBoolean.as
│ │ ├── PBtn.as
│ │ ├── PNumber.as
│ │ ├── PSpace.as
│ │ ├── PString.as
│ │ └── PStringComboBox.as
│ └── noEvent
│ │ ├── InputTextAreaNoEvent.as
│ │ ├── InputTextNoEvent.as
│ │ └── comboBoxNoEvent.as
└── tryai
│ ├── ActorMgr.as
│ ├── AiUpdateMgr.as
│ ├── ai
│ ├── AINode.as
│ ├── God.as
│ ├── Leaf.as
│ └── Select.as
│ └── smlgame
│ ├── ActorData.as
│ ├── EffectLayer.as
│ ├── RenderOB.as
│ ├── Scene.as
│ └── SmlGameSetupWin.as
└── 参考ai.gif
/.actionScriptProperties:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | AiEditor
4 |
5 |
6 |
7 |
8 |
9 | com.adobe.flexbuilder.project.flexbuilder
10 |
11 |
12 |
13 |
14 | com.adobe.flexbuilder.project.apollobuilder
15 |
16 |
17 |
18 |
19 |
20 | com.adobe.flexbuilder.project.apollonature
21 | com.adobe.flexbuilder.project.actionscriptnature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | #Wed Aug 20 17:49:24 CST 2014
2 | eclipse.preferences.version=1
3 | encoding/=utf-8
4 |
--------------------------------------------------------------------------------
/123.p12:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/123.p12
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # BehaviorTreeEditor
2 |
3 | 演示视频:
4 |
5 | 行为树 AI 的真实运作过程:
6 |
7 | https://www.bilibili.com/video/av44645818
8 |
9 |
10 |
11 | 行为树 AI 的编辑和节点连接:
12 |
13 | https://www.bilibili.com/video/av44645517
14 |
15 |
16 |
17 |
18 |
19 | 在线效果预览:
20 |
21 | http://poerlang.com/ai/
22 |
--------------------------------------------------------------------------------
/lib/Greensock.swc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/lib/Greensock.swc
--------------------------------------------------------------------------------
/psd/alert.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/alert.png
--------------------------------------------------------------------------------
/psd/alert.psd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/alert.psd
--------------------------------------------------------------------------------
/psd/boss.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/boss.png
--------------------------------------------------------------------------------
/psd/boss.psd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/boss.psd
--------------------------------------------------------------------------------
/psd/leaf.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/leaf.png
--------------------------------------------------------------------------------
/psd/leaf.psd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/leaf.psd
--------------------------------------------------------------------------------
/psd/player.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/player.png
--------------------------------------------------------------------------------
/psd/player.psd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/player.psd
--------------------------------------------------------------------------------
/psd/trunk.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/trunk.png
--------------------------------------------------------------------------------
/psd/trunk.psd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/trunk.psd
--------------------------------------------------------------------------------
/psd/what.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/what.png
--------------------------------------------------------------------------------
/psd/what.psd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/psd/what.psd
--------------------------------------------------------------------------------
/src/AIBrowser.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import com.bit101.components.HBox;
4 | import com.bit101.components.PushButton;
5 | import com.bit101.components.ScrollPane;
6 | import com.bit101.components.VBox;
7 |
8 | import flash.display.DisplayObjectContainer;
9 | import flash.events.Event;
10 | import flash.filesystem.File;
11 | import flash.filesystem.FileMode;
12 | import flash.filesystem.FileStream;
13 | import props.WindowX;
14 |
15 | public class AIBrowser extends WindowX
16 | {
17 | private var path:String;
18 |
19 | private var html:VBox;
20 | private var menu:HBox;
21 | private var body:VBox;
22 |
23 | private var dir:File;
24 | public static var one:AIBrowser;
25 | public function AIBrowser(parent:DisplayObjectContainer=null,title:String="Window")
26 | {
27 | one = this;
28 | super(parent,"AIBrowser",976,268);
29 | html = new VBox(this,2,2);
30 | menu = new HBox(html,2,2);
31 | var scroll:ScrollPane = new ScrollPane(html); scroll.width = ServerState.one.width-5; scroll.height=400-50; scroll.autoHideScrollBar = true;
32 | body = new VBox(scroll,2,2); body.spacing = 0;
33 |
34 | new PushButton(menu,0,0,"新建",onNew);
35 | new PushButton(menu,0,0,"删除",onDel);
36 |
37 | width = ServerState.one.width;
38 | height = 400;
39 | this.addEventListener(Event.SELECT,onFileSel);
40 | }
41 |
42 | private function onDel(e:*):void
43 | {
44 | NodeContainer.one.clear();
45 | clear(true);
46 | }
47 |
48 | private function onNew(e:*):void
49 | {
50 | AlertInput.show("请填写文件名,如“19.ai”或者“19”,后缀名可略 ",function(fileName:String):void{
51 | fileName = fileName.replace(".ai","");
52 | AlertInput.show("请填写描述,如 “XXXBoss的Ai,70%以下血时暴走”",function(descStr:String):void{
53 | var root:Object = getNewNodeOb();
54 | root.desc = descStr;
55 |
56 | NodeContainer.one.clear();
57 | save(root,fileName);
58 | refresh();
59 | });
60 | });
61 | }
62 |
63 | public function refresh():void
64 | {
65 | clear();
66 | show(path);
67 | sel(lastFileName);
68 | }
69 |
70 | private function sel(fileName:String):void
71 | {
72 | if(fileName=="") return;
73 | for (var j:int = 0; j < body.numChildren; j++) {
74 | var ff:FileX = body.getChildAt(j) as FileX;
75 | if(ff.f.name == fileName){
76 | ff.selected = true;
77 | showAiTree(ff);
78 | }
79 | }
80 |
81 | }
82 |
83 | private function clear(delSel:Boolean=false):void
84 | {
85 | if(delSel){
86 | for (var j:int = 0; j < body.numChildren; j++) {
87 | var ff:FileX = body.getChildAt(j) as FileX;
88 | if(ff.selected){
89 | ff.dispose(true);
90 | return;
91 | }
92 | }
93 | }
94 | var len:int = body.numChildren;
95 | for (var i:int = 0; i < len; i++) {
96 | var f:FileX = body.getChildAt(0) as FileX;
97 | if(f.selected)f.selected=false;
98 | f.dispose();
99 | }
100 | }
101 |
102 | private function save(root:Object, fileName:String):void
103 | {
104 | try{
105 | var s:String = JSON.stringify(root);
106 | if(!dir.exists){
107 | Alert.show("目录"+dir+"不存在");return;
108 | }
109 | var f:File = dir.resolvePath(fileName+".ai");
110 | var fs:FileStream = new FileStream();
111 | fs.open(f,FileMode.WRITE);
112 | fs.writeUTFBytes(s);
113 | fs.close();
114 | }catch(e:Error) {
115 | Alert.show("保存时出错:"+e.message);
116 | }
117 | }
118 |
119 | protected function onFileSel(e:Event):void
120 | {
121 | var fx:FileX = e.target as FileX;
122 | if(!fx) return;
123 | showAiTree(fx);
124 | }
125 |
126 | private function showAiTree(fx:FileX):void
127 | {
128 | lastFileName = fx.f.name;
129 | NodeContainer.one.showAiTree(fx);
130 | }
131 | public var lastFileName:String="";
132 | private function getNewNodeOb():Object{
133 | var ob:Object = {};
134 | ob.desc = "";
135 | ob.childs = [];
136 | ob.name = "根节点";
137 | ob.sel = "All";
138 | ob.x = 100;
139 | ob.y = 100;
140 | return ob;
141 | }
142 | public function show(path:String):void
143 | {
144 | this.path = path;
145 | var files:Vector.;
146 | files = new Vector.;
147 |
148 | dir = new File(path);
149 | if(!dir.exists){
150 | Alert.show(dir+"目录不存在,请检查");
151 | return;
152 | }
153 |
154 | var count:int;
155 | var arr:Array = dir.getDirectoryListing();
156 | for (var i:int = 0; i < arr.length; i++) {
157 | var f:File = arr[i] as File;
158 | if(f.extension!="ai") continue;
159 | count++;
160 | files.push(f);
161 | }
162 | files.sort(function(f1,f2):int{
163 | var n1:int = parseInt(f1.name.replace(".ai"));
164 | var n2:int = parseInt(f2.name.replace(".ai"));
165 | if(n1>n2) return 1;
166 | if(n10){
15 | sizeW = ww;
16 | sizeH = hh;
17 | p.stage.nativeWindow.width = ww;
18 | p.stage.nativeWindow.height = hh;
19 | }
20 | super(p, sizeW, sizeH, color, canResize, dragerSize);
21 | }
22 | override protected function onMouseMove(e:Event):void
23 | {
24 | super.onMouseMove(e);
25 | onResize();
26 | }
27 | override protected function onMouseDown(e:MouseEvent):void
28 | {
29 | super.onMouseDown(e);
30 | onResize();
31 | }
32 | override protected function onMouseUp(e:MouseEvent):void
33 | {
34 | super.onMouseUp(e);
35 | onResize2();
36 | }
37 | protected function onResize2():void
38 | {
39 | var ww:Number = drager.x + drager.width;
40 | var hh:Number = drager.y + drager.height;
41 | stage.nativeWindow.width = ww;
42 | stage.nativeWindow.height = hh;
43 | SO("AppBG_W",ww);
44 | SO("AppBG_H",hh);
45 | dispatchEvent(new Event(Event.RESIZE));
46 | }
47 | protected function onResize():void
48 | {
49 | var ww:Number = drager.x + drager.width;
50 | var hh:Number = drager.y + drager.height;
51 | stage.nativeWindow.width = ww+250;
52 | stage.nativeWindow.height = hh+250;
53 | dispatchEvent(new Event(Event.RESIZE));
54 | }
55 | public static function SO(key:String, val:*=null):*
56 | {
57 | var so:SharedObject = SharedObject.getLocal("AiEditor");
58 | if(val!=null) so.data[key] = val;
59 | return so.data[key];
60 | }
61 | }
62 | }
--------------------------------------------------------------------------------
/src/AppMover.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import flash.display.DisplayObjectContainer;
4 | import flash.events.Event;
5 | import flash.events.MouseEvent;
6 |
7 | public class AppMover extends Box
8 | {
9 | private var bgbox:AppBG;
10 | public function AppMover(p:DisplayObjectContainer, color:uint=0x999999,bgbox:AppBG=null)
11 | {
12 | super(p, bgbox.width, 60, color);
13 | this.bgbox = bgbox;
14 | addEventListener(MouseEvent.MOUSE_DOWN,onMoveAppWin);
15 | if(bgbox){
16 | bgbox.addEventListener(Event.RESIZE,onResize);
17 | }
18 | }
19 |
20 | protected function onResize(e:Event):void
21 | {
22 | setSize(bgbox.width,this.height);
23 | }
24 | protected function onMoveAppWin(event:MouseEvent):void
25 | {
26 | stage.nativeWindow.startMove();
27 | }
28 | }
29 | }
--------------------------------------------------------------------------------
/src/Arrow.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import flash.display.DisplayObject;
4 | import flash.display.Graphics;
5 | import flash.display.Sprite;
6 | import flash.geom.Point;
7 |
8 | public class Arrow extends Sprite
9 | {
10 | private var color:uint = 0x333333;
11 | public function Arrow(d1:DisplayObject,d2:DisplayObject)
12 | {
13 | draw(d1,d2);
14 | }
15 |
16 | public function draw(d1:DisplayObject,d2:DisplayObject):void
17 | {
18 | var g:Graphics = graphics;
19 | g.clear();
20 | var p2:Point = new Point();
21 | var p1:Point = new Point();
22 | p2.x = d2.x+d2.width*.5;
23 | p2.y = d2.y+d2.height*.5;
24 | p1.x = d1.x+d1.width*.5;
25 | p1.y = d1.y+d1.height*.5;
26 | g.lineStyle(1,color);
27 | g.moveTo(p1.x,p1.y);
28 | g.lineTo(p2.x,p2.y);
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/src/Box.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import flash.display.Bitmap;
4 | import flash.display.BitmapData;
5 | import flash.display.DisplayObjectContainer;
6 | import flash.display.Sprite;
7 | import flash.display.StageQuality;
8 | import flash.events.Event;
9 | import flash.events.MouseEvent;
10 |
11 | public class Box extends Sprite
12 | {
13 | public var bg:Bitmap;
14 | public var sizeW:int;
15 | public var sizeH:int;
16 | private var _color:uint;
17 |
18 | public function get color():uint{
19 | return _color;
20 | }
21 |
22 | public function set color(v:uint):void
23 | {
24 | _color = v;
25 | redraw();
26 | }
27 |
28 | private var canResize:Boolean;
29 | public var drager:Sprite;
30 | private var dragerSize:int;
31 | public function Box(p:DisplayObjectContainer,sizeW:int=30,sizeH:int=30,__color:uint=0x999999,canResize:Boolean=false,dragerSize:int = 25)
32 | {
33 | super();
34 | this.dragerSize = dragerSize;
35 | this.canResize = canResize;
36 | this._color = __color;
37 | this.sizeH = sizeH;
38 | this.sizeW = sizeW;
39 | p.addChild(this);
40 | bg = new Bitmap(new BitmapData(1,1,false)); addChild(bg);
41 |
42 | if(canResize){
43 | drager = new Sprite(); addChild(drager);
44 | drager.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
45 | drager.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
46 | drawDrager();
47 | }
48 | redraw();
49 | }
50 |
51 | protected function onMouseOut(e:MouseEvent):void
52 | {
53 | if(!press)drawDrager();
54 | }
55 |
56 | protected function onMouseOver(e:MouseEvent):void
57 | {
58 | drawDrager(-30);
59 | drager.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
60 | }
61 |
62 | private function drawDrager(d:int=-10):void
63 | {
64 | var dragerColor:uint = ColorUtil.Change(_color,d);
65 | drager.graphics.beginFill(dragerColor);
66 | drager.graphics.moveTo(dragerSize,0);
67 | drager.graphics.lineTo(dragerSize,dragerSize);
68 | drager.graphics.lineTo(0,dragerSize);
69 | drager.graphics.lineTo(dragerSize,0);
70 | drager.graphics.endFill();
71 | drager.x = sizeW-dragerSize;
72 | drager.y = sizeH-dragerSize;
73 | }
74 | public function setSize(w:int,h:int):void
75 | {
76 | sizeW = w;
77 | sizeH = h;
78 | redraw();
79 | }
80 | public function redraw():void
81 | {
82 | if(drager){
83 | drager.x = int(drager.x);
84 | drager.y = int(drager.y);
85 | sizeW =drager.x+drager.width;
86 | sizeH =drager.y+drager.height;
87 | if(lastX==drager.x && drager.y==lastY) return;
88 | }
89 | bg.bitmapData.dispose();
90 | AiEditor.flashStage.quality = StageQuality.LOW;
91 | bg.bitmapData = new BitmapData(sizeW,sizeH,false,_color);
92 | AiEditor.flashStage.quality = StageQuality.HIGH;
93 | if(drager){
94 | lastX = drager.x;
95 | lastY = drager.y;
96 | }
97 | }
98 | public function dispose():void
99 | {
100 | if(bg && bg.bitmapData)bg.bitmapData.dispose();
101 | if(parent)parent.removeChild(this);
102 | if(canResize){
103 | drager.removeEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
104 | drager.removeEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
105 | drager.removeEventListener(Event.ENTER_FRAME,onMouseMove);
106 | drager.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
107 | }
108 | }
109 | protected function onMouseUp(e:MouseEvent):void
110 | {
111 | press = false;
112 | drager.stopDrag();
113 | redraw();
114 | drawDrager();
115 | drager.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
116 | drager.removeEventListener(Event.ENTER_FRAME,onMouseMove);
117 | drager.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
118 | dispatchEvent(new Event(Event.RESIZE+2));
119 | }
120 | private var press:Boolean;
121 | private var lastX:int;
122 | private var lastY:int;
123 | protected function onMouseMove(e:Event):void
124 | {
125 | redraw();
126 | }
127 |
128 | protected function onMouseDown(e:MouseEvent):void
129 | {
130 | press = true;
131 | drager.removeEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
132 | drager.addEventListener(Event.ENTER_FRAME,onMouseMove);
133 | drager.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
134 | drager.startDrag();
135 | }
136 | }
137 | }
--------------------------------------------------------------------------------
/src/ColorUtil.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | public class ColorUtil
4 | {
5 | public static function getRGB(color:uint):Object{
6 | var o:Object = {};
7 | o.r = color >> 16 & 0xFF;
8 | o.g = color >> 8 & 0xFF;
9 | o.b = color & 0xFF;
10 | return o;
11 | }
12 |
13 | public static function getUINT(r:uint,g:uint,b:uint):uint{
14 | var h:uint = 0;
15 | h+=r<<16;
16 | h+=g<<8;
17 | h+=b;
18 | return h;
19 | }
20 |
21 | /** 加减灰度,如 Gray(0x333333,2) 结果是 0x353535 ,如果part被指定为"r",则只改变红色部分,其它g和b分别改变绿和蓝**/
22 | public static function Change(color:uint,d:int,part:String=null):uint{
23 | var c:Object = getRGB(color);
24 | if(part){
25 | var npart:int = c[part]+d;
26 | c[part] = npart>255?255:npart;
27 | }else{
28 | var nr:int = c.r+d;
29 | var ng:int = c.g+d;
30 | var nb:int = c.b+d;
31 | c.r = nr>255?255:nr;
32 | c.g = ng>255?255:ng;
33 | c.b = nb>255?255:nb;
34 | }
35 | return getUINT(c.r,c.g,c.b);
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/src/DirList.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import com.bit101.components.HBox;
4 | import com.bit101.components.InputText;
5 | import com.bit101.components.Label;
6 | import com.bit101.components.Style;
7 | import com.bit101.components.TextArea;
8 |
9 | import flash.display.Sprite;
10 | import flash.display.StageAlign;
11 | import flash.display.StageScaleMode;
12 | import flash.filesystem.File;
13 | import flash.net.SharedObject;
14 | import flash.utils.setTimeout;
15 |
16 | public class DirList extends Sprite
17 | {
18 |
19 | private var inputText:InputText;
20 |
21 | private var textArea:TextArea;
22 |
23 | private var so:SharedObject;
24 |
25 | private var inputText2:InputText;
26 | private var inputText3:InputText;
27 | public function DirList()
28 | {
29 | stage.align = StageAlign.TOP_LEFT;
30 | stage.scaleMode = StageScaleMode.NO_SCALE;
31 |
32 | Style.embedFonts = false;
33 | Style.fontName = "Arial";
34 | Style.fontSize = 14;
35 |
36 | inputText = new InputText(this,0,0,"",onChange); inputText.setSize(500,20);
37 | var hBox:HBox = new HBox(this,0,25);
38 |
39 | new Label(hBox,0,0,"将");
40 | inputText2 = new InputText(hBox,0,0,".mp3",searchDirAndListFiles); inputText2.setSize(50,20);
41 | new Label(hBox,0,0,"替换成");
42 | inputText3 = new InputText(hBox,0,0,"",searchDirAndListFiles); inputText3.setSize(50,20);
43 |
44 | textArea = new TextArea(this,0,55); textArea.setSize(500,400);
45 | so = SharedObject.getLocal("sounddir");
46 | if(!so.data.dir){
47 | so.data.dir = "E:\\client\\WSSG\\res\\asset\\sounds";
48 | }
49 | inputText.text = so.data.dir;
50 |
51 | searchDirAndListFiles();
52 | }
53 |
54 | private function searchDirAndListFiles(e:*=null):void
55 | {
56 | var f:File = new File(so.data.dir);
57 | if(!f.exists || !f.isDirectory){
58 | textArea.text = "";
59 | setTimeout(function():void{textArea.text = "目录不存在,或路径不存在,请重新指定路径";},200);
60 | return;
61 | }
62 | var list:Array = f.getDirectoryListing();
63 | var out:String = "[";
64 | for (var i:int = 0; i < list.length; i++)
65 | {
66 | out+="'"+(list[i] as File).name.replace(inputText2.text,inputText3.text)+"'";
67 | if(i+1!=list.length){
68 | out+=",";
69 | }else{
70 | out+="]";
71 | }
72 | }
73 | trace(out);
74 | textArea.text = out;
75 | }
76 |
77 | private function onChange(e:*):void
78 | {
79 | var so:SharedObject = SharedObject.getLocal("sounddir");
80 | so.data.dir = inputText.text;
81 | searchDirAndListFiles();
82 | }
83 | }
84 | }
--------------------------------------------------------------------------------
/src/DragBorder.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import flash.display.DisplayObject;
4 | import flash.display.DisplayObjectContainer;
5 | import flash.display.Graphics;
6 | import flash.display.Sprite;
7 | import flash.events.Event;
8 | import flash.events.MouseEvent;
9 | import flash.geom.Point;
10 |
11 | public class DragBorder extends Sprite
12 | {
13 | public static var p2:Point=new Point;
14 | public static var p1:Point=new Point;
15 | private var bg:DisplayObject;
16 | public function DragBorder(p:DisplayObjectContainer,bg:DisplayObject)
17 | {
18 | super();
19 | this.bg = bg;
20 | x = bg.x;
21 | y = bg.y;
22 | p.addChild(this);
23 | bg.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
24 | bg.addEventListener(MouseEvent.MOUSE_UP,onUp);
25 | bg.addEventListener(MouseEvent.RELEASE_OUTSIDE,onOut);
26 | this.mouseChildren = false;
27 | this.mouseEnabled = false;
28 | }
29 |
30 | protected function onOut(event:Event):void
31 | {
32 | onUp();
33 | }
34 |
35 | private function draw():void
36 | {
37 | var g:Graphics = graphics; g.clear();
38 | g.lineStyle(2,0x009900,.9);
39 | g.beginFill(0x009900,.3);
40 | g.drawRect(p1.x,p1.y,p2.x-p1.x,p2.y-p1.y);
41 | }
42 |
43 | protected function onUp(e:Event=null):void
44 | {
45 | p2.x = stage.mouseX;
46 | p2.y = stage.mouseY;
47 | bg.removeEventListener(MouseEvent.MOUSE_UP,onUp);
48 | stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
49 | graphics.clear();
50 |
51 | var minX:Number = Math.min(p1.x,p2.x);
52 | var minY:Number = Math.min(p1.y,p2.y);
53 | var maxX:Number = Math.max(p1.x,p2.x);
54 | var maxY:Number = Math.max(p1.y,p2.y);
55 |
56 | p1 = new Point(minX,minY);
57 | p2 = new Point(maxX,maxY);
58 |
59 | dispatchEvent(new Event(Event.SELECT));
60 | }
61 |
62 | protected function onMove(e:Event):void
63 | {
64 | p2 = new Point();
65 | p2.x = stage.mouseX;
66 | p2.y = stage.mouseY;
67 | draw();
68 | }
69 |
70 | protected function onDown(e:Event):void
71 | {
72 | stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
73 | bg.addEventListener(MouseEvent.MOUSE_UP,onUp);
74 | p1 = new Point();
75 | p1.x = stage.mouseX;
76 | p1.y = stage.mouseY;
77 | }
78 | }
79 | }
--------------------------------------------------------------------------------
/src/DragSprite.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import flash.display.Sprite;
4 | import flash.display.StageQuality;
5 | import flash.events.Event;
6 | import flash.events.MouseEvent;
7 |
8 | public class DragSprite extends Sprite
9 | {
10 | public var isDrag:Boolean;
11 | public function DragSprite()
12 | {
13 | addEventListener(Event.ADDED_TO_STAGE,onAdd);
14 | addEventListener(Event.REMOVED_FROM_STAGE,onRemoveEvent);
15 | }
16 |
17 | protected function onAdd(event:Event):void
18 | {
19 | removeEventListener(Event.ADDED_TO_STAGE,onAdd);
20 | addEventListener(MouseEvent.MOUSE_OVER,onIn);
21 | }
22 |
23 | protected function onRemoveEvent(e:Event=null):void
24 | {
25 | removeEventListener(MouseEvent.MOUSE_OVER,onIn);
26 | onOut();
27 | }
28 | protected function onIn(event:MouseEvent):void
29 | {
30 | addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
31 | addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
32 | addEventListener(MouseEvent.MOUSE_MOVE,onMove);
33 | addEventListener(MouseEvent.MOUSE_OUT,onOut);
34 | addEventListener(MouseEvent.RELEASE_OUTSIDE,onRelease);
35 | }
36 | protected function onRelease(event:MouseEvent = null):void
37 | {
38 | isDrag = false;
39 | }
40 | protected function onOut(event:MouseEvent = null):void
41 | {
42 | isDrag = false;
43 | removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
44 | removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
45 | removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
46 | removeEventListener(MouseEvent.MOUSE_OUT,onOut);
47 | removeEventListener(MouseEvent.RELEASE_OUTSIDE,onRelease);
48 | removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveEvent);
49 | }
50 |
51 | protected function onMove(e:MouseEvent):void
52 | {
53 | if(isDrag){
54 | this.startDrag();
55 | if(stage)stage.quality = StageQuality.LOW;
56 | }
57 | }
58 |
59 | protected function onMouseUp(e:MouseEvent):void
60 | {
61 | isDrag = false;
62 | this.stopDrag();
63 | this.dispatchEvent(new Event("stopDrag",true));
64 | x = int(x);
65 | y = int(y);
66 | //if(stage)stage.quality = StageQuality.BEST;
67 | }
68 | public function onMouseDown(e:MouseEvent=null):void
69 | {
70 | if(e)isDrag = true;
71 | }
72 | }
73 | }
--------------------------------------------------------------------------------
/src/FileX.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import com.bit101.components.HBox;
4 | import com.bit101.components.Label;
5 |
6 | import flash.display.DisplayObjectContainer;
7 | import flash.display.Sprite;
8 | import flash.events.Event;
9 | import flash.events.MouseEvent;
10 | import flash.filesystem.File;
11 | import flash.filesystem.FileMode;
12 | import flash.filesystem.FileStream;
13 |
14 | public class FileX extends Sprite
15 | {
16 | public var data:Object;
17 | public var f:File;
18 | private var num:Label;
19 | private var desc:Label;
20 | private var bg:Box;
21 | private var hBox:HBox;
22 | private var colorOver:uint=0xeeeeee;
23 | private var colorOut:uint=0xffffff;
24 | private var colorSel:uint=0x00aa00;
25 | public function FileX(p:DisplayObjectContainer, f:File)
26 | {
27 | bg = new Box(this,270,18,colorOut); addChild(bg);
28 | hBox = new HBox(this);
29 | this.f = f;
30 | num = new Label(hBox); num.width=50;
31 | desc = new Label(hBox); desc.width=200;
32 | p.addChild(this);
33 | addEventListener(MouseEvent.MOUSE_OVER,onOver);
34 | addEventListener(MouseEvent.MOUSE_OUT,onOut);
35 | addEventListener(MouseEvent.MOUSE_DOWN,onDown);
36 | read();
37 | }
38 |
39 | protected function onDown(e:MouseEvent):void
40 | {
41 | selected = true;
42 | }
43 |
44 | protected function onOut(e:MouseEvent):void
45 | {
46 | if(!selected)bg.color = colorOut;
47 | }
48 |
49 | protected function onOver(e:MouseEvent):void
50 | {
51 | if(!selected)bg.color = colorOver;
52 | }
53 | public function read():void
54 | {
55 | data = null;
56 | var s:FileStream = new FileStream();
57 | s.open(f,FileMode.READ);
58 | var str:String = s.readUTFBytes(s.bytesAvailable);
59 | try{
60 | data = JSON.parse(str);
61 | }catch(e:Error) {
62 | Alert.show(f.nativePath+"的 JSON 格式不正确: \n"+e.message);
63 | dispose();
64 | return;
65 | }
66 | num.text = f.name.replace(".ai","");
67 | desc.text = data.desc;
68 |
69 | }
70 | private var _selected:Boolean;
71 |
72 | public function get selected():Boolean
73 | {
74 | return _selected;
75 | }
76 | public static var lastSel:FileX;
77 | public function set selected(v:Boolean):void
78 | {
79 | _selected = v;
80 | if(v==true){
81 | if(lastSel && lastSel!=this){
82 | lastSel.selected = false;
83 | }
84 | if(lastSel!=this) {
85 | bg.color = colorSel;
86 | lastSel=this;
87 | this.dispatchEvent(new Event(Event.SELECT,true));
88 | }
89 | }else{
90 | bg.color = colorOut;
91 | }
92 | }
93 |
94 | public function dispose(delFile:Boolean=false):void
95 | {
96 | var me:FileX = this;
97 | function remove():void{
98 | f = null;
99 | data = null;
100 | bg.dispose();
101 | if(parent)parent.removeChild(me);
102 | }
103 | if(delFile && f.exists){
104 | Alert.show("确定从硬盘删除"+f.name+"?",function(yes:Boolean):void{
105 | if(yes){
106 | try{
107 | f.deleteFile();
108 | }catch(e:Error) {
109 | Alert.show("删除失败:"+e.message);
110 | }
111 | remove();
112 | }
113 | })
114 | }
115 | if(!delFile)remove();
116 | }
117 | }
118 | }
--------------------------------------------------------------------------------
/src/FlashBox.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import flash.display.Sprite;
4 |
5 | public class FlashBox extends Sprite
6 | {
7 | public function FlashBox()
8 | {
9 | graphics.beginFill(0xffffdd,.9);
10 | graphics.drawRect(-3,-3,6,6);
11 | graphics.endFill();
12 | }
13 | public static var arr:Array = [];
14 | public static function getPoolObject():FlashBox
15 | {
16 | if(arr.length>0){
17 | var pop:FlashBox = arr.pop();
18 | return pop;
19 | }
20 | var flashBox:FlashBox = new FlashBox();
21 | return flashBox;
22 | }
23 |
24 | public function hide():void
25 | {
26 | if(parent)parent.removeChild(this);
27 | arr.push(this);
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/src/InRect.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import flash.display.Sprite;
4 | import flash.display.StageAlign;
5 | import flash.display.StageScaleMode;
6 | import flash.events.Event;
7 | import flash.geom.Point;
8 | import flash.text.TextField;
9 |
10 | [SWF(width="1000",height="600")]
11 | public class InRect extends Sprite
12 | {
13 | private var world:Sprite;
14 |
15 | private var mouse:Yuan;
16 | public function InRect()
17 | {
18 | stage.align = StageAlign.TOP_LEFT;
19 | stage.scaleMode = StageScaleMode.NO_SCALE;
20 |
21 | world = new Sprite(); world.x = 200; world.y = 500; world.scaleY = -1;
22 | addChild(world);
23 |
24 | //坐标轴
25 | drawLine(-1000,0,1000,0);
26 | drawLine(0,-1000,0,1000);
27 |
28 | //矩形
29 | drawYuan(0,0);
30 | drawLine(0,0,400,320);
31 | drawLine(0,0,-180,350);
32 |
33 | //鼠标(在范围内时变绿,在范围外时变红)
34 | mouse = drawYuan(0,0);
35 | addEventListener(Event.ENTER_FRAME,onFrame);
36 | }
37 |
38 | protected function onFrame(event:Event):void
39 | {
40 | var mousePoint:Point = new Point(stage.mouseX,stage.mouseY);
41 | var p:Point = world.globalToLocal(mousePoint);
42 | mouse.x = p.x;
43 | mouse.y = p.y;
44 |
45 | var m:Matrix2D = new Matrix2D(400,320,-180,350);
46 | var mi:Matrix2D = m.Invert();
47 | check(p,mi);
48 | }
49 |
50 | private function check(pp:Point,m:Matrix2D):void
51 | {
52 | var p:Point = m.VectorLeftdot(pp);
53 | if(p.x>0 && p.x<1){
54 | if(p.y>0 && p.y<1){
55 | trace("mouse in rect");
56 | mouse.toGreen();
57 | return;
58 | }
59 | }
60 | mouse.toRed();
61 | trace("not in");
62 | }
63 |
64 | private function drawLine(x1:int,y1:int,x2:int,y2:int,color:uint = 0xff0000):void
65 | {
66 | world.graphics.lineStyle(1,color);
67 | world.graphics.moveTo(x1,y1);
68 | world.graphics.lineTo(x2,y2);
69 | }
70 | private function drawYuan(x1:Number,y1:Number):Yuan
71 | {
72 | var yuan:Yuan = new Yuan();
73 | yuan.x = x1;
74 | yuan.y = y1;
75 | world.addChild(yuan);
76 |
77 | var txt:TextField = new TextField();
78 | txt.selectable = false;
79 | txt.width = 200;
80 | txt.height = 20;
81 | txt.textColor = 0x000000;
82 | txt.text = "("+x1+","+y1+")";
83 | var p:Point = world.localToGlobal(new Point(x1,y1));
84 | txt.x = p.x;
85 | txt.y = p.y;
86 | addChild(txt);
87 |
88 | return yuan;
89 | }
90 | }
91 | }
92 | import flash.display.Sprite;
93 | import flash.geom.Point;
94 |
95 | class Yuan extends Sprite{
96 | private const Red:uint = 0xff0000;
97 | private const Green:uint = 0x009900;
98 | private var r:Number;
99 | public function Yuan(r:Number=15):void{
100 | this.r = r;
101 | toRed();
102 | }
103 | private function draw(color:uint):void
104 | {
105 | graphics.clear();
106 | graphics.beginFill(color,0.7);
107 | graphics.drawCircle(0,0,r);
108 | graphics.endFill();
109 | }
110 | public function toRed():void{
111 | draw(Red);
112 | }
113 | public function toGreen():void{
114 | draw(Green);
115 | }
116 | }
117 | class Matrix2D {
118 | //第一行
119 | public var x1:Number;//a
120 | public var y1:Number;//b
121 |
122 | //第二行
123 | public var x2:Number;//c
124 | public var y2:Number;//d
125 | public function Matrix2D(x1:Number,y1:Number,x2:Number,y2:Number):void{
126 | this.x1 = x1;
127 | this.y1 = y1;
128 | this.x2 = x2;
129 | this.y2 = y2;
130 | }
131 | public function VectorLeftdot(p:Point):Point{
132 | return new Point(p.x*x1+p.y*x2 , p.x*y1+p.y*y2);
133 | }
134 | public function Invert():Matrix2D{
135 | var m:Matrix2D = new Matrix2D(0,0,0,0);
136 | var det:Number = this.x1 * this.y2 - this.y1 * this.x2;
137 | m.x1 = (this.y2 / det);
138 | m.y1 = - (this.y1 / det);
139 | m.x2 = - (this.x2 / det);
140 | m.y2 = (this.x1 / det);
141 | return m;
142 | }
143 | public function toString():String{
144 | return "( "+x1+" , "+y1+" , "+x2+" , "+y2+" )";
145 | }
146 | }
--------------------------------------------------------------------------------
/src/LableX.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import com.bit101.components.Label;
4 |
5 | import flash.display.DisplayObjectContainer;
6 |
7 | public class LableX extends Label
8 | {
9 | public function LableX(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, text:String="")
10 | {
11 | super(parent, xpos, ypos, text);
12 | }
13 | private var _color:uint = 0xaaaaaa;
14 |
15 | public function get color():uint
16 | {
17 | return _color;
18 | }
19 |
20 | public function set color(v:uint):void
21 | {
22 | _color = v;
23 | _tf.textColor = _color;
24 | _tf.mouseEnabled = false;
25 | }
26 |
27 | }
28 | }
--------------------------------------------------------------------------------
/src/ListItemX.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import com.bit101.components.ListItem;
4 |
5 | import flash.display.Bitmap;
6 | import flash.display.BitmapData;
7 | import flash.display.DisplayObjectContainer;
8 |
9 | public class ListItemX extends ListItem
10 | {
11 |
12 | private var icon:Bitmap;
13 | public function ListItemX(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, data:Object=null)
14 | {
15 | super(parent, xpos, ypos, data);
16 | icon = new Bitmap(new BitmapData(25,25));
17 | addChild(icon);
18 | }
19 | public override function draw() : void
20 | {
21 | super.draw();
22 | if(Node.leafTypes.indexOf(_label.text)==-1){
23 | icon.bitmapData = Node.trunkbd;
24 | }else{
25 | icon.bitmapData = Node.leafbd;
26 | }
27 | addChildAt(icon,0);
28 | icon.scaleX = 0.7;
29 | icon.scaleY = 0.7;
30 | icon.alpha = 0.7;
31 | icon.x = _label.x+_label.getBounds(this).width+2;
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/src/ServerState.as:
--------------------------------------------------------------------------------
1 | package
2 | {
3 | import com.bit101.components.TextArea;
4 |
5 | import flash.display.DisplayObjectContainer;
6 | import flash.events.DataEvent;
7 |
8 | import net.SimpleSocketServer;
9 |
10 | import props.WindowX;
11 |
12 | public class ServerState extends WindowX
13 | {
14 | public var ww:int = 300;
15 | public var hh:int = 200;
16 |
17 | public var txt:TextArea;
18 | public static var one:ServerState;
19 | public function ServerState(p:DisplayObjectContainer, server:SimpleSocketServer)
20 | {
21 | one = this;
22 | super(p,"ServerState",976,63);
23 | setSize(ww,hh);
24 |
25 | txt = new TextArea(this,2,2);
26 | txt.width = ww-5;
27 | txt.height = hh-25;
28 | txt.text=server.toString();
29 |
30 | server.addEventListener(SimpleSocketServer.SOCKET_ALERT,function(e:DataEvent):void
31 | {
32 | txt.text += e.data+"\n";
33 | });
34 | }
35 | }
36 | }
--------------------------------------------------------------------------------
/src/com/bit101/charts/BarChart.as:
--------------------------------------------------------------------------------
1 | /**
2 | * BarChart.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A chart component for graphing an array of numeric data as a bar graph.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.charts
30 | {
31 | import flash.display.DisplayObjectContainer;
32 |
33 | public class BarChart extends Chart
34 | {
35 | protected var _spacing:Number = 2;
36 | protected var _barColor:uint = 0x999999;
37 |
38 | /**
39 | * Constructor
40 | * @param parent The parent DisplayObjectContainer on which to add this Label.
41 | * @param xpos The x position to place this component.
42 | * @param ypos The y position to place this component.
43 | * @param data The array of numeric values to graph.
44 | */
45 | public function BarChart(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, data:Array=null)
46 | {
47 | super(parent, xpos, ypos, data);
48 | }
49 |
50 | /**
51 | * Graphs the numeric data in the chart.
52 | */
53 | protected override function drawChart():void
54 | {
55 | var border:Number = 2;
56 | var totalSpace:Number = _spacing * _data.length;
57 | var barWidth:Number = (_width - border - totalSpace) / _data.length;
58 | var chartHeight:Number = _height - border;
59 | _chartHolder.x = 0;
60 | _chartHolder.y = _height;
61 | var xpos:Number = border;
62 | var max:Number = getMaxValue();
63 | var min:Number = getMinValue();
64 | var scale:Number = chartHeight / (max - min);
65 | for(var i:int = 0; i < _data.length; i++)
66 | {
67 | if(_data[i] != null)
68 | {
69 | _chartHolder.graphics.beginFill(_barColor);
70 | _chartHolder.graphics.drawRect(xpos, 0, barWidth, (_data[i] - min) * -scale);
71 | _chartHolder.graphics.endFill();
72 | }
73 | xpos += barWidth + _spacing;
74 | }
75 | }
76 |
77 |
78 |
79 | ///////////////////////////////////
80 | // getter/setters
81 | ///////////////////////////////////
82 |
83 | /**
84 | * Sets/gets the amount of space shown between each bar. If this is too wide, bars may become invisible.
85 | */
86 | public function set spacing(value:Number):void
87 | {
88 | _spacing = value;
89 | invalidate();
90 | }
91 | public function get spacing():Number
92 | {
93 | return _spacing;
94 | }
95 |
96 | /**
97 | * Sets/gets the color of the bars.
98 | */
99 | public function set barColor(value:uint):void
100 | {
101 | _barColor = value;
102 | invalidate();
103 | }
104 | public function get barColor():uint
105 | {
106 | return _barColor;
107 | }
108 |
109 |
110 | }
111 | }
--------------------------------------------------------------------------------
/src/com/bit101/charts/LineChart.as:
--------------------------------------------------------------------------------
1 | /**
2 | * LineChart.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A chart component for graphing an array of numeric data as a line graph.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.charts
30 | {
31 | import flash.display.DisplayObjectContainer;
32 |
33 | public class LineChart extends Chart
34 | {
35 | protected var _lineWidth:Number = 1;
36 | protected var _lineColor:uint = 0x999999;
37 |
38 | /**
39 | * Constructor
40 | * @param parent The parent DisplayObjectContainer on which to add this Label.
41 | * @param xpos The x position to place this component.
42 | * @param ypos The y position to place this component.
43 | * @param data The array of numeric values to graph.
44 | */
45 | public function LineChart(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, data:Array=null)
46 | {
47 | super(parent, xpos, ypos, data);
48 | }
49 |
50 | /**
51 | * Graphs the numeric data in the chart.
52 | */
53 | protected override function drawChart():void
54 | {
55 | var border:Number = 2;
56 | var lineWidth:Number = (_width - border) / (_data.length - 1);
57 | var chartHeight:Number = _height - border;
58 | _chartHolder.x = 0;
59 | _chartHolder.y = _height;
60 | var xpos:Number = border;
61 | var max:Number = getMaxValue();
62 | var min:Number = getMinValue();
63 | var scale:Number = chartHeight / (max - min);
64 | _chartHolder.graphics.lineStyle(_lineWidth, _lineColor);
65 | _chartHolder.graphics.moveTo(xpos, (_data[0] - min) * -scale);
66 | xpos += lineWidth;
67 | for(var i:int = 1; i < _data.length; i++)
68 | {
69 | if(_data[i] != null)
70 | {
71 | _chartHolder.graphics.lineTo(xpos, (_data[i] - min) * -scale);
72 | }
73 | xpos += lineWidth;
74 | }
75 | }
76 |
77 |
78 |
79 | ///////////////////////////////////
80 | // getter/setters
81 | ///////////////////////////////////
82 | /**
83 | * Sets/gets the width of the line in the graph.
84 | */
85 | public function set lineWidth(value:Number):void
86 | {
87 | _lineWidth = value;
88 | invalidate();
89 | }
90 | public function get lineWidth():Number
91 | {
92 | return _lineWidth;
93 | }
94 |
95 | /**
96 | * Sets/gets the color of the line in the graph.
97 | */
98 | public function set lineColor(value:uint):void
99 | {
100 | _lineColor = value;
101 | invalidate();
102 | }
103 | public function get lineColor():uint
104 | {
105 | return _lineColor;
106 | }
107 |
108 |
109 | }
110 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/Accordion.as:
--------------------------------------------------------------------------------
1 | /**
2 | * Accordion.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * Essentially a VBox full of Windows. Only one Window will be expanded at any time.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 |
30 | package com.bit101.components
31 | {
32 | import flash.display.DisplayObjectContainer;
33 | import flash.events.Event;
34 |
35 | public class Accordion extends Component
36 | {
37 | protected var _windows:Array;
38 | protected var _winWidth:Number = 100;
39 | protected var _winHeight:Number = 100;
40 | protected var _vbox:VBox;
41 |
42 | /**
43 | * Constructor
44 | * @param parent The parent DisplayObjectContainer on which to add this Panel.
45 | * @param xpos The x position to place this component.
46 | * @param ypos The y position to place this component.
47 | */
48 | public function Accordion(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
49 | {
50 | super(parent, xpos, ypos);
51 | }
52 |
53 | /**
54 | * Initializes the component.
55 | */
56 | protected override function init():void
57 | {
58 | super.init();
59 | setSize(100, 120);
60 | }
61 |
62 | /**
63 | * Creates and adds the child display objects of this component.
64 | */
65 | protected override function addChildren() : void
66 | {
67 | _vbox = new VBox(this);
68 | _vbox.spacing = 0;
69 |
70 | _windows = new Array();
71 | for(var i:int = 0; i < 2; i++)
72 | {
73 | var window:Window = new Window(_vbox, 0, 0, "Section " + (i + 1));
74 | window.grips.visible = false;
75 | window.draggable = false;
76 | window.addEventListener(Event.SELECT, onWindowSelect);
77 | if(i != 0) window.minimized = true;
78 | _windows.push(window);
79 | }
80 | }
81 |
82 | ///////////////////////////////////
83 | // public methods
84 | ///////////////////////////////////
85 |
86 | /**
87 | * Adds a new window to the bottom of the accordion.
88 | * @param title The title of the new window.
89 | */
90 | public function addWindow(title:String):void
91 | {
92 | addWindowAt(title, _windows.length);
93 | }
94 |
95 | public function addWindowAt(title:String, index:int):void
96 | {
97 | index = Math.min(index, _windows.length);
98 | index = Math.max(index, 0);
99 | var window:Window = new Window(null, 0, 0, title);
100 | _vbox.addChildAt(window, index);
101 | window.minimized = true;
102 | window.draggable = false;
103 | window.grips.visible = false;
104 | window.addEventListener(Event.SELECT, onWindowSelect);
105 | _windows.splice(index, 0, window);
106 | _winHeight = _height - (_windows.length - 1) * 20;
107 | setSize(_winWidth, _winHeight);
108 | }
109 |
110 | /**
111 | * Sets the size of the component.
112 | * @param w The width of the component.
113 | * @param h The height of the component.
114 | */
115 | override public function setSize(w:Number, h:Number) : void
116 | {
117 | super.setSize(w, h);
118 | _winWidth = w;
119 | _winHeight = h - (_windows.length - 1) * 20;
120 | draw();
121 | }
122 |
123 | override public function draw():void
124 | {
125 | _winHeight = Math.max(_winHeight, 40);
126 | for(var i:int = 0; i < _windows.length; i++)
127 | {
128 | _windows[i].setSize(_winWidth, _winHeight);
129 | _vbox.draw();
130 | }
131 | }
132 |
133 | /**
134 | * Returns the Window at the specified index.
135 | * @param index The index of the Window you want to get access to.
136 | */
137 | public function getWindowAt(index:int):Window
138 | {
139 | return _windows[index];
140 | }
141 |
142 |
143 | ///////////////////////////////////
144 | // event handlers
145 | ///////////////////////////////////
146 |
147 | /**
148 | * Called when any window is resized. If the window has been expanded, it closes all other windows.
149 | */
150 | protected function onWindowSelect(event:Event):void
151 | {
152 | var window:Window = event.target as Window;
153 | if(window.minimized)
154 | {
155 | for(var i:int = 0; i < _windows.length; i++)
156 | {
157 | _windows[i].minimized = true;
158 | }
159 | window.minimized = false;
160 | }
161 | _vbox.draw();
162 | }
163 |
164 | public override function set width(w:Number):void
165 | {
166 | _winWidth = w;
167 | super.width = w;
168 | }
169 |
170 | public override function set height(h:Number):void
171 | {
172 | _winHeight = h - (_windows.length - 1) * 20;
173 | super.height = h;
174 | }
175 |
176 | }
177 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/CheckBox.as:
--------------------------------------------------------------------------------
1 | /**
2 | * CheckBox.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A basic CheckBox component.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 | import flash.display.Sprite;
33 | import flash.events.MouseEvent;
34 |
35 | public class CheckBox extends Component
36 | {
37 | protected var _back:Sprite;
38 | protected var _button:Sprite;
39 | protected var _label:Label;
40 | protected var _labelText:String = "";
41 | protected var _selected:Boolean = false;
42 |
43 |
44 | /**
45 | * Constructor
46 | * @param parent The parent DisplayObjectContainer on which to add this CheckBox.
47 | * @param xpos The x position to place this component.
48 | * @param ypos The y position to place this component.
49 | * @param label String containing the label for this component.
50 | * @param defaultHandler The event handling function to handle the default event for this component (click in this case).
51 | */
52 | public function CheckBox(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, label:String = "", defaultHandler:Function = null)
53 | {
54 | _labelText = label;
55 | super(parent, xpos, ypos);
56 | if(defaultHandler != null)
57 | {
58 | addEventListener(MouseEvent.CLICK, defaultHandler);
59 | }
60 | }
61 |
62 | /**
63 | * Initializes the component.
64 | */
65 | override protected function init():void
66 | {
67 | super.init();
68 | buttonMode = true;
69 | useHandCursor = true;
70 | mouseChildren = false;
71 | }
72 |
73 | /**
74 | * Creates the children for this component
75 | */
76 | override protected function addChildren():void
77 | {
78 | _back = new Sprite();
79 | _back.filters = [getShadow(2, true)];
80 | addChild(_back);
81 |
82 | _button = new Sprite();
83 | _button.filters = [getShadow(1)];
84 | _button.visible = false;
85 | addChild(_button);
86 |
87 | _label = new Label(this, 0, 0, _labelText);
88 | draw();
89 |
90 | addEventListener(MouseEvent.CLICK, onClick);
91 | }
92 |
93 |
94 |
95 |
96 | ///////////////////////////////////
97 | // public methods
98 | ///////////////////////////////////
99 |
100 | /**
101 | * Draws the visual ui of the component.
102 | */
103 | override public function draw():void
104 | {
105 | super.draw();
106 | _back.graphics.clear();
107 | _back.graphics.beginFill(Style.BACKGROUND);
108 | _back.graphics.drawRect(0, 0, 10, 10);
109 | _back.graphics.endFill();
110 |
111 | _button.graphics.clear();
112 | _button.graphics.beginFill(Style.BUTTON_FACE);
113 | _button.graphics.drawRect(2, 2, 6, 6);
114 |
115 | _label.text = _labelText;
116 | _label.draw();
117 | _label.x = 12;
118 | _label.y = (10 - _label.height) / 2;
119 | _width = _label.width + 12;
120 | _height = 10;
121 | }
122 |
123 |
124 |
125 |
126 | ///////////////////////////////////
127 | // event handler
128 | ///////////////////////////////////
129 |
130 | /**
131 | * Internal click handler.
132 | * @param event The MouseEvent passed by the system.
133 | */
134 | protected function onClick(event:MouseEvent):void
135 | {
136 | _selected = !_selected;
137 | _button.visible = _selected;
138 | }
139 |
140 |
141 |
142 |
143 | ///////////////////////////////////
144 | // getter/setters
145 | ///////////////////////////////////
146 |
147 | /**
148 | * Sets / gets the label text shown on this CheckBox.
149 | */
150 | public function set label(str:String):void
151 | {
152 | _labelText = str;
153 | invalidate();
154 | }
155 | public function get label():String
156 | {
157 | return _labelText;
158 | }
159 |
160 | /**
161 | * Sets / gets the selected state of this CheckBox.
162 | */
163 | public function set selected(s:Boolean):void
164 | {
165 | _selected = s;
166 | _button.visible = _selected;
167 | }
168 | public function get selected():Boolean
169 | {
170 | return _selected;
171 | }
172 |
173 | /**
174 | * Sets/gets whether this component will be enabled or not.
175 | */
176 | public override function set enabled(value:Boolean):void
177 | {
178 | super.enabled = value;
179 | mouseChildren = false;
180 | }
181 |
182 | }
183 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/FPSMeter.as:
--------------------------------------------------------------------------------
1 | /**
2 | * FPSMeter.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * An simple component showing the frames per second the current movie is running at.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 | import flash.events.Event;
33 | import flash.utils.getTimer;
34 |
35 |
36 | public class FPSMeter extends Component
37 | {
38 | protected var _label:Label;
39 | protected var _startTime:int;
40 | protected var _frames:int;
41 | protected var _prefix:String = "";
42 | protected var _fps:int = 0;
43 |
44 | /**
45 | * Constructor
46 | * @param parent The parent DisplayObjectContainer on which to add this ColorChooser.
47 | * @param xpos The x position to place this component.
48 | * @param ypos The y position to place this component.
49 | * @param prefix A string to put in front of the number value shown. Default is "FPS:".
50 | */
51 |
52 | public function FPSMeter(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, prefix:String="FPS:")
53 | {
54 | super(parent, xpos, ypos);
55 | _prefix = prefix;
56 | _frames = 0;
57 | _startTime = getTimer();
58 | setSize(50, 20);
59 | if(stage != null)
60 | {
61 | addEventListener(Event.ENTER_FRAME, onEnterFrame);
62 | }
63 | addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
64 | }
65 |
66 | protected override function addChildren():void
67 | {
68 | super.addChildren();
69 | _label = new Label(this, 0, 0);
70 | }
71 |
72 |
73 | public override function draw():void
74 | {
75 | _label.text = _prefix + _fps.toString();
76 | }
77 |
78 | /**
79 | * Internal enterFrame handler to measure fps and update label.
80 | */
81 | protected function onEnterFrame(event:Event):void
82 | {
83 | // Increment frame count each frame. When more than a second has passed,
84 | // display number of accumulated frames and reset.
85 | // Thus FPS will only be calculated and displayed once per second.
86 | // There are more responsive methods that calculate FPS on every frame.
87 | // This method is uses less CPU and avoids the "jitter" of those other methods.
88 | _frames++;
89 | var time:int = getTimer();
90 | var elapsed:int = time - _startTime;
91 | if(elapsed >= 1000)
92 | {
93 | _fps = Math.round(_frames * 1000 / elapsed);
94 | _frames = 0;
95 | _startTime = time;
96 | draw();
97 | }
98 | }
99 |
100 | /**
101 | * Stops the meter if it is removed from stage.
102 | */
103 | protected function onRemovedFromStage(event:Event):void
104 | {
105 | stop();
106 | }
107 |
108 | /**
109 | * Stops the meter by removing the enterFrame listener.
110 | */
111 | public function stop():void
112 | {
113 | removeEventListener(Event.ENTER_FRAME, onEnterFrame);
114 | }
115 |
116 | /**
117 | * Starts the meter again if it has been stopped.
118 | */
119 | public function start():void
120 | {
121 | addEventListener(Event.ENTER_FRAME, onEnterFrame);
122 | }
123 |
124 | /**
125 | * Sets or gets the prefix shown before the number. Defaults to "FPS:".
126 | */
127 | public function set prefix(value:String):void
128 | {
129 | _prefix = value;
130 | }
131 | public function get prefix():String
132 | {
133 | return _prefix;
134 | }
135 |
136 | /**
137 | * Returns the current calculated FPS.
138 | */
139 | public function get fps():int
140 | {
141 | return _fps;
142 | }
143 | }
144 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/HBox.as:
--------------------------------------------------------------------------------
1 | /**
2 | * VBox.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A layout container for vertically aligning other components.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 |
30 | package com.bit101.components
31 | {
32 | import flash.display.DisplayObject;
33 | import flash.display.DisplayObjectContainer;
34 | import flash.events.Event;
35 |
36 | [Event(name="resize", type="flash.events.Event")]
37 | public class HBox extends Component
38 | {
39 | protected var _spacing:Number = 5;
40 | private var _alignment:String = NONE;
41 |
42 | public static const TOP:String = "top";
43 | public static const BOTTOM:String = "bottom";
44 | public static const MIDDLE:String = "middle";
45 | public static const NONE:String = "none";
46 |
47 |
48 | /**
49 | * Constructor
50 | * @param parent The parent DisplayObjectContainer on which to add this PushButton.
51 | * @param xpos The x position to place this component.
52 | * @param ypos The y position to place this component.
53 | */
54 | public function HBox(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0)
55 | {
56 | super(parent, xpos, ypos);
57 | }
58 |
59 | /**
60 | * Override of addChild to force layout;
61 | */
62 | override public function addChild(child:DisplayObject) : DisplayObject
63 | {
64 | super.addChild(child);
65 | child.addEventListener(Event.RESIZE, onResize);
66 | draw();
67 | return child;
68 | }
69 |
70 | /**
71 | * Override of addChildAt to force layout;
72 | */
73 | override public function addChildAt(child:DisplayObject, index:int) : DisplayObject
74 | {
75 | super.addChildAt(child, index);
76 | child.addEventListener(Event.RESIZE, onResize);
77 | draw();
78 | return child;
79 | }
80 |
81 | /**
82 | * Override of removeChild to force layout;
83 | */
84 | override public function removeChild(child:DisplayObject):DisplayObject
85 | {
86 | super.removeChild(child);
87 | child.removeEventListener(Event.RESIZE, onResize);
88 | draw();
89 | return child;
90 | }
91 |
92 | /**
93 | * Override of removeChild to force layout;
94 | */
95 | override public function removeChildAt(index:int):DisplayObject
96 | {
97 | var child:DisplayObject = super.removeChildAt(index);
98 | child.removeEventListener(Event.RESIZE, onResize);
99 | draw();
100 | return child;
101 | }
102 |
103 | protected function onResize(event:Event):void
104 | {
105 | invalidate();
106 | }
107 |
108 | protected function doAlignment():void
109 | {
110 | if(_alignment != NONE)
111 | {
112 | for(var i:int = 0; i < numChildren; i++)
113 | {
114 | var child:DisplayObject = getChildAt(i);
115 | if(_alignment == TOP)
116 | {
117 | child.y = 0;
118 | }
119 | else if(_alignment == BOTTOM)
120 | {
121 | child.y = _height - child.height;
122 | }
123 | else if(_alignment == MIDDLE)
124 | {
125 | child.y = (_height - child.height) / 2;
126 | }
127 | }
128 | }
129 | }
130 |
131 | /**
132 | * Draws the visual ui of the component, in this case, laying out the sub components.
133 | */
134 | override public function draw() : void
135 | {
136 | _width = 0;
137 | _height = 0;
138 | var xpos:Number = 0;
139 | for(var i:int = 0; i < numChildren; i++)
140 | {
141 | var child:DisplayObject = getChildAt(i);
142 | child.x = xpos;
143 | xpos += child.width;
144 | xpos += _spacing;
145 | _width += child.width;
146 | _height = Math.max(_height, child.height);
147 | }
148 | doAlignment();
149 | _width += _spacing * (numChildren - 1);
150 | dispatchEvent(new Event(Event.RESIZE));
151 | }
152 |
153 | /**
154 | * Gets / sets the spacing between each sub component.
155 | */
156 | public function set spacing(s:Number):void
157 | {
158 | _spacing = s;
159 | invalidate();
160 | }
161 | public function get spacing():Number
162 | {
163 | return _spacing;
164 | }
165 |
166 | /**
167 | * Gets / sets the vertical alignment of components in the box.
168 | */
169 | public function set alignment(value:String):void
170 | {
171 | _alignment = value;
172 | invalidate();
173 | }
174 | public function get alignment():String
175 | {
176 | return _alignment;
177 | }
178 | }
179 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/HRangeSlider.as:
--------------------------------------------------------------------------------
1 | /**
2 | * HRangeSlider.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A horizontal slider with two handles for selecting a range of values.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 |
33 | public class HRangeSlider extends RangeSlider
34 | {
35 | public function HRangeSlider(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, defaultHandler:Function = null)
36 | {
37 | super(HORIZONTAL, parent, xpos, ypos, defaultHandler);
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/HScrollBar.as:
--------------------------------------------------------------------------------
1 | /**
2 | * HScrollBar.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A horizontal scroll bar for use in other components.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 |
33 | /**
34 | * Constructor
35 | * @param parent The parent DisplayObjectContainer on which to add this ScrollBar.
36 | * @param xpos The x position to place this component.
37 | * @param ypos The y position to place this component.
38 | * @param defaultHandler The event handling function to handle the default event for this component (change in this case).
39 | */
40 | public class HScrollBar extends ScrollBar
41 | {
42 | public function HScrollBar(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, defaultHandler:Function=null)
43 | {
44 | super(Slider.HORIZONTAL, parent, xpos, ypos, defaultHandler);
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/HSlider.as:
--------------------------------------------------------------------------------
1 | /**
2 | * HSlider.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A Horizontal Slider component for choosing values.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 |
33 | public class HSlider extends Slider
34 | {
35 | /**
36 | * Constructor
37 | * @param parent The parent DisplayObjectContainer on which to add this HSlider.
38 | * @param xpos The x position to place this component.
39 | * @param ypos The y position to place this component.
40 | * @param defaultHandler The event handling function to handle the default event for this component.
41 | */
42 | public function HSlider(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, defaultHandler:Function = null)
43 | {
44 | super(Slider.HORIZONTAL, parent, xpos, ypos, defaultHandler);
45 | }
46 |
47 | }
48 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/HUISlider.as:
--------------------------------------------------------------------------------
1 | /**
2 | * HUISlider.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A Horizontal slider with a label and a value label.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 |
33 | public class HUISlider extends UISlider
34 | {
35 |
36 | /**
37 | * Constructor
38 | * @param parent The parent DisplayObjectContainer on which to add this HUISlider.
39 | * @param x The x position to place this component.
40 | * @param y The y position to place this component.
41 | * @param label The string to use as the label for this component.
42 | * @param defaultHandler The event handling function to handle the default event for this component.
43 | */
44 | public function HUISlider(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, label:String = "", defaultHandler:Function = null)
45 | {
46 | _sliderClass = HSlider;
47 | super(parent, xpos, ypos, label, defaultHandler);
48 | }
49 |
50 | /**
51 | * Initializes the component.
52 | */
53 | override protected function init():void
54 | {
55 | super.init();
56 | setSize(200, 18);
57 | }
58 |
59 | /**
60 | * Centers the label when label text is changed.
61 | */
62 | override protected function positionLabel():void
63 | {
64 | _valueLabel.x = _slider.x + _slider.width + 5;
65 | }
66 |
67 |
68 |
69 |
70 | ///////////////////////////////////
71 | // public methods
72 | ///////////////////////////////////
73 |
74 | /**
75 | * Draws the visual ui of this component.
76 | */
77 | override public function draw():void
78 | {
79 | super.draw();
80 | _slider.x = _label.width + 5;
81 | _slider.y = height / 2 - _slider.height / 2;
82 | _slider.width = width - _label.width - 50 - 10;
83 |
84 | _valueLabel.x = _slider.x + _slider.width + 5;
85 | }
86 |
87 | ///////////////////////////////////
88 | // event handlers
89 | ///////////////////////////////////
90 |
91 | ///////////////////////////////////
92 | // getter/setters
93 | ///////////////////////////////////
94 |
95 | }
96 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/IndicatorLight.as:
--------------------------------------------------------------------------------
1 | /**
2 | * IndicatorLight.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * An indicator light that can be turned on, off, or set to flash.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 | import flash.display.GradientType;
33 | import flash.display.Shape;
34 | import flash.events.TimerEvent;
35 | import flash.geom.Matrix;
36 | import flash.utils.Timer;
37 |
38 | public class IndicatorLight extends Component
39 | {
40 | protected var _color:uint;
41 | protected var _lit:Boolean = false;
42 | protected var _label:Label;
43 | protected var _labelText:String = "";
44 | protected var _lite:Shape;
45 | protected var _timer:Timer;
46 |
47 |
48 |
49 | /**
50 | * Constructor
51 | * @param parent The parent DisplayObjectContainer on which to add this CheckBox.
52 | * @param xpos The x position to place this component.
53 | * @param ypos The y position to place this component.
54 | * @param color The color of this light.
55 | * @param label String containing the label for this component.
56 | */
57 | public function IndicatorLight(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, color:uint = 0xff0000, label:String = "")
58 | {
59 | _color = color;
60 | _labelText = label;
61 | super(parent, xpos, ypos);
62 | }
63 |
64 | /**
65 | * Initializes the component.
66 | */
67 | override protected function init():void
68 | {
69 | super.init();
70 | _timer = new Timer(500);
71 | _timer.addEventListener(TimerEvent.TIMER, onTimer);
72 | }
73 |
74 | /**
75 | * Creates the children for this component
76 | */
77 | override protected function addChildren():void
78 | {
79 | _lite = new Shape();
80 | addChild(_lite);
81 |
82 | _label = new Label(this, 0, 0, _labelText);
83 | draw();
84 | }
85 |
86 | /**
87 | * Draw the light.
88 | */
89 | protected function drawLite():void
90 | {
91 | var colors:Array;
92 | if(_lit)
93 | {
94 | colors = [0xffffff, _color];
95 | }
96 | else
97 | {
98 | colors = [0xffffff, 0];
99 | }
100 |
101 | _lite.graphics.clear();
102 | var matrix:Matrix = new Matrix();
103 | matrix.createGradientBox(10, 10, 0, -2.5, -2.5);
104 | _lite.graphics.beginGradientFill(GradientType.RADIAL, colors, [1, 1], [0, 255], matrix);
105 | _lite.graphics.drawCircle(5, 5, 5);
106 | _lite.graphics.endFill();
107 | }
108 |
109 |
110 |
111 | ///////////////////////////////////
112 | // event handler
113 | ///////////////////////////////////
114 |
115 | /**
116 | * Internal timer handler.
117 | * @param event The TimerEvent passed by the system.
118 | */
119 | protected function onTimer(event:TimerEvent):void
120 | {
121 | _lit = !_lit;
122 | draw();
123 | }
124 |
125 |
126 |
127 |
128 | ///////////////////////////////////
129 | // public methods
130 | ///////////////////////////////////
131 |
132 | /**
133 | * Draws the visual ui of the component.
134 | */
135 | override public function draw():void
136 | {
137 | super.draw();
138 | drawLite();
139 |
140 | _label.text = _labelText;
141 | _label.x = 12;
142 | _label.y = (10 - _label.height) / 2;
143 | _width = _label.width + 12;
144 | _height = 10;
145 | }
146 |
147 | /**
148 | * Causes the light to flash on and off at the specified interval (milliseconds). A value less than 1 stops the flashing.
149 | */
150 | public function flash(interval:int = 500):void
151 | {
152 | if(interval < 1)
153 | {
154 | _timer.stop();
155 | isLit = false;
156 | return;
157 | }
158 | _timer.delay = interval;
159 | _timer.start();
160 | }
161 |
162 |
163 |
164 |
165 | ///////////////////////////////////
166 | // getter/setters
167 | ///////////////////////////////////
168 |
169 | /**
170 | * Sets or gets whether or not the light is lit.
171 | */
172 | public function set isLit(value:Boolean):void
173 | {
174 | _timer.stop();
175 | _lit = value;
176 | drawLite();
177 | }
178 | public function get isLit():Boolean
179 | {
180 | return _lit;
181 | }
182 |
183 | /**
184 | * Sets / gets the color of this light (when lit).
185 | */
186 | public function set color(value:uint):void
187 | {
188 | _color = value;
189 | draw();
190 | }
191 | public function get color():uint
192 | {
193 | return _color;
194 | }
195 |
196 | /**
197 | * Returns whether or not the light is currently flashing.
198 | */
199 | public function get isFlashing():Boolean
200 | {
201 | return _timer.running;
202 | }
203 |
204 | /**
205 | * Sets / gets the label text shown on this component.
206 | */
207 | public function set label(str:String):void
208 | {
209 | _labelText = str;
210 | draw();
211 | }
212 | public function get label():String
213 | {
214 | return _labelText;
215 | }
216 |
217 |
218 | }
219 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/InputText.as:
--------------------------------------------------------------------------------
1 | /**
2 | * InputText.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * Copyright (c) 2011 Keith Peters
7 | *
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
9 | * of this software and associated documentation files (the "Software"), to deal
10 | * in the Software without restriction, including without limitation the rights
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | * copies of the Software, and to permit persons to whom the Software is
13 | * furnished to do so, subject to the following conditions:
14 | *
15 | * The above copyright notice and this permission notice shall be included in
16 | * all copies or substantial portions of the Software.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 | * THE SOFTWARE.
25 | */
26 |
27 | package com.bit101.components
28 | {
29 | import flash.display.DisplayObjectContainer;
30 | import flash.display.Sprite;
31 | import flash.events.Event;
32 | import flash.text.TextField;
33 | import flash.text.TextFieldType;
34 | import flash.text.TextFormat;
35 |
36 | public class InputText extends Component
37 | {
38 | protected var _back:Sprite;
39 | protected var _password:Boolean = false;
40 | protected var _text:String = "";
41 | protected var _tf:TextField;
42 |
43 | /**
44 | * Constructor
45 | * @param parent The parent DisplayObjectContainer on which to add this InputText.
46 | * @param xpos The x position to place this component.
47 | * @param ypos The y position to place this component.
48 | * @param text The string containing the initial text of this component.
49 | * @param defaultHandler The event handling function to handle the default event for this component (change in this case).
50 | */
51 | public function InputText(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, text:String = "", defaultHandler:Function = null)
52 | {
53 | this.text = text;
54 | super(parent, xpos, ypos);
55 | if(defaultHandler != null)
56 | {
57 | addEventListener(Event.CHANGE, defaultHandler);
58 | }
59 | }
60 |
61 | /**
62 | * Initializes the component.
63 | */
64 | override protected function init():void
65 | {
66 | super.init();
67 | setSize(100, 20);
68 | }
69 |
70 | /**
71 | * Creates and adds child display objects.
72 | */
73 | override protected function addChildren():void
74 | {
75 | _back = new Sprite();
76 | _back.filters = [getShadow(2, true)];
77 | addChild(_back);
78 |
79 | _tf = new TextField();
80 | _tf.embedFonts = Style.embedFonts;
81 | _tf.selectable = true;
82 | _tf.type = TextFieldType.INPUT;
83 | _tf.defaultTextFormat = new TextFormat(Style.fontName, Style.fontSize, Style.INPUT_TEXT);
84 | addChild(_tf);
85 | _tf.addEventListener(Event.CHANGE, onChange);
86 |
87 | }
88 |
89 |
90 |
91 |
92 | ///////////////////////////////////
93 | // public methods
94 | ///////////////////////////////////
95 |
96 | /**
97 | * Draws the visual ui of the component.
98 | */
99 | override public function draw():void
100 | {
101 | super.draw();
102 | _back.graphics.clear();
103 | _back.graphics.beginFill(Style.BACKGROUND);
104 | _back.graphics.drawRect(0, 0, _width, _height);
105 | _back.graphics.endFill();
106 |
107 | _tf.displayAsPassword = _password;
108 |
109 | if(_text != null)
110 | {
111 | _tf.text = _text;
112 | }
113 | else
114 | {
115 | _tf.text = "";
116 | }
117 | _tf.width = _width - 4;
118 | if(_tf.text == "")
119 | {
120 | _tf.text = "X";
121 | _tf.height = Math.min(_tf.textHeight + 4, _height);
122 | _tf.text = "";
123 | }
124 | else
125 | {
126 | _tf.height = Math.min(_tf.textHeight + 4, _height);
127 | }
128 | _tf.x = 2;
129 | _tf.y = Math.round(_height / 2 - _tf.height / 2);
130 | }
131 |
132 |
133 |
134 |
135 | ///////////////////////////////////
136 | // event handlers
137 | ///////////////////////////////////
138 |
139 | /**
140 | * Internal change handler.
141 | * @param event The Event passed by the system.
142 | */
143 | protected function onChange(event:Event):void
144 | {
145 | _text = _tf.text;
146 | event.stopImmediatePropagation();
147 | dispatchEvent(event);
148 | }
149 |
150 |
151 |
152 |
153 | ///////////////////////////////////
154 | // getter/setters
155 | ///////////////////////////////////
156 |
157 | /**
158 | * Gets / sets the text shown in this InputText.
159 | */
160 | public function set text(t:String):void
161 | {
162 | _text = t;
163 | if(_text == null) _text = "";
164 | invalidate();
165 | }
166 | public function get text():String
167 | {
168 | return _text;
169 | }
170 |
171 | /**
172 | * Returns a reference to the internal text field in the component.
173 | */
174 | public function get textField():TextField
175 | {
176 | return _tf;
177 | }
178 |
179 | /**
180 | * Gets / sets the list of characters that are allowed in this TextInput.
181 | */
182 | public function set restrict(str:String):void
183 | {
184 | _tf.restrict = str;
185 | }
186 | public function get restrict():String
187 | {
188 | return _tf.restrict;
189 | }
190 |
191 | /**
192 | * Gets / sets the maximum number of characters that can be shown in this InputText.
193 | */
194 | public function set maxChars(max:int):void
195 | {
196 | _tf.maxChars = max;
197 | }
198 | public function get maxChars():int
199 | {
200 | return _tf.maxChars;
201 | }
202 |
203 | /**
204 | * Gets / sets whether or not this input text will show up as password (asterisks).
205 | */
206 | public function set password(b:Boolean):void
207 | {
208 | _password = b;
209 | invalidate();
210 | }
211 | public function get password():Boolean
212 | {
213 | return _password;
214 | }
215 |
216 | /**
217 | * Sets/gets whether this component is enabled or not.
218 | */
219 | public override function set enabled(value:Boolean):void
220 | {
221 | super.enabled = value;
222 | _tf.tabEnabled = value;
223 | }
224 |
225 | }
226 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/Label.as:
--------------------------------------------------------------------------------
1 | /**
2 | * Label.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A Label component for displaying a single line of text.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 | import flash.events.Event;
33 | import flash.text.TextField;
34 | import flash.text.TextFieldAutoSize;
35 | import flash.text.TextFormat;
36 |
37 | [Event(name="resize", type="flash.events.Event")]
38 | public class Label extends Component
39 | {
40 | protected var _autoSize:Boolean = true;
41 | protected var _text:String = "";
42 | protected var _tf:TextField;
43 |
44 | /**
45 | * Constructor
46 | * @param parent The parent DisplayObjectContainer on which to add this Label.
47 | * @param xpos The x position to place this component.
48 | * @param ypos The y position to place this component.
49 | * @param text The string to use as the initial text in this component.
50 | */
51 | public function Label(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, text:String = "")
52 | {
53 | this.text = text;
54 | super(parent, xpos, ypos);
55 | }
56 |
57 | /**
58 | * Initializes the component.
59 | */
60 | override protected function init():void
61 | {
62 | super.init();
63 | mouseEnabled = false;
64 | mouseChildren = false;
65 | }
66 |
67 | /**
68 | * Creates and adds the child display objects of this component.
69 | */
70 | override protected function addChildren():void
71 | {
72 | _height = 18;
73 | _tf = new TextField();
74 | _tf.height = _height;
75 | _tf.embedFonts = Style.embedFonts;
76 | _tf.selectable = false;
77 | _tf.mouseEnabled = false;
78 | _tf.defaultTextFormat = new TextFormat(Style.fontName, Style.fontSize, Style.LABEL_TEXT);
79 | _tf.text = _text;
80 | addChild(_tf);
81 | draw();
82 | }
83 |
84 |
85 |
86 |
87 | ///////////////////////////////////
88 | // public methods
89 | ///////////////////////////////////
90 |
91 | /**
92 | * Draws the visual ui of the component.
93 | */
94 | override public function draw():void
95 | {
96 | super.draw();
97 | _tf.text = _text;
98 | if(_autoSize)
99 | {
100 | _tf.autoSize = TextFieldAutoSize.LEFT;
101 | _width = _tf.width;
102 | dispatchEvent(new Event(Event.RESIZE));
103 | }
104 | else
105 | {
106 | _tf.autoSize = TextFieldAutoSize.NONE;
107 | _tf.width = _width;
108 | }
109 | _height = _tf.height = 18;
110 | }
111 |
112 | ///////////////////////////////////
113 | // event handlers
114 | ///////////////////////////////////
115 |
116 | ///////////////////////////////////
117 | // getter/setters
118 | ///////////////////////////////////
119 |
120 | /**
121 | * Gets / sets the text of this Label.
122 | */
123 | public function set text(t:String):void
124 | {
125 | _text = t;
126 | if(_text == null) _text = "";
127 | invalidate();
128 | }
129 | public function get text():String
130 | {
131 | return _text;
132 | }
133 |
134 | /**
135 | * Gets / sets whether or not this Label will autosize.
136 | */
137 | public function set autoSize(b:Boolean):void
138 | {
139 | _autoSize = b;
140 | }
141 | public function get autoSize():Boolean
142 | {
143 | return _autoSize;
144 | }
145 |
146 | /**
147 | * Gets the internal TextField of the label if you need to do further customization of it.
148 | */
149 | public function get textField():TextField
150 | {
151 | return _tf;
152 | }
153 | }
154 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/ListItem.as:
--------------------------------------------------------------------------------
1 | /**
2 | * ListItem.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A single item in a list.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 |
30 | package com.bit101.components
31 | {
32 | import flash.display.DisplayObjectContainer;
33 | import flash.events.MouseEvent;
34 |
35 | public class ListItem extends Component
36 | {
37 | protected var _data:Object;
38 | protected var _label:Label;
39 | protected var _defaultColor:uint = 0xffffff;
40 | protected var _selectedColor:uint = 0xdddddd;
41 | protected var _rolloverColor:uint = 0xeeeeee;
42 | protected var _selected:Boolean;
43 | protected var _mouseOver:Boolean = false;
44 |
45 | /**
46 | * Constructor
47 | * @param parent The parent DisplayObjectContainer on which to add this ListItem.
48 | * @param xpos The x position to place this component.
49 | * @param ypos The y position to place this component.
50 | * @param data The string to display as a label or object with a label property.
51 | */
52 | public function ListItem(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, data:Object = null)
53 | {
54 | _data = data;
55 | super(parent, xpos, ypos);
56 | }
57 |
58 | /**
59 | * Initilizes the component.
60 | */
61 | protected override function init() : void
62 | {
63 | super.init();
64 | addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
65 | setSize(100, 20);
66 | }
67 |
68 | /**
69 | * Creates and adds the child display objects of this component.
70 | */
71 | protected override function addChildren() : void
72 | {
73 | super.addChildren();
74 | _label = new Label(this, 5, 0);
75 | _label.draw();
76 | }
77 |
78 | ///////////////////////////////////
79 | // public methods
80 | ///////////////////////////////////
81 |
82 | /**
83 | * Draws the visual ui of the component.
84 | */
85 | public override function draw() : void
86 | {
87 | super.draw();
88 | graphics.clear();
89 |
90 | if(_selected)
91 | {
92 | graphics.beginFill(_selectedColor);
93 | }
94 | else if(_mouseOver)
95 | {
96 | graphics.beginFill(_rolloverColor);
97 | }
98 | else
99 | {
100 | graphics.beginFill(_defaultColor);
101 | }
102 | graphics.drawRect(0, 0, width, height);
103 | graphics.endFill();
104 |
105 | if(_data == null) return;
106 |
107 | if(_data is String)
108 | {
109 | _label.text = _data as String;
110 | }
111 | else if(_data.hasOwnProperty("label") && _data.label is String)
112 | {
113 | _label.text = _data.label;
114 | }
115 | else
116 | {
117 | _label.text = _data.toString();
118 | }
119 | }
120 |
121 |
122 |
123 |
124 | ///////////////////////////////////
125 | // event handlers
126 | ///////////////////////////////////
127 |
128 | /**
129 | * Called when the user rolls the mouse over the item. Changes the background color.
130 | */
131 | protected function onMouseOver(event:MouseEvent):void
132 | {
133 | addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
134 | _mouseOver = true;
135 | invalidate();
136 | }
137 |
138 | /**
139 | * Called when the user rolls the mouse off the item. Changes the background color.
140 | */
141 | protected function onMouseOut(event:MouseEvent):void
142 | {
143 | removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
144 | _mouseOver = false;
145 | invalidate();
146 | }
147 |
148 |
149 |
150 | ///////////////////////////////////
151 | // getter/setters
152 | ///////////////////////////////////
153 |
154 | /**
155 | * Sets/gets the string that appears in this item.
156 | */
157 | public function set data(value:Object):void
158 | {
159 | _data = value;
160 | invalidate();
161 | }
162 | public function get data():Object
163 | {
164 | return _data;
165 | }
166 |
167 | /**
168 | * Sets/gets whether or not this item is selected.
169 | */
170 | public function set selected(value:Boolean):void
171 | {
172 | _selected = value;
173 | invalidate();
174 | }
175 | public function get selected():Boolean
176 | {
177 | return _selected;
178 | }
179 |
180 | /**
181 | * Sets/gets the default background color of list items.
182 | */
183 | public function set defaultColor(value:uint):void
184 | {
185 | _defaultColor = value;
186 | invalidate();
187 | }
188 | public function get defaultColor():uint
189 | {
190 | return _defaultColor;
191 | }
192 |
193 | /**
194 | * Sets/gets the selected background color of list items.
195 | */
196 | public function set selectedColor(value:uint):void
197 | {
198 | _selectedColor = value;
199 | invalidate();
200 | }
201 | public function get selectedColor():uint
202 | {
203 | return _selectedColor;
204 | }
205 |
206 | /**
207 | * Sets/gets the rollover background color of list items.
208 | */
209 | public function set rolloverColor(value:uint):void
210 | {
211 | _rolloverColor = value;
212 | invalidate();
213 | }
214 | public function get rolloverColor():uint
215 | {
216 | return _rolloverColor;
217 | }
218 |
219 | }
220 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/Panel.as:
--------------------------------------------------------------------------------
1 | /**
2 | * Panel.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A rectangular panel. Can be used as a container for other components.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObject;
32 | import flash.display.DisplayObjectContainer;
33 | import flash.display.Sprite;
34 |
35 | public class Panel extends Component
36 | {
37 | protected var _mask:Sprite;
38 | protected var _background:Sprite;
39 | protected var _color:int = -1;
40 | protected var _shadow:Boolean = true;
41 | protected var _gridSize:int = 10;
42 | protected var _showGrid:Boolean = false;
43 | protected var _gridColor:uint = 0xd0d0d0;
44 |
45 |
46 | /**
47 | * Container for content added to this panel. This is masked, so best to add children to content, rather than directly to the panel.
48 | */
49 | public var content:Sprite;
50 |
51 |
52 | /**
53 | * Constructor
54 | * @param parent The parent DisplayObjectContainer on which to add this Panel.
55 | * @param xpos The x position to place this component.
56 | * @param ypos The y position to place this component.
57 | */
58 | public function Panel(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0)
59 | {
60 | super(parent, xpos, ypos);
61 | }
62 |
63 |
64 | /**
65 | * Initializes the component.
66 | */
67 | override protected function init():void
68 | {
69 | super.init();
70 | setSize(100, 100);
71 | }
72 |
73 | /**
74 | * Creates and adds the child display objects of this component.
75 | */
76 | override protected function addChildren():void
77 | {
78 | _background = new Sprite();
79 | super.addChild(_background);
80 |
81 | _mask = new Sprite();
82 | _mask.mouseEnabled = false;
83 | super.addChild(_mask);
84 |
85 | content = new Sprite();
86 | super.addChild(content);
87 | content.mask = _mask;
88 |
89 | filters = [getShadow(2, true)];
90 | }
91 |
92 |
93 |
94 |
95 | ///////////////////////////////////
96 | // public methods
97 | ///////////////////////////////////
98 |
99 | /**
100 | * Overridden to add new child to content.
101 | */
102 | public override function addChild(child:DisplayObject):DisplayObject
103 | {
104 | content.addChild(child);
105 | return child;
106 | }
107 |
108 | /**
109 | * Access to super.addChild
110 | */
111 | public function addRawChild(child:DisplayObject):DisplayObject
112 | {
113 | super.addChild(child);
114 | return child;
115 | }
116 |
117 | /**
118 | * Draws the visual ui of the component.
119 | */
120 | override public function draw():void
121 | {
122 | super.draw();
123 | _background.graphics.clear();
124 | _background.graphics.lineStyle(1, 0, 0.1);
125 | if(_color == -1)
126 | {
127 | _background.graphics.beginFill(Style.PANEL);
128 | }
129 | else
130 | {
131 | _background.graphics.beginFill(_color);
132 | }
133 | _background.graphics.drawRect(0, 0, _width, _height);
134 | _background.graphics.endFill();
135 |
136 | drawGrid();
137 |
138 | _mask.graphics.clear();
139 | _mask.graphics.beginFill(0xff0000);
140 | _mask.graphics.drawRect(0, 0, _width, _height);
141 | _mask.graphics.endFill();
142 | }
143 |
144 | protected function drawGrid():void
145 | {
146 | if(!_showGrid) return;
147 |
148 | _background.graphics.lineStyle(0, _gridColor);
149 | for(var i:int = 0; i < _width; i += _gridSize)
150 | {
151 | _background.graphics.moveTo(i, 0);
152 | _background.graphics.lineTo(i, _height);
153 | }
154 | for(i = 0; i < _height; i += _gridSize)
155 | {
156 | _background.graphics.moveTo(0, i);
157 | _background.graphics.lineTo(_width, i);
158 | }
159 | }
160 |
161 |
162 |
163 | ///////////////////////////////////
164 | // event handlers
165 | ///////////////////////////////////
166 |
167 | ///////////////////////////////////
168 | // getter/setters
169 | ///////////////////////////////////
170 |
171 | /**
172 | * Gets / sets whether or not this Panel will have an inner shadow.
173 | */
174 | public function set shadow(b:Boolean):void
175 | {
176 | _shadow = b;
177 | if(_shadow)
178 | {
179 | filters = [getShadow(2, true)];
180 | }
181 | else
182 | {
183 | filters = [];
184 | }
185 | }
186 | public function get shadow():Boolean
187 | {
188 | return _shadow;
189 | }
190 |
191 | /**
192 | * Gets / sets the backgrond color of this panel.
193 | */
194 | public function set color(c:int):void
195 | {
196 | _color = c;
197 | invalidate();
198 | }
199 | public function get color():int
200 | {
201 | return _color;
202 | }
203 |
204 | /**
205 | * Sets / gets the size of the grid.
206 | */
207 | public function set gridSize(value:int):void
208 | {
209 | _gridSize = value;
210 | invalidate();
211 | }
212 | public function get gridSize():int
213 | {
214 | return _gridSize;
215 | }
216 |
217 | /**
218 | * Sets / gets whether or not the grid will be shown.
219 | */
220 | public function set showGrid(value:Boolean):void
221 | {
222 | _showGrid = value;
223 | invalidate();
224 | }
225 | public function get showGrid():Boolean
226 | {
227 | return _showGrid;
228 | }
229 |
230 | /**
231 | * Sets / gets the color of the grid lines.
232 | */
233 | public function set gridColor(value:uint):void
234 | {
235 | _gridColor = value;
236 | invalidate();
237 | }
238 | public function get gridColor():uint
239 | {
240 | return _gridColor;
241 | }
242 | }
243 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/ProgressBar.as:
--------------------------------------------------------------------------------
1 | /**
2 | * ProgressBar.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A progress bar component for showing a changing value in relation to a total.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 | import flash.display.Sprite;
33 |
34 | public class ProgressBar extends Component
35 | {
36 | protected var _back:Sprite;
37 | protected var _bar:Sprite;
38 | protected var _value:Number = 0;
39 | protected var _max:Number = 1;
40 |
41 | /**
42 | * Constructor
43 | * @param parent The parent DisplayObjectContainer on which to add this ProgressBar.
44 | * @param xpos The x position to place this component.
45 | * @param ypos The y position to place this component.
46 | */
47 | public function ProgressBar(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0)
48 | {
49 | super(parent, xpos, ypos);
50 | }
51 |
52 |
53 | /**
54 | * Initializes the component.
55 | */
56 | override protected function init():void
57 | {
58 | super.init();
59 | setSize(100, 10);
60 | }
61 |
62 | /**
63 | * Creates and adds the child display objects of this component.
64 | */
65 | override protected function addChildren():void
66 | {
67 | _back = new Sprite();
68 | _back.filters = [getShadow(2, true)];
69 | addChild(_back);
70 |
71 | _bar = new Sprite();
72 | _bar.x = 1;
73 | _bar.y = 1;
74 | _bar.filters = [getShadow(1)];
75 | addChild(_bar);
76 | }
77 |
78 | /**
79 | * Updates the size of the progress bar based on the current value.
80 | */
81 | protected function update():void
82 | {
83 | _bar.scaleX = _value / _max;
84 | }
85 |
86 |
87 |
88 |
89 | ///////////////////////////////////
90 | // public methods
91 | ///////////////////////////////////
92 |
93 | /**
94 | * Draws the visual ui of the component.
95 | */
96 | override public function draw():void
97 | {
98 | super.draw();
99 | _back.graphics.clear();
100 | _back.graphics.beginFill(Style.BACKGROUND);
101 | _back.graphics.drawRect(0, 0, _width, _height);
102 | _back.graphics.endFill();
103 |
104 | _bar.graphics.clear();
105 | _bar.graphics.beginFill(Style.PROGRESS_BAR);
106 | _bar.graphics.drawRect(0, 0, _width - 2, _height - 2);
107 | _bar.graphics.endFill();
108 | update();
109 | }
110 |
111 |
112 |
113 |
114 | ///////////////////////////////////
115 | // event handlers
116 | ///////////////////////////////////
117 |
118 | ///////////////////////////////////
119 | // getter/setters
120 | ///////////////////////////////////
121 |
122 | /**
123 | * Gets / sets the maximum value of the ProgressBar.
124 | */
125 | public function set maximum(m:Number):void
126 | {
127 | _max = m;
128 | _value = Math.min(_value, _max);
129 | update();
130 | }
131 | public function get maximum():Number
132 | {
133 | return _max;
134 | }
135 |
136 | /**
137 | * Gets / sets the current value of the ProgressBar.
138 | */
139 | public function set value(v:Number):void
140 | {
141 | _value = Math.min(v, _max);
142 | update();
143 | }
144 | public function get value():Number
145 | {
146 | return _value;
147 | }
148 |
149 | }
150 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/PushButton.as:
--------------------------------------------------------------------------------
1 | /**
2 | * PushButton.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A basic button component with a label.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 | import flash.display.Sprite;
33 | import flash.events.MouseEvent;
34 |
35 | public class PushButton extends Component
36 | {
37 | protected var _back:Sprite;
38 | protected var _face:Sprite;
39 | protected var _label:Label;
40 | protected var _labelText:String = "";
41 | protected var _over:Boolean = false;
42 | protected var _down:Boolean = false;
43 | protected var _selected:Boolean = false;
44 | protected var _toggle:Boolean = false;
45 |
46 | private var bgcolor:uint;
47 |
48 | /**
49 | * Constructor
50 | * @param parent The parent DisplayObjectContainer on which to add this PushButton.
51 | * @param xpos The x position to place this component.
52 | * @param ypos The y position to place this component.
53 | * @param label The string to use for the initial label of this component.
54 | * @param defaultHandler The event handling function to handle the default event for this component (click in this case).
55 | */
56 | public function PushButton(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, label:String = "", defaultHandler:Function = null,bgcolor:uint=0xeeeeee)
57 | {
58 | super(parent, xpos, ypos);
59 | this.bgcolor = bgcolor;
60 | if(defaultHandler != null)
61 | {
62 | addEventListener(MouseEvent.CLICK, defaultHandler);
63 | }
64 | this.label = label;
65 | }
66 |
67 | /**
68 | * Initializes the component.
69 | */
70 | override protected function init():void
71 | {
72 | super.init();
73 | buttonMode = true;
74 | useHandCursor = true;
75 | setSize(100, 20);
76 | }
77 |
78 | /**
79 | * Creates and adds the child display objects of this component.
80 | */
81 | override protected function addChildren():void
82 | {
83 | _back = new Sprite();
84 | _back.filters = [getShadow(2, true)];
85 | _back.mouseEnabled = false;
86 | addChild(_back);
87 |
88 | _face = new Sprite();
89 | _face.mouseEnabled = false;
90 | _face.filters = [getShadow(1)];
91 | _face.x = 1;
92 | _face.y = 1;
93 | addChild(_face);
94 |
95 | _label = new Label();
96 | addChild(_label);
97 |
98 | addEventListener(MouseEvent.MOUSE_DOWN, onMouseGoDown);
99 | addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
100 | }
101 |
102 | /**
103 | * Draws the face of the button, color based on state.
104 | */
105 | protected function drawFace():void
106 | {
107 | _face.graphics.clear();
108 | if(_down)
109 | {
110 | _face.graphics.beginFill(Style.BUTTON_DOWN);
111 | }
112 | else
113 | {
114 | _face.graphics.beginFill(bgcolor);
115 | }
116 | _face.graphics.drawRect(0, 0, _width - 2, _height - 2);
117 | _face.graphics.endFill();
118 | }
119 |
120 |
121 | ///////////////////////////////////
122 | // public methods
123 | ///////////////////////////////////
124 |
125 | /**
126 | * Draws the visual ui of the component.
127 | */
128 | override public function draw():void
129 | {
130 | super.draw();
131 | _back.graphics.clear();
132 | _back.graphics.beginFill(Style.BACKGROUND);
133 | _back.graphics.drawRect(0, 0, _width, _height);
134 | _back.graphics.endFill();
135 |
136 | drawFace();
137 |
138 | _label.text = _labelText;
139 | _label.autoSize = true;
140 | _label.draw();
141 | if(_label.width > _width - 4)
142 | {
143 | _label.autoSize = false;
144 | _label.width = _width - 4;
145 | }
146 | else
147 | {
148 | _label.autoSize = true;
149 | }
150 | _label.draw();
151 | _label.move(_width / 2 - _label.width / 2, _height / 2 - _label.height / 2);
152 |
153 | }
154 |
155 |
156 |
157 |
158 | ///////////////////////////////////
159 | // event handlers
160 | ///////////////////////////////////
161 |
162 | /**
163 | * Internal mouseOver handler.
164 | * @param event The MouseEvent passed by the system.
165 | */
166 | protected function onMouseOver(event:MouseEvent):void
167 | {
168 | _over = true;
169 | addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
170 | }
171 |
172 | /**
173 | * Internal mouseOut handler.
174 | * @param event The MouseEvent passed by the system.
175 | */
176 | protected function onMouseOut(event:MouseEvent):void
177 | {
178 | _over = false;
179 | if(!_down)
180 | {
181 | _face.filters = [getShadow(1)];
182 | }
183 | removeEventListener(MouseEvent.ROLL_OUT, onMouseOut);
184 | }
185 |
186 | /**
187 | * Internal mouseOut handler.
188 | * @param event The MouseEvent passed by the system.
189 | */
190 | protected function onMouseGoDown(event:MouseEvent):void
191 | {
192 | _down = true;
193 | drawFace();
194 | _face.filters = [getShadow(1, true)];
195 | stage.addEventListener(MouseEvent.MOUSE_UP, onMouseGoUp);
196 | }
197 |
198 | /**
199 | * Internal mouseUp handler.
200 | * @param event The MouseEvent passed by the system.
201 | */
202 | protected function onMouseGoUp(event:MouseEvent):void
203 | {
204 | if(_toggle && _over)
205 | {
206 | _selected = !_selected;
207 | }
208 | _down = _selected;
209 | drawFace();
210 | _face.filters = [getShadow(1, _selected)];
211 | stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseGoUp);
212 | }
213 |
214 |
215 |
216 |
217 | ///////////////////////////////////
218 | // getter/setters
219 | ///////////////////////////////////
220 |
221 | /**
222 | * Sets / gets the label text shown on this Pushbutton.
223 | */
224 | public function set label(str:String):void
225 | {
226 | _labelText = str;
227 | draw();
228 | }
229 | public function get label():String
230 | {
231 | return _labelText;
232 | }
233 |
234 | public function set selected(value:Boolean):void
235 | {
236 | if(!_toggle)
237 | {
238 | value = false;
239 | }
240 |
241 | _selected = value;
242 | _down = _selected;
243 | _face.filters = [getShadow(1, _selected)];
244 | drawFace();
245 | }
246 | public function get selected():Boolean
247 | {
248 | return _selected;
249 | }
250 |
251 | public function set toggle(value:Boolean):void
252 | {
253 | _toggle = value;
254 | }
255 | public function get toggle():Boolean
256 | {
257 | return _toggle;
258 | }
259 |
260 |
261 | }
262 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/RadioButton.as:
--------------------------------------------------------------------------------
1 | /**
2 | * RadioButton.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A basic radio button component, meant to be used in groups, where only one button in the group can be selected.
7 | * Currently only one group can be created.
8 | *
9 | * Copyright (c) 2011 Keith Peters
10 | *
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy
12 | * of this software and associated documentation files (the "Software"), to deal
13 | * in the Software without restriction, including without limitation the rights
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 | * copies of the Software, and to permit persons to whom the Software is
16 | * furnished to do so, subject to the following conditions:
17 | *
18 | * The above copyright notice and this permission notice shall be included in
19 | * all copies or substantial portions of the Software.
20 | *
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 | * THE SOFTWARE.
28 | */
29 |
30 | package com.bit101.components
31 | {
32 | import flash.display.DisplayObjectContainer;
33 | import flash.display.Sprite;
34 | import flash.events.MouseEvent;
35 |
36 | public class RadioButton extends Component
37 | {
38 | protected var _back:Sprite;
39 | protected var _button:Sprite;
40 | protected var _selected:Boolean = false;
41 | protected var _label:Label;
42 | protected var _labelText:String = "";
43 | protected var _groupName:String = "defaultRadioGroup";
44 |
45 | protected static var buttons:Array;
46 |
47 |
48 | /**
49 | * Constructor
50 | * @param parent The parent DisplayObjectContainer on which to add this RadioButton.
51 | * @param xpos The x position to place this component.
52 | * @param ypos The y position to place this component.
53 | * @param label The string to use for the initial label of this component.
54 | * @param defaultHandler The event handling function to handle the default event for this component (click in this case).
55 | */
56 | public function RadioButton(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, label:String = "", checked:Boolean = false, defaultHandler:Function = null)
57 | {
58 | RadioButton.addButton(this);
59 | _selected = checked;
60 | _labelText = label;
61 | super(parent, xpos, ypos);
62 | if(defaultHandler != null)
63 | {
64 | addEventListener(MouseEvent.CLICK, defaultHandler);
65 | }
66 | }
67 |
68 | /**
69 | * Static method to add the newly created RadioButton to the list of buttons in the group.
70 | * @param rb The RadioButton to add.
71 | */
72 | protected static function addButton(rb:RadioButton):void
73 | {
74 | if(buttons == null)
75 | {
76 | buttons = new Array();
77 | }
78 | buttons.push(rb);
79 | }
80 |
81 | /**
82 | * Unselects all RadioButtons in the group, except the one passed.
83 | * This could use some rethinking or better naming.
84 | * @param rb The RadioButton to remain selected.
85 | */
86 | protected static function clear(rb:RadioButton):void
87 | {
88 | for(var i:uint = 0; i < buttons.length; i++)
89 | {
90 | if(buttons[i] != rb && buttons[i].groupName == rb.groupName)
91 | {
92 | buttons[i].selected = false;
93 | }
94 | }
95 | }
96 |
97 | /**
98 | * Initializes the component.
99 | */
100 | override protected function init():void
101 | {
102 | super.init();
103 |
104 | buttonMode = true;
105 | useHandCursor = true;
106 |
107 | addEventListener(MouseEvent.CLICK, onClick, false, 1);
108 | selected = _selected;
109 | }
110 |
111 | /**
112 | * Creates and adds the child display objects of this component.
113 | */
114 | override protected function addChildren():void
115 | {
116 | _back = new Sprite();
117 | _back.filters = [getShadow(2, true)];
118 | addChild(_back);
119 |
120 | _button = new Sprite();
121 | _button.filters = [getShadow(1)];
122 | _button.visible = false;
123 | addChild(_button);
124 |
125 | _label = new Label(this, 0, 0, _labelText);
126 | draw();
127 |
128 | mouseChildren = false;
129 | }
130 |
131 |
132 |
133 |
134 | ///////////////////////////////////
135 | // public methods
136 | ///////////////////////////////////
137 |
138 | /**
139 | * Draws the visual ui of the component.
140 | */
141 | override public function draw():void
142 | {
143 | super.draw();
144 | _back.graphics.clear();
145 | _back.graphics.beginFill(Style.BACKGROUND);
146 | _back.graphics.drawCircle(5, 5, 5);
147 | _back.graphics.endFill();
148 |
149 | _button.graphics.clear();
150 | _button.graphics.beginFill(Style.BUTTON_FACE);
151 | _button.graphics.drawCircle(5, 5, 3);
152 |
153 | _label.x = 12;
154 | _label.y = (10 - _label.height) / 2;
155 | _label.text = _labelText;
156 | _label.draw();
157 | _width = _label.width + 12;
158 | _height = 10;
159 | }
160 |
161 |
162 |
163 |
164 | ///////////////////////////////////
165 | // event handlers
166 | ///////////////////////////////////
167 |
168 | /**
169 | * Internal click handler.
170 | * @param event The MouseEvent passed by the system.
171 | */
172 | protected function onClick(event:MouseEvent):void
173 | {
174 | selected = true;
175 | }
176 |
177 |
178 |
179 |
180 | ///////////////////////////////////
181 | // getter/setters
182 | ///////////////////////////////////
183 |
184 | /**
185 | * Sets / gets the selected state of this CheckBox.
186 | */
187 | public function set selected(s:Boolean):void
188 | {
189 | _selected = s;
190 | _button.visible = _selected;
191 | if(_selected)
192 | {
193 | RadioButton.clear(this);
194 | }
195 | }
196 | public function get selected():Boolean
197 | {
198 | return _selected;
199 | }
200 |
201 | /**
202 | * Sets / gets the label text shown on this CheckBox.
203 | */
204 | public function set label(str:String):void
205 | {
206 | _labelText = str;
207 | invalidate();
208 | }
209 | public function get label():String
210 | {
211 | return _labelText;
212 | }
213 |
214 | /**
215 | * Sets / gets the group name, which allows groups of RadioButtons to function seperately.
216 | */
217 | public function get groupName():String
218 | {
219 | return _groupName;
220 | }
221 |
222 | public function set groupName(value:String):void
223 | {
224 | _groupName = value;
225 | }
226 |
227 |
228 | }
229 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/ScrollPane.as:
--------------------------------------------------------------------------------
1 | /**
2 | * ScrollPane.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A panel with scroll bars for scrolling content that is larger.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 |
32 | import flash.display.DisplayObjectContainer;
33 | import flash.display.Shape;
34 | import flash.events.Event;
35 | import flash.events.MouseEvent;
36 | import flash.geom.Rectangle;
37 |
38 | public class ScrollPane extends Panel
39 | {
40 | protected var _vScrollbar:VScrollBar;
41 | protected var _hScrollbar:HScrollBar;
42 | protected var _corner:Shape;
43 | protected var _dragContent:Boolean = true;
44 |
45 | /**
46 | * Constructor
47 | * @param parent The parent DisplayObjectContainer on which to add this ScrollPane.
48 | * @param xpos The x position to place this component.
49 | * @param ypos The y position to place this component.
50 | */
51 | public function ScrollPane(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
52 | {
53 | super(parent, xpos, ypos);
54 | }
55 |
56 | /**
57 | * Initializes this component.
58 | */
59 | override protected function init():void
60 | {
61 | super.init();
62 | addEventListener(Event.RESIZE, onResize);
63 | _background.addEventListener(MouseEvent.MOUSE_DOWN, onMouseGoDown);
64 | _background.useHandCursor = true;
65 | _background.buttonMode = true;
66 | setSize(100, 100);
67 | }
68 |
69 | /**
70 | * Creates and adds the child display objects of this component.
71 | */
72 | override protected function addChildren():void
73 | {
74 | super.addChildren();
75 | _vScrollbar = new VScrollBar(null, width - 10, 0, onScroll);
76 | _hScrollbar = new HScrollBar(null, 0, height - 10, onScroll);
77 | addRawChild(_vScrollbar);
78 | addRawChild(_hScrollbar);
79 | _corner = new Shape();
80 | _corner.graphics.beginFill(Style.BUTTON_FACE);
81 | _corner.graphics.drawRect(0, 0, 10, 10);
82 | _corner.graphics.endFill();
83 | addRawChild(_corner);
84 | }
85 |
86 |
87 |
88 |
89 |
90 | ///////////////////////////////////
91 | // public methods
92 | ///////////////////////////////////
93 |
94 | /**
95 | * Draws the visual ui of the component.
96 | */
97 | override public function draw():void
98 | {
99 | super.draw();
100 |
101 | var vPercent:Number = (_height - 10) / content.height;
102 | var hPercent:Number = (_width - 10) / content.width;
103 |
104 | _vScrollbar.x = width - 10;
105 | _hScrollbar.y = height - 10;
106 |
107 | if(hPercent >= 1)
108 | {
109 | _vScrollbar.height = height;
110 | _mask.height = height;
111 | }
112 | else
113 | {
114 | _vScrollbar.height = height - 10;
115 | _mask.height = height - 10;
116 | }
117 | if(vPercent >= 1)
118 | {
119 | _hScrollbar.width = width;
120 | _mask.width = width;
121 | }
122 | else
123 | {
124 | _hScrollbar.width = width - 10;
125 | _mask.width = width - 10;
126 | }
127 | _vScrollbar.setThumbPercent(vPercent);
128 | _vScrollbar.maximum = Math.max(0, content.height - _height + 10);
129 | _vScrollbar.pageSize = _height - 10;
130 |
131 | _hScrollbar.setThumbPercent(hPercent);
132 | _hScrollbar.maximum = Math.max(0, content.width - _width + 10);
133 | _hScrollbar.pageSize = _width - 10;
134 |
135 | _corner.x = width - 10;
136 | _corner.y = height - 10;
137 | _corner.visible = (hPercent < 1) && (vPercent < 1);
138 | content.x = -_hScrollbar.value;
139 | content.y = -_vScrollbar.value;
140 | }
141 |
142 | /**
143 | * Updates the scrollbars when content is changed. Needs to be done manually.
144 | */
145 | public function update():void
146 | {
147 | invalidate();
148 | }
149 |
150 |
151 | ///////////////////////////////////
152 | // event handlers
153 | ///////////////////////////////////
154 |
155 | /**
156 | * Called when either scroll bar is scrolled.
157 | */
158 | protected function onScroll(event:Event):void
159 | {
160 | content.x = -_hScrollbar.value;
161 | content.y = -_vScrollbar.value;
162 | }
163 |
164 | protected function onResize(event:Event):void
165 | {
166 | invalidate();
167 | }
168 |
169 | protected function onMouseGoDown(event:MouseEvent):void
170 | {
171 | content.startDrag(false, new Rectangle(0, 0, Math.min(0, _width - content.width - 10), Math.min(0, _height - content.height - 10)));
172 | stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
173 | stage.addEventListener(MouseEvent.MOUSE_UP, onMouseGoUp);
174 | }
175 |
176 | protected function onMouseMove(event:MouseEvent):void
177 | {
178 | _hScrollbar.value = -content.x;
179 | _vScrollbar.value = -content.y;
180 | }
181 |
182 | protected function onMouseGoUp(event:MouseEvent):void
183 | {
184 | content.stopDrag();
185 | stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
186 | stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseGoUp);
187 | }
188 |
189 | public function set dragContent(value:Boolean):void
190 | {
191 | _dragContent = value;
192 | if(_dragContent)
193 | {
194 | _background.addEventListener(MouseEvent.MOUSE_DOWN, onMouseGoDown);
195 | _background.useHandCursor = true;
196 | _background.buttonMode = true;
197 | }
198 | else
199 | {
200 | _background.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseGoDown);
201 | _background.useHandCursor = false;
202 | _background.buttonMode = false;
203 | }
204 | }
205 | public function get dragContent():Boolean
206 | {
207 | return _dragContent;
208 | }
209 |
210 | /**
211 | * Sets / gets whether the scrollbar will auto hide when there is nothing to scroll.
212 | */
213 | public function set autoHideScrollBar(value:Boolean):void
214 | {
215 | _vScrollbar.autoHide = value;
216 | _hScrollbar.autoHide = value;
217 | }
218 | public function get autoHideScrollBar():Boolean
219 | {
220 | return _vScrollbar.autoHide;
221 | }
222 |
223 |
224 | }
225 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/Style.as:
--------------------------------------------------------------------------------
1 | /**
2 | * Style.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A collection of style variables used by the components.
7 | * If you want to customize the colors of your components, change these values BEFORE instantiating any components.
8 | *
9 | * Copyright (c) 2011 Keith Peters
10 | *
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy
12 | * of this software and associated documentation files (the "Software"), to deal
13 | * in the Software without restriction, including without limitation the rights
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 | * copies of the Software, and to permit persons to whom the Software is
16 | * furnished to do so, subject to the following conditions:
17 | *
18 | * The above copyright notice and this permission notice shall be included in
19 | * all copies or substantial portions of the Software.
20 | *
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 | * THE SOFTWARE.
28 | */
29 |
30 | package com.bit101.components
31 | {
32 |
33 | public class Style
34 | {
35 | public static var TEXT_BACKGROUND:uint = 0xFFFFFF;
36 | public static var BACKGROUND:uint = 0xaaaaaa;
37 | public static var BUTTON_FACE:uint = 0xeeeeee;
38 | public static var BUTTON_DOWN:uint = 0xbbbbbb;
39 | public static var INPUT_TEXT:uint = 0x333333;
40 | public static var LABEL_TEXT:uint = 0x666666;
41 | public static var DROPSHADOW:uint = 0x777777;
42 | public static var PANEL:uint = 0xF3F3F3;
43 | public static var PROGRESS_BAR:uint = 0xFFFFFF;
44 | public static var LIST_DEFAULT:uint = 0xFFFFFF;
45 | public static var LIST_ALTERNATE:uint = 0xF3F3F3;
46 | public static var LIST_SELECTED:uint = 0xCCCCCC;
47 | public static var LIST_ROLLOVER:uint = 0XDDDDDD;
48 |
49 | public static var embedFonts:Boolean = true;
50 | public static var fontName:String = "PF Ronda Seven";
51 | public static var fontSize:Number = 8;
52 |
53 | public static const DARK:String = "dark";
54 | public static const LIGHT:String = "light";
55 |
56 | /**
57 | * Applies a preset style as a list of color values. Should be called before creating any components.
58 | */
59 | public static function setStyle(style:String):void
60 | {
61 | switch(style)
62 | {
63 | case DARK:
64 | Style.BACKGROUND = 0x444444;
65 | Style.BUTTON_FACE = 0x666666;
66 | Style.BUTTON_DOWN = 0x222222;
67 | Style.INPUT_TEXT = 0xBBBBBB;
68 | Style.LABEL_TEXT = 0xCCCCCC;
69 | Style.PANEL = 0x666666;
70 | Style.PROGRESS_BAR = 0x666666;
71 | Style.TEXT_BACKGROUND = 0x555555;
72 | Style.LIST_DEFAULT = 0x444444;
73 | Style.LIST_ALTERNATE = 0x393939;
74 | Style.LIST_SELECTED = 0x666666;
75 | Style.LIST_ROLLOVER = 0x777777;
76 | break;
77 | case LIGHT:
78 | default:
79 | Style.BACKGROUND = 0xCCCCCC;
80 | Style.BUTTON_FACE = 0xFFFFFF;
81 | Style.BUTTON_DOWN = 0xEEEEEE;
82 | Style.INPUT_TEXT = 0x333333;
83 | Style.LABEL_TEXT = 0x666666;
84 | Style.PANEL = 0xF3F3F3;
85 | Style.PROGRESS_BAR = 0xFFFFFF;
86 | Style.TEXT_BACKGROUND = 0xFFFFFF;
87 | Style.LIST_DEFAULT = 0xFFFFFF;
88 | Style.LIST_ALTERNATE = 0xF3F3F3;
89 | Style.LIST_SELECTED = 0xCCCCCC;
90 | Style.LIST_ROLLOVER = 0xDDDDDD;
91 | break;
92 | }
93 | }
94 | }
95 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/Text.as:
--------------------------------------------------------------------------------
1 | /**
2 | * Text.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A Text component for displaying multiple lines of text.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 | import flash.events.Event;
33 | import flash.text.TextField;
34 | import flash.text.TextFieldType;
35 | import flash.text.TextFormat;
36 |
37 | [Event(name="change", type="flash.events.Event")]
38 | public class Text extends Component
39 | {
40 | protected var _tf:TextField;
41 | protected var _text:String = "";
42 | protected var _editable:Boolean = true;
43 | protected var _panel:Panel;
44 | protected var _selectable:Boolean = true;
45 | protected var _html:Boolean = false;
46 | protected var _format:TextFormat;
47 |
48 | /**
49 | * Constructor
50 | * @param parent The parent DisplayObjectContainer on which to add this Label.
51 | * @param xpos The x position to place this component.
52 | * @param ypos The y position to place this component.
53 | * @param text The initial text to display in this component.
54 | */
55 | public function Text(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, text:String = "")
56 | {
57 | this.text = text;
58 | super(parent, xpos, ypos);
59 | setSize(200, 100);
60 | }
61 |
62 | /**
63 | * Initializes the component.
64 | */
65 | override protected function init():void
66 | {
67 | super.init();
68 | }
69 |
70 | /**
71 | * Creates and adds the child display objects of this component.
72 | */
73 | override protected function addChildren():void
74 | {
75 | _panel = new Panel(this);
76 | _panel.color = Style.TEXT_BACKGROUND;
77 |
78 | _format = new TextFormat(Style.fontName, Style.fontSize, Style.LABEL_TEXT);
79 |
80 | _tf = new TextField();
81 | _tf.x = 2;
82 | _tf.y = 2;
83 | _tf.height = _height;
84 | _tf.embedFonts = Style.embedFonts;
85 | _tf.multiline = true;
86 | _tf.wordWrap = true;
87 | _tf.selectable = true;
88 | _tf.type = TextFieldType.INPUT;
89 | _tf.defaultTextFormat = _format;
90 | _tf.addEventListener(Event.CHANGE, onChange);
91 | addChild(_tf);
92 | }
93 |
94 |
95 |
96 |
97 | ///////////////////////////////////
98 | // public methods
99 | ///////////////////////////////////
100 |
101 | /**
102 | * Draws the visual ui of the component.
103 | */
104 | override public function draw():void
105 | {
106 | super.draw();
107 |
108 | _panel.setSize(_width, _height);
109 | _panel.draw();
110 |
111 | _tf.width = _width - 4;
112 | _tf.height = _height - 4;
113 | if(_html)
114 | {
115 | _tf.htmlText = _text;
116 | }
117 | else
118 | {
119 | _tf.text = _text;
120 | }
121 | if(_editable)
122 | {
123 | _tf.mouseEnabled = true;
124 | _tf.selectable = true;
125 | _tf.type = TextFieldType.INPUT;
126 | }
127 | else
128 | {
129 | _tf.mouseEnabled = _selectable;
130 | _tf.selectable = _selectable;
131 | _tf.type = TextFieldType.DYNAMIC;
132 | }
133 | _tf.setTextFormat(_format);
134 | }
135 |
136 |
137 |
138 |
139 | ///////////////////////////////////
140 | // event handlers
141 | ///////////////////////////////////
142 |
143 | /**
144 | * Called when the text in the text field is manually changed.
145 | */
146 | protected function onChange(event:Event):void
147 | {
148 | _text = _tf.text;
149 | dispatchEvent(event);
150 | }
151 |
152 | ///////////////////////////////////
153 | // getter/setters
154 | ///////////////////////////////////
155 |
156 | /**
157 | * Gets / sets the text of this Label.
158 | */
159 | public function set text(t:String):void
160 | {
161 | _text = t;
162 | if(_text == null) _text = "";
163 | invalidate();
164 | }
165 | public function get text():String
166 | {
167 | return _text;
168 | }
169 |
170 | /**
171 | * Returns a reference to the internal text field in the component.
172 | */
173 | public function get textField():TextField
174 | {
175 | return _tf;
176 | }
177 |
178 | /**
179 | * Gets / sets whether or not this text component will be editable.
180 | */
181 | public function set editable(b:Boolean):void
182 | {
183 | _editable = b;
184 | invalidate();
185 | }
186 | public function get editable():Boolean
187 | {
188 | return _editable;
189 | }
190 |
191 | /**
192 | * Gets / sets whether or not this text component will be selectable. Only meaningful if editable is false.
193 | */
194 | public function set selectable(b:Boolean):void
195 | {
196 | _selectable = b;
197 | invalidate();
198 | }
199 | public function get selectable():Boolean
200 | {
201 | return _selectable;
202 | }
203 |
204 | /**
205 | * Gets / sets whether or not text will be rendered as HTML or plain text.
206 | */
207 | public function set html(b:Boolean):void
208 | {
209 | _html = b;
210 | invalidate();
211 | }
212 | public function get html():Boolean
213 | {
214 | return _html;
215 | }
216 |
217 | /**
218 | * Sets/gets whether this component is enabled or not.
219 | */
220 | public override function set enabled(value:Boolean):void
221 | {
222 | super.enabled = value;
223 | _tf.tabEnabled = value;
224 | }
225 |
226 | }
227 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/TextArea.as:
--------------------------------------------------------------------------------
1 | /**
2 | * TextArea.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A Text component for displaying multiple lines of text with a scrollbar.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 | import flash.events.Event;
33 | import flash.events.MouseEvent;
34 |
35 | public class TextArea extends Text
36 | {
37 | protected var _scrollbar:VScrollBar;
38 |
39 | /**
40 | * Constructor
41 | * @param parent The parent DisplayObjectContainer on which to add this Label.
42 | * @param xpos The x position to place this component.
43 | * @param ypos The y position to place this component.
44 | * @param text The initial text to display in this component.
45 | */
46 | public function TextArea(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, text:String="")
47 | {
48 | super(parent, xpos, ypos, text);
49 | }
50 |
51 | /**
52 | * Initilizes the component.
53 | */
54 | protected override function init() : void
55 | {
56 | super.init();
57 | addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
58 | }
59 | /**
60 | * Creates and adds the child display objects of this component.
61 | */
62 | override protected function addChildren():void
63 | {
64 | super.addChildren();
65 | _scrollbar = new VScrollBar(this, 0, 0, onScrollbarScroll);
66 | _tf.addEventListener(Event.SCROLL, onTextScroll);
67 | }
68 |
69 | /**
70 | * Changes the thumb percent of the scrollbar based on how much text is shown in the text area.
71 | */
72 | protected function updateScrollbar():void
73 | {
74 | var visibleLines:int = _tf.numLines - _tf.maxScrollV + 1;
75 | var percent:Number = visibleLines / _tf.numLines;
76 | _scrollbar.setSliderParams(1, _tf.maxScrollV, _tf.scrollV);
77 | _scrollbar.setThumbPercent(percent);
78 | _scrollbar.pageSize = visibleLines;
79 | }
80 |
81 |
82 |
83 |
84 | ///////////////////////////////////
85 | // public methods
86 | ///////////////////////////////////
87 |
88 | /**
89 | * Draws the visual ui of the component.
90 | */
91 | override public function draw():void
92 | {
93 | super.draw();
94 |
95 | _tf.width = _width - _scrollbar.width - 4;
96 | _scrollbar.x = _width - _scrollbar.width;
97 | _scrollbar.height = _height;
98 | _scrollbar.draw();
99 | addEventListener(Event.ENTER_FRAME, onTextScrollDelay);
100 | }
101 |
102 |
103 |
104 | ///////////////////////////////////
105 | // event handlers
106 | ///////////////////////////////////
107 |
108 | /**
109 | * Waits one more frame before updating scroll bar.
110 | * It seems that numLines and maxScrollV are not valid immediately after changing a TextField's size.
111 | */
112 | protected function onTextScrollDelay(event:Event):void
113 | {
114 | removeEventListener(Event.ENTER_FRAME, onTextScrollDelay);
115 | updateScrollbar();
116 | }
117 |
118 | /**
119 | * Called when the text in the text field is manually changed.
120 | */
121 | protected override function onChange(event:Event):void
122 | {
123 | super.onChange(event);
124 | updateScrollbar();
125 | }
126 |
127 | /**
128 | * Called when the scroll bar is moved. Scrolls text accordingly.
129 | */
130 | protected function onScrollbarScroll(event:Event):void
131 | {
132 | _tf.scrollV = Math.round(_scrollbar.value);
133 | }
134 |
135 | /**
136 | * Called when the text is scrolled manually. Updates the position of the scroll bar.
137 | */
138 | protected function onTextScroll(event:Event):void
139 | {
140 | _scrollbar.value = _tf.scrollV;
141 | updateScrollbar();
142 | }
143 |
144 | /**
145 | * Called when the mouse wheel is scrolled over the component.
146 | */
147 | protected function onMouseWheel(event:MouseEvent):void
148 | {
149 | _scrollbar.value -= event.delta;
150 | _tf.scrollV = Math.round(_scrollbar.value);
151 | }
152 |
153 | /**
154 | * Sets/gets whether this component is enabled or not.
155 | */
156 | public override function set enabled(value:Boolean):void
157 | {
158 | super.enabled = value;
159 | _tf.tabEnabled = value;
160 | }
161 |
162 | /**
163 | * Sets / gets whether the scrollbar will auto hide when there is nothing to scroll.
164 | */
165 | public function set autoHideScrollBar(value:Boolean):void
166 | {
167 | _scrollbar.autoHide = value;
168 | }
169 | public function get autoHideScrollBar():Boolean
170 | {
171 | return _scrollbar.autoHide;
172 | }
173 |
174 | }
175 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/UISlider.as:
--------------------------------------------------------------------------------
1 | /**
2 | * UISlider.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A Slider with a label and value label. Abstract base class for VUISlider and HUISlider
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 | import flash.events.Event;
33 |
34 | [Event(name="change", type="flash.events.Event")]
35 | public class UISlider extends Component
36 | {
37 | protected var _label:Label;
38 | protected var _valueLabel:Label;
39 | protected var _slider:Slider;
40 | protected var _precision:int = 1;
41 | protected var _sliderClass:Class;
42 | protected var _labelText:String;
43 | protected var _tick:Number = 1;
44 |
45 |
46 | /**
47 | * Constructor
48 | * @param parent The parent DisplayObjectContainer on which to add this UISlider.
49 | * @param xpos The x position to place this component.
50 | * @param ypos The y position to place this component.
51 | * @param label The initial string to display as this component's label.
52 | * @param defaultHandler The event handling function to handle the default event for this component (change in this case).
53 | */
54 | public function UISlider(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, label:String = "", defaultHandler:Function = null)
55 | {
56 | _labelText = label;
57 | super(parent, xpos, ypos);
58 | if(defaultHandler != null)
59 | {
60 | addEventListener(Event.CHANGE, defaultHandler);
61 | }
62 | formatValueLabel();
63 | }
64 |
65 | /**
66 | * Creates and adds the child display objects of this component.
67 | */
68 | override protected function addChildren():void
69 | {
70 | _label = new Label(this, 0, 0);
71 | _slider = new _sliderClass(this, 0, 0, onSliderChange);
72 | _valueLabel = new Label(this);
73 | }
74 |
75 | /**
76 | * Formats the value of the slider to a string based on the current level of precision.
77 | */
78 | protected function formatValueLabel():void
79 | {
80 | if(isNaN(_slider.value))
81 | {
82 | _valueLabel.text = "NaN";
83 | return;
84 | }
85 | var mult:Number = Math.pow(10, _precision);
86 | var val:String = (Math.round(_slider.value * mult) / mult).toString();
87 | var parts:Array = val.split(".");
88 | if(parts[1] == null)
89 | {
90 | if(_precision > 0)
91 | {
92 | val += ".";
93 | }
94 | for(var i:uint = 0; i < _precision; i++)
95 | {
96 | val += "0";
97 | }
98 | }
99 | else if(parts[1].length < _precision)
100 | {
101 | for(i = 0; i < _precision - parts[1].length; i++)
102 | {
103 | val += "0";
104 | }
105 | }
106 | _valueLabel.text = val;
107 | positionLabel();
108 | }
109 |
110 | /**
111 | * Positions the label when it has changed. Implemented in child classes.
112 | */
113 | protected function positionLabel():void
114 | {
115 |
116 | }
117 |
118 |
119 |
120 |
121 | ///////////////////////////////////
122 | // public methods
123 | ///////////////////////////////////
124 |
125 | /**
126 | * Draws the visual ui of this component.
127 | */
128 | override public function draw():void
129 | {
130 | super.draw();
131 | _label.text = _labelText;
132 | _label.draw();
133 | formatValueLabel();
134 | }
135 |
136 | /**
137 | * Convenience method to set the three main parameters in one shot.
138 | * @param min The minimum value of the slider.
139 | * @param max The maximum value of the slider.
140 | * @param value The value of the slider.
141 | */
142 | public function setSliderParams(min:Number, max:Number, value:Number):void
143 | {
144 | _slider.setSliderParams(min, max, value);
145 | }
146 |
147 |
148 |
149 |
150 | ///////////////////////////////////
151 | // event handlers
152 | ///////////////////////////////////
153 |
154 | /**
155 | * Handler called when the slider's value changes.
156 | * @param event The Event passed by the slider.
157 | */
158 | protected function onSliderChange(event:Event):void
159 | {
160 | formatValueLabel();
161 | dispatchEvent(new Event(Event.CHANGE));
162 | }
163 |
164 |
165 |
166 |
167 | ///////////////////////////////////
168 | // getter/setters
169 | ///////////////////////////////////
170 |
171 | /**
172 | * Sets / gets the current value of this slider.
173 | */
174 | public function set value(v:Number):void
175 | {
176 | _slider.value = v;
177 | formatValueLabel();
178 | }
179 | public function get value():Number
180 | {
181 | return _slider.value;
182 | }
183 |
184 | /**
185 | * Gets / sets the maximum value of this slider.
186 | */
187 | public function set maximum(m:Number):void
188 | {
189 | _slider.maximum = m;
190 | }
191 | public function get maximum():Number
192 | {
193 | return _slider.maximum;
194 | }
195 |
196 | /**
197 | * Gets / sets the minimum value of this slider.
198 | */
199 | public function set minimum(m:Number):void
200 | {
201 | _slider.minimum = m;
202 | }
203 | public function get minimum():Number
204 | {
205 | return _slider.minimum;
206 | }
207 |
208 | /**
209 | * Gets / sets the number of decimals to format the value label. Does not affect the actual value of the slider, just the number shown.
210 | */
211 | public function set labelPrecision(decimals:int):void
212 | {
213 | _precision = decimals;
214 | }
215 | public function get labelPrecision():int
216 | {
217 | return _precision;
218 | }
219 |
220 | /**
221 | * Gets / sets the text shown in this component's label.
222 | */
223 | public function set label(str:String):void
224 | {
225 | _labelText = str;
226 | // invalidate();
227 | draw();
228 | }
229 | public function get label():String
230 | {
231 | return _labelText;
232 | }
233 |
234 | /**
235 | * Gets / sets the tick value of this slider. This round the value to the nearest multiple of this number.
236 | */
237 | public function set tick(t:Number):void
238 | {
239 | _tick = t;
240 | _slider.tick = _tick;
241 | }
242 | public function get tick():Number
243 | {
244 | return _tick;
245 | }
246 |
247 |
248 | }
249 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/VBox.as:
--------------------------------------------------------------------------------
1 | /**
2 | * VBox.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A layout container for vertically aligning other components.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 |
30 | package com.bit101.components
31 | {
32 | import flash.display.DisplayObject;
33 | import flash.display.DisplayObjectContainer;
34 | import flash.events.Event;
35 |
36 | [Event(name="resize", type="flash.events.Event")]
37 | public class VBox extends Component
38 | {
39 | protected var _spacing:Number = 5;
40 | private var _alignment:String = NONE;
41 |
42 | public static const LEFT:String = "left";
43 | public static const RIGHT:String = "right";
44 | public static const CENTER:String = "center";
45 | public static const NONE:String = "none";
46 |
47 | /**
48 | * Constructor
49 | * @param parent The parent DisplayObjectContainer on which to add this PushButton.
50 | * @param xpos The x position to place this component.
51 | * @param ypos The y position to place this component.
52 | */
53 | public function VBox(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0)
54 | {
55 | super(parent, xpos, ypos);
56 | }
57 |
58 | /**
59 | * Override of addChild to force layout;
60 | */
61 | override public function addChild(child:DisplayObject) : DisplayObject
62 | {
63 | super.addChild(child);
64 | child.addEventListener(Event.RESIZE, onResize);
65 | draw();
66 | return child;
67 | }
68 |
69 | /**
70 | * Override of addChildAt to force layout;
71 | */
72 | override public function addChildAt(child:DisplayObject, index:int) : DisplayObject
73 | {
74 | super.addChildAt(child, index);
75 | child.addEventListener(Event.RESIZE, onResize);
76 | draw();
77 | return child;
78 | }
79 |
80 | /**
81 | * Override of removeChild to force layout;
82 | */
83 | override public function removeChild(child:DisplayObject):DisplayObject
84 | {
85 | super.removeChild(child);
86 | child.removeEventListener(Event.RESIZE, onResize);
87 | draw();
88 | return child;
89 | }
90 |
91 | /**
92 | * Override of removeChild to force layout;
93 | */
94 | override public function removeChildAt(index:int):DisplayObject
95 | {
96 | var child:DisplayObject = super.removeChildAt(index);
97 | child.removeEventListener(Event.RESIZE, onResize);
98 | draw();
99 | return child;
100 | }
101 |
102 | /**
103 | * Internal handler for resize event of any attached component. Will redo the layout based on new size.
104 | */
105 | protected function onResize(event:Event):void
106 | {
107 | invalidate();
108 | }
109 |
110 | /**
111 | * Sets element's x positions based on alignment value.
112 | */
113 | protected function doAlignment():void
114 | {
115 | if(_alignment != NONE)
116 | {
117 | for(var i:int = 0; i < numChildren; i++)
118 | {
119 | var child:DisplayObject = getChildAt(i);
120 | if(_alignment == LEFT)
121 | {
122 | child.x = 0;
123 | }
124 | else if(_alignment == RIGHT)
125 | {
126 | child.x = _width - child.width;
127 | }
128 | else if(_alignment == CENTER)
129 | {
130 | child.x = (_width - child.width) / 2;
131 | }
132 | }
133 | }
134 | }
135 |
136 | /**
137 | * Draws the visual ui of the component, in this case, laying out the sub components.
138 | */
139 | override public function draw() : void
140 | {
141 | _width = 0;
142 | _height = 0;
143 | var ypos:Number = 0;
144 | for(var i:int = 0; i < numChildren; i++)
145 | {
146 | var child:DisplayObject = getChildAt(i);
147 | child.y = ypos;
148 | ypos += child.height;
149 | ypos += _spacing;
150 | _height += child.height;
151 | _width = Math.max(_width, child.width);
152 | }
153 |
154 | doAlignment();
155 | _height += _spacing * (numChildren - 1);
156 | }
157 |
158 | /**
159 | * Gets / sets the spacing between each sub component.
160 | */
161 | public function set spacing(s:Number):void
162 | {
163 | _spacing = s;
164 | invalidate();
165 | }
166 | public function get spacing():Number
167 | {
168 | return _spacing;
169 | }
170 |
171 | /**
172 | * Gets / sets the horizontal alignment of components in the box.
173 | */
174 | public function set alignment(value:String):void
175 | {
176 | _alignment = value;
177 | invalidate();
178 | }
179 | public function get alignment():String
180 | {
181 | return _alignment;
182 | }
183 |
184 | }
185 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/VRangeSlider.as:
--------------------------------------------------------------------------------
1 | /**
2 | * VRangeSlider.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A vertical slider with two handles for selecting a range of values.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 |
33 | public class VRangeSlider extends RangeSlider
34 | {
35 | public function VRangeSlider(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, defaultHandler:Function = null)
36 | {
37 | super(VERTICAL, parent, xpos, ypos, defaultHandler);
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/VScrollBar.as:
--------------------------------------------------------------------------------
1 | /**
2 | * VScrollBar.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A vertical scroll bar for use in other components.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 |
33 | public class VScrollBar extends ScrollBar
34 | {
35 | /**
36 | * Constructor
37 | * @param parent The parent DisplayObjectContainer on which to add this ScrollBar.
38 | * @param xpos The x position to place this component.
39 | * @param ypos The y position to place this component.
40 | * @param defaultHandler The event handling function to handle the default event for this component (change in this case).
41 | */
42 | public function VScrollBar(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, defaultHandler:Function=null)
43 | {
44 | super(Slider.VERTICAL, parent, xpos, ypos, defaultHandler);
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/VSlider.as:
--------------------------------------------------------------------------------
1 | /**
2 | * VSlider.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A Vertical Slider component for choosing values.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 |
33 | public class VSlider extends Slider
34 | {
35 | /**
36 | * Constructor
37 | * @param parent The parent DisplayObjectContainer on which to add this Slider.
38 | * @param xpos The x position to place this component.
39 | * @param ypos The y position to place this component.
40 | * @param defaultHandler The event handling function to handle the default event for this component.
41 | */
42 | public function VSlider(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, defaultHandler:Function = null)
43 | {
44 | super(Slider.VERTICAL, parent, xpos, ypos, defaultHandler);
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/src/com/bit101/components/VUISlider.as:
--------------------------------------------------------------------------------
1 | /**
2 | * VUISlider.as
3 | * Keith Peters
4 | * version 0.9.10
5 | *
6 | * A vertical Slider with a label and value label.
7 | *
8 | * Copyright (c) 2011 Keith Peters
9 | *
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy
11 | * of this software and associated documentation files (the "Software"), to deal
12 | * in the Software without restriction, including without limitation the rights
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | * copies of the Software, and to permit persons to whom the Software is
15 | * furnished to do so, subject to the following conditions:
16 | *
17 | * The above copyright notice and this permission notice shall be included in
18 | * all copies or substantial portions of the Software.
19 | *
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 | * THE SOFTWARE.
27 | */
28 |
29 | package com.bit101.components
30 | {
31 | import flash.display.DisplayObjectContainer;
32 |
33 | public class VUISlider extends UISlider
34 | {
35 |
36 |
37 | /**
38 | * Constructor
39 | * @param parent The parent DisplayObjectContainer on which to add this VUISlider.
40 | * @param x The x position to place this component.
41 | * @param y The y position to place this component.
42 | * @param label The string to use as the label for this component.
43 | * @param defaultHandler The event handling function to handle the default event for this component.
44 | */
45 | public function VUISlider(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, label:String = "", defaultHandler:Function = null)
46 | {
47 | _sliderClass = VSlider;
48 | super(parent, xpos, ypos, label, defaultHandler);
49 | }
50 |
51 | /**
52 | * Initializes this component.
53 | */
54 | protected override function init():void
55 | {
56 | super.init();
57 | setSize(20, 146);
58 | }
59 |
60 |
61 |
62 |
63 | ///////////////////////////////////
64 | // public methods
65 | ///////////////////////////////////
66 |
67 | override public function draw():void
68 | {
69 | super.draw();
70 | _label.x = width / 2 - _label.width / 2;
71 |
72 | _slider.x = width / 2 - _slider.width / 2;
73 | _slider.y = _label.height + 5;
74 | _slider.height = height - _label.height - _valueLabel.height - 10;
75 |
76 | _valueLabel.x = width / 2 - _valueLabel.width / 2;
77 | _valueLabel.y = _slider.y + _slider.height + 5;
78 | }
79 |
80 | override protected function positionLabel():void
81 | {
82 | _valueLabel.x = width / 2 - _valueLabel.width / 2;
83 | }
84 |
85 |
86 |
87 |
88 | ///////////////////////////////////
89 | // event handlers
90 | ///////////////////////////////////
91 |
92 | ///////////////////////////////////
93 | // getter/setters
94 | ///////////////////////////////////
95 |
96 | override public function get width():Number
97 | {
98 | if(_label == null) return _width;
99 | return Math.max(_width, _label.width);
100 | }
101 |
102 | }
103 | }
--------------------------------------------------------------------------------
/src/comm/EventData.as:
--------------------------------------------------------------------------------
1 | package comm
2 | {
3 | import flash.events.Event;
4 |
5 | public class EventData extends Event
6 | {
7 | public static const EVENT_DATA:String = "EVENT_DATA";
8 | public var data:*;
9 | public function EventData(data:*, bubbles:Boolean=false, cancelable:Boolean=false)
10 | {
11 | super(EVENT_DATA, bubbles, cancelable);
12 | this.data = data;
13 | }
14 | override public function clone():Event
15 | {
16 | return new EventData(data);
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/src/net/SimpleSocketClient.as:
--------------------------------------------------------------------------------
1 | package net {
2 | import flash.events.DataEvent;
3 | import flash.events.Event;
4 | import flash.events.EventDispatcher;
5 | import flash.events.IOErrorEvent;
6 | import flash.events.ProgressEvent;
7 | import flash.events.SecurityErrorEvent;
8 | import flash.net.Socket;
9 |
10 | /**
11 | * 简化版Socket客户端,只收发字符串。配合SocketServer使用。使用方法:
12 | * var c = new SimpleSocketClient();
13 | * c.addEventListener(SimpleSocketMessageEvent.MESSAGE_RECEIVED, handle);
14 | * c.s.connect("127.0.0.1", 9999);
15 | *
16 | * function handle(e:SimpleSocketMessageEvent):void {
17 | * trace("收到数据"e.message);
18 | * }
19 | */
20 | public class SimpleSocketClient extends EventDispatcher {
21 | protected var _message:String;
22 | public var s:Socket;
23 | public static var SPLITER:String = "_$_";
24 | public function SimpleSocketClient(warpSocket:Socket=null) {
25 | super();
26 | this._message = "";
27 |
28 | s = warpSocket ? warpSocket : new Socket();
29 |
30 | s.addEventListener(Event.CONNECT, socketConnected);
31 | s.addEventListener(Event.CLOSE, socketClosed);
32 | s.addEventListener(ProgressEvent.SOCKET_DATA, socketData);
33 | s.addEventListener(IOErrorEvent.IO_ERROR, socketError);
34 | s.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketError);
35 | }
36 | public static var SOCKET_ALERT:String = "SOCKET_ALERT";
37 | private function alert(str:String):void
38 | {
39 | dispatchEvent(new DataEvent(SOCKET_ALERT,false,false,str));
40 | }
41 | protected function socketData(event:ProgressEvent):void {
42 | var str:String = s.readUTFBytes(s.bytesAvailable);
43 |
44 | //For this example, look for \n as a message terminator
45 | var messageParts:Array = str.split(SPLITER);
46 |
47 | //There could be multiple messages or a partial message,
48 | //pass on complete messages and buffer any partial
49 | for (var i:int = 0; i < messageParts.length; i++) {
50 | this._message += messageParts[i];
51 | if (i < messageParts.length - 1) {
52 | this.notifyMessage(this._message);
53 | this._message = "";
54 | }
55 | }
56 | }
57 |
58 | protected function notifyMessage(value:String):void {
59 | this.dispatchEvent(new SimpleSocketMessageEvent(SimpleSocketMessageEvent.MESSAGE_RECEIVED, value));
60 | }
61 |
62 | protected function socketConnected(event:Event):void {
63 | alert("【Clinet】Socket connected");
64 | }
65 |
66 | protected function socketClosed(event:Event):void {
67 | alert("【Clinet】Connection was closed");
68 | //TODO: Reconnect if needed
69 | }
70 |
71 | protected function socketError(event:Event):void {
72 | alert("【Clinet】An error occurred:"+ event);
73 | }
74 |
75 | public function close():void
76 | {
77 | if(s && s.connected){
78 | s.close();
79 | s = null;
80 | }
81 | }
82 |
83 | public function send(str:String):void
84 | {
85 | if(s && s.connected){
86 | s.writeUTFBytes(str + SPLITER);
87 | s.flush();
88 | }else{
89 | trace("【Clinet】not connected");
90 | }
91 | }
92 | }
93 | }
--------------------------------------------------------------------------------
/src/net/SimpleSocketMessageEvent.as:
--------------------------------------------------------------------------------
1 | package net {
2 | import flash.events.Event;
3 |
4 | public class SimpleSocketMessageEvent extends Event {
5 | public static const MESSAGE_RECEIVED:String = "messageReceived";
6 |
7 | protected var _message:String;
8 |
9 | public function SimpleSocketMessageEvent(type:String, message:String = "", bubbles:Boolean = false, cancelable:Boolean = false) {
10 | super(type, bubbles, cancelable);
11 | this._message = message;
12 | }
13 |
14 | public function get message():String {
15 | return this._message;
16 | }
17 | }
18 | }
--------------------------------------------------------------------------------
/src/net/SimpleSocketServer.as:
--------------------------------------------------------------------------------
1 | package net
2 | {
3 | import flash.events.DataEvent;
4 | import flash.events.EventDispatcher;
5 | import flash.events.ServerSocketConnectEvent;
6 | import flash.net.ServerSocket;
7 | import flash.net.Socket;
8 |
9 | /**
10 | * AS3版本的 Socket服务器,为了简化,没有对多个客户端进行管理,只针对一个客户端,
11 | * 也没有用二进制去实现收发,目前只支持字符串,消息之间用"\n"分割。默认侦听 127.0.0.1:9999
12 | * 具体用法:
13 | *
14 | * var s = new SocketServer();
15 | * s.start();
16 | *
17 | * 如果觉得有必要,也可以对 SocketServer产生的Err信息进行侦听:
18 | *
19 | * s.addEventListener(SimpleSocketServer.SOCKET_ALERT,function(e:DataEvent):void
20 | * {
21 | * trace(e.data);
22 | * });
23 | */
24 | public class SimpleSocketServer extends EventDispatcher
25 | {
26 | private var clientSocket:SimpleSocketClient;
27 | private var serverSocket:ServerSocket;
28 |
29 | public static var one:SimpleSocketServer;
30 | public function SimpleSocketServer(){
31 | one = this;
32 | }
33 | public function start(IP:String="127.0.0.1",PORT:int=9999):void{
34 | stop();
35 | if(ServerSocket && ServerSocket.isSupported){
36 | //
37 | }else{
38 | alert("当前设备(或SWF)不支持ServerSocket,无法启动ServerSocket");
39 | return;
40 | }
41 | serverSocket = new ServerSocket();//关闭的serverSocket无法再重新打开,所以必须每次都 new 新的。
42 | serverSocket.bind(PORT, IP);
43 | serverSocket.addEventListener(ServerSocketConnectEvent.CONNECT, onConnectServer);
44 | serverSocket.listen();
45 | }
46 |
47 | public function get running():Boolean{
48 | if(!serverSocket) return false;
49 | return serverSocket.listening;
50 | }
51 |
52 | protected function onConnectServer(event:ServerSocketConnectEvent):void
53 | {
54 | clientSocket = new SimpleSocketClient(event.socket as Socket);
55 | clientSocket.addEventListener(SimpleSocketMessageEvent.MESSAGE_RECEIVED, receive);
56 | alert( "新的客户端连接" + clientSocket.s.remoteAddress + ":" + clientSocket.s.remotePort );
57 | }
58 |
59 | /**发送**/
60 | public function send(str:String):void{
61 | if(clientSocket){
62 | clientSocket.send(str);
63 | }else{
64 | alert("暂无客户端连接,等待中...");
65 | }
66 | }
67 |
68 | public static var SOCKET_ALERT:String = "SOCKET_ALERT";
69 | public var onReceive:Function;
70 | private function alert(str:String):void
71 | {
72 | dispatchEvent(new DataEvent(SOCKET_ALERT,false,false,str));
73 | }
74 |
75 | /**接收**/
76 | protected function receive(e:SimpleSocketMessageEvent):void
77 | {
78 | // var date:Date = new Date();
79 | // alert("收到客户端消息:"+date.hoursUTC + ":" + date.minutesUTC + ":" + date.secondsUTC + " 内容为: " + e.message);
80 | if(onReceive){
81 | onReceive(e.message);
82 | }
83 | }
84 |
85 | /**停止(清理)**/
86 | public function stop():void
87 | {
88 | if(serverSocket){
89 | if(serverSocket.listening){
90 | serverSocket.close();
91 | }
92 | serverSocket.removeEventListener(ServerSocketConnectEvent.CONNECT, onConnectServer);
93 | serverSocket = null;
94 | }
95 | if(clientSocket){
96 | clientSocket.removeEventListener(SimpleSocketMessageEvent.MESSAGE_RECEIVED, receive);
97 | clientSocket.close();
98 | }
99 | }
100 | override public function toString():String
101 | {
102 | return "正在侦听"+serverSocket.localAddress+":"+serverSocket.localPort+"\n";
103 | }
104 | }
105 | }
--------------------------------------------------------------------------------
/src/props/PropertyPanel.as:
--------------------------------------------------------------------------------
1 | package props
2 | {
3 | import com.bit101.components.Panel;
4 | import com.bit101.components.PushButton;
5 | import com.bit101.components.VBox;
6 |
7 | import flash.display.DisplayObject;
8 | import flash.display.DisplayObjectContainer;
9 |
10 | public class PropertyPanel extends WindowX
11 | {
12 | public static var one:PropertyPanel;
13 | private var body:VBox;
14 | private var delBtn:PushButton;
15 | private var ob:DisplayObject;
16 | public function PropertyPanel(p:DisplayObjectContainer=null)
17 | {
18 | one = this;
19 | super(p,"★ 属性",752,63);
20 | if(p) p.addChild(this);
21 | var panel:Panel = new Panel(this,6,6);
22 | panel.shadow = false;
23 | panel.setSize(220-12,350-31);
24 | body = new VBox(panel);
25 |
26 | setSize(220,350);
27 | delBtn = new PushButton(null,0,0,"删除",onDelete); delBtn.setSize(43,20);
28 | }
29 |
30 | private function onDelete(e:*):void
31 | {
32 | if(body.numChildren) body.removeChildren();
33 | if(ob && ob.parent){
34 | ob.parent.removeChild(ob);
35 | }
36 | }
37 | public function show(ob:DisplayObject,...arr):void
38 | {
39 | this.ob = ob;
40 | if(body.numChildren) body.removeChildren();
41 | for (var i:int = 0; i < arr.length; i++)
42 | {
43 | body.addChild(arr[i]);
44 | }
45 | body.addChild(delBtn);
46 | }
47 | }
48 | }
--------------------------------------------------------------------------------
/src/props/WindowX.as:
--------------------------------------------------------------------------------
1 | package props
2 | {
3 | import com.bit101.components.Window;
4 |
5 | import flash.display.DisplayObjectContainer;
6 | import flash.events.MouseEvent;
7 | import flash.net.SharedObject;
8 |
9 | public class WindowX extends Window
10 | {
11 | public function WindowX(parent:DisplayObjectContainer=null,title:String="WindowX", defaulX:int=-1,defaulY:int=-1)
12 | {
13 | super(parent, 0, 0, title);
14 | var sxx:* = SO(title+"_x");
15 | var syy:* = SO(title+"_y");
16 | if(sxx){
17 | x=sxx;
18 | y=syy;
19 | }else{
20 | x=defaulX;
21 | y=defaulY;
22 | }
23 | savePos();
24 | }
25 | override protected function onMouseGoUp(e:MouseEvent):void
26 | {
27 | super.onMouseGoUp(e);
28 | savePos();
29 | }
30 |
31 | public function savePos():void
32 | {
33 | SO(title+"_x",x);
34 | SO(title+"_y",y);
35 | }
36 | public static function SO(key:String, val:*=null):*
37 | {
38 | var so:SharedObject = SharedObject.getLocal("AiEditor");
39 | if(val!=null) so.data[key] = val;
40 | return so.data[key];
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/src/props/comm/PBoolean.as:
--------------------------------------------------------------------------------
1 | package props.comm
2 | {
3 | import com.bit101.components.CheckBox;
4 | import com.bit101.components.HBox;
5 | import comm.EventData;
6 |
7 | public class PBoolean extends HBox
8 | {
9 | private var ui:CheckBox;
10 | private var _val:* = false;
11 | public var bind:*;
12 | public var sendEvent:Boolean = true;
13 | public function PBoolean(label:String,defaultVal:Boolean = true)
14 | {
15 | super(null);
16 | ui = new CheckBox(this,0,0,label,onChange);
17 | ui.selected = defaultVal;
18 | _val = defaultVal;
19 | }
20 | public function get val():*
21 | {
22 | return _val;
23 | }
24 | public function set val(value:*):void
25 | {
26 | _val = ui.selected = value;
27 | if(sendEvent)dispatchEvent(new EventData(_val));
28 | }
29 | private function onChange(e:*):void
30 | {
31 | _val = ui.selected;
32 | if(bind)bind.text = _val;
33 | if(sendEvent)dispatchEvent(new EventData(_val));
34 | }
35 | }
36 | }
--------------------------------------------------------------------------------
/src/props/comm/PBtn.as:
--------------------------------------------------------------------------------
1 | package props.comm
2 | {
3 | import com.bit101.components.HBox;
4 | import com.bit101.components.PushButton;
5 |
6 | public class PBtn extends HBox
7 | {
8 | private var ui:PushButton;
9 | private var _val:*;
10 | private var func:Function;
11 | private var param:Array;
12 | public function PBtn(btnWidth:Number,str:String,func:Function,...param)
13 | {
14 | super(null);
15 | this.param = param;
16 | this.func = func;
17 | ui = new PushButton(this,0,0,str,onClick); setSize(btnWidth,20);
18 | }
19 |
20 | private function onClick():void
21 | {
22 | if(param){
23 | func.apply(param);
24 | }else{
25 | func();
26 | }
27 | }
28 | }
29 | }
--------------------------------------------------------------------------------
/src/props/comm/PNumber.as:
--------------------------------------------------------------------------------
1 | package props.comm
2 | {
3 | import com.bit101.components.HBox;
4 | import com.bit101.components.Label;
5 |
6 | import props.noEvent.InputTextNoEvent;
7 | import comm.EventData;
8 |
9 | public class PNumber extends HBox
10 | {
11 | private var ui:InputTextNoEvent;
12 | private var _val:* = 0;
13 | public var bind:*;
14 | private var sendEvent:Boolean = true;
15 | public function PNumber(label:String,defaultVal:Number = 0)
16 | {
17 | super(null);
18 | new Label(this,0,0,label);
19 | ui = new InputTextNoEvent(this,0,0,"",onChange);
20 | ui.text = defaultVal+"";
21 | _val = defaultVal;
22 | }
23 | public function get val():*
24 | {
25 | return _val;
26 | }
27 | public function set val(v:*):void
28 | {
29 | ui.sendEvent = false;
30 | ui.textField.text = v+"";
31 | _val = parseFloat(v);
32 | ui.sendEvent = true;
33 | if(sendEvent)dispatchEvent(new EventData(_val));
34 | }
35 | private function onChange(e:*):void
36 | {
37 | _val = parseFloat(ui.textField.text);
38 | if(bind)bind.text = _val;
39 | if(sendEvent)dispatchEvent(new EventData(_val));
40 | }
41 | }
42 | }
--------------------------------------------------------------------------------
/src/props/comm/PSpace.as:
--------------------------------------------------------------------------------
1 | package props.comm
2 | {
3 | import com.bit101.components.HBox;
4 | import com.bit101.components.Panel;
5 |
6 | public class PSpace extends HBox
7 | {
8 | private var ui:Panel;
9 | private var _val:*;
10 | public function PSpace(defaultHeight:Number = 5,theAlpha:Number=.1)
11 | {
12 | super(null);
13 | ui = new Panel(this,0,0); ui.setSize(defaultHeight,defaultHeight); ui.alpha = theAlpha;
14 | }
15 | }
16 | }
--------------------------------------------------------------------------------
/src/props/comm/PString.as:
--------------------------------------------------------------------------------
1 | package props.comm
2 | {
3 | import com.bit101.components.HBox;
4 | import com.bit101.components.Label;
5 |
6 | import comm.EventData;
7 |
8 | import props.noEvent.InputTextNoEvent;
9 |
10 | public class PString extends HBox
11 | {
12 | public var ui:InputTextNoEvent;
13 | private var _val:* ="";
14 | private var sendEvent:Boolean = true;
15 | public function PString(label:String)
16 | {
17 | super(null);
18 | new Label(this,0,0,label);
19 | ui = new InputTextNoEvent(this,0,0,"",onChange);
20 | ui.height = 20;
21 | }
22 | public function get val():*
23 | {
24 | return _val;
25 | }
26 | public function set val(v:*):void
27 | {
28 | ui.sendEvent = false;
29 | _val = v;
30 | ui.setText(v);
31 | ui.sendEvent = true;
32 | if(sendEvent)dispatchEvent(new EventData(_val));
33 | }
34 | public function set valNoEvent(v:*):void
35 | {
36 | ui.sendEvent = false;
37 | _val = v;
38 | ui.setText(v);
39 | ui.sendEvent = true;
40 | }
41 | private function onChange(e:*=null):void
42 | {
43 | _val = ui.textField.text;
44 | if(sendEvent)dispatchEvent(new EventData(_val));
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/src/props/comm/PStringComboBox.as:
--------------------------------------------------------------------------------
1 | package props.comm
2 | {
3 | import com.bit101.components.HBox;
4 | import com.bit101.components.Label;
5 |
6 | import flash.events.Event;
7 |
8 | import comm.EventData;
9 |
10 | import props.noEvent.comboBoxNoEvent;
11 |
12 | public class PStringComboBox extends HBox
13 | {
14 | public var ui:comboBoxNoEvent;
15 | private var _val:* = "";
16 | private var sendEvent:Boolean = true;
17 | public function PStringComboBox(label:String,arr:Array)
18 | {
19 | super(null);
20 | new Label(this,0,0,label);
21 | ui = new comboBoxNoEvent(this,0,0,"",arr); ui.addEventListener(Event.SELECT,onChange);
22 | ui.numVisibleItems = arr.length+1;
23 | ui.height = 20;
24 | ui.selectedIndex = 0;
25 | }
26 | public function get val():*
27 | {
28 | return _val;
29 | }
30 | public function set val(value:*):void
31 | {
32 | ui.sendEvent = false;
33 | _val = ui.selectedItem = value;
34 | ui.sendEvent = true;
35 | if(sendEvent)dispatchEvent(new EventData(_val));
36 | }
37 | public function set valNoEvent(v:*):void
38 | {
39 | ui.sendEvent = false;
40 | _val = ui.selectedItem = v;
41 | ui.sendEvent = true;
42 | }
43 | private function onChange(e:*=null):void
44 | {
45 | _val = ui.selectedItem;
46 | if(sendEvent)dispatchEvent(new EventData(_val));
47 | }
48 | }
49 | }
--------------------------------------------------------------------------------
/src/props/noEvent/InputTextAreaNoEvent.as:
--------------------------------------------------------------------------------
1 | package props.noEvent
2 | {
3 | import com.bit101.components.Text;
4 |
5 | import flash.display.DisplayObjectContainer;
6 | import flash.events.Event;
7 |
8 | public class InputTextAreaNoEvent extends Text
9 | {
10 | public var sendEvent:Boolean=true;
11 | public function InputTextAreaNoEvent(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, text:String="", defaultHandler:Function=null)
12 | {
13 | super(parent, xpos, ypos, text);
14 | }
15 | override protected function onChange(e:Event):void
16 | {
17 | e.stopImmediatePropagation();
18 | _text = _tf.text;
19 | if(sendEvent){
20 | dispatchEvent(e);
21 | }
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/src/props/noEvent/InputTextNoEvent.as:
--------------------------------------------------------------------------------
1 | package props.noEvent
2 | {
3 | import com.bit101.components.InputText;
4 |
5 | import flash.display.DisplayObjectContainer;
6 | import flash.events.Event;
7 |
8 | public class InputTextNoEvent extends InputText
9 | {
10 | public var sendEvent:Boolean=true;
11 | public function InputTextNoEvent(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, text:String="", defaultHandler:Function=null)
12 | {
13 | super(parent, xpos, ypos, text, defaultHandler);
14 | height = 20;
15 | }
16 | public function setText(t:String):void{
17 | _text = t;
18 | }
19 | override protected function onChange(e:Event):void
20 | {
21 | e.stopImmediatePropagation();
22 | _text = _tf.text;
23 | if(sendEvent){
24 | dispatchEvent(e);
25 | }
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/src/props/noEvent/comboBoxNoEvent.as:
--------------------------------------------------------------------------------
1 | package props.noEvent
2 | {
3 | import com.bit101.components.ComboBox;
4 |
5 | import flash.display.DisplayObjectContainer;
6 | import flash.events.Event;
7 |
8 | public class comboBoxNoEvent extends ComboBox
9 | {
10 | public var sendEvent:Boolean=true;
11 |
12 | public function comboBoxNoEvent(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, defaultLabel:String="", items:Array=null)
13 | {
14 | super(parent, xpos, ypos, defaultLabel, items);
15 | _list.listItemClass = ListItemX;
16 | }
17 | override protected function onSelect(e:Event):void
18 | {
19 | _open = false;
20 | _dropDownButton.label = "+";
21 | if(stage != null && stage.contains(_list))
22 | {
23 | stage.removeChild(_list);
24 | }
25 | setLabelButtonLabel();
26 | if(sendEvent){
27 | dispatchEvent(e);
28 | }
29 | }
30 |
31 | }
32 | }
--------------------------------------------------------------------------------
/src/tryai/ActorMgr.as:
--------------------------------------------------------------------------------
1 | package tryai
2 | {
3 | import flash.utils.Dictionary;
4 |
5 | import tryai.smlgame.ActorData;
6 |
7 | public class ActorMgr
8 | {
9 | public static var one:ActorMgr;
10 | private var dic:Dictionary = new Dictionary();
11 | public function ActorMgr()
12 | {
13 | one = this;
14 | }
15 |
16 | public function forAllActor(f:Function):void
17 | {
18 | for each(var a:ActorData in dic){
19 | var ok:Boolean = f(a);
20 | if(ok)return;
21 | }
22 | }
23 | public function add(pid:int):ActorData
24 | {
25 | dic[pid] = new ActorData(pid);
26 | return dic[pid];
27 | }
28 | public function getActorById(pid:int):ActorData
29 | {
30 | var a:ActorData = dic[pid];
31 | if(a) return a;
32 | return null;
33 | }
34 | }
35 | }
--------------------------------------------------------------------------------
/src/tryai/AiUpdateMgr.as:
--------------------------------------------------------------------------------
1 | package tryai
2 | {
3 | import flash.utils.Dictionary;
4 |
5 | import tryai.ai.AINode;
6 | /**
7 | * 行为树管理器
8 | */
9 | public class AiUpdateMgr
10 | {
11 | public var dicAINode : Dictionary = new Dictionary();
12 | public static var one:AiUpdateMgr;
13 | public function AiUpdateMgr(){
14 | one = this;
15 | }
16 | public function JsonToAI(ob:Object,pid:uint=0):AINode{
17 | var rootAI:AINode;
18 | show(ob,null);
19 | function show(ob:Object,parentAI:AINode):void{
20 | var aai:AINode = new AINode(parentAI,ob.sel,pid);
21 | if(parentAI==null){
22 | rootAI = aai;
23 | }
24 | aai.name = ob.name;
25 | aai.x = ob.x;
26 | aai.y = ob.y;
27 | aai.ID = ob.id;
28 | if(ob.arr)aai.arr = ob.arr;
29 | if(ob.childs){
30 | for (var i:int = 0; i < ob.childs.length; i++)
31 | {
32 | show(ob.childs[i],aai);
33 | }
34 | }
35 | }
36 | dicAINode[pid] = rootAI;
37 | return rootAI;
38 | }
39 | public function get(pid:int):AINode{
40 | return dicAINode[pid];
41 | }
42 | private var lastTime:Number = -1;
43 | public var canRun:Boolean;
44 | public function update(t:Number):void{
45 | if(!canRun)return;
46 | if(lastTime==-1) lastTime = t-33;
47 | var dt:Number = (t-lastTime)/1000;//转换成秒
48 | lastTime = t;
49 | for (var id:uint in dicAINode)
50 | {
51 | var node:AINode = dicAINode[id];
52 | node.god.update(dt);
53 | //node.god.update(0);
54 | }
55 | }
56 | }
57 | }
--------------------------------------------------------------------------------
/src/tryai/ai/God.as:
--------------------------------------------------------------------------------
1 | package tryai.ai
2 | {
3 | public class God
4 | {
5 | public function God(pid:int){
6 | select = new Select();
7 | leaf = new Leaf(pid,this);
8 | }
9 | public var select:Select;
10 | public var leaf:Leaf;
11 | private var _nodeNow:AINode;
12 | private var isStop:Boolean;
13 | public function stop():void
14 | {
15 | isStop = true;
16 | }
17 | public function start():void
18 | {
19 | isStop = false;
20 | }
21 | public function get nodeNow():AINode
22 | {
23 | return _nodeNow;
24 | }
25 |
26 | public function set nodeNow(value:AINode):void
27 | {
28 | _nodeNow = value;
29 | }
30 |
31 | public function update(dt:Number):void{
32 | if(isStop) return;
33 | if(nodeNow){
34 | nodeNow.update(dt);
35 | }
36 | }
37 |
38 | /** 暂停 or 开启 **/
39 | public function switchPower():void
40 | {
41 | if(isStop){
42 | start();
43 | }else{
44 | stop();
45 | }
46 | }
47 |
48 | public function dispose():void
49 | {
50 | if(leaf){
51 | leaf.dispose();
52 | leaf = null;
53 | }
54 | }
55 | }
56 | }
--------------------------------------------------------------------------------
/src/tryai/ai/Select.as:
--------------------------------------------------------------------------------
1 | package tryai.ai
2 | {
3 | import flash.utils.getTimer;
4 | public class Select
5 | {
6 | public static var typeArr:Array = ["And","All","Or","Not","AndReturnTrue","CountAndLoop","NotAgainInXsec","Rnd"];
7 | public function And(childs:Vector.,ai:AINode):int{
8 | for (var i:int = 0; i < childs.length; i++)
9 | {
10 | if(childs[i].state == AINode.FAIL){
11 | return AINode.FAIL;
12 | }
13 | if(!childs[i].hasRun){
14 | childs[i].parent.running_sub = childs[i];
15 | return AINode.WAITSUB;
16 | }
17 | }
18 | return AINode.SUCCESS;
19 | }
20 | public function AndReturnTrue(childs:Vector.,ai:AINode):int{
21 | for (var i:int = 0; i < childs.length; i++)
22 | {
23 | if(childs[i].state == AINode.FAIL){
24 | return AINode.SUCCESS;
25 | }
26 | if(!childs[i].hasRun){
27 | childs[i].parent.running_sub = childs[i];
28 | return AINode.WAITSUB;
29 | }
30 | }
31 | return AINode.SUCCESS;
32 | }
33 | public function Or(childs:Vector.,ai:AINode):int{
34 | for (var i:int = 0; i < childs.length; i++)
35 | {
36 | if(childs[i].state == AINode.SUCCESS){
37 | return AINode.SUCCESS;
38 | }
39 | if(!childs[i].hasRun){
40 | childs[i].parent.running_sub = childs[i];
41 | return AINode.WAITSUB;
42 | }
43 | }
44 | return AINode.FAIL;
45 | }
46 | public function All(childs:Vector.,ai:AINode):int{
47 | for (var i:int = 0; i < childs.length; i++)
48 | {
49 | if(!childs[i].hasRun){
50 | childs[i].parent.running_sub = childs[i];
51 | return AINode.WAITSUB;
52 | }
53 | }
54 | return AINode.SUCCESS;
55 | }
56 | public function Not(childs:Vector.,ai:AINode):int{
57 | if(childs.length==0)return AINode.FAIL;
58 | if(childs[0].hasRun){
59 | if(childs[0].state == AINode.SUCCESS){
60 | return AINode.FAIL;
61 | }else{
62 | return AINode.SUCCESS;
63 | }
64 | }
65 | childs[0].parent.running_sub = childs[0];
66 | return AINode.WAITSUB;
67 | }
68 | public function CountAndLoop(childs:Vector.,ai:AINode):int{
69 | if(childs.length==0)return AINode.FAIL;
70 | if(ai.arr && ai.arr.length>0){
71 | ai.countMax = ai.arr[0];//编辑器节点中,【】内的参数
72 | }
73 | ai.running_sub = childs[0];
74 | if(childs[0].hasRun){
75 | ai.count++;//递增次数
76 | if(childs[0].state == AINode.FAIL){
77 | return AINode.FAIL;
78 | }
79 | if(ai.count>=ai.countMax-1){
80 | return AINode.SUCCESS;
81 | }
82 | resetHasrun(childs[0]);
83 | function resetHasrun(n:AINode):void{
84 | for (var i:int = 0; i < n.childs.length; i++) {
85 | //如果子节点是And节点,则重置子节点的下一层节点
86 | n.childs[i].hasRun = false;
87 | n.childs[i].stepNow = 0;
88 | resetHasrun(n.childs[i]);
89 | }
90 | }
91 | childs[0]._stepNow = 0;
92 | ai._stepNow = AINode.STEP_TYPE_SEL_SUB_NODE;
93 | }
94 | return AINode.WAITSUB;
95 | }
96 | public function NotAgainInXsec(childs:Vector.,ai:AINode):int{
97 | if(childs.length==0) return AINode.FAIL;
98 | var child:AINode = childs[0];
99 | var parent:AINode = childs[0].parent;
100 | parent.running_sub = child;
101 | if(parent.arr && parent.arr.length>0){
102 | parent.notAgainInXsec = parent.arr[0];//编辑器节点中,【】内的参数
103 | }
104 | var now:int = getTimer();
105 | var d:int = now - parent.lastTimeRunning;//时间差
106 | if(d,ai:AINode):int{
115 | if(childs.length==0){
116 | if(Math.random()<=ai.arr[0])return AINode.SUCCESS;//例如:Math.random()>0.6, 代表60%的几率
117 | return AINode.FAIL;
118 | }
119 | var parent:AINode = childs[0].parent;
120 | if(parent.arr==null || parent.arr.length==0)
121 | return AINode.FAIL;
122 |
123 | //子对象数量为1的情况,例如,Math.random()>0.6,则进入子节点
124 | if(childs.length==1){
125 | if(childs[0].hasRun)return AINode.SUCCESS;
126 | if(Math.random()>ai.arr[0]){
127 | ai.running_sub = childs[0];//选择权重最大的子对象运行。
128 | return AINode.WAITSUB;
129 | }
130 | }
131 |
132 | //子对象数量>1的情况,例如,【0.6, 0.2, 0.2】,那就看几率了,哪个子节点跟 Math.random()相乘最大,则选择执行哪个子节点。
133 | var w:Number=0;//权重值
134 | var index:int = 0;
135 | for (var i:int = 0; i < childs.length; i++)
136 | {
137 | if(childs[i].hasRun){
138 | return AINode.SUCCESS;//如果有子对象曾经运行,则直接返回上层
139 | }
140 | var ww:Number = iw){
142 | w = ww;//比较权重值,并记录
143 | index = i;//记录最大的权重的index
144 | }
145 | }
146 | childs[index].parent.running_sub = childs[index];//选择权重最大的子对象运行。
147 | return AINode.WAITSUB;
148 | }
149 | }
150 | }
--------------------------------------------------------------------------------
/src/tryai/smlgame/ActorData.as:
--------------------------------------------------------------------------------
1 | package tryai.smlgame
2 | {
3 | import flash.geom.Point;
4 |
5 | public class ActorData
6 | {
7 | public var target:ActorData;
8 | public var hpMax:int = 100;
9 | public var hp:int = hpMax;
10 | public var x:Number=0;//当前位置
11 | public var y:Number=0;
12 | private var _targetPos:Point;//移动目标点,为空时不移动
13 | public function get targetPos():Point
14 | {
15 | return _targetPos;
16 | }
17 | public function set targetPos(v:Point):void
18 | {
19 | _targetPos = v;
20 | }
21 | public var xBorn:Number=0;//出生点
22 | public var yBorn:Number=0;
23 | public var isSilence:Boolean;//是否被沉默(不能施放技能,但能普通攻击)
24 | private var _view:Number=200;//视野范围
25 | public var pid:int;
26 | private var _attRad:Number=70;//攻击范围
27 | public function get attRad():Number
28 | {
29 | return _attRad;
30 | }
31 |
32 | public function set attRad(value:Number):void
33 | {
34 | _attRad = value;
35 | var r:RenderOB = Scene.one.get(pid);
36 | if(r){
37 | r.drawAttackArea();
38 | }
39 | }
40 | private var _pos:Point=new Point();//位置 x,y 的 point 形式
41 | public function get pos():Point
42 | {
43 | _pos.x=x;
44 | _pos.y=y;
45 | return _pos;
46 | }
47 | public function get view():Number
48 | {
49 | return _view;
50 | }
51 | public function set view(value:Number):void
52 | {
53 | _view = value;
54 | var r:RenderOB = Scene.one.get(pid);
55 | if(r){
56 | r.drawViewArea();
57 | }
58 | }
59 |
60 |
61 | public function ActorData(pid:int)
62 | {
63 | this.pid = pid;
64 | }
65 |
66 | public function action(s:String):void
67 | {
68 | var r:RenderOB = Scene.one.get(pid);
69 | if(!r) return;
70 | if(s=="alert"){
71 | r.showAlert();
72 | }
73 | if(s=="what"){
74 | r.showWhat();
75 | }
76 | if(s=="attack"){
77 | r.showAttack();
78 | }
79 | if(s=="skill"){
80 | r.showSkill();
81 | }
82 | if(s=="turn"){
83 | if(target){
84 | if(target.x>x){
85 | r.turnRight();
86 | }else{
87 | r.turnLeft();
88 | }
89 | }
90 | }
91 | }
92 | }
93 | }
--------------------------------------------------------------------------------
/src/tryai/smlgame/EffectLayer.as:
--------------------------------------------------------------------------------
1 | package tryai.smlgame
2 | {
3 | import com.greensock.TweenLite;
4 | import com.greensock.TweenMax;
5 | import com.greensock.plugins.*;
6 | import flash.display.Graphics;
7 | import flash.display.Sprite;
8 |
9 | public class EffectLayer extends Sprite
10 | {
11 | public static var one:EffectLayer;
12 | public function EffectLayer()
13 | {
14 | one = this;
15 | TweenPlugin.activate([GlowFilterPlugin]);
16 | }
17 | public function showSkill(arr:Array):void
18 | {
19 | var s:Sprite = new Sprite();
20 | TweenMax.to(s, 0.02, {glowFilter:{color:0x00eeff, alpha:1, blurX:17, blurY:17, strength:23.5, quality:2}});
21 | addChild(s);
22 | drawOnce();
23 | TweenLite.delayedCall(.1,drawOnce);
24 | TweenLite.delayedCall(.2,drawOnce);
25 | TweenLite.delayedCall(.3,clear);
26 |
27 | function clear():void{
28 | one.removeChild(s);
29 | }
30 | function drawOnce():void{
31 | var g:Graphics = s.graphics;
32 | g.clear();
33 | g.lineStyle(1,0xffffff);
34 | g.moveTo(arr[0].x,arr[0].y);
35 | for (var i:int = 1; i < arr.length; i++) {
36 | if(i+1actor.attRad){
129 | if(dir.x>0){
130 | turnRight();
131 | }
132 | if(dir.x<0){
133 | turnLeft();
134 | }
135 | x+=dir.x;
136 | y+=dir.y;
137 | }else{
138 | actor.targetPos = null;
139 | }
140 | actor.x = x;
141 | actor.y = y;
142 | }
143 |
144 | public function turnLeft():void
145 | {
146 | bitmap.scaleX = -1;
147 | bitmap.x = bitmap.width-12;
148 | }
149 |
150 | public function turnRight():void
151 | {
152 | bitmap.scaleX = 1;
153 | bitmap.x =- 12;
154 | }
155 | private function move(xx:Number,yy:Number,speed:Number=1):void
156 | {
157 | var p2:Point = new Point(xx,yy);
158 | moveToPoint(p2,speed);
159 | }
160 |
161 | public function showAlert():void
162 | {
163 | tipBitmap.bitmapData = ClassPicAlertBD;
164 | tipBitmap.visible = true;
165 | var ox:Number = tipBitmap.x;
166 | var oy:Number = tipBitmap.y;
167 | TweenLite.to(tipBitmap,.2,{y:"-5",ease:Elastic.easeOut,onComplete:function():void{
168 | tipBitmap.visible = false;
169 | tipBitmap.x = ox;
170 | tipBitmap.y = oy;
171 | }});
172 | }
173 | public function showWhat():void
174 | {
175 | tipBitmap.bitmapData = ClassPicWhatBD;
176 | tipBitmap.visible = true;
177 | var ox:Number = tipBitmap.x;
178 | var oy:Number = tipBitmap.y;
179 | tipBitmap.y-=5;
180 | TweenLite.to(tipBitmap,.2,{y:"-5",ease:Elastic.easeOut,onComplete:function():void{
181 | tipBitmap.visible = false;
182 | tipBitmap.x = ox;
183 | tipBitmap.y = oy;
184 | }});
185 | }
186 |
187 | public function showAttack():void
188 | {
189 | if(!actor.target) return;
190 | playerAttack();
191 | }
192 |
193 | private function playerAttack():void
194 | {
195 | var p2:Point = actor.target.pos;
196 | var p1:Point = new Point(x,y);
197 | var dir:Point = p2.subtract(p1);
198 | dir.normalize(3);
199 | var mybody:RenderOB = this;
200 | TweenLite.to(mybody,.2,{x:x+dir.x,y:y+dir.y,ease:Elastic.easeOut,onComplete:function():void{
201 | TweenLite.to(mybody,.1,{x:x-dir.x, y:y-dir.x});
202 | }});
203 | }
204 |
205 | public function showSkill():void
206 | {
207 | if(!actor.target) return;
208 |
209 | playerAttack();
210 |
211 | var p2:Point = actor.target.pos;
212 | var p1:Point = new Point(x,y);
213 | var dir:Point = p2.subtract(p1);
214 | var dis:Number = dir.length;
215 | var pathlen:Number = dis+5;
216 |
217 | dir.normalize(pathlen*1);
218 | var s5:Point = p1.add(dir);
219 |
220 | dir.normalize(pathlen*.8);
221 | var s4:Point = p1.add(dir);
222 |
223 | dir.normalize(pathlen*.6);
224 | var s3:Point = p1.add(dir);
225 |
226 | dir.normalize(pathlen*.4);
227 | var s2:Point = p1.add(dir);
228 |
229 | dir.normalize(pathlen*.2);
230 | var s1:Point = p1.add(dir);
231 |
232 | dir.normalize(pathlen*.1);
233 | var s0:Point = p1.add(dir);
234 |
235 | EffectLayer.one.showSkill([s0,s1,s2,s3,s4,s5]);
236 | }
237 | }
238 | }
--------------------------------------------------------------------------------
/src/tryai/smlgame/Scene.as:
--------------------------------------------------------------------------------
1 | package tryai.smlgame
2 | {
3 | import com.greensock.TweenLite;
4 |
5 | import flash.display.BitmapData;
6 | import flash.display.DisplayObjectContainer;
7 | import flash.display.Graphics;
8 | import flash.display.Sprite;
9 | import flash.display.StageQuality;
10 | import flash.events.Event;
11 | import flash.events.MouseEvent;
12 | import flash.utils.Dictionary;
13 | import flash.utils.getTimer;
14 |
15 | import tryai.AiUpdateMgr;
16 |
17 | public class Scene extends Box
18 | {
19 | private var dic:Dictionary = new Dictionary();
20 | public static var one:Scene;
21 |
22 | public static var playerTarget:Sprite;
23 | private var lastClickTime:int;
24 |
25 | private var maskSprite:Sprite;
26 | public function Scene(p:DisplayObjectContainer)
27 | {
28 | one = this;
29 | super(p,904,250,0x666666,true);
30 | x = 2;
31 | y = 417;
32 | addEventListener(MouseEvent.MOUSE_DOWN,onDown);
33 | addEventListener(MouseEvent.MOUSE_UP,onUp);
34 |
35 | bg.alpha = .8;
36 |
37 | playerTarget = new Sprite();
38 | var g:Graphics = playerTarget.graphics;
39 | g.beginFill(0x00ee00);
40 | g.drawCircle(0,0,3);
41 | g.endFill();
42 | playerTarget.alpha = 0;
43 | playerTarget.x = 720;
44 | playerTarget.y = 140;
45 | addChild(playerTarget);
46 | p.stage.addEventListener(Event.ENTER_FRAME,onTick);
47 |
48 | maskSprite = new Sprite();
49 | addChild(maskSprite);
50 | drawMask();
51 |
52 | var effectLayer:EffectLayer = new EffectLayer();
53 | addChild(effectLayer);
54 |
55 | }
56 | override public function redraw():void
57 | {
58 | super.redraw();
59 | drawMask();
60 | }
61 | private function drawMask():void
62 | {
63 | if(!maskSprite)return;
64 | var g:Graphics = maskSprite.graphics;
65 | g.clear();
66 | g.beginFill(0);
67 | g.drawRect(0,0,bg.width,bg.height);
68 | g.endFill();this.mask = maskSprite;
69 | }
70 |
71 | protected function onTick(e:Event):void
72 | {
73 | if(!AiUpdateMgr.one.canRun) return;
74 | for (var pid:int in dic) {
75 | var r:RenderOB = dic[pid];
76 | r.update();
77 | }
78 | }
79 |
80 | public function add(pid:int):RenderOB
81 | {
82 | var r:RenderOB = new RenderOB(pid);
83 | addChild(r);
84 | r.x = 100+int(Math.random()*50);
85 | r.y = 100+int(Math.random()*50);
86 | dic[pid] = r;
87 | return r;
88 | }
89 | public function get(pid:int):RenderOB
90 | {
91 | var r:RenderOB = dic[pid];
92 | if(r) return r;
93 | return null;
94 | }
95 |
96 | protected function onUp(e:MouseEvent):void
97 | {
98 | this.stopDrag();
99 | }
100 |
101 | protected function onDown(e:MouseEvent):void
102 | {
103 | if(e.target != drager){
104 | this.startDrag();
105 | playerTarget.x = this.mouseX;
106 | playerTarget.y = this.mouseY;
107 | var now:int = getTimer();
108 | if(lastClickTime==0){
109 | lastClickTime = now;
110 | }
111 | if((now-lastClickTime)<500){
112 | var player:RenderOB = get(1);
113 | player.x = playerTarget.x;
114 | player.y = playerTarget.y;
115 | return;
116 | }
117 | lastClickTime = now;
118 | playerTarget.alpha = 1;
119 | playerTarget.scaleX = 1.5;
120 | playerTarget.scaleY = 1.5;
121 | TweenLite.killTweensOf(playerTarget);
122 | TweenLite.to(playerTarget,.5,{alpha:0,scaleX:1,scaleY:1});
123 | }
124 | }
125 | }
126 | }
--------------------------------------------------------------------------------
/src/tryai/smlgame/SmlGameSetupWin.as:
--------------------------------------------------------------------------------
1 | package tryai.smlgame
2 | {
3 | import com.bit101.components.HSlider;
4 | import com.bit101.components.InputText;
5 | import com.bit101.components.NumericStepper;
6 | import com.bit101.components.VBox;
7 |
8 | import flash.display.DisplayObjectContainer;
9 |
10 | import props.WindowX;
11 |
12 | import tryai.ActorMgr;
13 | import tryai.ai.AINode;
14 |
15 | public class SmlGameSetupWin extends WindowX
16 | {
17 |
18 | private var speed:InputText;
19 |
20 | private var ss:HSlider;
21 |
22 | public static var view:NumericStepper;
23 | public static var attRad:NumericStepper;
24 | public function SmlGameSetupWin(parent:DisplayObjectContainer=null)
25 | {
26 | super(parent,"SmlGameWin", 908,418);
27 | setSize(220,249);
28 | var body:VBox = new VBox(this);
29 |
30 | //new LableX(body,0,0," ");
31 | new LableX(body,0,0,"闪烁间隔:");
32 | speed = new InputText(body,0,0,"1",onSpeedChange);
33 | ss = new HSlider(body,0,0,onSpeedSlider); ss.minimum = 0.01; ss.maximum = 1;
34 | new LableX(body,0,0,"视野范围:");
35 | view = new NumericStepper(body,0,0,onViewChange);view.value = 200;view.step=10;
36 | new LableX(body,0,0,"攻击范围:");
37 | attRad = new NumericStepper(body,0,0,onAttChange);attRad.value = 70;attRad.step=10;
38 | }
39 |
40 | private function onAttChange(e:*):void
41 | {
42 | var a:ActorData = ActorMgr.one.getActorById(0);
43 | a.attRad = attRad.value;
44 | }
45 |
46 | private function onViewChange(e:*):void
47 | {
48 | var a:ActorData = ActorMgr.one.getActorById(0);
49 | a.view = view.value;
50 | }
51 |
52 | private function onSpeedSlider(e):void
53 | {
54 | speed.text = ss.value.toFixed(2);
55 | AINode.waitTime = ss.value;
56 | }
57 |
58 | private function onSpeedChange(e:*):void
59 | {
60 | var speedNum:Number = parseFloat(speed.text);
61 | AINode.waitTime = speedNum;
62 | ss.value = speedNum;
63 | }
64 | }
65 | }
--------------------------------------------------------------------------------
/参考ai.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/poerlang/BehaviorTreeEditor/71cfcc7c67a7d299d7843cd1e787cd3f329e91f5/参考ai.gif
--------------------------------------------------------------------------------