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 ;
@@ -46,10 +48,10 @@ class GameView {
4648private:
4749 GameBoard& model;
4850
49- std::vector< std::unique_ptr<ViewElement>> viewElements;
51+ std::map< int , std::unique_ptr<ViewElement>> viewElements;
5052
51- GameView (const GameView& o) : model(o.model) {} // deleted
52- GameView& operator =(const GameView& o) { return * this ; } // deleted
53+ GameView (const GameView& o) = delete ;
54+ GameView& operator =(const GameView& o) = delete ;
5355public:
5456 GameView (GameBoard&);
5557 ~GameView ();
@@ -58,6 +60,11 @@ class GameView {
5860 bool acceptInput (SDL_Event& event);
5961
6062 void addElement (std::unique_ptr<ViewElement>);
63+ void addElement (int priority, std::unique_ptr<ViewElement>);
64+
65+ std::unique_ptr<ViewElement> removeElement (int priority);
66+ std::unique_ptr<ViewElement> removeElement (const ViewElement*);
67+ std::unique_ptr<ViewElement> removeElement (const ViewElement&);
6168};
6269
6370/* *
@@ -67,8 +74,8 @@ class DrawingGameVisitor : public GameVisitor {
6774private:
6875 GameView& view;
6976
70- DrawingGameVisitor (const DrawingGameVisitor& o) : view(o.view) {} // deleted
71- DrawingGameVisitor& operator =(const DrawingGameVisitor& o) { return * this ; } // deleted
77+ DrawingGameVisitor (const DrawingGameVisitor& o) = delete ;
78+ DrawingGameVisitor& operator =(const DrawingGameVisitor& o) = delete ;
7279public:
7380 DrawingGameVisitor (GameView& view);
7481 ~DrawingGameVisitor ();
@@ -87,22 +94,19 @@ class DrawingGameVisitor : public GameVisitor {
8794/* *
8895 * A view element that is invisible and calls a callback function when it is clicked.
8996 */
90- template <class Fn >
9197class ViewButton : public ViewElement {
9298private:
93- Fn action;
99+ std::function< bool (ScreenCoordinate)> action;
94100
95- ViewButton (const ViewButton& vb) : ViewElement(vb) {} // deleted
96- ViewButton& operator =(const ViewButton&) { return * this ; } // deleted
101+ ViewButton (const ViewButton& vb) = delete ;
102+ ViewButton& operator =(const ViewButton&) = delete ;
97103protected:
98- virtual bool clicked (ScreenCoordinate coord) {
99- return action (coord);
100- }
104+ virtual bool clicked (ScreenCoordinate coord);
101105public:
102- ViewButton (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect) : ViewElement(rect), action(action) {}
103- virtual ~ViewButton () {}
106+ ViewButton (std::function< bool (ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect);
107+ virtual ~ViewButton ();
104108
105- virtual void render () {}
109+ virtual void render ();
106110};
107111
108112/* *
@@ -114,35 +118,23 @@ class ViewButton : public ViewElement {
114118 */
115119template <class Fn >
116120std::unique_ptr<ViewElement> makeViewButton (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect) {
117- return std::unique_ptr<ViewElement>(new ViewButton<Fn> (fn, rect));
121+ return std::unique_ptr<ViewElement>(new ViewButton (fn, rect));
118122}
119123
120124/* *
121125 * A view element drawn as a solid color that has a callback function that is called when it is clicked.
122126 */
123- template <class Fn >
124- class ViewButtonColor : public ViewButton <Fn> {
127+ class ViewButtonColor : public ViewButton {
125128private:
126129 std::tuple<float , float , float > color;
127130
128- ViewButtonColor (const ViewButtonColor& vb) : ViewElement(vb) {} // deleted
129- ViewButtonColor& operator =(const ViewButtonColor& vb) { return * this ; }
131+ ViewButtonColor (const ViewButtonColor& vb) = delete ;
132+ ViewButtonColor& operator =(const ViewButtonColor& vb) = delete ;
130133public:
131- ViewButtonColor (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) : ViewButton<Fn>(action, rect), color(color) {}
132- virtual ~ViewButtonColor () {}
133-
134- virtual void render () {
135- glBindTexture (GL_TEXTURE_2D, 0 );
136- glColor3f (std::get<0 >(color), std::get<1 >(color), std::get<2 >(color));
137- auto topLeft = ViewElement::getRect ().first ;
138- auto bottomRight = ViewElement::getRect ().second ;
139- glBegin (GL_QUADS);
140- glVertex2f (topLeft.first , topLeft.second );
141- glVertex2f (bottomRight.first , topLeft.second );
142- glVertex2f (bottomRight.first , bottomRight.second );
143- glVertex2f (topLeft.first , bottomRight.second );
144- glEnd ();
145- }
134+ ViewButtonColor (std::function<bool (ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color);
135+ virtual ~ViewButtonColor ();
136+
137+ virtual void render ();
146138};
147139
148140/* *
@@ -155,7 +147,60 @@ class ViewButtonColor : public ViewButton<Fn> {
155147 */
156148template <class Fn >
157149std::unique_ptr<ViewElement> makeViewButtonColor (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) {
158- return std::unique_ptr<ViewElement>(new ViewButtonColor<Fn> (fn, rect, color));
150+ return std::unique_ptr<ViewElement>(new ViewButtonColor (fn, rect, color));
159151}
160152
153+ /* *
154+ * A view element drawn as some text on the screen that has a callback function when it is clicked.
155+ */
156+ class ViewButtonText : public ViewButton {
157+ private:
158+ GLuint texture;
159+
160+ ViewButtonText (const ViewButtonText& vb) = delete ;
161+ ViewButtonText& operator =(const ViewButtonText& vb) = delete ;
162+ public:
163+ ViewButtonText (std::function<bool (ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text);
164+ virtual ~ViewButtonText ();
165+
166+ void setText (const std::string& font, int fontSize, const std::string& text);
167+
168+ virtual void render ();
169+ };
170+
171+ /* *
172+ * Constructs a ViewButtonText using the same parameters as the ViewButtonText. Exists because template inference exists only
173+ * for functions, not classes.
174+ * @param fn The callback function to be called with the ScreenCoordinate clicked and returning a boolean on if it was handled.
175+ * @param rect The location on screen to draw to and receive clicks from.
176+ * @param font The path to the font to use to draw the text.
177+ * @param fontSize The font size of the text.
178+ * @param text The text to render.
179+ */
180+ template <class Fn >
181+ std::unique_ptr<ViewElement> makeViewButtonText (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text) {
182+ return std::unique_ptr<ViewElement>(new ViewButtonText (fn, rect, font, fontSize, text));
183+ }
184+
185+ class TradingView : public ViewElement {
186+ private:
187+ Player& initiating;
188+ Player& receiving;
189+
190+ ViewButtonText trade;
191+ ViewButtonText cancel;
192+
193+ std::array<int , 5 > offer;
194+
195+ TradingView (TradingView& o) = delete ;
196+ TradingView& operator =(TradingView& o) = delete ;
197+ protected:
198+ virtual bool clicked (ScreenCoordinate coord);
199+ public:
200+ TradingView (Player& initiating, Player& receiving, std::function<bool (std::array<int , 5 >, ScreenCoordinate)> trade, std::function<bool (ScreenCoordinate)> cancel, std::array<int , 5 > offer);
201+ virtual ~TradingView ();
202+
203+ void render ();
204+ };
205+
161206#endif
0 commit comments