/lib/com/bit101/components/PushButton.as
https://github.com/pombredanne/Elastic-Lists · ActionScript · 242 lines · 136 code · 34 blank · 72 comment · 7 complexity · a8a4c43608c955cfc85ef8c8728c3aa0 MD5 · raw file
- /**
- * PushButton.as
- * Keith Peters
- * version 0.9.1
- *
- * A basic button component with a label.
- *
- * Copyright (c) 2010 Keith Peters
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
- package com.bit101.components
- {
- import flash.display.DisplayObjectContainer;
- import flash.display.Sprite;
- import flash.events.MouseEvent;
- public class PushButton extends Component
- {
- protected var _back:Sprite;
- protected var _face:Sprite;
- protected var _label:Label;
- protected var _labelText:String = "";
- protected var _over:Boolean = false;
- protected var _down:Boolean = false;
- protected var _selected:Boolean = false;
- protected var _toggle:Boolean = false;
-
- /**
- * Constructor
- * @param parent The parent DisplayObjectContainer on which to add this PushButton.
- * @param xpos The x position to place this component.
- * @param ypos The y position to place this component.
- * @param label The string to use for the initial label of this component.
- * @param defaultHandler The event handling function to handle the default event for this component (click in this case).
- */
- public function PushButton(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, label:String = "", defaultHandler:Function = null)
- {
- super(parent, xpos, ypos);
- if(defaultHandler != null)
- {
- addEventListener(MouseEvent.CLICK, defaultHandler);
- }
- this.label = label;
- }
-
- /**
- * Initializes the component.
- */
- override protected function init():void
- {
- super.init();
- buttonMode = true;
- useHandCursor = true;
- setSize(100, 20);
- }
-
- /**
- * Creates and adds the child display objects of this component.
- */
- override protected function addChildren():void
- {
- _back = new Sprite();
- _back.filters = [getShadow(2, true)];
- _back.mouseEnabled = false;
- addChild(_back);
-
- _face = new Sprite();
- _face.mouseEnabled = false;
- _face.filters = [getShadow(1)];
- _face.x = 1;
- _face.y = 1;
- addChild(_face);
-
- _label = new Label();
- addChild(_label);
-
- addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
- addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
- }
-
-
-
-
- ///////////////////////////////////
- // public methods
- ///////////////////////////////////
-
- /**
- * Draws the visual ui of the component.
- */
- override public function draw():void
- {
- super.draw();
- _back.graphics.clear();
- _back.graphics.beginFill(Style.BACKGROUND);
- _back.graphics.drawRect(0, 0, _width, _height);
- _back.graphics.endFill();
-
- _face.graphics.clear();
- _face.graphics.beginFill(Style.BUTTON_FACE);
- _face.graphics.drawRect(0, 0, _width - 2, _height - 2);
- _face.graphics.endFill();
-
- _label.autoSize = true;
- _label.text = _labelText;
- if(_label.width > _width - 4)
- {
- _label.autoSize = false;
- _label.width = _width - 4;
- }
- else
- {
- _label.autoSize = true;
- }
- _label.draw();
- _label.move(_width / 2 - _label.width / 2, _height / 2 - _label.height / 2);
-
- }
-
-
-
-
- ///////////////////////////////////
- // event handlers
- ///////////////////////////////////
-
- /**
- * Internal mouseOver handler.
- * @param event The MouseEvent passed by the system.
- */
- protected function onMouseOver(event:MouseEvent):void
- {
- _over = true;
- addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
- }
-
- /**
- * Internal mouseOut handler.
- * @param event The MouseEvent passed by the system.
- */
- protected function onMouseOut(event:MouseEvent):void
- {
- _over = false;
- if(!_down)
- {
- _face.filters = [getShadow(1)];
- }
- removeEventListener(MouseEvent.ROLL_OUT, onMouseOut);
- }
-
- /**
- * Internal mouseOut handler.
- * @param event The MouseEvent passed by the system.
- */
- protected function onMouseDown(event:MouseEvent):void
- {
- _down = true;
- _face.filters = [getShadow(1, true)];
- stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
- }
-
- /**
- * Internal mouseUp handler.
- * @param event The MouseEvent passed by the system.
- */
- protected function onMouseUp(event:MouseEvent):void
- {
- if(_toggle && _over)
- {
- _selected = !_selected;
- }
- _down = _selected;
- _face.filters = [getShadow(1, _selected)];
- stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
- }
-
-
-
-
- ///////////////////////////////////
- // getter/setters
- ///////////////////////////////////
-
- /**
- * Sets / gets the label text shown on this Pushbutton.
- */
- public function set label(str:String):void
- {
- _labelText = str;
- draw();
- }
- public function get label():String
- {
- return _labelText;
- }
-
- public function set selected(value:Boolean):void
- {
- if(!_toggle)
- {
- value = false;
- }
-
- _selected = value;
- _down = _selected;
- _face.filters = [getShadow(1, _selected)];
- }
- public function get selected():Boolean
- {
- return _selected;
- }
-
- public function set toggle(value:Boolean):void
- {
- _toggle = value;
- }
- public function get toggle():Boolean
- {
- return _toggle;
- }
-
-
- }
- }