Presentation as3commons
/**
* Copyright leichtgewicht ( http://wonderfl.net/user/leichtgewicht )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/d40C
*/
// Note: In Progress ... (have to sleep now)
package {
import flash.text.TextFieldAutoSize;
import flash.system.Security;
import flash.events.Event;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.text.TextField;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.utils.setInterval;
import flash.text.Font;
import flash.display.Sprite;
import org.libspark.betweenas3.BetweenAS3;
public class Presentation extends Sprite {
public var container: Sprite = new Sprite();
private var _graph: Graph;
private var _actions: ActionList;
private var _processed: Array = [];
private var _console:TextField = new TextField();
public function Presentation() {
Security.loadPolicyFile("http://assets.wonderfl.net/crossdomain.xml");
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
addChild( container );
addChild( _console = new TextField() );
_console.visible = false;
_console.background = true;
var surrounding: Sprite = new Sprite();
var logo: Image = new Image("3/33/33c0/33c068a431b341f22d32e3d2bf9d7d8b266ad875");
logo.scaleX = logo.scaleY = 0.5;
var url: MyTextField = new MyTextField( 18, 0x999999);
url.x = 200;
url.y = 3;
url.text = "http://www.as3commons.org/as3-commons-logging/";
surrounding.addChild( logo );
surrounding.addChild( url );
_actions = inone(
introduction(),
showDO(surrounding),
page1(),
performance(),
size(),
hideDO(surrounding)
);
stage.addEventListener( MouseEvent.CLICK, next );
stage.addEventListener( KeyboardEvent.KEY_DOWN, handleKeyboard );
stage.addEventListener( Event.RESIZE, resize );
resize();
}
private function resize( e: Event = null ): void {
var r1: Number = stage.stageWidth/stage.stageHeight;
var r2: Number = PRES_WIDTH/PRES_HEIGHT;
if( r1 < r2 ) {
var newH: Number = (container.scaleY = container.scaleX = stage.stageWidth / PRES_WIDTH ) * PRES_HEIGHT;
container.y = (stage.stageHeight - newH)/2;
} else {
var newW: Number = (container.scaleY = container.scaleX = stage.stageHeight / PRES_HEIGHT ) * PRES_WIDTH;
container.x = (stage.stageWidth - newW)/2;
}
}
public function handleKeyboard( event: KeyboardEvent ):void {
switch( event.keyCode ) {
case 39: // arrow left
case 32: // space
_actions.next(this,event.shiftKey?0:1);
break;
case 37: // arrow right
_actions.prev(this,event.shiftKey?0:1);
break;
}
_console.text = event.keyCode + "";
}
public function next( event: MouseEvent = null ):void {
_actions.next(this,1);
}
public function prev( event: MouseEvent = null ):void {
_actions.prev(this,1);
}
}
}
import flash.filters.GlowFilter;
import flash.text.AntiAliasType;
import flash.system.LoaderContext;
import flash.filters.BlurFilter;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.text.TextFormat;
import flash.display.DisplayObjectContainer;
import flash.display.DisplayObject;
import flash.utils.Dictionary;
import flash.text.TextFieldAutoSize;
import flash.display.Shape;
import flash.text.TextField;
import flash.display.Sprite;
import flash.events.Event;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.IObjectTween;
import com.modestmaps.TweenMap;
import com.modestmaps.mapproviders.yahoo.*;
import com.modestmaps.mapproviders.microsoft.*;
import com.modestmaps.geo.Location;
const PRES_WIDTH: Number = 800.0;
const PRES_HEIGHT: Number = 600.0;
function introduction(): ActionList {
var data: Sprite = new Sprite();
var text: MyTextField = new MyTextField();
text.htmlText = "<b>Martin Heidegger</b>\n<br/><i>twitter:</i> @leichtgewicht<br />\n<i>mail:</i> mh@leichtgewicht.at<br />\n<i>lang:</i> de/en/jp \n<i>prog:</i> as/js/php/java/haxe";
text.x = 20;
text.y = 200;
data.addChild( text );
var portrait: Image = new Image("e/ef/efdf/efdf7a2ec42a20113524729e4f5eabff797e7220");
portrait.scaleX = portrait.scaleY = 0.3;
portrait.x = 20;
portrait.y = 20;
data.addChild( portrait );
var osaka: Location = new Location(34.717588,135.455776);
var map: TweenMap = new TweenMap(550,600,false,new MicrosoftAerialMapProvider());
map.filters =[new BlurFilter(1, 1) ];
map.setCenter(new Location(47.474718,9.760681));
map.setZoom(6);
map.x = 250;
map.putMarker(map.getCenter(), new MapMarker( "Bregenz, Austria" ));
map.putMarker(osaka, new MapMarker( "Osaka, Japan" ));
data.addChild( map );
return inone(
showDO(data),
new MoveMap(map,map.getCenter(),osaka),
hideDO(data)
);
}
function page1(): ActionList {
var out: MyCenterTextField = new MyCenterTextField();
return inone(
showDO(out),
changeText(out, "trace()" ),
changeText(out, "CONFIG::debugging { trace(\"...\"); }"),
changeText(out, "特別のふーレムワーク?"),
hideDO(out)
);
}
function size(): ActionList {
var graph: Graph = new Graph( [0x99ACAC, 0xACFF00] )
graph.width = 500;
graph.labelSpace = 200;
graph.x = 20;
graph.y = 60;
const kB: String = "kB";
var swiz: LineData = new LineData("Swiz Framework",kB);
var prog: LineData = new LineData("Progression",kB);
var spice: LineData = new LineData("Spicelib",kB);
var flex: LineData = new LineData("Flex",kB);
var asap: LineData = new LineData("ASAP",kB);
var mate: LineData = new LineData("Mate",kB);
var pbe: LineData = new LineData("Push Button Engine",kB);
var as3commons: LineData = new LineData("as3commons",kB);
var athropod: LineData = new LineData("Athropod",kB);
var md3: LineData = new LineData("MonsterDebugger 3",kB);
var md2: LineData = new LineData("MonsterDebugger 2",kB);
var thunderbolt: LineData = new LineData("Thunderbolt",kB);
var flashConsole: LineData = new LineData("Flash Console",kB);
return inone(
inone(
changeVisibility( graph, true ),
showAndAdd( graph, pbe, 72.7 )
),
showAndAdd( graph, flashConsole, 48.3 ),
showAndAdd( graph, flex, 17.6 ),
showAndAdd( graph, md3, 15.8 ),
showAndAdd( graph, md2, 7.99 ),
showAndAdd( graph, prog, 6.23 ),
showAndAdd( graph, thunderbolt, 3.41 ),
showAndAdd( graph, athropod, 2.14 ),
inone(
showAndAdd( graph, as3commons, 2.08 ),
showAndAdd( graph, swiz, 2.07 )
),
showAndAdd( graph, spice, 1.96 ),
showAndAdd( graph, asap, 1.48 ),
inone(
changeVisibility( graph, false )
)
);
}
function performance(): ActionList {
var graph: Graph = new Graph( [0x99ACAC, 0xACFF00] )
graph.width = 500;
graph.labelSpace = 150;
graph.x = 20;
graph.y = 60;
const ms:String = "ms";
var swiz: LineData = new LineData("Swiz Framework",ms);
var prog: LineData = new LineData("Progression",ms);
var spice: LineData = new LineData("Spicelib",ms);
var flex: LineData = new LineData("Flex",ms);
var asap: LineData = new LineData("ASAP",ms);
var mate: LineData = new LineData("Mate",ms);
var pbe: LineData = new LineData("Push Button Engine",ms);
var as3commons: LineData = new LineData("as3commons",ms);
return inone(
inone(
changeVisibility( graph, true ),
showAndAdd( graph, spice,380 )
),
showAndAdd( graph, asap,132 ),
showAndAdd( graph, mate,97 ),
inone(
showAndAdd( graph, prog, 75 ),
showAndAdd( graph, swiz, 71 )
),
showAndAdd( graph, pbe,21 ),
showAndAdd( graph, flex,18 ),
showAndAdd( graph, as3commons,4 ),
addValue( prog, 3595 ),
inone(
addValue( pbe, 257 ),
addValue( mate, 235 ),
addValue( swiz, 224 )
),
addValue( flex, 205 ),
addValue( asap, 72 ),
addValue( spice, 62 ),
addValue( as3commons, 12 ),
changeVisibility( graph, false )
);
}
interface IAction {
function go(p:Presentation):void;
function revert(p:Presentation):void;
}
function showAndAdd( graph: Graph, line:LineData, value: Number ):ActionList {
return inone(
show(graph,line),
addValue(line,value)
);
}
function showDO( object:DisplayObject, inContainer: DisplayObjectContainer = null ):ChangeVisibility {
return changeVisibility( object, true, inContainer );
}
function hideDO( object:DisplayObject, inContainer: DisplayObjectContainer = null ):ChangeVisibility {
return changeVisibility( object, false, inContainer );
}
function changeVisibility( object: DisplayObject, visible:Boolean, inContainer: DisplayObjectContainer = null ): ChangeVisibility {
return new ChangeVisibility( object, visible, inContainer );
}
function addValue( line: LineData, value: Number ): AddValue {
return new AddValue( line, value );
}
function show( graph: Graph, line:LineData ):ShowLine {
return new ShowLine( graph, line );
}
function inone( ...actions ):ActionList {
return new ActionList(actions);
}
function changeText(tf:MyTextField, text:String):ChangeText {
return new ChangeText(tf,text);
}
class ChangeText implements IAction {
private var _tf:MyTextField;
private var _oldText:String;
private var _newText:String;
public function ChangeText(tf:MyTextField, text:String) {
_tf = tf;
_newText = text;
}
public function go(p:Presentation):void {
_oldText = _tf.text;
_tf.text = _newText;
}
public function revert(p:Presentation):void {
_tf.text = _oldText;
}
}
class ChangeVisibility implements IAction {
private var _object: DisplayObject;
private var _visible: Boolean;
private var _container: DisplayObjectContainer;
public function ChangeVisibility( object:DisplayObject, visible:Boolean, inContainer: DisplayObjectContainer = null ) {
_object = object;
_container = inContainer;
_visible = visible;
}
public function go(p:Presentation):void {
if( _visible ) {
( _container || p.container ).addChild( _object );
} else {
( _container || p.container ).removeChild( _object );
}
}
public function revert(p:Presentation):void {
if( _visible ) {
( _container || p.container ).removeChild( _object );
} else {
( _container || p.container ).addChild( _object );
}
}
}
class MoveMap implements IAction {
private var _from: Location;
private var _map: TweenMap;
private var _to: Location;
public function MoveMap( map:TweenMap, from: Location, to:Location ) {
_map = map;
_from = from;
_to = to;
}
public function go(p:Presentation):void {
_map.tweenTo(_to,2);
}
public function revert(p:Presentation):void {
_map.tweenTo(_from,2);
}
}
class ActionList implements IAction {
public var actions: Array;
private var _next: int = 0;
private var _prev: int = -1;
public function ActionList(actions:Array) {
this.actions = actions;
}
public function get done(): Boolean {
return actions.length == _next;
}
public function get started(): Boolean {
return _prev != -1;
}
public function next(p:Presentation,depth:int):void {
if( !done ) {
var current:IAction = actions[_next];
_prev = _next;
if( current is ActionList ) {
var currentList: ActionList = ActionList(current);
if( depth == 0 ) {
currentList.go(p);
++_next;
} else {
currentList.next(p, depth-1);
if( currentList.done ) {
++_next;
}
}
} else {
current.go(p);
++_next;
}
}
}
public function prev(p:Presentation,depth:int):void {
if( started ) {
var current:IAction = actions[_prev];
_next = _prev;
if( current is ActionList ) {
var currentList: ActionList = ActionList(current);
if( depth == 0 ) {
currentList.revert(p);
--_prev;
} else {
currentList.prev(p, depth-1);
if( !currentList.started ) {
--_prev;
}
}
} else {
current.revert(p);
--_prev;
}
}
}
public function go(p:Presentation):void {
for( var i: int = _next; i<actions.length; ++i ){
IAction( actions[i] ).go(p);
}
_next = actions.length;
_prev = _next-1;
}
public function revert(p:Presentation):void {
for( var i: int = _prev; i>=0; --i ){
IAction( actions[i] ).revert(p);
}
_prev = -1;
_next = 0;
}
}
class ShowLine implements IAction {
private var _line:LineData;
private var _graph:Graph;
public function ShowLine(graph:Graph, line:LineData):void {
_graph = graph;
_line = line;
}
public function go(p:Presentation):void {
_graph.lines.push(_line);
}
public function revert(p:Presentation):void {
_graph.lines.splice(_graph.lines.indexOf(_line),1);
}
}
class AddValue implements IAction {
private var _line:LineData;
private var _value:Number;
public function AddValue(line:LineData, value:Number):void {
_line = line;
_value = value;
}
public function go(p:Presentation):void {
_line.values.push(_value);
}
public function revert(p:Presentation):void {
_line.values.pop();
}
}
class LineData {
public var name: String;
public var values: Array = [];
private var _postfix: String;
function LineData( name: String, postfix: String ) {
this.name = name;
_postfix = postfix;
}
public function format( value: Number ): String {
return Math.round(value*10)/10+_postfix;
}
public function get max(): Number {
var size: Number = 0;
for( var i: int = 0; i<values.length; ++i) {
size += values[i];
}
return size;
}
}
class Graph extends Sprite {
public static const LINE_OFFSET: Number = 35;
private var _assoc: Dictionary /* LineData -> LineWrapper */ = new Dictionary(false);
public var lines: Array /* LineData */ = [];
private var _colors: Array = [];
private var _visibleLines: Array /* LineWrapper */ = [];
private var _width: Number = 100;
private var _space: Number = 100;
function Graph(colors: Array) {
_colors = colors;
addEventListener( Event.ENTER_FRAME, render, false, 0, true );
}
override public function set width( width: Number ): void {
_width = width;
for each( var line: LineWrapper in _visibleLines ){
line.width = width;
}
}
public function set labelSpace(space: Number): void {
_space = space;
for each( var line: LineWrapper in _visibleLines ){
line.labelSpace = space;
}
}
public function render( e: Event ): void {
var data: LineData;
var line: LineWrapper;
var i:int;
var neccessary: Dictionary = new Dictionary();
for( i = 0; i<lines.length; ++i ) {
data = lines[i];
line = _assoc[data];
neccessary[line] = true;
if( !line ) {
addChild( line = new LineWrapper( data, _colors ) );
line.width = _width;
line.labelSpace = _space;
_assoc[data] = line;
_visibleLines.push( line );
}
line.alive = true;
}
lines.sort(function(lineA:LineData, lineB:LineData):int {
if(lineA.max == lineB.max ) {
return 0;
}
return lineA.max > lineB.max ? 1 : -1;
});
for( i = _visibleLines.length-1; i>=0; --i ) {
line = _visibleLines[i];
if(!neccessary[line]) {
line.alive = false;
if( !line.moving ) {
_visibleLines.splice( i, 1 );
delete _assoc[line.data];
removeChild( line );
}
}
}
var yOff: Number = 0;
var max: Number = 0;
var labelSize: Number = 0;
for each( data in lines ) {
if( data.max > max ) {
max = data.max;
}
line = LineWrapper( _assoc[data] );
line._y = yOff;
yOff += LINE_OFFSET;
}
if( max > 500 ) {
max = 500;
}
for each( line in _visibleLines ){
line.max = max;
}
}
}
class LineWrapper extends Sprite {
public static const SPEED: Number = 0.2;
public var moving: Boolean;
public var _y: Number;
private var _line: Line;
private var _alive: Boolean = false;
private var _targetAlpha: Number;
private var _targetY: Number;
function LineWrapper( data: LineData, colors: Array ):void {
var line: Line = new Line( data, colors );
addChild( _line = line );
reset();
}
public function get data():LineData {
return _line.data;
}
public function reset(): void {
_line.y = -20;
_line.alpha = 0;
}
public function set alive( alive: Boolean ):void {
if( _alive != alive ) {
_alive = alive;
moving = true;
if( !_alive ) {
_targetAlpha = 0;
_targetY = 20;
} else {
_targetY = 0;
_targetAlpha = 1.0;
}
addEventListener( Event.ENTER_FRAME, render, false, 0, true );
}
}
override public function set width( width: Number ):void {
_line.width = width;
}
public function set max ( max: Number ):void {
_line.max = max;
}
public function set labelSpace( space: Number ):void {
_line.labelSpace = space;
}
public function render( e: Event ):void {
_line.y = _line.y + (_targetY-_line.y)*SPEED;
_line.alpha = _line.alpha + (_targetAlpha-_line.alpha)*SPEED;
y = y + (_y-y)* SPEED;
if( Math.abs( _targetY - _line.y) < 0.5
&& Math.abs( _targetAlpha - _line.alpha) < 0.01
&& Math.abs( _y - y ) < 0.5 ) {
_line.y = _targetY;
_line.alpha = _targetAlpha;
y = _y;
moving = false;
removeEventListener( Event.ENTER_FRAME, render );
}
}
}
class Line extends Sprite {
public static const ANI_SPEED: Number = 0.09;
public static const BAR_HEIGHT: Number = 26;
public var max: Number;
public var colors: Array;
public var data: LineData;
private var _width: Number = 200;
private var _text: MyTextField;
private var _label: MyTextField;
private var _bar: Shape;
private var _currentValues: Array = [];
private var _displayedValue: Number = 0;
private var _textTween: IObjectTween;
private var _space: Number;
public function Line( data: LineData, colors: Array ) {
addChild( _bar = new Shape() );
addChild( _text = new MyTextField() );
addChild( _label = new MyTextField() );
this.data = data;
this.colors = colors;
addEventListener( Event.ENTER_FRAME, render, false, 0, true );
}
override public function set width( width: Number ) :void {
_width = width;
}
override public function get width(): Number {
return _width;
}
public function set labelSpace( space: Number ):void {
_text.x += space - _label.width;
_label.width = space;
_bar.x = space;
_space = space;
}
public function render( e: Event ):void {
_bar.graphics.clear();
if( data ) {
if( _label.text != data.name ) {
_label.text = data.name || "";
}
while( data.values.length > _currentValues.length ) {
_currentValues.push(0);
}
var xOff: Number = 0;
var lastValue: Number = 0;
var lastSize: Number = 0;
for( var i:int = 0; i<_currentValues.length;++i ) {
_bar.graphics.beginFill( colors[i] || 0 );
var current: Number = _currentValues[i];
var target: Number = data.values[i];
if( isNaN(target) ) {
target = 0;
}
if( target > _width ) {
target = _width;
}
if( current != target ) {
current = current + (target-current)*ANI_SPEED;
if( Math.abs( current - target ) < 0.001 ) {
current = target;
}
_currentValues[i] = current;
} else if( Math.abs( current ) > 3 && i == _currentValues.length-1 && _currentValues.length > data.values.length ) {
_currentValues.pop();
break;
}
var size: int = current / max * width;
if( size < 1 ) size = 1;
_bar.graphics.drawRect( xOff, 0, size, BAR_HEIGHT );
xOff += size;
if( current != 0 ) {
lastSize = size;
lastValue = current;
}
}
_text.text = data.format(lastValue);
var targetPos: Number;
if( lastSize > _text.width + 5 ) {
targetPos = xOff - 5 - _text.textWidth;
} else {
targetPos = xOff + 5;
}
targetPos += _space;
_text.x = Number( _text.x + (targetPos-_text.x)*ANI_SPEED*2 );
}
}
}
class Image extends Bitmap {
private var _loader: Loader = new Loader();
function Image(id: String) {
_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, useImage );
_loader.load( new URLRequest( "http://assets.wonderfl.net/images/related_images/" + id ), new LoaderContext(true) );
smoothing = true;
}
private function useImage( e: Event):void {
bitmapData = Bitmap( _loader.content ).bitmapData;
}
}
class MyTextField extends Sprite {
private var latin: TextField;
private var jap: TextField;
private var _current: TextField;
function MyTextField(size:int=18,color:int=0) {
latin = new TextField();
latin.defaultTextFormat = new TextFormat("Meta Medium",size,color);
latin.selectable = false;
latin.antiAliasType = AntiAliasType.ADVANCED;
jap = new TextField();
jap.defaultTextFormat = new TextFormat("MS Mincho",size,color);
jap.selectable = false;
jap.antiAliasType = AntiAliasType.ADVANCED;
}
public function setText(text:String):MyTextField {
this.text = text;
return this;
}
public function set text( text: String ):void {
const l: int = text.length;
var hasjap: Boolean = false;
for( var i: int = 0; i<l; ++i ){
if( text.charCodeAt(i) > 500 ) {
hasjap = true;
break;
}
}
var textField: TextField;
var removeField: TextField;
if( hasjap ) {
textField = jap;
removeField = latin;
} else {
textField = latin;
removeField = jap;
}
textField.text = text;
if( !textField.parent ) {
addChild( textField );
_current = textField;
}
if( removeField.parent ) {
removeChild( removeField );
}
textField.autoSize = TextFieldAutoSize.LEFT;
}
override public function set width(width:Number):void {
jap.width = width;
latin.width = width;
}
override public function set height(height:Number):void {
jap.height = height;
latin.height = height;
}
override public function get width(): Number {
return _current ? _current.width : 0;
}
override public function get height(): Number {
return _current ? _current.height : 0;
}
public function get textWidth(): Number {
return _current ? _current.textWidth : 0;
}
public function get textHeight(): Number {
return _current ? _current.textHeight : 0;
}
public function get text(): String {
return _current ? _current.text : "";
}
public function set htmlText( text: String ): void {
const l: int = text.length;
var hasjap: Boolean = false;
for( var i: int = 0; i<l; ++i ){
if( text.charCodeAt(i) > 500 ) {
hasjap = true;
break;
}
}
var textField: TextField;
var removeField: TextField;
if( hasjap ) {
textField = jap;
removeField = latin;
} else {
textField = latin;
removeField = jap;
}
textField.htmlText = text;
textField.autoSize = TextFieldAutoSize.LEFT;
if( !textField.parent ) {
addChild( textField );
_current = textField;
}
if( removeField.parent ) {
removeChild( removeField );
}
}
}
class MyCenterTextField extends MyTextField {
override public function set text( text: String ):void {
super.text = text;
x = PRES_WIDTH/2.0 - textWidth/2;
y = PRES_HEIGHT/2.0 - textHeight/2;
}
}
class MapMarker extends Sprite {
function MapMarker( text: String ) {
var shape: Shape = new Shape();
var width: Number = 25;
var height: Number = 3;
shape.graphics.beginFill( 0xFFFFFF );
shape.graphics.drawRect( -width/2, -height/2, width, height );
shape.graphics.drawRect( -height/2, -width/2, height, width/2-height/2 );
shape.graphics.drawRect( -height/2, height/2, height, width/2-height/2 );
shape.rotation = 45;
addChild( shape );
var tf: MyTextField = new MyTextField( 22, 0xFFFFFF).setText( text );
filters = [ new GlowFilter(0) ];
tf.x = 15;
tf.y = -14;
addChild( tf );
}
}
var trace:Function= Wonderfl.log;