Robmaister's picture

Getting screen (and possibly window) coordinates from the new input API

Project:The Open Toolkit library
Version:1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:open
Description

So I switched to the new input API by compiling from the SVN trunk (Mouse.GetState(), etc.) in my game because it works a lot better for camera rotation, but I've recently run into a problem. When trying to add a main menu to my game, I realized that I needed window coordinates for my mouse input to be able to detect hovering/unhovering over UI elements. The only issue is that each state is receiving a MouseState and changing that will break camera rotation in the game, so right now I'm pulling window coordinates from the old API and using reflection to invoke the internal setters for X and Y to update them like so:

KeyboardState keyboard = OpenTK.Input.Keyboard.GetState();
MouseState mouse = OpenTK.Input.Mouse.GetState();
 
switch (currentState.MouseCoords)
{
	case MouseCoordinateSystem.Unbound:
		break;
	case MouseCoordinateSystem.Window:
	{
		object boxedMouse = mouse;
		PropertyInfo propX = typeof(MouseState).GetProperty("X");
		PropertyInfo propY = typeof(MouseState).GetProperty("Y");
		propX.SetValue(boxedMouse, Mouse.X, null);
		propY.SetValue(boxedMouse, Mouse.Y, null);
		mouse = (MouseState)boxedMouse;
		break;
	}
	case MouseCoordinateSystem.Screen:
	{
		Point screenPos = PointToScreen(new Point(Mouse.X, Mouse.Y));
		object boxedMouse = mouse;
		PropertyInfo propX = typeof(MouseState).GetProperty("X");
		PropertyInfo propY = typeof(MouseState).GetProperty("Y");
		propX.SetValue(boxedMouse, screenPos.X, null);
		propY.SetValue(boxedMouse, screenPos.Y, null);
		mouse = (MouseState)boxedMouse;
		break;
	}
}
 
currentState.InputUpdate(frameTime, keyboard, mouse);

Being able to retrieve screen coordinates would help a lot, but ideally it would be nice to be able to choose between at least those 3 coordinate systems, or however many are supported on Windows, Linux, and OS X.

I don't have a mac to help develop this/test on, but I can help with Windows and Linux.