package { import com.bit101.components.PushButton; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; /** * Sample01 * * @author alumican.net */ public class Sample01 extends Sprite { static public const LEFT:String = "l"; static public const CENTER:String = "c"; static public const RIGHT:String = "r"; static public const TOP:String = "t"; static public const MIDDLE:String = "m"; static public const BOTTOM:String = "b"; static public const X:String = "x"; static public const Y:String = "y"; static public var direction:String = X; static public var alignX:String = CENTER; static public var alignY:String = MIDDLE; /** * カルーセルエンジン */ private var _carousel:MyCarousel; private var _velocity:Number; private var _container:Sprite; public function Sample01():void { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; //アイテム配置用コンテナ _container = addChild( new Sprite() ) as Sprite; //カルーセル _carousel = new MyCarousel(); _carousel.setup( ["circle", "triangle", "rectangle"], _container, 0, 1000, 0, 30, "x", true ); //UI var backwardButton:PushButton = new PushButton(this, 10, 10, "<<", null); backwardButton.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void { stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler); addEventListener(Event.ENTER_FRAME, _updateScroll); _velocity = -10; }); var forwardButton:PushButton = new PushButton(this, 109, 10, ">>", null); forwardButton.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void { stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler); addEventListener(Event.ENTER_FRAME, _updateScroll); _velocity = 10; }); var isPlaying:Boolean = false; var scaleButton:PushButton = new PushButton(this, 230, 10, "PLAY", function(e:MouseEvent):void { isPlaying = !isPlaying; scaleButton.label = isPlaying ? "PAUSE" : "PLAY"; if (isPlaying) { addEventListener(Event.ENTER_FRAME, _updateScale); } else { removeEventListener(Event.ENTER_FRAME, _updateScale); } }); var alignGroup:Sprite = addChild( new Sprite() ) as Sprite; alignGroup.x = 350; alignGroup.y = 10; new PushButton(alignGroup, 0, 0, "TL", function(e:MouseEvent):void { alignY = TOP; alignX = LEFT; _updateAlign(); } ); new PushButton(alignGroup, 99, 0, "TC", function(e:MouseEvent):void { alignY = TOP; alignX = CENTER; _updateAlign(); } ); new PushButton(alignGroup, 198, 0, "TR", function(e:MouseEvent):void { alignY = TOP; alignX = RIGHT; _updateAlign(); } ); new PushButton(alignGroup, 0, 19, "ML", function(e:MouseEvent):void { alignY = MIDDLE; alignX = LEFT; _updateAlign(); } ); new PushButton(alignGroup, 99, 19, "MC", function(e:MouseEvent):void { alignY = MIDDLE; alignX = CENTER; _updateAlign(); } ); new PushButton(alignGroup, 198, 19, "MR", function(e:MouseEvent):void { alignY = MIDDLE; alignX = RIGHT; _updateAlign(); } ); new PushButton(alignGroup, 0, 38, "BL", function(e:MouseEvent):void { alignY = BOTTOM; alignX = LEFT; _updateAlign(); } ); new PushButton(alignGroup, 99, 38, "BC", function(e:MouseEvent):void { alignY = BOTTOM; alignX = CENTER; _updateAlign(); } ); new PushButton(alignGroup, 198, 38, "BR", function(e:MouseEvent):void { alignY = BOTTOM; alignX = RIGHT; _updateAlign(); } ); var isVertical:Boolean = false; var directionButton:PushButton = new PushButton(this, 670, 10, "VERTICAL", function(e:MouseEvent):void { isVertical = !isVertical; directionButton.label = isVertical ? "HORIZONTAL" : "VERTICAL"; _carousel.scrollProperty = direction = isVertical ? "y" : "x"; _updateDirection(); }); //リサイズ stage.addEventListener(Event.RESIZE, _resize); _resize(); } private function _updateScale(e:Event):void { var n:int = _carousel.itemCount; for (var i:int = 0; i < n; ++i) { MyItem(_carousel.items[i]).updateScale(); } _carousel.adjustScale(_carousel.getItemIndexByPosition(direction == Sample01.X ? _container.mouseX : _container.mouseY)); } private function _updateDirection():void { var n:int = _carousel.itemCount; for (var i:int = 0; i < n; ++i) { if (direction == Sample01.X) { MyItem(_carousel.items[i]).y = 0; } else { MyItem(_carousel.items[i]).x = 0; } } _carousel.adjustScale(0); _resize(); } private function _updateAlign():void { var n:int = _carousel.itemCount; for (var i:int = 0; i < n; ++i) { MyItem(_carousel.items[i]).updateAlign(); } _carousel.adjustScale(0); } private function _mouseUpHandler(e:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler); removeEventListener(Event.ENTER_FRAME, _updateScroll); } private function _updateScroll(e:Event):void { _carousel.scrollPosition += _velocity; } /** * リサイズ */ private function _resize(e:Event = null):void { if (direction == Sample01.Y) { _container.x = stage.stageWidth * 0.5; _carousel.fieldSize = stage.stageHeight; } else { _container.y = stage.stageHeight * 0.5; _carousel.fieldSize = stage.stageWidth; } if (!_carousel.feedForward) { if (direction == Sample01.Y) { _container.y = _carousel.fieldSize; } else { _container.x = _carousel.fieldSize; } } } } }