home *** CD-ROM | disk | FTP | other *** search
/ CyberMycha 2003 October / cmycha200310.iso / NHL2004 / NHL2004Demo.exe / fe / nhl / htc / conset / controller.htc next >
Text File  |  2003-08-20  |  11KB  |  400 lines

  1. <!-- Copyright (c) 2002 Electronic Arts Inc. All rights reserved. -->
  2.  
  3. <PROPERTY NAME="playerId"     />
  4. <PROPERTY NAME="targets"        />
  5. <METHOD   NAME="moveTo"         />
  6. <METHOD   NAME="cancelPosition"  />
  7. <METHOD   NAME="moveToPlayer" />
  8. <METHOD   NAME="moveToPlayerWithEvent" />
  9. <METHOD   NAME="snapToGrid"     />
  10. <METHOD   NAME="sliceTargetsHome"     />
  11. <METHOD   NAME="sliceTargetsAway"     />
  12. <METHOD   NAME="toggleArrowVisibility"     />
  13. <METHOD   NAME="DoSetController"     />
  14. <METHOD   NAME="resetPosition"     />
  15. <EVENT    NAME="ondrag"           ID="drag"      />
  16. <EVENT    NAME="ondragstart"      ID="dragstart" />
  17. <EVENT    NAME="ondragend"        ID="dragend"   />
  18. <EVENT    NAME="onerror"          ID="error"     />
  19. <ATTACH   EVENT="onmouseup"       HANDLER="DoMouseUp"   />
  20. <ATTACH   EVENT="onmousedown"     HANDLER="DoMouseDown" />
  21. <ATTACH   EVENT="onselectstart"   HANDLER="DoSelect"    />
  22. <ATTACH   EVENT="ondocumentready" HANDLER="DoInit"      />
  23. <ATTACH   EVENT="onmouseover"     HANDLER="DoMouseOver" />
  24.  
  25. <SCRIPT LANGUAGE="jscript">
  26. var iOffsetX;                       // On the dragstart event, this variable is
  27.                                     //    set to track the difference between the
  28.                                     //    mouse position and the corner of the
  29.                                     //    element
  30.  
  31. var iOffsetY;                       // Same as iOffsetX, but for Y coordinate
  32.  
  33. var normZindex = style.zIndex;      // Tracks the regular zIndex so it can be
  34.                                     //    restored once the dragend event occurs
  35.  
  36. var zBound = new Array              // Used for parsing the mvBoundary prop
  37.     ('Top', 'Right',                //     into it's four component parts 
  38.     'Bottom', 'Left');
  39.  
  40. var aTargets = new Array();
  41.  
  42. function DoInit()
  43. {
  44.     if(targets != null)
  45.     {        
  46.         var aTemp = targets.toString().split(',');
  47.         for( var index =0 ; index < aTemp.length ; index++)
  48.         {
  49.             aTargets.push(window.document.all[aTemp[ index ]]);            
  50.         }
  51.     }
  52.     else
  53.     {
  54.         ReturnError("Error: targets property not set");
  55.     }
  56.     this.setAttribute("startX", element.offsetLeft);
  57.     this.setAttribute("startY", element.offsetTop);
  58.     SetDefaults();
  59. }
  60.  
  61. function sliceTargetsHome()
  62. {
  63.     aTargets = aTargets.slice(6,12);
  64. }
  65.  
  66. function sliceTargetsAway()
  67. {
  68.     aTargets = aTargets.slice(0,6);
  69. }
  70.  
  71. /*
  72. description : returns index for given target name
  73. args: target name, ie home_0
  74. returns : -1 if none or index
  75. */
  76.  
  77. function getIndex( sTargetName )
  78. {
  79.     var targetIndex = -1;
  80.     for( var index =0 ; index < aTargets.length ; index ++ )
  81.     {
  82.         if( aTargets[ index ].id == sTargetName )
  83.         {
  84.             targetIndex = index;
  85.             break;
  86.         }
  87.     }
  88.     return targetIndex;
  89. }
  90.  
  91. function SetDefaults()
  92. {
  93.     //  Required CSS properties    
  94.     style.left = offsetLeft;
  95.     style.top = offsetTop;
  96.     style.position = "absolute";
  97. }
  98.  
  99. function CustomDefault(sCSSName, sScriptName, sDefault)
  100. {
  101.     if (currentStyle[sCSSName] == null)
  102.     {
  103.         style[sCSSName] = sDefault;
  104.     }
  105.     else style[sCSSName] = currentStyle[sCSSName];
  106.     
  107.     style[sScriptName] = style[sCSSName];
  108. }
  109.  
  110. //constant for left button event id
  111. var LEFT_BUTTON = 1;
  112.  
  113. function DoMouseDown()
  114. {
  115.     if (event.button == LEFT_BUTTON)
  116.     {
  117.         //  Capture the mouse
  118.         setCapture();
  119.         
  120.         //  Set the zIndex to 1000 to put it over other elements while it's moved
  121.         style.zIndex = 1000;
  122.     
  123.         //hide arrows
  124.         toggleArrowVisibility( false );
  125.         //
  126.         //  Determine the difference between the mouse click on the element and
  127.         //  the top left corner    
  128.         //
  129.         iOffsetX = window.event.x - element.style.pixelLeft;
  130.         iOffsetY = window.event.y - element.style.pixelTop;
  131.     
  132.         //  Start tracking the mousemove
  133.         attachEvent ("onmousemove", DoMouseMove);        
  134.         oAudioInterface.PlayDragDropClickSFX();
  135.         dragstart.fire();        
  136.     }
  137. }    
  138.  
  139. function DoMouseMove()
  140. {
  141.     if (event.button == LEFT_BUTTON)
  142.     {
  143.         style.pixelTop = event.y - iOffsetY;
  144.         style.pixelLeft = event.x - iOffsetX;
  145.         drag.fire();
  146.     }
  147. }
  148.  
  149. function DoMouseUp()
  150. {
  151.     if (event.button == LEFT_BUTTON)
  152.     {
  153.         //  Return the zIndex to its previous value
  154.         style.zIndex = normZindex;
  155.     
  156.         //  Stop tracking the onmousemove event
  157.         detachEvent ("onmousemove", DoMouseMove);
  158.         
  159.         //  Release the mouse
  160.         releaseCapture();
  161.        
  162.        /*leaving a player*/   
  163.         if(this.playerId !=null)
  164.         {    
  165.         removeController(this.playerId, this.uniqueID);
  166.         }
  167.        
  168.         var controllerOverPlayer = player = checkPosition();
  169.         var playerUsesThisController = playerHasThisController();
  170.         
  171.         /*on top of a target object && target obj has no controllers*/    
  172.         if(player != -1 && ! playerHasAController( player ))
  173.         {   
  174.             addController( player , this.uniqueID );
  175.         }
  176.       
  177.         //on top of a target obj with an existing controller  
  178.         else if(player != -1 && playerHasAController( player ))
  179.         {        
  180.             //if(playerUsesThisController != -1)removeController(playerUsesThisController, this.uniqueID ); 
  181.             addController( player , this.uniqueID );        
  182.     
  183.         }
  184.         else if(player == -1)
  185.         {        
  186.             resetPosition();
  187.         }
  188.         dragend.fire();        
  189.         oAudioInterface.PlayDragDropReleaseSFX();
  190.     }
  191. }
  192.  
  193.  
  194.  
  195. function DoSetController(nMove)
  196. {
  197.    /*leaving a player*/
  198.     if(this.playerId !=null)
  199.     {
  200.         removeController(this.playerId, this.uniqueID);
  201.     }
  202.    
  203.     var controllerOverPlayer = player = checkPosition();
  204.     var playerUsesThisController = playerHasThisController();
  205.     
  206.     /*on top of a target object && target obj has no controllers*/    
  207.     if(player != -1 && ! playerHasAController( player-nMove ))
  208.     {           
  209.         addController( player-nMove , this.uniqueID );
  210.         toggleArrowVisibility(false);
  211.     }
  212.     //on top of a target obj with an existing controller  
  213.     else if(player != -1 && playerHasAController( player-nMove ))
  214.     {        
  215.         addController( player-nMove , this.uniqueID );        
  216.         toggleArrowVisibility(false);
  217.     }
  218.     else if(player == -1)
  219.     {        
  220.         resetPosition();
  221.     }
  222. }
  223.  
  224.  
  225.  
  226.  
  227. /*
  228. description : checks target object's controller attr.
  229. args : index
  230. returns : true/false
  231. */
  232. function playerHasAController( index )
  233. {
  234.     var bController = false;    
  235.     if(aTargets[ index ].getAttribute("newController") != null) bController = true;
  236.     return bController;
  237. }
  238.  
  239. /*
  240. description : does the target object already have this controller?
  241. args : none
  242. returns : -1 if no controller, otherwise target index
  243.  
  244. */
  245.  
  246. function playerHasThisController()
  247. {
  248.     bController = -1;
  249.  
  250.     for( var index = 0 ; index < aTargets.length ; index ++ )
  251.     {
  252.         if(aTargets[ index ].getAttribute("newController") != null) 
  253.         {
  254.             if(aTargets[ index ].getAttribute("newController") == this.uniqueID)
  255.             {
  256.                 bController = index;
  257.                 break;
  258.             }
  259.         }  
  260.     }
  261.     return bController;
  262. }
  263.  
  264. /*
  265. description: moves controller back to original position
  266. args: none
  267. returns : nothing
  268. */
  269. function resetPosition()
  270. {
  271.     style.pixelTop = this.startY;
  272.     style.pixelLeft = this.startX;
  273.     toggleArrowVisibility( true );
  274. }
  275.  
  276. function toggleArrowVisibility( bVisible )
  277. {
  278.     var sElementId                     = returnObjectId( element.id );
  279.     var oArrowLeft                  = element.document.all( "arrow_" + sElementId + "_left");
  280.     var oArrowRight                 = element.document.all( "arrow_" + sElementId + "_right");
  281.  
  282.     if( bVisible )
  283.     {
  284.         oArrowLeft.style.visibility     = 'visible';
  285.         oArrowRight.style.visibility     = 'visible';
  286.  
  287.         if (!oGameData.CanSelectSide(0))
  288.             oArrowRight.style.visibility     = 'hidden';        
  289.         else if (!oGameData.CanSelectSide(1))
  290.             oArrowLeft.style.visibility     = 'hidden';        
  291.     }
  292.     else
  293.     {
  294.         oArrowLeft.style.visibility         = 'hidden';
  295.         oArrowRight.style.visibility         = 'hidden';    
  296.     }
  297.  
  298. }
  299.  
  300. function moveToPlayer( sTargetName )
  301. {   
  302.  
  303.     this.playerId = sTargetName;
  304.         
  305.     var oTarget = aTargets[ getIndex( sTargetName )];
  306.     (oTarget.id.indexOf('away') != -1) ? moveTo( oTarget.offsetLeft , oTarget.offsetTop ) : moveTo( oTarget.offsetLeft , oTarget.offsetTop );
  307. }
  308.  
  309. function moveTo( nLeft , nTop )
  310. {
  311.     this.style.pixelLeft = nLeft;
  312.     this.style.pixelTop = nTop;
  313. }
  314.  
  315.  
  316. /*
  317. description: adds/emoves a controller property to target object. value of property is uniqueID of dragged object
  318. args : index
  319. returns: nothing
  320. */
  321. function addController( index , value )
  322. {
  323.     //alert("adding myself (" + this.element.id +") to : " + aTargets[ index ].id)
  324.     aTargets[ index ].setAttribute("newController", value );    
  325.     aTargets[ index ].hasController = "true";
  326. }
  327.  
  328. function removeController( index , value )
  329. {
  330.    //alert("removing myself (" + this.element.id +") from: " + index)      
  331.     var targetIndex = getIndex( index );    
  332.     aTargets[ targetIndex ].setAttribute("oldController", value );
  333.     aTargets[ targetIndex ].hasController = "false";
  334.     this.playerId = null;
  335. }
  336.  
  337. function ReturnError(sMsg)
  338. {
  339.     var oEvent = createEventObject();
  340.     oEvent.setAttribute("error", sMsg);
  341.     error.fire(oEvent);
  342. }
  343.  
  344. /*
  345. description : checks to see if over target element(s)
  346. args : none
  347. returns: -1 if not above target el, otherwise returns target index
  348. */
  349. function checkPosition()
  350. {
  351.     var targetIndex = -1;
  352.  
  353.     for( var index = 0 ; index < aTargets.length ; index ++)
  354.     {        
  355.         var oTarget = aTargets[ index ];
  356.  
  357.         var withinXBoundary=((this.offsetLeft < oTarget.offsetLeft) && ((this.offsetLeft + this.offsetWidth) >= oTarget.offsetLeft) || ((this.offsetLeft >= oTarget.offsetLeft) && (this.offsetLeft <= (oTarget.offsetLeft + oTarget.offsetWidth))));
  358.         var withinYBoundary=((this.offsetTop < oTarget.offsetTop) && ((this.offsetTop + this.offsetHeight) >= oTarget.offsetTop) || ((this.offsetTop >= oTarget.offsetTop) && (this.offsetTop <= (oTarget.offsetTop + oTarget.offsetHeight))));    
  359.         
  360.        if(withinXBoundary && withinYBoundary)
  361.         {
  362.             targetIndex = index;
  363.             break;
  364.         }
  365.     }
  366.     return targetIndex;
  367. }
  368.  
  369. /*
  370. description : changes cursor
  371. args : cursor type. possible values : up / down / over
  372. returns : nothing
  373. */
  374. function setCursor( sType )
  375. {
  376.     switch( sType )
  377.     {
  378.         case "up" :
  379.            this.style.cursor = "hand"; 
  380.             break;
  381.         
  382.         case "down" :
  383.             this.style.cursor = "move";
  384.             break;
  385.         case "over" :
  386.         this.style.cursor = "";
  387.             this.style.cursor = "hand";
  388.             break;            
  389.        
  390.     }
  391.     window.status =this.style.cursor;
  392. }
  393.  
  394. function DoMouseOver()
  395. {
  396.     setCursor( "over" );
  397. }
  398.  
  399. </SCRIPT>
  400.