ControlBarObserver.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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: ControlBarObserver.cpp /////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Electronic Arts Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2002 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // created: Aug 2002
  34. //
  35. // Filename: ControlBarObserver.cpp
  36. //
  37. // author: Chris Huybregts
  38. //
  39. // purpose: All things related to the Observer Control bar, are in here.
  40. //
  41. //-----------------------------------------------------------------------------
  42. ///////////////////////////////////////////////////////////////////////////////
  43. //-----------------------------------------------------------------------------
  44. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  45. //-----------------------------------------------------------------------------
  46. //-----------------------------------------------------------------------------
  47. // USER INCLUDES //////////////////////////////////////////////////////////////
  48. //-----------------------------------------------------------------------------
  49. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  50. #include "Common/NameKeyGenerator.h"
  51. #include "Common/PlayerList.h"
  52. #include "Common/Player.h"
  53. #include "Common/PlayerTemplate.h"
  54. #include "Common/KindOf.h"
  55. #include "Common/Recorder.h"
  56. #include "GameClient/ControlBar.h"
  57. #include "GameClient/GameWindowManager.h"
  58. #include "GameClient/GadgetPushButton.h"
  59. #include "GameClient/GadgetStaticText.h"
  60. #include "GameClient/GameText.h"
  61. #include "GameNetwork/NetworkDefs.h"
  62. //-----------------------------------------------------------------------------
  63. // DEFINES ////////////////////////////////////////////////////////////////////
  64. //-----------------------------------------------------------------------------
  65. enum { MAX_BUTTONS = 8};
  66. static NameKeyType buttonPlayerID[MAX_BUTTONS] = { NAMEKEY_INVALID,NAMEKEY_INVALID,
  67. NAMEKEY_INVALID,NAMEKEY_INVALID,
  68. NAMEKEY_INVALID,NAMEKEY_INVALID,
  69. NAMEKEY_INVALID,NAMEKEY_INVALID };
  70. static NameKeyType staticTextPlayerID[MAX_BUTTONS] = { NAMEKEY_INVALID,NAMEKEY_INVALID,
  71. NAMEKEY_INVALID,NAMEKEY_INVALID,
  72. NAMEKEY_INVALID,NAMEKEY_INVALID,
  73. NAMEKEY_INVALID,NAMEKEY_INVALID };
  74. static GameWindow *ObserverPlayerInfoWindow = NULL;
  75. static GameWindow *ObserverPlayerListWindow = NULL;
  76. static GameWindow *buttonPlayer[MAX_BUTTONS] = {NULL,NULL,NULL,NULL,
  77. NULL,NULL,NULL,NULL };
  78. static GameWindow *staticTextPlayer[MAX_BUTTONS] = {NULL,NULL,NULL,NULL,
  79. NULL,NULL,NULL,NULL };
  80. static NameKeyType buttonCancelID = NAMEKEY_INVALID;
  81. static GameWindow *winFlag = NULL;
  82. static GameWindow *winGeneralPortrait = NULL;
  83. static GameWindow *staticTextNumberOfUnits = NULL;
  84. static GameWindow *staticTextNumberOfBuildings = NULL;
  85. static GameWindow *staticTextNumberOfUnitsKilled = NULL;
  86. static GameWindow *staticTextNumberOfUnitsLost = NULL;
  87. static GameWindow *staticTextPlayerName = NULL;
  88. //-----------------------------------------------------------------------------
  89. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
  90. //-----------------------------------------------------------------------------
  91. void ControlBar::initObserverControls( void )
  92. {
  93. ObserverPlayerInfoWindow = TheWindowManager->winGetWindowFromId(NULL, TheNameKeyGenerator->nameToKey("ControlBar.wnd:ObserverPlayerInfoWindow"));
  94. ObserverPlayerListWindow = TheWindowManager->winGetWindowFromId(NULL, TheNameKeyGenerator->nameToKey("ControlBar.wnd:ObserverPlayerListWindow"));
  95. for (Int i = 0; i < MAX_BUTTONS; i++)
  96. {
  97. AsciiString tmpString;
  98. tmpString.format("ControlBar.wnd:ButtonPlayer%d", i);
  99. buttonPlayerID[i] = TheNameKeyGenerator->nameToKey( tmpString );
  100. buttonPlayer[i] = TheWindowManager->winGetWindowFromId( ObserverPlayerListWindow, buttonPlayerID[i] );
  101. tmpString.format("ControlBar.wnd:StaticTextPlayer%d", i);
  102. staticTextPlayerID[i] = TheNameKeyGenerator->nameToKey( tmpString );
  103. staticTextPlayer[i] = TheWindowManager->winGetWindowFromId( ObserverPlayerListWindow, staticTextPlayerID[i] );
  104. }
  105. staticTextNumberOfUnits = TheWindowManager->winGetWindowFromId(NULL, TheNameKeyGenerator->nameToKey("ControlBar.wnd:StaticTextNumberOfUnits"));
  106. staticTextNumberOfBuildings = TheWindowManager->winGetWindowFromId(NULL, TheNameKeyGenerator->nameToKey("ControlBar.wnd:StaticTextNumberOfBuildings"));
  107. staticTextNumberOfUnitsKilled = TheWindowManager->winGetWindowFromId(NULL, TheNameKeyGenerator->nameToKey("ControlBar.wnd:StaticTextNumberOfUnitsKilled"));
  108. staticTextNumberOfUnitsLost = TheWindowManager->winGetWindowFromId(NULL, TheNameKeyGenerator->nameToKey("ControlBar.wnd:StaticTextNumberOfUnitsLost"));
  109. staticTextPlayerName = TheWindowManager->winGetWindowFromId(NULL, TheNameKeyGenerator->nameToKey("ControlBar.wnd:StaticTextPlayerName"));
  110. winFlag = TheWindowManager->winGetWindowFromId(NULL, TheNameKeyGenerator->nameToKey("ControlBar.wnd:WinFlag"));
  111. winGeneralPortrait = TheWindowManager->winGetWindowFromId(NULL, TheNameKeyGenerator->nameToKey("ControlBar.wnd:WinGeneralPortrait"));
  112. buttonCancelID = TheNameKeyGenerator->nameToKey("ControlBar.wnd:ButtonCancel");
  113. }
  114. //-------------------------------------------------------------------------------------------------
  115. /** System callback for the ControlBarObserverSystem */
  116. //-------------------------------------------------------------------------------------------------
  117. WindowMsgHandledType ControlBarObserverSystem( GameWindow *window, UnsignedInt msg,
  118. WindowMsgData mData1, WindowMsgData mData2 )
  119. {
  120. static NameKeyType buttonCommunicator = NAMEKEY_INVALID;
  121. switch( msg )
  122. {
  123. // --------------------------------------------------------------------------------------------
  124. case GWM_CREATE:
  125. {
  126. break;
  127. } // end create
  128. //---------------------------------------------------------------------------------------------
  129. case GBM_MOUSE_ENTERING:
  130. case GBM_MOUSE_LEAVING:
  131. {
  132. break;
  133. }
  134. //---------------------------------------------------------------------------------------------
  135. case GBM_SELECTED:
  136. case GBM_SELECTED_RIGHT:
  137. {
  138. GameWindow *control = (GameWindow *)mData1;
  139. Int controlID = control->winGetWindowId();
  140. if( controlID == buttonCancelID)
  141. {
  142. TheControlBar->setObserverLookAtPlayer(NULL);
  143. ObserverPlayerInfoWindow->winHide(TRUE);
  144. ObserverPlayerListWindow->winHide(FALSE);
  145. TheControlBar->populateObserverList();
  146. }
  147. for(Int i = 0; i <MAX_BUTTONS; ++i)
  148. {
  149. if( controlID == buttonPlayerID[i])
  150. {
  151. ObserverPlayerInfoWindow->winHide(FALSE);
  152. ObserverPlayerListWindow->winHide(TRUE);
  153. TheControlBar->setObserverLookAtPlayer((Player *) GadgetButtonGetData( buttonPlayer[i]));
  154. if(TheControlBar->getObserverLookAtPlayer())
  155. TheControlBar->populateObserverInfoWindow();
  156. return MSG_HANDLED;
  157. }
  158. }
  159. // if( controlID == buttonCommunicator && TheGameLogic->getGameMode() == GAME_INTERNET )
  160. /*
  161. {
  162. popupCommunicatorLayout = TheWindowManager->winCreateLayout( AsciiString( "Menus/PopupCommunicator.wnd" ) );
  163. popupCommunicatorLayout->runInit();
  164. popupCommunicatorLayout->hide( FALSE );
  165. popupCommunicatorLayout->bringForward();
  166. }
  167. */
  168. break;
  169. } // end button selected
  170. //---------------------------------------------------------------------------------------------
  171. default:
  172. return MSG_IGNORED;
  173. } // end switch( msg )
  174. return MSG_HANDLED;
  175. } // end ControlBarSystem
  176. //-----------------------------------------------------------------------------
  177. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
  178. //-----------------------------------------------------------------------------
  179. void ControlBar::populateObserverList( void )
  180. {
  181. Int currentButton = 0, i;
  182. if(TheRecorder->isMultiplayer())
  183. {
  184. for (i = 0; i < MAX_SLOTS; ++i)
  185. {
  186. AsciiString name;
  187. name.format("player%d", i);
  188. Player *p = ThePlayerList->findPlayerWithNameKey(TheNameKeyGenerator->nameToKey(name));
  189. if(p)
  190. {
  191. if(p->isPlayerObserver())
  192. continue;
  193. DEBUG_ASSERTCRASH(currentButton < MAX_BUTTONS, ("ControlBar::populateObserverList trying to populate more buttons then we have"));
  194. GadgetButtonSetData(buttonPlayer[currentButton], (void *)p);
  195. GadgetButtonSetEnabledImage( buttonPlayer[currentButton], p->getPlayerTemplate()->getEnabledImage() );
  196. //GadgetButtonSetHiliteImage( buttonPlayer[currentButton], p->getPlayerTemplate()->getHiliteImage() );
  197. //GadgetButtonSetHiliteSelectedImage( buttonPlayer[currentButton], p->getPlayerTemplate()->getPushedImage() );
  198. //GadgetButtonSetDisabledImage( buttonPlayer[currentButton], p->getPlayerTemplate()->getDisabledImage() );
  199. buttonPlayer[currentButton]->winSetTooltip(p->getPlayerDisplayName());
  200. buttonPlayer[currentButton]->winHide(FALSE);
  201. buttonPlayer[currentButton]->winSetStatus( WIN_STATUS_USE_OVERLAY_STATES );
  202. const GameSlot *slot = TheGameInfo->getConstSlot(currentButton);
  203. Color playerColor = p->getPlayerColor();
  204. Color backColor = GameMakeColor(0, 0, 0, 255);
  205. staticTextPlayer[currentButton]->winSetEnabledTextColors( playerColor, backColor );
  206. staticTextPlayer[currentButton]->winHide(FALSE);
  207. AsciiString teamStr;
  208. teamStr.format("Team:%d", slot->getTeamNumber() + 1);
  209. if (slot->isAI() && slot->getTeamNumber() == -1)
  210. teamStr = "Team:AI";
  211. UnicodeString text;
  212. text.format(TheGameText->fetch("CONTROLBAR:ObsPlayerLabel"), p->getPlayerDisplayName().str(),
  213. TheGameText->fetch(teamStr).str());
  214. GadgetStaticTextSetText(staticTextPlayer[currentButton], text );
  215. ++currentButton;
  216. }
  217. }
  218. for(currentButton; currentButton<MAX_BUTTONS; ++currentButton)
  219. {
  220. buttonPlayer[currentButton]->winHide(TRUE);
  221. staticTextPlayer[currentButton]->winHide(TRUE);
  222. }
  223. }
  224. else
  225. {
  226. for(i =0; i < MAX_PLAYER_COUNT; ++i)
  227. {
  228. Player *p = ThePlayerList->getNthPlayer(i);
  229. if(p && !p->isPlayerObserver() && p->getPlayerType() == PLAYER_HUMAN)
  230. {
  231. DEBUG_ASSERTCRASH(currentButton < MAX_BUTTONS, ("ControlBar::populateObserverList trying to populate more buttons then we have"));
  232. GadgetButtonSetData(buttonPlayer[currentButton], (void *)p);
  233. GadgetButtonSetEnabledImage( buttonPlayer[currentButton], p->getPlayerTemplate()->getEnabledImage() );
  234. //GadgetButtonSetHiliteImage( buttonPlayer[currentButton], p->getPlayerTemplate()->getHiliteImage() );
  235. //GadgetButtonSetHiliteSelectedImage( buttonPlayer[currentButton], p->getPlayerTemplate()->getPushedImage() );
  236. //GadgetButtonSetDisabledImage( buttonPlayer[currentButton], p->getPlayerTemplate()->getDisabledImage() );
  237. buttonPlayer[currentButton]->winSetTooltip(p->getPlayerDisplayName());
  238. buttonPlayer[currentButton]->winHide(FALSE);
  239. buttonPlayer[currentButton]->winSetStatus( WIN_STATUS_USE_OVERLAY_STATES );
  240. Color playerColor = p->getPlayerColor();
  241. Color backColor = GameMakeColor(0, 0, 0, 255);
  242. staticTextPlayer[currentButton]->winSetEnabledTextColors( playerColor, backColor );
  243. staticTextPlayer[currentButton]->winHide(FALSE);
  244. GadgetStaticTextSetText(staticTextPlayer[currentButton], p->getPlayerDisplayName());
  245. ++currentButton;
  246. break;
  247. }
  248. }
  249. for(currentButton; currentButton<MAX_BUTTONS; ++currentButton)
  250. {
  251. buttonPlayer[currentButton]->winHide(TRUE);
  252. staticTextPlayer[currentButton]->winHide(TRUE);
  253. }
  254. }
  255. }
  256. void ControlBar::populateObserverInfoWindow ( void )
  257. {
  258. if(ObserverPlayerInfoWindow->winIsHidden())
  259. return;
  260. if( !m_observerLookAtPlayer )
  261. {
  262. ObserverPlayerInfoWindow->winHide(TRUE);
  263. ObserverPlayerListWindow->winHide(FALSE);
  264. populateObserverList();
  265. return;
  266. }
  267. UnicodeString uString;
  268. KindOfMaskType mask,clearmask;
  269. mask.set(KINDOF_SCORE);
  270. clearmask.set(KINDOF_STRUCTURE);
  271. uString.format(L"%d",m_observerLookAtPlayer->countObjects(mask,clearmask));
  272. GadgetStaticTextSetText(staticTextNumberOfUnits, uString);
  273. Int numBuildings = 0;
  274. mask.clear();
  275. mask.set(KINDOF_SCORE);
  276. mask.set(KINDOF_STRUCTURE);
  277. clearmask.clear();
  278. numBuildings = m_observerLookAtPlayer->countObjects(mask,clearmask);
  279. mask.clear();
  280. mask.set(KINDOF_SCORE_CREATE);
  281. mask.set(KINDOF_STRUCTURE);
  282. numBuildings += m_observerLookAtPlayer->countObjects(mask,clearmask);
  283. mask.clear();
  284. mask.set(KINDOF_SCORE_DESTROY);
  285. mask.set(KINDOF_STRUCTURE);
  286. numBuildings += m_observerLookAtPlayer->countObjects(mask,clearmask);
  287. uString.format(L"%d",numBuildings);
  288. GadgetStaticTextSetText(staticTextNumberOfBuildings, uString);
  289. uString.format(L"%d",m_observerLookAtPlayer->getScoreKeeper()->getTotalUnitsDestroyed());
  290. GadgetStaticTextSetText(staticTextNumberOfUnitsKilled, uString);
  291. uString.format(L"%d",m_observerLookAtPlayer->getScoreKeeper()->getTotalUnitsLost());
  292. GadgetStaticTextSetText(staticTextNumberOfUnitsLost, uString);
  293. GadgetStaticTextSetText(staticTextPlayerName, m_observerLookAtPlayer->getPlayerDisplayName());
  294. Color color = m_observerLookAtPlayer->getPlayerColor();
  295. staticTextPlayerName->winSetEnabledTextColors(color, GameMakeColor(0,0,0,255));
  296. winFlag->winSetEnabledImage(0, m_observerLookAtPlayer->getPlayerTemplate()->getFlagWaterMarkImage());
  297. winGeneralPortrait->winHide(FALSE);
  298. }