Permalink
Browse files

[Examples] Cleaned up input device printing in GameWindowStates

  • Loading branch information...
1 parent 83f54f7 commit d49dacb5b3b850ff59c8f634dae9db588264f6b6 @thefiddler thefiddler committed Jan 6, 2014
Showing with 99 additions and 82 deletions.
  1. +99 −82 Source/Examples/OpenTK/Test/GameWindowStates.cs
@@ -25,8 +25,6 @@ public class GameWindowStates : GameWindow
int texture;
bool mouse_in_window = false;
bool viewport_changed = true;
- MouseState mouse;
- KeyboardState keyboard;
public GameWindowStates()
: base(800, 600, GraphicsMode.Default)
@@ -102,134 +100,145 @@ static int Clamp(int val, int min, int max)
return val > max ? max : val < min ? min : val;
}
- static void DrawString(Graphics gfx, string str, int line)
+ static float DrawString(Graphics gfx, string str, int line)
{
- gfx.DrawString(str, TextFont, Brushes.White, new PointF(0, line * TextFont.Height));
+ return DrawString(gfx, str, line, 0);
}
- static void DrawString(Graphics gfx, string str, int line, float offset)
+ static float DrawString(Graphics gfx, string str, int line, float offset)
{
gfx.DrawString(str, TextFont, Brushes.White, new PointF(offset, line * TextFont.Height));
+ return offset + gfx.MeasureString(str, TextFont).Width;
}
- static void DrawKeyboard(Graphics gfx, KeyboardState keyboard, int line)
+ static int DrawKeyboards(Graphics gfx, int line)
{
- const string str = "Keys pressed:";
- float space = gfx.MeasureString(" ", TextFont).Width;
- float offset = gfx.MeasureString(str, TextFont).Width + space;
- DrawString(gfx, str, line);
- for (int i = 0; i < (int)Key.LastKey; i++)
+ line++;
+ DrawString(gfx, "Keyboard:", line++);
+ for (int i = 0; i < 4; i++)
{
- Key k = (Key)i;
- if (keyboard[k])
+ var state = OpenTK.Input.Keyboard.GetState(i);
+ if (state.IsConnected)
{
- string key = k.ToString();
- DrawString(gfx, key, line, offset);
- offset += gfx.MeasureString(key, TextFont).Width + space;
+ StringBuilder sb = new StringBuilder();
+ sb.Append(i);
+ sb.Append(": ");
+ for (int key_index = 0; key_index < (int)Key.LastKey; key_index++)
+ {
+ Key k = (Key)key_index;
+ if (state[k])
+ {
+ sb.Append(k);
+ sb.Append(" ");
+ }
+ }
+ DrawString(gfx, sb.ToString(), line++);
}
}
+ return line;
}
- static void DrawMouse(Graphics gfx, MouseState mouse, int line)
+ static int DrawMice(Graphics gfx, int line)
{
- const string str = "Buttons pressed:";
- float space = gfx.MeasureString(" ", TextFont).Width;
- float offset = gfx.MeasureString(str, TextFont).Width + space;
- DrawString(gfx, str, line);
- for (int i = 0; i < (int)MouseButton.LastButton; i++)
+ line++;
+ DrawString(gfx, "Mouse:", line++);
+ for (int i = 0; i < 4; i++)
{
- MouseButton b = (MouseButton)i;
- if (mouse[b])
+ var state = OpenTK.Input.Mouse.GetState(i);
+ if (state.IsConnected)
{
- string button = b.ToString();
- DrawString(gfx, button, line, offset);
- offset += gfx.MeasureString(button, TextFont).Width + space;
+ StringBuilder sb = new StringBuilder();
+ Vector3 pos = new Vector3(state.X, state.Y, state.WheelPrecise);
+ sb.Append(i);
+ sb.Append(": ");
+ sb.Append(pos);
+ for (int button_index = 0; button_index < (int)MouseButton.LastButton; button_index++)
+ {
+ MouseButton b = (MouseButton)button_index;
+ if (state[b])
+ {
+ sb.Append(b);
+ sb.Append(" ");
+ }
+ }
+ DrawString(gfx, sb.ToString(), line++);
}
}
+ return line;
}
- static int DrawJoysticks(Graphics gfx, IList<JoystickDevice> joysticks, int line)
+ static int DrawLegacyJoysticks(Graphics gfx, IList<JoystickDevice> joysticks, int line)
{
- float space = gfx.MeasureString(" ", TextFont).Width;
+ line++;
+ DrawString(gfx, "Legacy Joystick:", line++);
+ int joy_index = -1;
foreach (var joy in joysticks)
{
- string str = String.Format("Joystick '{0}': ", joy.Description);
- DrawString(gfx, str, line);
-
- float offset = 0;
- line++;
- for (int i = 0; i < joy.Axis.Count; i++)
+ joy_index++;
+ if (!String.IsNullOrEmpty(joy.Description))
{
- string axis = joy.Axis[i].ToString();
- DrawString(gfx, axis, line, offset);
- offset += gfx.MeasureString(axis, TextFont).Width + space;
+ StringBuilder sb = new StringBuilder();
+ sb.Append(joy_index);
+ sb.Append(": '");
+ sb.Append(joy.Description);
+ sb.Append("' ");
+
+ for (int i = 0; i < joy.Axis.Count; i++)
+ {
+ sb.Append(joy.Axis[i]);
+ sb.Append(" ");
+ }
+
+ for (int i = 0; i < joy.Button.Count; i++)
+ {
+ sb.Append(joy.Button[i]);
+ sb.Append(" ");
+ }
+ DrawString(gfx, sb.ToString(), line++);
}
-
- offset = 0;
- line++;
- for (int i = 0; i < joy.Button.Count; i++)
- {
- string button = joy.Button[i].ToString();
- DrawString(gfx, button, line, offset);
- offset += gfx.MeasureString(button, TextFont).Width + space;
- }
-
- line++;
}
return line;
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
- InputDriver.Poll();
-
- mouse = OpenTK.Input.Mouse.GetState();
- keyboard = OpenTK.Input.Keyboard.GetState();
-
using (Graphics gfx = Graphics.FromImage(TextBitmap))
{
int line = 0;
gfx.Clear(Color.Black);
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
- DrawString(gfx, GL.GetString(StringName.Vendor), line++);
- DrawString(gfx, GL.GetString(StringName.Version), line++);
DrawString(gfx, GL.GetString(StringName.Renderer), line++);
+ DrawString(gfx, GL.GetString(StringName.Version), line++);
DrawString(gfx, Context.GraphicsMode.ToString(), line++);
+ line++;
+ DrawString(gfx, "GameWindow:", line++);
DrawString(gfx, String.Format("[1 - 4]: change WindowState (current: {0}).", this.WindowState), line++);
DrawString(gfx, String.Format("[5 - 7]: change WindowBorder (current: {0}).", this.WindowBorder), line++);
- DrawString(gfx, String.Format("Focused: {0}.", this.Focused), line++);
- DrawString(gfx, String.Format("Mouse {0} window.", mouse_in_window ? "inside" : "outside of"), line++);
- DrawString(gfx, String.Format("Mouse visible: {0}", CursorVisible), line++);
- DrawString(gfx, String.Format("Mouse position (absolute): {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.Wheel)), line++);
- DrawString(gfx, String.Format("Mouse position (relative): {0}", new Vector3(mouse.X, mouse.Y, mouse.WheelPrecise)), line++);
- DrawString(gfx, String.Format("Window.Bounds: {0}", Bounds), line++);
- DrawString(gfx, String.Format("Window.Location: {0}, Size: {1}", Location, Size), line++);
- DrawString(gfx, String.Format("Window: {{X={0},Y={1},Width={2},Height={3}}}", X, Y, Width, Height), line++);
- DrawString(gfx, String.Format("Window.ClientRectangle: {0}", ClientRectangle), line++);
+ DrawString(gfx, String.Format("Mouse {0} and {1}. {2}.",
+ mouse_in_window ? "inside" : "outside",
+ CursorVisible ? "visible" : "hidden",
+ Focused ? "Focused" : "Not focused"), line++);
+ DrawString(gfx, String.Format("Mouse (absolute): {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.WheelPrecise)), line++);
+ DrawString(gfx, String.Format("Bounds: {0}", Bounds), line++);
+ DrawString(gfx, String.Format("ClientRectangle: {0}", ClientRectangle), line++);
DrawString(gfx, TypedText.ToString(), line++);
- DrawKeyboard(gfx, keyboard, line++);
- DrawMouse(gfx, mouse, line++);
- line = DrawJoysticks(gfx, Joysticks, line++);
- line = DrawGamePads(gfx, line++);
- }
- System.Drawing.Imaging.BitmapData data = TextBitmap.LockBits(
- new System.Drawing.Rectangle(0, 0, TextBitmap.Width, TextBitmap.Height),
- System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
- GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, TextBitmap.Width, TextBitmap.Height, PixelFormat.Bgra,
- PixelType.UnsignedByte, data.Scan0);
- TextBitmap.UnlockBits(data);
+ line = DrawKeyboards(gfx, line);
+ line = DrawMice(gfx, line);
+ line = DrawJoysticks(gfx, line);
+ line = DrawLegacyJoysticks(gfx, Joysticks, line);
+ }
}
- int DrawGamePads(Graphics gfx, int line)
+ int DrawJoysticks(Graphics gfx, int line)
{
line++;
- DrawString(gfx, "GamePads:", line++);
+ DrawString(gfx, "GamePad:", line++);
for (int i = 0; i < 4; i++)
{
GamePadCapabilities caps = GamePad.GetCapabilities(i);
@@ -240,8 +249,9 @@ int DrawGamePads(Graphics gfx, int line)
DrawString(gfx, state.ToString(), line++);
}
}
+
line++;
- DrawString(gfx, "Joysticks:", line++);
+ DrawString(gfx, "Joystick:", line++);
for (int i = 0; i < 4; i++)
{
JoystickCapabilities caps = Joystick.GetCapabilities(i);
@@ -283,21 +293,28 @@ protected override void OnResize(EventArgs e)
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
-
+
+ System.Drawing.Imaging.BitmapData data = TextBitmap.LockBits(
+ new System.Drawing.Rectangle(0, 0, TextBitmap.Width, TextBitmap.Height),
+ System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
+ GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, TextBitmap.Width, TextBitmap.Height, PixelFormat.Bgra,
+ PixelType.UnsignedByte, data.Scan0);
+ TextBitmap.UnlockBits(data);
+
if (viewport_changed)
{
viewport_changed = false;
GL.Viewport(0, 0, Width, Height);
-
+
Matrix4 ortho_projection = Matrix4.CreateOrthographicOffCenter(0, Width, Height, 0, -1, 1);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref ortho_projection);
}
GL.Clear(ClearBufferMask.ColorBufferBit);
- GL.Begin(BeginMode.Quads);
+ GL.Begin(PrimitiveType.Quads);
GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
GL.TexCoord2(1, 0); GL.Vertex2(TextBitmap.Width, 0);

0 comments on commit d49dacb

Please sign in to comment.