Newer
Older
AIR-TUve / src / Carousel.as
@reddawg reddawg on 5 Dec 2007 5 KB *** empty log message ***
package {
    import flash.geom.Point;

//    import mx.core.Application;
    import mx.core.UIComponent;
    import mx.effects.Move;
    import mx.effects.Parallel;
    import mx.effects.Resize;
    import mx.controls.Alert;
    import flash.events.MouseEvent;
    import mx.events.EffectEvent;
    import mx.events.FlexEvent;
    //import mx.containers.Canvas;

    public class Carousel extends UIComponent
    {

		private var picPos:Array;
		private var picDimension:Array;
		[Bindable]
		public var gallery:Object;
		public var tvMaster:Master;

        public function Carousel() {
          super();
          }

		override protected function createChildren():void
		{
		    super.createChildren();
		    for (var i:int = 0; i < 7; i++)
		    {
		        addChild(new CarouselImage());
		    }
		}

		public function reset(selected:int):void
		{
			var appWidth:int = tvMaster.tuveSW.width; //Application.application.width;
			var compHeight:int = 160;//(Application.application.height - 90)*.8;
			
			picDimension = new Array(7);
			picPos = new Array(7);

            picDimension[0] = 0;//175 * 0.10;
			picDimension[1] = 175 * 0.20;
			picDimension[2] = 175 * 0.40;
			picDimension[3] = 175;
			picDimension[4] = 175 * 0.40;
			picDimension[5] = 175 * 0.20;
			picDimension[6] = 0;//175 * 0.10;

			picPos[0] = new Point(0, (compHeight - (175 * 0.10))/2);
			picPos[1] = new Point(appWidth*.025 + 8, (compHeight - picDimension[1])/2);
			picPos[2] = new Point(appWidth*.125 + 16, (compHeight - picDimension[2])/2);
			picPos[3] = new Point(appWidth*.275 + 24, (compHeight - picDimension[3])/2);
			picPos[4] = new Point(appWidth*.675 + 32, (compHeight - picDimension[4])/2);
			picPos[5] = new Point(appWidth*.825 + 40, (compHeight - picDimension[5])/2);
			picPos[6] = new Point(appWidth, (compHeight - (175 * 0.10))/2);

			for (var i:int=0; i < 7; i++)
			{
			    var image:CarouselImage = getChildAt(i) as CarouselImage;
			    var pos:int = selected + i - 3;
			    if (pos >= 0 && pos < tvMaster.songList.length)
			    {
			        image.width = picDimension[i];
			        image.height = picDimension[i];
			        image.x = picPos[i].x;
			        image.y = picPos[i].y;
			        image.source = "/images/" + tvMaster.songList.getItemAt(pos).thumb;
			        image.visible = true;
				}
				else
				{
				    image.visible = false;
				}
			}
		}

        /**
         * Add move and resize effects to the images specified.
         * @param direction +1 if you're rotating right, -1 if left
         * @param start where in the child list do you start playing effects (avoiding the offscreen one)
         * @param end where in the child list do you stop playing effects (avoiding the offscreen one)
         */
		private function playEffects(direction:int, start:int, end:int):void
		{
		    var parallel:Parallel = new Parallel();

		    for (var i:int=start; i < end; i++)
		    {

		        var image:CarouselImage = getChildAt(i) as CarouselImage;
		        if (image.visible)
		        {
		            var idx:int = i + direction;
		            var move:Move = new Move();
		            move.target = image;
		            move.duration = 1000;
		            move.xTo = picPos[idx].x;
		            move.yTo = picPos[idx].y;
		            parallel.addChild(move);

		            var resize:Resize = new Resize();
		            resize.target = image;
		            resize.duration = 1000;
		            resize.widthTo = picDimension[idx];
		            resize.heightTo = picDimension[idx];
		            parallel.addChild(resize);
		        }
		    }
            //parallel.addEventListener(EffectEvent.EFFECT_END,doNext,false,0,true);
			parallel.play();
		}

		public function rotateLeft(iX:Number):void
		{
			playEffects(-1, 1, 7);
			var offscreen:CarouselImage = getChildAt(0) as CarouselImage;
			this.removeChild(offscreen);
			//in an ideal world you'd re-use this image but we found
			//that if click the rotate buttons fast enough the offscreen
			//image will appear and fly across the screen because it was
			//playing in a previous effect
			//stopping the effect in the middle as soon as you know this
			//image is offscreen makes the image drop offscreen and
			//isn't a great visual effect
			//there are probably other ways to solve this like preventing
			//the rapid click of the next/prev buttons
			offscreen = new CarouselImage();
	        offscreen.move(picPos[6].x, picPos[6].y);
            offscreen.width = picDimension[6];
            offscreen.height = picDimension[6];
	        addChild(offscreen);
	        if (iX + 3 < tvMaster.songList.length)
	        {
	            offscreen.source = "/images/" + tvMaster.songList.getItemAt(iX + 3).thumb;
	            offscreen.visible = true;
	        }
	        else
	        {
	            offscreen.source = "";
	            offscreen.visible = false;
	        }


		}

		public function rotateRight(iX:Number):void
		{
		    playEffects(1, 0, 6);
			var offscreen:CarouselImage = getChildAt(6) as CarouselImage;
			this.removeChild(offscreen);
			//in an ideal world you'd re-use this image but we found
			//that if click the rotate buttons fast enough the offscreen
			//image will appear and fly across the screen because it was
			//playing in a previous effect
			//stopping the effect in the middle as soon as you know this
			//image is offscreen makes the image drop offscreen and
			//isn't a great visual effect
			//there are probably other ways to solve this like preventing
			//the rapid click of the next/prev buttons
			offscreen = new CarouselImage();
	        offscreen.move(picPos[0].x, picPos[0].y);
            offscreen.width = picDimension[0];
            offscreen.height = picDimension[0];
	        addChildAt(offscreen, 0);

	        if (iX - 3 >= 0)
	        {
	            offscreen.source = "/images/" + tvMaster.songList.getItemAt(iX - 3).thumb;
	            offscreen.visible = true;
	        }
	        else
	        {
	            offscreen.source = "";
	            offscreen.visible = false;
	        }
    	}
    }
}