Skip to content

Instantly share code, notes, and snippets.

@MattMcFarland
Created May 20, 2011 18:40
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 MattMcFarland/983514 to your computer and use it in GitHub Desktop.
Save MattMcFarland/983514 to your computer and use it in GitHub Desktop.
btntab
package campaign
{
import campaign.Galaxy.galaxymap;
import campaign.Tutorial.dummyWindow;
import campaign.Upgrade.upgradeMenu;
import net.flashpunk.Entity;
import net.flashpunk.graphics.Graphiclist;
import net.flashpunk.graphics.Image;
import net.flashpunk.graphics.Spritemap;
import net.flashpunk.FP;
import flash.display.BitmapData;
import flash.filters.GlowFilter;
import flash.geom.Rectangle;
import flash.geom.Point;
import net.flashpunk.Sfx;
import net.flashpunk.utils.Input;
import net.flashpunk.tweens.misc.VarTween;
import campaign.Galaxy.levelNode;
import flash.ui.Mouse;
import flash.ui.MouseCursor;
/**
* ...
* @author Matt McFarland
*/
public class btnTab extends Entity
{
//Tweeners
protected var Glow:GlowFilter;
protected var dimmer:VarTween;
//Graphics
private var _image:Image;
private var _glow:Image;
private var _glowie:BitmapData;
private var _glowActive:Boolean;
public var sprTab:Spritemap;
public var sprOver:Spritemap;
//Colors
private var _staticColor:uint;
private var _hilightColor:uint;
//Logic
protected var status:String
private var _select:String
/**
* Triangle Button Tab
* @param select must be "map" or "upgrade"
*/
public function btnTab(select:String)
{
status = "static";
layer = 8;
_image = new Image(GFX.iBTN_MASK);
_glowie = new BitmapData(_image.width, _image.height, true, 0xFF000000 | 0xFFFFFF);
_select = select;
var Frames:Array = new Array();
if (select == "map") {
Frames = createFrames(36);
sprTab = new Spritemap(GFX.iBTN_TO_MAP, 100, 100);
sprOver = new Spritemap(GFX.iBTN_TO_MAPOVER, 100,100);
}
if (select == "upgrade") {
Frames = createFrames(36);
sprTab = new Spritemap(GFX.iBTN_TO_UPG, 100, 100);
sprOver = new Spritemap(GFX.iBTN_TO_UPGOVER, 100, 100);
}
sprTab.add("normal", Frames, 30);
sprOver.add("normal", Frames, 30);
sprTab.play("normal");
sprOver.play("normal");
sprOver.blend = "lighten";
sprTab.x += 12;
sprOver.x += 12;
super(FP.width - _image.width, 0);
setHitbox(75, 75,-22,-3);
}
override public function added():void
{
if (collidePoint(x, y, world.mouseX, world.mouseY)) onMouseOver();
else graphic = new Graphiclist(sprTab, _image);
}
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();
if (glow) updateGlowFX();
}
public function onButtonClick():void
{
var fx:Sfx = new Sfx(SFX.TRANSITON);
if (!GV.muteSound) fx.play();
if (_select == "map") {
if (upgradeMenu.usedPoints < upgradeMenu.skillPoints && GV.player_level < 4) {
if (!GV.IgnoreUpgradeWarning) {
GV.SystemWindowActive = true;
world.add(new dummyWindow(2));
return;
}
}
FP.world = new galaxymap(0,0,0,false,0,upgradeMenu.lvl);
var extraNFO:String = "UPGRADE MENU USED >> XP: " + GV.XP;
extraNFO += ",LVL: " + GV.player_level;
extraNFO += ",WEP: " + GV.weaponType;
extraNFO += ",D/M/S: " + GV.damageLevel + "/" + GV.multiLevel + "/" + GV.specialLevel+",";
extraNFO += GV.magnetType + " str/rad(" + GV.magnetStrength +"/" +GV.magnetRadius + "),";
extraNFO += GV.shieldType + " abs/amt(" + GV.shieldAbsorb +"/" +GV.shieldAmount + "),";
extraNFO += GV.bombType + " amo/cld(" + GV.boomAmmo +"/" +GV.boomCoolDown + ")";
Main.tracker.alert(0, "Upgrade Menu End", extraNFO);
}
if (_select == "upgrade") {
var lvl:int = 0;
for each(var l:levelNode in galaxymap.lvls) {
trace (l.selectState);
if (l.selectState == "selected") lvl = l.index;
}
trace (lvl);
FP.world = new upgradeMenu(lvl);
}
}
public function onMouseLeave():void
{
glow = false;
sprTab.index = sprOver.index;
graphic = new Graphiclist(sprTab, _image);
status = "static";
sprTab.play("normal");
Mouse.cursor = MouseCursor.AUTO;
}
/**
* Runs when the mouse is over the button
*/
public function onMouseOver():void
{
Mouse.cursor = MouseCursor.BUTTON;
var fx:Sfx = new Sfx(SFX.MENU_HILIGHT);
if (!GV.muteSound) fx.play();
status = "hovered";
glow = true;
sprOver.index = sprTab.index;
graphic = new Graphiclist(sprTab, sprOver, _image);
sprOver.play("normal");
}
private function createFrames(amt:int):Array
{
var i:int = 0;
var arr:Array = [];
while (i < amt) {
arr.push(i)
i++;
}
return arr;
}
/**
* 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);
}
/**
* Pulsing glow effect!
*/
protected function updateGlowFX():void
{
if (Glow.strength == 1) glowTween(3,true);
if (Glow.strength == 3) glowTween(1,true);
_glowie.applyFilter(_image.source, new Rectangle( 0, 0, _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 = new Graphiclist(sprTab, sprOver, _glow);
}
/**
* set to true to make the icon glow
*/
public function get glow():Boolean { return _glowActive; }
public function set glow(arg:Boolean):void
{
if (!arg) {
_glowActive = false;
graphic = new Graphiclist(sprTab, _image);
_image.alpha = 1;
return;
}
_glowActive = true;
Glow = new GlowFilter();
_image.alpha = 1;
Glow.color = 0x7cb7f7;
Glow.alpha = .77;
Glow.blurX = 6;
Glow.blurY = 6;
Glow.strength = 1;
//Glow.inner = true;
_glowie.applyFilter(_image.source, new Rectangle( 0, 0, _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 = new Graphiclist(sprTab,_glow);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment