// Richard Caceres, rcaceres {at} ucla.edu // DMA 199 // $Id: ImageGallery3.as,v 380.2 2008-01-30 18:11:35-08 - - $ // $Date: 2008-01-30 18:11:35-08 $ // ImageGallery3 is an attempt to recreate the gallery software called AUTOVIEWER // that can be downloaded for free @ http://www.airtightinteractive.com/projects/autoviewer/ package { import flash.display.Sprite; import flash.display.MovieClip; import flash.display.StageScaleMode; import flash.display.StageAlign; import flash.display.Stage; import flash.display.StageDisplayState; import flash.events.*; import flash.geom.Point; import flash.net.*; import flash.display.SimpleButton; import flash.display.*; public class ImageGallery3 extends MovieClip { // Class variables. public var images:Array; // Store an array of Image objects public var xml_images:XML; // The variable to which the loaded XML will be assigned public var urlLoader:URLLoader; public var imgs_container:Sprite; public var curr_image_index:Number = 0; public var fullscreen_btn:SimpleButton; public var in_full_screen:Boolean = false; public var imageStrip:ImageStrip; private var IMAGE_SPACING = 10; // pixels // urls public static var photo_dir = "images/"; public static var xml_url = "gallerydata.xml"; ////////////////////////////////////////////// public function ImageGallery3 () { // Set stage settings stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; stage.quality = "low"; // Instantiate objects. // fullscreen_btn = new Fullscreen_btn(0,0); // Subscribe to events/ stage.addEventListener (Event.RESIZE, handleResize); stage.addEventListener (KeyboardEvent.KEY_UP, keyUpHandler); stage_btn.addEventListener (MouseEvent.CLICK , handleFullScreenClick); leftarrow_mc.addEventListener (MouseEvent.CLICK, handleArrowClickLeft); rightarrow_mc.addEventListener (MouseEvent.CLICK, handleArrowClickRight); // Load the xml. loadXML (xml_url, xmlCompleteHandler); } ////////////////////////////////////////////// public function xmlCompleteHandler (e:Event) { // This is the xml load complete handler. trace ("xmlCompleteListener!"); // The string containing the loaded XML is assigned to the URLLoader // object's data variable (i.e., urlLoader.data). To create a new XML // instance from that loaded string, we pass it to the XML constructor xml_images = new XML(urlLoader.data); xml_images.ignoreWhitespace = true; // Add all the images in the xml to the images array // imageXML = new XML (xmlLoader.data); // trace(imageXML.image[0].@src); /// CHECK OUT THE NEW XML API BASED ON "XPATH" // imageXML.image[0].title.@src; var images = new Array(); for each (var child in xml_images.image) { var img_url = photo_dir+child.filename; //trace ("child.width: "+child.width); var img = new ImageIG3 (img_url, child.width, child.height); images.push (img); } // Get the extra settings variables trace ("gallery: "+xml_images.@imagePadding); IMAGE_SPACING = Number(xml_images.@imagePadding); imageStrip = new ImageStrip (images, stage, IMAGE_SPACING); addChild(imageStrip); fullscreen_btn = new SimpleButton(); addChild(fullscreen_btn); positionArrowButtons(); //trace (imageStrip); imageStrip.focusImage(curr_image_index); } // end xmlCompleteHandler ////////////////////////////////////////////// public function handleArrowClickLeft (e:Event) { trace ("handleArrowClickLeft"); curr_image_index--; focusCurrImage(); } public function handleArrowClickRight (e:Event) { trace ("handleArrowClickRight"); curr_image_index++; focusCurrImage(); } ////////////////////////////////////////////// public function keyUpHandler (event:KeyboardEvent) { // This method will respond to KeyboardEvent.KEY_UP events and check to // to see if either LEFT or RIGHT were pressed and cycle through the images. //trace("keyUpHandler: " + event.keyCode); switch (event.keyCode) { case 37 : curr_image_index--; focusCurrImage(); break; case 39 : curr_image_index++; focusCurrImage(); break; default : return; } } ////////////////////////////////////////////// public function focusCurrImage () { if (curr_image_index < 0){ curr_image_index += imageStrip.images.length; } else if (curr_image_index >= imageStrip.images.length) { curr_image_index -= imageStrip.images.length; } // Finally call imageStripfocusImage(); imageStrip.focusImage(curr_image_index); } ////////////////////////////////////////////// public function handleResize (e:Event) { positionArrowButtons(); // resize the image strip imageStrip.handleResize(e); } public function positionArrowButtons():void { // make the arrow's depth greater removeChild(leftarrow_mc); addChild(leftarrow_mc); removeChild(rightarrow_mc); addChild(rightarrow_mc); // position the arrows leftarrow_mc.x = 20; leftarrow_mc.y = stage.stageHeight/2 - leftarrow_mc.height/2; rightarrow_mc.x = stage.stageWidth - 20 - rightarrow_mc.width; rightarrow_mc.y = stage.stageHeight/2 - leftarrow_mc.height/2; } ////////////////////////////////////////////// public function handleFullScreenClick (e:Event) { trace ("handleFullScreenClick:"+in_full_screen); //stage.displayState = StageDisplayState.FULL_SCREEN; if (in_full_screen == false) { stage.displayState = StageDisplayState.FULL_SCREEN; in_full_screen = true; //goFullScreen(); } else { in_full_screen = false; stage.displayState = StageDisplayState.NORMAL; //exitFullScreen(); } } ////////////////////////////////////////////// public function loadXML (url_str:String, handler:Function) { // This reusable method encapsoluates loading xml. // Specify the location of the external XML var urlRequest:URLRequest = new URLRequest(url_str); // Create an object that can load external text data urlLoader = new URLLoader(); // Register to be notified when the XML finishes loading urlLoader.addEventListener(Event.COMPLETE, handler); // Load the XML urlLoader.load(urlRequest); } } // end class ImageGallery2 } // end package