DisconnectMenu.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  24. #include "GameClient/DisconnectMenu.h"
  25. #include "GameClient/GUICallbacks.h"
  26. #include "Common/NameKeyGenerator.h"
  27. #include "GameClient/GameWindow.h"
  28. #include "GameClient/GameWindowManager.h"
  29. #include "GameClient/GadgetStaticText.h"
  30. #include "GameClient/GadgetListBox.h"
  31. #include "GameClient/GameText.h"
  32. #include "GameNetwork/GameInfo.h"
  33. #include "GameNetwork/NetworkInterface.h"
  34. char *DisconnectMenu::m_playerNameTextControlNames[] = {
  35. "DisconnectScreen.wnd:StaticPlayer1Name",
  36. "DisconnectScreen.wnd:StaticPlayer2Name",
  37. "DisconnectScreen.wnd:StaticPlayer3Name",
  38. "DisconnectScreen.wnd:StaticPlayer4Name",
  39. "DisconnectScreen.wnd:StaticPlayer5Name",
  40. "DisconnectScreen.wnd:StaticPlayer6Name",
  41. "DisconnectScreen.wnd:StaticPlayer7Name",
  42. NULL
  43. };
  44. char *DisconnectMenu::m_playerTimeoutTextControlNames[] = {
  45. "DisconnectScreen.wnd:StaticPlayer1Timeout",
  46. "DisconnectScreen.wnd:StaticPlayer2Timeout",
  47. "DisconnectScreen.wnd:StaticPlayer3Timeout",
  48. "DisconnectScreen.wnd:StaticPlayer4Timeout",
  49. "DisconnectScreen.wnd:StaticPlayer5Timeout",
  50. "DisconnectScreen.wnd:StaticPlayer6Timeout",
  51. "DisconnectScreen.wnd:StaticPlayer7Timeout",
  52. NULL
  53. };
  54. char *DisconnectMenu::m_playerVoteButtonControlNames[] = {
  55. "DisconnectScreen.wnd:ButtonKickPlayer1",
  56. "DisconnectScreen.wnd:ButtonKickPlayer2",
  57. "DisconnectScreen.wnd:ButtonKickPlayer3",
  58. "DisconnectScreen.wnd:ButtonKickPlayer4",
  59. "DisconnectScreen.wnd:ButtonKickPlayer5",
  60. "DisconnectScreen.wnd:ButtonKickPlayer6",
  61. "DisconnectScreen.wnd:ButtonKickPlayer7",
  62. NULL
  63. };
  64. char *DisconnectMenu::m_playerVoteCountControlNames[] = {
  65. "DisconnectScreen.wnd:StaticPlayer1Votes",
  66. "DisconnectScreen.wnd:StaticPlayer2Votes",
  67. "DisconnectScreen.wnd:StaticPlayer3Votes",
  68. "DisconnectScreen.wnd:StaticPlayer4Votes",
  69. "DisconnectScreen.wnd:StaticPlayer5Votes",
  70. "DisconnectScreen.wnd:StaticPlayer6Votes",
  71. "DisconnectScreen.wnd:StaticPlayer7Votes",
  72. NULL
  73. };
  74. char *DisconnectMenu::m_packetRouterTimeoutControlName = "DisconnectScreen.wnd:StaticPacketRouterTimeout";
  75. char *DisconnectMenu::m_packetRouterTimeoutLabelControlName = "DisconnectScreen.wnd:StaticPacketRouterTimeoutLabel";
  76. char *DisconnectMenu::m_textDisplayControlName = "DisconnectScreen.wnd:ListboxTextDisplay";
  77. static const Color chatNormalColor = GameMakeColor(255,0,0,255);
  78. DisconnectMenu *TheDisconnectMenu = NULL;
  79. DisconnectMenu::DisconnectMenu() {
  80. m_disconnectManager = NULL;
  81. }
  82. DisconnectMenu::~DisconnectMenu() {
  83. }
  84. void DisconnectMenu::init() {
  85. m_disconnectManager = NULL;
  86. HideDisconnectWindow();
  87. m_menuState = DISCONNECTMENUSTATETYPE_SCREENOFF;
  88. }
  89. void DisconnectMenu::attachDisconnectManager(DisconnectManager *disconnectManager) {
  90. m_disconnectManager = disconnectManager;
  91. }
  92. void DisconnectMenu::showScreen() {
  93. HideDiplomacy();
  94. HideInGameChat();
  95. HideQuitMenu();
  96. ShowDisconnectWindow();
  97. m_menuState = DISCONNECTMENUSTATETYPE_SCREENON;
  98. }
  99. void DisconnectMenu::hideScreen() {
  100. HideDisconnectWindow();
  101. m_menuState = DISCONNECTMENUSTATETYPE_SCREENOFF;
  102. }
  103. void DisconnectMenu::setPlayerName(Int playerNum, UnicodeString name) {
  104. NameKeyType id = TheNameKeyGenerator->nameToKey(m_playerNameTextControlNames[playerNum]);
  105. GameWindow *control = TheWindowManager->winGetWindowFromId(NULL, id);
  106. if (control != NULL) {
  107. if (name.getLength() > 0) {
  108. GadgetStaticTextSetText(control, name);
  109. // showPlayerControls(playerNum);
  110. }
  111. }
  112. id = TheNameKeyGenerator->nameToKey(m_playerTimeoutTextControlNames[playerNum]);
  113. control = TheWindowManager->winGetWindowFromId(NULL, id);
  114. if (control != NULL) {
  115. if (name.getLength() > 0) {
  116. GadgetStaticTextSetText(control, UnicodeString(L""));
  117. }
  118. }
  119. if (name.getLength() > 0) {
  120. showPlayerControls(playerNum);
  121. } else {
  122. hidePlayerControls(playerNum);
  123. }
  124. }
  125. void DisconnectMenu::setPlayerTimeoutTime(Int playerNum, time_t newTime) {
  126. NameKeyType id = TheNameKeyGenerator->nameToKey(m_playerTimeoutTextControlNames[playerNum]);
  127. GameWindow *control = TheWindowManager->winGetWindowFromId(NULL, id);
  128. char str[33]; // itoa uses a max of 33 bytes.
  129. itoa(newTime, str, 10);
  130. AsciiString asciiNum;
  131. asciiNum.set(str);
  132. UnicodeString uninum;
  133. uninum.translate(asciiNum);
  134. if (control != NULL) {
  135. GadgetStaticTextSetText(control, uninum);
  136. }
  137. }
  138. void DisconnectMenu::showPlayerControls(Int slot) {
  139. NameKeyType id = TheNameKeyGenerator->nameToKey(m_playerNameTextControlNames[slot]);
  140. GameWindow *control = TheWindowManager->winGetWindowFromId(NULL, id);
  141. if (control != NULL) {
  142. control->winHide(FALSE);
  143. }
  144. id = TheNameKeyGenerator->nameToKey(m_playerTimeoutTextControlNames[slot]);
  145. control = TheWindowManager->winGetWindowFromId(NULL, id);
  146. if (control != NULL) {
  147. control->winHide(FALSE);
  148. }
  149. id = TheNameKeyGenerator->nameToKey(m_playerVoteButtonControlNames[slot]);
  150. control = TheWindowManager->winGetWindowFromId(NULL, id);
  151. if (control != NULL) {
  152. control->winHide(FALSE);
  153. // Disallow voting for 2-player games. Cheating punk.
  154. if ( TheGameInfo && TheGameInfo->getNumPlayers() < 3 )
  155. {
  156. control->winEnable(FALSE);
  157. }
  158. else
  159. {
  160. control->winEnable(TRUE);
  161. }
  162. }
  163. id = TheNameKeyGenerator->nameToKey(m_playerVoteCountControlNames[slot]);
  164. control = TheWindowManager->winGetWindowFromId(NULL, id);
  165. if (control != NULL) {
  166. control->winHide(FALSE);
  167. }
  168. }
  169. void DisconnectMenu::hidePlayerControls(Int slot) {
  170. NameKeyType id = TheNameKeyGenerator->nameToKey(m_playerNameTextControlNames[slot]);
  171. GameWindow *control = TheWindowManager->winGetWindowFromId(NULL, id);
  172. if (control != NULL) {
  173. control->winHide(TRUE);
  174. }
  175. id = TheNameKeyGenerator->nameToKey(m_playerTimeoutTextControlNames[slot]);
  176. control = TheWindowManager->winGetWindowFromId(NULL, id);
  177. if (control != NULL) {
  178. control->winHide(TRUE);
  179. }
  180. id = TheNameKeyGenerator->nameToKey(m_playerVoteButtonControlNames[slot]);
  181. control = TheWindowManager->winGetWindowFromId(NULL, id);
  182. if (control != NULL) {
  183. control->winHide(TRUE);
  184. // Disallow voting for 2-player games. Cheating punk.
  185. if ( TheGameInfo && TheGameInfo->getNumPlayers() < 3 )
  186. {
  187. control->winEnable(FALSE);
  188. }
  189. else
  190. {
  191. control->winEnable(TRUE);
  192. }
  193. }
  194. id = TheNameKeyGenerator->nameToKey(m_playerVoteCountControlNames[slot]);
  195. control = TheWindowManager->winGetWindowFromId(NULL, id);
  196. if (control != NULL) {
  197. control->winHide(TRUE);
  198. }
  199. }
  200. void DisconnectMenu::showPacketRouterTimeout() {
  201. NameKeyType id = TheNameKeyGenerator->nameToKey(m_packetRouterTimeoutLabelControlName);
  202. GameWindow *control = TheWindowManager->winGetWindowFromId(NULL, id);
  203. if (control != NULL) {
  204. control->winHide(FALSE);
  205. }
  206. id = TheNameKeyGenerator->nameToKey(m_packetRouterTimeoutControlName);
  207. control = TheWindowManager->winGetWindowFromId(NULL, id);
  208. if (control != NULL) {
  209. GadgetStaticTextSetText(control, UnicodeString(L"")); // start it off with a blank string.
  210. control->winHide(FALSE);
  211. }
  212. }
  213. void DisconnectMenu::hidePacketRouterTimeout() {
  214. NameKeyType id = TheNameKeyGenerator->nameToKey(m_packetRouterTimeoutLabelControlName);
  215. GameWindow *control = TheWindowManager->winGetWindowFromId(NULL, id);
  216. if (control != NULL) {
  217. control->winHide(TRUE);
  218. }
  219. id = TheNameKeyGenerator->nameToKey(m_packetRouterTimeoutControlName);
  220. control = TheWindowManager->winGetWindowFromId(NULL, id);
  221. if (control != NULL) {
  222. control->winHide(TRUE);
  223. }
  224. }
  225. void DisconnectMenu::setPacketRouterTimeoutTime(time_t newTime) {
  226. NameKeyType id = TheNameKeyGenerator->nameToKey(m_packetRouterTimeoutControlName);
  227. GameWindow *control = TheWindowManager->winGetWindowFromId(NULL, id);
  228. char str[33]; // itoa uses a max of 33 bytes.
  229. itoa(newTime, str, 10);
  230. AsciiString asciiNum;
  231. asciiNum.set(str);
  232. UnicodeString uninum;
  233. uninum.translate(asciiNum);
  234. if (control != NULL) {
  235. GadgetStaticTextSetText(control, uninum);
  236. }
  237. }
  238. void DisconnectMenu::sendChat(UnicodeString text) {
  239. TheNetwork->sendDisconnectChat(text);
  240. }
  241. void DisconnectMenu::showChat(UnicodeString text) {
  242. NameKeyType displayID = TheNameKeyGenerator->nameToKey(m_textDisplayControlName);
  243. GameWindow *displayControl = TheWindowManager->winGetWindowFromId(NULL, displayID);
  244. if (displayControl != NULL) {
  245. GadgetListBoxAddEntryText(displayControl, text, chatNormalColor, -1, -1);
  246. }
  247. }
  248. void DisconnectMenu::quitGame() {
  249. TheNetwork->quitGame();
  250. }
  251. void DisconnectMenu::removePlayer(Int slot, UnicodeString playerName) {
  252. hidePlayerControls(slot);
  253. NameKeyType displayID = TheNameKeyGenerator->nameToKey(m_textDisplayControlName);
  254. GameWindow *displayControl = TheWindowManager->winGetWindowFromId(NULL, displayID);
  255. UnicodeString text;
  256. // UnicodeString name;
  257. // name.translate(playerName);
  258. text.format(TheGameText->fetch("Network:PlayerLeftGame"), playerName.str());
  259. if (displayControl != NULL) {
  260. GadgetListBoxAddEntryText(displayControl, text, chatNormalColor, -1, -1);
  261. }
  262. }
  263. void DisconnectMenu::voteForPlayer(Int slot) {
  264. DEBUG_LOG(("Casting vote for disconnect slot %d\n", slot));
  265. TheNetwork->voteForPlayerDisconnect(slot); // Do this next.
  266. }
  267. void DisconnectMenu::updateVotes(Int slot, Int votes) {
  268. NameKeyType id = TheNameKeyGenerator->nameToKey(m_playerVoteCountControlNames[slot]);
  269. GameWindow *control = TheWindowManager->winGetWindowFromId(NULL, id);
  270. if (control != NULL) {
  271. char votestr[16];
  272. itoa(votes, votestr, 10);
  273. AsciiString asciivotes;
  274. asciivotes.set(votestr);
  275. UnicodeString unistr;
  276. unistr.translate(asciivotes);
  277. GadgetStaticTextSetText(control, unistr);
  278. }
  279. }