Skip to content

Commit 50b5897

Browse files
committed
Merge pull request #70 from Databean/GameView_refactoring
Game view refactoring
2 parents d79d067 + 145e136 commit 50b5897

File tree

8 files changed

+981
-765
lines changed

8 files changed

+981
-765
lines changed

include/GameView.h

Lines changed: 11 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -18,84 +18,59 @@ class GameController;
1818
class ViewElement;
1919
class GameView;
2020

21-
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-
};
43-
4421
/**
4522
* The class in charge of drawing the view to the screen, using OpenGL calls.
4623
*/
4724
class GameView {
4825
private:
4926
GameBoard& model;
50-
27+
5128
std::map<int, std::unique_ptr<ViewElement>> viewElements;
5229
std::vector<ScreenCoordinate> pointsOfInterest;
5330

5431
void highlightPoint(ScreenCoordinate & coord);
55-
32+
5633
std::string controlStateText;
5734

5835
GameView(const GameView& o) = delete;
5936
GameView& operator=(const GameView& o) = delete;
60-
37+
6138
public:
6239
GameView(GameBoard&);
6340
~GameView();
64-
41+
6542
void render();
6643
bool acceptInput(SDL_Event& event);
67-
68-
void setControlStateText(std::string newText);
6944

45+
void setControlStateText(std::string newText);
7046

7147
void addPointOfInterest(ScreenCoordinate);
7248
void clearPointsOfInterest();
7349
void addElement(std::unique_ptr<ViewElement> element);
7450
void addElement(int priority, std::unique_ptr<ViewElement>);
75-
7651

7752
std::unique_ptr<ViewElement> removeElement(int priority);
7853
std::unique_ptr<ViewElement> removeElement(const ViewElement*);
7954
std::unique_ptr<ViewElement> removeElement(const ViewElement&);
80-
81-
void drawCardCount(std::string font, int fontSize);
55+
56+
void drawCardCount(std::string font, int fontSize);
8257
void drawResourceCount(std::string font, int fontSize);
83-
bool showTotals;
58+
bool showTotals;
8459
};
8560

8661
/**
8762
* A visitor of the GameBoard hierarchy that draws the entire model.
8863
*/
89-
class DrawingGameVisitor : public GameVisitor {
64+
class DrawingGameVisitor: public GameVisitor {
9065
private:
9166
GameView& view;
92-
67+
9368
DrawingGameVisitor(const DrawingGameVisitor& o) = delete;
9469
DrawingGameVisitor& operator=(const DrawingGameVisitor& o) = delete;
9570
public:
9671
DrawingGameVisitor(GameView& view);
9772
~DrawingGameVisitor();
98-
73+
9974
virtual void visit(GameBoard&);
10075
virtual void visit(Road&);
10176
virtual void visit(Settlement&);
@@ -107,147 +82,4 @@ class DrawingGameVisitor : public GameVisitor {
10782
virtual void visit(Wonder&);
10883
};
10984

110-
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-
25385
#endif

include/Renderer.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,25 @@
1313
void renderBoard(const GameBoard& board, const Player& perspective);
1414

1515
GLuint loadImageAsTexture(const std::string& name);
16-
GLuint loadTextAsTexture(const std::string& font, int fontSize, const std::string& text);
17-
18-
void renderText(const std::string& font, int fontSize, const std::pair<float, float> bottomLeft, const std::pair<float, float> topRight, const std::string& text);
16+
GLuint loadTextAsTexture(const std::string& font, int fontSize,
17+
const std::string& text);
18+
19+
void renderText(const std::string& font, int fontSize,
20+
const std::pair<float, float> bottomLeft,
21+
const std::pair<float, float> topRight, const std::string& text);
22+
void renderText(const std::pair<float, float> bottomLeft,
23+
const std::pair<float, float> topRight, const GLuint& texture);
24+
void renderRectangle(const std::pair<float, float> bottomLeft,
25+
const std::pair<float, float> topRight,
26+
const std::tuple<float, float, float> color);
27+
28+
void renderTexturedCircle(const std::pair<float, float> texCenter,
29+
const float texRadius, const std::pair<float, float> screenCenter,
30+
const float screenRadius, const GLuint& texture, int articulation = 20);
31+
void renderTexturedRectangle(const std::pair<float, float> screenBottomLeft,
32+
const std::pair<float, float> screenTopRight,
33+
const std::pair<float, float> texBottomLeft,
34+
const std::pair<float, float> texTopRight, const GLuint& texture);
1935

2036
std::pair<float, float> coordToScreen(const Coordinate& coord);
2137
Coordinate screenToCoord(const std::pair<float, float>&);
@@ -24,6 +40,7 @@ void vertexPair(const Coordinate& coord);
2440

2541
void texCoordPair(const std::pair<float, float>& p);
2642

27-
std::pair<float, float> averagePoint(const std::vector<std::pair<float, float>>& points);
43+
std::pair<float, float> averagePoint(
44+
const std::vector<std::pair<float, float>>& points);
2845

2946
#endif

0 commit comments

Comments
 (0)