Skip to content

Commit

Permalink
[ui] Integrating NewRoomDialog #23 into ServerView #11
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Apr 3, 2020
1 parent dbeb3a4 commit bf0925b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 77 deletions.
6 changes: 3 additions & 3 deletions src/common/commonroom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
* Status of a room
*/
enum class RoomStatus {
WAITING, //! still accepting players
FULL, //! all seats full, entrance not permitted
PLAYING //! game being played
WAITING, //! still accepting players
FULL, //! all seats full, entrance not permitted
PLAYING //! game being played
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/ui/newroomdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ NewRoomDialog::NewRoomDialog() : Window("Create room") {

setWidth(200);
setHeight(300);
setMovable(false);
addWidgets();
setVisible(false); // hidden at startup
}
Expand Down
111 changes: 71 additions & 40 deletions src/views/serverview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,84 +19,115 @@
ServerView::ServerView(ContextName previous) : Context(ContextName::ROOM_LIST),
origin(previous) {

btn_back.setActionEventId("logout");
btn_back.addActionListener(this);
btn_back.setActionEventId("logout");
btn_back.addActionListener(this);

btn_newRoom.setActionEventId("new");
btn_newRoom.addActionListener(this);
btn_newRoom.setActionEventId("new");
btn_newRoom.addActionListener(this);

btn_goToShop.setActionEventId("shop");
btn_goToShop.addActionListener(this);
btn_goToShop.setActionEventId("shop");
btn_goToShop.addActionListener(this);

btn_addFriend.setActionEventId("friend");
btn_addFriend.addActionListener(this);
btn_addFriend.setActionEventId("friend");
btn_addFriend.addActionListener(this);

btn_prevPage.setActionEventId("prev");
btn_prevPage.addActionListener(this);
btn_prevPage.setActionEventId("prev");
btn_prevPage.addActionListener(this);

btn_nextPage.setActionEventId("next");
btn_nextPage.addActionListener(this);
btn_nextPage.setActionEventId("next");
btn_nextPage.addActionListener(this);

sa_chat.setContent(&tb_chat);
tb_chat.setEditable(false); // read-only
sa_chat.setContent(&tb_chat);
tb_chat.setEditable(false); // read-only

tf_message.setActionEventId("send_msg");
tf_message.addActionListener(this);
tf_message.setActionEventId("send_msg");
tf_message.addActionListener(this);

addWidgets();
nrd.setActionEventId("new_request");
nrd.addActionListener(this);

addWidgets();
}

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

void ServerView::action(const gcn::ActionEvent& actionEvent) {
if (actionEvent.getId() == "logout") {
network.logout();
setNextContext(origin);
}
if (actionEvent.getId() == "logout") {
network.logout();
setNextContext(origin);
} else if (actionEvent.getId() == "new") {
nrd.setVisible(true);
} else if (actionEvent.getId() == "friend") {
in_addFriend.setVisible(true);
}
}

void ServerView::addWidgets() {



int xStep = getWidth() / 5;
addWidget(&btn_back, xStep - btn_back.getWidth(), getHeight() - 2 * btn_back.getHeight());
addWidget(&btn_newRoom, 2 *xStep - btn_newRoom.getWidth(), btn_back.getY());
addWidget(&btn_goToShop, 3 * xStep - btn_goToShop.getWidth(), btn_back.getY());
addWidget(&btn_addFriend, 4 * xStep - btn_addFriend.getWidth(), btn_back.getY());
// Add player data to display
addWidget(&lbl_playerName, getWidth() / 3, 2);
addWidget(&lbl_playerTeam, lbl_playerName.getX() + 50, 2);
addWidget(&lbl_playerPoints, lbl_playerTeam.getX() + 50, 2);
addWidget(&lbl_playerMoney, lbl_playerPoints.getX() + 50, 2);

int xStep = getWidth() / 5;
addWidget(&btn_back, xStep - btn_back.getWidth(), getHeight() - 2 * btn_back.getHeight());
addWidget(&btn_newRoom, 2 *xStep - btn_newRoom.getWidth(), btn_back.getY());
addWidget(&btn_goToShop, 3 * xStep - btn_goToShop.getWidth(), btn_back.getY());
addWidget(&btn_addFriend, 4 * xStep - btn_addFriend.getWidth(), btn_back.getY());

// Channel chat area
addWidget(&tf_message, btn_back.getX(), btn_back.getY() - btn_back.getHeight() - tf_message.getHeight());
addWidget(&sa_chat, btn_back.getX(), tf_message.getY() - 10 - sa_chat.getHeight());

// Dialogs
addCenteredWidget(&in_addFriend);
addCenteredWidget(&nrd);
}

/**
* \brief Append a new message to the channel view
*/
void ServerView::receiveChatMessage(const std::string& user, const std::string& message) {
tb_chat.addRow(user + "] " + message);
tb_chat.addRow(user + "] " + message);
}

/**
* Function to be called when a player status change is received from server
* \param player info (type to be created)
*/
void ServerView::playerStateChanged() {
// TBD
// TBD
}

/**
* Function to be called when room info changes
* \param new info for ONE room (type to be created)
*/
void ServerView::roomChanged() {
// TBD
// TBD
}

/**
* \brief Update data for the current player
* \param info basic data as received from the server
*/
void ServerView::setPlayerData(const PlayerBasicInfo info) {
lbl_playerName.setCaption(info.name);
lbl_playerName.adjustSize();
lbl_playerMoney.setCaption(std::to_string(info.gold) + " G");
lbl_playerMoney.adjustSize();
lbl_playerPoints.setCaption(std::to_string(info.points) + " XP");
lbl_playerPoints.adjustSize();
}

void ServerView::drawBackground(SDL_Renderer* screen) {
// TBD
// TBD
}
72 changes: 38 additions & 34 deletions src/views/serverview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,63 @@
#include <map>
#include <guisan.hpp>
#include "../context.hpp"
#include "../common/commonplayer.hpp"
#include "../ui/roombutton.hpp"
#include "../ui/newroomdialog.hpp"

/*
* UI class for server view
*/
class ServerView final : public Context, public gcn::ActionListener {
public:
explicit ServerView(ContextName previous);
~ServerView();
explicit ServerView(ContextName previous);
~ServerView();

void drawBackground(SDL_Renderer* screen) override;
void drawOverlay(SDL_Renderer* screen) override {
// No overlays in ServerView
};
void processEvent(SDL_Event& event) override {
// No specific handling to be done here, GUI handles everything
};
// Inherited via ActionListener
void action(const gcn::ActionEvent& actionEvent) override;
void drawBackground(SDL_Renderer* screen) override;
void drawOverlay(SDL_Renderer* screen) override {
// No overlays in ServerView
};
void processEvent(SDL_Event& event) override {
// No specific handling to be done here, GUI handles everything
};
// Inherited via ActionListener
void action(const gcn::ActionEvent& actionEvent) override;

private:
gcn::Button btn_back{ "Logout" };
gcn::Button btn_newRoom{ "Create room" };
gcn::Button btn_goToShop{ "Item shop" };
gcn::Button btn_addFriend{ "Add friend" };
gcn::Button btn_back{ "Logout" };
gcn::Button btn_newRoom{ "Create room" };
gcn::Button btn_goToShop{ "Item shop" };
gcn::Button btn_addFriend{ "Add friend" };

gcn::Button btn_prevPage{ "<" };
gcn::Button btn_nextPage{ ">" };
gcn::Button btn_prevPage{ "<" };
gcn::Button btn_nextPage{ ">" };

gcn::Label lbl_playerName;
gcn::Label lbl_playerTeam;
gcn::Label lbl_playerPoints;
gcn::Label lbl_playerMoney;
gcn::Label lbl_playerName;
gcn::Label lbl_playerTeam;
gcn::Label lbl_playerPoints;
gcn::Label lbl_playerMoney;

gcn::Label lbl_pages;
gcn::Label lbl_pages;

gcn::TextBox tb_chat;
gcn::ScrollArea sa_chat;
gcn::TextField tf_message;
gcn::TextBox tb_chat;
gcn::ScrollArea sa_chat;
gcn::TextField tf_message;

gcn::InputBox in_addFriend{ "Add friend", "Enter player id" };
gcn::InputBox in_addFriend{ "Add friend", "Enter player id" };
NewRoomDialog nrd;

ContextName origin;
ContextName origin;

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

void addWidgets();
void addWidgets();

void receiveChatMessage(const std::string& user, const std::string& message);
void playerStateChanged();
void roomChanged();
void receiveChatMessage(const std::string& user, const std::string& message);
void playerStateChanged();
void roomChanged();
void setPlayerData(const PlayerBasicInfo info);
};

#endif // !_H_SERVERVIEW_

0 comments on commit bf0925b

Please sign in to comment.