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+ GameView& view;
22+ std::pair<ScreenCoordinate, ScreenCoordinate> rect;
23+
24+ ViewElement (const ViewElement& vw) : view(vw.view) {} // deleted
25+ ViewElement& operator =(const ViewElement&) { return *this ; } // deleted
26+ protected:
27+ virtual bool clicked (ScreenCoordinate coord) = 0;
28+ public:
29+ ViewElement (GameView& view, decltype (rect) rect);
30+ virtual ~ViewElement ();
31+
32+ virtual decltype (rect) getRect() const ;
33+ virtual bool containsPoint (ScreenCoordinate coord) const ;
34+ virtual bool handleClick (ScreenCoordinate coord);
35+ virtual void render () = 0;
36+ };
1137
1238class GameView {
1339private:
1440 GameBoard& model;
1541 GameController& controller;
1642
43+ std::vector<std::unique_ptr<ViewElement>> viewElements;
44+
1745 GameView (const GameView& o) : model(o.model), controller(o.controller) {} // deleted
1846 GameView& operator =(const GameView& o) { return *this ; } // deleted
1947public:
@@ -55,4 +83,34 @@ class ClickCoordinateEvent {
5583 Coordinate getCoordinate () const ;
5684};
5785
86+ template <class Fn >
87+ class ViewButton : public ViewElement {
88+ private:
89+ Fn action;
90+ std::tuple<float , float , float > color;
91+
92+ ViewButton (const ViewButton& vb) : ViewElement(vb) {} // deleted
93+ ViewButton& operator =(const ViewButton& vb) { return *this ; }
94+ protected:
95+ virtual bool clicked (ScreenCoordinate coord) {
96+ return action (coord);
97+ }
98+ public:
99+ ViewButton (Fn& action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) : ViewElement(rect), action(action), color(color) {}
100+ ~ViewButton () {}
101+
102+ void render () {
103+ glBindTexture (GL_TEXTURE_2D, 0 );
104+ glColor3f (std::get<0 >(color), std::get<1 >(color), std::get<2 >(color));
105+ auto topLeft = getRect ().first ;
106+ auto bottomRight = getRect ().second ;
107+ glBegin (GL_QUADS);
108+ glVertex3f (topLeft.first , topLeft.second );
109+ glVertex3f (bottomRight.first , topLeft.second );
110+ glVertex3f (bottomRight.first , bottomRight.second );
111+ glVertex3f (topLeft.first , bottomRight.second );
112+ glEnd ();
113+ }
114+ };
115+
58116#endif
0 commit comments