Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Engine.GUI.components {
  2.     import flash.display.DisplayObject;
  3.     import flash.display.Sprite;
  4.     import flash.display.GradientType;
  5.     import flash.display.SpreadMethod;
  6.     import flash.text.TextField;
  7.     import flash.text.TextFormat;
  8.     import flash.text.TextFormatAlign;
  9.     import flash.geom.Matrix;
  10.     import flash.events.Event;
  11.     import flash.events.MouseEvent;
  12.    
  13.     public class dButton extends dComponent {
  14.  
  15.         public static const className:String = "dButton";
  16.         public static const version:Number = 1.1;
  17.        
  18.         private var _caption:TextField = new TextField();
  19.         private var _textFormat:TextFormat = new TextFormat("Verdana", 12, 0xFFFFFF, false, false, false, "",
  20.                                             "", TextFormatAlign.CENTER, 0, 0, 0, 0);
  21.         private var _button:Sprite = new Sprite();
  22.        
  23.         private var _enabled:Boolean = true;
  24.         private var _roundEdges:int = 20;
  25.        
  26.         public function dButton(width:int, height:int, caption:String) {
  27.             _caption.text = caption;
  28.             _width = width;
  29.             _height = height;
  30.             this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
  31.         }
  32.        
  33.         private function onAdded(e:Event):void {
  34.             this.addChild(_button);
  35.             _button.x = 0; _button.y = 0;
  36.             drawDefaultButton();
  37.             this.addChild(_caption);
  38.             applyTextFormat();
  39.             if (_enabled) {
  40.                 this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
  41.                 this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
  42.                 this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  43.             }
  44.             this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
  45.         }
  46.        
  47.         private function onRemoved(e:Event):void {
  48.             this.removeChild(_button);
  49.             this.removeChild(_caption);
  50.             if (this.hasEventListener(MouseEvent.MOUSE_OVER))
  51.                 this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
  52.             if (this.hasEventListener(MouseEvent.MOUSE_OUT))
  53.                 this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
  54.             if (this.hasEventListener(MouseEvent.MOUSE_DOWN))
  55.                 this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  56.         }
  57.        
  58.         private function onMouseOver(e:MouseEvent):void {
  59.             drawHoverButton();
  60.         }
  61.        
  62.         private function onMouseOut(e:MouseEvent):void {
  63.             drawDefaultButton();
  64.         }
  65.        
  66.         private function onMouseDown(e:MouseEvent):void {
  67.             this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
  68.             this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
  69.             stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  70.             drawPressedButton();
  71.         }
  72.        
  73.         private function onMouseUp(e:MouseEvent):void {
  74.             this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
  75.             this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
  76.             stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  77.             drawHoverButton();
  78.         }
  79.        
  80.         private function applyTextFormat():void {
  81.             _caption.defaultTextFormat = _textFormat;
  82.             if (_caption.text != "") _caption.setTextFormat(textFormat, 0, _caption.length);
  83.             _caption.width = _width -3;
  84.             _caption.y = (_height / 2 - _caption.textHeight / 2) -2;
  85.             _caption.x = 2;
  86.             _caption.height = _caption.textHeight+5;
  87.             _caption.selectable = false;
  88.         }
  89.        
  90.         public function get text():String {
  91.             return _caption.text;
  92.         }
  93.        
  94.         public function set text(s:String):void {
  95.             _caption.text = s;
  96.         }
  97.        
  98.         public function get textFormat():TextFormat {
  99.             return _textFormat;
  100.         }
  101.        
  102.         public function set textFormat(t:TextFormat):void {
  103.             _textFormat = t;
  104.             _caption.defaultTextFormat = _textFormat;
  105.             if (_caption.text != "") _caption.setTextFormat(_textFormat, 0, _caption.length);
  106.         }
  107.        
  108.         /* STATES */
  109.        
  110.         public function get enabled():Boolean {
  111.             return _enabled;
  112.         }
  113.        
  114.         public function set enabled(a:Boolean):void {
  115.             if (_enabled && !a) {
  116.                 this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
  117.                 this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
  118.                 this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  119.                 this.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  120.                 _enabled = false;
  121.                 drawDefaultButton();
  122.                 return;
  123.             }
  124.             if (!_enabled && a) {
  125.                 this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
  126.                 this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
  127.                 this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  128.                 this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  129.                 _enabled = true;
  130.                 drawDefaultButton();
  131.             }
  132.         }  
  133.        
  134.         public function get buttonWidth():int {
  135.             return _width;
  136.         }
  137.        
  138.         public function set buttonWidth(a:int):void {
  139.             _width = Math.abs(a);
  140.             drawDefaultButton();
  141.         }
  142.        
  143.         public function get buttonHeight():int {
  144.             return _height;
  145.         }
  146.        
  147.         public function set buttonHeight(a:int):void {
  148.             _height = Math.abs(a);
  149.             drawDefaultButton();
  150.         }
  151.        
  152.         public function get roundEdges():int {
  153.             return _roundEdges;
  154.         }
  155.        
  156.         public function set roundEdges(a:int):void {
  157.             _roundEdges = a;
  158.             drawDefaultButton();
  159.         }
  160.        
  161.         private function drawDefaultButton():void {
  162.             _button.graphics.clear();
  163.             var m:Matrix = new Matrix(); m.createGradientBox(_width, _height, (Math.PI / 180) * 90, 0, 0);
  164.             if (_enabled)
  165.                 _button.graphics.beginGradientFill(GradientType.LINEAR, [0x363636, 0x141414], [1, 1], [0, 255], m, SpreadMethod.PAD);
  166.             else
  167.                 _button.graphics.beginGradientFill(GradientType.LINEAR, [0x452405, 0x321A03], [1, 1], [0, 255], m, SpreadMethod.PAD);
  168.             _button.graphics.drawRoundRect(0, 0, _width, _height, _roundEdges, _roundEdges);
  169.         }
  170.        
  171.         private function drawHoverButton():void {
  172.             _button.graphics.clear();
  173.             var m:Matrix = new Matrix(); m.createGradientBox(_width, _height, (Math.PI / 180) * 90, 0, 0);
  174.             _button.graphics.beginGradientFill(GradientType.LINEAR, [0x5C5C5C, 0x2C2C2C], [1, 1], [0, 255], m, SpreadMethod.PAD);
  175.             _button.graphics.drawRoundRect(0, 0, _width, _height, _roundEdges, _roundEdges);
  176.         }
  177.        
  178.         private function drawPressedButton():void {
  179.             _button.graphics.clear();
  180.             var m:Matrix = new Matrix(); m.createGradientBox(_width, _height, (Math.PI / 180) * 90, 0, 0);
  181.             _button.graphics.beginGradientFill(GradientType.LINEAR, [0x141414, 0x363636], [1, 1], [0, 255], m, SpreadMethod.PAD);
  182.             _button.graphics.drawRoundRect(0, 0, _width, _height, _roundEdges, _roundEdges);
  183.         }
  184.        
  185.     }
  186.    
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement