drawTriangles (5)
////////////////////////////////////////////////////////////////////////////////
// drawTriangles (5)
//
// [AS3.0] drawTriangles()メソッド! (5)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1616
////////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/68N4
*/
////////////////////////////////////////////////////////////////////////////////
// drawTriangles (5)
//
// [AS3.0] drawTriangles()メソッド! (5)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1616
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.ITween;
import org.libspark.betweenas3.events.TweenEvent;
import org.libspark.betweenas3.easing.*;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var fontloader:FontLoader;
//private static var basePath:String = "";
private static var basePath:String = "http://www.project-nya.jp/images/flash/";
private static var fontPath:String = "fonts/MyriadProSemibold.swf";
private static var className:String = "MyriadProSemibold";
private var progressBar:ProgressBar;
private var loader:ImageLoader;
private static var basicPath:String = "http://assets.wonderfl.net/images/related_images/";
private static var filePath:String = "8/8c/8c69/8c6904101b9e3edcedcd4d40a0a21741ceac7f24";
private var container:Sprite;
private var texture:BitmapData;
private var perspective:Perspective;
private static var cx:uint = 232;
private static var cy:uint = 232;
private var view:Object;
private var photos:Array;
private var indices:Array;
private var vertexs:Array;
private var perspectives:Array;
private var controller:Sprite;
public function Main() {
//Wonderfl.capture_delay(4);
init();
}
private function init():void {
fontloader = new FontLoader();
fontloader.addEventListener(FontLoader.COMPLETE, loaded, false, 0, true);
fontloader.load(basePath + fontPath, className);
}
private function loaded(evt:Event):void {
fontloader.removeEventListener(FontLoader.COMPLETE, loaded);
fontloader = null;
//
progressBar = new ProgressBar(465);
addChild(progressBar);
progressBar.x = 232;
progressBar.y = 222;
progressBar.addEventListener(ProgressBar.FADE_OUT, fadeOut, false, 0, true);
progressBar.addEventListener(ProgressBar.BAR_FADE_OUT, barFadeOut, false, 0, true);
progressBar.init();
//
loader = new ImageLoader();
loader.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
loader.addEventListener(Event.COMPLETE, complete, false, 0, true);
loader.load(basicPath + filePath, true);
}
private function progress(evt:ProgressEvent):void {
progressBar.progress(evt.bytesLoaded, evt.bytesTotal);
}
private function complete(evt:Event):void {
loader.removeEventListener(ProgressEvent.PROGRESS, progress);
loader.removeEventListener(Event.COMPLETE, complete);
//
texture = loader.content.bitmapData;
initialize();
}
private function fadeOut(evt:Event):void {
progressBar.removeEventListener(ProgressBar.FADE_OUT, fadeOut);
}
private function barFadeOut(evt:Event):void {
progressBar.removeEventListener(ProgressBar.BAR_FADE_OUT, barFadeOut);
//
var itween:ITween = BetweenAS3.parallel(
BetweenAS3.to(container, {alpha: 1, visible: 1}, 0.6, Linear.easeNone),
BetweenAS3.to(controller, {alpha: 1, visible: 1}, 0.6, Linear.easeNone)
);
itween.addEventListener(TweenEvent.COMPLETE, faded, false, 0, true);
itween.play();
}
private function faded(evt:Event):void {
evt.target.removeEventListener(TweenEvent.COMPLETE, faded);
removeChild(progressBar);
progressBar = null;
loader = null;
}
private function initialize():void {
container = new Sprite();
addChild(container);
container.alpha = 0;
container.visible = false;
controller = new Sprite();
addChild(controller);
controller.alpha = 0;
controller.visible = false;
perspective = new Perspective();
perspective.init(cx, cy);
createPoints();
createFaces();
perspective.transform();
view = {x: 0, y: 0, z:0};
apply();
setup();
}
private function apply():void {
for (var n:uint = 0; n < 6; n++) {
var _perspective:PerspectiveUtil = perspectives[n];
var list:Array = indices[n];
var p1:Point = new Point(vertexs[list[0]].x - cx, vertexs[list[0]].y - cy);
var p2:Point = new Point(vertexs[list[1]].x - cx, vertexs[list[1]].y - cy);
var p3:Point = new Point(vertexs[list[2]].x - cx, vertexs[list[2]].y - cy);
var p4:Point = new Point(vertexs[list[3]].x - cx, vertexs[list[3]].y - cy);
_perspective.transform(p1, p2, p3, p4);
}
}
private function createPoints():void {
photos = new Array();
var bitmapData1:BitmapData = new BitmapData(240, 240, false, 0xFFFFFFFF);
var matrix1:Matrix = new Matrix();
matrix1.translate(0, 0);
bitmapData1.draw(texture, matrix1);
var bitmapData2:BitmapData = new BitmapData(240, 240, false, 0xFFFFFFFF);
var matrix2:Matrix = new Matrix();
matrix2.translate(-240, 0);
bitmapData2.draw(texture, matrix2);
var bitmapData3:BitmapData = new BitmapData(240, 240, false, 0xFFFFFFFF);
var matrix3:Matrix = new Matrix();
matrix3.translate(-480, 0);
bitmapData3.draw(texture, matrix3);
var bitmapData4:BitmapData = new BitmapData(240, 240, false, 0xFFFFFFFF);
var matrix4:Matrix = new Matrix();
matrix4.translate(0, -240);
bitmapData4.draw(texture, matrix4);
var bitmapData5:BitmapData = new BitmapData(240, 240, false, 0xFFFFFFFF);
var matrix5:Matrix = new Matrix();
matrix5.translate(-240, -240);
bitmapData5.draw(texture, matrix5);
var bitmapData6:BitmapData = new BitmapData(240, 240, false, 0xFFFFFFFF);
var matrix6:Matrix = new Matrix();
matrix6.translate(-480, -240);
bitmapData6.draw(texture, matrix6);
photos.push(bitmapData1);
photos.push(bitmapData2);
photos.push(bitmapData3);
photos.push(bitmapData4);
photos.push(bitmapData5);
photos.push(bitmapData6);
//
vertexs = new Array();
createPoint(6, -50, 50, -50);
createPoint(6, 50, 50, -50);
createPoint(6, 50, 50, 50);
createPoint(6, -50, 50, 50);
createPoint(6, -50, -50, -50);
createPoint(6, 50, -50, -50);
createPoint(6, 50, -50, 50);
createPoint(6, -50, -50, 50);
}
private function createPoint(id:uint, px:int, py:int, pz:int):void {
var point:Object3D = new Object3D();
container.addChild(point);
perspective.register(point, id);
point.px = px;
point.py = py;
point.pz = pz;
vertexs.push(point);
}
private function createFaces():void {
indices = new Array();
indices.push([0, 1, 4, 5]);
indices.push([1, 2, 5, 6]);
indices.push([2, 3, 6, 7]);
indices.push([3, 0, 7, 4]);
indices.push([0, 1, 3, 2]);
indices.push([4, 5, 7, 6]);
//
perspectives = new Array();
createFace(0, 0, 0, -0.1);
createFace(1, 0.1, 0, 0);
createFace(2, 0, 0, 0.1);
createFace(3, -0.1, 0, 0);
createFace(4, 0, 0.1, 0);
createFace(5, 0, -0.1, 0);
}
private function createFace(id:uint, px:Number, py:Number, pz:Number):void {
var face:Object3D = new Object3D();
container.addChild(face);
perspective.register(face, id);
face.px = px;
face.py = py;
face.pz = pz;
var texture:BitmapData = photos[id];
var _perspective:PerspectiveUtil = new PerspectiveUtil(face.graphics, texture);
perspectives.push(_perspective);
}
private function setup():void {
var sliderX:Slider = new Slider();
sliderX.x = 32;
sliderX.y = 0;
controller.addChild(sliderX);
sliderX.init({label: "x", width: 100, grid: 10, min: -50, max: 50});
sliderX.addEventListener(CompoEvent.CHANGE, changeX, false, 0, true);
var sliderY:Slider = new Slider();
sliderY.x = 182;
sliderY.y = 0;
controller.addChild(sliderY);
sliderY.init({label: "y", width: 100, grid: 10, min: -50, max: 50});
sliderY.addEventListener(CompoEvent.CHANGE, changeY, false, 0, true);
var sliderZ:Slider = new Slider();
sliderZ.x = 332;
sliderZ.y = 0;
controller.addChild(sliderZ);
sliderZ.init({label: "z", width: 100, grid: 10, min: -50, max: 50});
sliderZ.addEventListener(CompoEvent.CHANGE, changeZ, false, 0, true);
var wheelX:Wheel = new Wheel();
wheelX.x = 32;
wheelX.y = 375;
controller.addChild(wheelX);
wheelX.init({label: "angleX"});
wheelX.addEventListener(CompoEvent.CHANGE, rotateX, false, 0, true);
var wheelY:Wheel = new Wheel();
wheelY.x = 182;
wheelY.y = 375;
controller.addChild(wheelY);
wheelY.init({label: "angleY"});
wheelY.addEventListener(CompoEvent.CHANGE, rotateY, false, 0, true);
var wheelZ:Wheel = new Wheel();
wheelZ.x = 332;
wheelZ.y = 375;
controller.addChild(wheelZ);
wheelZ.init({label: "angleZ"});
wheelZ.addEventListener(CompoEvent.CHANGE, rotateZ, false, 0, true);
}
private function changeX(evt:CompoEvent):void {
view.x = evt.value;
perspective.setView(view.x, view.y, view.z);
apply();
}
private function changeY(evt:CompoEvent):void {
view.y = evt.value;
perspective.setView(view.x, view.y, view.z);
apply();
}
private function changeZ(evt:CompoEvent):void {
view.z = evt.value;
perspective.setView(view.x, view.y, view.z);
apply();
}
private function rotateX(evt:CompoEvent):void {
perspective.rotateX(evt.value);
apply();
}
private function rotateY(evt:CompoEvent):void {
perspective.rotateY(evt.value);
apply();
}
private function rotateZ(evt:CompoEvent):void {
perspective.rotateZ(evt.value);
apply();
}
}
}
//////////////////////////////////////////////////
// Perspectiveクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
class Perspective {
private var objs:Array;
private var fl:Number = 250;
private var vp:Object;
private var view:Object;
private var ax:Number = 0;
private var ay:Number = 0;
private var az:Number = 0;
private var brighted:Boolean = false;
private var front:Number = -100;
private var back:Number = 100;
private var depths:Array;
public function Perspective() {
objs = new Array();
vp = {x: 0, y: 0};
view = {x: 0, y: 0, z: 0};
depths = new Array();
}
public function init(x:int, y:int):void {
vp = {x: x, y: -y};
}
public function register(obj:Object3D, n:uint):void {
objs.push(obj);
obj.id = n;
}
public function transform():void {
for (var n:uint = 0; n < objs.length; n++) {
var obj:Object3D = objs[n];
setPerspective(obj);
}
manageDepths();
}
private function setPerspective(obj:Object3D):void {
if (obj.pz + view.z > - fl + 50) {
var scale:Number = fl/(fl + obj.pz + view.z);
obj.scale = scale;
obj.x = vp.x + (obj.px - view.x)*scale;
obj.y = - vp.y - (obj.py + view.y)*scale;
if (brighted) obj.brightness = -100*(obj.pz + view.z - front)/(back - front);
obj.visible = true;
} else {
obj.visible = false;
}
}
private function manageDepths():void {
objs.sortOn(["pz", "id"], [Array.DESCENDING | Array.NUMERIC, Array.DESCENDING | Array.NUMERIC]);
for (var n:uint = 0; n < objs.length; n++) {
var obj:Object3D = objs[n];
obj.parent.addChildAt(obj, n+1);
}
}
public function screen(f:Number, b:Number):void {
if (front == back) {
brighted = false;
} else {
brighted = true;
front = f;
back = b;
}
}
public function setView(x:Number, y:Number, z:Number):void {
view.x = x;
view.y = -y;
view.z = -z;
transform();
}
public function rotate(x:Number, y:Number, z:Number):void {
for (var n:uint = 0; n < objs.length; n++) {
var obj:Object3D = objs[n];
angleX(obj, x - ax);
angleY(obj, y - ay);
angleZ(obj, z - az);
setPerspective(obj);
}
manageDepths();
ax = x%360;
ay = y%360;
az = z%360;
}
public function rotateX(angle:Number):void {
var a:Number = angle - ax;
for (var n:uint = 0; n < objs.length; n++) {
var obj:Object3D = objs[n];
angleX(obj, a);
setPerspective(obj);
}
manageDepths();
ax = angle%360;
}
public function rotateY(angle:Number):void {
var a:Number = angle - ay;
for (var n:uint = 0; n < objs.length; n++) {
var obj:Object3D = objs[n];
angleY(obj, a);
setPerspective(obj);
}
manageDepths();
ay = angle%360;
}
public function rotateZ(angle:Number):void {
var a:Number = angle - az;
for (var n:uint = 0; n < objs.length; n++) {
var obj:Object3D = objs[n];
angleZ(obj, a);
setPerspective(obj);
}
manageDepths();
az = angle%360;
}
private function angleX(obj:Object3D, angle:Number):void {
var radian:Number = -angle*Math.PI/180;
var cos:Number = Math.cos(radian);
var sin:Number = Math.sin(radian);
var ry:Number = obj.py*cos - obj.pz*sin;
var rz:Number = obj.pz*cos + obj.py*sin;
obj.py = ry;
obj.pz = rz;
}
private function angleY(obj:Object3D, angle:Number):void {
var radian:Number = -angle*Math.PI/180;
var cos:Number = Math.cos(radian);
var sin:Number = Math.sin(radian);
var rx:Number = obj.px*cos - obj.pz*sin;
var rz:Number = obj.pz*cos + obj.px*sin;
obj.px = rx;
obj.pz = rz;
}
private function angleZ(obj:Object3D, angle:Number):void {
var radian:Number = -angle*Math.PI/180;
var cos:Number = Math.cos(radian);
var sin:Number = Math.sin(radian);
var rx:Number = obj.px*cos - obj.py*sin;
var ry:Number = obj.py*cos + obj.px*sin;
obj.px = rx;
obj.py = ry;
}
}
//////////////////////////////////////////////////
// Object3Dクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.geom.ColorTransform;
class Object3D extends Sprite {
public var id:uint;
public var px:Number;
public var py:Number;
public var pz:Number;
public var angle:Number;
private var _scale:Number = 1;
private var colorTrans:ColorTransform;
private var _brightness:Number = 0;
public function Object3D() {
colorTrans = new ColorTransform(0, 0, 0, 1, 0, 0, 0, 0);
}
public function get scale():Number {
return _scale;
}
public function set scale(value:Number):void {
_scale = value;
scaleX = scaleY = _scale;
}
public function get brightness():Number {
return _brightness;
}
public function set brightness(value:Number):void {
_brightness = value;
var percent:uint = 100 - Math.abs(value);
var offset:uint = (value > 0) ? (255*value*0.01) : 0;
setColorTrans(percent, offset);
}
private function setColorTrans(percent:Number, offset:Number):void {
colorTrans.redMultiplier = percent*0.01;
colorTrans.greenMultiplier = percent*0.01;
colorTrans.blueMultiplier = percent*0.01;
colorTrans.redOffset = offset;
colorTrans.greenOffset = offset;
colorTrans.blueOffset = offset;
colorTrans.alphaMultiplier = alpha;
colorTrans.alphaOffset = 0;
transform.colorTransform = colorTrans;
}
}
//////////////////////////////////////////////////
// PerspectiveUtilクラス
//////////////////////////////////////////////////
import flash.display.Graphics;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.display.TriangleCulling;
class PerspectiveUtil {
private var graphics:Graphics;
private var texture:BitmapData;
private static var indices:Vector.<int> = Vector.<int>([0, 1, 2, 1, 3, 2]);
public function PerspectiveUtil(g:Graphics, t:BitmapData) {
graphics = g;
texture = t;
}
public function transform(p1:Point, p2:Point, p3:Point, p4:Point):void {
var pc:Point = getIntersection(p1, p4, p2, p3);
if (!Boolean(pc)) {
drawNone();
return;
}
var l1:Number = Point.distance(p1, pc);
var l2:Number = Point.distance(pc, p4);
var r1:Number = Point.distance(p2, pc);
var r2:Number = Point.distance(pc, p3);
var f:Number = (l1 + l2)/(r1 + r2);
//
var vertices:Vector.<Number> = Vector.<Number>([p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y]);
var uvtData:Vector.<Number> = Vector.<Number>([0, 0, (1/l2)*f, 1, 0, (1/r2), 0, 1, (1/r1), 1, 1, (1/l1)*f]);
//
graphics.clear();
graphics.beginBitmapFill(texture, null, false, true);
graphics.drawTriangles(vertices, indices, uvtData, TriangleCulling.NONE);
graphics.endFill();
}
private function getIntersection(p1:Point, p2:Point, p3:Point, p4:Point):Point {
var a1:Number = p2.y - p1.y;
var b1:Number = p1.x - p2.x;
var a2:Number = p4.y - p3.y;
var b2:Number = p3.x - p4.x;
var d:Number = a1*b2 - a2*b1;
if (d == 0) return null;
var c1:Number = p2.x*p1.y - p1.x*p2.y;
var c2:Number = p4.x*p3.y - p3.x*p4.y;
var point:Point = new Point((b1*c2 - b2*c1)/d, (a2*c1 - a1*c2)/d);
if (Point.distance(point, p2) > Point.distance(p1, p2)) return null;
if (Point.distance(point, p1) > Point.distance(p1, p2)) return null;
if (Point.distance(point, p4) > Point.distance(p3, p4)) return null;
if (Point.distance(point, p3) > Point.distance(p3, p4)) return null;
return point;
}
private function drawNone():void {
graphics.clear();
graphics.beginBitmapFill(texture, null, false, true);
graphics.drawTriangles(null, null, null, TriangleCulling.NONE);
graphics.endFill();
}
}
//////////////////////////////////////////////////
// FontLoaderクラス
//////////////////////////////////////////////////
import flash.events.EventDispatcher;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.text.Font;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.system.ApplicationDomain;
import flash.system.SecurityDomain;
import flash.system.LoaderContext;
import flash.utils.getDefinitionByName;
class FontLoader extends EventDispatcher {
public var id:uint;
private var loader:Loader;
private var info:LoaderInfo;
private var _className:String;
private var _font:Font;
private var _fontName:String;
private var embeded:Boolean = false;
public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
public static const INIT:String = Event.INIT;
public static const COMPLETE:String = Event.COMPLETE;
public function FontLoader() {
loader = new Loader();
info = loader.contentLoaderInfo;
}
public function load(file:String, name:String, e:Boolean = false):void {
_className = name;
embeded = e;
info.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
info.addEventListener(Event.INIT, initialize, false, 0, true);
info.addEventListener(Event.COMPLETE, complete, false, 0, true);
try {
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
context.securityDomain = SecurityDomain.currentDomain;
loader.load(new URLRequest(file), context);
} catch (err:Error) {
trace(err.message);
}
}
public function unload():void {
loader.unload();
}
private function progress(evt:ProgressEvent):void {
dispatchEvent(evt);
}
private function ioerror(evt:IOErrorEvent):void {
loader.unload();
dispatchEvent(new Event(FontLoader.IO_ERROR));
}
private function httpstatus(evt:HTTPStatusEvent):void {
dispatchEvent(new Event(FontLoader.HTTP_STATUS));
}
private function securityerror(evt:SecurityErrorEvent):void {
dispatchEvent(new Event(FontLoader.SECURITY_ERROR));
}
private function initialize(evt:Event):void {
dispatchEvent(new Event(FontLoader.INIT));
}
private function complete(evt:Event):void {
info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
info.removeEventListener(Event.INIT, initialize);
info.removeEventListener(Event.COMPLETE, complete);
var FontClass:Class = Class(ApplicationDomain.currentDomain.getDefinition(className));
if (!embeded) {
Font.registerFont(FontClass);
_font = Font(new FontClass());
} else {
var document:Object = new FontClass();
_font = document.font;
}
_fontName = _font.fontName;
dispatchEvent(new Event(FontLoader.COMPLETE));
}
public function get className():String {
return _className;
}
public function get font():Font {
return _font;
}
public function get fontName():String {
return _fontName;
}
}
//////////////////////////////////////////////////
// ProgressBarクラス
//////////////////////////////////////////////////
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.filters.DropShadowFilter;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
class ProgressBar extends Sprite {
private var base:Sprite;
private var bar:Shape;
private var prog:Shape;
private var title:TextField;
private var txt:TextField;
private static var label:String = "loading...";
private static var fontType:String = "Myriad Pro Semibold";
private var _width:uint = 800;
private static var _height:uint = 40;
private static var bHeight:uint = 10;
private static var tWidth:uint = 50;
private static var tHeight:uint = 20;
private static var bColor:uint = 0x000000;
private static var tColor:uint = 0x000000;
private static var pColor:uint = 0x000000;
private static var eColor:uint = 0xCC0000;
private var shade:DropShadowFilter;
private var _percent:Number = 0;
private var targetPercent:Number;
private static var interval:uint = 500;
private static var deceleration:Number = 0.4;
private var timer:Timer;
public static const FADE_OUT:String = "fadeOut";
public static const BAR_FADE_OUT:String = "baseFadeOut";
public function ProgressBar(w:uint) {
_width = w;
draw();
percent = 0;
visible = false;
}
private function draw():void {
createChildren();
createText();
}
public function update(w:uint):void {
_width = w;
base.x = -_width/2;
createBar(bar, _width, pColor);
createLine(prog, _width, bColor);
}
private function createChildren():void {
base = new Sprite();
bar = new Shape();
prog = new Shape();
addChild(base);
base.addChild(bar);
base.addChild(prog);
base.x = -_width/2;
base.y = 8;
createBar(bar, _width, pColor);
createLine(prog, _width, bColor);
bar.mask = prog;
}
private function createText():void {
title = new TextField();
txt = new TextField();
addChild(title);
addChild(txt);
title.x = -tWidth;
title.y = -_height/2 + 2;
title.width = tWidth;
title.height = tHeight - 2;
title.type = TextFieldType.DYNAMIC;
title.selectable = false;
title.embedFonts = true;
title.antiAliasType = AntiAliasType.NORMAL;
var tfl:TextFormat = new TextFormat();
tfl.font = fontType;
tfl.size = 12;
tfl.align = TextFormatAlign.LEFT;
title.defaultTextFormat = tfl;
title.textColor = tColor;
title.text = label;
txt.x = 0;
txt.y = -_height/2 + 2;
txt.width = tWidth;
txt.height = tHeight - 2;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
txt.embedFonts = true;
txt.antiAliasType = AntiAliasType.NORMAL;
var tfr:TextFormat = new TextFormat();
tfr.font = fontType;
tfr.size = 12;
tfr.align = TextFormatAlign.RIGHT;
txt.textColor = tColor;
txt.defaultTextFormat = tfr;
}
public function init():void {
alpha = 1;
createBar(bar, _width, pColor);
percent = 0;
prog.x = 0;
prog.scaleX = 0;
visible = true;
}
public function error(e:String):void {
alpha = 1;
txt.text = "0%";
createBar(bar, _width, eColor);
prog.scaleX = 1;
visible = true;
}
public function progress(l:Number, t:Number):void {
targetPercent = Math.round(l/t*100);
addEventListener(Event.ENTER_FRAME, progressTo, false, 0, true);
}
private function progressTo(evt:Event):void {
percent += (targetPercent - percent)*deceleration;
if (Math.abs(targetPercent - percent) < 0.5) {
percent = targetPercent;
removeEventListener(Event.ENTER_FRAME, progressTo);
}
if (percent >= 100) {
timer = new Timer(interval, 1);
timer.addEventListener(TimerEvent.TIMER, fadeOut, false, 0, true);
timer.start();
}
}
private function fadeOut(evt:TimerEvent):void {
timer.removeEventListener(TimerEvent.TIMER, fadeOut);
addEventListener(Event.ENTER_FRAME, reduction, false, 0, true);
}
private function reduction(evt:Event):void {
prog.x += (_width - prog.x)*deceleration;
if (Math.abs(_width - prog.x) < 0.5) {
prog.x = _width;
removeEventListener(Event.ENTER_FRAME, reduction);
addEventListener(Event.ENTER_FRAME, baseFadeOut, false, 0, true);
dispatchEvent(new Event(ProgressBar.FADE_OUT));
}
}
private function baseFadeOut(evt:Event):void {
var speed:Number = 0.1;
alpha -= speed;
if (alpha <= 0) {
removeEventListener(Event.ENTER_FRAME, baseFadeOut);
alpha = 0;
visible = false;
dispatchEvent(new Event(ProgressBar.BAR_FADE_OUT));
}
}
public function get percent():Number {
return _percent;
}
public function set percent(value:Number):void {
_percent = value;
manage(_percent);
}
private function manage(p:Number):void {
txt.text = String(Math.round(p)) + "%";
prog.scaleX = p/100;
}
private function createBase(target:Shape, x:int, y:int, w:uint, h:uint, color:uint):void {
target.graphics.beginFill(color);
target.graphics.drawRect(x, y, w, h);
target.graphics.endFill();
}
private function createLine(target:Shape, w:uint, color:uint, alpha:Number = 1):void {
target.graphics.clear();
target.graphics.beginFill(color, alpha);
target.graphics.drawRect(0, 0, w, 1);
target.graphics.endFill();
}
private function createBar(target:Shape, w:uint, color:uint):void {
target.graphics.clear();
target.graphics.beginFill(color);
target.graphics.drawRect(0, 0, w, 1);
target.graphics.endFill();
}
}
//////////////////////////////////////////////////
// ImageLoaderクラス
//////////////////////////////////////////////////
import flash.events.EventDispatcher;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.system.LoaderContext;
class ImageLoader extends EventDispatcher {
public var id:uint;
private var loader:Loader;
private var info:LoaderInfo;
public var content:Bitmap;
private var smoothing:Boolean;
public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
public static const INIT:String = Event.INIT;
public static const COMPLETE:String = Event.COMPLETE;
public function ImageLoader() {
loader = new Loader();
info = loader.contentLoaderInfo;
}
public function load(file:String, s:Boolean = false):void {
smoothing = s;
info.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
info.addEventListener(Event.INIT, initialize, false, 0, true);
info.addEventListener(Event.COMPLETE, complete, false, 0, true);
try {
loader.load(new URLRequest(file), new LoaderContext(true));
} catch (err:Error) {
trace(err.message);
}
}
public function unload():void {
loader.unload();
}
private function progress(evt:ProgressEvent):void {
dispatchEvent(evt);
}
private function ioerror(evt:IOErrorEvent):void {
loader.unload();
dispatchEvent(new Event(ImageLoader.IO_ERROR));
}
private function httpstatus(evt:HTTPStatusEvent):void {
dispatchEvent(new Event(ImageLoader.HTTP_STATUS));
}
private function securityerror(evt:SecurityErrorEvent):void {
dispatchEvent(new Event(ImageLoader.SECURITY_ERROR));
}
private function initialize(evt:Event):void {
content = Bitmap(info.content);
if (smoothing) {
content.smoothing = true;
}
dispatchEvent(new Event(ImageLoader.INIT));
}
private function complete(evt:Event):void {
info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
info.removeEventListener(Event.INIT, initialize);
info.removeEventListener(Event.COMPLETE, complete);
dispatchEvent(new Event(ImageLoader.COMPLETE));
}
}
//////////////////////////////////////////////////
// Sliderクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import flash.filters.GlowFilter;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;
import flash.events.Event;
import flash.events.MouseEvent;
class Slider extends Sprite {
private var hole:Shape;
private var line:Sprite;
private var thumb:Sprite;
private var light:Shape;
private var shade:Shape;
private var base:Shape;
private var title:TextField;
private var txt:TextField;
private var label:String = "";
private static var fontType:String = "Myriad Pro Semibold";
private var _width:uint = 100;
private static var tHeight:uint = 20;
private static var bHeight:uint = 30;
private var grid:uint = 5;
private static var bColor:uint = 0xFFFFFF;
private static var tColor:uint = 0x666666;
private static var gColor:uint = 0x999999;
private static var mColor:uint = 0x333333;
private static var bgColor:uint = 0x0099FF;
private static var sColor:uint = 0x000000;
private static var offColor:uint = 0x999999;
private var min:Number = 0;
private var max:Number = 100;
private var initValue:Number = 0;
private var blueGlow:GlowFilter;
private var shadeDrop:DropShadowFilter;
private var value:Number;
private var _enabled:Boolean = true;
// コンストラクタ
public function Slider() {
}
// メソッド
public function init(option:Object):void {
if (option.label != undefined) label = option.label;
if (option.width != undefined) _width = option.width;
if (option.min != undefined) min = option.min;
if (option.max != undefined) max = option.max;
if (option.grid != undefined) grid = option.grid;
if (option.init != undefined) initValue = option.init;
draw();
}
private function draw():void {
shadeDrop = new DropShadowFilter(1, 90, sColor, 0.5, 4, 4, 2, 3, false, false);
blueGlow = new GlowFilter(bgColor, 0.6, 5, 5, 2, 3, false, true);
hole = new Shape();
line = new Sprite();
title = new TextField();
txt = new TextField();
thumb = new Sprite();
shade = new Shape();
light = new Shape();
base = new Shape();
addChild(hole);
addChild(line);
addChild(title);
addChild(txt);
addChild(thumb);
thumb.addChild(shade);
thumb.addChild(light);
thumb.addChild(base);
hole.y = bHeight;
createGradientHole(hole, _width, 3);
line.y = bHeight;
createGrid(line);
title.height = tHeight-1;
title.type = TextFieldType.DYNAMIC;
title.selectable = false;
title.embedFonts = true;
title.antiAliasType = AntiAliasType.ADVANCED;
title.textColor = tColor;
title.autoSize = TextFieldAutoSize.LEFT;
var tfl:TextFormat = new TextFormat();
tfl.font = fontType;
tfl.size = 14;
tfl.align = TextFormatAlign.LEFT;
title.defaultTextFormat = tfl;
title.text = label;
txt.x = title.textWidth;
txt.width = 50;
txt.height = tHeight-1;
txt.selectable = false;
txt.embedFonts = true;
txt.antiAliasType = AntiAliasType.ADVANCED;
var tfr:TextFormat = new TextFormat();
tfr.font = fontType;
tfr.size = 14;
tfr.align = TextFormatAlign.RIGHT;
txt.defaultTextFormat = tfr;
reset();
thumb.y = bHeight;
createThumb(shade, 8, 20, 12, sColor);
shade.filters = [shadeDrop];
createThumb(light, 8, 20, 12, bgColor);
light.filters = [blueGlow];
createThumb(base, 8, 20, 12, bColor);
_up();
enabled = true;
thumb.mouseChildren = false;
}
private function rollOver(evt:MouseEvent):void {
_over();
}
private function rollOut(evt:MouseEvent):void {
_up();
}
private function press(evt:MouseEvent):void {
_down();
var rect:Rectangle = new Rectangle(0, bHeight, _width, 0);
thumb.startDrag(false, rect);
thumb.addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
stage.addEventListener(Event.MOUSE_LEAVE, leave, false, 0, true);
thumb.addEventListener(Event.ENTER_FRAME, change, false, 0, true);
}
private function release(evt:MouseEvent):void {
_up();
thumb.stopDrag();
checkValue();
var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
dispatchEvent(e);
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(Event.MOUSE_LEAVE, leave);
thumb.removeEventListener(Event.ENTER_FRAME, change);
}
private function leave(evt:Event):void {
_up();
thumb.stopDrag();
checkValue();
var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
dispatchEvent(e);
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(Event.MOUSE_LEAVE, leave);
thumb.removeEventListener(Event.ENTER_FRAME, change);
}
private function _up():void {
light.visible = false;
}
private function _over():void {
light.visible = true;
}
private function _down():void {
light.visible = true;
}
private function _off():void {
light.visible = false;
txt.textColor = offColor;
}
private function change(evt:Event):void {
_down();
checkValue();
var e:CompoEvent = new CompoEvent(CompoEvent.CHANGE, value);
dispatchEvent(e);
}
private function checkValue():void {
value = min + Math.round(thumb.x/_width*(max-min));
txt.text = String(value);
}
public function get enabled():Boolean {
return _enabled;
}
public function set enabled(value:Boolean):void {
_enabled = value;
if (!_enabled) _off();
thumb.buttonMode = _enabled;
thumb.mouseEnabled = _enabled;
thumb.useHandCursor = _enabled;
if (_enabled) {
thumb.addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
thumb.addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
thumb.addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
thumb.addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
} else {
thumb.removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
thumb.removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
thumb.removeEventListener(MouseEvent.MOUSE_DOWN, press);
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
}
}
public function reset():void {
thumb.x = _width*(initValue-min)/(max-min);
value = initValue;
txt.text = String(value);
}
private function createGrid(target:Sprite):void {
for (var n:uint = 0; n <= grid; n++) {
var w:uint = Math.floor(_width/grid);
if (n == 0 || n == grid*0.5 || n == grid) {
createGridLine(target, w*n, mColor);
var _txt:TextField = new TextField();
target.addChild(_txt);
_txt.x = w*n - 20;
_txt.y = 13;
_txt.width = 40;
_txt.height = 14;
_txt.selectable = false;
_txt.embedFonts = true;
_txt.antiAliasType = AntiAliasType.ADVANCED;
_txt.textColor = mColor;
var tfc:TextFormat = new TextFormat();
tfc.font = fontType;
tfc.size = 10;
tfc.align = TextFormatAlign.CENTER;
_txt.defaultTextFormat = tfc;
if (n == 0) _txt.text = String(min);
if (n == grid*0.5) _txt.text = String(min+(max-min)*0.5);
if (n == grid) _txt.text = String(max);
} else {
createGridLine(target, w*n, gColor);
}
}
}
private function createThumb(target:Shape, w:uint, h:uint, y:uint, color:uint, alpha:Number = 1):void {
target.graphics.beginFill(color, alpha);
target.graphics.drawRoundRect(-w*0.5, -y, w, h, w);
target.graphics.endFill();
}
private function createGradientHole(target:Shape, w:uint, c:Number):void {
var colors:Array = [0x000000, 0x000000];
var alphas:Array = [0.4, 0];
var ratios:Array = [0, 255];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(w+c*2, c*2, 0.5*Math.PI, -c, -c);
target.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
target.graphics.drawRoundRect(-c, -c, w+c*2, c*2, c*2);
target.graphics.endFill();
}
private function createGridLine(target:Sprite, x:uint, color:uint):void {
target.graphics.lineStyle(0, color);
target.graphics.moveTo(x, 8);
target.graphics.lineTo(x, 12);
}
}
//////////////////////////////////////////////////
// Wheelクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import flash.geom.ColorTransform;
import flash.events.Event;
import flash.events.MouseEvent;
class Wheel extends Sprite {
private var base:Sprite;
private var thumb:Sprite;
private var point:Shape;
private var hit:Shape;
private var title:TextField;
private var txt:TextField;
private var label:String = "";
private static var fontType:String = "Myriad Pro Semibold";
private static var __width:uint = 90;
private static var _height:uint = 85;
private static var tHeight:uint = 20;
private static var bColor:uint = 0xFFFFFF;
private static var cColor:uint = 0x999999;
private static var sColor:uint = 0x000000;
private static var tColor:uint = 0x666666;
private static var pColor:uint = 0x666666;
private static var offColor:uint = 0xCCCCCC;
private static var cColorTrans:ColorTransform;
private static var pColorTrans:ColorTransform;
private static var offColorTrans:ColorTransform;
private var zero:Number = 0;
private var angle:Number = 0;
private var shade:DropShadowFilter;
private var initValue:Number;
private var value:Number;
private var _enabled:Boolean = true;
public function Wheel() {
}
public function init(option:Object):void {
if (option.label != undefined) label = option.label;
if (option.zero != undefined) zero = option.zero;
if (option.angle != undefined) angle = option.angle;
value = initValue = angle;
draw();
}
private function draw():void {
shade = new DropShadowFilter(1, 90, sColor, 0.4, 4, 4, 2, 3, false, false);
cColorTrans = new ColorTransform();
cColorTrans.color = cColor;
pColorTrans = new ColorTransform();
pColorTrans.color = pColor;
offColorTrans = new ColorTransform();
offColorTrans.color = offColor;
base = new Sprite();
thumb = new Sprite();
point = new Shape();
hit = new Shape();
title = new TextField();
txt = new TextField();
addChild(base);
base.addChild(thumb);
thumb.addChild(point);
thumb.addChild(hit);
addChild(title);
addChild(txt);
addChild(thumb);
base.x = thumb.x = 50;
base.y = thumb.y = 55;
createDonut(base, 30, 10);
base.filters = [shade];
point.x = 20;
createCircle(point, 5, bColor, 1);
hit.x = 20;
createCircle(hit, 10, bColor, 0);
title.height = tHeight-1;
title.type = TextFieldType.DYNAMIC;
title.selectable = false;
title.embedFonts = true;
title.antiAliasType = AntiAliasType.ADVANCED;
title.textColor = tColor;
title.autoSize = TextFieldAutoSize.LEFT;
var tfl:TextFormat = new TextFormat();
tfl.font = fontType;
tfl.size = 14;
tfl.align = TextFormatAlign.LEFT;
title.defaultTextFormat = tfl;
title.text = label;
txt.x = 50;
txt.width = 40;
txt.height = tHeight-1;
txt.selectable = false;
txt.embedFonts = true;
txt.antiAliasType = AntiAliasType.ADVANCED;
var tfr:TextFormat = new TextFormat();
tfr.font = fontType;
tfr.size = 14;
tfr.align = TextFormatAlign.RIGHT;
txt.defaultTextFormat = tfr;
reset();
_up();
enabled = true;
thumb.mouseChildren = false;
}
private function rollOver(evt:MouseEvent):void {
_over();
}
private function rollOut(evt:MouseEvent):void {
_up();
}
private function press(evt:MouseEvent):void {
_down();
thumb.addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_UP, releaseOutside, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_MOVE, change, false, 0, true);
stage.addEventListener(Event.MOUSE_LEAVE, leave, false, 0, true);
}
private function release(evt:MouseEvent):void {
_up();
checkValue();
var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
dispatchEvent(e);
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);
stage.removeEventListener(Event.MOUSE_LEAVE, leave);
}
private function releaseOutside(evt:MouseEvent):void {
_up();
checkValue();
var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
dispatchEvent(e);
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);
stage.removeEventListener(Event.MOUSE_LEAVE, leave);
}
private function leave(evt:Event):void {
_up();
checkValue();
var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
dispatchEvent(e);
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);
stage.removeEventListener(Event.MOUSE_LEAVE, leave);
}
private function _up():void {
point.transform.colorTransform = cColorTrans;
}
private function _over():void {
point.transform.colorTransform = pColorTrans;
}
private function _down():void {
point.transform.colorTransform = pColorTrans;
}
private function _off():void {
point.transform.colorTransform = offColorTrans;
txt.textColor = offColor;
}
private function change(evt:MouseEvent):void {
_down();
thumb.rotation = Math.round(Math.atan2(base.mouseY, base.mouseX)/Math.PI*180);
evt.updateAfterEvent();
checkValue();
var e:CompoEvent = new CompoEvent(CompoEvent.CHANGE, value);
dispatchEvent(e);
}
private function checkValue():void {
value = (thumb.rotation + 360 + 90 - zero)%360;
txt.text = String(value);
}
public function get enabled():Boolean {
return _enabled;
}
public function set enabled(value:Boolean):void {
_enabled = value;
if (!_enabled) _off();
thumb.buttonMode = _enabled;
thumb.mouseEnabled = _enabled;
thumb.useHandCursor = _enabled;
if (_enabled) {
thumb.addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
thumb.addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
thumb.addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
} else {
thumb.removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
thumb.removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
thumb.removeEventListener(MouseEvent.MOUSE_DOWN, press);
}
}
public function reset():void {
value = initValue;
thumb.rotation = value - 90 + zero;
txt.text = String(value);
}
private function createCircle(target:Shape, r:uint, color:uint, alpha:Number):void {
target.graphics.beginFill(color, alpha);
target.graphics.drawCircle(0, 0, r);
target.graphics.endFill();
}
private function createDonut(target:Sprite, outer:uint, inner:uint):void {
target.graphics.beginFill(bColor);
target.graphics.drawCircle(0, 0, outer);
target.graphics.drawCircle(0, 0, inner);
target.graphics.endFill();
}
}
//////////////////////////////////////////////////
// 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;
}
override public function clone():Event {
return new CompoEvent(type, value);
}
}