PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/bit101/components/Window.as

https://bitbucket.org/HopeSky/mars_nd2d
ActionScript | 333 lines | 215 code | 34 blank | 84 comment | 10 complexity | 25cd8310e5b836bd4ef296997b136ef8 MD5 | raw file
  1/**
  2 * Window.as
  3 * Keith Peters
  4 * version 0.9.2
  5 * 
  6 * A draggable window. Can be used as a container for other components.
  7 * 
  8 * Copyright (c) 2010 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 
 29package com.bit101.components
 30{
 31	import flash.display.DisplayObjectContainer;
 32	import flash.display.Sprite;
 33	import flash.events.Event;
 34	import flash.events.MouseEvent;
 35
 36	public class Window extends Component
 37	{
 38		protected var _title:String;
 39		protected var _titleBar:Panel;
 40		protected var _titleLabel:Label;
 41		protected var _panel:Panel;
 42		protected var _color:int = -1;
 43		protected var _shadow:Boolean = true;
 44		protected var _draggable:Boolean = true;
 45		protected var _minimizeButton:Sprite;
 46		protected var _hasMinimizeButton:Boolean = false;
 47		protected var _minimized:Boolean = false;
 48		protected var _hasCloseButton:Boolean;
 49		protected var _closeButton:PushButton;
 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		 * @param title The string to display in the title bar.
 58		 */
 59		public function Window(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, title:String="Window")
 60		{
 61			_title = title;
 62			super(parent, xpos, ypos);
 63		}
 64		
 65		/**
 66		 * Initializes the component.
 67		 */
 68		override protected function init():void
 69		{
 70			super.init();
 71			setSize(100, 100);
 72		}
 73		
 74		/**
 75		 * Creates and adds the child display objects of this component.
 76		 */
 77		override protected function addChildren():void
 78		{
 79			_titleBar = new Panel(this);
 80			_titleBar.buttonMode = true;
 81			_titleBar.useHandCursor = true;
 82			_titleBar.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
 83			_titleBar.height = 20;
 84			_titleLabel = new Label(_titleBar.content, 5, 1, _title);
 85			
 86			_panel = new Panel(this, 0, 20);
 87			_panel.visible = !_minimized;
 88			
 89			_minimizeButton = new Sprite();
 90			_minimizeButton.graphics.beginFill(0, 0);
 91			_minimizeButton.graphics.drawRect(-10, -10, 20, 20);
 92			_minimizeButton.graphics.endFill();
 93			_minimizeButton.graphics.beginFill(0, .35);
 94			_minimizeButton.graphics.moveTo(-5, -3);
 95			_minimizeButton.graphics.lineTo(5, -3);
 96			_minimizeButton.graphics.lineTo(0, 4);
 97			_minimizeButton.graphics.lineTo(-5, -3);
 98			_minimizeButton.graphics.endFill();
 99			_minimizeButton.x = 10;
100			_minimizeButton.y = 10;
101			_minimizeButton.useHandCursor = true;
102			_minimizeButton.buttonMode = true;
103			_minimizeButton.addEventListener(MouseEvent.CLICK, onMinimize);
104			
105			_closeButton = new PushButton(null, 86, 6, "", onClose);
106			_closeButton.setSize(8, 8);
107			
108			filters = [getShadow(4, false)];
109		}
110		
111		
112		
113		
114		///////////////////////////////////
115		// public methods
116		///////////////////////////////////
117		
118		/**
119		 * Draws the visual ui of the component.
120		 */
121		override public function draw():void
122		{
123			super.draw();
124			_titleBar.color = _color;
125			_panel.color = _color;
126			_titleBar.width = width;
127			_titleBar.draw();
128			_titleLabel.x = _hasMinimizeButton ? 20 : 5;
129			_closeButton.x = _width - 14;
130			_panel.setSize(_width, _height - 20);
131			_panel.draw();
132		}
133
134
135		///////////////////////////////////
136		// event handlers
137		///////////////////////////////////
138		
139		/**
140		 * Internal mouseDown handler. Starts a drag.
141		 * @param event The MouseEvent passed by the system.
142		 */
143		protected function onMouseDown(event:MouseEvent):void
144		{
145			if(_draggable)
146			{
147				this.startDrag();
148				stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
149				parent.addChild(this); // move to top
150			}
151			dispatchEvent(new Event(Event.SELECT));
152		}
153		
154		/**
155		 * Internal mouseUp handler. Stops the drag.
156		 * @param event The MouseEvent passed by the system.
157		 */
158		protected function onMouseUp(event:MouseEvent):void
159		{
160			this.stopDrag();
161			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
162		}
163		
164		protected function onMinimize(event:MouseEvent):void
165		{
166			minimized = !minimized;
167		}
168		
169		protected function onClose(event:MouseEvent):void
170		{
171			dispatchEvent(new Event(Event.CLOSE));
172		}
173		
174		///////////////////////////////////
175		// getter/setters
176		///////////////////////////////////
177		
178		/**
179		 * Gets / sets whether or not this Window will have a drop shadow.
180		 */
181		public function set shadow(b:Boolean):void
182		{
183			_shadow = b;
184			if(_shadow)
185			{
186				filters = [getShadow(4, false)];
187			}
188			else
189			{
190				filters = [];
191			}
192		}
193		public function get shadow():Boolean
194		{
195			return _shadow;
196		}
197		
198		/**
199		 * Gets / sets the background color of this panel.
200		 */
201		public function set color(c:int):void
202		{
203			_color = c;
204			invalidate();
205		}
206		public function get color():int
207		{
208			return _color;
209		}
210		
211		/**
212		 * Gets / sets the title shown in the title bar.
213		 */
214		public function set title(t:String):void
215		{
216			_title = t;
217			_titleLabel.text = _title;
218		}
219		public function get title():String
220		{
221			return _title;
222		}
223		
224		/**
225		 * Container for content added to this panel. This is just a reference to the content of the internal Panel, which is masked, so best to add children to content, rather than directly to the window.
226		 */
227		public function get content():DisplayObjectContainer
228		{
229			return _panel.content;
230		}
231		
232		/**
233		 * Sets / gets whether or not the window will be draggable by the title bar.
234		 */
235		public function set draggable(b:Boolean):void
236		{
237			_draggable = b;
238			_titleBar.buttonMode = _draggable;
239			_titleBar.useHandCursor = _draggable;
240		}
241		public function get draggable():Boolean
242		{
243			return _draggable;
244		}
245		
246		/**
247		 * Gets / sets whether or not the window will show a minimize button that will toggle the window open and closed. A closed window will only show the title bar.
248		 */
249		public function set hasMinimizeButton(b:Boolean):void
250		{
251			_hasMinimizeButton = b;
252			if(_hasMinimizeButton)
253			{
254				addChild(_minimizeButton);
255			}
256			else if(contains(_minimizeButton))
257			{
258				removeChild(_minimizeButton);
259			}
260			invalidate();
261		}
262		public function get hasMinimizeButton():Boolean
263		{
264			return _hasMinimizeButton;
265		}
266		
267		/**
268		 * Gets / sets whether the window is closed. A closed window will only show its title bar.
269		 */
270		public function set minimized(value:Boolean):void
271		{
272			_minimized = value;
273			_panel.visible = !_minimized;
274			if(_minimized)
275			{
276				_minimizeButton.rotation = -90;
277			}
278			else
279			{
280				_minimizeButton.rotation = 0;
281			}
282			dispatchEvent(new Event(Event.RESIZE));
283		}
284		public function get minimized():Boolean
285		{
286			return _minimized;
287		}
288		
289		/**
290		 * Gets the height of the component. A minimized window's height will only be that of its title bar.
291		 */
292		override public function get height():Number
293		{
294			if(_panel.visible)
295			{
296				return super.height;
297			}
298			else
299			{
300				return 20;
301			}
302		}
303
304		public function set hasCloseButton(value:Boolean):void
305		{
306			_hasCloseButton = value;
307			if(_hasCloseButton)
308			{
309				_titleBar.content.addChild(_closeButton);
310			}
311			else if(_titleBar.content.contains(_closeButton))
312			{
313				_titleBar.content.removeChild(_closeButton);
314			}
315			invalidate();
316		}
317		public function get hasCloseButton():Boolean
318		{
319			return _hasCloseButton;
320		}
321
322		public function get titleBar():Panel
323		{
324			return _titleBar;
325		}
326		public function set titleBar(value:Panel):void
327		{
328			_titleBar = value;
329		}
330
331
332	}
333}