win32CursorController.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <windows.h>
  23. #include <tchar.h>
  24. #include "core/strings/unicode.h"
  25. #include "math/mMath.h"
  26. #include "windowManager/win32/win32Window.h"
  27. #include "windowManager/win32/win32WindowMgr.h"
  28. #include "windowManager/win32/winDispatch.h"
  29. #include "windowManager/win32/win32CursorController.h"
  30. #include "platform/platformInput.h"
  31. #include <zmouse.h>
  32. static struct { U32 id; LPTSTR resourceID; } sgCursorShapeMap[]=
  33. {
  34. { PlatformCursorController::curArrow, IDC_ARROW },
  35. { PlatformCursorController::curWait, IDC_WAIT },
  36. { PlatformCursorController::curPlus, IDC_CROSS },
  37. { PlatformCursorController::curResizeVert, IDC_SIZEWE },
  38. { PlatformCursorController::curResizeHorz, IDC_SIZENS },
  39. { PlatformCursorController::curResizeAll, IDC_SIZEALL },
  40. { PlatformCursorController::curIBeam, IDC_IBEAM },
  41. { PlatformCursorController::curResizeNESW, IDC_SIZENESW },
  42. { PlatformCursorController::curResizeNWSE, IDC_SIZENWSE },
  43. { PlatformCursorController::curHand, IDC_HAND },
  44. { 0, 0 },
  45. };
  46. //static const EnumTable::Enums curManagerShapesEnums[] =
  47. //{
  48. // { Win32CursorController::curArrow, "Arrow" },
  49. // { Win32CursorController::curWait, "Wait" },
  50. // { Win32CursorController::curPlus, "Plus" },
  51. // { Win32CursorController::curResizeVert, "ResizeVert" },
  52. // { Win32CursorController::curResizeHorz, "ResizeHorz" },
  53. // { Win32CursorController::curResizeAll, "ResizeAll" },
  54. // { Win32CursorController::curIBeam, "ibeam" },
  55. // { Win32CursorController::curResizeNESW, "ResizeNESW" },
  56. // { Win32CursorController::curResizeNWSE, "ResizeNWSE" },
  57. //};
  58. //
  59. //static const EnumTable gCurManagerShapesTable(8, &curManagerShapesEnums[0]);
  60. // CodeReview I've duplicated this 'cache' trick for system settings
  61. // because they're unlikely to change and calling into the OS for values
  62. // repeatedly is just silly to begin with. [6/29/2007 justind]
  63. U32 Win32CursorController::getDoubleClickTime()
  64. {
  65. static S32 sPlatWinDoubleClicktime = -1;
  66. if( sPlatWinDoubleClicktime == -1 )
  67. sPlatWinDoubleClicktime = GetDoubleClickTime();
  68. return sPlatWinDoubleClicktime;
  69. }
  70. S32 Win32CursorController::getDoubleClickWidth()
  71. {
  72. static S32 sPlatWinDoubleClickwidth = -1;
  73. if( sPlatWinDoubleClickwidth == -1 )
  74. sPlatWinDoubleClickwidth = GetSystemMetrics(SM_CXDOUBLECLK);
  75. return sPlatWinDoubleClickwidth;
  76. }
  77. S32 Win32CursorController::getDoubleClickHeight()
  78. {
  79. static S32 sPlatWinDoubleClickheight = -1;
  80. if( sPlatWinDoubleClickheight == -1 )
  81. sPlatWinDoubleClickheight = GetSystemMetrics(SM_CYDOUBLECLK);
  82. return sPlatWinDoubleClickheight;
  83. }
  84. void Win32CursorController::setCursorPosition( S32 x, S32 y )
  85. {
  86. ::SetCursorPos(x, y);
  87. }
  88. void Win32CursorController::getCursorPosition( Point2I &point )
  89. {
  90. POINT rPoint;
  91. ::GetCursorPos( &rPoint );
  92. // Return
  93. point.x = rPoint.x;
  94. point.y = rPoint.y;
  95. }
  96. void Win32CursorController::setCursorVisible( bool visible )
  97. {
  98. if( visible )
  99. ShowCursor( true );
  100. else
  101. while( ShowCursor(false) >= 0 );
  102. }
  103. bool Win32CursorController::isCursorVisible()
  104. {
  105. CURSORINFO rCursorInfo;
  106. rCursorInfo.cbSize = sizeof(CURSORINFO);
  107. if( !GetCursorInfo( &rCursorInfo ) )
  108. {
  109. //DWORD error = GetLastError();
  110. return false;
  111. }
  112. // rCursorInfo.flags values :
  113. // 0 == Cursor is hidden
  114. // CURSOR_SHOWING == cursor is visible
  115. return (bool)(rCursorInfo.flags == CURSOR_SHOWING);
  116. }
  117. void Win32CursorController::setCursorShape(U32 cursorID)
  118. {
  119. LPTSTR resourceID = NULL;
  120. for(S32 i = 0;sgCursorShapeMap[i].resourceID != NULL;++i)
  121. {
  122. if(cursorID == sgCursorShapeMap[i].id)
  123. {
  124. resourceID = sgCursorShapeMap[i].resourceID;
  125. break;
  126. }
  127. }
  128. if(resourceID == NULL)
  129. return;
  130. HCURSOR cur = LoadCursor(NULL, resourceID);
  131. if(cur)
  132. SetCursor(cur);
  133. }
  134. static HCURSOR gCursorShape = NULL;
  135. void Win32CursorController::setCursorShape( const UTF8 *fileName, bool reload )
  136. {
  137. #ifdef UNICODE
  138. const UTF16 *lFileName = createUTF16string( fileName );
  139. #else
  140. const UTF8 *lFileName = fileName;
  141. #endif
  142. if ( !gCursorShape || reload )
  143. gCursorShape = LoadCursorFromFile( lFileName );
  144. if ( gCursorShape )
  145. SetCursor( gCursorShape );
  146. #ifdef UNICODE
  147. delete[] lFileName;
  148. #endif
  149. }
  150. // Console function to set the current cursor shape given the cursor shape
  151. // name as defined in the enum above.
  152. //ConsoleFunction( inputPushCursor, void, 2, 2, "inputPushCursor(cursor shape name)" )
  153. //{
  154. // S32 val = 0;
  155. //
  156. // // Find the cursor shape
  157. // if(argc == 2)
  158. // {
  159. // for (S32 i = 0; i < gCurManagerShapesTable.size; i++)
  160. // {
  161. // if (! dStricmp(argv[1], gCurManagerShapesTable.table[i].label))
  162. // {
  163. // val = gCurManagerShapesTable.table[i].index;
  164. // break;
  165. // }
  166. // }
  167. // }
  168. //
  169. // // Now set it
  170. // Win32CursorController* cm = Input::getCursorManager();
  171. // if(cm)
  172. // {
  173. // cm->pushCursor(val);
  174. // }
  175. //}
  176. //// Function to pop the current cursor shape
  177. //ConsoleFunction( inputPopCursor, void, 1, 1, "inputPopCursor()" )
  178. //{
  179. // argc;
  180. // argv;
  181. //
  182. // Win32CursorController* cm = Input::getCursorManager();
  183. // if(cm)
  184. // {
  185. // cm->popCursor();
  186. // }
  187. //}