/* * CSOptionsMenuPanel.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 Microsoft.Xna.Framework.Graphics; using System.IO; using Util; namespace ColonySim.GUI { /// /// Represents the option panel for the Colony Simulation. /// class CSOptionsMenuPanel : GUIPanel { #region Fields // The Save Button GUIButton mSaveButton = new GUIButton(); // The Load Button GUIButton mLoadButton = new GUIButton(); // The Delete Button GUIButton mDeleteButton = new GUIButton(); // The GUI list for the saved games we currently have GUIList mSavedGamesList = new GUIList(); // The save / load game text box GUITextInput mSaveLoadText = new GUITextInput(); // The array of current save games in the Colony Sim List mCurrentSaveGames = new List(); #endregion #region Properties /// /// Gets the text for the save string. /// public string GetSaveString { get { return mSaveLoadText.Text; } } public string SetSaveString { set { mSaveLoadText.Text = value; } } /// /// Gets the string to load the saved game. /// public string GetLoadString { get { // Set the value to null string retVal = null; // If the selected index is not null if (mSavedGamesList.SelectedLabel != null) { retVal = mSavedGamesList.SelectedLabel.Text; } // Return return retVal; } } #endregion #region Constructrs /// /// Base constructor. /// public CSOptionsMenuPanel() { // Add the components this.Add(mSaveButton); this.Add(mLoadButton); this.Add(mDeleteButton); this.Add(mSavedGamesList); this.Add(mSaveLoadText); } #endregion #region Initialization /// /// Initialize the panel. /// public override void Initialize() { // Base call base.Initialize(); // Set the save button and load button events mSaveButton.ButtonPressed += new EventHandler(mSaveButton_ButtonPressed); mLoadButton.ButtonPressed += new EventHandler(mLoadButton_ButtonPressed); mDeleteButton.ButtonPressed += new EventHandler(mDeleteButton_ButtonPressed); // If thew directory does not exist if(Directory.Exists(GameScreen.GlobalRootStorageDirectory + UserProfileUtility.OutputDirectory + UserProfileUtility.SaveDirectory)) { // Get all of the csg files in the folder string[] files = Directory.GetFiles(GameScreen.GlobalRootStorageDirectory + UserProfileUtility.OutputDirectory + UserProfileUtility.SaveDirectory); foreach (string file in files) { // If the file ends in .csg if (file.Contains(".csg") && file.IndexOf('.') == file.Length - 4) { // Get the file string saveFile = file.Substring(file.LastIndexOf("\\") + 1); // Add it to the list of save games mCurrentSaveGames.Add(saveFile.Substring(0, saveFile.Length - 4)); } } } // Add the list of saved games mSavedGamesList.Add(mCurrentSaveGames); // Add an event to the saved games list mSavedGamesList.SelectionChanged += new EventHandler(mSavedGamesList_ListSelectionEvent); } /// /// Load content for the model. /// public override void LoadContent() { // Base call base.LoadContent(); // get the font SpriteFont font = ParentScreen.Content.Load(@"Fonts\Courier New"); // Set the text for the buttons mSaveButton.Font = font; mSaveButton.Text = "SAVE"; mSaveButton.ToolTipText = "Save the Current Game to File"; mLoadButton.Font = font; mLoadButton.Text = "LOAD"; mLoadButton.ToolTipText = "Load the Last Saved Game"; mDeleteButton.Font = font; mDeleteButton.Text = "DELETE"; mDeleteButton.ToolTipText = "Delete the Currently Selected Save Game"; // Set the background color for the list and the text color mSavedGamesList.BackgroundColor = new Color(0, 0, 0, 150); mSavedGamesList.TextColor = Color.White; // set the text, if it's there if (mSavedGamesList.SelectedLabel != null) { mSaveLoadText.Text = mSavedGamesList.SelectedLabel.Text; } // Resize this.Resize(); } /// /// Resize the internal components for the panel. /// public override void Resize() { // Base call base.Resize(); // Set the positions and the sizes // The saved games list mSavedGamesList.Size = new Microsoft.Xna.Framework.Vector2(Size.X - 12, Size.Y * 13 / 16 - 6); mSavedGamesList.Position = new Microsoft.Xna.Framework.Vector2( Bounds.Left + 6, Bounds.Top + 6); // The save load text box mSaveLoadText.Size = new Microsoft.Xna.Framework.Vector2(Size.X - 12, Size.Y / 16 - 6); mSaveLoadText.Position = new Microsoft.Xna.Framework.Vector2( Bounds.Left + 6, mSavedGamesList.Bounds.Bottom + 3); // Saving and loading mSaveButton.Size = new Microsoft.Xna.Framework.Vector2(Size.X / 3 - 6, Size.Y * 2 / 16 - 6); mSaveButton.Position = new Microsoft.Xna.Framework.Vector2( Bounds.Left + 6, mSaveLoadText.Bounds.Bottom + 3); mLoadButton.Size = new Microsoft.Xna.Framework.Vector2(Size.X / 3 - 6, Size.Y * 2 / 16 - 6); mLoadButton.Position = new Microsoft.Xna.Framework.Vector2( mSaveButton.Bounds.Right + 3, mSaveLoadText.Bounds.Bottom + 3); mDeleteButton.Size = new Microsoft.Xna.Framework.Vector2(Size.X / 3 - 6, Size.Y * 2 / 16 - 6); mDeleteButton.Position = new Microsoft.Xna.Framework.Vector2( mLoadButton.Bounds.Right + 3, mSaveLoadText.Bounds.Bottom + 3); } #endregion #region Events /// /// Event for saving the current game. /// public event EventHandler SaveEvent; /// /// Event for loading the current game. /// public event EventHandler LoadEvent; /// /// The event that occurs when an item is selected inside of the list. /// void mSavedGamesList_ListSelectionEvent(object sender, EventArgs e) { // Set the current text in the save text if (mSavedGamesList.SelectedLabel != null) { // Set the text mSaveLoadText.Text = mSavedGamesList.SelectedLabel.Text; } } /// /// The Save Button was pressed, hence we should save the game to file. /// void mSaveButton_ButtonPressed(object sender, EventArgs e) { // If the save string isn't null if (GetSaveString != null && GetSaveString.Trim().Length > 0) { // Execute the save event SaveEvent.Invoke(this, EventArgs.Empty); // The testing value bool test = true; // Go through the list of the current names foreach (string str in mSavedGamesList.Items) { // If the string is in there if (str == GetSaveString) { // break out and fail test = false; break; } } // If true if (test) { // Add to the saved games list mSavedGamesList.Add(GetSaveString); } } } /// /// The Load Button was pressed, hence we should load the game from file. /// void mLoadButton_ButtonPressed(object sender, EventArgs e) { // If the save string isn't null if (GetLoadString != null && GetLoadString.Trim().Length > 0) { // Execute the save event LoadEvent.Invoke(this, EventArgs.Empty); } } /// /// The delete button was pressed. Time to delete the current save'd game. /// void mDeleteButton_ButtonPressed(object sender, EventArgs e) { // Get the current selected file to be loaded if (GetLoadString != null && GetLoadString.Trim().Length > 0) { // Set the file string fileToDelete = GameScreen.GlobalRootStorageDirectory + UserProfileUtility.OutputDirectory + UserProfileUtility.SaveDirectory + GetLoadString + ".csg"; // Delete the file if it's there if (File.Exists(fileToDelete)) { // Delete it File.Delete(fileToDelete); // Remove the entry from the list mCurrentSaveGames.Remove(GetLoadString); // Remove it from the view list mSavedGamesList.Remove(mSavedGamesList.SelectedLabel); } } } #endregion } }