Skip to content

Commit d0beb1d

Browse files
committed
Added buttons with callbacks for trade accept/cancel
1 parent 9da4177 commit d0beb1d

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

include/GameController.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "Util.h"
55

6+
#include <array>
7+
68
class GameBoard;
79
class ClickCoordinateEvent;
810
class GameView;
@@ -31,6 +33,7 @@ class GameController {
3133
bool handleRoadButtonEvent(ScreenCoordinate);
3234
bool handleSettlementButtonEvent(ScreenCoordinate);
3335
bool handlePlayerClick(ScreenCoordinate, Player&);
36+
bool handleTradeOffer(ScreenCoordinate, Player& initiating, std::array<int, 5>, Player& receiving);
3437
};
3538

3639
#endif

include/GameView.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class GameView {
6161
void addElement(std::unique_ptr<ViewElement>);
6262
void addElement(int priority, std::unique_ptr<ViewElement>);
6363

64+
std::unique_ptr<ViewElement> removeElement(int priority);
6465
std::unique_ptr<ViewElement> removeElement(const ViewElement*);
6566
std::unique_ptr<ViewElement> removeElement(const ViewElement&);
6667
};
@@ -185,12 +186,15 @@ class TradingView : public ViewElement {
185186

186187
std::array<int, 5> offer;
187188

189+
ViewButtonText trade;
190+
ViewButtonText cancel;
191+
188192
TradingView(TradingView& o) = delete;
189193
TradingView& operator=(TradingView& o) = delete;
190194
protected:
191195
virtual bool clicked(ScreenCoordinate coord);
192196
public:
193-
TradingView(Player& initiating, Player& receiving);
197+
TradingView(Player& initiating, Player& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel);
194198
virtual ~TradingView();
195199

196200
void render();

src/GameController.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,35 @@ bool GameController::handleSettlementButtonEvent(ScreenCoordinate coord) {
8989
* @param player The player whose name was clicked on.
9090
*/
9191
bool GameController::handlePlayerClick(ScreenCoordinate coord, Player& player) {
92-
view.addElement(-10, std::unique_ptr<ViewElement>(new TradingView(*model.getPlayers()[0], player)));
92+
using namespace std::placeholders;
93+
Player& initiating = *model.getPlayers()[0];
94+
Player& receiving = player;
95+
auto priority = -10;
96+
97+
std::function<bool(std::array<int, 5>, ScreenCoordinate)> tradeFunction(std::bind(&GameController::handleTradeOffer, this, _2, std::ref(initiating), _1, std::ref(receiving)));
98+
std::function<bool(ScreenCoordinate)> cancelFunction([this, priority](ScreenCoordinate coord) {
99+
view.removeElement(priority);
100+
return true;
101+
});
102+
103+
view.addElement(priority, std::unique_ptr<ViewElement>(new TradingView(initiating, receiving, tradeFunction, cancelFunction)));
93104
std::cout << player.getName() << std::endl;
94105
return true;
95106
}
96107

108+
/**
109+
* Handle a trade offer from a player.
110+
* @param coord The coordinate clicked on to initiate the trade.
111+
* @param initiating The player initiating the trade.
112+
* @param offer The offer the player is giving.
113+
* @param receiving The other player in the trade.
114+
*/
115+
bool GameController::handleTradeOffer(ScreenCoordinate coord, Player& initiating, std::array<int, 5> offer, Player& receiving) {
116+
std::cout << "Received trade offer of ";
117+
for(auto& it : offer) {
118+
std::cout << it << " ";
119+
}
120+
std::cout << std::endl;
121+
return true;
122+
}
123+

src/GameView.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,22 @@ unique_ptr<ViewElement> GameView::removeElement(const ViewElement& element) {
161161
return removeElement(&element);
162162
}
163163

164+
/**
165+
* Remove a ViewElement from the list, and return its owning pointer.
166+
* @param priority The priority of the element to remove.
167+
* @return An owning pointer to the element that was removed.
168+
*/
169+
unique_ptr<ViewElement> GameView::removeElement(int priority) {
170+
auto it = viewElements.find(priority);
171+
if(it == viewElements.end()) {
172+
return std::unique_ptr<ViewElement>();
173+
} else {
174+
auto ret = std::move(it->second);
175+
viewElements.erase(it);
176+
return ret;
177+
}
178+
}
179+
164180
/**
165181
* Construct a ViewButton.
166182
* @param action The action to take when the button is pressed.
@@ -478,7 +494,10 @@ void DrawingGameVisitor::visit(DevelopmentCard& card) {
478494

479495
}
480496

481-
TradingView::TradingView(Player& initiating, Player& receiving) : ViewElement({{0.1, 0.1},{0.9, 0.9}}), initiating(initiating), receiving(receiving) {
497+
TradingView::TradingView(Player& initiating, Player& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel) : ViewElement({{0.1, 0.1},{0.9, 0.9}}), initiating(initiating), receiving(receiving),
498+
trade(std::bind(trade, std::ref(offer), std::placeholders::_1), {{0.7, 0.1}, {0.9, 0.2}}, "resources/TypeWritersSubstitute-Black.ttf", 50, "Trade"),
499+
cancel(cancel, {{0.1, 0.1}, {0.3, 0.2}}, "resources/TypeWritersSubstitute-Black.ttf", 50, "Cancel") {
500+
482501
for(auto& res : offer) {
483502
res = 0;
484503
}
@@ -489,8 +508,13 @@ TradingView::~TradingView() {
489508
}
490509

491510
bool TradingView::clicked(ScreenCoordinate coord) {
511+
if(cancel.handleClick(coord)) {
512+
return true;
513+
} else if(trade.handleClick(coord)) {
514+
return true;
515+
}
492516
int modifier = coord.first <= 0.5 ? -1 : 1;
493-
int resource = (coord.second - 0.1) / 0.13;
517+
int resource = (coord.second - 0.2) / 0.13;
494518
if(resource >= 0 && resource <= 5) {
495519
offer[resource] += modifier;
496520
}
@@ -518,4 +542,7 @@ void TradingView::render() {
518542
renderText(font, fontSize, {0.3, 0.2 + (i * height)}, {0.6, 0.2 + height + (i * height)}, toString(offer[i]) + " " + resources[i]);
519543
}
520544
renderText(font, fontSize, {0.1, 0.8}, {0.9, 0.9}, initiating.getName() + " -> " + receiving.getName());
545+
546+
cancel.render();
547+
trade.render();
521548
}

0 commit comments

Comments
 (0)