Browse Source

Some cursor management methods

Marko Pintera 13 years ago
parent
commit
54d787cc56

+ 5 - 0
CamelotClient/CmDebugCamera.cpp

@@ -5,6 +5,7 @@
 #include "CmMath.h"
 #include "CmGameObject.h"
 #include "CmCamera.h"
+#include "CmOSCursor.h"
 
 namespace CamelotEngine
 {
@@ -67,6 +68,8 @@ namespace CamelotEngine
 		if(buttonID == MB_Right)
 		{
 			mCameraRotating = true;
+
+			OSCursor::hide();
 		}
 	}
 
@@ -75,6 +78,8 @@ namespace CamelotEngine
 		if(buttonID == MB_Right)
 		{
 			mCameraRotating = false;
+
+			OSCursor::show();
 		}
 	}
 

+ 3 - 0
CamelotRenderer/CamelotRenderer.vcxproj

@@ -124,6 +124,7 @@
     <ClInclude Include="Include\CmMesh.h" />
     <ClInclude Include="Include\CmMeshData.h" />
     <ClInclude Include="Include\CmMeshDataRTTI.h" />
+    <ClInclude Include="Include\CmOSCursor.h" />
     <ClInclude Include="Include\CmPass.h" />
     <ClInclude Include="Include\CmPassRTTI.h" />
     <ClInclude Include="Include\CmPrerequisites.h" />
@@ -164,6 +165,7 @@
     <ClInclude Include="Include\targetver.h" />
     <ClInclude Include="Include\CmVertexDeclarationRTTI.h" />
     <ClInclude Include="Include\CmTechnique.h" />
+    <ClInclude Include="Include\Win32\CmOSCursorImpl.h" />
     <ClInclude Include="Source\CmMeshRTTI.h" />
   </ItemGroup>
   <ItemGroup>
@@ -213,6 +215,7 @@
     <ClCompile Include="Source\CmGameObject.cpp" />
     <ClCompile Include="Source\CmComponent.cpp" />
     <ClCompile Include="Source\stdafx.cpp" />
+    <ClCompile Include="Source\Win32\CmOSCursorImpl.cpp" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">

+ 15 - 0
CamelotRenderer/CamelotRenderer.vcxproj.filters

@@ -76,6 +76,12 @@
     <Filter Include="Source Files\RTTI">
       <UniqueIdentifier>{dc50e07b-6351-4bc2-8bfa-cc3fc1d26c39}</UniqueIdentifier>
     </Filter>
+    <Filter Include="Header Files\Win32">
+      <UniqueIdentifier>{1d3fe8eb-ec10-4356-90f0-b27f89f01a13}</UniqueIdentifier>
+    </Filter>
+    <Filter Include="Source Files\Win32">
+      <UniqueIdentifier>{5a1e28c5-e784-44e6-974f-f1d0d66474ed}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <Text Include="TODO.txt" />
@@ -295,6 +301,12 @@
     <ClInclude Include="Include\CmMaterialRTTI.h">
       <Filter>Header Files\RTTI</Filter>
     </ClInclude>
+    <ClInclude Include="Include\CmOSCursor.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="Include\Win32\CmOSCursorImpl.h">
+      <Filter>Header Files\Win32</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\CamelotRenderer.cpp">
@@ -435,5 +447,8 @@
     <ClCompile Include="Source\CmMaterialRTTI.cpp">
       <Filter>Source Files\RTTI</Filter>
     </ClCompile>
+    <ClCompile Include="Source\Win32\CmOSCursorImpl.cpp">
+      <Filter>Source Files\Win32</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 12 - 0
CamelotRenderer/Include/CmOSCursor.h

@@ -0,0 +1,12 @@
+#pragma once
+
+#include "CmPrerequisites.h"
+
+//Bring in the specific platform's header file
+#if CM_PLATFORM == CM_PLATFORM_WIN32
+# include "Win32/CmOSCursorImpl.h"
+#elif (CM_PLATFORM == CM_PLATFORM_LINUX)
+# include "GLX/CmOSCursorImpl.h"
+#elif CM_PLATFORM == CM_PLATFORM_APPLE
+# include "OSX/CmOSCursorImpl.h"
+#endif

+ 21 - 0
CamelotRenderer/Include/Win32/CmOSCursorImpl.h

