/*
* CSStructureSelectionPanel.cs
* Authors: Bradley R. Blankenship
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Collections.Generic;
using System.Text;
using tAC_Engine.GUI;
using ColonySim.Structures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ColonySim.GUI;
namespace ColonySim.GUI
{
///
/// Represents the panel for selecting Mining structures.
///
class CSStructureSelectionPanel : GUIRadioContainer
{
#region Fields
// The array of buttons for the Basic Tubes
List mButtons = new List();
// The current selected structure
Structure mCurrentStructure;
#endregion
#region Properties
///
/// Gets the currently available structure to build.
///
public Structure CurrentStructure
{
get { return mCurrentStructure.Clone(); ; }
}
#endregion
#region Constructs
///
/// Base constructor
///
public CSStructureSelectionPanel()
{
}
#endregion
#region Initialize
///
/// Initialize all of the components and set their positions and sizes.
///
public override void Initialize()
{
// Base call
base.Initialize();
}
///
/// Resize the internals of the panel.
///
public override void Resize()
{
// Base call
base.Resize();
// Sort the buttons
SortButtons();
// Set the size of the buttons and positions
Vector2 position = Position;
foreach (CSStructureButton button in mButtons)
{
// Set the size
button.Size = new Vector2(Size.X, Size.Y);
button.Position = position;
position.X = button.Bounds.Right;
}
}
///
/// Sorts the buttons in the grid.
///
private void SortButtons()
{
// Go through each buttons and sort
mButtons.Sort(new StructureSorter());
}
///
/// Loads the content of the buttons.
///
public override void LoadContent()
{
// Base call
base.LoadContent();
}
#endregion
#region Events
public event EventHandler SelectionChanged;
public override bool HandleMouseEvent(Util.MouseInputEvent e)
{
// Get the retVal
bool retVal = base.HandleMouseEvent(e);
// Only if we received the event
if (retVal)
{
// Set the current structure based on the pressed button
foreach (CSStructureButton button in mButtons)
{
// If the button is down
if (button.State == GUIButton.ButtonState.DOWN)
{
// Set the current structure
mCurrentStructure = button.Structure;
//Invoke Event
if (SelectionChanged != null)
SelectionChanged.Invoke(this, EventArgs.Empty);
break;
}
}
}
// Return
return retVal;
}
#endregion
#region Collection
///
/// Add the given component to the container. For structure buttons,
/// perform special operations on their placement and allocation.
///
///
public override void Add(GUIComponent component)
{
// Base call
base.Add(component);
// If it's a structure button
if (component is CSStructureButton)
{
// Add to the mbuttons list
mButtons.Add(component as CSStructureButton);
}
}
#endregion
}
}
class StructureSorter : Comparer
{
///
/// Compares two structures and returns which one is correct in order.
///
public override int Compare(CSStructureButton pX, CSStructureButton pY)
{
// The return value
int retVal = 0;
// Get the structures
Structure x = pX.Structure;
Structure y = pY.Structure;
// Compare for residentials
if (x is Residential && y is Entertainment)
{
retVal = -1;
}
else if (x is Entertainment && y is Residential)
{
retVal = 1;
}
// Compare for commercials
if (x is MiningPlant && (y is Factory || y is Commercial) )
{
retVal = -1;
}
else if (x is Factory && y is MiningPlant)
{
retVal = 1;
}
else if (x is Factory && y is Commercial)
{
retVal = -1;
}
else if (x is Commercial && (y is MiningPlant || y is Factory))
{
retVal = 1;
}
// Return
return retVal;
}
}