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 ();
@@ -86,22 +93,19 @@ class DrawingGameVisitor : public GameVisitor {
8693/* *
8794 * A view element that is invisible and calls a callback function when it is clicked.
8895 */
89- template <class Fn >
9096class ViewButton : public ViewElement {
9197private:
92- Fn action;
98+ std::function< bool (ScreenCoordinate)> action;
9399
94- ViewButton (const ViewButton& vb) : ViewElement(vb) {} // deleted
95- ViewButton& operator =(const ViewButton&) { return * this ; } // deleted
100+ ViewButton (const ViewButton& vb) = delete ;
101+ ViewButton& operator =(const ViewButton&) = delete ;
96102protected:
97- virtual bool clicked (ScreenCoordinate coord) {
98- return action (coord);
99- }
103+ virtual bool clicked (ScreenCoordinate coord);
100104public:
101- ViewButton (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect) : ViewElement(rect), action(action) {}
102- virtual ~ViewButton () {}
105+ ViewButton (std::function< bool (ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect);
106+ virtual ~ViewButton ();
103107
104- virtual void render () {}
108+ virtual void render ();
105109};
106110
107111/* *
@@ -113,35 +117,23 @@ class ViewButton : public ViewElement {
113117 */
114118template <class Fn >
115119std::unique_ptr<ViewElement> makeViewButton (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect) {
116- return std::unique_ptr<ViewElement>(new ViewButton<Fn> (fn, rect));
120+ return std::unique_ptr<ViewElement>(new ViewButton (fn, rect));
117121}
118122
119123/* *
120124 * A view element drawn as a solid color that has a callback function that is called when it is clicked.
121125 */
122- template <class Fn >
123- class ViewButtonColor : public ViewButton <Fn> {
126+ class ViewButtonColor : public ViewButton {
124127private:
125128 std::tuple<float , float , float > color;
126129
127- ViewButtonColor (const ViewButtonColor& vb) : ViewElement(vb) {} // deleted
128- ViewButtonColor& operator =(const ViewButtonColor& vb) { return * this ; }
130+ ViewButtonColor (const ViewButtonColor& vb) = delete ;
131+ ViewButtonColor& operator =(const ViewButtonColor& vb) = delete ;
129132public:
130- ViewButtonColor (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) : ViewButton<Fn>(action, rect), color(color) {}
131- virtual ~ViewButtonColor () {}
132-
133- virtual void render () {
134- glBindTexture (GL_TEXTURE_2D, 0 );
135- glColor3f (std::get<0 >(color), std::get<1 >(color), std::get<2 >(color));
136- auto topLeft = ViewElement::getRect ().first ;
137- auto bottomRight = ViewElement::getRect ().second ;
138- glBegin (GL_QUADS);
139- glVertex2f (topLeft.first , topLeft.second );
140- glVertex2f (bottomRight.first , topLeft.second );
141- glVertex2f (bottomRight.first , bottomRight.second );
142- glVertex2f (topLeft.first , bottomRight.second );
143- glEnd ();
144- }
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 ();
145137};
146138
147139/* *
@@ -154,7 +146,60 @@ class ViewButtonColor : public ViewButton<Fn> {
154146 */
155147template <class Fn >
156148std::unique_ptr<ViewElement> makeViewButtonColor (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) {
157- return std::unique_ptr<ViewElement>(new ViewButtonColor<Fn> (fn, rect, color));
149+ return std::unique_ptr<ViewElement>(new ViewButtonColor (fn, rect, color));
158150}
159151
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+
160205#endif
0 commit comments