home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2001 May / Gamestar_29_2001-05_cd1.bin / DEMA / DesperadosDemoEnglish.exe / Extras / md8lib.js < prev    next >
Text File  |  2001-02-13  |  11KB  |  429 lines

  1. /******************************************************************************
  2. * File: md8lib.js                                                          *
  3. *                                                                             *
  4. * Copyright MatchWare A/S                                                     *
  5. * Author: Jens ╪. Nielsen                                                     *
  6. ******************************************************************************/
  7.  
  8.  
  9. var clickobj  = null;
  10. var insideobj = null;
  11. var actions   = new Array();
  12. var terminating = false;
  13. var initialized = false;
  14. var animatingobjs = new Array();
  15. var nextanimation = 0;
  16.  
  17. //-------------------------------------
  18.  
  19. function IsNetscape() {
  20.  
  21.  if ( navigator.appName != "Netscape") return false;
  22.  if ( parseInt(navigator.appVersion) < 4) return false;
  23.  if ( parseInt(navigator.appVersion) > 4) return false;
  24.  return true;
  25. }
  26.  
  27.  
  28. function IsIE() {
  29.  
  30.  var sAgent = navigator.userAgent;
  31.  var bIsMac = sAgent.indexOf("Mac") > -1;
  32.  var bIsIE4Final = sAgent.indexOf("IE 4")  > -1
  33.      && sAgent.indexOf("b1") <= -1
  34.      && sAgent.indexOf("p1") <= -1;
  35.  var bIsIE5 = sAgent.indexOf("IE 5")  > -1;
  36.  return bIsIE4Final || bIsIE5;
  37. }
  38.  
  39. function IsOldBrowser() {
  40.  
  41.  return ! IsIE() && ! IsNetscape();
  42. }
  43.  
  44. function FrameIt( page) {
  45.  
  46.  terminating = true;
  47.  window.location = "frame.htm?" + page;
  48. }
  49.  
  50. //------------------------------------
  51.  
  52. var ANIM_TICK = 50;
  53.  
  54. var ANIM_STYLE_NORMAL   = 0;
  55. var ANIM_STYLE_FIRSTPOS = 1;
  56. var ANIM_STYLE_FLYTO    = 2;
  57. var ANIM_STYLE_FLYFROM  = 3;
  58.  
  59. //------------------------------------
  60.  
  61. function PageAction( pagename) {
  62.  
  63.  this.m_PageName = pagename;
  64.  this.Start = PageAction_Start;
  65. }
  66.  
  67. function PageAction_Start() {
  68.  
  69.  if ( this.m_PageName == "") return;
  70.  if ( this.m_PageName == "@PrePage") {
  71.    window.history.back();
  72.  }
  73.  else {
  74.    if ( parent != self) {
  75.      if ( top.global.advbookmark) {
  76.        var url = top.location.href;
  77.        var len = url.indexOf( "?",0);
  78.        if ( len > 0)
  79.          url = url.substring( 0,len);
  80.        url += "?" + this.m_PageName + PAGE_ACTION_POST_FIX;
  81.        terminating = true;
  82.        top.location = url;
  83.        return;
  84.      }
  85.    }
  86.    terminating = true;
  87.    location = this.m_PageName + PAGE_ACTION_POST_FIX;
  88.  }
  89. }
  90.  
  91. //------------------------------------
  92.  
  93. function bsearch(myarray, val) {
  94.  
  95.   var lo=0;
  96.   var hi=myarray.length;
  97.  
  98.   while(true) {
  99.     var mid=Math.floor((hi+lo)/2);
  100.     if(mid==0)
  101.       return mid;
  102.     if(mid==myarray.length-1)
  103.       return mid-1;
  104.     else if((myarray[mid]<=val && myarray[mid+1]>val))
  105.       return mid;               
  106.     else if(val<myarray[mid])
  107.       hi=mid;
  108.     else
  109.       lo=mid;
  110.   }
  111.   return 0;
  112. }
  113.  
  114.  
  115. /******************************************************************************
  116. * Class AnimationPath                                                         *
  117. ******************************************************************************/
  118.  
  119. function AnimationPath(xpoints_array, ypoints_array) {
  120.  
  121.   this.m_xpoints_array=xpoints_array;
  122.   this.m_ypoints_array=ypoints_array;
  123.   this.m_sqdists=new Array(xpoints_array.length);
  124.   this.m_sqdistsum=0;
  125.  
  126.   this.GetPointAtTime=AnimationPath_GetPointAtTime;
  127.  
  128.   /*********************/ 
  129.   var x=xpoints_array[0];
  130.   var y=ypoints_array[0];
  131.  
  132.   for(i=0; i<this.m_sqdists.length; i++) {
  133.     var dx = this.m_xpoints_array[i]-x;
  134.     var dy = this.m_ypoints_array[i]-y;
  135.     this.m_sqdistsum += Math.sqrt(dx*dx+dy*dy);
  136.     this.m_sqdists[i] = this.m_sqdistsum;
  137.  
  138.     x=xpoints_array[i];
  139.     y=ypoints_array[i];
  140.   }
  141.  
  142. }
  143.  
  144.  
  145. function AnimationPath_GetPointAtTime(t) {
  146.  
  147.   if(t>=1) {
  148.     var endx=this.m_xpoints_array[this.m_xpoints_array.length-1];
  149.     var endy=this.m_ypoints_array[this.m_ypoints_array.length-1];
  150.     return new Array(endx, endy);
  151.   }
  152.   
  153.   var pos = t*this.m_sqdistsum;
  154.   var idx = bsearch(this.m_sqdists, pos);
  155.   
  156.   var dx  = this.m_xpoints_array[idx+1] - this.m_xpoints_array[idx];
  157.   var dy  = this.m_ypoints_array[idx+1] - this.m_ypoints_array[idx];
  158.    
  159.  
  160.   var dv  = pos - this.m_sqdists[idx];     /* distance we went too far */
  161.   var lenseg = this.m_sqdists[idx+1]-this.m_sqdists[idx];
  162.  
  163.   dx*=(dv/lenseg);
  164.   dy*=(dv/lenseg);
  165.  
  166.   xpos=this.m_xpoints_array[idx]+dx;
  167.   ypos=this.m_ypoints_array[idx]+dy;
  168.  
  169.   return new Array(xpos, ypos);
  170. }
  171.  
  172.  
  173. /******************************************************************************
  174. * Class AnimationAction                                                       *
  175. ******************************************************************************/
  176.  
  177. function AnimationAction( myname,obj,path,totaltime,repeat,reverse,autoshow,style) {
  178.  
  179.   this.m_name=myname;
  180.   this.m_object=obj;
  181.   this.m_path=path;
  182.   this.m_totaltime=totaltime;
  183.   this.m_time=0;
  184.   this.m_paused=false;
  185.   this.m_startdate=null;
  186.   this.m_repeat=repeat;
  187.   this.m_reverse=reverse;
  188.   this.m_style=style;
  189.   this.m_autoshow = autoshow;
  190.   this.m_doshow = this.m_autoshow;
  191.   this.m_timerid = 0;
  192.   
  193.   this.Start = AnimationAction_Start;
  194.   this.Stop  = AnimationAction_Stop;
  195.   this.Pause = AnimationAction_Pause;
  196.   this.Tick  = AnimationAction_Tick;
  197. }
  198.  
  199. function FindAnimation( objname) {
  200.  
  201.  for ( var i = 0; i < nextanimation;i++) {
  202.    if ( animatingobjs[ i].m_object == objname)
  203.      return i;
  204.  }
  205.  return -1;
  206. }
  207.  
  208. function RemoveAnimation( objname) {
  209.  
  210.  var i = FindAnimation( objname);
  211.  if ( i >= 0) {
  212.    animatingobjs[ i] = null;
  213.    nextanimation--;
  214.    for ( ; i < nextanimation; i++)
  215.      animatingobjs[ i] = animatingobjs[ i+1];
  216.  }
  217. }
  218.  
  219.  
  220. function AnimationAction_Start() { 
  221.  
  222.  var i = FindAnimation( this.m_object);
  223.  if ( i >= 0) {
  224.    animatingobjs[ i].Stop();
  225.  }
  226.  animatingobjs[ nextanimation++] = this;
  227.  
  228.  if ( IsObjVisible( this.m_object))
  229.    this.m_doshow = false;
  230.  
  231.  var obj_x = GetObjLeft( this.m_object) + GetObjWidth( this.m_object) / 2;
  232.  var obj_y = GetObjTop( this.m_object) + GetObjHeight( this.m_object) / 2;
  233.  if ( ! this.m_paused) { // !paused => anim from start 
  234.    this.m_time=0;
  235.    this.m_startdate=new Date();
  236.    var endidx=this.m_path.m_xpoints_array.length-1;
  237.    if ( this.m_style == ANIM_STYLE_NORMAL) {
  238.        this.m_offset_x=0;
  239.        this.m_offset_y=0;
  240.    }
  241.    else if ( this.m_style == ANIM_STYLE_FLYTO) {
  242.      var endpoint = this.m_reverse ? 0 : endidx;
  243.        this.m_offset_x = obj_x - this.m_path.m_xpoints_array[endpoint];
  244.      this.m_offset_y = obj_y - this.m_path.m_ypoints_array[endpoint];
  245.    }
  246.    else if ( this.m_style == ANIM_STYLE_FLYFROM) {
  247.      var startpoint = this.m_reverse ? endidx : 0;
  248.        this.m_offset_x = obj_x - this.m_path.m_xpoints_array[ startpoint];
  249.        this.m_offset_y = obj_y - this.m_path.m_ypoints_array[ startpoint];
  250.    }
  251.    else if ( this.m_style == ANIM_STYLE_FIRSTPOS) {
  252.        this.m_seg0_x = obj_x;
  253.        this.m_seg0_y = obj_y;
  254.      startpoint = this.m_reverse ? endidx : 0;
  255.      this.m_seg0_dx = this.m_path.m_xpoints_array[ startpoint]-this.m_seg0_x;
  256.      this.m_seg0_dy = this.m_path.m_ypoints_array[ startpoint]-this.m_seg0_y;
  257.   
  258.      this.m_seg0_len=Math.sqrt(this.m_seg0_dx*this.m_seg0_dx + this.m_seg0_dy*this.m_seg0_dy);
  259.      this.m_seg0_endtime= this.m_seg0_len / (this.m_seg0_len+this.m_path.m_sqdistsum);
  260.    }
  261.  }
  262.  else {               // paused
  263.    this.m_startdate=new Date();
  264.    paused=false;
  265.  }
  266.  
  267.  this.m_timerid = window.setTimeout( "actions." + this.m_name + ".Tick()", ANIM_TICK);
  268. }
  269.  
  270.  
  271. function AnimationAction_Tick() {
  272.  
  273.  if ( terminating) return;
  274.  this.m_timerid = 0;
  275.  if(this.m_startdate!=null) {
  276.    var datenow=new Date();
  277.    var msnow=this.m_time + (datenow-this.m_startdate);
  278.    var t=msnow/this.m_totaltime;
  279.    if ( t > 1) t = 1;
  280.    if ( this.m_style != ANIM_STYLE_FIRSTPOS) {
  281.      if(this.m_reverse)
  282.        t=1-t;
  283.      point = this.m_path.GetPointAtTime(t);
  284.      var x = point[0] + this.m_offset_x - GetObjWidth( this.m_object) / 2;
  285.      var y = point[1] + this.m_offset_y - GetObjHeight( this.m_object) / 2;
  286.      SetObjPosition( this.m_object,x,y);
  287.    }
  288.    else { 
  289.      // firstpos hack
  290.      if(t < this.m_seg0_endtime) {
  291.        var tt= t*(1/this.m_seg0_endtime);
  292.        var x = this.m_seg0_x+tt*this.m_seg0_dx - GetObjWidth( this.m_object) / 2;
  293.        var y = this.m_seg0_y+tt*this.m_seg0_dy - GetObjHeight( this.m_object) / 2;
  294.        SetObjPosition( this.m_object,x,y);
  295.      }
  296.      else {
  297.        var tt=(t-this.m_seg0_endtime)*(1/(1-this.m_seg0_endtime));
  298.        if(this.m_reverse)
  299.          tt=1-tt;
  300.        point=this.m_path.GetPointAtTime(tt);
  301.        var x = point[ 0] - GetObjWidth( this.m_object) / 2;
  302.        var y = point[ 1] - GetObjHeight( this.m_object) / 2;
  303.        SetObjPosition( this.m_object,x,y);
  304.      }
  305.    }
  306.  
  307.    if(msnow<this.m_totaltime)     
  308.      this.m_timerid = window.setTimeout( "actions." + this.m_name+".Tick()", ANIM_TICK);
  309.    else if(this.m_repeat) {
  310.      this.m_startdate=new Date();
  311.      this.m_timerid = window.setTimeout( "actions." + this.m_name + ".Tick()", ANIM_TICK);
  312.    }
  313.    else {
  314.      RemoveAnimation( this.m_object);
  315.    }
  316.  }
  317.  if ( this.m_doshow) {
  318.    ShowObject( this.m_object,true);
  319.    this.m_doshow = false;
  320.  }
  321. }
  322.  
  323. function AnimationAction_Stop() {
  324.  
  325.  RemoveAnimation( this.m_object);
  326.  this.m_paused=false; 
  327.  this.m_time=0;
  328.  if ( this.m_timerid) {
  329.    clearTimeout( this.m_timerid);
  330.    this.m_timerid = 0;
  331.  }
  332. }
  333.  
  334. function AnimationAction_Pause() {
  335.  
  336.   this.m_paused=true;
  337.   d= new Date();
  338.   this.m_time+=d-this.m_startdate;
  339.   this.m_startdate=null;
  340. }
  341.  
  342. //------------------------------------
  343.  
  344. function TimeLineAction( myname, actionarray,delayarray) {
  345.  
  346.  this.m_Name       = myname;
  347.  this.m_ActionList = actionarray;
  348.  this.m_DelayList  = delayarray;
  349.  this.m_Current    = 0;
  350.  this.m_TimerId    = 0;
  351.  
  352.  this.Start = TimeLineAction_Start;
  353.  this.Tick  = TimeLineAction_Tick;
  354. }
  355.  
  356.  
  357. function TimeLineAction_Start() {
  358.  
  359.  if ( this.m_ActionList.length == 0) return;
  360.  if ( this.m_TimerId) 
  361.    clearTimeout( this.m_TimerId);
  362.  this.m_Current = 0;
  363.  this.m_TimerId = window.setTimeout( "actions." + this.m_Name+".Tick()", this.m_DelayList[ 0]);
  364. }
  365.  
  366.  
  367. function TimeLineAction_Tick() {
  368.  
  369.  this.m_TimerId = 0;
  370.  if ( terminating) return;
  371.  var index = this.m_Current;
  372.  this.m_ActionList[ index].Start();
  373.  if ( this.m_Current != index) return;
  374.  this.m_Current++;
  375.  if ( this.m_Current >= this.m_ActionList.length) return;
  376.  this.m_TimerId = window.setTimeout( "actions." + this.m_Name+".Tick()", this.m_DelayList[ this.m_Current]);
  377. }
  378.  
  379.  
  380. //------------------------------------
  381.  
  382. function EmailAction( to,subject,text) {
  383.  
  384.  this.m_To      = to;
  385.  this.m_Subject = subject;
  386.  this.m_Text    = text;
  387.  
  388.  this.Start = EmailAction_Start;
  389. }
  390.  
  391. function EmailAction_Start() {
  392.  
  393.  window.location.href = "mailto:" + this.m_To + "?Subject=" + this.m_Subject + "&Body=" + this.m_Text;
  394. }
  395.  
  396. //------------------------------------
  397.  
  398. function HttpAction( url,innewwindow) {
  399.  
  400.  this.m_Url         = url;
  401.  this.m_InNewWindow = innewwindow;
  402.  
  403.  this.Start = HttpAction_Start;
  404. }
  405.  
  406. function HttpAction_Start() {
  407.  
  408.  if ( this.m_InNewWindow) 
  409.    window.open( this.m_Url,"","menubar,toolbar,status,location",false);
  410.  else
  411.    window.location = this.m_Url;
  412. }
  413.  
  414. //------------------------------------
  415.  
  416. function StartAction( action) {
  417.  
  418.  this.m_Action = action;
  419.  
  420.  this.Start = StartAction_Start;
  421. }
  422.  
  423. function StartAction_Start() {
  424.  
  425.  eval( this.m_Action);
  426. }
  427.  
  428.  
  429.