2007-08-10 09:27:13 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-10 16:55:24 +00:00
|
|
|
|
#region --- Using Directives ---
|
|
|
|
|
|
2007-08-10 09:27:13 +00:00
|
|
|
|
using System;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading;
|
2007-07-27 12:06:32 +00:00
|
|
|
|
using System.Diagnostics;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
using System.Security;
|
|
|
|
|
using System.Security.Permissions;
|
|
|
|
|
using System.Security.Policy;
|
2007-08-04 12:09:58 +00:00
|
|
|
|
using System.IO;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-10 16:55:24 +00:00
|
|
|
|
using OpenTK;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
namespace Examples
|
|
|
|
|
{
|
|
|
|
|
public partial class ExampleLauncher : Form
|
|
|
|
|
{
|
2008-01-11 20:17:36 +00:00
|
|
|
|
bool show_hidden;
|
|
|
|
|
List<ExampleInfo> hidden_items = new List<ExampleInfo>();
|
|
|
|
|
|
2007-10-20 10:34:29 +00:00
|
|
|
|
#region class ExampleInfo
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Contains the information necessary to display and launch an example thorugh the ExampleLauncer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
class ExampleInfo
|
|
|
|
|
{
|
|
|
|
|
public Type Example;
|
2007-11-11 18:44:10 +00:00
|
|
|
|
public ExampleAttribute Attributes;
|
2007-10-20 10:34:29 +00:00
|
|
|
|
|
2007-11-11 18:44:10 +00:00
|
|
|
|
public ExampleInfo(Type example, ExampleAttribute attr)
|
2007-10-20 10:34:29 +00:00
|
|
|
|
{
|
|
|
|
|
Example = example;
|
2007-11-11 18:44:10 +00:00
|
|
|
|
Attributes = attr;
|
2007-10-20 10:34:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2007-11-11 18:44:10 +00:00
|
|
|
|
return Attributes.ToString();
|
2007-10-20 10:34:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Constructor ---
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-05 13:42:31 +00:00
|
|
|
|
public ExampleLauncher()
|
2007-08-04 12:09:58 +00:00
|
|
|
|
{
|
2007-08-05 13:42:31 +00:00
|
|
|
|
InitializeComponent();
|
2007-08-04 12:09:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-10-20 10:34:29 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
#region public void ExampleLauncher_Load(object sender, EventArgs e)
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
public void ExampleLauncher_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
2007-09-02 13:34:44 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists("debug.log"))
|
|
|
|
|
File.Delete("debug.log");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception expt)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Could not access debug.log", expt.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Listeners.Clear();
|
|
|
|
|
Debug.Listeners.Add(new TextWriterTraceListener("debug.log"));
|
|
|
|
|
Debug.Listeners.Add(new ConsoleTraceListener());
|
|
|
|
|
Debug.AutoFlush = true;
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
// Get all examples
|
|
|
|
|
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
|
2007-10-20 10:34:29 +00:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2007-08-20 12:25:48 +00:00
|
|
|
|
foreach (Type type in types)
|
|
|
|
|
{
|
2007-11-11 18:44:10 +00:00
|
|
|
|
object[] attributes = type.GetCustomAttributes(false);
|
|
|
|
|
ExampleAttribute example = null;
|
|
|
|
|
foreach (object attr in attributes)
|
|
|
|
|
if (attr is ExampleAttribute)
|
|
|
|
|
example = (ExampleAttribute)attr;
|
|
|
|
|
|
2008-01-11 20:17:36 +00:00
|
|
|
|
if (example != null)
|
2007-08-20 12:25:48 +00:00
|
|
|
|
{
|
2007-11-11 18:44:10 +00:00
|
|
|
|
sb.Append(example.Category);
|
2007-10-20 10:34:29 +00:00
|
|
|
|
sb.Append(" ");
|
2007-11-11 18:44:10 +00:00
|
|
|
|
if (example.Difficulty < 10)
|
|
|
|
|
sb.Append("0"); // To keep items nicely sorted.
|
|
|
|
|
sb.Append(example.Difficulty);
|
2007-10-20 10:34:29 +00:00
|
|
|
|
sb.Append(": ");
|
2007-11-11 18:44:10 +00:00
|
|
|
|
//sb.Append(type.Name);
|
|
|
|
|
sb.Append(example.Title);
|
2007-10-20 10:34:29 +00:00
|
|
|
|
|
2008-01-11 20:17:36 +00:00
|
|
|
|
if (example.Visible && example.Category != ExampleCategory.Test)
|
|
|
|
|
listBox1.Items.Add(new ExampleInfo(type, example));
|
|
|
|
|
else
|
|
|
|
|
hidden_items.Add(new ExampleInfo(type, example));
|
2007-10-20 10:34:29 +00:00
|
|
|
|
|
|
|
|
|
// Clean the StringBuilder for the next pass.
|
|
|
|
|
sb.Remove(0, sb.Length);
|
2007-08-20 12:25:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Select first item
|
|
|
|
|
if (listBox1.Items.Count > 0)
|
|
|
|
|
this.listBox1.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-04 15:32:24 +00:00
|
|
|
|
#endregion
|
2007-09-02 00:02:10 +00:00
|
|
|
|
|
2007-11-11 18:44:10 +00:00
|
|
|
|
#region private void RunExample()
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
private void RunExample()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
|
|
|
|
if (listBox1.SelectedItem != null)
|
|
|
|
|
{
|
2007-11-04 15:32:24 +00:00
|
|
|
|
try
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-11-11 18:44:10 +00:00
|
|
|
|
ExampleInfo info = (ExampleInfo)listBox1.SelectedItem;
|
|
|
|
|
Type example = info.Example;
|
|
|
|
|
|
|
|
|
|
Debug.Print("Launching example: {0}", example.ToString());
|
|
|
|
|
this.Visible = false;
|
|
|
|
|
|
|
|
|
|
example.GetMethod("Main").Invoke(null, null);
|
|
|
|
|
|
|
|
|
|
GC.Collect();
|
|
|
|
|
GC.WaitForPendingFinalizers();
|
|
|
|
|
GC.Collect();
|
2007-11-04 15:32:24 +00:00
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
2007-11-11 18:44:10 +00:00
|
|
|
|
catch (TargetInvocationException expt)
|
2007-10-17 11:33:11 +00:00
|
|
|
|
{
|
2007-11-11 18:44:10 +00:00
|
|
|
|
string ex_info;
|
2007-11-04 15:32:24 +00:00
|
|
|
|
if (expt.InnerException != null)
|
2007-11-11 18:44:10 +00:00
|
|
|
|
ex_info = expt.InnerException.ToString();
|
2007-11-04 15:32:24 +00:00
|
|
|
|
else
|
2007-11-11 18:44:10 +00:00
|
|
|
|
ex_info = expt.ToString();
|
|
|
|
|
MessageBox.Show(ex_info, "An OpenTK example encountered an error.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
2007-11-04 15:32:24 +00:00
|
|
|
|
|
2007-11-11 18:44:10 +00:00
|
|
|
|
Debug.Print(expt.ToString());
|
|
|
|
|
#if DEBUG
|
|
|
|
|
throw;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
catch (NullReferenceException expt)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(expt.ToString(), "The Example launcher failed to load the example.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
this.Visible = true;
|
|
|
|
|
this.TopMost = true; // Bring the ExampleLauncher window to front
|
2008-01-15 00:01:46 +00:00
|
|
|
|
this.TopMost = false; // but allow the user to cover it with other windows.
|
2007-10-17 11:33:11 +00:00
|
|
|
|
}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-11 18:44:10 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Launcher events
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
private void listBox1_DoubleClick(object sender, EventArgs e)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
RunExample();
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
private void listBox1_KeyUp(object sender, KeyEventArgs e)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
switch (e.KeyCode)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
case Keys.Enter:
|
|
|
|
|
RunExample();
|
|
|
|
|
break;
|
2008-01-11 20:17:36 +00:00
|
|
|
|
|
|
|
|
|
// On Ctrl+F1 enable hidden items (for debugging/testing OpenTK)
|
|
|
|
|
case Keys.F1:
|
|
|
|
|
if (e.Control)
|
|
|
|
|
show_hidden = !show_hidden;
|
|
|
|
|
|
|
|
|
|
if (show_hidden)
|
|
|
|
|
listBox1.Items.AddRange(hidden_items.ToArray());
|
|
|
|
|
else
|
|
|
|
|
foreach (ExampleInfo item in hidden_items)
|
|
|
|
|
listBox1.Items.Remove(item);
|
|
|
|
|
|
|
|
|
|
break;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
2007-08-20 12:25:48 +00:00
|
|
|
|
}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
private void runButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
RunExample();
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
2007-09-02 00:02:10 +00:00
|
|
|
|
|
2007-11-11 18:44:10 +00:00
|
|
|
|
private void ExampleLauncher_FormClosing(object sender, FormClosingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Debug.Flush();
|
|
|
|
|
Debug.Close();
|
|
|
|
|
Trace.Flush();
|
|
|
|
|
Trace.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-09-02 00:02:10 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The main entry point for the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[STAThread]
|
|
|
|
|
static void Main()
|
|
|
|
|
{
|
2007-09-02 13:34:44 +00:00
|
|
|
|
using (Form exampleLauncher = new ExampleLauncher())
|
2007-09-02 00:02:10 +00:00
|
|
|
|
{
|
2007-09-02 13:34:44 +00:00
|
|
|
|
Application.EnableVisualStyles();
|
|
|
|
|
Application.Run(exampleLauncher);
|
2007-09-02 00:02:10 +00:00
|
|
|
|
}
|
2007-09-02 13:34:44 +00:00
|
|
|
|
}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
2007-08-10 09:27:13 +00:00
|
|
|
|
}
|