Implemented server selection interface

This commit is contained in:
Kayne Ruse
2013-05-20 18:54:10 +10:00
parent d76cbe13da
commit bf73b542f1
4 changed files with 50 additions and 23 deletions
+43 -14
View File
@@ -20,17 +20,19 @@ Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUti
//members
font.SetSurface(surfaceMgr->Get("font"));
pingButton.SetSurfaces(surfaceMgr->Get("button"), surfaceMgr->Get("font"));
pingButton.SetText("Refresh");
pingButton.SetX(50);
pingButton.SetY(50);
//ping the network, ping the preset "phone home" servers
//generate the server list
//eventually
buttonMap["ping"] = new Button(50, 50 , surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Refresh");
buttonMap["join"] = new Button(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Join");
buttonMap["back"] = new Button(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Back");
//ping the network
PingNetwork();
}
Lobby::~Lobby() {
for (auto it : buttonMap) {
delete it.second;
}
#ifdef DEBUG
cout << "leaving Lobby" << endl;
#endif
@@ -67,9 +69,16 @@ void Lobby::Receive() {
}
void Lobby::Render(SDL_Surface* const screen) {
pingButton.DrawTo(screen);
for (auto it : buttonMap) {
it.second->DrawTo(screen);
}
//draw the server list, highlighting the selected server
for (int i = 0; i < serverVector.size(); i++) {
font.DrawStringTo(serverVector[i].name, screen, 50, 16*i + 100);
if (selectedServer && selectedServer == &serverVector[i]) {
SDL_Rect clip = {250, Sint16(16*i + 50), Uint16(screen->w - 250), 16};
SDL_FillRect(screen, &clip, SDL_MapRGB(screen->format, 255, 127, 39));
}
font.DrawStringTo(serverVector[i].name, screen, 250, 16*i + 50);
}
}
@@ -78,23 +87,43 @@ void Lobby::Render(SDL_Surface* const screen) {
//-------------------------
void Lobby::MouseMotion(SDL_MouseMotionEvent const& motion) {
pingButton.MouseMotion(motion);
for (auto it : buttonMap) {
it.second->MouseMotion(motion);
}
}
void Lobby::MouseButtonDown(SDL_MouseButtonEvent const& button) {
pingButton.MouseButtonDown(button);
for (auto it : buttonMap) {
it.second->MouseButtonDown(button);
}
}
void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (pingButton.MouseButtonUp(button) == Button::State::HOVER) {
if (buttonMap["ping"]->MouseButtonUp(button) == Button::State::HOVER) {
PingNetwork();
}
if (buttonMap["join"]->MouseButtonUp(button) == Button::State::HOVER) {
//join...
if (selectedServer) {
cout << "joining server: " << selectedServer->name << endl;
}
}
if (buttonMap["back"]->MouseButtonUp(button) == Button::State::HOVER) {
SetNextScene(SceneList::MAINMENU);
}
//select a server
if (button.x >= 250 && button.y >= 50 && button.y < serverVector.size() * 16 + 50) {
selectedServer = &serverVector[(button.y-50)/16];
}
else {
selectedServer = nullptr;
}
}
void Lobby::KeyDown(SDL_KeyboardEvent const& key) {
switch(key.keysym.sym) {
case SDLK_ESCAPE:
QuitEvent();
SetNextScene(SceneList::MAINMENU);
break;
}
}
@@ -113,7 +142,7 @@ void Lobby::PingNetwork() {
packet.type = PacketList::PING;
netUtil->Send("255.255.255.255", configUtil->Integer("server.port"), reinterpret_cast<void*>(&packet), sizeof(Packet));
//reset the server list
serverVector.clear();
// serverVector.clear();
}
void Lobby::PushServer(Packet* packet) {
+4 -1
View File
@@ -12,6 +12,7 @@
#include "button.hpp"
#include <vector>
#include <map>
#include <string>
struct Server {
@@ -43,6 +44,7 @@ protected:
//utilities
void PingNetwork();
void PushServer(Packet*);
typedef std::map<std::string, Button*> ButtonMap;
//members
ConfigUtility* configUtil = nullptr;
@@ -50,9 +52,10 @@ protected:
UDPNetworkUtility* netUtil = nullptr;
RasterFont font;
Button pingButton;
ButtonMap buttonMap;
std::vector<Server> serverVector;
Server* selectedServer = nullptr;
};
#endif
+3 -8
View File
@@ -15,9 +15,9 @@ MainMenu::MainMenu(ConfigUtility* cUtil, SurfaceManager* sMgr) {
configUtil = cUtil;
surfaceMgr = sMgr;
buttonMap["start"] = new Button(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "start");
buttonMap["options"] = new Button(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "options");
buttonMap["quit"] = new Button(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "quit");
buttonMap["start"] = new Button(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Start");
buttonMap["options"] = new Button(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Options");
buttonMap["quit"] = new Button(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Quit");
}
MainMenu::~MainMenu() {
@@ -69,18 +69,13 @@ void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (buttonMap["start"]->MouseButtonUp(button) == Button::State::HOVER) {
//TODO
SetNextScene(SceneList::LOBBY);
cout << "start" << endl;
}
if (buttonMap["options"]->MouseButtonUp(button) == Button::State::HOVER) {
//TODO
cout << "options" << endl;
}
if (buttonMap["quit"]->MouseButtonUp(button) == Button::State::HOVER) {
//TODO
QuitEvent();
cout << "quit" << endl;
}
}