/client/src/fl/controls/Button.as

https://github.com/lilin01/haha · ActionScript · 257 lines · 76 code · 21 blank · 160 comment · 17 complexity · 90ee4300894f6bce3b338dde9e104b65 MD5 · raw file

  1. // Copyright 2007. Adobe Systems Incorporated. All Rights Reserved.
  2. package fl.controls {
  3. import fl.controls.LabelButton;
  4. import fl.core.InvalidationType;
  5. import fl.core.UIComponent;
  6. import fl.managers.IFocusManagerComponent;
  7. import flash.display.DisplayObject;
  8. import flash.display.Sprite;
  9. //--------------------------------------
  10. // Styles
  11. //--------------------------------------
  12. /**
  13. * The skin to be used when a button has emphasis.
  14. *
  15. * @default Button_emphasizedSkin
  16. *
  17. * @langversion 3.0
  18. * @playerversion Flash 9.0.28.0
  19. *
  20. * @playerversion AIR 1.0
  21. * @productversion Flash CS3
  22. */
  23. [Style(name="emphasizedSkin", type="Class")]
  24. /**
  25. * The padding to be applied around the Buttons in an
  26. * emphasized skin, in pixels.
  27. *
  28. * @default 2
  29. *
  30. * @langversion 3.0
  31. * @playerversion Flash 9.0.28.0
  32. *
  33. * @playerversion AIR 1.0
  34. * @productversion Flash CS3
  35. */
  36. [Style(name="emphasizedPadding", type="Number", format="Length")]
  37. //--------------------------------------
  38. // Class description
  39. //--------------------------------------
  40. /**
  41. * The Button component represents a commonly used rectangular button.
  42. * Button components display a text label, an icon, or both.
  43. *
  44. * <p>A Button component is typically associated with an event handler
  45. * method that listens for a <code>click</code> event and performs the
  46. * specified task after the <code>click</code> event is dispatched. When
  47. * the user clicks an enabled button, the button dispatches the <code>click</code>
  48. * and <code>buttonDown</code> events. Even if it is not enabled, a button
  49. * dispatches other events including <code>mouseMove</code>, <code>mouseOver</code>, <code>mouseOut</code>,
  50. * <code>rollOver</code>, <code>rollOut</code>, <code>mouseDown</code>,
  51. * and <code>mouseUp</code>.</p>
  52. *
  53. * <p>You can change the appearance of the button by associating a different
  54. * skin with each button state. A Button component can also be set to
  55. * function as a push button or a toggle button. </p>
  56. *
  57. * @langversion 3.0
  58. * @playerversion Flash 9.0.28.0
  59. * @includeExample examples/ButtonExample.as
  60. *
  61. * @playerversion AIR 1.0
  62. * @productversion Flash CS3
  63. */
  64. public class Button extends LabelButton implements IFocusManagerComponent {
  65. /**
  66. * @private (protected)
  67. *
  68. * @langversion 3.0
  69. * @playerversion Flash 9.0.28.0
  70. */
  71. protected var _emphasized:Boolean = false;
  72. /**
  73. * @private (protected)
  74. *
  75. * @langversion 3.0
  76. * @playerversion Flash 9.0.28.0
  77. */
  78. protected var emphasizedBorder:DisplayObject;
  79. /**
  80. * @private
  81. *
  82. * @langversion 3.0
  83. * @playerversion Flash 9.0.28.0
  84. */
  85. private static var defaultStyles:Object = {
  86. emphasizedSkin:"Button_emphasizedSkin", emphasizedPadding:2
  87. };
  88. /**
  89. * @copy fl.core.UIComponent#getStyleDefinition()
  90. *
  91. * @includeExample ../core/examples/UIComponent.getStyleDefinition.1.as -noswf
  92. *
  93. * @see fl.core.UIComponent#getStyle() UIComponent.getStyle()
  94. * @see fl.core.UIComponent#setStyle() UIComponent.setStyle()
  95. * @see fl.managers.StyleManager StyleManager
  96. *
  97. * @langversion 3.0
  98. * @playerversion Flash 9.0.28.0
  99. *
  100. * @playerversion AIR 1.0
  101. * @productversion Flash CS3
  102. */
  103. public static function getStyleDefinition():Object {
  104. return UIComponent.mergeStyles(LabelButton.getStyleDefinition(), defaultStyles);
  105. }
  106. /**
  107. * @private
  108. *
  109. * Method for creating the Accessibility class.
  110. * This method is called from UIComponent.
  111. *
  112. * @langversion 3.0
  113. * @playerversion Flash 9.0.28.0
  114. */
  115. public static var createAccessibilityImplementation:Function;
  116. /**
  117. * Creates a new Button component instance.
  118. *
  119. * @langversion 3.0
  120. * @playerversion Flash 9.0.28.0
  121. *
  122. * @playerversion AIR 1.0
  123. * @productversion Flash CS3
  124. */
  125. public function Button() {
  126. super();
  127. }
  128. [Inspectable(defaultValue=false)]
  129. /**
  130. * Gets or sets a Boolean value that indicates whether a border is drawn
  131. * around the Button component when the button is in its up state. A value
  132. * of <code>true</code> indicates that the button is surrounded by a border
  133. * when it is in its up state; a value of <code>false</code> indicates that
  134. * it is not.
  135. *
  136. * @default false
  137. *
  138. * @includeExample examples/Button.emphasized.1.as -noswf
  139. *
  140. * @see #style:emphasizedPadding
  141. * @see #style:emphasizedSkin
  142. *
  143. * @langversion 3.0
  144. * @playerversion Flash 9.0.28.0
  145. *
  146. * @playerversion AIR 1.0
  147. * @productversion Flash CS3
  148. */
  149. public function get emphasized():Boolean {
  150. return _emphasized;
  151. }
  152. /**
  153. * @private (setter)
  154. *
  155. * @langversion 3.0
  156. * @playerversion Flash 9.0.28.0
  157. */
  158. public function set emphasized(value:Boolean):void {
  159. _emphasized = value;
  160. invalidate(InvalidationType.STYLES)
  161. }
  162. /**
  163. * @private (protected)
  164. *
  165. * @langversion 3.0
  166. * @playerversion Flash 9.0.28.0
  167. */
  168. override protected function draw():void {
  169. if (isInvalid(InvalidationType.STYLES) || isInvalid(InvalidationType.SIZE)) {
  170. drawEmphasized();
  171. }
  172. super.draw();
  173. if (emphasizedBorder != null) {
  174. setChildIndex(emphasizedBorder, numChildren-1);
  175. }
  176. }
  177. /**
  178. * @private (protected)
  179. *
  180. * @langversion 3.0
  181. * @playerversion Flash 9.0.28.0
  182. */
  183. protected function drawEmphasized():void {
  184. if (emphasizedBorder != null) { removeChild(emphasizedBorder); }
  185. emphasizedBorder = null;
  186. if (!_emphasized) { return; }
  187. var emphasizedStyle:Object = getStyleValue("emphasizedSkin");
  188. if (emphasizedStyle != null) {
  189. emphasizedBorder = getDisplayObjectInstance(emphasizedStyle);
  190. }
  191. if (emphasizedBorder != null) {
  192. addChildAt(emphasizedBorder, 0);
  193. var padding:Number = Number(getStyleValue("emphasizedPadding"));
  194. emphasizedBorder.x = emphasizedBorder.y = -padding;
  195. emphasizedBorder.width = width + padding*2;
  196. emphasizedBorder.height = height + padding*2;
  197. }
  198. }
  199. /**
  200. * @private
  201. *
  202. * @copy fl.core.UIComponent#drawFocus()
  203. * @internal Added logic to resize focusRect if button has emphasis
  204. *
  205. * @langversion 3.0
  206. * @playerversion Flash 9.0.28.0
  207. */
  208. override public function drawFocus(focused:Boolean):void {
  209. super.drawFocus(focused);
  210. //Add focusRect to stage, and resize
  211. if (focused) {
  212. // Add the emphasis padding to the focus padding if appropriate
  213. var emphasizedPadding:Number = Number(getStyleValue("emphasizedPadding"));
  214. if (emphasizedPadding < 0 || !_emphasized) { emphasizedPadding = 0; }
  215. var focusPadding = getStyleValue("focusRectPadding");
  216. focusPadding = (focusPadding == null) ? 2 : focusPadding;
  217. focusPadding += emphasizedPadding;
  218. uiFocusRect.x = -focusPadding;
  219. uiFocusRect.y = -focusPadding;
  220. uiFocusRect.width = width + (focusPadding*2);
  221. uiFocusRect.height = height + (focusPadding*2);
  222. }
  223. }
  224. /**
  225. * @private (protected)
  226. *
  227. * @langversion 3.0
  228. * @playerversion Flash 9.0.28.0
  229. */
  230. override protected function initializeAccessibility():void {
  231. if (Button.createAccessibilityImplementation != null) {
  232. Button.createAccessibilityImplementation(this);
  233. }
  234. }
  235. }
  236. }