Skip to content

Commit

Permalink
[ui] Integrating LobbyView #18 into RoomView #19 and Context
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Apr 8, 2020
1 parent bef61bd commit 04b1fe8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 23 deletions.
53 changes: 43 additions & 10 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ NetworkManager Context::network;
* \param type name of the context being created
*/
Context::Context(ContextName type) :
name(type) {
name(type) {
top.setWidth(parent->getWidth());
top.setHeight(parent->getHeight());
top.setOpaque(false);
Expand All @@ -48,7 +48,7 @@ Context::~Context() {
* \return name of the current context
*/
ContextName const Context::getName() {
return name;
return name;
}

/**
Expand Down Expand Up @@ -90,17 +90,17 @@ void Context::leave() {
* It is up to the Context to do the processing
*/
void Context::receive() {
// Get messages from network manager
vector<UDPpacket *> data = network.receive();
// Unpack and feed context, one by one
for(const UDPpacket *p : data) {
processMessage(NetworkManager::getCode(p), NetworkManager::getMessage(p));
}
// Get messages from network manager
vector<UDPpacket *> data = network.receive();
// Unpack and feed context, one by one
for(const UDPpacket *p : data) {
processMessage(NetworkManager::getCode(p), NetworkManager::getMessage(p));
}
}

void Context::send(const Uint8 code, const std::string & message) {
network.send(code, message);
network.send(code, message);
}

void Context::setServerIP(const Uint32 ip) {
Expand Down Expand Up @@ -160,6 +160,39 @@ Context * Context::getNextContext(ContextName nextName) {
currentContext = new ServerView(currentName);
currentContext->enter();
break;
case ContextName::ROOM_LOBBY: // fallthrough
case ContextName::ROOM_LOBBY_LOCAL:
if (currentContext) {
currentName = currentContext->getName();
currentContext->leave();
if (currentName == ContextName::ROOM) {
// Restore the previous lobby
Context* previousContext = currentContext;
currentContext = dynamic_cast<RoomView*>(currentContext)->getLobby();
delete previousContext;
} else {
// Create a new one from scratch
delete currentContext;
currentContext = new LobbyView(nextName);
}
currentContext->enter();
} else {
SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Trying to enter room lobby from nowhere");
}
break;
case ContextName::ROOM:
if (currentContext) {
currentContext->leave();
LobbyView* lobby = dynamic_cast<LobbyView*>(currentContext);
if (lobby) {
// Normally this should be the only path from which we can create a RoomView...
currentContext = new RoomView(lobby);
}
currentContext->enter();
} else {
SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Trying to enter room from nowhere");
}
break;
default:
break;
}
Expand Down
25 changes: 21 additions & 4 deletions src/views/lobbyview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "lobbyview.hpp"
using namespace gcn;

LobbyView::LobbyView(): Context(ContextName::ROOM_LOBBY) {
LobbyView::LobbyView(ContextName name): Context(name) {

btn_back.setActionEventId("back");
btn_back.addActionListener(this);
Expand All @@ -32,6 +32,13 @@ LobbyView::LobbyView(): Context(ContextName::ROOM_LOBBY) {
tf_message.setActionEventId("message");
tf_message.addActionListener(this);

Color darkblue{ 0, 0, 0xff };
Color white{ 0xff, 0xff, 0xff };
tf_message.setForegroundColor(white);
tf_message.setBackgroundColor(darkblue);
tb_messages.setForegroundColor(white);
tb_messages.setBackgroundColor(darkblue);

pg_loading.setVisible(false);
}

Expand All @@ -42,6 +49,7 @@ void LobbyView::action(const gcn::ActionEvent& actionEvent) {
// TODO open specific edition box
} else if (actionEvent.getId() == "message") {
std::string msg = tf_message.getText();
tf_message.clear();
// Handle special processing of commands
std::regex pattern{ "^(/kick|/mute)\\s+" };
if (std::regex_match(msg, pattern)) {
Expand All @@ -52,8 +60,9 @@ void LobbyView::action(const gcn::ActionEvent& actionEvent) {
}
}

void LobbyView::updateRoomInfo() {
// TBD
void LobbyView::updateRoomInfo(const RoomBasicInfo &info) {
// TBD: refresh the view accordingly
roomBasicInfo = info;
}

void LobbyView::updatePlayerInfo() {
Expand All @@ -64,13 +73,21 @@ void LobbyView::updateItemInfo() {
// TBD
}

void LobbyView::addMessage(std::string user, std::string message, bool showBalloon) {
void LobbyView::addMessage(const std::string &user, const std::string &message, bool showBalloon) {
tb_messages.addRow(user + "] " + message);
if (showBalloon) {
// Reset the balloon for this user
}
}

GameMode LobbyView::getMode() const {
return roomBasicInfo.mode;
}

std::string LobbyView::getMap() const {
return roomBasicInfo.mapName;
}

void LobbyView::addWidgets() {
addWidget(&lbl_number, 5, 5);
addWidget(&lbl_name, 20, 5);
Expand Down
12 changes: 9 additions & 3 deletions src/views/lobbyview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@

#include <string>
#include <guisan.hpp>
#include "../constants.hpp"
#include "../gamemap.hpp"
#include "../context.hpp"
#include "../common/commonroom.hpp"
#include "../common/messages.hpp"

class LobbyView : public Context, public gcn::ActionListener {
public:
LobbyView();
explicit LobbyView(ContextName name);
void action(const gcn::ActionEvent& actionEvent) override;

void updateRoomInfo();
void updateRoomInfo(const RoomBasicInfo &info);
void updatePlayerInfo();
void updateItemInfo();
void addMessage(std::string user, std::string message, bool showBalloon = true);
void addMessage(const std::string &user, const std::string &message, bool showBalloon = true);

GameMode getMode() const;
std::string getMap() const;

virtual void drawBackground(SDL_Renderer* screen) override;
virtual void processMessage(const Uint8 code, const std::string& message) override;
Expand All @@ -54,6 +59,7 @@ class LobbyView : public Context, public gcn::ActionListener {
gcn::TextBox tb_messages;

bool isCurrentPlayerAdmin = false;
RoomBasicInfo roomBasicInfo;

void addWidgets();
};
Expand Down
23 changes: 18 additions & 5 deletions src/views/roomview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ const Uint16 RoomView::MAX_POWER;

/**
* Constructor
* @param mode game mode to play among the four implemented
* @param map an already partly loaded object representing the game map
* @param view the LobbyView, which already has players and all parameters set
*/
RoomView::RoomView(const GameMode mode, GameMap *map) : Context(ContextName::ROOM),
gameMode(mode), currentMap(map) {
RoomView::RoomView(LobbyView *view) : Context(ContextName::ROOM),
gameMode(view->getMode()) {

currentMap = new GameMap(view->getMap());

fg_rect.w = getWidth();
fg_rect.h = getHeight();
Expand Down Expand Up @@ -173,8 +174,11 @@ void RoomView::addMessage(const std::string& sender, const std::string& message,
msgLog.addMessage(sender, message, type);
if (!sender.empty()) {
// TODO show in chatballoon

// and add to the chat channel of the room lobby
origin->addMessage(sender, message, false);
}
// TODO add to the chat channel of the room lobby

}

/**
Expand Down Expand Up @@ -234,6 +238,15 @@ void RoomView::setTurn() {
// TODO play a sound once to wake up player
}

/**
* \brief Getter for original lobby
* Will be used to restore the view instead of recreating it from scratch
* \return the saved LobbyView
*/
LobbyView* RoomView::getLobby() {
return origin;
}

/**
* Move the viewport from a given offset
* @param xDelta x offset
Expand Down
7 changes: 6 additions & 1 deletion src/views/roomview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "../ui/resultsdialog.hpp"
#include "../ui/scoreboard.hpp"
#include "../common/commonplayer.hpp" // temporary, use clientplayer
#include "lobbyview.hpp"

/**
* \brief An extended version of a Label for current gains
Expand Down Expand Up @@ -54,7 +55,7 @@ class RoomView: public Context, public gcn::ActionListener {
RESULTS //! match is over
};

explicit RoomView(const GameMode mode, GameMap* map);
explicit RoomView(LobbyView *view);
virtual ~RoomView();

void action(const gcn::ActionEvent &actionEvent) override;
Expand All @@ -68,6 +69,8 @@ class RoomView: public Context, public gcn::ActionListener {

void setTurn();

LobbyView* getLobby();

private:
// Viewport definitions
SDL_Rect fg_rect;
Expand Down Expand Up @@ -95,6 +98,8 @@ class RoomView: public Context, public gcn::ActionListener {
GameMode gameMode;
GameMap *currentMap;

LobbyView* origin = nullptr;

Uint8 windPower = 0;
Uint16 windAngle = 0;

Expand Down

0 comments on commit 04b1fe8

Please sign in to comment.