Просмотр исходного кода

Linux/Mac OSCursor implementations (untested)

Marko Pintera 13 лет назад
Родитель
Сommit
58db262ed7

+ 27 - 0
CamelotRenderer/Include/Cocoa/CmOSCursorImpl.h

@@ -0,0 +1,27 @@
+#pragma once
+
+#include "CmPrerequisitesUtil.h"
+#include "CmInt2.h"
+
+#if defined(__OBJC__)
+#import <Cocoa/Cocoa.h>
+#else
+#include <ApplicationServices/ApplicationServices.h>
+typedef void *id;
+#endif
+
+namespace CamelotEngine
+{
+	/**
+	 * @brief	Provides controls for Windows operating system cursor.
+	 */
+	class CM_UTILITY_EXPORT OSCursor
+	{
+	public:
+		static Int2 getPosition();
+		static void setPosition(const Int2& pos);
+
+		static void hide();
+		static void show();
+	};
+}

+ 33 - 0
CamelotRenderer/Include/X11/CmOSCursorImpl.h

@@ -0,0 +1,33 @@
+#pragma once
+
+#include "CmPrerequisitesUtil.h"
+#include "CmInt2.h"
+#include <X11/X.h>
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
+
+namespace CamelotEngine
+{
+	/**
+	 * @brief	Provides controls for operating system cursor.
+	 */
+	class CM_UTILITY_EXPORT OSCursor
+	{
+	public:
+		OSCursor();
+
+		static Int2 getPosition();
+		static void setPosition(const Int2& pos);
+
+		static void hide();
+		static void show();
+
+	private:
+		Window mWindow; // TODO - This isn't being set anywhere yet
+		Cursor mCursor;
+		Display* mDisplay;
+
+		bool mCursorGrabbed;
+		bool mCursorHidden;
+	};
+}

+ 36 - 0
CamelotRenderer/Source/Cocoa/CmOSCursorImpl.cpp

@@ -0,0 +1,36 @@
+#include "X11/CmOSCursorImpl.h"
+
+// TODO - Not tested and will not work until I properly test it on a mac system!
+
+namespace CamelotEngine
+{
+	OSCursor()
+	{
+	}
+
+	Int2 OSCursor::getPosition()
+	{
+		NSPoint mouseLoc;
+		mouseLoc = [NSEvent mouseLocation];
+
+		return Int2(mouseLoc.x, mouseLoc.y);
+	}
+
+	void OSCursor::setPosition(const Int2& pos)
+	{
+		CGPoint globalPoint = CGPointMake(pos.x, pos.y);
+		CGWarpMouseCursorPosition(globalPoint);
+	}
+
+	void OSCursor::hide()
+	{
+		[NSCursor hide];
+		CGAssociateMouseAndMouseCursorPosition(false);
+	}
+
+	void OSCursor::show()
+	{
+		[NSCursor unhide];
+		CGAssociateMouseAndMouseCursorPosition(true);
+	}
+}

+ 76 - 0
CamelotRenderer/Source/X11/CmOSCursorImpl.cpp

@@ -0,0 +1,76 @@
+#include "X11/CmOSCursorImpl.h"
+
+// TODO - Not tested and will not work until I properly test it on a unix system!
+
+namespace CamelotEngine
+{
+	OSCursor()
+		:mCursorGrabbed(false), mCursorHidden(false)
+	{
+		mDisplay = XOpenDisplay(0); // TODO - What if there are multiple displays? Need to check for that
+		mWindow = XRootWindow(mDisplay, 0);
+		mCursor = createNULLCursor(mDisplay, mWindow); // Create invisible cursor
+	}
+
+	Int2 OSCursor::getPosition()
+	{
+		Window returnedWindow;
+		int rootX, rootY;
+		int winX, winY;
+		unsigned int returnedMask;
+
+		XQueryPointer(mDisplay, mWindow, &returnedWindow,
+			&returnedWindow, &rootX, &rootY, &winX, &winY,
+			&returnedMask)
+
+		return Int2(rootX, rootY);
+	}
+
+	void OSCursor::setPosition(const Int2& pos)
+	{
+		XWarpPointer(mDisplay, None, mWindow, 0,0,0,0, pos.x, pos.y);
+		XFlush(mDisplay);
+	}
+
+	void OSCursor::hide()
+	{
+		// Hide cursor
+		if(!mCursorHidden)
+		{
+			XDefineCursor(mDisplay, mWindow, mCursor );
+			mCursorHidden = true;
+		}
+
+		// Grab cursor to user window
+		if(!mCursorGrabbed)
+		{
+			if( XGrabPointer( mDisplay, mWindow, True,
+				ButtonPressMask | ButtonReleaseMask |
+				PointerMotionMask, GrabModeAsync, GrabModeAsync,
+				mWindow, None, CurrentTime ) ==
+				GrabSuccess )
+			{
+				mCursorGrabbed = true;
+			}
+		}
+	}
+
+	void OSCursor::show()
+	{
+		// Un-grab cursor (only in windowed mode: in fullscreen mode we still
+		// want the mouse grabbed in order to confine the cursor to the window
+		// area)
+		if(mCursorGrabbed)
+		{
+			XUngrabPointer(mDisplay, CurrentTime);
+			mCursorGrabbed = false;
+		}
+
+		// Show cursor
+		if(mCursorHidden)
+		{
+			XUndefineCursor(mDisplay, mWindow);
+			mCursorHidden = GL_FALSE;
+		}
+	}
+}