1 /*
2  *  This file is part of Dune Legacy.
3  *
4  *  Dune Legacy is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  Dune Legacy is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with Dune Legacy.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <GUI/dune/WaitingForOtherPlayers.h>
19 
20 #include <globals.h>
21 
22 #include <FileClasses/TextManager.h>
23 #include <players/HumanPlayer.h>
24 #include <Network/NetworkManager.h>
25 
26 #include <Game.h>
27 
28 #include <list>
29 #include <string>
30 
31 
WaitingForOtherPlayers()32 WaitingForOtherPlayers::WaitingForOtherPlayers() : Window(50,50,50,50)
33 {
34     setWindowWidget(&vbox);
35     vbox.addWidget(VSpacer::create(6));
36 
37     vbox.addWidget(&textLabel);
38     vbox.addWidget(VSpacer::create(3));
39     vbox.addWidget(&hbox);
40     vbox.addWidget(VSpacer::create(6));
41     hbox.addWidget(Spacer::create());
42     hbox.addWidget(&vbox2);
43     vbox2.addWidget(VSpacer::create(4));
44     removeButton.setText(_("Remove"));
45     removeButton.setOnClick(std::bind(&WaitingForOtherPlayers::onRemove, this));
46     removeButton.setVisible(false);
47     removeButton.setEnabled(false);
48     vbox2.addWidget(&removeButton);
49     vbox2.addWidget(VSpacer::create(4));
50     hbox.addWidget(Spacer::create());
51     textLabel.setAlignment(Alignment_HCenter);
52 
53     update();
54 
55     int xpos = std::max(0,(getRendererWidth() - getSize().x)/2);
56     int ypos = std::max(0,(getRendererHeight() - getSize().y)/2);
57 
58     setCurrentPosition(xpos,ypos,getSize().x,getSize().y);
59 }
60 
~WaitingForOtherPlayers()61 WaitingForOtherPlayers::~WaitingForOtherPlayers()
62 {
63     ;
64 }
65 
update()66 void WaitingForOtherPlayers::update() {
67     std::string text = _("Waiting for other players... ");
68 
69     // test if we need to wait for data to arrive
70     for(const std::string& playername : pNetworkManager->getConnectedPeers()) {
71         HumanPlayer* pPlayer = dynamic_cast<HumanPlayer*>(currentGame->getPlayerByName(playername));
72         if(pPlayer != nullptr) {
73             if(pPlayer->nextExpectedCommandsCycle <= currentGame->getGameCycleCount()) {
74                 text += "\n" + pPlayer->getPlayername();
75             }
76         }
77     }
78 
79     setText(text);
80 }
81 
onRemove()82 void WaitingForOtherPlayers::onRemove()
83 {
84     currentGame->resumeGame();
85 }
86