@@ -0,0 +1,21 @@
+#pragma once
+
+#include "CmPrerequisites.h"
+#include "CmInt2.h"
+#include <windows.h>
+
+namespace CamelotEngine
+{
+	/**
+	 * @brief	Provides controls for Windows operating system cursor.
+	 */
+	class CM_EXPORT OSCursor
+	{
+	public:
+		static Int2 getPosition();
+		static void setPosition(const Int2& pos);
+
+		static void hide();
+		static void show();
+	};
+}

+ 68 - 0
CamelotRenderer/Source/Win32/CmOSCursorImpl.cpp

@@ -0,0 +1,68 @@
+#include "Win32/CmOSCursorImpl.h"
+#include "CmApplication.h"
+#include "CmRenderWindow.h"
+
+namespace CamelotEngine
+{
+	Int2 OSCursor::getPosition()
+	{
+		POINT screenPos;
+		GetCursorPos(&screenPos);
+
+		RenderWindow* primaryWindow = gApplication().getPrimaryRenderWindow();
+		HWND hwnd;
+		primaryWindow->getCustomAttribute("WINDOW", &hwnd);
+
+		ScreenToClient(hwnd, &screenPos);
+
+		return Int2(screenPos.x, screenPos.y);
+	}
+
+	void OSCursor::setPosition(const Int2& pos)
+	{
+		POINT screenPos;
+
+		// Convert client coordinates to screen coordinates
+		screenPos.x = pos.x;
+		screenPos.y = pos.y;
+
+		RenderWindow* primaryWindow = gApplication().getPrimaryRenderWindow();
+		HWND hwnd;
+		primaryWindow->getCustomAttribute("WINDOW", &hwnd);
+
+		ClientToScreen(hwnd, &screenPos);
+
+		SetCursorPos(screenPos.x, screenPos.y);
+	}
+
+	void OSCursor::hide()
+	{
+		RECT clipWindowRect;
+
+		ShowCursor( FALSE );
+
+		RenderWindow* primaryWindow = gApplication().getPrimaryRenderWindow();
+		HWND hwnd;
+		primaryWindow->getCustomAttribute("WINDOW", &hwnd);
+
+		// Clip cursor to the window
+		if( GetWindowRect(hwnd, &clipWindowRect))
+		{
+			ClipCursor(&clipWindowRect);
+		}
+
+		// Capture cursor to user window
+		SetCapture(hwnd);
+	}
+
+	void OSCursor::show()
+	{
+		// Un-capture cursor
+		ReleaseCapture();
+
+		// Release the cursor from the window
+		ClipCursor(NULL);
+
+		ShowCursor(TRUE);
+	}
+}

+ 2 - 0
CamelotUtility/CamelotUtility.vcxproj

@@ -83,6 +83,7 @@
   </ItemDefinitionGroup>
   <ItemGroup>
     <ClCompile Include="..\CamelotRenderer\Source\CmManagedDataBlock.cpp" />
+    <ClCompile Include="Source\CmInt2.cpp" />
     <ClCompile Include="Source\CmUUID.cpp" />
     <ClCompile Include="Source\CmWorkQueue.cpp" />
     <ClCompile Include="Source\Win32\CmTimer.cpp" />
@@ -95,6 +96,7 @@
     <ClInclude Include="Include\CmException.h" />
     <ClInclude Include="Include\CmFileSerializer.h" />
     <ClInclude Include="Include\CmFileSystem.h" />
+    <ClInclude Include="Include\CmInt2.h" />
     <ClInclude Include="Include\CmIReflectable.h" />
     <ClInclude Include="Include\CmKeyValuePair.h" />
     <ClInclude Include="Include\CmLog.h" />

+ 6 - 0
CamelotUtility/CamelotUtility.vcxproj.filters

@@ -195,6 +195,9 @@
     <ClInclude Include="Include\CmKeyValuePair.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="Include\CmInt2.h">
+      <Filter>Header Files\Math</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Include\CmAxisAlignedBox.cpp">
@@ -287,5 +290,8 @@
     <ClCompile Include="..\CamelotRenderer\Source\CmManagedDataBlock.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="Source\CmInt2.cpp">
+      <Filter>Source Files\Math</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 217 - 0
CamelotUtility/Include/CmInt2.h

