-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.hpp
219 lines (185 loc) · 5.91 KB
/
MainWindow.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#pragma once
#include "color.hpp"
#include "line.hpp"
#include "Widget.hpp"
#include "ToolSetup.hpp"
#include <SFML/Graphics.hpp>
#include <cassert>
#include <filesystem>
#include <dlfcn.h>
namespace gGUI {
class TopBar;
class ToolSetup;
class ToolPalette;
class Canvas;
class MainWindow : public Widget
{
public:
TopBar *topBar = nullptr;
ToolSetup *toolSetup = nullptr;
ToolPalette *toolPalette = nullptr;
Canvas *canvas = nullptr;
private:
sf::RenderWindow *window = NULL;
sf::Font font;
bool is_closed = false;
void drawLine(float x1, float y1, float x2, float y2, g::color c)
{
sf::Vertex line[] =
{
sf::Vertex(sf::Vector2f(x1, y1), convertColor(c)),
sf::Vertex(sf::Vector2f(x2, y2), convertColor(c)),
};
window->draw(line, 2, sf::Lines);
}
void drawLine(const g::line &l, g::color c)
{
drawLine(l.getKer().x1, l.getKer().y1, l.getKer().x2, l.getKer().y2, c);
}
void drawText(float x, float y, const std::string &t)
{
sf::Text text;
text.setFont(font);
text.setString(t);
text.setCharacterSize(24);
text.setFillColor(sf::Color(0, 0, 0));
text.setPosition(x, y);
window->draw(text);
}
void setBackground(g::color c)
{
window->clear(convertColor(c));
}
sf::Color convertColor(g::color c)
{
return sf::Color(c.getR(), c.getG(), c.getB());
}
void update()
{
window->display();
}
Event getEvent()
{
static sf::Vector2i pos;
sf::Event e;
if (!(window->pollEvent(e)))
return Event(Event::None);
sf::Vector2i prev = pos;
pos = sf::Mouse::getPosition();
pos -= window->getPosition();
if (e.type == sf::Event::MouseButtonPressed)
return Event(Event::MousePress, g::vector2f(pos.x, pos.y), e.mouseButton.button == sf::Mouse::Button::Left);
else if (e.type == sf::Event::MouseButtonReleased)
return Event(Event::MouseRelease, g::vector2f(pos.x, pos.y), e.mouseButton.button == sf::Mouse::Button::Left);
else if (e.type == sf::Event::MouseMoved)
return Event(Event::MouseMove, g::vector2f(pos.x, pos.y), g::vector2f(prev.x, prev.y));
return Event(Event::Unsupported);
}
public:
void testSlots(Event ev)
{
std::cerr << "Button is pressed and slot in MainWindow activated!\n";
}
Slot testSlot = Slot(this, static_cast<handler_t>(&MainWindow::testSlots));
MainWindow(int w, int h, char name[]) : Widget(0, 0, w, h, nullptr, "NONE")
{
window = new sf::RenderWindow(sf::VideoMode(w, h), name);
assert(window);
manager = new TextureManager;
assert(manager);
assert(font.loadFromFile("../fonts/arial.ttf"));
window->setActive();
window->clear();
window->display();
}
~MainWindow()
{
assert(window);
delete window;
window = nullptr;
delete manager;
manager = nullptr;
}
void setTopBar(TopBar *tb)
{
assert(tb);
topBar = tb;
}
void setToolSetup(ToolSetup *ts)
{
if (toolSetup)
toolSetup->hide();
if (ts)
ts->show();
toolSetup = ts;
}
void setToolPalette(ToolPalette *tp)
{
assert(tp);
toolPalette = tp;
}
void setCanvas(Canvas *c)
{
assert(c);
canvas = c;
}
TopBar* getTopBar()
{
return topBar;
}
ToolSetup* getToolSetup()
{
return toolSetup;
}
ToolPalette* getToolPalette()
{
return toolPalette;
}
Canvas* getCanvas()
{
return canvas;
}
void initPlugins(std::string dir)
{
for (auto file : std::filesystem::directory_iterator(dir)) {
std::cerr << file.path().string() << "\n";
if (file.is_directory() or not file.path().string().ends_with(".aboba.so"))
continue;
void* dlHandler = dlopen(file.path().c_str(), RTLD_LAZY);
if (dlHandler) {
void (*init)() = nullptr;
*((void**)(&init)) = dlsym(dlHandler, "init_module");
(*init)();
} else {
fprintf(stderr, "ERROR: Unable to open plugin: %s\n", dlerror());
}
}
}
void run()
{
postload();
while (not is_closed) {
Event ev = getEvent();
if (ev.type != Event::Unsupported && ev.type != Event::None) {
Widget *callee = belongs(ev.pos.ker.x, ev.pos.ker.y);
printf("callee: %p\n", callee);
if (callee) {
// globax coords are updated at every draw()
ev.pos.ker.x -= callee->getGlobalX();
ev.pos.ker.y -= callee->getGlobalY();
callee->emitSignals(ev);
}
}
draw(*window, 0, 0);
}
}
void draw(sf::RenderWindow &w, size_t, size_t) override
{
setBackground(g::color::gray);
for (auto child : children) {
child->draw(w, 0, 0);
}
update();
}
};
}