DisconnectMenu.cpp 9.9 KB

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