TGB Strategy Mouse mouse cs

From TDN

// --------------------------------------------------------------------
// initMouse()
//
// Here we set the initial values that the mouse settings and functions
// will need
// --------------------------------------------------------------------
function initMouse()
{
   // create the selection container object
   new SimSet(Selections);
   
   // this is the limit of the objects you can select
   $selectionLimit = 20;

   // create the selection box object that will be stretched with our
   // selection box lines
   createBox(SelectionBox);

   // create the selection box four line objects that will be positioned
   // in drawBox()
   createBox(LeftLine);
   createBox(RightLine);
   createBox(TopLine);
   createBox(BottomLine);

   // create a border box container object
   new SimSet(BorderBoxes);
}

// --------------------------------------------------------------------
// createBox()
//
// Here we create a static sprite that will be used as a line in the 
// selection box
// --------------------------------------------------------------------
function createBox(%name)
{
   // store the object
   %obj = new t2dStaticSprite(%name) { scenegraph = $strategyScene; };
   // set the object to enabled FALSE
   %obj.setEnabled(false);
   // set the image map for the border piece
   %obj.setImageMap(borderImageMap);
   // set the objects layer to the first
   %obj.setLayer(0);

   // return the object to be further manipulated
   return %obj;
}

// --------------------------------------------------------------------
// updateSelectionBox()
//
// This function will update the selection box to the position the mouse
// is dragging... this function will take the positions and order them
// correctly for the drawBox() function so the box will draw in all
// quadrants
// --------------------------------------------------------------------
function updateSelectionBox(%pos)
{
   // lets reset our selection box(s) first
   resetSelectionBoxes();
   // first lets grab the stored clicked spot
   %corner = $selectionBox::corner; 
   // grab both positions for calculations
   %posX = getWord(%pos, 0);
   %posY = getWord(%pos, 1);
   %cornerX = getWord(%corner, 0);
   %cornerY = getWord(%corner, 1);
 // we do some checks to make sure the selection box will draw correctly
   if(%posX < %cornerX)
   {
      %tempX = %posX;
      %posX = %cornerX;
      %cornerX = %tempX; 
   }
   
   if(%posY < %cornerY)
   {
      %tempY = %posY;
      %posY = %cornerY;
      %cornerY = %tempY; 
   }
   // peice the positions back together, without the previous step the box will 
   // only draw correctly in one quadrant
   %pos = %posX SPC %posY;
   %corner = %cornerX SPC %cornerY;
   // time to do some vector calculations to determine the center spot between both points
   %vecBetween = t2dVectorSub(%pos, %corner);
   %halfVec = t2dVectorScale(%vecBetween, 0.5);
   %finalVec = t2dVectorAdd(%halfVec, %corner);
   // uncomment this to allow an image to be placed inside the border box 

   // now we finaly set the position and size of the selection box
   //SelectionBox.setPosition(%finalVec);
   //SelectionBox.setSize(%vecBetween);

   // call the drawBox command passing the new starting and ending corner position
   drawBox(%corner, %pos, %vecBetween);
}