@@ -0,0 +1,217 @@
+#pragma once
+
+#include "CmPrerequisitesUtil.h"
+
+namespace CamelotEngine
+{
+	struct CM_UTILITY_EXPORT Int2
+	{
+		int x;
+		int y;
+
+		inline Int2(const int _x, const int _y )
+            : x( _x ), y( _y )
+        {
+        }
+
+        inline explicit Int2( const int scaler )
+            : x( scaler), y( scaler )
+        {
+        }
+
+        inline explicit Int2( const int afCoordinate[2] )
+        {
+            x = afCoordinate[0];
+            y = afCoordinate[1];
+        }
+
+        inline explicit Int2( int* const r )
+            : x( r[0] ), y( r[1] )
+        {
+        }
+
+		/** Exchange the contents of this vector with another. 
+		*/
+		inline void swap(Int2& other)
+		{
+			std::swap(x, other.x);
+			std::swap(y, other.y);
+		}
+
+		inline int operator [] ( const size_t i ) const
+        {
+            assert( i < 2 );
+
+            return *(&x+i);
+        }
+
+		inline int& operator [] ( const size_t i )
+        {
+            assert( i < 2 );
+
+            return *(&x+i);
+        }
+
+		/// Pointer accessor for direct copying
+		inline int* ptr()
+		{
+			return &x;
+		}
+		/// Pointer accessor for direct copying
+		inline const int* ptr() const
+		{
+			return &x;
+		}
+
+        /** Assigns the value of the other vector.
+            @param
+                rhs The other vector
+        */
+        inline Int2& operator = ( const Int2& rhs )
+        {
+            x = rhs.x;
+            y = rhs.y;
+
+            return *this;
+        }
+
+		inline Int2& operator = ( const int fScalar)
+		{
+			x = fScalar;
+			y = fScalar;
+
+			return *this;
+		}
+
+        inline bool operator == ( const Int2& rhs ) const
+        {
+            return ( x == rhs.x && y == rhs.y );
+        }
+
+        inline bool operator != ( const Int2& rhs ) const
+        {
+            return ( x != rhs.x || y != rhs.y  );
+        }
+
+        // arithmetic operations
+        inline Int2 operator + ( const Int2& rhs ) const
+        {
+            return Int2(
+                x + rhs.x,
+                y + rhs.y);
+        }
+
+        inline Int2 operator - ( const Int2& rhs ) const
+        {
+            return Int2(
+                x - rhs.x,
+                y - rhs.y);
+        }
+
+        inline Int2 operator * ( const int fScalar ) const
+        {
+            return Int2(
+                x * fScalar,
+                y * fScalar);
+        }
+
+        inline Int2 operator * ( const Int2& rhs) const
+        {
+            return Int2(
+                x * rhs.x,
+                y * rhs.y);
+        }
+
+        inline Int2 operator / ( const int fScalar ) const
+        {
+            assert( fScalar != 0 );
+
+            return Int2(
+                x / fScalar,
+                y / fScalar);
+        }
+
+        inline Int2 operator / ( const Int2& rhs) const
+        {
+            return Int2(
+                x / rhs.x,
+                y / rhs.y);
+        }
+
+        inline const Int2& operator + () const
+        {
+            return *this;
+        }
+
+        inline Int2 operator - () const
+        {
+            return Int2(-x, -y);
+        }
+
+        // overloaded operators to help Vector2
+        inline friend Int2 operator * ( const int fScalar, const Int2& rhs )
+        {
+            return Int2(
+                fScalar * rhs.x,
+                fScalar * rhs.y);
+        }
+
+        inline friend Int2 operator / ( const int fScalar, const Int2& rhs )
+        {
+            return Int2(
+                fScalar / rhs.x,
+                fScalar / rhs.y);
+        }
+
+        // arithmetic updates
+        inline Int2& operator += ( const Int2& rhs )
+        {
+            x += rhs.x;
+            y += rhs.y;
+
+            return *this;
+        }
+
+        inline Int2& operator -= ( const Int2& rhs )
+        {
+            x -= rhs.x;
+            y -= rhs.y;
+
+            return *this;
+        }
+
+        inline Int2& operator *= ( const int fScalar )
+        {
+            x *= fScalar;
+            y *= fScalar;
+
+            return *this;
+        }
+
+        inline Int2& operator *= ( const Int2& rhs )
+        {
+            x *= rhs.x;
+            y *= rhs.y;
+
+            return *this;
+        }
+
+        inline Int2& operator /= ( const int fScalar )
+        {
+            assert( fScalar != 0 );
+
+            x /= fScalar;
+            y /= fScalar;
+
+            return *this;
+        }
+
+        inline Int2& operator /= ( const Int2& rhs )
+        {
+            x /= rhs.x;
+            y /= rhs.y;
+
+            return *this;
+        }
+	};
+}

+ 1 - 0
CamelotUtility/Source/CmInt2.cpp

@@ -0,0 +1 @@
+#include "CmInt2.h"