Skip to content

Commit bd9294d

Browse files
committed
Started by moving ViewElements to a separate .h file to remove the
clutter from the GameView.h and GameView.cpp
1 parent 7b8de2b commit bd9294d

File tree

3 files changed

+23
-497
lines changed

3 files changed

+23
-497
lines changed

include/GameView.h

Lines changed: 0 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,6 @@ class ViewElement;
1919
class GameView;
2020

2121

22-
/**
23-
* An element that is drawn on screen and can receive inputs from the user. These all occupy a rectangular area on screen
24-
* and can choose to handle clicks from the user that are inside their area.
25-
*/
26-
class ViewElement {
27-
private:
28-
std::pair<ScreenCoordinate, ScreenCoordinate> rect;
29-
30-
ViewElement(const ViewElement& vw) {} //deleted
31-
ViewElement& operator=(const ViewElement&) { return *this; } // deleted
32-
protected:
33-
virtual bool clicked(ScreenCoordinate coord) = 0;
34-
public:
35-
ViewElement(decltype(rect) rect);
36-
virtual ~ViewElement();
37-
38-
virtual decltype(rect) getRect() const;
39-
virtual bool containsPoint(ScreenCoordinate coord) const;
40-
virtual bool handleClick(ScreenCoordinate coord);
41-
virtual void render() = 0;
42-
};
4322

4423
/**
4524
* The class in charge of drawing the view to the screen, using OpenGL calls.
@@ -108,146 +87,4 @@ class DrawingGameVisitor : public GameVisitor {
10887
};
10988

11089

111-
/**
112-
* A view element that is invisible and calls a callback function when it is clicked.
113-
*/
114-
class ViewButton : public ViewElement {
115-
private:
116-
std::function<bool(ScreenCoordinate)> action;
117-
118-
ViewButton(const ViewButton& vb) = delete;
119-
ViewButton& operator=(const ViewButton&) = delete;
120-
protected:
121-
virtual bool clicked(ScreenCoordinate coord);
122-
public:
123-
ViewButton(std::function<bool(ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect);
124-
virtual ~ViewButton();
125-
126-
virtual void render();
127-
};
128-
129-
/**
130-
* Constructs a ViewButton using the same parameters as the ViewButton. Exists because template inference exists only
131-
* for functions, not classes.
132-
* @param fn The callback function to be called with the ScreenCoordinate clicked and returning a boolean on if it was handled.
133-
* @param rect The location on screen that the invisible button receives clicks from.
134-
* @return An owning unique pointer to the constructed view button.
135-
*/
136-
template<class Fn>
137-
std::unique_ptr<ViewElement> makeViewButton(Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect) {
138-
return std::unique_ptr<ViewElement>(new ViewButton(fn, rect));
139-
}
140-
141-
/**
142-
* A view element drawn as a solid color that has a callback function that is called when it is clicked.
143-
*/
144-
class ViewButtonColor : public ViewButton {
145-
private:
146-
std::tuple<float, float, float> color;
147-
148-
ViewButtonColor(const ViewButtonColor& vb) = delete;
149-
ViewButtonColor& operator=(const ViewButtonColor& vb) = delete;
150-
public:
151-
ViewButtonColor(std::function<bool(ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color);
152-
virtual ~ViewButtonColor();
153-
154-
virtual void render();
155-
};
156-
157-
158-
159-
160-
161-
/**
162-
* Constructs a ViewButtonColor using the same parameters as the ViewButtonColor. Exists because template inference exists only
163-
* for functions, not classes.
164-
* @param fn The callback function to be called with the ScreenCoordinate clicked and returning a boolean on if it was handled.
165-
* @param rect The location on screen to draw to and receive clicks from.
166-
* @param color The color to draw the button. RGB floats from 0 to 1 for intensity.
167-
* @return An owning unique pointer to the constructed view button.
168-
*/
169-
template<class Fn>
170-
std::unique_ptr<ViewElement> makeViewButtonColor(Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color) {
171-
return std::unique_ptr<ViewElement>(new ViewButtonColor(fn, rect, color));
172-
}
173-
174-
/**
175-
* A view element drawn as some text on the screen that has a callback function when it is clicked.
176-
*/
177-
class ViewButtonText : public ViewButton {
178-
private:
179-
GLuint texture;
180-
181-
ViewButtonText(const ViewButtonText& vb) = delete;
182-
ViewButtonText& operator=(const ViewButtonText& vb) = delete;
183-
public:
184-
ViewButtonText(std::function<bool(ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text);
185-
virtual ~ViewButtonText();
186-
187-
void setText(const std::string& font, int fontSize, const std::string& text);
188-
189-
virtual void render();
190-
};
191-
192-
/**
193-
* Constructs a ViewButtonText using the same parameters as the ViewButtonText. Exists because template inference exists only
194-
* for functions, not classes.
195-
* @param fn The callback function to be called with the ScreenCoordinate clicked and returning a boolean on if it was handled.
196-
* @param rect The location on screen to draw to and receive clicks from.
197-
* @param font The path to the font to use to draw the text.
198-
* @param fontSize The font size of the text.
199-
* @param text The text to render.
200-
*/
201-
template<class Fn>
202-
std::unique_ptr<ViewElement> makeViewButtonText(Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text) {
203-
return std::unique_ptr<ViewElement>(new ViewButtonText(fn, rect, font, fontSize, text));
204-
}
205-
206-
class TradingView : public ViewElement {
207-
private:
208-
std::string initiating;
209-
std::string receiving;
210-
211-
ViewButtonText trade;
212-
ViewButtonText cancel;
213-
214-
std::array<int, 5> offer;
215-
216-
TradingView(TradingView& o) = delete;
217-
TradingView& operator=(TradingView& o) = delete;
218-
protected:
219-
virtual bool clicked(ScreenCoordinate coord);
220-
public:
221-
TradingView(const std::string& initiating, const std::string& receiving, std::function<bool(std::array<int, 5>, ScreenCoordinate)> trade, std::function<bool(ScreenCoordinate)> cancel, std::array<int, 5> offer);
222-
virtual ~TradingView();
223-
224-
void render();
225-
};
226-
227-
228-
class ConfirmationDialogue : public ViewElement {
229-
private:
230-
ScreenCoordinate topLeft;
231-
ScreenCoordinate bottomRight;
232-
233-
std::string message;
234-
235-
std::unique_ptr<ViewElement> confirmButton;
236-
std::unique_ptr<ViewElement> cancelButton;
237-
238-
protected:
239-
virtual bool clicked(ScreenCoordinate coord);
240-
241-
public:
242-
ConfirmationDialogue(std::function<bool(ScreenCoordinate)> confirm_action, std::function<bool(ScreenCoordinate)> cancel_action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::string message);
243-
void render();
244-
};
245-
246-
template<class Fn>
247-
std::unique_ptr<ViewElement> makeConfirmationDialogue(Fn confirm_fn, Fn cancel_fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::string message) {
248-
return std::unique_ptr<ViewElement>(new ConfirmationDialogue(confirm_fn, cancel_fn, rect, message));
249-
}
250-
251-
252-
25390
#endif

src/GameController.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "Config.h"
66
#include "GameBoard.h"
7+
#include "ViewElement.h"
78
#include "GameView.h"
89
#include "Renderer.h"
910
#include "Player.h"

0 commit comments

Comments
 (0)