// --------------------------------------------------------------------
// drawBox()
//
// This function will draw the correct selection box when passed
// the proper positions... the positions are ordered in updateSelectionBox()
// and passed to this function
// --------------------------------------------------------------------
function drawBox(%corner, %pos, %vecBetween)
{
   // we grab the x and y positions to get the width and height
   %x = getWord(%vecBetween, 0);
   %y = getWord(%vecBetween, 1);
   // divide up the starting corner position
   %cornerX = getWord(%corner, 0);
   %cornerY = getWord(%corner, 1);

   // divide up the ending corner position
   %posX = getWord(%pos, 0);
   %posY = getWord(%pos, 1);
   
   // specify a line thickness
   %lineThickness = "0.5";
   
   // left line
   // assign a box line object and set its size appropriately
   %obj = LeftLine;
   %obj.setSize(%lineThickness SPC %y);
   
   // calculate the center position needing to be set
   %leftY = %cornerY + (%y/2);
   %leftX = %cornerX + %lineThickness/2;

   // set the left line box position
   %obj.setPosition(%leftX SPC %leftY);
   
   // right line
   // assign a box line object and set its size appropriately
   %obj = RightLine;
   %obj.setSize(%lineThickness SPC %y);

   // calculate the center position needing to be set
   %rightY = %cornerY + (%y/2);
   %rightX = %cornerX + %x + %lineThickness/2;

   // set the right line box position
   %obj.setPosition(%rightX SPC %rightY); 

   // top line
   // assign a box line object and set its size appropriately
   %obj = TopLine;
   %obj.setSize(%x + %lineThickness SPC %lineThickness);

   // calculate the center position needing to be set
   %topX = %cornerX + (%x/2) + (%lineThickness/2);
   %topY = %cornerY + %lineThickness/2;

   // set the top line box position
   %obj.setPosition(%topX SPC %topY);  

   // bottom line
   // assign a box line object and set its size appropriately
   %obj = BottomLine;
   %obj.setSize(%x + %lineThickness SPC %lineThickness);

   // calculate the center position needing to be set
   %bottomX = %cornerX + (%x/2) + (%lineThickness/2);
   %bottomY = %cornerY + %y + %lineThickness/2;

   // set the bottom line box position
   %obj.setPosition(%bottomX SPC %bottomY); 
}

// --------------------------------------------------------------------
// resetSelectionBoxes()
//
// This will reset the four objects that make up the lines of the
// selection box
// --------------------------------------------------------------------
function resetSelectionBoxes()
{  
   // this is for an image inside the selection box
   //%obj = SelectionBox;
   //%obj.setPosition("0 0");
   //%obj.setSize("0 0");
   //%obj.setEnabled(true); 

   // this is for the left line of the selection box border
   %obj = LeftLine;
   %obj.setPosition("0 0");
   %obj.setSize("0 0");
   %obj.setEnabled(true); 
   // this is for the right line of the selection box border
   %obj = RightLine;
   %obj.setPosition("0 0");
   %obj.setSize("0 0");
   %obj.setEnabled(true); 
   // this is for the top line of the selection box border
   %obj = TopLine;
   %obj.setPosition("0 0");
   %obj.setSize("0 0");
   %obj.setEnabled(true); 
   // this is for the bottom line of the selection box border
   %obj = BottomLine;
   %obj.setPosition("0 0");
   %obj.setSize("0 0");
   %obj.setEnabled(true); 
}

// --------------------------------------------------------------------
// disableSelectionBoxes()
//
// This will disable the selection boxes
// --------------------------------------------------------------------
function disableSelectionBoxes()
{
   // to disable the boxes we set them to enabled false and set their size to 
   // "0 0" so they are not visible (just in case we re-enable the incorrectly)

   %obj = SelectionBox;
   %obj.setEnabled(false);
   %obj.setSize("0 0");

   %obj = LeftLine;
   %obj.setEnabled(false);
   %obj.setSize("0 0"); 
   %obj = RightLine;
   %obj.setEnabled(false);
   %obj.setSize("0 0"); 
   %obj = TopLine;
   %obj.setEnabled(false);
   %obj.setSize("0 0"); 
   %obj = BottomLine;
   %obj.setEnabled(false);
   %obj.setSize("0 0");    
}

// --------------------------------------------------------------------
// SceneWindow2D::onMouseDown()
//
// This function is triggered from the engine when the mouse is "down"
// --------------------------------------------------------------------
function SceneWindow2D::onMouseDown(%this, %modifier, %worldPos, %mouseClicks)
{
   // store the position clicked for the corner position of
   // the selection box
   $selectionBox::corner = %worldPos;

   // clear our selected object border boxes
   resetBorderBoxes();
}

// --------------------------------------------------------------------
// SceneWindow2D::onMouseUp()
//
// This function is triggered from the engine when the mouse is "up"
// --------------------------------------------------------------------
function SceneWindow2D::onMouseUp(%this, %modifier, %worldPos, %mouseClicks)
{
   // get the selections passing the position the mouse was let up
   getSelections(%worldPos);
   // lets disable our selection boxes
   disableSelectionBoxes();
}

