Menu
//////////////////////////////////////////////////////////////////////////////
[AS3.0] Menuコンポーネントに挑戦! (1)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=996
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/mP8a
*/
////////////////////////////////////////////////////////////////////////////////
// [AS3.0] Menuコンポーネントに挑戦! (1)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=996
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
[SWF(backgroundColor="#EEEEEE", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var menu:Menu;
private var itemList:Array;
private var label:Label;
public function Main() {
//Wonderfl.capture_delay(8);
init();
}
private function init():void {
itemList = new Array();
itemList.push({label: "DotLight [Blue]", link: "http://wonderfl.net/c/3lDU"});
itemList.push({label: "LightFolium + SoundMixer", link: "http://wonderfl.net/c/92Jy"});
itemList.push({label: "EscapeDot", link: "http://wonderfl.net/c/ztPH"});
itemList.push({label: "ドット地形", link: "http://wonderfl.net/c/ticj"});
itemList.push({label: "Fireworks + SoundMixer", link: "http://wonderfl.net/c/mDrG"});
itemList.push({label: "FlickrMultiLoader", link: "http://wonderfl.net/c/sI8g"});
itemList.push({label: "Reversi", link: "http://wonderfl.net/c/vDfO"});
itemList.push({label: "SorcerersFingerSound [color] - 魔法使いの指", link: "http://wonderfl.net/c/sGB5"});
itemList.push({label: "TransitFlare", link: "http://wonderfl.net/c/g2HX"});
itemList.push({label: "Flare", link: "http://wonderfl.net/c/mMPQ"});
itemList.push({label: "星と空間 (フルスクリーン)", link: "http://wonderfl.net/c/gRq0"});
itemList.push({label: "MP3Player (1)", link: "http://wonderfl.net/c/5Kfb"});
itemList.push({label: "ツリー形式のメニュー", link: "http://wonderfl.net/c/3n23"});
itemList.push({label: "Calendar (祝日あり)", link: "http://wonderfl.net/c/3Roc"});
itemList.push({label: "ひよこちゃん簡易チャット", link: "http://wonderfl.net/c/o1Wq"});
menu = new Menu();
addChild(menu);
menu.x = 20;
menu.y = 20;
menu.init({label: "wonderfl", width: 80});
menu.dataProvider = itemList;
menu.addEventListener(CompoEvent.SELECT, select, false, 0, true);
label = new Label(18);
addChild(label);
label.x = 20;
label.y = 400;
label.textColor = 0x333333;
}
private function select(evt:CompoEvent):void {
var item:Object = itemList[evt.value];
label.text = item.label;
navigateToURL(new URLRequest(item.link), "_blank");
}
}
}
//////////////////////////////////////////////////
// Menuクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
class Menu extends Sprite {
public var id:uint;
private var tab:Sprite;
private var base:Shape;
private var txt:TextField;
private var label:String = "";
private static var fontType:String = "_ゴシック";
private var _width:uint = 60;
private static var _height:uint = 20;
private static var tHeight:uint = 20;
private static var bColor:uint = 0xFFFFFF;
private static var cColor:uint = 0x3165B5;
private static var upColor:uint = 0x000000;
private static var overColor:uint = 0xFFFFFF;
private static var offColor:uint = 0x999999;
private static var bColorTrans:ColorTransform;
private static var cColorTrans:ColorTransform;
private var child:MenuChild;
private var dataList:Array;
private var _enabled:Boolean = true;
private var _selected:Boolean = false;
public function Menu() {
}
public function init(option:Object):void {
if (option.id != undefined) id = option.id;
if (option.label != undefined) label = option.label;
if (option.width != undefined) _width = option.width;
draw();
}
private function draw():void {
bColorTrans = new ColorTransform();
bColorTrans.color = bColor;
cColorTrans = new ColorTransform();
cColorTrans.color = cColor;
tab = new Sprite();
base = new Shape();
txt = new TextField();
addChild(tab);
tab.addChild(base);
tab.addChild(txt);
createBox(base, _width, _height);
txt.y = 1;
txt.width = _width;
txt.height = _height - 1;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = 12;
tf.align = TextFormatAlign.CENTER;
txt.defaultTextFormat = tf;
txt.text = label;
_up();
enabled = true;
tab.mouseChildren = false;
}
private function rollOver(evt:MouseEvent):void {
_over();
}
private function rollOut(evt:MouseEvent):void {
up();
}
private function press(evt:MouseEvent):void {
_over();
}
private function release(evt:MouseEvent):void {
_over();
}
private function click(evt:MouseEvent):void {
_over();
child.opencloseMenu();
}
private function up():void {
if (_selected) {
_over();
} else {
_up();
}
}
private function _up():void {
txt.textColor = upColor;
base.transform.colorTransform = bColorTrans;
}
private function _over():void {
txt.textColor = overColor;
base.transform.colorTransform = cColorTrans;
}
private function _off():void {
txt.textColor = offColor;
base.transform.colorTransform = bColorTrans;
}
public function set dataProvider(list:Array):void {
dataList = list;
if (dataList.length > 0) addChildren();
}
private function addChildren():void {
child = new MenuChild(dataList, this);
addChild(child);
child.y = tHeight;
child.visible = false;
child.addEventListener(MouseEvent.CLICK, select, false, 0, true);
}
private function mouseDown(evt:MouseEvent):void {
if (!hitTestPoint(stage.mouseX, stage.mouseY, true)) {
child.closeMenu();
}
}
public function initialize(param:uint):void {
var selectedID:uint = param;
child.selectItem(selectedID);
}
private function select(evt:MouseEvent):void {
var selectedID:uint = evt.target.id;
var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, selectedID);
dispatchEvent(e);
}
public function get selected():Boolean {
return _selected;
}
public function set selected(param:Boolean):void {
_selected = param;
if (_enabled) up();
}
public function get enabled():Boolean {
return _enabled;
}
public function set enabled(param:Boolean):void {
_enabled = param;
tab.buttonMode = _enabled;
tab.mouseEnabled = _enabled;
tab.useHandCursor = _enabled;
if (_enabled) {
_up();
tab.addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
tab.addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
tab.addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
tab.addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
tab.addEventListener(MouseEvent.CLICK, click, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown, false, 0, true);
} else {
_off();
tab.removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
tab.removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
tab.removeEventListener(MouseEvent.MOUSE_DOWN, press);
tab.removeEventListener(MouseEvent.MOUSE_UP, release);
tab.removeEventListener(MouseEvent.CLICK, click);
stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
}
private function createBox(target:Shape, w:uint, h:uint):void {
target.graphics.clear();
target.graphics.beginFill(bColor);
target.graphics.drawRect(0, 0, w, h);
target.graphics.endFill();
}
}
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
class MenuChild extends Sprite {
private var _width:uint = 100;
private var _height:uint;
private static var tHeight:uint = 20;
private static var bColor:uint = 0xFFFFFF;
private static var sColor:uint = 0x000000;
private var dataList:Array;
private var max:uint;
private var itemList:Array;
private var maxWidth:uint = 0;
private var back:Shape;
private var shade:DropShadowFilter;
private var menu:Menu;
private var opened:Boolean = false;
private var selectedID:uint;
public function MenuChild(list:Array, m:Menu) {
dataList = list;
max = dataList.length;
_height = tHeight*max;
menu = m;
init();
}
private function init():void {
back = new Shape();
addChild(back);
shade = new DropShadowFilter(1, 90, sColor, 0.5, 4, 4, 2, 3, false, false);
back.filters = [shade];
itemList = new Array();
for (var n:uint = 0; n < max; n++) {
var item:MenuItem = new MenuItem({id: n, label: dataList[n].label});
addChild(item);
item.y = tHeight*n;
itemList.push(item);
item.addEventListener(MouseEvent.CLICK, select, false, 0, true);
resizeWidth(item);
}
}
private function select(evt:MouseEvent):void {
closeMenu();
selectedID = MenuItem(evt.currentTarget).id;
checkItem();
}
public function selectItem(id:uint):void {
selectedID = id;
checkItem();
}
public function opencloseMenu():void {
if (!opened) {
openMenu();
} else {
closeMenu();
}
}
private function openMenu():void {
opened = true;
visible = true;
menu.selected = true;
}
public function closeMenu():void {
opened = false;
visible = false;
menu.selected = false;
}
private function checkItem():void {
for (var n:uint = 0; n < itemList.length; n++) {
var item:MenuItem = itemList[n];
if (n == selectedID) {
item.selected = true;
} else {
item.selected = false;
}
}
}
private function resizeWidth(item:MenuItem):void {
if (item._width > maxWidth) maxWidth = item._width;
if (itemList.length >= max) resize();
}
private function resize():void {
_width = maxWidth;
createBox(back, _width, _height);
for (var n:uint = 0; n < max; n++) {
var item:MenuItem = itemList[n];
item._width = maxWidth;
item.txt.width = item._width - 20;
createBox(item.base, item._width, item._height);
}
}
private function createBox(target:Shape, w:uint, h:uint):void {
target.graphics.clear();
target.graphics.beginFill(bColor);
target.graphics.drawRect(0, 0, w, h);
target.graphics.endFill();
}
}
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
class MenuItem extends Sprite {
public var id:uint;
private var item:Sprite;
public var base:Shape;
public var txt:TextField;
private var check:TextField;
public var _width:uint = 100;
public var _height:uint = 20;
private var label:String = "";
private static var fontType:String = "_ゴシック";
private var mark:String = String.fromCharCode(10003);
private static var checkType:String = "_ゴシック";
private static var bColor:uint = 0xFFFFFF;
private static var cColor:uint = 0x3165B5;
private static var upColor:uint = 0x000000;
private static var overColor:uint = 0xFFFFFF;
private static var bColorTrans:ColorTransform;
private static var cColorTrans:ColorTransform;
private var _selected:Boolean = false;
public function MenuItem(option:Object) {
if (option.id != undefined) id = option.id;
if (option.label) label = option.label;
init();
}
private function init():void {
bColorTrans = new ColorTransform();
bColorTrans.color = bColor;
cColorTrans = new ColorTransform();
cColorTrans.color = cColor;
item = new Sprite();
base = new Shape();
txt = new TextField();
check = new TextField();
addChild(item);
item.addChild(base);
item.addChild(txt);
item.addChild(check);
txt.x = 20;
txt.y = 1;
txt.width = _width;
txt.height = _height - 1;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = 12;
tf.align = TextFormatAlign.LEFT;
txt.defaultTextFormat = tf;
txt.text = label;
_width = txt.textWidth + 35;
check.x = 3;
check.y = -1;
check.width = 12;
check.height = 22;
check.type = TextFieldType.DYNAMIC;
check.selectable = false;
//check.embedFonts = true;
//check.antiAliasType = AntiAliasType.ADVANCED;
var tfc:TextFormat = new TextFormat();
tfc.font = checkType;
tfc.size = 12;
tfc.align = TextFormatAlign.LEFT;
check.defaultTextFormat = tfc;
check.text = mark;
check.visible = false;
buttonMode = true;
mouseEnabled = true;
useHandCursor = true;
_up();
mouseChildren = false;
addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
addEventListener(MouseEvent.CLICK, click, false, 0, true);
}
private function rollOver(evt:MouseEvent):void {
_over();
}
private function rollOut(evt:MouseEvent):void {
_up();
}
private function press(evt:MouseEvent):void {
_over();
}
private function release(evt:MouseEvent):void {
_up();
}
private function click(evt:MouseEvent):void {
_up();
}
private function _up():void {
txt.textColor = upColor;
check.textColor = upColor;
base.transform.colorTransform = bColorTrans;
}
private function _over():void {
txt.textColor = overColor;
check.textColor = overColor;
base.transform.colorTransform = cColorTrans;
}
public function get selected():Boolean {
return _selected;
}
public function set selected(param:Boolean):void {
_selected = param;
check.visible = _selected;
}
}
//////////////////////////////////////////////////
// CompoEventクラス
//////////////////////////////////////////////////
import flash.events.Event;
class CompoEvent extends Event {
public static const SELECT:String = "select";
public static const CHANGE:String = "change";
public var value:*;
public function CompoEvent(type:String, value:*) {
super(type);
this.value = value;
}
public override function clone():Event {
return new CompoEvent(type, value);
}
}
//////////////////////////////////////////////////
// Labelクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
class Label extends Sprite {
private var txt:TextField;
private var fontSize:uint;
private static var fontType:String = "_ゴシック";
private static var _height:uint = 20;
public function Label(s:uint) {
fontSize = s;
draw();
}
private function draw():void {
txt = new TextField();
addChild(txt);
txt.height = _height;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = fontSize;
tf.align = TextFormatAlign.LEFT;
txt.defaultTextFormat = tf;
}
public function set text(param:String):void {
txt.text = param;
}
public function set textColor(param:uint):void {
txt.textColor = param;
}
}