TGB Strategy Mouse selection cs

From TDN

// --------------------------------------------------------------------
// singleSelection()
//
// This function handles when an individual object is selected,
// the position of the selection is also passed
// --------------------------------------------------------------------
function singleSelection(%pos)
{
   // lets grab the single object in our Selections container
   %obj = Selections.getObject(0);
   
   // now lets call the objects own .onSelected() callback passing
   // it the %pos
   %obj.onSelected(%pos);
}

// --------------------------------------------------------------------
// t2dSceneObject::onSelected()
//
// This is the function all objects pass through when selected
// it places the description text that comes directly from the object
// and places it into a temporary GUI that gets attached to the 
// description section of the Object Properties box
// --------------------------------------------------------------------
function t2dSceneObject::onSelected(%this, %pos)
{

}

// --------------------------------------------------------------------
// multipleSelections()
//
// This function handles when multiple selections are selected,
// the position of the selection is also passed
// --------------------------------------------------------------------
function multipleSelections(%pos)
{
   // get the count of selections
   %count = Selections.getCount();
   
   // set the extent of the buttons to be generated
   %extent = "60 15";
   
   // init the SelectionMenu
   // this will generate all the positions needed to place our buttons
   initSelectionMenu(%extent, %count);
   
   // here we loop through the selections and call the function to generate the
   // proper selection button in the selection box
   for(%i=0;%i<%count;%i++)
   {
      // store the selection in a local variable
      %obj = Selections.getObject(%i);
   
      // here we add the button by passing all the needed values to the function
      addSelectionMenuButton(%extent, %obj, %pos, %i);         
   }
}

// --------------------------------------------------------------------
// initSelectionMenu()
//
// This function handles the calculations for dynamically configuring the
// ammount of rows and columns needed to tile our selection buttons inside
// whatever selection menu we use.  It stores all of the positions to place
// the buttons so when they need to be placed its a snap
// --------------------------------------------------------------------
function initSelectionMenu(%buttonExtent, %count)
{
   // we get the extent, width, and height of the selection box we will add buttons to
   %extent = SelectionMenu.getExtent();
   %width = getWord(%extent, 0);
   %height = getWord(%extent, 1); 
   
   // divide up the extent into width and height
   %buttonWidth = getWord(%buttonExtent, 0);
   %buttonHeight = getWord(%buttonExtent, 1);
   
   // button height plus padding
   %calcHeight = %buttonHeight + 1;
   
   // calculation of buttons per column rounded down
   %buttonsPerColumn = mFloor(%height / %calcHeight);
   
   // calculation of how many colums it will take to hold the buttons rounded up
   %columns = mCeil(%count / %buttonsPerColumn);
   
   // calculate the max columns
   %maxColumns = mFloor(%width / (%buttonWidth + 5));

   // we have too many buttons to fit
   if(%columns > %maxColumns)
      error("too many buttons to fit");
      
   // store the max values of how many columns and rows we need
   SelectionMenu.columnMax = %columns;
   SelectionMenu.rowMax = %buttonsPerColumn;
   
   // start the first column and row with a little buffer
   SelectionMenu.xPos[0] = "5";
   SelectionMenu.yPos[0] = "5";
   
   // we loop through each of the columns needed and generate the column locations
   for(%i=1;%i<%columns;%i++)
   {
      SelectionMenu.xPos[%i] = SelectionMenu.xPos[%i-1] + %buttonWidth + 5;  
   }
   
   // we loop through each of the rows needed and generate the row locations
   for(%i=1;%i<%buttonsPerColumn;%i++)
   {
      SelectionMenu.yPos[%i] = SelectionMenu.yPos[%i-1] + %buttonHeight + 5;   
   }
   
   // we reset the counters we will use to populate the property window with buttons
   SelectionMenu.columnCount = 0;
   SelectionMenu.rowCount = 0;
}

// --------------------------------------------------------------------
// addSelectionMenuButton()
//
// Since we have all the positions calculated in initSelectionMenu()
// this function just grabs that position for the next button in line
// and generates a gui cutton control for it and adds it to the proper 
// GuiControl, it also does all the proper incrementing
// --------------------------------------------------------------------
function addSelectionMenuButton(%extent, %obj, %pos, %val)
{
   // first lets grab the x and the y position for this button based on its 
   // column and row position
   %xPos = SelectionMenu.xPos[SelectionMenu.columnCount];
   %yPos = SelectionMenu.yPos[SelectionMenu.rowCount];
   
   // lets grab the object's name
   %name = %obj.name;//getSelectionName();
   
   // new we generate the action gui button control, inputing the correct name,
   // command (function to be called when clicked), position, and extent
   new GuiButtonCtrl(%name @ %val) { 
      profile = "GuiButtonSelectionProfile";
      text = %name; 
      command = "selectionInMultiple(" @ %obj @ ", \"" @ %pos @ "\");";
      position = %xPos SPC %yPos;
      extent = %extent;
   };
   
   // then we add the button we just generated to the control 
   SelectionMenu.add(%name @ %val);
   
      // incriment the row count
   SelectionMenu.rowCount++;

   // here we check if the rowcount should trigger the increment of the next column
   if(SelectionMenu.rowCount >= SelectionMenu.rowMax)
   {
      SelectionMenu.rowCount = 0;
      SelectionMenu.columnCount++;      
   }
}

// --------------------------------------------------------------------
// selectionInMultiple()
//
// This is a small function used to translate when you click a button
// in the selection box after a multiple selection, this way it converts 
// it to a basic single selection
// --------------------------------------------------------------------
function selectionInMultiple(%obj, %pos)
{
   // lets do a border border and selection reset
   resetBorderBoxes();

   // lets create a border for our single selected object
   createBorderBox(%obj);

   // clear out all of our selections
   Selections.clear();

   // lets re-add our single selected object
   Selections.add(%obj);

   // then we pass the object and position to the single selection function
   singleSelection(%pos);   
}

// --------------------------------------------------------------------
// resetBorderBoxes()
//
// This function will reset the selected object's border boxes as well
// as reseting the SelectionMenu buttons
// --------------------------------------------------------------------
function resetBorderBoxes()
{
   // first lets clear out our border boxes
   clearBorderBoxes();
   
   // now lets clear out all the SelectionMenu items
   SelectionMenu.clearItems();
}


// --------------------------------------------------------------------
// GuiControl::clearItems()
//
// This function can be called on any gui control, it will loop through
// each of the control's items within it and remove and then 'delete' it
// --------------------------------------------------------------------
function GuiControl::clearItems(%this)
{
      // first we grab the count of all objects
   %count = %this.getCount();
   
   // check if there are no objects within this Gui Control
   if(%count < 1)
      return;
      
   // we then loop through each object in reverse order
   for(%i=%count-1;%i>=0;%i--)
   {
      // store the object in a local variable
      %obj = %this.getObject(%i);
      // we remove the object
      %this.remove(%obj);
      // then we delete the object to ensure we don't waste memory
      // NOTE: this is a gui object so we use .delete() and not safeDelete()
      %obj.delete();     
   }
}