@@ -18,15 +18,14 @@ class GameView;
1818
1919class ViewElement {
2020private:
21- GameView& view;
2221 std::pair<ScreenCoordinate, ScreenCoordinate> rect;
2322
24- ViewElement (const ViewElement& vw) : view(vw.view) {} // deleted
23+ ViewElement (const ViewElement& vw) {} // deleted
2524 ViewElement& operator =(const ViewElement&) { return *this ; } // deleted
2625protected:
2726 virtual bool clicked (ScreenCoordinate coord) = 0;
2827public:
29- ViewElement (GameView& view, decltype (rect) rect);
28+ ViewElement (decltype (rect) rect);
3029 virtual ~ViewElement ();
3130
3231 virtual decltype (rect) getRect() const ;
@@ -38,18 +37,19 @@ class ViewElement {
3837class GameView {
3938private:
4039 GameBoard& model;
41- GameController& controller;
4240
4341 std::vector<std::unique_ptr<ViewElement>> viewElements;
4442
45- GameView (const GameView& o) : model(o.model), controller(o.controller) {} // deleted
43+ GameView (const GameView& o) : model(o.model) {} // deleted
4644 GameView& operator =(const GameView& o) { return *this ; } // deleted
4745public:
48- GameView (GameBoard&, GameController& );
46+ GameView (GameBoard&);
4947 ~GameView ();
5048
5149 void render ();
5250 bool acceptInput (SDL_Event& event);
51+
52+ void addElement (std::unique_ptr<ViewElement>);
5353};
5454
5555class DrawingGameVisitor : public GameVisitor {
@@ -87,23 +87,41 @@ template<class Fn>
8787class ViewButton : public ViewElement {
8888private:
8989 Fn action;
90- std::tuple<float , float , float > color;
9190
9291 ViewButton (const ViewButton& vb) : ViewElement(vb) {} // deleted
93- ViewButton& operator =(const ViewButton& vb ) { return *this ; }
92+ ViewButton& operator =(const ViewButton&) { return *this ; } // deleted
9493protected:
9594 virtual bool clicked (ScreenCoordinate coord) {
9695 return action (coord);
9796 }
9897public:
99- ViewButton (Fn& action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple< float , float , float > color ) : ViewElement(rect), action(action), color(color ) {}
100- ~ViewButton () {}
98+ ViewButton (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect) : ViewElement(rect), action(action) {}
99+ virtual ~ViewButton () {}
101100
102- void render () {
101+ virtual void render () {}
102+ };
103+
104+ template <class Fn >
105+ std::unique_ptr<ViewElement> makeViewButton (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect) {
106+ return std::unique_ptr<ViewElement>(new ViewButton<Fn>(fn, rect));
107+ }
108+
109+ template <class Fn >
110+ class ViewButtonColor : public ViewButton <Fn> {
111+ private:
112+ std::tuple<float , float , float > color;
113+
114+ ViewButtonColor (const ViewButtonColor& vb) : ViewElement(vb) {} // deleted
115+ ViewButtonColor& operator =(const ViewButtonColor& vb) { return *this ; }
116+ public:
117+ ViewButtonColor (Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) : ViewElement(rect, action), color(color) {}
118+ virtual ~ViewButtonColor () {}
119+
120+ virtual void render () {
103121 glBindTexture (GL_TEXTURE_2D, 0 );
104122 glColor3f (std::get<0 >(color), std::get<1 >(color), std::get<2 >(color));
105- auto topLeft = getRect ().first ;
106- auto bottomRight = getRect ().second ;
123+ auto topLeft = ViewElement:: getRect ().first ;
124+ auto bottomRight = ViewElement:: getRect ().second ;
107125 glBegin (GL_QUADS);
108126 glVertex3f (topLeft.first , topLeft.second );
109127 glVertex3f (bottomRight.first , topLeft.second );
@@ -113,4 +131,9 @@ class ViewButton : public ViewElement {
113131 }
114132};
115133
134+ template <class Fn >
135+ std::unique_ptr<ViewElement> makeViewButtonColor (Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float , float , float > color) {
136+ return std::unique_ptr<ViewElement>(new ViewButtonColor<Fn>(fn, rect, color));
137+ }
138+
116139#endif
0 commit comments