Video like controls with BetweenAS3
forked from matsumos's BetweenAS3でビデオ的なコントロールを試してみる
* forked from rsakane's BetweenAS3.serial(), reverse()の練習
* トゥイーンのサンプルとしてお借りしました
* http://wonderfl.kayac.com/code/f74dab73e022acb75c05a84fa73514322e85ae96
*
* よりBetweenAS3の素晴らしさをおわかりいただくために、シークバーをドラッグできるようにしました。
/**
* forked from matsumos's BetweenAS3でビデオ的なコントロールを試してみる
* forked from rsakane's BetweenAS3.serial(), reverse()の練習
* トゥイーンのサンプルとしてお借りしました
* http://wonderfl.kayac.com/code/f74dab73e022acb75c05a84fa73514322e85ae96
*
* よりBetweenAS3の素晴らしさをおわかりいただくために、シークバーをドラッグできるようにしました。
*/
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.net.URLRequest;
import flash.system.Security;
import flash.system.LoaderContext;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.easing.*;
import org.libspark.betweenas3.tweens.ITween;
[SWF(width="465", height="465", frameRate="30", backgroundColor="0x000000")]
public class Main extends Sprite
{
private const SIZE:int = 30;
public function Main()
{
Security.loadPolicyFile("http://farm3.static.flickr.com/crossdomain.xml");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, init);
loader.load(new URLRequest("http://farm3.static.flickr.com/2315/3530328081_38167d20dd.jpg"), new LoaderContext(true));
}
private function init(event:Event):void
{
var loader:Loader = event.currentTarget.loader;
var bd:BitmapData = new BitmapData(loader.width, loader.height, false);
bd.draw(loader);
var delay:Number = 0.0;
var count:Number = 0.0;
var tweens:Array = [];
for (var y:int = 0; y < bd.height; y += SIZE)
{
count += 0.1;
delay = count;
for (var x:int = 0; x < bd.width; x += SIZE)
{
var yy:int = (bd.height - 1 - y >= SIZE) ? SIZE : bd.height - 1 - y;
var xx:int = (bd.width - 1 - x >= SIZE) ? SIZE : bd.width - 1 - x;
if (xx <= 0 || yy <= 0) continue;
var bitmap:Bitmap = new Bitmap(clipping(bd, x, y, xx, yy, 1.0));
bitmap.x = x;
bitmap.y = y;
addChild(bitmap);
tweens.push(BetweenAS3.delay(BetweenAS3.tween(bitmap, { y:bitmap.y - 60, alpha:0} , null, 1.7, Bounce.easeInOut),delay) as ITween);
delay += 0.1;
}
}
var t:ITween = BetweenAS3.parallel.apply(null, tweens);;
var ctrlpanel:ControlPanel = new ControlPanel(t);
ctrlpanel.y = 400;
addChild(ctrlpanel);
}
private function clipping(bd:BitmapData, x1:int, y1:int, width:int, height:int, scale:Number):BitmapData
{
var matrix:Matrix = new Matrix();
matrix.translate( -x1, -y1);
matrix.scale(scale, scale);
var destbd:BitmapData = new BitmapData(width * scale, height * scale, false);
destbd.draw(bd, matrix);
return destbd;
}
}
}
import flash.display.*;
import flash.text.*;
import flash.events.*;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.easing.*;
import org.libspark.betweenas3.tweens.ITween;
import org.libspark.betweenas3.events.TweenEvent;
class ControlPanel extends Sprite
{
private var playBtn:Sprite;
private var fastPlayBtn:Sprite;
private var reverseBtn:Sprite;
private var fastReverseBtn:Sprite;
private var pauseBtn:Sprite;
private var seekBar:Sprite;
private var seekBase:Sprite;
private var posText:TextField;
private var durText:TextField;
private var _seekpos:Number;
private var _t:ITween;
private var naturalT:ITween;
private var reversed:Boolean = false;
private var scaled:Boolean = false;
private var tp:Number;
private const SCALE_NUM:Number = 3.5;
private var _playinged:Boolean = false;
public function ControlPanel(t:ITween)
{
var bg:Sprite = new Sprite();
bg.graphics.beginFill(0xFFFFFF);
bg.graphics.drawRect(0, 0, 465, 90);
bg.graphics.endFill();
bg.y = -25;
addChild(bg);
_t = t;
naturalT = t;
var timeText:TextField = new TextField();
timeText.x = 10;
timeText.y = -5;
timeText.text = "TIME";
addChild(timeText);
posText = new TextField();
posText.autoSize = TextFieldAutoSize.LEFT;
posText.x = 10;
posText.y = 10;
addChild(posText);
durText = new TextField();
durText.autoSize = TextFieldAutoSize.LEFT;
durText.x = 50;
durText.y = 10;
addChild(durText);
playBtn = new Sprite();
playBtn.graphics.beginFill(0xFF00FF);
playBtn.graphics.moveTo(0, 0);
playBtn.graphics.lineTo(30, 20);
playBtn.graphics.lineTo(0, 40);
playBtn.graphics.endFill();
playBtn.buttonMode = true;
addChild(playBtn);
playBtn.x = 305;
var playText:TextField = new TextField();
playText.text = "Play";
addChild(playText);
playText.x = playBtn.x;
playText.y = playBtn.y + 40;
fastPlayBtn = new Sprite();
fastPlayBtn.graphics.beginFill(0xFF00FF);
fastPlayBtn.graphics.moveTo(0, 0);
fastPlayBtn.graphics.lineTo(20, 20);
fastPlayBtn.graphics.lineTo(0, 40);
fastPlayBtn.graphics.moveTo(20, 0);
fastPlayBtn.graphics.lineTo(40, 20);
fastPlayBtn.graphics.lineTo(20, 40);
fastPlayBtn.graphics.endFill();
fastPlayBtn.buttonMode = true;
addChild(fastPlayBtn);
fastPlayBtn.x = 350;
var fastPlayText:TextField = new TextField();
fastPlayText.text = "FastPlay";
addChild(fastPlayText);
fastPlayText.x = fastPlayBtn.x;
fastPlayText.y = fastPlayBtn.y+40;
reverseBtn = new Sprite();
reverseBtn.graphics.beginFill(0xFF00FF);
reverseBtn.graphics.moveTo(40, 0);
reverseBtn.graphics.lineTo(10, 20);
reverseBtn.graphics.lineTo(40, 40);
reverseBtn.graphics.endFill();
reverseBtn.buttonMode = true;
addChild(reverseBtn);
reverseBtn.x = 195;
var reverseText:TextField = new TextField();
reverseText.text = "Reverse";
addChild(reverseText);
reverseText.x = reverseBtn.x;
reverseText.y = reverseBtn.y + 40;
fastReverseBtn = new Sprite();
fastReverseBtn.graphics.beginFill(0xFF00FF);
fastReverseBtn.graphics.moveTo(20, 0);
fastReverseBtn.graphics.lineTo(00, 20);
fastReverseBtn.graphics.lineTo(20, 40);
fastReverseBtn.graphics.moveTo(40, 0);
fastReverseBtn.graphics.lineTo(20, 20);
fastReverseBtn.graphics.lineTo(40, 40);
fastReverseBtn.graphics.endFill();
fastReverseBtn.buttonMode = true;
addChild(fastReverseBtn);
fastReverseBtn.x = 150;
var fastReverseText:TextField = new TextField();
fastReverseText.text = "FastRev";
addChild(fastReverseText);
fastReverseText.x = fastReverseBtn.x;
fastReverseText.y = fastReverseBtn.y+40;
pauseBtn = new Sprite();
pauseBtn.graphics.beginFill(0xFF00FF);
pauseBtn.graphics.moveTo(0, 0);
pauseBtn.graphics.lineTo(15, 0);
pauseBtn.graphics.lineTo(15, 40);
pauseBtn.graphics.lineTo(0, 40);
pauseBtn.graphics.moveTo(25, 0);
pauseBtn.graphics.lineTo(40, 0);
pauseBtn.graphics.lineTo(40, 40);
pauseBtn.graphics.lineTo(25, 40);
pauseBtn.graphics.endFill();
pauseBtn.buttonMode = true;
addChild(pauseBtn);
pauseBtn.x = 250;
var pauseText:TextField = new TextField();
pauseText.text = "Pause";
addChild(pauseText);
pauseText.x = pauseBtn.x;
pauseText.y = pauseBtn.y+40;
seekBase = new Sprite();
seekBase.graphics.beginFill(0xCCCCCC);
seekBase.graphics.drawRect(0,0,400,10);
seekBase.x = 20;
seekBase.y = -20;
seekBase.buttonMode = true;
addChild(seekBase);
seekBar = new Sprite();
seekBar.graphics.beginFill(0xFF00FF);
seekBar.graphics.drawRect(0,0,400,10);
seekBar.x = 20;
seekBar.y = -20;
seekBar.mouseEnabled = false;
addChild(seekBar);
playBtn.addEventListener(MouseEvent.MOUSE_DOWN,
function():void {
if (!reversed && !scaled && _t.isPlaying) {
return;
}
if (_t.isPlaying) {
_t.stop();
}
if (!reversed){
tp = _t.position;
}
else {
tp = _t.duration - _t.position;
}
if (scaled){
tp = tp * SCALE_NUM;
}
_t = naturalT;
reversed = false;
scaled = false;
_t.gotoAndPlay(tp);
}
);
fastPlayBtn.addEventListener(MouseEvent.MOUSE_DOWN,
function():void {
if (_t.isPlaying) {
_t.stop();
}
if (!reversed){
tp = _t.position;
}
else {
tp = _t.duration - _t.position;
}
if (!scaled){
tp = tp / SCALE_NUM;
}
_t = BetweenAS3.scale(naturalT, 1 / SCALE_NUM);
reversed = false;
scaled = true;
_t.gotoAndPlay(tp);
}
);
reverseBtn.addEventListener(MouseEvent.MOUSE_DOWN,
function():void {
if (_t.isPlaying) {
_t.stop();
}
if (!reversed){
tp = _t.duration - _t.position;
}
else {
tp = _t.position;
}
if (scaled){
tp = tp * SCALE_NUM;
}
_t = BetweenAS3.reverse(naturalT);
reversed = true;
scaled = false;
_t.gotoAndPlay(tp);
}
);
fastReverseBtn.addEventListener(MouseEvent.MOUSE_DOWN,
function():void {
if (_t.isPlaying) {
_t.stop();
}
if (!reversed){
tp = _t.duration - _t.position;
}
else {
tp = _t.position;
}
if (!scaled){
tp = tp / SCALE_NUM;
}
_t = BetweenAS3.reverse(BetweenAS3.scale(naturalT, 1 / SCALE_NUM));
reversed = true;
scaled = true;
_t.gotoAndPlay(tp);
}
);
pauseBtn.addEventListener(MouseEvent.MOUSE_DOWN,
function():void {
if (_t.isPlaying) {
_t.stop();
}
}
);
seekBase.addEventListener(MouseEvent.MOUSE_DOWN,
function():void {
if (_t.isPlaying) {
_t.stop();
_playinged = true;
}
addEventListener(Event.ENTER_FRAME,startDragSeek);
stage.addEventListener(MouseEvent.MOUSE_UP,stopDragSeek);
}
);
addEventListener(Event.ENTER_FRAME,
function():void {
var tpos:Number;
if (!reversed) {
tpos = _t.position;
}
else {
tpos = _t.duration - _t.position;
}
if (scaled){
tpos = tpos * SCALE_NUM;
}
posText.text = tpos.toString().substr(0, 4);
seekBar.width = tpos / naturalT.duration * seekBase.width;
}
);
durText.text = _t.duration.toString().substr(0, 4);
}
private function startDragSeek(e:Event):void
{
var mousex:Number = seekBase.mouseX < 0 ? 0 : seekBase.mouseX < seekBase.width ? seekBase.mouseX : seekBase.width;
_seekpos = mousex / seekBase.width * naturalT.duration;
if (scaled){
_seekpos = _seekpos/ SCALE_NUM;
}
if (reversed) {
_seekpos = _t.duration - _seekpos;
}
_t.gotoAndStop(_seekpos);
}
private function stopDragSeek(e:Event):void
{
removeEventListener(Event.ENTER_FRAME,startDragSeek);
stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragSeek);
_t.gotoAndStop(_seekpos);
}
}