Skip to content

Commit

Permalink
Introducing ServerListElement for use in #10
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Feb 15, 2019
1 parent e6b73ed commit 5c7a5be
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 8 deletions.
69 changes: 69 additions & 0 deletions src/ui/serverlistelement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* 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 "../utils.hpp"
#include "serverlistelement.hpp"
using namespace std;

ServerListElement::ServerListElement(ServerInfo info) :
_info(info), btn_connect("Connect") {
setWidth(500);
setHeight(60);

lbl_name.setCaption(info.name);
lbl_name.adjustSize();

lbl_ip.setCaption(linbound::prettifyIP(info.ip));
lbl_ip.adjustSize();

// Levels are displayed as text for the moment...
lbl_lvlMin.setCaption("Level min: " + to_string(info.levelMin));
lbl_lvlMin.adjustSize();

lbl_lvlMax.setCaption("Level max: " + to_string(info.levelMax));
lbl_lvlMax.adjustSize();

string business = "";
switch (info.busy) {
case 0: business = "Low"; break;
case 1: business = "Medium"; break;
case 2: business = "High"; break;
case 255: business = "Full"; break;
default: break;
}

lbl_busy.setCaption(business);
lbl_busy.adjustSize();

btn_connect.setWidth(100);
btn_connect.setHeight(getHeight() - 10);
btn_connect.addActionListener(this);

addWidgets();
}

ServerInfo ServerListElement::getInfo() const {
return _info;
}

void ServerListElement::action(const gcn::ActionEvent & event) {
if (event.getSource() == &btn_connect) {
generateAction();
}
}

void ServerListElement::addWidgets() {
int padding = 5;
add(&lbl_name, padding, padding);
add(&lbl_ip, lbl_name.getWidth() + 2 * padding, padding);
add(&btn_connect, getWidth() - padding - btn_connect.getWidth(), padding);

add(&lbl_lvlMin, padding, getHeight() - padding - lbl_lvlMin.getHeight());
add(&lbl_lvlMax, 100, getHeight() - padding - lbl_lvlMin.getHeight());
add(&lbl_busy, btn_connect.getX() - padding - lbl_busy.getWidth(), getHeight() - padding - lbl_lvlMin.getHeight());
}
43 changes: 43 additions & 0 deletions src/ui/serverlistelement.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* 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 <SDL2/SDL.h>
#include <guisan.hpp>

#ifndef _H_SERVERLISTELEMENT_
#define _H_SERVERLISTELEMENT_

struct ServerInfo {
Uint32 ip;
Uint16 port;
std::string name;
Uint8 levelMin;
Uint8 levelMax;
Uint8 busy; // level of attendance
};

class ServerListElement : public gcn::Container, public gcn::ActionListener {
public:
ServerListElement(ServerInfo info);
ServerInfo getInfo() const;
virtual void action(const gcn::ActionEvent &event) override;

private:
ServerInfo _info;
gcn::Button btn_connect;
gcn::Label lbl_ip;
gcn::Label lbl_name;
gcn::Label lbl_lvlMin;
gcn::Label lbl_lvlMax;
gcn::Label lbl_busy;

void addWidgets();
};

#endif // !_H_SERVERLISTELEMENT_

9 changes: 1 addition & 8 deletions src/views/serverlist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
#include <SDL2/SDL.h>
#include "../context.hpp"
#include "../ui/loginwindow.hpp"

struct ServerInfo {
Uint32 ip;
std::string name;
Uint8 levelMin;
Uint8 levelMax;
Uint8 busy; // level of attendance
};
#include "../ui/serverlistelement.hpp"

/**
* View representing the list of servers, used in LAN and web game modes
Expand Down

0 comments on commit 5c7a5be

Please sign in to comment.