WindowXlat.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. ** Command & Conquer Generals(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: WindowXlat.cpp ///////////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Westwood Studios Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2001 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: RTS3
  34. //
  35. // File name: WindowXlat.cpp
  36. //
  37. // Created: Colin Day, September 2001
  38. //
  39. // Desc: Window system translator that monitors raw input messages
  40. // on the stream from the input devices and acts on anything
  41. // relevant to the windowing system.
  42. //
  43. //-----------------------------------------------------------------------------
  44. ///////////////////////////////////////////////////////////////////////////////
  45. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  46. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  47. // USER INCLUDES //////////////////////////////////////////////////////////////
  48. #include "Common/MessageStream.h"
  49. #include "GameClient/GameWindowManager.h"
  50. #include "GameClient/WindowXlat.h"
  51. #include "GameClient/Shell.h"
  52. #include "GameClient/Display.h"
  53. #ifdef _INTERNAL
  54. // for occasional debugging...
  55. //#pragma optimize("", off)
  56. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  57. #endif
  58. // DEFINES ////////////////////////////////////////////////////////////////////
  59. // PRIVATE TYPES //////////////////////////////////////////////////////////////
  60. // PRIVATE DATA ///////////////////////////////////////////////////////////////
  61. // PUBLIC DATA ////////////////////////////////////////////////////////////////
  62. // PRIVATE PROTOTYPES /////////////////////////////////////////////////////////
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
  65. ///////////////////////////////////////////////////////////////////////////////
  66. #if defined(_DEBUG) || defined(_INTERNAL) //debug hack to view object under mouse stats
  67. extern ICoord2D TheMousePos;
  68. #endif
  69. // rawMouseToWindowMessage ====================================================
  70. /** Translate a raw mouse input event to a game window specific message
  71. * for the window system */
  72. //=============================================================================
  73. static GameWindowMessage rawMouseToWindowMessage( const GameMessage *msg )
  74. {
  75. GameWindowMessage gwm = GWM_NONE;
  76. switch( msg->getType() )
  77. {
  78. // ------------------------------------------------------------------------
  79. case GameMessage::MSG_RAW_MOUSE_POSITION:
  80. gwm = GWM_MOUSE_POS;
  81. break;
  82. // ------------------------------------------------------------------------
  83. // Strange, but true. The window stuff really doesn't care about double clicks, so just
  84. // treat it as a down click.. Kinda like a second click.
  85. case GameMessage::MSG_RAW_MOUSE_LEFT_DOUBLE_CLICK:
  86. case GameMessage::MSG_RAW_MOUSE_LEFT_BUTTON_DOWN:
  87. gwm = GWM_LEFT_DOWN;
  88. break;
  89. case GameMessage::MSG_RAW_MOUSE_LEFT_BUTTON_UP:
  90. gwm = GWM_LEFT_UP;
  91. break;
  92. case GameMessage::MSG_RAW_MOUSE_LEFT_DRAG:
  93. gwm = GWM_LEFT_DRAG;
  94. break;
  95. // ------------------------------------------------------------------------
  96. case GameMessage::MSG_RAW_MOUSE_MIDDLE_DOUBLE_CLICK:
  97. case GameMessage::MSG_RAW_MOUSE_MIDDLE_BUTTON_DOWN:
  98. gwm = GWM_MIDDLE_DOWN;
  99. break;
  100. case GameMessage::MSG_RAW_MOUSE_MIDDLE_BUTTON_UP:
  101. gwm = GWM_MIDDLE_UP;
  102. break;
  103. case GameMessage::MSG_RAW_MOUSE_MIDDLE_DRAG:
  104. gwm = GWM_MIDDLE_DRAG;
  105. break;
  106. // ------------------------------------------------------------------------
  107. case GameMessage::MSG_RAW_MOUSE_RIGHT_DOUBLE_CLICK:
  108. case GameMessage::MSG_RAW_MOUSE_RIGHT_BUTTON_DOWN:
  109. gwm = GWM_RIGHT_DOWN;
  110. break;
  111. case GameMessage::MSG_RAW_MOUSE_RIGHT_BUTTON_UP:
  112. gwm = GWM_RIGHT_UP;
  113. break;
  114. case GameMessage::MSG_RAW_MOUSE_RIGHT_DRAG:
  115. gwm = GWM_RIGHT_DRAG;
  116. break;
  117. // ------------------------------------------------------------------------
  118. case GameMessage::MSG_RAW_MOUSE_WHEEL:
  119. if( msg->getArgument( 1 )->integer > 0 )
  120. gwm = GWM_WHEEL_UP;
  121. else
  122. gwm = GWM_WHEEL_DOWN;
  123. break;
  124. } // end switch
  125. return gwm;
  126. } // end rawMouseToWindowMessage
  127. ///////////////////////////////////////////////////////////////////////////////
  128. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
  129. ///////////////////////////////////////////////////////////////////////////////
  130. //=============================================================================
  131. WindowTranslator::WindowTranslator()
  132. {
  133. }
  134. //=============================================================================
  135. WindowTranslator::~WindowTranslator()
  136. {
  137. }
  138. // WindowTranslator ===========================================================
  139. /** Window translator that monitors raw input messages on the stream and
  140. * acts on anything relavant to the windowing system */
  141. //=============================================================================
  142. GameMessageDisposition WindowTranslator::translateGameMessage(const GameMessage *msg)
  143. {
  144. GameMessageDisposition disp = KEEP_MESSAGE;
  145. Bool forceKeepMessage = FALSE;
  146. WinInputReturnCode returnCode = WIN_INPUT_NOT_USED;
  147. if (TheTacticalView && TheTacticalView->isMouseLocked())
  148. {
  149. return KEEP_MESSAGE;
  150. }
  151. switch( msg->getType() )
  152. {
  153. // ------------------------------------------------------------------------
  154. case GameMessage::MSG_META_TOGGLE_ATTACKMOVE:
  155. {
  156. // Basically, we're cheating here. The mouse no longer sends us useless spam.
  157. ICoord2D mousePos = TheMouse->getMouseStatus()->pos;
  158. if( TheWindowManager )
  159. TheWindowManager->winProcessMouseEvent( GWM_NONE, &mousePos, NULL );
  160. // Force it to keep the message, regardless of what the window thinks it did with the input.
  161. return KEEP_MESSAGE;
  162. }
  163. // ------------------------------------------------------------------------
  164. case GameMessage::MSG_RAW_MOUSE_LEFT_BUTTON_UP:
  165. {
  166. if( TheInGameUI && TheInGameUI->isPlacementAnchored() )
  167. {
  168. //If we release the button outside
  169. forceKeepMessage = TRUE;
  170. }
  171. //FALL THROUGH INTENTIONALLY!
  172. }
  173. case GameMessage::MSG_RAW_MOUSE_POSITION:
  174. case GameMessage::MSG_RAW_MOUSE_LEFT_BUTTON_DOWN:
  175. case GameMessage::MSG_RAW_MOUSE_LEFT_DOUBLE_CLICK:
  176. case GameMessage::MSG_RAW_MOUSE_MIDDLE_BUTTON_DOWN:
  177. case GameMessage::MSG_RAW_MOUSE_MIDDLE_DOUBLE_CLICK:
  178. case GameMessage::MSG_RAW_MOUSE_MIDDLE_BUTTON_UP:
  179. case GameMessage::MSG_RAW_MOUSE_RIGHT_BUTTON_DOWN:
  180. case GameMessage::MSG_RAW_MOUSE_RIGHT_DOUBLE_CLICK:
  181. case GameMessage::MSG_RAW_MOUSE_RIGHT_BUTTON_UP:
  182. {
  183. // all window events have the position of the mouse as arg 0
  184. ICoord2D mousePos = msg->getArgument( 0 )->pixel;
  185. #if defined(_DEBUG) || defined(_INTERNAL) //debug hack to view object under mouse stats
  186. TheMousePos.x = mousePos.x;
  187. TheMousePos.y = mousePos.y;
  188. #endif
  189. // process the mouse event position
  190. GameWindowMessage gwm = rawMouseToWindowMessage( msg );
  191. if( TheWindowManager )
  192. returnCode = TheWindowManager->winProcessMouseEvent( gwm, &mousePos, NULL );
  193. if( TheShell && TheShell->isShellActive() )
  194. returnCode = WIN_INPUT_USED;
  195. if ( TheInGameUI && TheInGameUI->getInputEnabled() == FALSE )
  196. returnCode = WIN_INPUT_USED;
  197. break;
  198. } // end, raw mouse position
  199. // ------------------------------------------------------------------------
  200. case GameMessage::MSG_RAW_MOUSE_LEFT_DRAG:
  201. case GameMessage::MSG_RAW_MOUSE_MIDDLE_DRAG:
  202. case GameMessage::MSG_RAW_MOUSE_RIGHT_DRAG:
  203. {
  204. // all window events have the position of the mouse as arg 0
  205. ICoord2D mousePos = msg->getArgument( 0 )->pixel;
  206. // get delta for drag
  207. ICoord2D delta = msg->getArgument( 1 )->pixel;
  208. // process drag event
  209. GameWindowMessage gwm = rawMouseToWindowMessage( msg );
  210. if( TheWindowManager )
  211. returnCode = TheWindowManager->winProcessMouseEvent( gwm, &mousePos, &delta );
  212. if( TheShell && TheShell->isShellActive() )
  213. returnCode = WIN_INPUT_USED;
  214. if ( TheInGameUI && TheInGameUI->getInputEnabled() == FALSE )
  215. returnCode = WIN_INPUT_USED;
  216. break;
  217. } // end drag mouse
  218. // ------------------------------------------------------------------------
  219. case GameMessage::MSG_RAW_MOUSE_WHEEL:
  220. {
  221. // all window events have the position of the mouse as arg 0
  222. ICoord2D mousePos = msg->getArgument( 0 )->pixel;
  223. // get wheel position
  224. Int wheelPos = msg->getArgument( 1 )->integer;
  225. // process wheel event
  226. GameWindowMessage gwm = rawMouseToWindowMessage( msg );
  227. if( TheWindowManager )
  228. returnCode = TheWindowManager->winProcessMouseEvent( gwm, &mousePos,
  229. &wheelPos );
  230. if( TheShell && TheShell->isShellActive() )
  231. returnCode = WIN_INPUT_USED;
  232. if ( TheInGameUI && TheInGameUI->getInputEnabled() == FALSE )
  233. returnCode = WIN_INPUT_USED;
  234. break;
  235. } // end mouse wheel
  236. // ------------------------------------------------------------------------
  237. case GameMessage::MSG_RAW_KEY_DOWN:
  238. case GameMessage::MSG_RAW_KEY_UP:
  239. {
  240. // get key and state from args
  241. UnsignedByte key = msg->getArgument( 0 )->integer;
  242. UnsignedByte state = msg->getArgument( 1 )->integer;
  243. // process event through window system
  244. if( TheWindowManager )
  245. returnCode = TheWindowManager->winProcessKey( key, state );
  246. // If we're in a movie, we want to be able to escape out of it
  247. if(returnCode != WIN_INPUT_USED
  248. && (key == KEY_ESC)
  249. && (BitTest( state, KEY_STATE_UP ))
  250. && TheDisplay->isMoviePlaying()
  251. && TheGlobalData->m_allowExitOutOfMovies == TRUE )
  252. {
  253. TheDisplay->stopMovie();
  254. returnCode = WIN_INPUT_USED;
  255. }
  256. if(returnCode != WIN_INPUT_USED
  257. && (key == KEY_ESC)
  258. && (BitTest( state, KEY_STATE_UP ))
  259. && (TheInGameUI && (TheInGameUI->getInputEnabled() == FALSE)) )
  260. {
  261. returnCode = WIN_INPUT_USED;
  262. }
  263. break;
  264. } // end key messages
  265. // ------------------------------------------------------------------------
  266. default:
  267. break;
  268. } // end switch( msg->getType() )
  269. // remove event from the stream if the return code specifies to do so
  270. // If TheShell doesn't exist, then well, we're not in RTS, we're in GUIEdit
  271. if( returnCode == WIN_INPUT_USED && !forceKeepMessage )// || (TheShell && TheShell->isShellActive()))
  272. {
  273. disp = DESTROY_MESSAGE;
  274. }
  275. return disp;
  276. }