Skip to content

Instantly share code, notes, and snippets.

@k0t0vich
Created June 4, 2014 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0t0vich/0bd80081116313fbc586 to your computer and use it in GitHub Desktop.
Save k0t0vich/0bd80081116313fbc586 to your computer and use it in GitHub Desktop.
with super/this etc
package components
{
/**
*
* @author Dimarik
* @version 1.0
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
*
* @keyword button, togglebutton
*/
public class ToggleButton extends Sprite
{
//-------------------------------------------------------------------------
//
// Constructor
//
//-------------------------------------------------------------------------
/**
* Constructor
*/
public function ToggleButton() {
super();
super.addEventListener(MouseEvent.CLICK, this.handler_click);
// Скины не должны быть источником событий. Только сам компонент.
super.mouseChildren = false;
}
//-------------------------------------------------------------------------
//
// Properties
//
//-------------------------------------------------------------------------
//---------------------------------
// selected
//---------------------------------
/**
* @private
* Storage for the selected property
*/
private var _selected: Boolean;
/**
* Contains <code>true</code> if the button is in the down state,
* and <code>false</code> if it is in the up state.
*
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
*/
public function get selected():Boolean {
return this._selected;
}
/**
* @private
*/
public function set selected(value:Boolean):void {
if (this._selected === value)
return;
this._selected = value;
this.validateState();
}
//---------------------------------
// upSkin
//---------------------------------
/**
* @private
* upSkin reference
*/
private var _upSkin: DisplayObject;
public function set upSkin(value:DisplayObject):void {
if (this._upSkin === value)
return;
if (this._upSkin && this._upSkin.parent)
super.removeChild(this._upSkin);
this._upSkun = value;
this.validateState();
}
public function get upSkin():DisplayObject {
return this._upSkin;
}
//---------------------------------
// upAndSelectedSkin
//---------------------------------
/**
* @private
* upAndSelectedSkin reference
*/
private var _upAndSelectedSkin: DisplayObject;
public function set upAndSelectedSkin(value:DisplayObject):void {
if (this._upAndSelectedSkin === value)
return;
if (this._upAndSelectedSkin && this._upAndSelectedSkin.parent)
super.removeChild(this._upAndSelectedSkin);
this._upSkun = value;
this.validateState();
}
public function get upAndSelectedSkin():DisplayObject {
return this._upSkin;
}
//-------------------------------------------------------------------------
//
// Private methods
//
//-------------------------------------------------------------------------
private function validateState():void {
if (this._selected) {
if (this._upSkin && this._upSkin.parent) {
super.removeChild(this._upSkin);
}
if (this._upAndSelectedSkin)
super.addChild(this._upAndSelectedSkin);
} else {
if (this._upAndSelectedSkin && this._upAndSelectedSkin.parent) {
super.removeChild(this._upAndSelectedSkin);
}
if (this._upSkin)
super.addChild(this._upSkin);
}
}
//-------------------------------------------------------------------------
//
// Event handlers
//
//-------------------------------------------------------------------------
protected function handler_click(event:MouseEvent):void {
this.selected = !this.selected;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment