flash on 2011-1-19
/**
* Copyright NOB_Q ( http://wonderfl.net/user/NOB_Q )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/sa5W
*/
package {
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
var body:Body = new Body();
body.addIcon(new AppIcon("safari"));
body.addIcon(new AppIcon("2"));
body.addIcon(new AppIcon("3"));
body.addIcon(new AppIcon("4"));
body.addIcon(new AppIcon("5"));
body.addIcon(new AppIcon("6"));
body.addIcon(new AppIcon("7"));
body.addIcon(new AppIcon("8"));
body.addIcon(new AppIcon("9"));
body.addIcon(new AppIcon("10"));
body.addIcon(new AppIcon("11"));
body.addIcon(new AppIcon("12"));
body.addIcon(new AppIcon("13"));
body.addIcon(new AppIcon("14"));
body.addIcon(new AppIcon("15"));
body.addIcon(new AppIcon("16"));
addChild(body);
}
}
}
import flash.display.BitmapData;
import flash.display.GradientType;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.easing.Quad;
import org.libspark.betweenas3.tweens.ITween;
internal class Body extends Sprite {
private var _screen:Screen = null;
private var _homeButton:HomeButton = null;
public function Body()
{
_screen = new Screen();
_screen.x = 10;
_screen.y = 50;
addChild(_screen);
_homeButton = new HomeButton();
_homeButton.x = 86;
_homeButton.y = 360;
_homeButton.addEventListener(MouseEvent.CLICK, _screen.applicationEnd);
_homeButton.buttonMode = true;
addChild(_homeButton);
drawBackGround();
}
private function drawBackGround():void
{
var g:Graphics = this.graphics;
g.beginFill(0x333333);
g.drawRoundRect(0, 0, 220, 410, 60, 60);
g.endFill();
}
public function addIcon(appIcon:AppIcon):void
{
_screen.addIcon(appIcon);
}
}
internal class HomeButton extends Sprite {
public function HomeButton()
{
var g:Graphics = this.graphics;
g.beginFill(0x000000);
g.drawCircle(20, 20, 20);
g.beginFill(0xFFFFFF);
g.drawRect(12, 13, 15, 15);
g.beginFill(0x000000);
g.drawRect(13, 14, 13, 13);
g.endFill();
}
}
internal class Screen extends Sprite {
public static const WIDTH:int = 200;
public static const HEIGHT:int = 300;
private var _appIcons:Vector.<AppIcon> = null;
private var _applications:Vector.<Application> = null;
private var _isDrag:Boolean = false;
private var _dragFrom:Point = new Point();
private var _appRunLock:Boolean = false;
public function Screen()
{
_appIcons = new Vector.<AppIcon>();
_applications = new Vector.<Application>();
drawBackGround();
addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
private function mouseDownHandler(event:MouseEvent):void
{
_isDrag = true;
_dragFrom.x = this.mouseX;
_dragFrom.y = this.mouseY;
}
private function mouseUpHandler(event:MouseEvent):void
{
_isDrag = false;
var tweens:Array = [];
var n:int = _appIcons.length;
for (var i:int = 0; i < n; i++)
{
var point:Point = getIconPos(i);
tweens.push(BetweenAS3.to(_appIcons[i], { x:point.x }, 0.3, Quad.easeOut));
tweens.push(BetweenAS3.to(_appIcons[i], { y:point.y }, 0.3, Quad.easeOut));
}
var tween:ITween = BetweenAS3.parallelTweens(tweens);
tween.onComplete = function():void
{
_appRunLock = false;
}
tween.play();
}
private function mouseMoveHandler(event:MouseEvent):void
{
if (_isDrag)
{
var n:int = _appIcons.length;
for (var i:int = 0; i < n; i++)
{
_appIcons[i].x = getIconPos(i).x + this.mouseX - _dragFrom.x;
}
_appRunLock = true;
}
}
private function drawBackGround():void
{
var matrix:Matrix = new Matrix();
matrix.createGradientBox(200, 300, Math.PI / 2);
var g:Graphics = this.graphics;
g.beginGradientFill(GradientType.LINEAR, [0x000000, 0xAAAAAA], [1, 1], [0x00, 0xFF], matrix);
g.drawRect(0, 0, WIDTH, HEIGHT);
g.endFill();
}
public function addIcon(app:AppIcon):void
{
var n:int = _appIcons.length;
var point:Point = getIconPos(n);
app.x = point.x;
app.y = point.y;
app.addEventListener(MouseEvent.CLICK, applicationStart);
_appIcons[n] = app;
addChild(app);
}
private function getIconPos(index:int):Point
{
var ret:Point = new Point();
ret.x = index % 4 * 47 + 10 + int(index / 16) * WIDTH;
ret.y = int((index % 16) / 4) * 55 + 10;
return ret;
}
private function applicationStart(event:MouseEvent):void
{
if (_appRunLock)
{
return;
}
var application:Application = _appIcons[0].application;
application.scaleX = 0;
application.scaleY = 0;
application.run();
addChild(application);
var tweens:Array = [];
tweens.push(BetweenAS3.to(application, { scaleX:1 }, 0.3));
tweens.push(BetweenAS3.to(application, { scaleY:1 }, 0.3));
var tween:ITween = BetweenAS3.parallelTweens(tweens);
tween.onUpdate = function():void
{
application.x = WIDTH / 2 - application.width / 2;
application.y = HEIGHT / 2 - application.height / 2;
}
tween.onComplete = function():void
{
application.x = 0;
application.y = 0;
}
tween.play();
_applications[_applications.length] = application;
}
public function applicationEnd(event:MouseEvent):void
{
if (0 < _applications.length)
{
var application:Application = _applications.pop();
var tweens:Array = [];
tweens.push(BetweenAS3.to(application, { scaleX:0 }, 0.3));
tweens.push(BetweenAS3.to(application, { scaleY:0 }, 0.3));
var tween:ITween = BetweenAS3.parallelTweens(tweens);
tween.onUpdate = function():void
{
application.x = WIDTH / 2 - application.width / 2;
application.y = HEIGHT / 2 - application.height / 2;
}
tween.onComplete = function():void
{
application.quit();
removeChild(application);
}
tween.play();
tweens = [];
var n:int = _appIcons.length;
for (var i:int = 0; i < n; i++)
{
var point:Point = getIconPos(i);
_appIcons[i].x = point.x;
_appIcons[i].y = point.y;
tweens.push(BetweenAS3.to(_appIcons[i], { x:_appIcons[i].x }, 0.3));
tweens.push(BetweenAS3.to(_appIcons[i], { y:_appIcons[i].y }, 0.3));
switch (i)
{
case 0:
case 1:
case 4:
case 5:
{
_appIcons[i].x -= 100;
_appIcons[i].y -= 100;
break;
}
case 2:
case 3:
case 6:
case 7:
{
_appIcons[i].x += 100;
_appIcons[i].y -= 100;
break;
}
case 8:
case 9:
case 12:
case 13:
{
_appIcons[i].x -= 100;
_appIcons[i].y += 100;
break;
}
case 10:
case 11:
case 14:
case 15:
{
_appIcons[i].x += 100;
_appIcons[i].y += 100;
break;
}
}
}
BetweenAS3.parallelTweens(tweens).play();
}
}
}
internal class AppIcon extends Sprite {
private var _label:TextField = null;
private var _application:Application = null;
public function AppIcon(name:String)
{
_label = new TextField();
_label.defaultTextFormat = new TextFormat("ゴシック", null, 0xCCCCCC, null, null, null, null, null, TextFormatAlign.CENTER);
_label.text = name;
_label.x = -7;
_label.y = 34;
_label.width = 55;
_label.height = 15;
_label.selectable = false;
addChild(_label);
drawBackGround();
_application = new Application();
buttonMode = true;
}
private function drawBackGround():void
{
var g:Graphics = this.graphics;
g.beginFill(0x555555);
g.drawRoundRect(0, 0, 37, 37, 10, 10);
g.endFill();
}
public function get application():Application
{
return _application;
}
}
internal class Application extends Sprite {
public function Application()
{
}
public function run():void
{
drawBackGround();
}
public function quit():void
{
this.graphics.clear();
}
private function drawBackGround():void
{
var g:Graphics = this.graphics;
g.beginFill(0xFFFFFF);
g.drawRect(0, 0, Screen.WIDTH, Screen.HEIGHT);
g.endFill();
}
}