Created
May 15, 2011 14:30
-
-
Save MattMcFarland/973204 to your computer and use it in GitHub Desktop.
Dynamic Button without editing Image.as
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package base | |
{ | |
import net.flashpunk.Entity; | |
import net.flashpunk.Sfx; | |
import net.flashpunk.tweens.misc.ColorTween; | |
import net.flashpunk.tweens.misc.VarTween; | |
import net.flashpunk.graphics.Image; | |
import flash.filters.GlowFilter; | |
import flash.display.BitmapData; | |
import flash.geom.Rectangle; | |
import flash.geom.Point; | |
import net.flashpunk.utils.Input; | |
import flash.ui.Mouse; | |
import flash.ui.MouseCursor; | |
/** | |
* ... | |
* @author Matt McFarland | |
*/ | |
public class dynamicBtn extends Entity | |
{ | |
//Tweeners | |
protected var Glow:GlowFilter; | |
protected var colorTween:ColorTween; | |
protected var dimmer:VarTween; | |
//Graphics | |
protected var _image:Image; | |
protected var _glow:Image; | |
protected var _glowie:BitmapData; | |
protected var _glowActive:Boolean; | |
public var source:BitmapData; | |
//Colors | |
public var _staticColor:uint; | |
public var _hilightColor:uint; | |
//Logic | |
protected var _callback:Function; | |
protected var status:String | |
protected var index:int; | |
protected var mAlpha:Number; | |
/** | |
* Create a new upgrade button | |
* @param index Set index, unique number. | |
* @param x Coordinate | |
* @param y Coordinate | |
* @param icon Image Class | |
* @param callback Function to run when button is clicked. | |
*/ | |
public function dynamicBtn(index:int, x:Number, y:Number, icon:Image, callback:Function = null, cutheight:Boolean = false ) | |
{ | |
_callback = callback; | |
status = "static"; | |
super(x, y); | |
_staticColor = icon.color; | |
image = icon; | |
mAlpha = _image.alpha; | |
_glowie = new BitmapData(icon.width + 8 * icon.scale + .5, icon.height + 8 * icon.scale + .5, true, 0xFF000000 | 0xFFFFFF); | |
source = new BitmapData(icon.width + 8 * icon.scale + .5, icon.height + 8 * icon.scale + .5, true, 0); | |
_image.render(source, new Point(0, 0), new Point(0, 0)); | |
setHitbox(icon.width*icon.scale, icon.height*icon.scale); | |
if (cutheight) setHitbox(icon.width*icon.scale, icon.height*icon.scale*.85); | |
type = "button"; | |
establishColors(); | |
} | |
/** | |
* If the image isn't white we'll pulse between a dim and bright when hilighted. | |
*/ | |
protected function establishColors():void | |
{ | |
if (_staticColor != 0xFFFFFF) { | |
_hilightColor = 0xFFFFFF; | |
colorTween = new ColorTween(); | |
colorTween.color = _staticColor; | |
} | |
} | |
override public function update():void | |
{ | |
//Draconian Error Handling, force status to be correct! | |
if (status != "static" && | |
status != "hovered" ) | |
{ | |
throw new Error("Illegal status on upgrade button!"); | |
} | |
//If mouse is over. | |
if (collidePoint(x, y, world.mouseX, world.mouseY) && status == "static") onMouseOver(); | |
if (!collidePoint(x, y, world.mouseX, world.mouseY) && status == "hovered") onMouseLeave(); | |
//On mouse click: | |
if (collidePoint(x, y, world.mouseX, world.mouseY) && status == "hovered" && Input.mousePressed) onButtonClick(); | |
//Update graphics. | |
if (colorTween) _image.color = colorTween.color; | |
if (glow) updateGlowFX(); | |
if (_image) _image.alpha = mAlpha; | |
if (_glow) _glow.alpha = mAlpha; | |
} | |
/** | |
* Pulsing glow effect! | |
*/ | |
protected function updateGlowFX():void | |
{ | |
if (Glow.strength == 5) glowTween(15,true); | |
if (Glow.strength == 15) glowTween(5,true); | |
_glowie.applyFilter(source, new Rectangle( -8, -8, _glowie.width*_image.scale+.5, _glowie.height*_image.scale+.5), new Point(0, 0),Glow); | |
_glow = new Image(_glowie); | |
_glow.color = _image.color; | |
_glow.scale = _image.scale; | |
_glow.x = -8*_image.scale, _glow.y = -8*_image.scale; | |
graphic = _glow; | |
} | |
public function onButtonClick():void | |
{ | |
//Here you can place sound effect code | |
if (_callback != null) _callback(); | |
} | |
public function onMouseLeave():void | |
{ | |
glow = false; | |
status = "static"; | |
if (colorTween) colortween(_image.color, _staticColor,.5,true); | |
Mouse.cursor = MouseCursor.AUTO; | |
} | |
/** | |
* Runs when the mouse is over the button | |
*/ | |
public function onMouseOver():void | |
{ | |
status = "hovered"; | |
glow = true; | |
//Here you can place Sound Effect Code | |
if (colorTween) colortween(_image.color, _hilightColor,.2,true); | |
Mouse.cursor = MouseCursor.BUTTON; | |
} | |
/** | |
* The graphic property of the upgrade button. | |
*/ | |
public function set image(arg:Image):void | |
{ | |
_glowActive = false; | |
_image = arg; | |
_image.color = arg.color; | |
_image.scale = arg.scale; | |
graphic = arg; | |
} | |
/** | |
* Tween between two colors | |
* @param colFrom from color | |
* @param colTo to color | |
* @param duration length of tween. | |
* @param Reset set to true to force color tween if its not finished yet. | |
*/ | |
protected function colortween(colFrom:uint, colTo:uint, duration:Number, Reset:Boolean = false ):void | |
{ | |
if (colorTween && colorTween.percent < 1 && !Reset) return; | |
colorTween = new ColorTween(); | |
colorTween.tween(duration, colFrom, colTo); | |
addTween(colorTween); | |
colorTween.start(); | |
} | |
/** | |
* Glow Effect | |
* @param dim number to tween the glow to | |
* @param Reset set to true to force glow tween if its not finished yet. | |
*/ | |
protected function glowTween(dim:Number, Reset:Boolean = false ):void | |
{ | |
if (dimmer && dimmer.percent < 1 && !Reset) return; | |
dimmer = new VarTween(); | |
dimmer.tween(Glow, "strength", dim, .5); | |
addTween(dimmer,true); | |
} | |
/** | |
* set to true to make the icon glow | |
*/ | |
public function set glow(arg:Boolean):void | |
{ | |
if (!arg) { | |
_glowActive = false; | |
graphic = _image; | |
_image.alpha = 1; | |
return; | |
} | |
_glowActive = true; | |
Glow = new GlowFilter(); | |
_image.alpha = .47; | |
Glow.color = 0x7cb7f7; | |
Glow.alpha = .77; | |
Glow.blurX = 6; | |
Glow.blurY = 6; | |
Glow.strength = 5; | |
_glowie.applyFilter(source, new Rectangle( -8, -8, _glowie.width, _glowie.height), new Point(0, 0),Glow); | |
_glow = new Image(_glowie); | |
_glow.color = _image.color; | |
_glow.scale = _image.scale; | |
_glow.x = -8, _glow.y = -8; | |
graphic = _glow; | |
} | |
public function get glow():Boolean | |
{ | |
return _glowActive; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment