Skip to content

Commit ea5cf4c

Browse files
committed
Added the files. Forgot to do so earlier.
1 parent ba026bf commit ea5cf4c

File tree

2 files changed

+492
-0
lines changed

2 files changed

+492
-0
lines changed

include/ViewElement.h

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
#ifndef VIEW_ELEMENT_H
2+
#define VIEW_ELEMENT_H
3+
4+
#include <vector>
5+
#include <memory>
6+
#include <array>
7+
8+
#include "SDL2/SDL.h"
9+
#include "SDL2/SDL_opengl.h"
10+
#include "GL/gl.h"
11+
12+
#include "Util.h"
13+
14+
15+
/**
16+
* An element that is drawn on screen and can receive inputs from the user. These all occupy a rectangular area on screen
17+
* and can choose to handle clicks from the user that are inside their area.
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+
};
36+
37+
38+
/**
39+
* A view element that is invisible and calls a callback function when it is clicked.
40+
*/
41+
class ViewButton : public ViewElement {
42+
private:
43+
std::function<bool(ScreenCoordinate)> action;
44+
45+
ViewButton(const ViewButton& vb) = delete;
46+
ViewButton& operator=(const ViewButton&) = delete;
47+
protected:
48+
virtual bool clicked(ScreenCoordinate coord);
49+
public:
50+
ViewButton(std::function<bool(ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect);
51+
virtual ~ViewButton();
52+
53+
virtual void render();
54+
};
55+
56+
/**
57+
* Constructs a ViewButton using the same parameters as the ViewButton. Exists because template inference exists only
58+
* for functions, not classes.
59+
* @param fn The callback function to be called with the ScreenCoordinate clicked and returning a boolean on if it was handled.
60+
* @param rect The location on screen that the invisible button receives clicks from.
61+
* @return An owning unique pointer to the constructed view button.
62+
*/
63+
template<class Fn>
64+
std::unique_ptr<ViewElement> makeViewButton(Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect) {
65+
return std::unique_ptr<ViewElement>(new ViewButton(fn, rect));
66+
}
67+
68+
/**
69+
* A view element drawn as a solid color that has a callback function that is called when it is clicked.
70+
*/
71+
class ViewButtonColor : public ViewButton {
72+
private:
73+
std::tuple<float, float, float> color;
74+
75+
ViewButtonColor(const ViewButtonColor& vb) = delete;
76+
ViewButtonColor& operator=(const ViewButtonColor& vb) = delete;
77+
public:
78+
ViewButtonColor(std::function<bool(ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color);
79+
virtual ~ViewButtonColor();
80+
81+
virtual void render();
82+
};
83+
84+
85+
/**
86+
* Constructs a ViewButtonColor using the same parameters as the ViewButtonColor. Exists because template inference exists only
87+
* for functions, not classes.
88+
* @param fn The callback function to be called with the ScreenCoordinate clicked and returning a boolean on if it was handled.
89+
* @param rect The location on screen to draw to and receive clicks from.
90+
* @param color The color to draw the button. RGB floats from 0 to 1 for intensity.
91+
* @return An owning unique pointer to the constructed view button.
92+
*/
93+
template<class Fn>
94+
std::unique_ptr<ViewElement> makeViewButtonColor(Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color) {
95+
return std::unique_ptr<ViewElement>(new ViewButtonColor(fn, rect, color));
96+
}
97+
98+
/**
99+
* A view element drawn as some text on the screen that has a callback function when it is clicked.
100+
*/
101+
class ViewButtonText : public ViewButton {
102+
private:
103+
GLuint texture;
104+
105+
ViewButtonText(const ViewButtonText& vb) = delete;
106+
ViewButtonText& operator=(const ViewButtonText& vb) = delete;
107+
public:
108+
ViewButtonText(std::function<bool(ScreenCoordinate)> action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text);
109+
virtual ~ViewButtonText();
110+
111+
void setText(const std::string& font, int fontSize, const std::string& text);
112+
113+
virtual void render();
114+
};
115+
116+
/**
117+
* Constructs a ViewButtonText using the same parameters as the ViewButtonText. Exists because template inference exists only
118+
* for functions, not classes.
119+
* @param fn The callback function to be called with the ScreenCoordinate clicked and returning a boolean on if it was handled.
120+
* @param rect The location on screen to draw to and receive clicks from.
121+
* @param font The path to the font to use to draw the text.
122+
* @param fontSize The font size of the text.
123+
* @param text The text to render.
124+
*/
125+
template<class Fn>
126+
std::unique_ptr<ViewElement> makeViewButtonText(Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, const std::string& font, int fontSize, const std::string& text) {
127+
return std::unique_ptr<ViewElement>(new ViewButtonText(fn, rect, font, fontSize, text));
128+
}
129+
130+
class TradingView : public ViewElement {
131+
private:
132+
std::string initiating;
133+
std::string receiving;
134+
135+
ViewButtonText trade;
136+
ViewButtonText cancel;
137+
138+
std::array<int, 5> offer;
139+
140+
TradingView(TradingView& o) = delete;
141+
TradingView& operator=(TradingView& o) = delete;
142+
protected:
143+
virtual bool clicked(ScreenCoordinate coord);
144+
public:
145+
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);
146+
virtual ~TradingView();
147+
148+
void render();
149+
};
150+
151+
152+
class ConfirmationDialogue : public ViewElement {
153+
private:
154+
ScreenCoordinate topLeft;
155+
ScreenCoordinate bottomRight;
156+
157+
std::string message;
158+
159+
std::unique_ptr<ViewElement> confirmButton;
160+
std::unique_ptr<ViewElement> cancelButton;
161+
162+
protected:
163+
virtual bool clicked(ScreenCoordinate coord);
164+
165+
public:
166+
ConfirmationDialogue(std::function<bool(ScreenCoordinate)> confirm_action, std::function<bool(ScreenCoordinate)> cancel_action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::string message);
167+
void render();
168+
};
169+
170+
/**
171+
* Creates a unique_ptr to a confirmation dialogue view element.
172+
*
173+
* @param confirm_fn the function to run when clicking the confirm button
174+
* @param cancel_fn the function to run when clicking the cancel button
175+
* @param rect the coorinates of the bottom left and top right protions of the main dialogue box
176+
* @param message the message to display in the confirmation box
177+
* @return a unique_ptr to a newly created confirmation dialogue in the form of a view element
178+
*/
179+
template<class Fn>
180+
std::unique_ptr<ViewElement> makeConfirmationDialogue(Fn confirm_fn, Fn cancel_fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::string message) {
181+
return std::unique_ptr<ViewElement>(new ConfirmationDialogue(confirm_fn, cancel_fn, rect, message));
182+
}
183+
184+
185+
186+
187+
188+
189+
190+
191+
192+
193+
194+
195+
196+
#endif

0 commit comments

Comments
 (0)