2007-08-10 09:27:13 +00:00
#region - - - License - - -
2008-03-24 13:15:25 +00:00
/ * Licensed under the MIT / X11 license .
* Copyright ( c ) 2006 - 2008 the OpenTK Team .
* This notice may not be removed from any source distribution .
* See license . txt for licensing details .
2007-08-10 09:27:13 +00:00
* /
#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
{
2008-01-17 14:06:14 +00:00
//sb.Append(example.Category);
//sb.Append(" ");
//if (example.Difficulty < 10)
// sb.Append("0"); // To keep items nicely sorted.
//sb.Append(example.Difficulty);
//sb.Append(": ");
////sb.Append(type.Name);
//sb.Append(example.Title);
2007-10-20 10:34:29 +00:00
2008-05-04 16:32:11 +00:00
if ( example . Visible )
2008-01-11 20:17:36 +00:00
listBox1 . Items . Add ( new ExampleInfo ( type , example ) ) ;
else
2008-01-17 14:06:14 +00:00
{
#if DEBUG
example . Title + = " (hidden)" ;
listBox1 . Items . Add ( new ExampleInfo ( type , example ) ) ;
#else
2008-01-11 20:17:36 +00:00
hidden_items . Add ( new ExampleInfo ( type , example ) ) ;
2008-01-17 14:06:14 +00:00
#endif
}
// Clean the StringBuilder for the next pass.
2007-10-20 10:34:29 +00:00
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 ;
2008-04-04 19:46:08 +00:00
RaiseWindow ( ) ;
2007-08-20 12:25:48 +00:00
}
2007-11-04 15:32:24 +00:00
#endregion
2007-09-02 00:02:10 +00:00
2008-04-04 19:46:08 +00:00
void RaiseWindow ( )
{
// Force the ExampleLauncher to appear.
this . TopMost = false ;
this . TopMost = true ;
this . TopMost = false ;
}
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 ;
2008-12-25 00:57:00 +00:00
Application . DoEvents ( ) ;
2007-11-11 18:44:10 +00:00
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
2008-01-20 19:29:42 +00:00
//throw;
2007-11-11 18:44:10 +00:00
#endif
}
catch ( NullReferenceException expt )
{
MessageBox . Show ( expt . ToString ( ) , "The Example launcher failed to load the example." , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
}
finally
{
this . Visible = true ;
2008-04-04 19:46:08 +00:00
RaiseWindow ( ) ;
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 ( )
{
2008-03-24 13:15:25 +00:00
try
{
//FileIOPermission fileIO = new FileIOPermission(FileIOPermissionAccess.AllAccess, ".");
//fileIO.Demand();
2008-04-04 19:46:08 +00:00
Application . EnableVisualStyles ( ) ;
2009-02-13 19:09:38 +00:00
Application . SetCompatibleTextRenderingDefault ( false ) ;
2008-03-24 13:15:25 +00:00
using ( Form exampleLauncher = new ExampleLauncher ( ) )
{
Application . Run ( exampleLauncher ) ;
}
}
catch ( System . Security . SecurityException e )
2007-09-02 00:02:10 +00:00
{
2008-04-13 15:51:31 +00:00
MessageBox . Show ( "The Example Launcher failed to start, due to insufficient permissions. This may happen if you execute the application from a network share." , "OpenTK Example Launcher failed to start." ,
MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
Trace . WriteLine ( e . ToString ( ) ) ;
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
}