スライドショー
////////////////////////////////////////////////////////////////////////////////
// スライドショー
//
// [AS3.0] スライドショーに挑戦! (15)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1388
////////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/v6Io
*/
////////////////////////////////////////////////////////////////////////////////
// スライドショー
//
// [AS3.0] スライドショーに挑戦! (15)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1388
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
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 slideshow:SlideShow;
private var controller:Sprite;
private var prevBtn:Btn;
private var nextBtn:Btn;
private var base:Sprite;
private var txtloader:TextLoader;
private static var basePath:String = "http://www.project-nya.jp/images/flash/";
private static var dataPath:String = "slideshow325.xml";
private static var max:uint;
private var loaders:Array;
private var loaded:uint = 0;
private var contents:Array;
private var photos:Array;
private var thumbs:Array;
private var selectedId:int = 0;
private var timer:Timer;
private static var interval:uint = 2000;
public function Main() {
init();
}
private function init():void {
trace("init");
base = new Sprite();
addChild(base);
base.x = 232;
base.y = 212;
base.graphics.beginFill(0xEEEEEE);
base.graphics.drawRect(-160, -120, 320, 240);
base.graphics.endFill();
slideshow = new SlideShow();
addChild(slideshow);
slideshow.x = 232 - 160;
slideshow.y = 212 - 120;
slideshow.alpha = 0;
slideshow.visible = false;
controller = new Sprite();
addChild(controller);
controller.x = 232;
controller.y = 340;
controller.alpha = 0;
controller.visible = false;
prevBtn = new Btn(Btn.PREV);
controller.addChild(prevBtn);
prevBtn.x = - 180;
prevBtn.y = 22;
nextBtn = new Btn(Btn.NEXT);
controller.addChild(nextBtn);
nextBtn.x = 180;
nextBtn.y = 22;
//
txtloader = new TextLoader();
txtloader.addEventListener(TextLoader.COMPLETE, parse, false, 0, true);
txtloader.load(basePath + dataPath);
}
private function parse(evt:Event):void {
evt.target.removeEventListener(TextLoader.COMPLETE, parse);
var xml:XML = XML(evt.target.data);
trace("parse", xml);
max = xml.item.length();
loaders = new Array();
contents = new Array();
photos = new Array();
for (var n:uint = 0; n < max; n++) {
var thumbPath:String = xml.item[n].thumb;
var photoPath:String = xml.item[n].photo;
var _loader:ImageLoader = new ImageLoader();
_loader.id = n;
_loader.addEventListener(ImageLoader.COMPLETE, _complete, false, 0, true);
_loader.load(basePath + thumbPath, true);
loaders.push(_loader);
var loader:ImageLoader = new ImageLoader();
loader.id = n;
loader.addEventListener(ImageLoader.COMPLETE, complete, false, 0, true);
loader.load(basePath + photoPath, true);
loaders.push(loader);
}
}
private function _complete(evt:Event):void {
var _loader:ImageLoader = ImageLoader(evt.target);
_loader.removeEventListener(ImageLoader.COMPLETE, _complete);
contents[_loader.id] = _loader.content;
initialize();
}
private function complete(evt:Event):void {
var loader:ImageLoader = ImageLoader(evt.target);
loader.removeEventListener(ImageLoader.COMPLETE, complete);
photos[loader.id] = loader.content.bitmapData;
initialize();
}
private function initialize():void {
loaded ++;
if (loaded > max*2 - 1) {
setup();
}
}
private function setup():void {
thumbs = new Array();
for (var n:uint = 0; n < max; n++) {
var thumb:ActionMenu = new ActionMenu();
controller.addChild(thumb);
thumb.x = - 160 + 65*n;
thumb.y = 0;
thumb.id = n;
thumb.base.addChild(contents[n]);
thumb.addEventListener(MouseEvent.CLICK, click, false, 0, true);
thumbs.push(thumb);
}
slideshow.init(photos);
slideshow.addEventListener(Event.COMPLETE, update, false, 0, true);
prevBtn.addEventListener(MouseEvent.CLICK, prev, false, 0, true);
nextBtn.addEventListener(MouseEvent.CLICK, next, false, 0, true);
manage();
fade();
}
private function fade():void {
var itween:ITween = BetweenAS3.serial(
BetweenAS3.to(base, {alpha: 0, visible: 0}, 0.4, Linear.easeNone),
BetweenAS3.parallel(
BetweenAS3.to(slideshow, {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:TweenEvent):void {
evt.target.removeEventListener(TweenEvent.COMPLETE, faded);
removeChild(base);
startTimer();
}
private function prev(evt:MouseEvent):void {
transit(slideshow.selectedId - 1);
}
private function next(evt:MouseEvent):void {
transit(slideshow.selectedId + 1);
}
private function click(evt:MouseEvent):void {
var id:uint = evt.target.id;
var offset:int = id - slideshow.selectedId;
var distance:int = 0;
if (Math.abs(offset) < max/2) {
distance = offset;
} else {
if (offset > 0) {
distance = offset - max;
} else {
distance = offset + max;
}
}
transit(slideshow.selectedId + distance);
}
private function transit(id:int):void {
selectedId = id;
manage();
slideshow.transit(selectedId);
stopTimer();
}
private function manage():void {
var sid:int = (selectedId + max)%max;
for (var n:uint = 0; n < max; n++) {
var thumb:ActionMenu = thumbs[n];
if (n == sid) {
thumb.selected = true;
} else {
thumb.selected = false;
}
}
}
private function update(evt:Event):void {
startTimer();
}
private function startTimer():void {
timer = new Timer(interval, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, tick, false, 0, true);
timer.start();
}
private function stopTimer():void {
if (timer) {
timer.stop();
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, tick);
timer = null;
}
}
private function tick(evt:TimerEvent):void {
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, tick);
transit(slideshow.selectedId + 1);
}
}
}
//////////////////////////////////////////////////
// SlideShowクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.events.Event;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.filters.BlurFilter;
class SlideShow extends Sprite {
private var container:Sprite;
private static var max:uint;
private var photos:Array;
private static var xoffset:uint = 360;
public var selectedId:int = 0;
private var releaseId:int = 0;
private var direction:Boolean = false;
private var basePos:int = 0;
private var targetPos:Number = 0;
private static var deceleration:Number = 0.25;
private static var unit:uint = 1;
private static var limit:uint;
private static var offset:uint;
public function SlideShow() {
basePos = 0;
container = new Sprite();
addChild(container);
}
public function init(list:Array):void {
photos = list;
max = photos.length;
for (var n:uint = 0; n < max + unit*2; n++) {
var bitmapData:BitmapData = photos[(n - unit + max)%max];
var photo:Bitmap = new Bitmap(bitmapData.clone());
photo.smoothing = true;
container.addChild(photo);
photo.x = xoffset*(n - unit);
}
limit = xoffset*(max - 1);
offset = xoffset*max;
}
public function transit(id:int):void {
direction = (id - selectedId > 0);
selectedId = id;
releaseId = id;
targetPos = basePos - xoffset*selectedId;
addEventListener(Event.ENTER_FRAME, slide, false, 0, true);
}
private function slide(evt:Event):void {
container.x += (targetPos - container.x)*deceleration;
manage();
if (Math.abs(targetPos - container.x) < 0.5) {
container.x = targetPos;
removeEventListener(Event.ENTER_FRAME, slide);
dispatchEvent(new Event(Event.COMPLETE));
}
motionblur();
}
private function manage():void {
if (direction && container.x < - limit) {
selectedId = releaseId - max;
container.x += offset;
targetPos = basePos - xoffset*selectedId;
}
if (!direction && container.x > 0) {
selectedId = max + releaseId;
container.x -= offset;
targetPos = basePos - xoffset*selectedId;
}
}
private function motionblur():void {
var blur:Number = Math.abs(targetPos - container.x)/xoffset*100;
for (var n:uint = 0; n < max + unit*2; n++) {
var photo:Bitmap = Bitmap(container.getChildAt(n));
photo.filters = [new BlurFilter(blur, 0, 3)];
}
}
}
//////////////////////////////////////////////////
// TextLoaderクラス
//////////////////////////////////////////////////
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
class TextLoader extends EventDispatcher {
private var loader:URLLoader;
private var _data:*;
public static const TEXT:String = URLLoaderDataFormat.TEXT;
public static const BINARY:String = URLLoaderDataFormat.BINARY;
public static const COMPLETE:String = Event.COMPLETE;
public function TextLoader() {
loader = new URLLoader();
}
public function load(file:String, format:String = TextLoader.TEXT):void {
loader.dataFormat = format;
loader.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
loader.addEventListener(Event.COMPLETE, complete, false, 0, true);
try {
loader.load(new URLRequest(file));
} catch (err:Error) {
trace(err.message);
}
}
private function ioerror(evt:IOErrorEvent):void {
trace(evt.text);
}
private function httpstatus(evt:HTTPStatusEvent):void {
trace(evt.status);
}
private function securityerror(evt:SecurityErrorEvent):void {
trace(evt.text);
}
private function complete(evt:Event):void {
loader.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
loader.removeEventListener(Event.COMPLETE, complete);
_data = evt.target.data;
dispatchEvent(new Event(TextLoader.COMPLETE));
}
public function get data():* {
return _data;
}
}
//////////////////////////////////////////////////
// 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.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
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(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));
} catch (err:Error) {
trace(err.message);
}
}
public function unload():void {
loader.unload();
}
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));
}
}
//////////////////////////////////////////////////
// Btnクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.events.MouseEvent;
class Btn extends Sprite {
public var id:uint;
private var base:Sprite;
private var over:Sprite;
private var icon:Sprite;
private var _enabled:Boolean = true;
private var type:String = "";
public static const PREV:String = "prev";
public static const NEXT:String = "next";
public function Btn(t:String) {
type = t;
init();
}
private function init():void {
draw();
_up();
enabled = true;
mouseChildren = false;
}
private function rollOver(evt:MouseEvent = null):void {
_over();
}
private function rollOut(evt:MouseEvent = null):void {
_up();
}
public function get enabled():Boolean {
return _enabled;
}
public function set enabled(param:Boolean):void {
_enabled = param;
buttonMode = _enabled;
mouseEnabled = _enabled;
useHandCursor = _enabled;
if (_enabled) {
addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
_up();
} else {
removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
_over();
}
}
private function _up():void {
base.visible = true;
over.visible = false;
}
private function _over():void {
base.visible = false;
over.visible = true;
}
private function draw():void {
base = new Sprite();
addChild(base);
base.graphics.beginFill(0x333333);
base.graphics.drawRect(-16, -16, 32, 32);
base.graphics.endFill();
over = new Sprite();
addChild(over);
over.graphics.beginFill(0x666666);
over.graphics.drawRect(-16, -16, 32, 32);
over.graphics.endFill();
icon = new Sprite();
addChild(icon);
icon.graphics.beginFill(0xFFFFFF);
switch (type) {
case PREV :
icon.graphics.moveTo(1, -5);
icon.graphics.lineTo(-3, 0);
icon.graphics.lineTo(1, 5);
icon.graphics.lineTo(1, -5);
break;
case NEXT :
icon.graphics.moveTo(-1, -5);
icon.graphics.lineTo(3, 0);
icon.graphics.lineTo(-1, 5);
icon.graphics.lineTo(-1, -5);
break;
}
icon.graphics.endFill();
}
}
//////////////////////////////////////////////////
// ActionMenuクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
class ActionMenu extends Sprite {
public var id:uint;
public var base:Sprite;
public var over:Sprite;
private var mode:int = 0;
private var speed:Number = 0.1;
private var _selected:Boolean = false;
private var _enabled:Boolean = true;
public function ActionMenu() {
init();
}
private function init():void {
draw();
enabled = true;
mouseChildren = false;
}
private function rollOver(evt:MouseEvent = null):void {
mode = 1;
addEventListener(Event.ENTER_FRAME, action, false, 0, true);
}
private function rollOut(evt:MouseEvent = null):void {
mode = -1;
addEventListener(Event.ENTER_FRAME, action, false, 0, true);
}
private function action(evt:Event):void {
over.alpha += speed*mode;
if (over.alpha < 0) {
over.alpha = 0;
removeEventListener(Event.ENTER_FRAME, action);
}
if (over.alpha > 1) {
over.alpha = 1;
removeEventListener(Event.ENTER_FRAME, action);
}
}
public function get enabled():Boolean {
return _enabled;
}
public function set enabled(param:Boolean):void {
if (!_selected) {
_enabled = param;
} else {
_enabled = false;
}
buttonMode = _enabled;
mouseEnabled = _enabled;
useHandCursor = _enabled;
if (_enabled) {
addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
} else {
removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
}
}
public function get selected():Boolean {
return _selected;
}
public function set selected(param:Boolean):void {
_selected = param;
enabled = !_selected;
if (_selected) {
removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
rollOver();
} else {
addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
rollOut();
}
}
private function draw():void {
base = new Sprite();
addChild(base);
over = new Sprite();
addChild(over);
over.graphics.beginFill(0xFFFF00, 0.8);
over.graphics.drawRect(0, 0, 60, 45);
over.graphics.drawRect(3, 3, 54, 39);
over.graphics.endFill();
over.graphics.beginFill(0xFFFFFF, 0.4);
over.graphics.drawRect(3, 3, 54, 39);
over.graphics.endFill();
}
}