HotKey.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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: HotKey.cpp /////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Electronic Arts Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2002 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // created: Sep 2002
  34. //
  35. // Filename: HotKey.cpp
  36. //
  37. // author: Chris Huybregts
  38. //
  39. // purpose:
  40. //
  41. //-----------------------------------------------------------------------------
  42. ///////////////////////////////////////////////////////////////////////////////
  43. //-----------------------------------------------------------------------------
  44. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  45. //-----------------------------------------------------------------------------
  46. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  47. //-----------------------------------------------------------------------------
  48. // USER INCLUDES //////////////////////////////////////////////////////////////
  49. //-----------------------------------------------------------------------------
  50. #include "GameClient/HotKey.h"
  51. #include "GameClient/KeyDefs.h"
  52. #include "GameClient/MetaEvent.h"
  53. #include "GameClient/GameWindow.h"
  54. #include "GameClient/GameWindowManager.h"
  55. #include "GameClient/keyboard.h"
  56. #include "GameClient/GameText.h"
  57. #include "Common/AudioEventRTS.h"
  58. //-----------------------------------------------------------------------------
  59. // DEFINES ////////////////////////////////////////////////////////////////////
  60. //-----------------------------------------------------------------------------
  61. //-----------------------------------------------------------------------------
  62. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
  63. //-----------------------------------------------------------------------------
  64. //-----------------------------------------------------------------------------
  65. GameMessageDisposition HotKeyTranslator::translateGameMessage(const GameMessage *msg)
  66. {
  67. GameMessageDisposition disp = KEEP_MESSAGE;
  68. GameMessage::Type t = msg->getType();
  69. if ( t == GameMessage::MSG_RAW_KEY_UP)
  70. {
  71. //char key = msg->getArgument(0)->integer;
  72. Int keyState = msg->getArgument(1)->integer;
  73. // for our purposes here, we don't care to distinguish between right and left keys,
  74. // so just fudge a little to simplify things.
  75. Int newModState = 0;
  76. if( keyState & KEY_STATE_CONTROL )
  77. {
  78. newModState |= CTRL;
  79. }
  80. if( keyState & KEY_STATE_SHIFT )
  81. {
  82. newModState |= SHIFT;
  83. }
  84. if( keyState & KEY_STATE_ALT )
  85. {
  86. newModState |= ALT;
  87. }
  88. if(newModState != 0)
  89. return disp;
  90. WideChar key = TheKeyboard->getPrintableKey(msg->getArgument(0)->integer, 0);
  91. UnicodeString uKey;
  92. uKey.set(&key);
  93. AsciiString aKey;
  94. aKey.translate(uKey);
  95. if(TheHotKeyManager && TheHotKeyManager->executeHotKey(aKey))
  96. disp = DESTROY_MESSAGE;
  97. }
  98. return disp;
  99. }
  100. //-----------------------------------------------------------------------------
  101. HotKey::HotKey()
  102. {
  103. m_win = NULL;
  104. //Added By Sadullah Nader
  105. //Initializations missing and needed
  106. m_key.clear();
  107. //
  108. }
  109. //-----------------------------------------------------------------------------
  110. HotKeyManager::HotKeyManager( void )
  111. {
  112. }
  113. //-----------------------------------------------------------------------------
  114. HotKeyManager::~HotKeyManager( void )
  115. {
  116. m_hotKeyMap.clear();
  117. }
  118. //-----------------------------------------------------------------------------
  119. void HotKeyManager::init( void )
  120. {
  121. m_hotKeyMap.clear();
  122. }
  123. //-----------------------------------------------------------------------------
  124. void HotKeyManager::reset( void )
  125. {
  126. m_hotKeyMap.clear();
  127. }
  128. //-----------------------------------------------------------------------------
  129. void HotKeyManager::addHotKey( GameWindow *win, const AsciiString& keyIn)
  130. {
  131. AsciiString key = keyIn;
  132. key.toLower();
  133. HotKeyMap::iterator it = m_hotKeyMap.find(key);
  134. if( it != m_hotKeyMap.end() )
  135. {
  136. DEBUG_ASSERTCRASH(FALSE,("Hotkey %s is already mapped to window %s, current window is %s", key.str(), it->second.m_win->winGetInstanceData()->m_decoratedNameString.str(), win->winGetInstanceData()->m_decoratedNameString.str()));
  137. return;
  138. }
  139. HotKey newHK;
  140. newHK.m_key.set(key);
  141. newHK.m_win = win;
  142. m_hotKeyMap[key] = newHK;
  143. }
  144. //-----------------------------------------------------------------------------
  145. Bool HotKeyManager::executeHotKey( const AsciiString& keyIn )
  146. {
  147. AsciiString key = keyIn;
  148. key.toLower();
  149. HotKeyMap::iterator it = m_hotKeyMap.find(key);
  150. if( it == m_hotKeyMap.end() )
  151. return FALSE;
  152. GameWindow *win = it->second.m_win;
  153. if( !win )
  154. return FALSE;
  155. if( !BitTest( win->winGetStatus(), WIN_STATUS_HIDDEN ) )
  156. {
  157. if( BitTest( win->winGetStatus(), WIN_STATUS_ENABLED ) )
  158. {
  159. TheWindowManager->winSendSystemMsg( win->winGetParent(), GBM_SELECTED, (WindowMsgData)win, win->winGetWindowId() );
  160. // here we make the same click sound that the GUI uses when you click a button
  161. AudioEventRTS buttonClick("GUIClick");
  162. if( TheAudio )
  163. {
  164. TheAudio->addAudioEvent( &buttonClick );
  165. } // end if
  166. return TRUE;
  167. }
  168. AudioEventRTS disabledClick( "GUIClickDisabled" );
  169. if( TheAudio )
  170. {
  171. TheAudio->addAudioEvent( &disabledClick );
  172. }
  173. }
  174. return FALSE;
  175. }
  176. //-----------------------------------------------------------------------------
  177. AsciiString HotKeyManager::searchHotKey( const AsciiString& label)
  178. {
  179. return searchHotKey(TheGameText->fetch(label));
  180. }
  181. //-----------------------------------------------------------------------------
  182. AsciiString HotKeyManager::searchHotKey( const UnicodeString& uStr )
  183. {
  184. if(uStr.isEmpty())
  185. return AsciiString::TheEmptyString;
  186. const WideChar *marker = (const WideChar *)uStr.str();
  187. while (marker && *marker)
  188. {
  189. if (*marker == L'&')
  190. {
  191. // found a '&' - now look for the next char
  192. UnicodeString tmp = UnicodeString::TheEmptyString;
  193. tmp.concat(*(marker+1));
  194. AsciiString retStr;
  195. retStr.translate(tmp);
  196. return retStr;
  197. }
  198. marker++;
  199. }
  200. return AsciiString::TheEmptyString;
  201. }
  202. //-----------------------------------------------------------------------------
  203. HotKeyManager *TheHotKeyManager = NULL;
  204. //-----------------------------------------------------------------------------
  205. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
  206. //-----------------------------------------------------------------------------