#region Using Statements
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace Fusion_Control_Centre_Uber_XNA_Edition
{
public partial class Fusion_Control_Centre_Uber_XNA_Edition_Game : Microsoft.Xna.Framework.Game
{
public Fusion_Control_Centre_Uber_XNA_Edition_Game()
{
//CheckForAncientHardware();
try
{
XNA_FusionGraphicsDeviceManager = new GraphicsDeviceManager(this);
}
catch (NoSuitableGraphicsDeviceException)
{
//Video Card Not Supported
MessageBox.Show("Your Video Card is not supported by this application.\r\n" +
"This could be due to many things including incorrect, outdated ," +
"or missing video drivers, as well as trying to use this program " +
"through Remote Desktop where DirectX is not supported.");
}
}
#region Initializing Main Program
protected override void Initialize()
{
try
{
VerboseLog("Starting Main Program");
VerboseLog("Start--> Initializing Main Program Block");
base.Initialize();
CheckForAncientHardware();
SetUpXNADevice();
SetupTimers();
if (!ReadInConfigurationFile())
{
DisplayMessageBox("Unknown Error in Configuration File", true);
return;
}
InitializeTimers();
SetupRemoteControl();
SetupDebugging();
this.Exiting += new EventHandler(Fusion_Control_Centre_Uber_XNA_Edition_Game_Exiting);
VerboseLog("End--> Initializing Main Program Block");
}
catch (Exception)
{
//Error in main of initialization block
MessageBox.Show("Error in main of initialization block");
}
}
#endregion
#region Initialize XNA Devices
private void SetUpXNADevice()
{
VerboseLog("Start--> Initializing XNA Devices");
XNA_FusionGraphicsDevice = XNA_FusionGraphicsDeviceManager.GraphicsDevice;
XNA_FusionGraphicsDeviceManager.PreferredBackBufferWidth = 640;
XNA_FusionGraphicsDeviceManager.PreferredBackBufferHeight = 480;
XNA_FusionGraphicsDeviceManager.IsFullScreen = false;
XNA_FusionGraphicsDevice.SoftwareVertexProcessing = true;
XNA_FusionGraphicsDeviceManager.ApplyChanges();
Window.Title = "Fusion Control Centre Uber XNA Edition";
XNA_FusionSpriteBatchManager = new SpriteBatch(XNA_FusionGraphicsDevice);
XNA_Equivalent_UnderForm = (Form)Control.FromHandle(this.Window.Handle);
XNA_Equivalent_UnderForm.MouseDown += new MouseEventHandler(XNA_Equivalent_UnderForm_MouseDown);
XNA_Equivalent_UnderForm.MouseClick += new MouseEventHandler(XNA_Equivalent_UnderForm_MouseClick);
XNA_Equivalent_UnderForm.MouseMove += new MouseEventHandler(XNA_Equivalent_UnderForm_MouseMove);
this.IsMouseVisible = true;
VerboseLog("End--> Initializing XNA Devices");
}
#endregion
#region Ancient Hardware Check
public bool CheckForAncientHardware()
{
VerboseLog("Beginning Hardware Check");
ShaderProfile FusionRequirements = GraphicsAdapter.DefaultAdapter.GetCapabilities(DeviceType.Hardware).MaxPixelShaderProfile;
if (FusionRequirements < ShaderProfile.PS_1_1)
{
DisplayMessageBox("Computer does not support Pixel Shader version 1.1 or later for XNA processing", true);
return false;
}
return true;
VerboseLog("Ending Hardware Check");
}
#endregion
#region Default Update Timer
protected override void Update(GameTime gameTime)
{
//Nothing
}
#endregion
#region Underlying Form Mouse Click & Move Events
void XNA_Equivalent_UnderForm_MouseDown(object sender, MouseEventArgs e)
{
int CurrentMouse_x = e.X;
int CurrentMouse_y = e.Y;
foreach (DisplayButton CurrentDisplayButton in AllDisplayButtons)
{
if (CurrentMouse_x >= CurrentDisplayButton.XNA_x_left && CurrentMouse_x <= CurrentDisplayButton.XNA_x_right)
{
if (CurrentMouse_y >= CurrentDisplayButton.XNA_y_top && CurrentMouse_y <= CurrentDisplayButton.XNA_y_bottom)
{
if (e.Button == MouseButtons.Left)
{
CurrentDisplayButton.isBeingClicked = true;
return;
}
}
}
}
}
void XNA_Equivalent_UnderForm_MouseMove(object sender, MouseEventArgs e)
{
return;
int CurrentMouse_x = e.X;
int CurrentMouse_y = e.Y;
foreach (DisplayButton CurrentDisplayButton in AllDisplayButtons)
{
CurrentDisplayButton.isBeingClicked = false;
if (CurrentMouse_x >= CurrentDisplayButton.XNA_x_left && CurrentMouse_x <= CurrentDisplayButton.XNA_x_right)
{
if (CurrentMouse_y >= CurrentDisplayButton.XNA_y_top && CurrentMouse_y <= CurrentDisplayButton.XNA_y_bottom)
{
continue;
}
}
}
}
void XNA_Equivalent_UnderForm_MouseClick(object sender, MouseEventArgs e)
{
int CurrentMouse_x = e.X;
int CurrentMouse_y = e.Y;
foreach (DisplayButton CurrentDisplayButton in AllDisplayButtons)
{
CurrentDisplayButton.isBeingClicked = false;
if (CurrentMouse_x >= CurrentDisplayButton.XNA_x_left && CurrentMouse_x <= CurrentDisplayButton.XNA_x_right)
{
if (CurrentMouse_y >= CurrentDisplayButton.XNA_y_top && CurrentMouse_y <= CurrentDisplayButton.XNA_y_bottom)
{
if (CurrentDisplayButton.DisplayPage.Contains(CurrentPage))
{
CurrentDisplayButton.isPressed = !CurrentDisplayButton.isPressed;
CurrentDisplayButton.isBeingClicked = false;
DoSomethingWithLogicCommand(CurrentDisplayButton.functionString, CurrentDisplayButton.functionTarget,
CurrentDisplayButton.voteOpinion, CurrentDisplayButton.votePriority,
CurrentDisplayButton.voteType, (object)CurrentDisplayButton.isPressed);
return;
}
}
}
}
}
#endregion
#region Default GUI Timer
protected override void Draw(GameTime gameTime)
{
//VerboseLog("Start--> XNA Drawing");
if (XNA_Equivalent_UnderForm.WindowState == FormWindowState.Minimized)
{
//Don't update
}
else
{
UpdateDaGUI();
}
//VerboseLog("End--> XNA Drawing");
}
#endregion
#region Sweep Image
public void SweepImage(FusionImage toSweepImage, float beginAngle, float endAngle, float delAngle)
{
if (beginAngle < endAngle)
{
for (float newAngle = beginAngle; newAngle <= endAngle; newAngle += delAngle)
{
toSweepImage.XNA_Rotation = DegreesToRadians(newAngle);
toSweepImage.Draw(XNA_FusionSpriteBatchManager, Application_isBeingScaledBy);
}
}
else if (beginAngle > endAngle)
{
DateTime bTime = DateTime.Now;
for (float newAngle = beginAngle; newAngle <= (endAngle + 360.0f); newAngle += delAngle)
{
toSweepImage.XNA_Rotation = DegreesToRadians(newAngle);
toSweepImage.Draw(XNA_FusionSpriteBatchManager, Application_isBeingScaledBy);
}
TimeSpan eTime = DateTime.Now.Subtract(bTime);
Console.WriteLine(eTime.TotalMilliseconds);
}
}
#endregion
#region Degrees to Radians
public float DegreesToRadians(float inDegrees)
{
return (float)((inDegrees * Math.PI) / (180.0f));
}
#endregion
#region Error Message Box
///
/// When an error occurs, make a message box for the user
///
/// The main body text for the error message
public void DisplayMessageBox(string error)
{
VerboseLog("Fatal Error--> " + error);
MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
#region Fatal Error Message Box
///
/// Fatal Error, so display the error and then close the program
///
///
/// Should always be true when using this function
public void DisplayMessageBox(string error, bool isFatal)
{
VerboseLog("Fatal Error--> " + error);
MessageBox.Show(error, "FATAL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (isFatal)
{
this.Exit();
}
}
#endregion
#region Setup & Initialize Timers
///
/// Initializes the Timers used in the application
///
public void SetupTimers()
{
VerboseLog("Start--> Initializing Timers");
LogicTimer.Tick += new EventHandler(LogicTimer_Tick);
LogicTimer.Enabled = false;
LogicTimer.Interval = 100;
GUI_Timer.Tick += new EventHandler(GUI_Timer_Tick);
GUI_Timer.Enabled = false;
GUI_Timer.Interval = 40;
OutputTimer.Tick += new EventHandler(OutputTimer_Tick);
OutputTimer.Enabled = false;
OutputTimer.Interval = 75;
InputTimer.Tick += new EventHandler(InputTimer_Tick);
InputTimer.Enabled = false;
InputTimer.Interval = 45;
VerboseLog("End--> Initializing Timers");
}
public void InitializeTimers()
{
VerboseLog("Start--> Enable Timers");
LogicTimer.Enabled = true;
GUI_Timer.Enabled = true;
OutputTimer.Enabled = true;
InputTimer.Enabled = true;
VerboseLog("End--> Enable Timers");
}
#endregion
#region Verbose Log Writing
public void VerboseLog(string eventinput)
{
if (Debug_VerboseLoggingEvent != null)
{
Debug_VerboseLoggingEvent(eventinput + "... @" + DateTime.Now.ToString("hh:mm:ss:fffff"));
}
return;
}
#endregion
#region Timer Events
///
/// When the Logic Timer Event fires. Used for secondary logic commands
///
///
///
private void LogicTimer_Tick(object sender, EventArgs e)
{
VerboseLog("Entering Logic Timer Event");
PerformLogicCommands("logic");
UpdateLogs("logic");
VerboseLog("Leaving Logic Timer Event");
}
///
/// When the Output Timer Event fires. Triggers a write command to the Fusion Brain
///
///
///
private void OutputTimer_Tick(object sender, EventArgs e)
{
VerboseLog("Entering Output Timer Event");
PerformLogicCommands("output");
USB_OutputSteps();
UpdateLogs("output");
VerboseLog("Leaving Output Timer Event");
}
///
/// When the Input Timer Event fires. Triggers a read command to the Fusion Brain
///
///
///
private void InputTimer_Tick(object sender, EventArgs e)
{
VerboseLog("Entering Input Timer Event");
USB_InputSteps();
PerformLogicCommands("input");
UpdateLogs("input");
VerboseLog("Leaving Input Timer Event");
}
///
/// When the GUI Timer Event fires. Triggers a refresh of the display
///
///
///
private void GUI_Timer_Tick(object sender, EventArgs e)
{
VerboseLog("Entering GUI Logic Timer Event");
UpdateLogs("gui");
VerboseLog("Leaving GUI Logic Timer Event");
}
#endregion
#region Debugging
public bool SetupDebugging()
{
return false; //Not working so skip it...
if (Debug_EnableDebugWindow)
{
InitializeNewDebugClass();
return true;
}
return false;
}
public void InitializeNewDebugClass()
{
Debug myNewDebug = new Debug(this);
myNewDebug.RequestNewInformation += new Debug_Delegate_RequestNewLists(ThrowDebugEvents);
myNewDebug.SaveVariableList += new Debug_Delegate_SaveVariable(Debug_SaveVariable);
myNewDebug.SaveDigitalOut += new Debug_Delegate_SaveDigitalOut(Debug_SaveDigitalOutput);
myNewDebug.SaveAnalogueInput += new Debug_Delegate_SaveAnalogueInput(Debug_SaveAnalogueInput);
myNewDebug.SaveDigitalInput += new Debug_Delegate_SaveDigitalInput(Debug_SaveDigitalInput);
ThrowDebugEvents();
myNewDebug.TopMost = true;
myNewDebug.Show();
myNewDebug.TopMost = false;
}
void Debug_SaveVariable(object _inputValue, int _inputIndex)
{
LogicalVote _lv = new LogicalVote();
_lv.MyLogicalPriority = Vote_Priority.HIGH_PRIORITY;
_lv.toWhat = _inputValue;
AllVariables[_inputIndex].WhatShouldMyValueBe.Add(_lv);
UpdateVariables();
ThrowDebugEvents();
}
void Debug_SaveDigitalOutput(List UpdatedDigitalOutList)
{
AllDigitalOutputs = new List(UpdatedDigitalOutList);
UpdateDaGUI();
ThrowDebugEvents();
}
void Debug_SaveAnalogueInput(List UpdatedAnalogueInputList)
{
AllAnalogueInputs = new List(UpdatedAnalogueInputList);
UpdateDaGUI();
ThrowDebugEvents();
}
void Debug_SaveDigitalInput(List UpdatedDigitalInputList)
{
AllDigitalInputs = new List(UpdatedDigitalInputList);
UpdateDaGUI();
ThrowDebugEvents();
}
public void ThrowDebugEvents()
{
if (Debug_Event_SendLists != null)
{
Debug_Event_SendLists(AllBrains, AllDigitalOutputs, AllAnalogueInputs, AllDigitalInputs, AllVariables);
}
}
#endregion
#region When the Program is Closing
public void Fusion_Control_Centre_Uber_XNA_Edition_Game_Exiting(object sender, EventArgs e)
{
VerboseLog("Start--> Program Closing");
try
{
VerboseLog("Close All Open Log Streams");
CloseAllOpenLogStreams();
VerboseLog("All Log Streams Closed Successfully");
VerboseLog("Start--> Turn off Outputs");
foreach (DigitalOutputChannel DOC in AllDigitalOutputs)
{
DOC.CurrentState = false;
DOC.PreviousState = false;
DOC.ShouldIChangeState.Clear();
}
VerboseLog("All Outputs off in software");
VerboseLog("Start--> Tell Fusion Brain to turn off");
USB_OutputSteps();
VerboseLog("End--> Fusion Brain is turned off");
uint suc = FreeTheDevice();
VerboseLog("End--> Outputs are off");
VerboseLog("Brain is free");
}
catch (Exception)
{
//Error Closing the Program
MessageBox.Show("Error Closing the Program... You shouldn't be seeing this. :(", "UH OH", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
}
}