/com/zehfernando/display/debug/QuickButton.as

https://github.com/duponce/as3
ActionScript | 107 lines | 69 code | 24 blank | 14 comment | 7 complexity | 34796be550a944e74fa89bb142d7f758 MD5 | raw file
  1. package com.zehfernando.display.debug {
  2. import com.zehfernando.display.shapes.RoundedBox;
  3. import flash.display.Sprite;
  4. import flash.events.MouseEvent;
  5. import flash.text.TextField;
  6. import flash.text.TextFormat;
  7. import flash.text.TextFormatAlign;
  8. /**
  9. * @author zeh
  10. */
  11. public class QuickButton extends Sprite {
  12. // Properties
  13. protected var _width:Number;
  14. protected var _height:Number;
  15. protected var _text:String;
  16. // Instances
  17. protected var background:RoundedBox;
  18. protected var textField:TextField;
  19. // ================================================================================================================
  20. // CONSTRUCTOR ----------------------------------------------------------------------------------------------------
  21. public function QuickButton(__text:String = "CLICK", __x:Number = 0, __y:Number = 0, __onClick:Function = null, __width:Number = 70, __height:Number = 20) {
  22. x = __x;
  23. y = __y;
  24. _width = __width;
  25. _height = __height;
  26. _text = __text;
  27. //background = new RoundedBox(100, 100, 0x550033, 4);
  28. background = new RoundedBox(100, 100, 0x885500, 4);
  29. addChild(background);
  30. textField = new TextField();
  31. textField.wordWrap = true;
  32. textField.selectable = false;
  33. textField.embedFonts = false;
  34. addChild(textField);
  35. var fmt:TextFormat = new TextFormat();
  36. fmt.font = "_sans";
  37. fmt.size = 10;
  38. fmt.color = 0xffffff;
  39. fmt.align = TextFormatAlign.CENTER;
  40. textField.defaultTextFormat = fmt;
  41. buttonMode = true;
  42. mouseChildren = false;
  43. if (Boolean(__onClick)) addEventListener(MouseEvent.CLICK, __onClick);
  44. redraw();
  45. }
  46. // ================================================================================================================
  47. // INTERNAL INTERFACE ---------------------------------------------------------------------------------------------
  48. public function redraw(): void {
  49. background.width = _width;
  50. background.height = _height;
  51. textField.text = _text;
  52. textField.x = 0;
  53. textField.width = _width;
  54. textField.height = textField.textHeight + 4;
  55. textField.y = _height / 2 - textField.height / 2;
  56. }
  57. // ================================================================================================================
  58. // ACCESSOR functions ---------------------------------------------------------------------------------------------
  59. // TODO: use invalidate
  60. // The repetitive redraws don't look good but impact in rendering is virtually none
  61. override public function get width(): Number { return _width; }
  62. override public function set width(__value:Number): void {
  63. if (_width != __value) {
  64. _width = __value;
  65. redraw();
  66. }
  67. }
  68. override public function get height(): Number { return _height; }
  69. override public function set height(__value:Number): void {
  70. if (_height != __value) {
  71. _height = __value;
  72. redraw();
  73. }
  74. }
  75. public function get text(): String { return _text; }
  76. public function set text(__value:String): void {
  77. if (_text != __value) {
  78. _text = __value;
  79. redraw();
  80. }
  81. }
  82. }
  83. }