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,10 +47,10 @@ 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
50- GameView (const GameView& o) : model(o.model) {} // deleted
51- GameView& operator =(const GameView& o) { return * this ; } // deleted
52+ GameView (const GameView& o) = delete ;
53+ GameView& operator =(const GameView& o) = delete ;
5254public:
5355 GameView (GameBoard&);
5456 ~GameView ();
@@ -57,6 +59,11 @@ class GameView {
5759 bool acceptInput (SDL_Event& event);
5860
5961 void addElement (std::unique_ptr<ViewElement>);
62+ void addElement (int priority, std::unique_ptr<ViewElement>);
63+
64+ std::unique_ptr<ViewElement> removeElement (int priority);
65+ std::unique_ptr<ViewElement> removeElement (const ViewElement*);
66+ std::unique_ptr<ViewElement> removeElement (const ViewElement&);
6067};
6168
6269/* *
@@ -66,8 +73,8 @@ class DrawingGameVisitor : public GameVisitor {
6673private:
6774 GameView& view;
6875
69- DrawingGameVisitor (const DrawingGameVisitor& o) : view(o.view) {} // deleted
70- DrawingGameVisitor& operator =(const DrawingGameVisitor& o) { return * this ; } // deleted
76+ DrawingGameVisitor (const DrawingGameVisitor& o) = delete ;
77+ DrawingGameVisitor& operator =(const DrawingGameVisitor& o) = delete ;
7178public:
7279 DrawingGameVisitor (GameView& view);
7380 ~DrawingGameVisitor ();
@@ -79,27 +86,26 @@ class DrawingGameVisitor : public GameVisitor {
7986 virtual void visit (Player&);
8087 virtual void visit (ResourceTile&);
8188 virtual void visit (DevelopmentCard&);
89+ virtual void visit (GameDice&);
90+ virtual void visit (Wonder&);
8291};
8392
8493/* *
8594 * A view element that is invisible and calls a callback function when it is clicked.
8695 */
87- template <class Fn >
8896class ViewButton : public ViewElement {
8997private:
90- Fn action;
98+ std::function< bool (ScreenCoordinate)> action;
9199
92- ViewButton (const ViewButton& vb) : ViewElement(vb) {} // deleted
93- ViewButton& operator =(const ViewButton&) { return * this ; } // deleted
100+ ViewButton (const ViewButton& vb) = delete ;
101+ ViewButton& operator =(const ViewButton&) = delete ;
94102protected:
95- virtual bool clicked (ScreenCoordinate coord) {
96- return action (coord);
97- }
103+ virtual bool clicked (ScreenCoordinate coord);
98104public:
99- ViewButton (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect) : ViewElement(rect), action(action) {}
100- virtual ~ViewButton () {}
105+ ViewButton (std::function< bool (ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect);
106+ virtual ~ViewButton ();
101107
102- virtual void render () {}
108+ virtual void render ();
103109};
104110
105111/* *
@@ -111,35 +117,23 @@ class ViewButton : public ViewElement {
111117 */
112118template <class Fn >
113119std::unique_ptr<ViewElement> makeViewButton (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect) {
114- return std::unique_ptr<ViewElement>(new ViewButton<Fn> (fn, rect));
120+ return std::unique_ptr<ViewElement>(new ViewButton (fn, rect));
115121}
116122
117123/* *
118124 * A view element drawn as a solid color that has a callback function that is called when it is clicked.
119125 */
120- template <class Fn >
121- class ViewButtonColor : public ViewButton <Fn> {
126+ class ViewButtonColor : public ViewButton {
122127private:
123128 std::tuple<float , float , float > color;
124129
125- ViewButtonColor (const ViewButtonColor& vb) : ViewElement(vb) {} // deleted
126- ViewButtonColor& operator =(const ViewButtonColor& vb) { return * this ; }
130+ ViewButtonColor (const ViewButtonColor& vb) = delete ;
131+ ViewButtonColor& operator =(const ViewButtonColor& vb) = delete ;
127132public:
128- ViewButtonColor (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) : ViewButton<Fn>(action, rect), color(color) {}
129- virtual ~ViewButtonColor () {}
130-
131- virtual void render () {
132- glBindTexture (GL_TEXTURE_2D, 0 );
133- glColor3f (std::get<0 >(color), std::get<1 >(color), std::get<2 >(color));
134- auto topLeft = ViewElement::getRect ().first ;
135- auto bottomRight = ViewElement::getRect ().second ;
136- glBegin (GL_QUADS);
137- glVertex2f (topLeft.first , topLeft.second );
138- glVertex2f (bottomRight.first , topLeft.second );
139- glVertex2f (bottomRight.first , bottomRight.second );
140- glVertex2f (topLeft.first , bottomRight.second );
141- glEnd ();
142- }
133+ ViewButtonColor (std::function<bool (ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color);
134+ virtual ~ViewButtonColor ();
135+
136+ virtual void render ();
143137};
144138
145139/* *
@@ -152,7 +146,60 @@ class ViewButtonColor : public ViewButton<Fn> {
152146 */
153147template <class Fn >
154148std::unique_ptr<ViewElement> makeViewButtonColor (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) {
155- return std::unique_ptr<ViewElement>(new ViewButtonColor<Fn> (fn, rect, color));
149+ return std::unique_ptr<ViewElement>(new ViewButtonColor (fn, rect, color));
156150}
157151
158- #endif
152+ /* *
153+ * A view element drawn as some text on the screen that has a callback function when it is clicked.
154+ */
155+ class ViewButtonText : public ViewButton {
156+ private:
157+ GLuint texture;
158+
159+ ViewButtonText (const ViewButtonText& vb) = delete ;
160+ ViewButtonText& operator =(const ViewButtonText& vb) = delete ;
161+ public:
162+ ViewButtonText (std::function<bool (ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text);
163+ virtual ~ViewButtonText ();
164+
165+ void setText (const std::string& font, int fontSize, const std::string& text);
166+
167+ virtual void render ();
168+ };
169+
170+ /* *
171+ * Constructs a ViewButtonText using the same parameters as the ViewButtonText. Exists because template inference exists only
172+ * for functions, not classes.
173+ * @param fn The callback function to be called with the ScreenCoordinate clicked and returning a boolean on if it was handled.
174+ * @param rect The location on screen to draw to and receive clicks from.
175+ * @param font The path to the font to use to draw the text.
176+ * @param fontSize The font size of the text.
177+ * @param text The text to render.
178+ */
179+ template <class Fn >
180+ std::unique_ptr<ViewElement> makeViewButtonText (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text) {
181+ return std::unique_ptr<ViewElement>(new ViewButtonText (fn, rect, font, fontSize, text));
182+ }
183+
184+ class TradingView : public ViewElement {
185+ private:
186+ Player& initiating;
187+ Player& receiving;
188+
189+ ViewButtonText trade;
190+ ViewButtonText cancel;
191+
192+ std::array<int , 5 > offer;
193+
194+ TradingView (TradingView& o) = delete ;
195+ TradingView& operator =(TradingView& o) = delete ;
196+ protected:
197+ virtual bool clicked (ScreenCoordinate coord);
198+ public:
199+ TradingView (Player& initiating, Player& receiving, std::function<bool (std::array<int , 5 >, ScreenCoordinate)> trade, std::function<bool (ScreenCoordinate)> cancel, std::array<int , 5 > offer);
200+ virtual ~TradingView ();
201+
202+ void render ();
203+ };
204+
205+ #endif
0 commit comments