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

/examples/ThirdPartySrc/com/bit101/components/Window.as

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