Skip to content

Commit

Permalink
Initiate the ServerList #10
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Feb 8, 2019
1 parent cf409fa commit bc0f201
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Context {

virtual void enter();
virtual void leave();
virtual void processMessage(const Uint16 code, const std::string &message) {
virtual void processMessage(const Uint8 code, const std::string &message) {
// Left empty because some derived classes will not need this
};
virtual void processEvent(SDL_Event &event) = 0;
Expand Down
75 changes: 75 additions & 0 deletions src/views/serverlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is “Incompatible With Secondary Licenses”,
* as defined by the Mozilla Public License, v. 2.0.
*/

#include "../protocol.hpp"
#include "../utils.hpp"
#include "serverlist.hpp"
using namespace std;

ServerList::ServerList(ContextName name, gcn::Container * p) : Context(name, p),
btn_back("< Back"), btn_manualIP("Enter IP"), btn_rescan("Rescan local network")
{

if (name == ContextName::SERVER_LIST_WEB) {
btn_rescan.setVisible(false);
}

addWidgets();
}

ServerList::~ServerList() {
// Cleanup our surfaces
if (background) {
SDL_FreeSurface(background);
}
if (backTexture) {
SDL_DestroyTexture(backTexture);
}
}

void ServerList::action(const gcn::ActionEvent & actionEvent) {
if (actionEvent.getId() == "back") {
setNextContext(ContextName::MAIN_MENU);
}
else if (actionEvent.getId() == "ip") {
// Open the InputBox
}
else if (actionEvent.getId() == "rescan") {
scanNetwork();
}
}

void ServerList::drawBackground(SDL_Renderer * screen) {
if (!backTexture) {
backTexture = SDL_CreateTextureFromSurface(screen, background);
}
SDL_RenderCopy(screen, backTexture, NULL, NULL);
}

void ServerList::processMessage(const Uint8 code, const std::string & message) {
switch (code) {
case SERVER_INFO:
// Analyze message and create button
break;
default:
break;
}
}

void ServerList::addWidgets() {
addWidget(&btn_back, 40, background->h - 40);
addWidget(&btn_manualIP, 700, background->h / 2);
addWidget(&btn_rescan, 700, btn_manualIP.getY() + 40);
}

/**
* Send a broadcast message to find other servers
*/
void ServerList::scanNetwork() {
// TBD
}
47 changes: 47 additions & 0 deletions src/views/serverlist.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is “Incompatible With Secondary Licenses”,
* as defined by the Mozilla Public License, v. 2.0.
*/

#ifndef _H_SERVERLIST_
#define _H_SERVERLIST_

#include <string>
#include <vector>
#include <guisan.hpp>
#include <SDL2/SDL.h>
#include "../context.hpp"

class ServerList : public Context, public gcn::ActionListener {
public:
ServerList(ContextName name, gcn::Container *p);
virtual ~ServerList();
void action(const gcn::ActionEvent &actionEvent) override;

virtual void drawBackground(SDL_Renderer *screen) override;
virtual void processMessage(const Uint8 code, const std::string &message) override;
virtual void drawOverlay(SDL_Renderer *screen) override {
// There will never be any overlays in ServerList
};

virtual void processEvent(SDL_Event &event) override {
// No specific handling to be done here, GUI handles everything
};

private:
gcn::Button btn_back;
gcn::Button btn_manualIP;
gcn::Button btn_rescan;
// gcn::InputBox input_ip;

SDL_Texture *backTexture = nullptr;
SDL_Surface *background = nullptr;

void addWidgets();
void scanNetwork();
};

#endif // !_H_SERVERLIST_

0 comments on commit bc0f201

Please sign in to comment.