scaffold.libs_as.feathers.skins.ImageSkin.as Maven / Gradle / Ivy
/*
Feathers
Copyright 2012-2015 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.skins
{
import feathers.core.IFeathersControl;
import feathers.core.IMeasureDisplayObject;
import feathers.core.IStateContext;
import feathers.core.IStateObserver;
import feathers.core.IToggle;
import feathers.events.FeathersEventType;
import starling.display.Image;
import starling.events.Event;
import starling.textures.Texture;
/**
* A skin for Feathers components that displays a texture. Has the ability
* to change its texture based on the current state of the Feathers
* component that is being skinned.
*
*
* function setButtonSkin( button:Button ):void
* {
* var skin:ImageSkin = new ImageSkin( upTexture );
* skin.setTextureForState( ButtonState.DOWN, downTexture );
* skin.setTextureForState( ButtonState.HOVER, hoverTexture );
* button.defaultSkin = skin;
* }
*
* var button:Button = new Button();
* button.label = "Click Me";
* button.styleProvider = new AddOnFunctionStyleProvider( setButtonSkin, button.styleProvider );
* this.addChild( button );
*
* @see starling.display.Image
*/
public class ImageSkin extends Image implements IMeasureDisplayObject, IStateObserver
{
/**
* Constructor.
*/
public function ImageSkin(defaultTexture:Texture = null)
{
super(defaultTexture);
this.defaultTexture = defaultTexture;
}
/**
* @private
*/
protected var _defaultTexture:Texture;
/**
* The default texture that the skin will display. If the component
* being skinned supports states, the texture for a specific state may
* be specified using the setTextureForState()
method. If
* no texture has been specified for the current state, the default
* texture will be used.
*
* In the following example, the default texture is specified in the
* constructor:
*
*
* var skin:ImageSkin = new ImageSkin( texture );
*
* In the following example, the default texture is specified by
* setting the property:
*
*
* var skin:ImageSkin = new ImageSkin();
* skin.defaultTexture = texture;
*
* @default null
*
* @see #defaultTexture
* @see #disabledTexture
* @see #selectedTexture
* @see #setTextureForState()
* @see http://doc.starling-framework.org/current/starling/textures/Texture.html starling.textures.Texture
*/
public function get defaultTexture():Texture
{
return this._defaultTexture;
}
/**
* @private
*/
public function set defaultTexture(value:Texture):void
{
this._defaultTexture = value;
}
/**
* @private
*/
protected var _disabledTexture:Texture;
/**
* The texture to display when the stateContext
is
* an IFeathersControl
and its isEnabled
* property is false
. If a texture has been specified for
* the context's current state with setTextureForState()
,
* it will take precedence over the disabledTexture
.
*
* In the following example, the disabled texture is changed:
*
*
* var skin:ImageSkin = new ImageSkin( upTexture );
* skin.disabledTexture = disabledTexture;
* button.skin = skin;
* button.isEnabled = false;
*
* @default null
*
* @see #defaultTexture
* @see #selectedTexture
* @see #setTextureForState()
* @see http://doc.starling-framework.org/current/starling/textures/Texture.html starling.textures.Texture
*/
public function get disabledTexture():Texture
{
return this._disabledTexture;
}
/**
* @private
*/
public function set disabledTexture(value:Texture):void
{
this._disabledTexture = value;
}
/**
* @private
*/
protected var _selectedTexture:Texture;
/**
* The texture to display when the stateContext
is
* an IToggle
instance and its isSelected
* property is true
. If a texture has been specified for
* the context's current state with setTextureForState()
,
* it will take precedence over the selectedTexture
.
*
* In the following example, the selected texture is changed:
*
*
* var skin:ImageSkin = new ImageSkin( upTexture );
* skin.selectedTexture = selectedTexture;
* toggleButton.skin = skin;
* toggleButton.isSelected = true;
*
* @default null
*
* @see #defaultTexture
* @see #disabledTexture
* @see #setTextureForState()
* @see http://doc.starling-framework.org/current/starling/textures/Texture.html starling.textures.Texture
*/
public function get selectedTexture():Texture
{
return this._selectedTexture;
}
/**
* @private
*/
public function set selectedTexture(value:Texture):void
{
this._selectedTexture = value;
}
/**
* @private
*/
protected var _stateContext:IStateContext;
/**
* When the skin observes a state context, the skin may change its
* Texture
based on the current state of that context.
* Typically, a relevant component will automatically assign itself as
* the state context of its skin, so this property is considered to be
* for internal use only.
*
* @default null
*
* @see #setTextureForState()
*/
public function get stateContext():IStateContext
{
return this._stateContext;
}
/**
* @private
*/
public function set stateContext(value:IStateContext):void
{
if(this._stateContext === value)
{
return;
}
if(this._stateContext)
{
this._stateContext.removeEventListener(FeathersEventType.STATE_CHANGE, stateContext_stageChangeHandler);
}
this._stateContext = value;
if(this._stateContext)
{
this._stateContext.addEventListener(FeathersEventType.STATE_CHANGE, stateContext_stageChangeHandler);
}
this.updateTextureFromContext();
}
/**
* @private
*/
protected var _explicitWidth:Number = NaN;
/**
* The value passed to the width
property setter. If the
* width
property has not be set, returns NaN
.
*
* @see #width
*/
public function get explicitWidth():Number
{
return this._explicitWidth;
}
/**
* @private
*/
override public function set width(value:Number):void
{
if(this._explicitWidth === value)
{
return;
}
if(value !== value && this._explicitWidth !== this._explicitWidth)
{
return;
}
this._explicitWidth = value;
if(value === value) //!isNaN
{
super.width = value;
}
else //return to the original width of the texture
{
this.readjustSize();
}
this.dispatchEventWith(Event.RESIZE);
}
/**
* @private
*/
protected var _explicitHeight:Number = NaN;
/**
* The value passed to the height
property setter. If the
* height
property has not be set, returns
* NaN
.
*
* @see #height
*/
public function get explicitHeight():Number
{
return this._explicitHeight;
}
/**
* @private
*/
override public function set height(value:Number):void
{
if(this._explicitHeight === value)
{
return;
}
if(value !== value && this._explicitHeight !== this._explicitHeight)
{
return;
}
this._explicitHeight = value;
if(value === value) //!isNaN
{
super.height = value;
}
else //return to the original height of the texture
{
this.readjustSize();
}
this.dispatchEventWith(Event.RESIZE);
}
/**
* @private
*/
protected var _explicitMinWidth:Number = NaN;
/**
* The value passed to the minWidth
property setter. If the
* minWidth
property has not be set, returns
* NaN
.
*
* @see #minWidth
*/
public function get explicitMinWidth():Number
{
return this._explicitMinWidth;
}
/**
*
*/
public function get minWidth():Number
{
if(this._explicitMinWidth === this._explicitMinWidth) //!isNaN
{
return this._explicitMinWidth;
}
return 0;
}
/**
* @private
*/
public function set minWidth(value:Number):void
{
if(this._explicitMinWidth === value)
{
return;
}
if(value !== value && this._explicitMinWidth !== this._explicitMinWidth)
{
return;
}
this._explicitMinWidth = value;
this.dispatchEventWith(Event.RESIZE);
}
/**
* @private
*/
protected var _explicitMinHeight:Number = NaN;
/**
* The value passed to the minHeight
property setter. If
* the minHeight
property has not be set, returns
* NaN
.
*
* @see #minHeight
*/
public function get explicitMinHeight():Number
{
return this._explicitMinHeight;
}
/**
*
*/
public function get minHeight():Number
{
if(this._explicitMinHeight === _explicitMinHeight) //!isNaN
{
return this._explicitMinHeight;
}
return 0;
}
/**
* @private
*/
public function set minHeight(value:Number):void
{
if(this._explicitMinHeight === value)
{
return;
}
if(value !== value && this._explicitMinHeight !== this._explicitMinHeight)
{
return;
}
this._explicitMinHeight = value;
this.dispatchEventWith(Event.RESIZE);
}
/**
* @private
*/
protected var _stateToTexture:Object = {};
/**
* Gets the texture to be used by the skin when the context's
* currentState
property matches the specified state value.
*
* If a texture is not defined for a specific state, returns
* null
.
*
* @see #setTextureForState()
*/
public function getTextureForState(state:String):Texture
{
return this._stateToTexture[state] as Texture;
}
/**
* Sets the texture to be used by the skin when the context's
* currentState
property matches the specified state value.
*
* If a texture is not defined for a specific state, the value of the
* defaultTexture
property will be used instead.
*
* @see #defaultTexture
* @see #getTextureForState()
*/
public function setTextureForState(state:String, texture:Texture):void
{
if(texture !== null)
{
this._stateToTexture[state] = texture;
}
else
{
delete this._stateToTexture[state];
}
}
/**
* @private
*/
protected function updateTextureFromContext():void
{
if(this._stateContext === null)
{
this.texture = this._defaultTexture;
return;
}
var texture:Texture = this._stateToTexture[this._stateContext.currentState] as Texture;
if(texture === null &&
this._disabledTexture !== null &&
this._stateContext is IFeathersControl &&
!IFeathersControl(this._stateContext).isEnabled)
{
texture = this._disabledTexture;
}
if(texture === null &&
this._selectedTexture !== null &&
this._stateContext is IToggle &&
IToggle(this._stateContext).isSelected)
{
texture = this._selectedTexture;
}
if(texture === null)
{
texture = this._defaultTexture;
}
this.texture = texture;
if(this._explicitWidth === this._explicitWidth && //!isNaN
super.width !== this._explicitWidth)
{
super.width = this._explicitWidth;
}
if(this._explicitHeight === this._explicitHeight && //!isNaN
super.height !== this._explicitHeight)
{
super.height = this._explicitHeight;
}
}
/**
* @private
*/
protected function stateContext_stageChangeHandler(event:Event):void
{
this.updateTextureFromContext();
}
}
}