// --------------------------------------------------------------------
// SceneWindow2D::onMouseDragged()
//
// This function is triggered from the engine when the mouse is dragged
// --------------------------------------------------------------------
function SceneWindow2D::onMouseDragged(%this, %modifier, %worldPos, %mouseClicks)
{  
   // update the seletion box as we drag as well as check if the mouse
   // is on the border for camera panning and easing
   updateSelectionBox(%worldPos);
}

// --------------------------------------------------------------------
// getSelections()
//
// This function will grab the selections within the selection box
// --------------------------------------------------------------------
function getSelections(%pos)
{   
   // grab a list of objects within the rectangle of the selection box
   %objList = $strategyScene.pickRect($selectionBox::corner, %pos);
   // now we get the count of objects within the object list   
   %objCount = getWordCount(%objList);

   // clear the selections container object
   Selections.clear();

   // loop through the objects in the selection rectangle list
   for(%i=0;%i<%objCount;%i++)
   {
      // grab the looped object
      %obj = getWord(%objList, %i);
      
      // if the object isSelectable then we want to store it in the
      // Selections container object
      if(%obj.isSelectable)
      {
         Selections.add(%obj);
         
         // lets create a selected object border box
         createBorderBox(%obj); 

         // here we check to see if we have reached our selection limit
         if(Selections.getCount() >= $selectionLimit)
         {
            // if we have reached our limit then set %i to exit the loop
            %i = %objCount;
         } 
      }
   }

   // if we have more than one selection we want to call the multiple 
   // selections function and if we only have one we want to call the
   // single selection function
   if(Selections.getCount() > 1)
   {
      multipleSelections(%pos);      
   } else if(Selections.getCount() == 1)
   {
      singleSelection(%pos);
   }
}

// --------------------------------------------------------------------
// createBorderBox()
//
// We call this function passing a selected object and we create a selected
// object's border box sprite, mount it to it, size it, and store it properly
// --------------------------------------------------------------------
function createBorderBox(%obj)
{
   // lets get the ammount of border boxes presently created
   %count = BorderBoxes.getCount();
   
   // compare the count to the selectionLimit we have already set in our initMouse() function
   if(%count > $selectionLimit)
   {
      // we already reached our limit of border boxes so we won't add any more
      return;
   }
   
   // create a new static sprite
   %box = new t2dStaticSprite() { scenegraph = $strategyScene; };

   // set its image map to the border image
   %box.setImageMap(borderImageMap);
   
   // set its layer to the selected objects layer
   %box.setLayer(%obj.getLayer());
   
   // move it to the front of the layer
   $strategyScene.setLayerDrawOrder(%box, FRONT);
   
   //get the size numbers to set the border box 
   %size = %obj.getSize();
   %sizeX = getWord(%size, 0);
   %sizeY = getWord(%size, 1);
   
   // these are the values of how much taller and wider the selection box will be than
   // the selected object
   %taller = 0.25;
   %wider = 0.25;

   // mount the box to the selected object and size it slightly larger than it
   %box.mount(%obj);
   %box.setSize((%sizeX + %wider) SPC (%sizeY + %taller));
   
   // now we add this newly created border box to our SimSet for management
   BorderBoxes.add(%box);
}

// --------------------------------------------------------------------
// clearBorderBoxes()
//
// This function will clear all of our selected object's border boxes
// --------------------------------------------------------------------
function clearBorderBoxes()
{
   // lets get the ammount of border boxes presently created
   %count = BorderBoxes.getCount();
   
   // now lets loop through all of them
   for(%i=0;%i<%count;%i++)
   {
      // lets get the current border box
      %box = BorderBoxes.getObject(%i);
     
      // lets store the object in a temporary array
      %tempArray[%i] = %box;
   }
   
   // now that we've looped through our SimSet and stored all the objects
   // we can clear the simset
   BorderBoxes.clear();

   // then we can loop through our temp array and safeDelete() our border boxes
   for(%i=0;%i<%count;%i++)
   {
      // safe delete the selected object's border box sprite
      %tempArray[%i].safeDelete();
   }
}