PageRenderTime 5302ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/com/bit101/components/Window.as

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