/flare/src/flare/display/buttons/SpriteButton.as

https://github.com/kindy61/Flare · ActionScript · 416 lines · 209 code · 83 blank · 124 comment · 27 complexity · 66f4b49689e438e66e5054d519e373e2 MD5 · raw file

  1. package flare.display.buttons
  2. {
  3. import flare.display.render.BitmapRenderer;
  4. import flash.display.Sprite;
  5. import flash.events.MouseEvent;
  6. import flash.utils.Dictionary;
  7. /**
  8. *
  9. * @author thomasburleson
  10. *
  11. */
  12. public class SpriteButton extends Sprite
  13. {
  14. // ========================================
  15. // Protected constants
  16. // ========================================
  17. /**
  18. * Drag threshold.
  19. */
  20. protected static const DRAG_THRESHOLD:Number = 1;
  21. /**
  22. * BitmapData cache (by Bitmap Class).
  23. */
  24. protected static const bitmapDataCache:Dictionary = new Dictionary();
  25. // ========================================
  26. // Protected properties
  27. // ========================================
  28. /**
  29. * Backing variable for <code>disabled</code> property.
  30. */
  31. protected var _disabled:Boolean;
  32. /**
  33. * Backing variable for <code>icon</code> property.
  34. */
  35. protected var _icon:Class;
  36. /**
  37. * Backing variable for <code>upSkinBitmapClass</code> property.
  38. */
  39. protected var _upSkinBitmapClass:Class;
  40. /**
  41. * Backing variable for <code>overSkinBitmapClass</code> property.
  42. */
  43. protected var _overSkinBitmapClass:Class;
  44. /**
  45. * Backing variable for <code>downSkinBitmapClass</code> property.
  46. */
  47. protected var _downSkinBitmapClass:Class;
  48. /**
  49. * Backing variable for <code>disabledSkinBitmapClass</code> property.
  50. */
  51. protected var _disabledSkinBitmapClass:Class;
  52. /**
  53. * Backing variable for <code>minWidth</code> property.
  54. */
  55. protected var _minWidth:Number;
  56. /**
  57. * Backing variable for <code>minHeight</code> property.
  58. */
  59. protected var _minHeight:Number;
  60. /**
  61. * Button phase.
  62. */
  63. protected var buttonPhase:String;
  64. protected var renderer : BitmapRenderer = new BitmapRenderer();;
  65. /**
  66. * Backing variable for <code>verticalPadding</code> property.
  67. */
  68. protected var _verticalPadding:Number = 0;
  69. /**
  70. * Backing variable for <code>horizontalPadding</code> property.
  71. */
  72. protected var _horizontalPadding:Number = 0;
  73. // ========================================
  74. // Public properties
  75. // ========================================
  76. /**
  77. * Indicates whether this button is disabled.
  78. */
  79. public function get disabled():Boolean
  80. {
  81. return _disabled;
  82. }
  83. public function set disabled( value:Boolean ):void
  84. {
  85. if ( _disabled != value )
  86. {
  87. _disabled = value;
  88. refresh();
  89. }
  90. }
  91. /**
  92. * Icon.
  93. *
  94. * NOTE: Unlike a Flex Button, SpriteButton displays EITHER an icon OR a label.
  95. */
  96. public function get icon():Class
  97. {
  98. return _icon;
  99. }
  100. public function set icon( value:Class ):void
  101. {
  102. if ( _icon != value )
  103. {
  104. _icon = value;
  105. refresh();
  106. }
  107. }
  108. /**
  109. * Bitmap skin for the 'up' button state.
  110. */
  111. public function get upSkinBitmapClass():Class
  112. {
  113. return _upSkinBitmapClass;
  114. }
  115. public function set upSkinBitmapClass( value:Class ):void
  116. {
  117. if ( _upSkinBitmapClass != value )
  118. {
  119. _upSkinBitmapClass = value;
  120. refresh();
  121. }
  122. }
  123. /**
  124. * Bitmap skin for the 'over' button state.
  125. */
  126. public function get overSkinBitmapClass():Class
  127. {
  128. return _overSkinBitmapClass;
  129. }
  130. public function set overSkinBitmapClass( value:Class ):void
  131. {
  132. if ( _overSkinBitmapClass != value )
  133. {
  134. _overSkinBitmapClass = value;
  135. refresh();
  136. }
  137. }
  138. /**
  139. * Bitmap skin for the 'down' button state.
  140. */
  141. public function get downSkinBitmapClass():Class
  142. {
  143. return _downSkinBitmapClass;
  144. }
  145. public function set downSkinBitmapClass( value:Class ):void
  146. {
  147. if ( _downSkinBitmapClass != value )
  148. {
  149. _downSkinBitmapClass = value;
  150. refresh();
  151. }
  152. }
  153. /**
  154. * Bitmap skin for the 'disabled' button state.
  155. */
  156. public function get disabledSkinBitmapClass():Class
  157. {
  158. return _disabledSkinBitmapClass;
  159. }
  160. public function set disabledSkinBitmapClass( value:Class ):void
  161. {
  162. if ( _disabledSkinBitmapClass != value )
  163. {
  164. _disabledSkinBitmapClass = value;
  165. refresh();
  166. }
  167. }
  168. /**
  169. * Vertical padding.
  170. */
  171. public function get verticalPadding():Number
  172. {
  173. return _verticalPadding;
  174. }
  175. public function set verticalPadding( value:Number ):void
  176. {
  177. if ( _verticalPadding != value )
  178. {
  179. _verticalPadding = value;
  180. refresh();
  181. }
  182. }
  183. /**
  184. * Horizontal padding.
  185. */
  186. public function get horizontalPadding():Number
  187. {
  188. return _horizontalPadding;
  189. }
  190. public function set horizontalPadding( value:Number ):void
  191. {
  192. if ( _horizontalPadding != value )
  193. {
  194. _horizontalPadding = value;
  195. refresh();
  196. }
  197. }
  198. /**
  199. * Minimum width.
  200. */
  201. public function get minWidth():Number
  202. {
  203. return _minWidth;
  204. }
  205. public function set minWidth( value:Number ):void
  206. {
  207. if ( _minWidth != value )
  208. {
  209. _minWidth = value;
  210. refresh();
  211. }
  212. }
  213. /**
  214. * Minimum height.
  215. */
  216. public function get minHeight():Number
  217. {
  218. return _minHeight;
  219. }
  220. public function set minHeight( value:Number ):void
  221. {
  222. if ( _minHeight != value )
  223. {
  224. _minHeight = value;
  225. refresh();
  226. }
  227. }
  228. // ========================================
  229. // Constructor
  230. // ========================================
  231. /**
  232. * Constructor.
  233. */
  234. public function SpriteButton()
  235. {
  236. super();
  237. // Configure Sprite.
  238. mouseEnabled = true;
  239. mouseChildren = false;
  240. useHandCursor = true;
  241. buttonMode = true;
  242. // Setup initial internal state.
  243. buttonPhase = ButtonPhase_UP;
  244. // Add MouseEvent.ROLL_OVER, MouseEvent.ROLL_OUT listeners.
  245. addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
  246. addEventListener( MouseEvent.ROLL_OVER, rollOverHandler );
  247. addEventListener( MouseEvent.ROLL_OUT, rollOutHandler );
  248. refresh();
  249. }
  250. protected function refresh():void
  251. {
  252. graphics.clear();
  253. // Draw the button skin.
  254. var buttonSkin:Class = getBitmapSkinForButtonPhase();
  255. if ( buttonSkin != null ) {
  256. graphics.clear();
  257. renderer.drawBitmap(graphics,buttonSkin,0,0);
  258. }
  259. // Draw the icon OR the label.
  260. if ( icon != null ) renderer.drawBitmap( graphics, icon, horizontalPadding, verticalPadding);
  261. // Update width / height to remain with specified <code>minWidth</code. and <code>maxWidth</code>.
  262. width = Math.max( width, minWidth );
  263. height = Math.max( height, minHeight );
  264. }
  265. protected function getBitmapSkinForButtonPhase():Class
  266. {
  267. switch ( buttonPhase )
  268. {
  269. default:
  270. case ButtonPhase_UP:
  271. return upSkinBitmapClass;
  272. case ButtonPhase_OVER:
  273. return ( overSkinBitmapClass != null ) ? overSkinBitmapClass : upSkinBitmapClass;
  274. case ButtonPhase_DOWN:
  275. return ( downSkinBitmapClass != null ) ? downSkinBitmapClass : upSkinBitmapClass;
  276. }
  277. }
  278. /**
  279. * Handle MouseEvent.MOUSE_DOWN.
  280. */
  281. protected function mouseDownHandler( event : MouseEvent ):void
  282. {
  283. buttonPhase = ButtonPhase_DOWN;
  284. addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  285. refresh();
  286. }
  287. /**
  288. * Handle MouseEvent.MOUSE_MOVE.
  289. */
  290. protected function mouseMoveHandler( event:MouseEvent ):void {
  291. }
  292. /**
  293. * Handle MouseEvent.MOUSE_UP.
  294. */
  295. protected function mouseUpHandler( event:MouseEvent ):void
  296. {
  297. buttonPhase = ButtonPhase_UP;
  298. removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  299. refresh();
  300. }
  301. /**
  302. * Handle MouseEvent.ROLL_OVER.
  303. */
  304. protected function rollOverHandler( event:MouseEvent ):void
  305. {
  306. buttonPhase = ( event.buttonDown ) ? ButtonPhase_DOWN : ButtonPhase_OVER;
  307. refresh();
  308. }
  309. /**
  310. * Handle MouseEvent.ROLL_OUT.
  311. */
  312. protected function rollOutHandler( event:MouseEvent ):void
  313. {
  314. buttonPhase = ButtonPhase_UP;
  315. refresh();
  316. }
  317. // ************************************************************************
  318. // Copies of values in mx.controls.ButtonPhase so dependency is removed!
  319. // ************************************************************************
  320. /**
  321. * @private
  322. */
  323. public static const ButtonPhase_DOWN:String = "down";
  324. /**
  325. * @private
  326. */
  327. public static const ButtonPhase_OVER:String = "over";
  328. /**
  329. * @private
  330. */
  331. public static const ButtonPhase_UP:String = "up";
  332. }
  333. }