-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidget.hpp
181 lines (150 loc) · 4.43 KB
/
Widget.hpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#pragma once
#include <vector>
#include <string>
#include <cassert>
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Events.hpp"
#include "TextureManager.hpp"
using size_t = long unsigned;
namespace gGUI {
class Widget
{
protected:
size_t x;
size_t y;
size_t w;
size_t h;
/* These are updated at every draw(). */
size_t global_x;
size_t global_y;
bool isShown = true;
Widget *parent;
std::vector<Widget*> children;
TextureManager *manager;
sf::Sprite sprite;
std::string texture;
void setTexture(std::string t)
{
assert(manager != nullptr);
texture = t;
sprite.setTexture(manager->get(t));
sprite.setScale(w / sprite.getLocalBounds().width,
h / sprite.getLocalBounds().height);
}
public:
Widget(size_t x, size_t y, size_t w, size_t h, Widget *p = nullptr, std::string t = "badtexture")
: x(x), y(y), w(w), h(h), parent(p), manager(nullptr), texture(t)
{
printf("Widget (%p) created with parent (%p)!\n", this, p);
if (parent != nullptr) {
parent->add_child(this);
manager = parent->manager;
if (manager != nullptr and texture != "NONE")
setTexture(texture);
}
}
virtual void draw(sf::RenderWindow &window, size_t p_x, size_t p_y)
{
if (not isShown)
return;
global_x = p_x + x;
global_y = p_y + y;
sprite.setPosition(global_x, global_y);
window.draw(sprite);
for (auto child : children)
child->draw(window, p_x + x, p_y + y);
}
virtual void add_child(Widget *child)
{
assert(child != nullptr);
children.push_back(child);
}
virtual Widget* belongs(size_t pos_x, size_t pos_y, size_t parent_x = 0, size_t parent_y = 0) const
{
if (not isShown)
return nullptr;
for (auto child : children) {
Widget *res = child->belongs(pos_x, pos_y, parent_x + x, parent_y + y);
if (res)
return res;
}
if ((pos_x <= x + parent_x) || (pos_y <= y + parent_y)
|| (w <= pos_x - x - parent_x) || (h <= pos_y - y - parent_y))
return nullptr;
assert(manager);
if (texture == "NONE")
return nullptr;
size_t cur_x = pos_x - parent_x - x;
size_t cur_y = pos_y - parent_y - y;
cur_x *= sprite.getLocalBounds().width / w;
cur_y *= sprite.getLocalBounds().height / h;
sf::Color c = manager->images[texture].getPixel(cur_x, cur_y);
if (c.a == 0)
return nullptr;
return (Widget*)this;
}
virtual void move(size_t new_x, size_t new_y)
{
x = new_x;
y = new_y;
}
virtual void resize(size_t new_w, size_t new_h)
{
if (new_w != -1)
w = new_w;
if (new_h != -1)
h = new_h;
sprite.setScale(w / sprite.getLocalBounds().width,
h / sprite.getLocalBounds().height);
}
size_t getWidth() const
{
return w;
}
size_t getHeight() const
{
return h;
}
size_t getX() const
{
return x;
}
size_t getY() const
{
return y;
}
size_t getGlobalX() const
{
return global_x;
}
size_t getGlobalY() const
{
return global_y;
}
void show()
{
isShown = true;
}
void hide()
{
isShown = false;
}
void setParent(Widget *w)
{
assert(w != nullptr);
parent = w;
manager = w->manager;
assert(manager);
setTexture(texture);
for (auto ch : children)
ch->setParent(this);
}
virtual void emitSignals(Event ev) {}
virtual void postload()
{
for (auto ch : children)
ch->postload();
}
};
}