/*
* ParticleGame.cs
* Authors: Brian Murphy, Adam Nabinger
* 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.Threading;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using tAC_Engine.Graphics.Cameras;
using TACParticleEngine;
using TACParticleEngine.Particles;
using Util;
using Keys=Microsoft.Xna.Framework.Input.Keys;
namespace TACParticleEditor
{
///
/// The game screen used for displaying the current particle effect being modified
///
public class ParticleGame : GameScreen
{
#region Fields
// The current sprite batch used for rendering
private SpriteBatch spriteBatch;
// The particle editor form used for changing the current particle effect
private ParticleEditor particleEditor;
// The current camera being used for rendering
private Camera mActiveCamera;
// The particle manager to control all the particle effects
private ParticleManager mParticleManager;
// Lets the particle manager know that th particle effect reference has already been loaded in
private bool mAlreadyAdded;
///
/// Boolean used for setting up file loading
///
internal bool DoOpenFile;
///
/// Boolean used for setting up file saving
///
internal bool DoSaveFile;
#endregion
#region Initialization
///
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
///
public override void Initialize()
{
mAlreadyAdded = false;
particleEditor = new ParticleEditor(this);
new System.Threading.Thread(delegate()
{
Application.Run(particleEditor);
}).Start();
mActiveCamera = new Perspective3DCamera(new Vector3(0, 0, -5), Vector3.Zero, Vector3.Up, MathHelper.PiOver2, 1, 1, 100);
AutoClear(DrawOrder - 1);
ShowFPS();
base.Initialize();
}
#endregion
#region Management
///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
mParticleManager = new ParticleManager(Content, mActiveCamera, spriteBatch, GraphicsDevice.Viewport);
mParticleManager.DrawOrder = 1;
AddComponent(mParticleManager);
}
///
/// Disposes of any unmanaged components
///
/// Whether or not this is currently being disposed
protected override void Dispose(bool disposing)
{
if (disposing)
{
spriteBatch.Dispose();
}
base.Dispose(disposing);
}
///
/// Adds the given particle effect into the Particle Manager
///
/// The particle effect to add in
public void AddEffect(ParticleEngine pEngine)
{
mParticleManager.Add(pEngine, mAlreadyAdded);
if (!mAlreadyAdded)
{
mAlreadyAdded = true;
}
}
#endregion
#region Update
///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///
/// Provides a snapshot of timing values.
public override void Update(GameTime gameTime)
{
if (Microsoft.Xna.Framework.Input.Keyboard.GetState().IsKeyDown(Keys.Escape))
{
Exit();
}
if (Microsoft.Xna.Framework.Input.Keyboard.GetState().IsKeyDown(Keys.Enter))
{
if (particleEditor.ParticleEngine != null)
{
particleEditor.ParticleEngine.SetActive(true);
}
}
if (DoOpenFile)
{
StartOpenFile();
}
if (DoSaveFile)
{
StartSaveFile();
}
base.Update(gameTime);
}
#endregion
#region File Opening
///
/// Creates the thread that the SaveFileDialog object will run on
///
private void StartOpenFile()
{
DoOpenFile = false;
Thread ownThread = new Thread(OpenFile);
ownThread.SetApartmentState(ApartmentState.STA);
ownThread.Start();
}
///
/// Sets up a OpenFileDialog object to start file loading
///
private void OpenFile()
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "Particle Files|*.particle";
openFileDialog.Title = "Select a Particle File";
openFileDialog.Multiselect = false;
openFileDialog.CheckFileExists = true;
if (openFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
string file = openFileDialog.FileName;
if (particleEditor.ParticleEngine != null)
{
particleEditor.ParticleEngine.SetActive(false);
particleEditor.ParticleEngine.ToDestory = true;
Content.Unload();
}
if (file.Contains(@"\Content\"))
{
Content.RootDirectory = file.Substring(0, file.LastIndexOf(@"\Content\") + 9);
}
else
{
Content.RootDirectory = file.Substring(0, file.LastIndexOf('\\'));
}
file = file.Substring(Content.RootDirectory.Length, file.Length - (Content.RootDirectory.Length + 9));
particleEditor.ProcessOpenFile(mParticleManager.Load(file));
}
}
#endregion
#region File Saving
///
/// Creates the thread that the SaveFileDialog object will run on
///
private void StartSaveFile()
{
DoSaveFile = false;
Thread ownThread = new Thread(SaveFile);
ownThread.SetApartmentState(ApartmentState.STA);
ownThread.Start();
}
///
/// Sets up a SaveFileDialog object to start file saving
///
private void SaveFile()
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.CheckFileExists = false;
saveFileDialog.OverwritePrompt = false;
saveFileDialog.Filter = "Particle Files|*.particle";
saveFileDialog.Title = "Select a Particle File";
saveFileDialog.DefaultExt = ".particle";
saveFileDialog.AddExtension = true;
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
particleEditor.ParticleEngine.SaveToFile(saveFileDialog.FileName);
}
#endregion
}
}