WindowXlat.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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: 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. //Kris: Aug 15, 2003
  150. //Added the scrolling check that will not return KEEP_MESSAGE if we happen
  151. //to in scrolling mode (via keyboard or mouse) and left click in the controlbar.
  152. //Without this code, the left click goes through the interface ignoring buttons and blockage
  153. //and ends up issuing orders right through the controlbar!
  154. if( TheInGameUI->isScrolling() )
  155. {
  156. if( msg->getType() != GameMessage::MSG_RAW_MOUSE_LEFT_BUTTON_UP &&
  157. msg->getType() != GameMessage::MSG_RAW_MOUSE_LEFT_BUTTON_DOWN )
  158. {
  159. //We're scrolling, but unless we're clicking the left button, get out.
  160. return KEEP_MESSAGE;
  161. }
  162. //Pass through and handle button clicks or getting input blocked!
  163. }
  164. else
  165. {
  166. return KEEP_MESSAGE;
  167. }
  168. }
  169. switch( msg->getType() )
  170. {
  171. // ------------------------------------------------------------------------
  172. case GameMessage::MSG_META_TOGGLE_ATTACKMOVE:
  173. {
  174. // Basically, we're cheating here. The mouse no longer sends us useless spam.
  175. ICoord2D mousePos = TheMouse->getMouseStatus()->pos;
  176. if( TheWindowManager )
  177. TheWindowManager->winProcessMouseEvent( GWM_NONE, &mousePos, NULL );
  178. // Force it to keep the message, regardless of what the window thinks it did with the input.
  179. return KEEP_MESSAGE;
  180. }
  181. // ------------------------------------------------------------------------
  182. case GameMessage::MSG_RAW_MOUSE_LEFT_BUTTON_UP:
  183. {
  184. if( TheInGameUI && TheInGameUI->isPlacementAnchored() )
  185. {
  186. //If we release the button outside
  187. forceKeepMessage = TRUE;
  188. }
  189. //FALL THROUGH INTENTIONALLY!
  190. }
  191. case GameMessage::MSG_RAW_MOUSE_POSITION:
  192. case GameMessage::MSG_RAW_MOUSE_LEFT_BUTTON_DOWN:
  193. case GameMessage::MSG_RAW_MOUSE_LEFT_DOUBLE_CLICK:
  194. case GameMessage::MSG_RAW_MOUSE_MIDDLE_BUTTON_DOWN:
  195. case GameMessage::MSG_RAW_MOUSE_MIDDLE_DOUBLE_CLICK:
  196. case GameMessage::MSG_RAW_MOUSE_MIDDLE_BUTTON_UP:
  197. case GameMessage::MSG_RAW_MOUSE_RIGHT_BUTTON_DOWN:
  198. case GameMessage::MSG_RAW_MOUSE_RIGHT_DOUBLE_CLICK:
  199. case GameMessage::MSG_RAW_MOUSE_RIGHT_BUTTON_UP:
  200. {
  201. // all window events have the position of the mouse as arg 0
  202. ICoord2D mousePos = msg->getArgument( 0 )->pixel;
  203. #if defined(_DEBUG) || defined(_INTERNAL) //debug hack to view object under mouse stats
  204. TheMousePos.x = mousePos.x;
  205. TheMousePos.y = mousePos.y;
  206. #endif
  207. // process the mouse event position
  208. GameWindowMessage gwm = rawMouseToWindowMessage( msg );
  209. if( TheWindowManager )
  210. returnCode = TheWindowManager->winProcessMouseEvent( gwm, &mousePos, NULL );
  211. if( TheShell && TheShell->isShellActive() )
  212. returnCode = WIN_INPUT_USED;
  213. if ( TheInGameUI && TheInGameUI->getInputEnabled() == FALSE )
  214. returnCode = WIN_INPUT_USED;
  215. break;
  216. } // end, raw mouse position
  217. // ------------------------------------------------------------------------
  218. case GameMessage::MSG_RAW_MOUSE_LEFT_DRAG:
  219. case GameMessage::MSG_RAW_MOUSE_MIDDLE_DRAG:
  220. case GameMessage::MSG_RAW_MOUSE_RIGHT_DRAG:
  221. {
  222. // all window events have the position of the mouse as arg 0
  223. ICoord2D mousePos = msg->getArgument( 0 )->pixel;
  224. // get delta for drag
  225. ICoord2D delta = msg->getArgument( 1 )->pixel;
  226. // process drag event
  227. GameWindowMessage gwm = rawMouseToWindowMessage( msg );
  228. if( TheWindowManager )
  229. returnCode = TheWindowManager->winProcessMouseEvent( gwm, &mousePos, &delta );
  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 drag mouse
  236. // ------------------------------------------------------------------------
  237. case GameMessage::MSG_RAW_MOUSE_WHEEL:
  238. {
  239. // all window events have the position of the mouse as arg 0
  240. ICoord2D mousePos = msg->getArgument( 0 )->pixel;
  241. // get wheel position
  242. Int wheelPos = msg->getArgument( 1 )->integer;
  243. // process wheel event
  244. GameWindowMessage gwm = rawMouseToWindowMessage( msg );
  245. if( TheWindowManager )
  246. returnCode = TheWindowManager->winProcessMouseEvent( gwm, &mousePos,
  247. &wheelPos );
  248. if( TheShell && TheShell->isShellActive() )
  249. returnCode = WIN_INPUT_USED;
  250. if ( TheInGameUI && TheInGameUI->getInputEnabled() == FALSE )
  251. returnCode = WIN_INPUT_USED;
  252. break;
  253. } // end mouse wheel
  254. // ------------------------------------------------------------------------
  255. case GameMessage::MSG_RAW_KEY_DOWN:
  256. case GameMessage::MSG_RAW_KEY_UP:
  257. {
  258. // get key and state from args
  259. UnsignedByte key = msg->getArgument( 0 )->integer;
  260. UnsignedByte state = msg->getArgument( 1 )->integer;
  261. // process event through window system
  262. if( TheWindowManager )
  263. returnCode = TheWindowManager->winProcessKey( key, state );
  264. // If we're in a movie, we want to be able to escape out of it
  265. if(returnCode != WIN_INPUT_USED
  266. && (key == KEY_ESC)
  267. && (BitTest( state, KEY_STATE_UP ))
  268. && TheDisplay->isMoviePlaying()
  269. && TheGlobalData->m_allowExitOutOfMovies == TRUE )
  270. {
  271. TheDisplay->stopMovie();
  272. returnCode = WIN_INPUT_USED;
  273. }
  274. if(returnCode != WIN_INPUT_USED
  275. && (key == KEY_ESC)
  276. && (BitTest( state, KEY_STATE_UP ))
  277. && (TheInGameUI && (TheInGameUI->getInputEnabled() == FALSE)) )
  278. {
  279. returnCode = WIN_INPUT_USED;
  280. }
  281. break;
  282. } // end key messages
  283. // ------------------------------------------------------------------------
  284. default:
  285. break;
  286. } // end switch( msg->getType() )
  287. // remove event from the stream if the return code specifies to do so
  288. // If TheShell doesn't exist, then well, we're not in RTS, we're in GUIEdit
  289. if( returnCode == WIN_INPUT_USED && !forceKeepMessage )// || (TheShell && TheShell->isShellActive()))
  290. {
  291. disp = DESTROY_MESSAGE;
  292. }
  293. return disp;
  294. }