Win32Mouse.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: Win32Mouse.cpp ///////////////////////////////////////////////////////////////////////////
  24. // Created: Colin Day, July 2001
  25. // Desc: Interface for the mouse using only the Win32 messages
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #define WIN32_LEAN_AND_MEAN
  28. #include <windows.h>
  29. #include "Common/Debug.h"
  30. #include "GameClient/GameClient.h"
  31. #include "Win32Device/GameClient/Win32Mouse.h"
  32. #include "WinMain.h"
  33. #ifdef _INTERNAL
  34. // for occasional debugging...
  35. //#pragma optimize("", off)
  36. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  37. #endif
  38. // EXTERN /////////////////////////////////////////////////////////////////////////////////////////
  39. extern Win32Mouse *TheWin32Mouse;
  40. HCURSOR cursorResources[Mouse::NUM_MOUSE_CURSORS][MAX_2D_CURSOR_DIRECTIONS];
  41. ///////////////////////////////////////////////////////////////////////////////////////////////////
  42. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////////////////////////
  43. ///////////////////////////////////////////////////////////////////////////////////////////////////
  44. //-------------------------------------------------------------------------------------------------
  45. /** Get a mouse event from the buffer if available, we need to translate
  46. * from the windows message meanings to our own internal mouse
  47. * structure */
  48. //-------------------------------------------------------------------------------------------------
  49. UnsignedByte Win32Mouse::getMouseEvent( MouseIO *result, Bool flush )
  50. {
  51. // if there is nothing here there is no event data to do
  52. if( m_eventBuffer[ m_nextGetIndex ].msg == 0 )
  53. return MOUSE_NONE;
  54. // translate the win32 mouse message to our own system
  55. translateEvent( m_nextGetIndex, result );
  56. // remove this event from the buffer by setting msg to zero
  57. m_eventBuffer[ m_nextGetIndex ].msg = 0;
  58. //
  59. // our next get index will now be advanced to the next index, wrapping at
  60. // the mad
  61. //
  62. m_nextGetIndex++;
  63. if( m_nextGetIndex >= Mouse::NUM_MOUSE_EVENTS )
  64. m_nextGetIndex = 0;
  65. // got event OK and all done with this one
  66. return MOUSE_OK;
  67. } // end getMouseEvent
  68. //-------------------------------------------------------------------------------------------------
  69. /** Translate a win32 mouse event to our own event info */
  70. //-------------------------------------------------------------------------------------------------
  71. void Win32Mouse::translateEvent( UnsignedInt eventIndex, MouseIO *result )
  72. {
  73. UINT msg = m_eventBuffer[ eventIndex ].msg;
  74. WPARAM wParam = m_eventBuffer[ eventIndex ].wParam;
  75. LPARAM lParam = m_eventBuffer[ eventIndex ].lParam;
  76. UnsignedInt frame;
  77. //
  78. // get the current input frame from the client, if we don't have
  79. // a client (like in the GUI editor) we just use frame 1 so it
  80. // registers with the system
  81. //
  82. if( TheGameClient )
  83. frame = TheGameClient->getFrame();
  84. else
  85. frame = 1;
  86. // set these to defaults
  87. result->leftState = result->middleState = result->rightState = MBS_Up;
  88. result->leftFrame = result->middleFrame = result->rightFrame = 0;
  89. result->pos.x = result->pos.y = result->wheelPos = 0;
  90. // Time is the same for all events
  91. result->time = m_eventBuffer[ eventIndex ].time;
  92. switch( msg )
  93. {
  94. // ------------------------------------------------------------------------
  95. case WM_LBUTTONDOWN:
  96. {
  97. result->leftState = MBS_Down;
  98. result->leftFrame = frame;
  99. result->pos.x = LOWORD( lParam );
  100. result->pos.y = HIWORD( lParam );
  101. break;
  102. } // end left button down
  103. // ------------------------------------------------------------------------
  104. case WM_LBUTTONUP:
  105. {
  106. result->leftState = MBS_Up;
  107. result->leftFrame = frame;
  108. result->pos.x = LOWORD( lParam );
  109. result->pos.y = HIWORD( lParam );
  110. break;
  111. } // end left button up
  112. // ------------------------------------------------------------------------
  113. case WM_LBUTTONDBLCLK:
  114. {
  115. result->leftState = MBS_DoubleClick;
  116. result->leftFrame = frame;
  117. result->pos.x = LOWORD( lParam );
  118. result->pos.y = HIWORD( lParam );
  119. break;
  120. } // end left button double click
  121. // ------------------------------------------------------------------------
  122. case WM_MBUTTONDOWN:
  123. {
  124. result->middleState = MBS_Down;
  125. result->middleFrame = frame;
  126. result->pos.x = LOWORD( lParam );
  127. result->pos.y = HIWORD( lParam );
  128. break;
  129. } // end middle button down
  130. // ------------------------------------------------------------------------
  131. case WM_MBUTTONUP:
  132. {
  133. result->middleState = MBS_Up;
  134. result->middleFrame = frame;
  135. result->pos.x = LOWORD( lParam );
  136. result->pos.y = HIWORD( lParam );
  137. break;
  138. } // end middle button up
  139. // ------------------------------------------------------------------------
  140. case WM_MBUTTONDBLCLK:
  141. {
  142. result->middleState = MBS_DoubleClick;
  143. result->middleFrame = frame;
  144. result->pos.x = LOWORD( lParam );
  145. result->pos.y = HIWORD( lParam );
  146. break;
  147. } // end middle button double click
  148. // ------------------------------------------------------------------------
  149. case WM_RBUTTONDOWN:
  150. {
  151. result->rightState = MBS_Down;
  152. result->rightFrame = frame;
  153. result->pos.x = LOWORD( lParam );
  154. result->pos.y = HIWORD( lParam );
  155. break;
  156. } // end right button down
  157. // ------------------------------------------------------------------------
  158. case WM_RBUTTONUP:
  159. {
  160. result->rightState = MBS_Up;
  161. result->rightFrame = frame;
  162. result->pos.x = LOWORD( lParam );
  163. result->pos.y = HIWORD( lParam );
  164. break;
  165. } // end right button up
  166. // ------------------------------------------------------------------------
  167. case WM_RBUTTONDBLCLK:
  168. {
  169. result->rightState = MBS_DoubleClick;
  170. result->rightFrame = frame;
  171. result->pos.x = LOWORD( lParam );
  172. result->pos.y = HIWORD( lParam );
  173. break;
  174. } // end right button double click
  175. // ------------------------------------------------------------------------
  176. case WM_MOUSEMOVE:
  177. {
  178. result->pos.x = LOWORD( lParam );
  179. result->pos.y = HIWORD( lParam );
  180. break;
  181. } // end mouse move
  182. // ------------------------------------------------------------------------
  183. case 0x020A: // WM_MOUSEWHEEL
  184. {
  185. POINT p;
  186. // translate the screen mouse position to be relative to the application window
  187. p.x = LOWORD( lParam );
  188. p.y = HIWORD( lParam );
  189. ScreenToClient( ApplicationHWnd, &p );
  190. // note the short cast here to keep signed information in tact
  191. result->wheelPos = (Short)HIWORD( wParam );
  192. result->pos.x = p.x;
  193. result->pos.y = p.y;
  194. break;
  195. } // end mouse wheel
  196. // ------------------------------------------------------------------------
  197. default:
  198. {
  199. DEBUG_CRASH(( "translateEvent: Unknown Win32 mouse event [%d,%d,%d]\n",
  200. msg, wParam, lParam ));
  201. return;
  202. } // end default
  203. } // end switch on message at event index in buffer
  204. } // end translateEvent
  205. ///////////////////////////////////////////////////////////////////////////////////////////////////
  206. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////////////////////////
  207. ///////////////////////////////////////////////////////////////////////////////////////////////////
  208. //-------------------------------------------------------------------------------------------------
  209. //-------------------------------------------------------------------------------------------------
  210. Win32Mouse::Win32Mouse( void )
  211. {
  212. // zero our event list
  213. memset( &m_eventBuffer, 0, sizeof( m_eventBuffer ) );
  214. m_nextFreeIndex = 0;
  215. m_nextGetIndex = 0;
  216. m_currentWin32Cursor = NONE;
  217. for (Int i=0; i<NUM_MOUSE_CURSORS; i++)
  218. for (Int j=0; j<MAX_2D_CURSOR_DIRECTIONS; j++)
  219. cursorResources[i][j]=NULL;
  220. m_directionFrame=0; //points up.
  221. m_lostFocus = FALSE;
  222. } // end Win32Mouse
  223. //-------------------------------------------------------------------------------------------------
  224. //-------------------------------------------------------------------------------------------------
  225. Win32Mouse::~Win32Mouse( void )
  226. {
  227. // remove our global reference that was for the WndProc() only
  228. TheWin32Mouse = NULL;
  229. } // end ~Win32Mouse
  230. //-------------------------------------------------------------------------------------------------
  231. /** Initialize our device */
  232. //-------------------------------------------------------------------------------------------------
  233. void Win32Mouse::init( void )
  234. {
  235. // extending functionality
  236. Mouse::init();
  237. //
  238. // when we receive messages from a Windows message procedure, the mouse
  239. // moves report the current cursor position and not deltas, our mouse
  240. // needs to process those positions as absolute and not relative
  241. //
  242. m_inputMovesAbsolute = TRUE;
  243. } // end int
  244. //-------------------------------------------------------------------------------------------------
  245. /** Reset */
  246. //-------------------------------------------------------------------------------------------------
  247. void Win32Mouse::reset( void )
  248. {
  249. // extend
  250. Mouse::reset();
  251. } // end reset
  252. //-------------------------------------------------------------------------------------------------
  253. /** Update, called once per frame */
  254. //-------------------------------------------------------------------------------------------------
  255. void Win32Mouse::update( void )
  256. {
  257. // extend
  258. Mouse::update();
  259. } // end update
  260. //-------------------------------------------------------------------------------------------------
  261. /** Add a window message event along with its WPARAM and LPARAM parameters
  262. * to our input storage buffer */
  263. //-------------------------------------------------------------------------------------------------
  264. void Win32Mouse::addWin32Event( UINT msg, WPARAM wParam, LPARAM lParam, DWORD time )
  265. {
  266. //
  267. // we can only add this event if our next free index does not already
  268. // have an event in it, if it does ... our buffer is full and this input
  269. // event will be lost
  270. //
  271. if( m_eventBuffer[ m_nextFreeIndex ].msg != 0 )
  272. return;
  273. // add to this index
  274. m_eventBuffer[ m_nextFreeIndex ].msg = msg;
  275. m_eventBuffer[ m_nextFreeIndex ].wParam = wParam;
  276. m_eventBuffer[ m_nextFreeIndex ].lParam = lParam;
  277. m_eventBuffer[ m_nextFreeIndex ].time = time;
  278. // wrap index at max
  279. m_nextFreeIndex++;
  280. if( m_nextFreeIndex >= Mouse::NUM_MOUSE_EVENTS )
  281. m_nextFreeIndex = 0;
  282. } // end addWin32Event
  283. extern HINSTANCE ApplicationHInstance;
  284. void Win32Mouse::setVisibility(Bool visible)
  285. {
  286. //Extend
  287. Mouse::setVisibility(visible);
  288. //Maybe need to set cursor to force hiding of some cursors.
  289. Win32Mouse::setCursor(getMouseCursor());
  290. }
  291. /**Preload all the cursors we may need during the game. This must be done before the D3D device
  292. is created to avoid cursor corruption on buggy ATI Radeon cards. */
  293. void Win32Mouse::initCursorResources(void)
  294. {
  295. for (Int cursor=FIRST_CURSOR; cursor<NUM_MOUSE_CURSORS; cursor++)
  296. {
  297. for (Int direction=0; direction<m_cursorInfo[cursor].numDirections; direction++)
  298. { if (!cursorResources[cursor][direction] && !m_cursorInfo[cursor].textureName.isEmpty())
  299. { //this cursor has never been loaded before.
  300. char resourcePath[256];
  301. //Check if this is a directional cursor
  302. if (m_cursorInfo[cursor].numDirections > 1)
  303. sprintf(resourcePath,"data\\cursors\\%s%d.ANI",m_cursorInfo[cursor].textureName.str(),direction);
  304. else
  305. sprintf(resourcePath,"data\\cursors\\%s.ANI",m_cursorInfo[cursor].textureName.str());
  306. cursorResources[cursor][direction]=LoadCursorFromFile(resourcePath);
  307. DEBUG_ASSERTCRASH(cursorResources[cursor][direction], ("MissingCursor %s\n",resourcePath));
  308. }
  309. }
  310. // SetCursor(cursorResources[cursor][m_directionFrame]);
  311. }
  312. }
  313. //-------------------------------------------------------------------------------------------------
  314. /** Super basic simplistic cursor */
  315. //-------------------------------------------------------------------------------------------------
  316. void Win32Mouse::setCursor( MouseCursor cursor )
  317. {
  318. // extend
  319. Mouse::setCursor( cursor );
  320. if (m_lostFocus)
  321. return; //stop messing with mouse cursor if we don't have focus.
  322. if (cursor == NONE || !m_visible)
  323. SetCursor( NULL );
  324. else
  325. {
  326. SetCursor(cursorResources[cursor][m_directionFrame]);
  327. } // end switch
  328. // save current cursor
  329. m_currentWin32Cursor=m_currentCursor = cursor;
  330. } // end setCursor
  331. //-------------------------------------------------------------------------------------------------
  332. /** Capture the mouse to our application */
  333. //-------------------------------------------------------------------------------------------------
  334. void Win32Mouse::capture( void )
  335. {
  336. // SetCapture( ApplicationHWnd );
  337. } // end capture
  338. //-------------------------------------------------------------------------------------------------
  339. /** Release the mouse capture for our app window */
  340. //-------------------------------------------------------------------------------------------------
  341. void Win32Mouse::releaseCapture( void )
  342. {
  343. // ReleaseCapture();
  344. } // end releaseCapture