Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Engine.GUI.components {
- import flash.display.DisplayObject;
- import flash.display.Sprite;
- import flash.display.GradientType;
- import flash.display.SpreadMethod;
- import flash.text.TextField;
- import flash.text.TextFormat;
- import flash.text.TextFormatAlign;
- import flash.geom.Matrix;
- import flash.events.Event;
- import flash.events.MouseEvent;
- public class dButton extends dComponent {
- public static const className:String = "dButton";
- public static const version:Number = 1.1;
- private var _caption:TextField = new TextField();
- private var _textFormat:TextFormat = new TextFormat("Verdana", 12, 0xFFFFFF, false, false, false, "",
- "", TextFormatAlign.CENTER, 0, 0, 0, 0);
- private var _button:Sprite = new Sprite();
- private var _enabled:Boolean = true;
- private var _roundEdges:int = 20;
- public function dButton(width:int, height:int, caption:String) {
- _caption.text = caption;
- _width = width;
- _height = height;
- this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
- }
- private function onAdded(e:Event):void {
- this.addChild(_button);
- _button.x = 0; _button.y = 0;
- drawDefaultButton();
- this.addChild(_caption);
- applyTextFormat();
- if (_enabled) {
- this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
- this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
- this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
- }
- this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
- }
- private function onRemoved(e:Event):void {
- this.removeChild(_button);
- this.removeChild(_caption);
- if (this.hasEventListener(MouseEvent.MOUSE_OVER))
- this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
- if (this.hasEventListener(MouseEvent.MOUSE_OUT))
- this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
- if (this.hasEventListener(MouseEvent.MOUSE_DOWN))
- this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
- }
- private function onMouseOver(e:MouseEvent):void {
- drawHoverButton();
- }
- private function onMouseOut(e:MouseEvent):void {
- drawDefaultButton();
- }
- private function onMouseDown(e:MouseEvent):void {
- this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
- this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
- stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
- drawPressedButton();
- }
- private function onMouseUp(e:MouseEvent):void {
- this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
- this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
- stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
- drawHoverButton();
- }
- private function applyTextFormat():void {
- _caption.defaultTextFormat = _textFormat;
- if (_caption.text != "") _caption.setTextFormat(textFormat, 0, _caption.length);
- _caption.width = _width -3;
- _caption.y = (_height / 2 - _caption.textHeight / 2) -2;
- _caption.x = 2;
- _caption.height = _caption.textHeight+5;
- _caption.selectable = false;
- }
- public function get text():String {
- return _caption.text;
- }
- public function set text(s:String):void {
- _caption.text = s;
- }
- public function get textFormat():TextFormat {
- return _textFormat;
- }
- public function set textFormat(t:TextFormat):void {
- _textFormat = t;
- _caption.defaultTextFormat = _textFormat;
- if (_caption.text != "") _caption.setTextFormat(_textFormat, 0, _caption.length);
- }
- /* STATES */
- public function get enabled():Boolean {
- return _enabled;
- }
- public function set enabled(a:Boolean):void {
- if (_enabled && !a) {
- this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
- this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
- this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
- this.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
- _enabled = false;
- drawDefaultButton();
- return;
- }
- if (!_enabled && a) {
- this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
- this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
- this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
- this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
- _enabled = true;
- drawDefaultButton();
- }
- }
- public function get buttonWidth():int {
- return _width;
- }
- public function set buttonWidth(a:int):void {
- _width = Math.abs(a);
- drawDefaultButton();
- }
- public function get buttonHeight():int {
- return _height;
- }
- public function set buttonHeight(a:int):void {
- _height = Math.abs(a);
- drawDefaultButton();
- }
- public function get roundEdges():int {
- return _roundEdges;
- }
- public function set roundEdges(a:int):void {
- _roundEdges = a;
- drawDefaultButton();
- }
- private function drawDefaultButton():void {
- _button.graphics.clear();
- var m:Matrix = new Matrix(); m.createGradientBox(_width, _height, (Math.PI / 180) * 90, 0, 0);
- if (_enabled)
- _button.graphics.beginGradientFill(GradientType.LINEAR, [0x363636, 0x141414], [1, 1], [0, 255], m, SpreadMethod.PAD);
- else
- _button.graphics.beginGradientFill(GradientType.LINEAR, [0x452405, 0x321A03], [1, 1], [0, 255], m, SpreadMethod.PAD);
- _button.graphics.drawRoundRect(0, 0, _width, _height, _roundEdges, _roundEdges);
- }
- private function drawHoverButton():void {
- _button.graphics.clear();
- var m:Matrix = new Matrix(); m.createGradientBox(_width, _height, (Math.PI / 180) * 90, 0, 0);
- _button.graphics.beginGradientFill(GradientType.LINEAR, [0x5C5C5C, 0x2C2C2C], [1, 1], [0, 255], m, SpreadMethod.PAD);
- _button.graphics.drawRoundRect(0, 0, _width, _height, _roundEdges, _roundEdges);
- }
- private function drawPressedButton():void {
- _button.graphics.clear();
- var m:Matrix = new Matrix(); m.createGradientBox(_width, _height, (Math.PI / 180) * 90, 0, 0);
- _button.graphics.beginGradientFill(GradientType.LINEAR, [0x141414, 0x363636], [1, 1], [0, 255], m, SpreadMethod.PAD);
- _button.graphics.drawRoundRect(0, 0, _width, _height, _roundEdges, _roundEdges);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement