import mx.transitions.Tween; // on importe la classe Tween
import mx.transitions.easing.*; // on importe les classe easing pour les effets visuels.
// URLs
var url_photos:String = "http://luc.juggery.free.fr/test/photos/";
var url_xml:String = "http://luc.juggery.free.fr/test/xml/picts.xml";
main_pict._x = 5;
main_pict._y = 5;
// Width of diaporama
var diaporama_width:Number = 600;
// Space between diapo
var diapo_space:Number = 20;
// Max height and width of main_pict movieClip
var max_w:Number = 590;
var max_h:Number = 370;
// Borders for main pict
var border_w:Number = 5;
var border_h:Number = 5;
// Position of main_pict movieClip
var main_pict_x:Number = 5;
var main_pict_y:Number = 5;
// MovieClip Loader and listener
var mcl:MovieClipLoader = new MovieClipLoader();
var listener:Object = new Object();
listener.onLoadProgress = function(mc:MovieClip, loaded:Number, total:Number){
// trace(mc + ".onLoadProgress with " + loaded + " bytes of " + total);
}
listener.onLoadComplete = function(mc:MovieClip){
// trace("onLoadComplete, mc._width: " + mc._width);
}
listener.onLoadInit = function(mc:MovieClip) {
// Get Height and Width of loaded pict
var pict_w:Number = mc._width;
var pict_h:Number = mc._height;
// Default value for scale
var scale:Number = 100;
// Set default position in MovieClip
mc._x = 0;
mc._y = 0;
trace("sizes, pict_w: " + pict_w + ", pict_h: " + pict_h + ", max_w: " + max_w + ", max_h: " + max_h);
// Check if picture is bigger than frame and set scale and position accordingly
if((pict_w > max_w) && (pict_h > max_h)){ // width and height are bigger than max
// trace("both dim bigger than max");
scale = pict_w / max_w > pict_h / max_h ? pict_w / max_w : pict_h / max_h;
scale = 100 / scale;
}
else if(pict_w > max_w){ // only width is bigger than max
// trace("width bigger than max");
scale = 100 / (pict_w / max_w);
}
else if(pict_h > max_h){ // only height is bigger than max
// trace("height bigger than max");
scale = 100 / (pict_h / max_h);
}
else{ // pict fits the frame
// trace("picts fits");
}
// Set position in Movie Clip
set_pict_position(mc, pict_w * scale / 100, pict_h * scale / 100);
//mc._scale = scale;
new mx.transitions.Tween(mc, "_yscale",Regular.easeOut, 0, scale, 1, true);
new mx.transitions.Tween(mc, "_xscale",Regular.easeOut, 0, scale, 1, true);
};
mcl.addListener(listener);
// Build array containing pict info
var h_infos:Object = {};
// Get info from XML file
xml_doc = new XML();
xml_doc.ignoreWhite=true;
xml_doc.load(url_xml);
xml_doc.onLoad = function(ok){
if(ok){
// Parse XML file to get picts'name and comments
parse_file(xml_doc);
// Display first pict
display_pict(1);
display_comments(1);
}
}
function display_pict(index){
_root.mcl.loadClip(url_photos + h_infos[index].pict_name, main_pict);
_root.current = index;
}
function display_comments(index){
this.commentaires.text = h_infos[index].pict_name;
this.commentaires.text += "\n";
this.commentaires.text += h_infos[index].pict_comments;
}
// Parse XML file and build hash
function parse_file(xml_file){
var picts:Object = xml_file.firstChild.childNodes;
_root.largeurTotale = picts.length*100;
_root.pict_nbr = picts.length;
// Loop through all childs
for(i=0;i<picts.length;i++){
// Get pict name and comments
var pict:Object = xml_file.firstChild.childNodes[i];
var p_name:String = pict.childNodes[0].childNodes[0];
var p_comments:String = pict.childNodes[1].childNodes[0];
// Populate h_infos hash
h_infos[i] = {pict_name:p_name,pict_comments:p_comments};
// MovieClip Loader and listener
var mcl_diapo:MovieClipLoader = new MovieClipLoader();
var listener_diapo:Object = new Object();
listener_diapo.onLoadInit = function(mc_diapo:MovieClip) {
// Set width and height to 70
mc_diapo._width = 70;
mc_diapo._height = 70;
}
mcl_diapo.addListener(listener_diapo);
// Create movieClip for each pict
var mc:MovieClip = this.diaporama.createEmptyMovieClip("photo"+i, i);
mc._x = 100*i + diapo_space;
mc.createEmptyMovieClip("conteneur",0); // I do not really understand why....
// Load diapo
mcl_diapo.loadClip(url_photos + p_name, mc.conteneur);
// Set id of the current movieClip
mc.number = i;
// Add frame to movieClip
mc.createEmptyMovieClip("cadre", mc.getNextHighestDepth());
with(mc.cadre) {
//lineStyle(1, "0x000000", 100);
lineStyle(1, "0xFFFFFF", 100);
moveTo(0, 0);
lineTo(70, 0);
lineTo(70, 70);
lineTo(0, 70);
lineTo(0, 0);
}
//mc.onRollOver = function() {
mc.onPress = function() {
// Keep track of current pict
_root.current = this.number;
// Load pict in movieClip
display_pict(_root.current);
// Display comments
display_comments(_root.current);
}
}
}
// Go backward
previous_pict.onPress = function(){
if(diaporama._x <= 0){
diaporama._x += 100;
}
// Display previous
if(_root.current > 0){
_root.current -= 1;
display_pict(_root.current);
display_comments(_root.current);
}
};
// Go forward
next_pict.onPress = function(){
if((diaporama._x + _root.largeurTotale) >= 600 ){
diaporama._x -= 100;
}
if(_root.current < _root.pict_nbr-1){
_root.current += 1;
display_pict(_root.current);
display_comments(_root.current);
}
};
function set_pict_position(mc:MovieClip,w:Number,h:Number){
//trace("w: " + w + ", h: " + h);
mc._x = (600 - w) / 2;
mc._y = (max_h + border_h - h) / 2;
}
Partager