win32CursorController.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. { PlatformCursorController::curWaitArrow, IDC_WAIT },
  45. { PlatformCursorController::curNoNo, IDC_NO },
  46. };
  47. //static const EnumTable::Enums curManagerShapesEnums[] =
  48. //{
  49. // { Win32CursorController::curArrow, "Arrow" },
  50. // { Win32CursorController::curWait, "Wait" },
  51. // { Win32CursorController::curPlus, "Plus" },
  52. // { Win32CursorController::curResizeVert, "ResizeVert" },
  53. // { Win32CursorController::curResizeHorz, "ResizeHorz" },
  54. // { Win32CursorController::curResizeAll, "ResizeAll" },
  55. // { Win32CursorController::curIBeam, "ibeam" },
  56. // { Win32CursorController::curResizeNESW, "ResizeNESW" },
  57. // { Win32CursorController::curResizeNWSE, "ResizeNWSE" },
  58. //};
  59. //
  60. //static const EnumTable gCurManagerShapesTable(8, &curManagerShapesEnums[0]);
  61. // CodeReview I've duplicated this 'cache' trick for system settings
  62. // because they're unlikely to change and calling into the OS for values
  63. // repeatedly is just silly to begin with. [6/29/2007 justind]
  64. U32 Win32CursorController::getDoubleClickTime()
  65. {
  66. static S32 sPlatWinDoubleClicktime = -1;
  67. if( sPlatWinDoubleClicktime == -1 )
  68. sPlatWinDoubleClicktime = GetDoubleClickTime();
  69. return sPlatWinDoubleClicktime;
  70. }
  71. S32 Win32CursorController::getDoubleClickWidth()
  72. {
  73. static S32 sPlatWinDoubleClickwidth = -1;
  74. if( sPlatWinDoubleClickwidth == -1 )
  75. sPlatWinDoubleClickwidth = GetSystemMetrics(SM_CXDOUBLECLK);
  76. return sPlatWinDoubleClickwidth;
  77. }
  78. S32 Win32CursorController::getDoubleClickHeight()
  79. {
  80. static S32 sPlatWinDoubleClickheight = -1;
  81. if( sPlatWinDoubleClickheight == -1 )
  82. sPlatWinDoubleClickheight = GetSystemMetrics(SM_CYDOUBLECLK);
  83. return sPlatWinDoubleClickheight;
  84. }
  85. void Win32CursorController::setCursorPosition( S32 x, S32 y )
  86. {
  87. ::SetCursorPos(x, y);
  88. }
  89. void Win32CursorController::getCursorPosition( Point2I &point )
  90. {
  91. POINT rPoint;
  92. ::GetCursorPos( &rPoint );
  93. // Return
  94. point.x = rPoint.x;
  95. point.y = rPoint.y;
  96. }
  97. void Win32CursorController::setCursorVisible( bool visible )
  98. {
  99. if( visible )
  100. ShowCursor( true );
  101. else
  102. while( ShowCursor(false) >= 0 );
  103. }
  104. bool Win32CursorController::isCursorVisible()
  105. {
  106. CURSORINFO rCursorInfo;
  107. rCursorInfo.cbSize = sizeof(CURSORINFO);
  108. if( !GetCursorInfo( &rCursorInfo ) )
  109. {
  110. //DWORD error = GetLastError();
  111. return false;
  112. }
  113. // rCursorInfo.flags values :
  114. // 0 == Cursor is hidden
  115. // CURSOR_SHOWING == cursor is visible
  116. return (bool)(rCursorInfo.flags == CURSOR_SHOWING);
  117. }
  118. void Win32CursorController::setCursorShape(U32 cursorID)
  119. {
  120. LPTSTR resourceID = NULL;
  121. for(S32 i = 0; i < numPlatformCursors; ++i)
  122. {
  123. if(cursorID == sgCursorShapeMap[i].id)
  124. {
  125. resourceID = sgCursorShapeMap[i].resourceID;
  126. break;
  127. }
  128. }
  129. if(resourceID == NULL)
  130. return;
  131. HCURSOR cur = LoadCursor(NULL, resourceID);
  132. if(cur)
  133. SetCursor(cur);
  134. }
  135. static HCURSOR gCursorShape = NULL;
  136. void Win32CursorController::setCursorShape( const UTF8 *fileName, bool reload )
  137. {
  138. #ifdef UNICODE
  139. const UTF16 *lFileName = createUTF16string( fileName );
  140. #else
  141. const UTF8 *lFileName = fileName;
  142. #endif
  143. if ( !gCursorShape || reload )
  144. gCursorShape = LoadCursorFromFile( lFileName );
  145. if ( gCursorShape )
  146. SetCursor( gCursorShape );
  147. #ifdef UNICODE
  148. delete[] lFileName;
  149. #endif
  150. }
  151. // Console function to set the current cursor shape given the cursor shape
  152. // name as defined in the enum above.
  153. //ConsoleFunction( inputPushCursor, void, 2, 2, "inputPushCursor(cursor shape name)" )
  154. //{
  155. // S32 val = 0;
  156. //
  157. // // Find the cursor shape
  158. // if(argc == 2)
  159. // {
  160. // for (S32 i = 0; i < gCurManagerShapesTable.size; i++)
  161. // {
  162. // if (! dStricmp(argv[1], gCurManagerShapesTable.table[i].label))
  163. // {
  164. // val = gCurManagerShapesTable.table[i].index;
  165. // break;
  166. // }
  167. // }
  168. // }
  169. //
  170. // // Now set it
  171. // Win32CursorController* cm = Input::getCursorManager();
  172. // if(cm)
  173. // {
  174. // cm->pushCursor(val);
  175. // }
  176. //}
  177. //// Function to pop the current cursor shape
  178. //ConsoleFunction( inputPopCursor, void, 1, 1, "inputPopCursor()" )
  179. //{
  180. // argc;
  181. // argv;
  182. //
  183. // Win32CursorController* cm = Input::getCursorManager();
  184. // if(cm)
  185. // {
  186. // cm->popCursor();
  187. // }
  188. //}