33
44#include < vector>
55#include < memory>
6+ #include < array>
67
78#include " SDL2/SDL.h"
89#include " SDL2/SDL_opengl.h"
910#include " GL/gl.h"
1011
1112#include " GameVisitor.h"
13+ #include " Renderer.h"
1214#include " Util.h"
1315
1416class GameBoard ;
@@ -45,26 +47,28 @@ class GameView {
4547private:
4648 GameBoard& model;
4749
48- std::vector< std::unique_ptr<ViewElement>> viewElements;
50+ std::map< int , std::unique_ptr<ViewElement>> viewElements;
4951 std::vector<ScreenCoordinate> pointsOfInterest;
5052
5153 void highlightPoint (ScreenCoordinate & coord);
5254
53- GameView (const GameView& o) : model(o.model) {} // deleted
54- GameView& operator =(const GameView& o) { return * this ; } // deleted
55+ GameView (const GameView& o) = delete ;
56+ GameView& operator =(const GameView& o) = delete ;
5557public:
5658 GameView (GameBoard&);
5759 ~GameView ();
5860
5961 void render ();
6062 bool acceptInput (SDL_Event& event);
6163
62- void addElement (std::unique_ptr<ViewElement>);
63- bool removeElement (ViewElement*);
64- bool removeLastElement ();
6564
6665 void addPointOfInterest (ScreenCoordinate);
6766 void clearPointsOfInterest ();
67+ void addElement (int priority, std::unique_ptr<ViewElement>);
68+
69+ std::unique_ptr<ViewElement> removeElement (int priority);
70+ std::unique_ptr<ViewElement> removeElement (const ViewElement*);
71+ std::unique_ptr<ViewElement> removeElement (const ViewElement&);
6872};
6973
7074/* *
@@ -74,8 +78,8 @@ class DrawingGameVisitor : public GameVisitor {
7478private:
7579 GameView& view;
7680
77- DrawingGameVisitor (const DrawingGameVisitor& o) : view(o.view) {} // deleted
78- DrawingGameVisitor& operator =(const DrawingGameVisitor& o) { return * this ; } // deleted
81+ DrawingGameVisitor (const DrawingGameVisitor& o) = delete ;
82+ DrawingGameVisitor& operator =(const DrawingGameVisitor& o) = delete ;
7983public:
8084 DrawingGameVisitor (GameView& view);
8185 ~DrawingGameVisitor ();
@@ -87,28 +91,27 @@ class DrawingGameVisitor : public GameVisitor {
8791 virtual void visit (Player&);
8892 virtual void visit (ResourceTile&);
8993 virtual void visit (DevelopmentCard&);
94+ virtual void visit (GameDice&);
95+ virtual void visit (Wonder&);
9096};
9197
9298
9399/* *
94100 * A view element that is invisible and calls a callback function when it is clicked.
95101 */
96- template <class Fn >
97102class ViewButton : public ViewElement {
98103private:
99- Fn action;
104+ std::function< bool (ScreenCoordinate)> action;
100105
101- ViewButton (const ViewButton& vb) : ViewElement(vb) {} // deleted
102- ViewButton& operator =(const ViewButton&) { return * this ; } // deleted
106+ ViewButton (const ViewButton& vb) = delete ;
107+ ViewButton& operator =(const ViewButton&) = delete ;
103108protected:
104- virtual bool clicked (ScreenCoordinate coord) {
105- return action (coord);
106- }
109+ virtual bool clicked (ScreenCoordinate coord);
107110public:
108- ViewButton (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect) : ViewElement(rect), action(action) {}
109- virtual ~ViewButton () {}
111+ ViewButton (std::function< bool (ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect);
112+ virtual ~ViewButton ();
110113
111- virtual void render () {}
114+ virtual void render ();
112115};
113116
114117/* *
@@ -120,35 +123,23 @@ class ViewButton : public ViewElement {
120123 */
121124template <class Fn >
122125std::unique_ptr<ViewElement> makeViewButton (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect) {
123- return std::unique_ptr<ViewElement>(new ViewButton<Fn> (fn, rect));
126+ return std::unique_ptr<ViewElement>(new ViewButton (fn, rect));
124127}
125128
126129/* *
127130 * A view element drawn as a solid color that has a callback function that is called when it is clicked.
128131 */
129- template <class Fn >
130- class ViewButtonColor : public ViewButton <Fn> {
132+ class ViewButtonColor : public ViewButton {
131133private:
132134 std::tuple<float , float , float > color;
133135
134- ViewButtonColor (const ViewButtonColor& vb) : ViewElement(vb) {} // deleted
135- ViewButtonColor& operator =(const ViewButtonColor& vb) { return * this ; }
136+ ViewButtonColor (const ViewButtonColor& vb) = delete ;
137+ ViewButtonColor& operator =(const ViewButtonColor& vb) = delete ;
136138public:
137- ViewButtonColor (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) : ViewButton<Fn>(action, rect), color(color) {}
138- virtual ~ViewButtonColor () {}
139+ ViewButtonColor (std::function< bool (ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color);
140+ virtual ~ViewButtonColor ();
139141
140- virtual void render () {
141- glBindTexture (GL_TEXTURE_2D, 0 );
142- glColor3f (std::get<0 >(color), std::get<1 >(color), std::get<2 >(color));
143- auto topLeft = ViewElement::getRect ().first ;
144- auto bottomRight = ViewElement::getRect ().second ;
145- glBegin (GL_QUADS);
146- glVertex2f (topLeft.first , topLeft.second );
147- glVertex2f (bottomRight.first , topLeft.second );
148- glVertex2f (bottomRight.first , bottomRight.second );
149- glVertex2f (topLeft.first , bottomRight.second );
150- glEnd ();
151- }
142+ virtual void render ();
152143};
153144
154145template <class Fn >
@@ -249,7 +240,60 @@ std::unique_ptr<ViewElement> makeConfirmationDialogue(Fn confirm_fn, Fn cancel_f
249240 */
250241template <class Fn >
251242std::unique_ptr<ViewElement> makeViewButtonColor (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) {
252- return std::unique_ptr<ViewElement>(new ViewButtonColor<Fn> (fn, rect, color));
243+ return std::unique_ptr<ViewElement>(new ViewButtonColor (fn, rect, color));
253244}
254245
246+ /* *
247+ * A view element drawn as some text on the screen that has a callback function when it is clicked.
248+ */
249+ class ViewButtonText : public ViewButton {
250+ private:
251+ GLuint texture;
252+
253+ ViewButtonText (const ViewButtonText& vb) = delete ;
254+ ViewButtonText& operator =(const ViewButtonText& vb) = delete ;
255+ public:
256+ ViewButtonText (std::function<bool (ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text);
257+ virtual ~ViewButtonText ();
258+
259+ void setText (const std::string& font, int fontSize, const std::string& text);
260+
261+ virtual void render ();
262+ };
263+
264+ /* *
265+ * Constructs a ViewButtonText using the same parameters as the ViewButtonText. Exists because template inference exists only
266+ * for functions, not classes.
267+ * @param fn The callback function to be called with the ScreenCoordinate clicked and returning a boolean on if it was handled.
268+ * @param rect The location on screen to draw to and receive clicks from.
269+ * @param font The path to the font to use to draw the text.
270+ * @param fontSize The font size of the text.
271+ * @param text The text to render.
272+ */
273+ template <class Fn >
274+ std::unique_ptr<ViewElement> makeViewButtonText (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text) {
275+ return std::unique_ptr<ViewElement>(new ViewButtonText (fn, rect, font, fontSize, text));
276+ }
277+
278+ class TradingView : public ViewElement {
279+ private:
280+ Player& initiating;
281+ Player& receiving;
282+
283+ ViewButtonText trade;
284+ ViewButtonText cancel;
285+
286+ std::array<int , 5 > offer;
287+
288+ TradingView (TradingView& o) = delete ;
289+ TradingView& operator =(TradingView& o) = delete ;
290+ protected:
291+ virtual bool clicked (ScreenCoordinate coord);
292+ public:
293+ TradingView (Player& initiating, Player& receiving, std::function<bool (std::array<int , 5 >, ScreenCoordinate)> trade, std::function<bool (ScreenCoordinate)> cancel, std::array<int , 5 > offer);
294+ virtual ~TradingView ();
295+
296+ void render ();
297+ };
298+
255299#endif
0 commit comments