Skip to content

Commit

Permalink
[Examples] Cleaned up input device printing in GameWindowStates
Browse files Browse the repository at this point in the history
  • Loading branch information
thefiddler committed Jan 6, 2014
1 parent 83f54f7 commit d49dacb
Showing 1 changed file with 99 additions and 82 deletions.
181 changes: 99 additions & 82 deletions Source/Examples/OpenTK/Test/GameWindowStates.cs
Expand Up @@ -25,8 +25,6 @@ public class GameWindowStates : GameWindow
int texture; int texture;
bool mouse_in_window = false; bool mouse_in_window = false;
bool viewport_changed = true; bool viewport_changed = true;
MouseState mouse;
KeyboardState keyboard;


public GameWindowStates() public GameWindowStates()
: base(800, 600, GraphicsMode.Default) : base(800, 600, GraphicsMode.Default)
Expand Down Expand Up @@ -102,134 +100,145 @@ static int Clamp(int val, int min, int max)
return val > max ? max : val < min ? min : val; 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)); 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:"; line++;
float space = gfx.MeasureString(" ", TextFont).Width; DrawString(gfx, "Keyboard:", line++);
float offset = gfx.MeasureString(str, TextFont).Width + space; for (int i = 0; i < 4; i++)
DrawString(gfx, str, line);
for (int i = 0; i < (int)Key.LastKey; i++)
{ {
Key k = (Key)i; var state = OpenTK.Input.Keyboard.GetState(i);
if (keyboard[k]) if (state.IsConnected)
{ {
string key = k.ToString(); StringBuilder sb = new StringBuilder();
DrawString(gfx, key, line, offset); sb.Append(i);
offset += gfx.MeasureString(key, TextFont).Width + space; 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:"; line++;
float space = gfx.MeasureString(" ", TextFont).Width; DrawString(gfx, "Mouse:", line++);
float offset = gfx.MeasureString(str, TextFont).Width + space; for (int i = 0; i < 4; i++)
DrawString(gfx, str, line);
for (int i = 0; i < (int)MouseButton.LastButton; i++)
{ {
MouseButton b = (MouseButton)i; var state = OpenTK.Input.Mouse.GetState(i);
if (mouse[b]) if (state.IsConnected)
{ {
string button = b.ToString(); StringBuilder sb = new StringBuilder();
DrawString(gfx, button, line, offset); Vector3 pos = new Vector3(state.X, state.Y, state.WheelPrecise);
offset += gfx.MeasureString(button, TextFont).Width + space; 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) foreach (var joy in joysticks)
{ {
string str = String.Format("Joystick '{0}': ", joy.Description); joy_index++;
DrawString(gfx, str, line); if (!String.IsNullOrEmpty(joy.Description))

float offset = 0;
line++;
for (int i = 0; i < joy.Axis.Count; i++)
{ {
string axis = joy.Axis[i].ToString(); StringBuilder sb = new StringBuilder();
DrawString(gfx, axis, line, offset); sb.Append(joy_index);
offset += gfx.MeasureString(axis, TextFont).Width + space; 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; return line;
} }


protected override void OnUpdateFrame(FrameEventArgs e) protected override void OnUpdateFrame(FrameEventArgs e)
{ {
InputDriver.Poll();

mouse = OpenTK.Input.Mouse.GetState();
keyboard = OpenTK.Input.Keyboard.GetState();

using (Graphics gfx = Graphics.FromImage(TextBitmap)) using (Graphics gfx = Graphics.FromImage(TextBitmap))
{ {
int line = 0; int line = 0;


gfx.Clear(Color.Black); gfx.Clear(Color.Black);
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; 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.Renderer), line++);
DrawString(gfx, GL.GetString(StringName.Version), line++);
DrawString(gfx, Context.GraphicsMode.ToString(), 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("[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("[5 - 7]: change WindowBorder (current: {0}).", this.WindowBorder), line++);
DrawString(gfx, String.Format("Focused: {0}.", this.Focused), line++); DrawString(gfx, String.Format("Mouse {0} and {1}. {2}.",
DrawString(gfx, String.Format("Mouse {0} window.", mouse_in_window ? "inside" : "outside of"), line++); mouse_in_window ? "inside" : "outside",
DrawString(gfx, String.Format("Mouse visible: {0}", CursorVisible), line++); CursorVisible ? "visible" : "hidden",
DrawString(gfx, String.Format("Mouse position (absolute): {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.Wheel)), line++); Focused ? "Focused" : "Not focused"), line++);
DrawString(gfx, String.Format("Mouse position (relative): {0}", new Vector3(mouse.X, mouse.Y, mouse.WheelPrecise)), line++); DrawString(gfx, String.Format("Mouse (absolute): {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.WheelPrecise)), line++);
DrawString(gfx, String.Format("Window.Bounds: {0}", Bounds), line++); DrawString(gfx, String.Format("Bounds: {0}", Bounds), line++);
DrawString(gfx, String.Format("Window.Location: {0}, Size: {1}", Location, Size), line++); DrawString(gfx, String.Format("ClientRectangle: {0}", ClientRectangle), 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, TypedText.ToString(), 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( line = DrawKeyboards(gfx, line);
new System.Drawing.Rectangle(0, 0, TextBitmap.Width, TextBitmap.Height), line = DrawMice(gfx, line);
System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); line = DrawJoysticks(gfx, line);
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, TextBitmap.Width, TextBitmap.Height, PixelFormat.Bgra, line = DrawLegacyJoysticks(gfx, Joysticks, line);
PixelType.UnsignedByte, data.Scan0); }
TextBitmap.UnlockBits(data);
} }


int DrawGamePads(Graphics gfx, int line) int DrawJoysticks(Graphics gfx, int line)
{ {
line++; line++;
DrawString(gfx, "GamePads:", line++); DrawString(gfx, "GamePad:", line++);
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
GamePadCapabilities caps = GamePad.GetCapabilities(i); GamePadCapabilities caps = GamePad.GetCapabilities(i);
Expand All @@ -240,8 +249,9 @@ int DrawGamePads(Graphics gfx, int line)
DrawString(gfx, state.ToString(), line++); DrawString(gfx, state.ToString(), line++);
} }
} }

line++; line++;
DrawString(gfx, "Joysticks:", line++); DrawString(gfx, "Joystick:", line++);
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
JoystickCapabilities caps = Joystick.GetCapabilities(i); JoystickCapabilities caps = Joystick.GetCapabilities(i);
Expand Down Expand Up @@ -283,21 +293,28 @@ protected override void OnResize(EventArgs e)
protected override void OnRenderFrame(FrameEventArgs e) protected override void OnRenderFrame(FrameEventArgs e)
{ {
base.OnRenderFrame(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) if (viewport_changed)
{ {
viewport_changed = false; viewport_changed = false;


GL.Viewport(0, 0, Width, Height); GL.Viewport(0, 0, Width, Height);

Matrix4 ortho_projection = Matrix4.CreateOrthographicOffCenter(0, Width, Height, 0, -1, 1); Matrix4 ortho_projection = Matrix4.CreateOrthographicOffCenter(0, Width, Height, 0, -1, 1);
GL.MatrixMode(MatrixMode.Projection); GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref ortho_projection); GL.LoadMatrix(ref ortho_projection);
} }


GL.Clear(ClearBufferMask.ColorBufferBit); GL.Clear(ClearBufferMask.ColorBufferBit);


GL.Begin(BeginMode.Quads); GL.Begin(PrimitiveType.Quads);


GL.TexCoord2(0, 0); GL.Vertex2(0, 0); GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
GL.TexCoord2(1, 0); GL.Vertex2(TextBitmap.Width, 0); GL.TexCoord2(1, 0); GL.Vertex2(TextBitmap.Width, 0);
Expand Down

0 comments on commit d49dacb

Please sign in to comment.