11#ifndef GAME_VIEW_H
22#define GAME_VIEW_H
33
4+ #include < vector>
5+ #include < memory>
6+
47#include " SDL2/SDL.h"
8+ #include " SDL2/SDL_opengl.h"
9+ #include " GL/gl.h"
510
611#include " GameVisitor.h"
712#include " Util.h"
813
914class GameBoard ;
1015class GameController ;
16+ class ViewElement ;
17+ class GameView ;
18+
19+ class ViewElement {
20+ private:
21+ std::pair<ScreenCoordinate, ScreenCoordinate> rect;
22+
23+ ViewElement (const ViewElement& vw) {} // deleted
24+ ViewElement& operator =(const ViewElement&) { return *this ; } // deleted
25+ protected:
26+ virtual bool clicked (ScreenCoordinate coord) = 0;
27+ public:
28+ ViewElement (decltype (rect) rect);
29+ virtual ~ViewElement ();
30+
31+ virtual decltype (rect) getRect() const ;
32+ virtual bool containsPoint (ScreenCoordinate coord) const ;
33+ virtual bool handleClick (ScreenCoordinate coord);
34+ virtual void render () = 0;
35+ };
1136
1237class GameView {
1338private:
1439 GameBoard& model;
15- GameController& controller;
1640
17- GameView (const GameView& o) : model(o.model), controller(o.controller) {} // deleted
41+ std::vector<std::unique_ptr<ViewElement>> viewElements;
42+
43+ GameView (const GameView& o) : model(o.model) {} // deleted
1844 GameView& operator =(const GameView& o) { return *this ; } // deleted
1945public:
20- GameView (GameBoard&, GameController& );
46+ GameView (GameBoard&);
2147 ~GameView ();
2248
2349 void render ();
2450 bool acceptInput (SDL_Event& event);
51+
52+ void addElement (std::unique_ptr<ViewElement>);
2553};
2654
2755class DrawingGameVisitor : public GameVisitor {
@@ -43,16 +71,57 @@ class DrawingGameVisitor : public GameVisitor {
4371 virtual void visit (DevelopmentCard&);
4472};
4573
46- class ClickCoordinateEvent {
74+ template <class Fn >
75+ class ViewButton : public ViewElement {
4776private:
48- Coordinate clicked;
77+ Fn action;
78+
79+ ViewButton (const ViewButton& vb) : ViewElement(vb) {} // deleted
80+ ViewButton& operator =(const ViewButton&) { return *this ; } // deleted
81+ protected:
82+ virtual bool clicked (ScreenCoordinate coord) {
83+ return action (coord);
84+ }
4985public:
50- ClickCoordinateEvent (const Coordinate& clicked);
51- ClickCoordinateEvent (const ClickCoordinateEvent&);
52- ~ClickCoordinateEvent ();
53- ClickCoordinateEvent& operator =(const ClickCoordinateEvent&);
86+ ViewButton (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect) : ViewElement(rect), action(action) {}
87+ virtual ~ViewButton () {}
5488
55- Coordinate getCoordinate () const ;
89+ virtual void render () {}
5690};
5791
92+ template <class Fn >
93+ std::unique_ptr<ViewElement> makeViewButton (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect) {
94+ return std::unique_ptr<ViewElement>(new ViewButton<Fn>(fn, rect));
95+ }
96+
97+ template <class Fn >
98+ class ViewButtonColor : public ViewButton <Fn> {
99+ private:
100+ std::tuple<float , float , float > color;
101+
102+ ViewButtonColor (const ViewButtonColor& vb) : ViewElement(vb) {} // deleted
103+ ViewButtonColor& operator =(const ViewButtonColor& vb) { return *this ; }
104+ public:
105+ ViewButtonColor (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) : ViewButton<Fn>(action, rect), color(color) {}
106+ virtual ~ViewButtonColor () {}
107+
108+ virtual void render () {
109+ glBindTexture (GL_TEXTURE_2D, 0 );
110+ glColor3f (std::get<0 >(color), std::get<1 >(color), std::get<2 >(color));
111+ auto topLeft = ViewElement::getRect ().first ;
112+ auto bottomRight = ViewElement::getRect ().second ;
113+ glBegin (GL_QUADS);
114+ glVertex2f (topLeft.first , topLeft.second );
115+ glVertex2f (bottomRight.first , topLeft.second );
116+ glVertex2f (bottomRight.first , bottomRight.second );
117+ glVertex2f (topLeft.first , bottomRight.second );
118+ glEnd ();
119+ }
120+ };
121+
122+ template <class Fn >
123+ std::unique_ptr<ViewElement> makeViewButtonColor (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) {
124+ return std::unique_ptr<ViewElement>(new ViewButtonColor<Fn>(fn, rect, color));
125+ }
126+
58127#endif
0 commit comments