Added GetNext and GetPrevious methods to improve toggling of window states and borders.

This commit is contained in:
the_fiddler 2008-05-04 14:40:08 +00:00
parent 6ee92bb453
commit 1540fab506

View file

@ -8,11 +8,11 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Threading; using System.Threading;
using OpenTK; using OpenTK;
using OpenTK.Graphics; using OpenTK.Graphics;
using OpenTK.Input; using OpenTK.Input;
namespace Examples.Tests namespace Examples.Tests
@ -21,51 +21,45 @@ namespace Examples.Tests
public class GameWindowStates : GameWindow public class GameWindowStates : GameWindow
{ {
TextureFont font = new TextureFont(new Font(FontFamily.GenericSansSerif, 16.0f)); TextureFont font = new TextureFont(new Font(FontFamily.GenericSansSerif, 16.0f));
TextPrinter printer = new TextPrinter(); TextPrinter printer = new TextPrinter();
WindowState[] window_state_sequence = new WindowState[] #region GetNext and GetPrevious methods for enums.
{
WindowState.Normal, T GetNext<T>(T t)
WindowState.Maximized, {
WindowState.Fullscreen, if (!(t is Enum))
WindowState.Minimized throw new ArgumentException(String.Format("Should be an Enum type (is {0}).", t.GetType().ToString()),
}; "t");
WindowBorder[] window_border_sequence = new WindowBorder[] string[] names = Enum.GetNames(t.GetType());
{ T[] values = (T[])Enum.GetValues(t.GetType());
WindowBorder.Resizable,
WindowBorder.Fixed, int current_index = Array.IndexOf(names, t.ToString());
WindowBorder.Hidden, if (current_index >= values.Length - 1)
}; return values[0];
else
int window_state_counter = 0; return values[current_index + 1];
int WindowStateCounter
{ }
get { return window_state_counter; }
set T GetPrevious<T>(T t)
{ {
if (value < 0) if (!(t is Enum))
window_state_counter = window_state_sequence.Length - 1; throw new ArgumentException(String.Format("Should be an Enum type (is {0}).", t.GetType().ToString()),
else "t");
window_state_counter = ++window_state_counter % window_state_sequence.Length;
} string[] names = Enum.GetNames(t.GetType());
} T[] values = (T[])Enum.GetValues(t.GetType());
int window_border_counter = 0; int current_index = Array.IndexOf(names, t.ToString());
int WindowBorderCounter if (current_index <= 0)
{ return values[values.Length - 1];
get { return window_border_counter; } else
set return values[current_index - 1];
{ }
if (value < 0)
window_border_counter = window_border_sequence.Length - 1; #endregion
else
window_border_counter = ++window_border_counter % window_border_sequence.Length;
}
}
public GameWindowStates() public GameWindowStates()
: base(800, 600) : base(800, 600)
{ {
@ -84,49 +78,31 @@ namespace Examples.Tests
this.Exit(); this.Exit();
break; break;
case OpenTK.Input.Key.Number1: case OpenTK.Input.Key.Number1:
if (sender[Key.ShiftLeft] || sender[Key.ShiftRight])
WindowStateCounter--; if (sender[Key.ShiftLeft] || sender[Key.ShiftRight])
else WindowState = GetPrevious(WindowState);
WindowStateCounter++; else
WindowState = window_state_sequence[WindowStateCounter]; WindowState = GetNext(WindowState);
// switch (this.WindowState)
// {
// case WindowState.Normal: this.WindowState = WindowState.Maximized; break;1
// case WindowState.Maximized: this.WindowState = WindowState.Fullscreen; break;
// case WindowState.Fullscreen:
// this.WindowState = WindowState.Normal;
// this.WindowState = WindowState.Minimized;
// break;
// case WindowState.Minimized: this.WindowState = WindowState.Normal;
// break;
//
// }
break; break;
case OpenTK.Input.Key.Number2: case OpenTK.Input.Key.Number2:
if (sender[Key.ShiftLeft] || sender[Key.ShiftRight])
WindowBorderCounter--; if (sender[Key.ShiftLeft] || sender[Key.ShiftRight])
else WindowBorder = GetPrevious(WindowBorder);
WindowBorderCounter++; else
WindowBorder = window_border_sequence[WindowBorderCounter]; WindowBorder = GetNext(WindowBorder);
// this.WindowState = WindowState.Normal;
// switch (this.WindowBorder)
// {
// case WindowBorder.Fixed: this.WindowBorder = WindowBorder.Hidden; break;
// case WindowBorder.Hidden: this.WindowBorder = WindowBorder.Resizable; break;
// case WindowBorder.Resizable: this.WindowBorder = WindowBorder.Fixed; break;
// }
break; break;
case OpenTK.Input.Key.Number3: case OpenTK.Input.Key.Number3:
if (this.WindowState == WindowState.Fullscreen)
this.WindowState = WindowState.Normal; if (this.WindowState == WindowState.Fullscreen)
else this.WindowState = WindowState.Normal;
this.WindowState = WindowState.Fullscreen; else
this.WindowState = WindowState.Fullscreen;
break; break;
} }
} }
@ -143,17 +119,17 @@ namespace Examples.Tests
printer.Begin(); printer.Begin();
printer.Draw("Instructions:", font); GL.Translate(0, font.Height, 0); printer.Draw("Instructions:", font); GL.Translate(0, font.Height, 0);
printer.Draw(String.Format("1 - cycle through window styles (current: {0}).", this.WindowState), font); printer.Draw(String.Format("1 - cycle through window styles (current: {0}).", this.WindowState), font);
GL.Translate(0, font.Height, 0); GL.Translate(0, font.Height, 0);
printer.Draw(String.Format("2 - cycle through window borders (current: {0}).", this.WindowBorder), font); printer.Draw(String.Format("2 - cycle through window borders (current: {0}).", this.WindowBorder), font);
GL.Translate(0, font.Height, 0); GL.Translate(0, font.Height, 0);
printer.Draw(String.Format("3 - toggle fullscreen (current: {0}).", printer.Draw(String.Format("3 - toggle fullscreen (current: {0}).",
this.WindowState == WindowState.Fullscreen ? "enabled" : "disabled"), font); this.WindowState == WindowState.Fullscreen ? "enabled" : "disabled"), font);
printer.End(); printer.End();
SwapBuffers(); SwapBuffers();
Thread.Sleep(5); Thread.Sleep(5);
} }
@ -161,7 +137,7 @@ namespace Examples.Tests
{ {
using (GameWindowStates ex = new GameWindowStates()) using (GameWindowStates ex = new GameWindowStates())
{ {
Utilities.SetWindowTitle(ex); Utilities.SetWindowTitle(ex);
ex.Run(20.0); ex.Run(20.0);
} }
} }