@@ -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
491510bool 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