/src/editor/ui/CheckBox.as

https://github.com/initials/Ogmo-Editor
ActionScript | 133 lines | 109 code | 23 blank | 1 comment | 7 complexity | 8100e732bb1e3b95eb7353a40fe52553 MD5 | raw file
  1. package editor.ui
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.Sprite;
  5. import flash.events.Event;
  6. import flash.events.MouseEvent;
  7. import flash.ui.Mouse;
  8. import flash.ui.MouseCursor;
  9. public dynamic class CheckBox extends ValueModifier
  10. {
  11. [Embed(source = '../../../assets/checkbox1.png')]
  12. static private const Img1:Class;
  13. [Embed(source = '../../../assets/checkbox2.png')]
  14. static private const Img2:Class;
  15. [Embed(source = '../../../assets/checkbox1b.png')]
  16. static private const Img1b:Class;
  17. [Embed(source = '../../../assets/checkbox2b.png')]
  18. static private const Img2b:Class;
  19. private var def:Boolean;
  20. private var callback:Function;
  21. private var bitmap:Bitmap;
  22. private var _value:Boolean;
  23. private var hover:Boolean;
  24. public function CheckBox( x:int, y:int, def:Boolean = false, callback:Function = null )
  25. {
  26. this.x = x;
  27. this.y = y;
  28. this.def = def;
  29. this.callback = callback;
  30. _value = def;
  31. hover = false;
  32. addEventListener( Event.ADDED_TO_STAGE, init );
  33. }
  34. private function init( e:Event ):void
  35. {
  36. removeEventListener( Event.ADDED_TO_STAGE, init );
  37. addEventListener( Event.REMOVED_FROM_STAGE, destroy );
  38. addEventListener( MouseEvent.CLICK, onClick );
  39. addEventListener( MouseEvent.MOUSE_OVER, onMouseOver );
  40. addEventListener( MouseEvent.MOUSE_OUT, onMouseOut );
  41. setImage();
  42. }
  43. private function destroy( e:Event ):void
  44. {
  45. removeEventListener( Event.REMOVED_FROM_STAGE, destroy );
  46. removeEventListener( MouseEvent.CLICK, onClick );
  47. }
  48. private function onClick( e:MouseEvent ):void
  49. {
  50. _value = !_value;
  51. setImage();
  52. if (callback != null)
  53. callback( this );
  54. }
  55. private function onMouseOver( e:MouseEvent ):void
  56. {
  57. Mouse.cursor = MouseCursor.BUTTON;
  58. hover = true;
  59. setImage();
  60. }
  61. private function onMouseOut( e:MouseEvent ):void
  62. {
  63. Mouse.cursor = MouseCursor.AUTO;
  64. hover = false;
  65. setImage();
  66. }
  67. private function setImage():void
  68. {
  69. if (bitmap)
  70. removeChild( bitmap );
  71. if (_value)
  72. {
  73. if (hover)
  74. bitmap = new Img2b;
  75. else
  76. bitmap = new Img2;
  77. }
  78. else
  79. {
  80. if (hover)
  81. bitmap = new Img1b;
  82. else
  83. bitmap = new Img1;
  84. }
  85. bitmap.x = -8;
  86. bitmap.y = -8;
  87. addChild( bitmap );
  88. }
  89. /* ================ VALUE STUFF ================ */
  90. override public function get value():*
  91. {
  92. return _value;
  93. }
  94. override public function set value( to:* ):void
  95. {
  96. if (!(to is Boolean))
  97. throw new Error( "Checkbox passed non-boolean in value set." );
  98. _value = to;
  99. setImage();
  100. }
  101. override public function giveValue():void
  102. {
  103. valueObject.value = value;
  104. }
  105. override public function takeValue():void
  106. {
  107. value = valueObject.value;
  108. }
  109. }
  110. }