/lib/com/bit101/components/PushButton.as

https://github.com/pombredanne/Elastic-Lists · ActionScript · 242 lines · 136 code · 34 blank · 72 comment · 7 complexity · a8a4c43608c955cfc85ef8c8728c3aa0 MD5 · raw file

  1. /**
  2. * PushButton.as
  3. * Keith Peters
  4. * version 0.9.1
  5. *
  6. * A basic button component with a label.
  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.MouseEvent;
  33. public class PushButton extends Component
  34. {
  35. protected var _back:Sprite;
  36. protected var _face:Sprite;
  37. protected var _label:Label;
  38. protected var _labelText:String = "";
  39. protected var _over:Boolean = false;
  40. protected var _down:Boolean = false;
  41. protected var _selected:Boolean = false;
  42. protected var _toggle:Boolean = false;
  43. /**
  44. * Constructor
  45. * @param parent The parent DisplayObjectContainer on which to add this PushButton.
  46. * @param xpos The x position to place this component.
  47. * @param ypos The y position to place this component.
  48. * @param label The string to use for the initial label of this component.
  49. * @param defaultHandler The event handling function to handle the default event for this component (click in this case).
  50. */
  51. public function PushButton(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, label:String = "", defaultHandler:Function = null)
  52. {
  53. super(parent, xpos, ypos);
  54. if(defaultHandler != null)
  55. {
  56. addEventListener(MouseEvent.CLICK, defaultHandler);
  57. }
  58. this.label = label;
  59. }
  60. /**
  61. * Initializes the component.
  62. */
  63. override protected function init():void
  64. {
  65. super.init();
  66. buttonMode = true;
  67. useHandCursor = true;
  68. setSize(100, 20);
  69. }
  70. /**
  71. * Creates and adds the child display objects of this component.
  72. */
  73. override protected function addChildren():void
  74. {
  75. _back = new Sprite();
  76. _back.filters = [getShadow(2, true)];
  77. _back.mouseEnabled = false;
  78. addChild(_back);
  79. _face = new Sprite();
  80. _face.mouseEnabled = false;
  81. _face.filters = [getShadow(1)];
  82. _face.x = 1;
  83. _face.y = 1;
  84. addChild(_face);
  85. _label = new Label();
  86. addChild(_label);
  87. addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  88. addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
  89. }
  90. ///////////////////////////////////
  91. // public methods
  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. _face.graphics.clear();
  104. _face.graphics.beginFill(Style.BUTTON_FACE);
  105. _face.graphics.drawRect(0, 0, _width - 2, _height - 2);
  106. _face.graphics.endFill();
  107. _label.autoSize = true;
  108. _label.text = _labelText;
  109. if(_label.width > _width - 4)
  110. {
  111. _label.autoSize = false;
  112. _label.width = _width - 4;
  113. }
  114. else
  115. {
  116. _label.autoSize = true;
  117. }
  118. _label.draw();
  119. _label.move(_width / 2 - _label.width / 2, _height / 2 - _label.height / 2);
  120. }
  121. ///////////////////////////////////
  122. // event handlers
  123. ///////////////////////////////////
  124. /**
  125. * Internal mouseOver handler.
  126. * @param event The MouseEvent passed by the system.
  127. */
  128. protected function onMouseOver(event:MouseEvent):void
  129. {
  130. _over = true;
  131. addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
  132. }
  133. /**
  134. * Internal mouseOut handler.
  135. * @param event The MouseEvent passed by the system.
  136. */
  137. protected function onMouseOut(event:MouseEvent):void
  138. {
  139. _over = false;
  140. if(!_down)
  141. {
  142. _face.filters = [getShadow(1)];
  143. }
  144. removeEventListener(MouseEvent.ROLL_OUT, onMouseOut);
  145. }
  146. /**
  147. * Internal mouseOut handler.
  148. * @param event The MouseEvent passed by the system.
  149. */
  150. protected function onMouseDown(event:MouseEvent):void
  151. {
  152. _down = true;
  153. _face.filters = [getShadow(1, true)];
  154. stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  155. }
  156. /**
  157. * Internal mouseUp handler.
  158. * @param event The MouseEvent passed by the system.
  159. */
  160. protected function onMouseUp(event:MouseEvent):void
  161. {
  162. if(_toggle && _over)
  163. {
  164. _selected = !_selected;
  165. }
  166. _down = _selected;
  167. _face.filters = [getShadow(1, _selected)];
  168. stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  169. }
  170. ///////////////////////////////////
  171. // getter/setters
  172. ///////////////////////////////////
  173. /**
  174. * Sets / gets the label text shown on this Pushbutton.
  175. */
  176. public function set label(str:String):void
  177. {
  178. _labelText = str;
  179. draw();
  180. }
  181. public function get label():String
  182. {
  183. return _labelText;
  184. }
  185. public function set selected(value:Boolean):void
  186. {
  187. if(!_toggle)
  188. {
  189. value = false;
  190. }
  191. _selected = value;
  192. _down = _selected;
  193. _face.filters = [getShadow(1, _selected)];
  194. }
  195. public function get selected():Boolean
  196. {
  197. return _selected;
  198. }
  199. public function set toggle(value:Boolean):void
  200. {
  201. _toggle = value;
  202. }
  203. public function get toggle():Boolean
  204. {
  205. return _toggle;
  206. }
  207. }
  208. }