-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets.h
129 lines (92 loc) · 2.25 KB
/
widgets.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef WIDGETS_H
#define WIDGETS_H
#include <SDL2/SDL.h>
#include <iostream>
#include "util.h"
class Font {
public:
static void init(SDL_Renderer *r);
static void drawString(SDL_Renderer *r, SDL_Point p, const std::string& s);
static int renderedWidth(const std::string& s);
static const int charw = 8;
static const int charh = 8;
private:
static constexpr const char* const fontFile = "font.bmp";
static SDL_Texture *font;
};
class Widget {
public:
SDL_Rect bounds;
virtual void doEvent(SDL_Event e) {};
virtual void draw(SDL_Renderer *r) {};
virtual void gainFocus() {};
virtual void loseFocus() {};
};
class Label : public Widget {
public:
Label(SDL_Point p, const std::string& s);
void resize();
void setText(const std::string &s);
void draw(SDL_Renderer *r);
protected:
const int padding = 2;
const int minHeight = Font::charh + 2 * padding;
std::string text;
};
class Button;
class ButtonHandler {
public:
virtual void handleButton(Button *) = 0;
};
class Button : public Widget {
public:
Button(SDL_Point p, const std::string& s);
void setHandler(ButtonHandler *);
void doEvent(SDL_Event e);
void draw(SDL_Renderer *r);
protected:
const int padding = 8;
const int minHeight = Font::charh + 2 * padding;
enum State { UP, DOWN };
State state;
std::string text;
ButtonHandler* bh;
};
class CheckBox : public Button {
public:
CheckBox(SDL_Point p, const std::string& s, bool *b);
void setBinding(bool *b);
void doEvent(SDL_Event e);
void draw(SDL_Renderer *r);
protected:
bool *binding;
};
class TextBox : public Widget {
public:
TextBox(SDL_Point p, const std::string& s);
void setText(const std::string &s);
std::string getText();
void doEvent(SDL_Event e);
void draw(SDL_Renderer *r);
void gainFocus();
void loseFocus();
protected:
const int padding = 4;
const int minHeight = Font::charh + 2 * padding;
int cursorpos;
std::string text;
};
class NumBox : public TextBox {
public:
NumBox(SDL_Point p, double d);
double getNum();
void setNum(double d);
void gainFocus();
void loseFocus();
void doEvent(SDL_Event e);
protected:
double num;
//Either sets number from inputed text or resets to last valid value
void validate();
};
#endif