Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import starling.display.DisplayObject;
- import starling.display.Sprite;
- import starling.display.graphics.RoundedRectangle;
- import starling.display.materials.FlatColorMaterial;
- import starling.display.materials.TextureMaterial;
- import starling.events.Touch;
- import starling.events.TouchEvent;
- import starling.events.TouchPhase;
- import starling.filters.DropShadowFilter;
- import starling.text.TextField;
- import starling.text.TextFormat;
- import starling.textures.Texture;
- /**
- * ...
- * @author Angel Hdz
- */
- public class ToggleButton extends Sprite
- {
- public var padding:Number = 6;
- private var bg:RoundedRectangle;
- private var button:RoundedRectangle;
- private var onTxt:TextField;
- private var offTxt:TextField;
- private var _enabled:Boolean;
- private var dragging:Boolean;
- public function ToggleButton()
- {
- super();
- this._enabled = true;
- //Textfields
- this.onTxt = new TextField(200, 36, "ON", format({"bold": "true", "font": "Verdana", "size": "26", "horizontalAlign": "left", "color": "0xffffff"}));
- this.offTxt = new TextField(200, 36, "OFF", format({"bold": "true", "font": "Verdana", "size": "26", "horizontalAlign": "left", "color": "0x002233"}));
- this.onTxt.x = 12;
- this.offTxt.x = 82;
- this.onTxt.y = this.offTxt.y = 13;
- //Background
- this.bg = new RoundedRectangle(150, 50, 1, 1, 1, 1);
- this.bg.x = this.bg.y = this.padding;
- bg.material = new FlatColorMaterial(0x009DD4);
- this.button = new RoundedRectangle(80, 62);
- this.button.material = new FlatColorMaterial(0xFF3A0D);
- this.button.x = 76;
- this.button.filter = new DropShadowFilter(2, 0.785, 0, 1);
- //Add to stage
- this.addChild(bg);
- this.addChild(this.onTxt);
- this.addChild(this.offTxt);
- this.addChild(button);
- //Add listeners
- this.button.addEventListener(TouchEvent.TOUCH, this.onTouch);
- }
- private function onTouch(event:TouchEvent):void
- {
- var target:RoundedRectangle = RoundedRectangle(event.currentTarget) || null;
- var touch:Touch = event.getTouch(event.currentTarget as DisplayObject) || null;
- if (target && touch)
- {
- var posX:Number = touch.globalX - (target.width / 2);
- switch(touch.phase)
- {
- case TouchPhase.BEGAN:
- this.dragging = true;
- break;
- case TouchPhase.ENDED:
- this.dragging = false;
- if (touch.globalX > (this.bg.x + (this.bg.width / 2)))
- {
- target.x = (this.bg.x + this.bg.width + this.padding) - target.width;
- this.enabled = true;
- }
- else
- {
- target.x = this.bg.x - this.padding;
- this.enabled = false;
- }
- break;
- case TouchPhase.MOVED:
- if (this.dragging)
- {
- target.x = (posX > (this.bg.x - this.padding) && touch.globalX < (this.bg.x + this.bg.width + this.padding - target.width/2)) ? posX : target.x;
- //target.y = touch.globalY - (target.height/2);
- }
- break;
- }
- }
- }
- private function format(value:Object):TextFormat
- {
- var f:TextFormat = new TextFormat();
- for (var i:String in value)
- {
- f[i] = value[i];
- }
- return f;
- }
- /**
- * Toggles the button ON or OFF
- */
- public function set enabled(value:Boolean):void
- {
- this._enabled = value;
- this.update();
- }
- /**
- * Toggles the button ON or OFF
- */
- public function get enabled():Boolean
- {
- return this._enabled;
- }
- private function update():void
- {
- trace(this.enabled);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement