NOGDUS $1670.00 has been donated to NOGDUS!
May 22, 2017, 02:27:50 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: MagicMenu class  (Read 2416 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Administrator
Offline Offline

Respect: 3425
« on: January 24, 2012, 04:57:24 PM »

The following ActionScript 3.0 class implements a very simple vertical scrolling magic selection menu component.


Code:
package 
{
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.geom.*;
import flash.text.*;
/**
* a basic scrolling menu game ui component for listing magic spells to cast
* implemented as a flash Sprite and extended SimpleButtons
* @author Richard Marks
*/
public class MagicMenu
{
private var myList:Vector.<Object> = new Vector.<Object>();
private var myScroll:int = 0;
private var mySprite:Sprite = new Sprite;
private var myScrollUpButton:SimpleButton;
private var myScrollDownButton:SimpleButton;
private var mySpellButtons:Vector.<MagicButton> = new Vector.<MagicButton>();

private var _spellsVisible:int = 3;

public function get spellsVisible():int { return _spellsVisible; }

public function set spellsVisible(value:int):void
{
// if we try to show more spells than we have in our list, or less than 2 spells
// we default back to showing 3 spells
if (value < myList.length && value > 1)
{
_spellsVisible = value;
}
else
{
_spellsVisible = 3;
}
}

public var onClick:Function = null;

public function MagicMenu(x:Number = 0, y:Number = 0, width:int = 120)
{
mySprite.x = x;
mySprite.y = y;

// make sure the sprite has a width because the buttons are created to the sprite's width
var g:Graphics = mySprite.graphics;
g.beginFill(0x000000, 0.0);
g.drawRect(0, 0, width, 1);
g.endFill();
}

public function add(spellName:String, spellType:String, spellLevel:int):void
{
// add the spell data
myList.push( { name: spellName, type: spellType, level: spellLevel } );

// add the button
var spellButton:MagicButton = new MagicButton( { id: myList.length - 1 } );
var caption:String = spellType + " L" + spellLevel + ": " + spellName;
spellButton.upState = makeButtonStateBitmap(caption, "up");
spellButton.overState = makeButtonStateBitmap(caption, "over");
spellButton.downState = makeButtonStateBitmap(caption, "down");
spellButton.hitTestState = spellButton.overState;
spellButton.addEventListener(MouseEvent.CLICK, onSpellButtonClick);
spellButton.visible = false;
mySpellButtons.push(spellButton);
mySprite.addChild(spellButton);
}

private function updateDisplay():void
{
var anchorY:int = myScrollUpButton.y + myScrollUpButton.height + 1;

// hide all spell buttons
for each (var sb:MagicButton in mySpellButtons) { sb.visible = false; }

// re-position and make the spell buttons visible for the current display
for (var i:int = 0; i < spellsVisible; i++)
{
var offset:int = myScroll + i;

var spell:Object = myList[offset];
var button:MagicButton = mySpellButtons[offset];

button.x = myScrollUpButton.x;
button.y = anchorY;
button.visible = true;
anchorY += button.height + 1;
}
}

public function createDisplay():void
{
// nothing in the list.
if (myList.length == 0)
{
return;
}

// create the scroll buttons
myScrollUpButton = new SimpleButton;
myScrollDownButton = new SimpleButton;

myScrollUpButton.addEventListener(MouseEvent.CLICK, onScrollUpButtonClick);
myScrollDownButton.addEventListener(MouseEvent.CLICK, onScrollDownButtonClick);

myScrollUpButton.upState = makeButtonStateBitmap("Scroll Up", "up");
myScrollUpButton.overState = makeButtonStateBitmap("Scroll Up", "over");
myScrollUpButton.downState = makeButtonStateBitmap("Scroll Up", "down");
myScrollUpButton.hitTestState = myScrollUpButton.overState;

myScrollDownButton.upState = makeButtonStateBitmap("Scroll Down", "up");
myScrollDownButton.overState = makeButtonStateBitmap("Scroll Down", "over");
myScrollDownButton.downState = makeButtonStateBitmap("Scroll Down", "down");
myScrollDownButton.hitTestState = myScrollDownButton.overState;

updateDisplay();

myScrollUpButton.x = 0;
myScrollUpButton.y = 0;
myScrollDownButton.x = myScrollUpButton.x;
myScrollDownButton.y = myScrollUpButton.y + ((myScrollUpButton.height + 1) * (spellsVisible + 1));

mySprite.addChild(myScrollUpButton);
mySprite.addChild(myScrollDownButton);
}

private function scrollList(delta:int):void
{
if (delta < 0)
{
// scroll up

if (myScroll == 0) { return; }
trace("scroll up");
myScroll--;

}
else
{
// scroll down

if (myScroll == myList.length - spellsVisible) { return; }
trace("scroll down");
myScroll++;
}

updateDisplay();
}

private function onScrollUpButtonClick(e:MouseEvent):void { scrollList( -1); }
private function onScrollDownButtonClick(e:MouseEvent):void { scrollList(1); }
private function onSpellButtonClick(e:MouseEvent):void
{
var b:MagicButton = MagicButton(e.target);
if (onClick != null)
{
onClick(this, myList[b.storage.id]);
}
}

private function makeButtonStateBitmap(caption:String, state:String):Bitmap
{
var label:TextField = new TextField();
label.defaultTextFormat = new TextFormat("Consolas", 14);
label.text = caption;
label.textColor = 0xFFFFFF;
label.autoSize = TextFieldAutoSize.LEFT;

var b:BitmapData = new BitmapData(mySprite.width, 8 + label.textHeight, false, 0xFFFFFF);
b.fillRect(new Rectangle(1, 1, b.width - 2, b.height - 2), 0x000000);

var labelGlow:GlowFilter = null;

if (state == "up")
{
label.textColor = 0xFFFFFF;
}
else if (state == "over")
{
label.textColor = 0xFFFF00;
labelGlow = new GlowFilter(0xF89207, 1.0);
}
else
{
label.textColor = 0xFFFF00;
labelGlow = new GlowFilter(0xF89207, 1.0);
}

if (labelGlow)
{
label.filters = [labelGlow];
}

var m:Matrix = new Matrix;
m.identity();
m.tx = (mySprite.width - label.width) * 0.5;
m.ty = (b.height - label.height) * 0.5;
b.draw(label, m);

return new Bitmap(b, PixelSnapping.NEVER, false);
}

public function attachTo(parent:Sprite):void
{
if (parent == null)
{
// cannot attach to a null sprite object
return;
}

if (mySprite.parent != null)
{
// cannot attach to multiple parents
return;
}

if (parent.contains(mySprite))
{
// cannot attach multiple times
return;
}

parent.addChild(mySprite);
}

public function detach():void
{
if (mySprite.parent == null)
{
// sprite is not attached to anything
return;
}

while (mySprite.numChildren > 0)
{
mySprite.removeChildAt(0);
}

mySprite.parent.removeChild(mySprite);
}

}
}

class MagicButton extends flash.display.SimpleButton
{
public var storage:Object = {}
public function MagicButton(data:Object = null)
{
if (data != null)
{
storage = data;
}
}
}

Here is a working example Main.as showing usage of the MagicMenu class
Code:
package
{
import assets.*;
import flash.display.*;
import flash.events.*;

public class Main extends Sprite
{
private var magicMenu:MagicMenu;

public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point

// create the magic menu
magicMenu = new MagicMenu(64, 64, 220);

// populate the magic menu
magicMenu.add("Fire", "fire", 1);
magicMenu.add("Fira", "fire", 2);
magicMenu.add("Firaga", "fire", 3);
magicMenu.add("Blizzard", "ice", 1);
magicMenu.add("Blizzara", "ice", 2);
magicMenu.add("Blizzaga", "ice", 3);
magicMenu.add("Cure", "cure", 1);
magicMenu.add("Cura", "cure", 2);
magicMenu.add("Curaga", "cure", 3);

// set the event handler function which gets called when a spell is selected
magicMenu.onClick = onMagicMenuSelection;

// set up the magic menu display
magicMenu.spellsVisible = 5;
magicMenu.createDisplay();

// add the magic menu to the stage
magicMenu.attachTo(this);
}

private function onMagicMenuSelection(menu:MagicMenu, selection:Object):void
{
trace("magic spell", selection.name, "selected.");
trace("casting", selection.type, "level", selection.level);
}
}
}
Logged

Tags:
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!