-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpanel_holder.cpp
More file actions
398 lines (332 loc) · 9.55 KB
/
Copy pathpanel_holder.cpp
File metadata and controls
398 lines (332 loc) · 9.55 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "panel_holder.h"
#include "application.h"
#include "asteroids.h"
#include "chat.h"
#include "compare.h"
#include "editor.h"
#include "lspdebug.h"
#include "mathwindow.h"
#include "graphwindow.h"
#include "terminalwidgettabbed.h"
#include "widgetchooser.h"
#include "settings.h"
#include "filetree.h"
#include "text_renderer.h"
PanelHolder::PanelHolder(Widget *parent) : Widget(parent) {
id = icu::UnicodeString::fromUTF8("Panel holder");
}
void PanelHolder::position(int x, int y, int width, int height) {
t_x = x;
t_y = y;
t_w = width;
t_h = height;
handle_short = TextRenderer::get_text_width(1)*0.4;
if (handle_short & 1) {
handle_short += 1;
}
hastwo = (children.size() == 2);
if (hastwo) {
if (is_horizontal) {
c1_x = x;
c1_w = width*ratio-width_h;
c2_x = x + c1_w + width_h * 2;
c2_w = width - c1_w - width_h * 2;
c1_y = c2_y = y;
c1_h = c2_h = height;
}else{
c1_y = y;
c1_h = height * ratio - height_h;
c2_y = y + c1_h + height_h * 2;
c2_h = height - c1_h - height_h * 2;
c1_x = c2_x = x;
c1_w = c2_w = width;
}
children[0]->position(c1_x, c1_y, c1_w, c1_h);
children[1]->position(c2_x, c2_y, c2_w, c2_h);
}else{
Widget::position(t_x, t_y, t_w, t_h);
}
int mx = App::mouseX;
int my = App::mouseY;
has_one_to_add = false;
has_one_to_rem = false;
first_hovered = mx > c1_x && mx < c1_x+c1_w && my > c1_y && my < c1_y+c1_h;
second_hovered = mx > c2_x && mx < c2_x+c2_w && my > c2_y && my < c2_y+c2_h;
bool updating_pos = false;
if (cursor_in_this){
if (!hastwo && App::curr_adding_panel) {
has_one_to_add = true;
adding_first_pos = false;
int horz_dst = mx-(t_x+t_w/2);
int vert_dst = my-(t_y+t_h/2);
bool horz = abs(horz_dst) > abs(vert_dst);
is_horizontal = horz;
if (horz && horz_dst < 0) {
x_nb = t_x;
w_nb = t_w/2;
y_nb = t_y;
h_nb = t_h;
adding_first_pos = true;
}else if (horz) {
x_nb = t_x+t_w/2;
w_nb = t_w/2;
y_nb = t_y;
h_nb = t_h;
}else if (vert_dst < 0) {
x_nb = t_x;
w_nb = t_w;
y_nb = t_y;
h_nb = t_h/2;
adding_first_pos = true;
}else {
x_nb = t_x;
w_nb = t_w;
y_nb = t_y+t_h/2;
h_nb = t_h/2;
}
updating_pos = true;
}
if (hastwo && App::curr_removing_panel) {
if (first_hovered) {
if (children[0]->children.size() == 1) {
x_nb = c1_x;
y_nb = c1_y;
w_nb = c1_w;
h_nb = c1_h;
has_one_to_rem = true;
updating_pos = true;
}
}else if (second_hovered) {
if (children[1]->children.size() == 1) {
x_nb = c2_x;
y_nb = c2_y;
w_nb = c2_w;
h_nb = c2_h;
has_one_to_rem = true;
updating_pos = true;
}
}
}
}
if (hastwo) {
if (is_horizontal) {
xpos_h = t_x + t_w*ratio - handle_short/2;
ypos_h = t_y + handle_short/2;
width_h = handle_short;
height_h = t_h - handle_short;
}else{
xpos_h = t_x + handle_short/2;
ypos_h = t_y + t_h*ratio - handle_short/2;
width_h = t_w - handle_short;
height_h = handle_short;
}
if (dragging_handle || hoveringHandle()) {
if (is_horizontal) {
App::expectedCursorType = 1;
}else{
App::expectedCursorType = 2;
}
}
}
if (updating_pos) {
// time to move the current positioning closer to the new locations over frames
App::x_nb_current = (App::x_nb_current*2+x_nb)/3;
App::y_nb_current = (App::y_nb_current*2+y_nb)/3;
App::w_nb_current = (App::w_nb_current*2+w_nb)/3;
App::h_nb_current = (App::h_nb_current*2+h_nb)/3;
if (std::abs(App::x_nb_current - x_nb) < 5) {
App::x_nb_current = x_nb;
}
if (std::abs(App::y_nb_current - y_nb) < 5) {
App::y_nb_current = y_nb;
}
if (std::abs(App::w_nb_current - w_nb) < 5) {
App::w_nb_current = w_nb;
}
if (std::abs(App::h_nb_current - h_nb) < 5) {
App::h_nb_current = h_nb;
}
App::time_till_regular = 2;
}
App::rendering_add_rect = App::rendering_add_rect || has_one_to_add;
App::rendering_rem_rect = App::rendering_rem_rect || has_one_to_rem;
}
void PanelHolder::addWidget(bool horz, bool frst, Widget* new_wid) {
is_horizontal = horz;
auto existing_widget = children[0];
auto p1_c = new PanelHolder(this);
auto p2_c = new PanelHolder(this);
if (frst){
App::MoveWidget(existing_widget, p2_c);
App::MoveWidget(new_wid, p1_c);
}else{
App::MoveWidget(existing_widget, p1_c);
App::MoveWidget(new_wid, p2_c);
}
ratio = 0.5f;
App::nada_panel();
}
bool PanelHolder::on_mouse_button_event(int button, int action, int mods) {
if (has_one_to_add && button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
addWidget(is_horizontal, adding_first_pos, new WidgetChooser(nullptr));
return true;
}
if (has_one_to_rem && button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
if (first_hovered) {
bool gonnadeletethis = false;
Widget* w1 = children[0];
children.erase(children.begin());
if (auto sub = dynamic_cast<PanelHolder*>(children[0])) {
int our_indx = App::GetWidgetIndexInParent(this);
App::MoveWidget(sub, parent);
App::RemoveWidgetFromParent(this);
if (our_indx == 0) {
std::swap(sub->parent->children[0], sub->parent->children[1]);
}
gonnadeletethis = true;
}
App::deleteWidget(w1);
if (gonnadeletethis) {
App::deleteWidget(this);
}
}else {
bool gonnadeletethis = false;
Widget* w2 = children[1];
children.erase(children.begin()+1);
if (auto sub = dynamic_cast<PanelHolder*>(children[0])) {
int our_indx = App::GetWidgetIndexInParent(this);
App::MoveWidget(sub, parent);
App::RemoveWidgetFromParent(this);
if (our_indx == 0) {
std::swap(sub->parent->children[0], sub->parent->children[1]);
}
gonnadeletethis = true;
}
App::deleteWidget(w2);
if (gonnadeletethis) {
App::deleteWidget(this);
}
}
App::nada_panel();
return true;
}
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS && hastwo) {
if (hoveringHandle()){
dragging_handle = true;
return true;
}
}else if (dragging_handle && button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
dragging_handle = false;
}
for (auto child : children) {
if (child->on_mouse_button_event(button, action, mods)) {
return true;
}
}
return false;
}
bool PanelHolder::hoveringHandle() {
int mx = App::mouseX;
int my = App::mouseY;
return mx > xpos_h - handle_short && mx < xpos_h + width_h + handle_short && my > ypos_h - handle_short && my < ypos_h + height_h + handle_short;
};
bool PanelHolder::on_mouse_move_event() {
if (dragging_handle) {
if (is_horizontal) {
ratio = (App::mouseX-t_x)/(float)t_w;
}else{
ratio = (App::mouseY-t_y)/(float)t_h;
}
if (ratio < 0.025) {
ratio = 0.025;
}else if (ratio > 0.975) {
ratio = 0.975;
}
return true;
}
Widget::on_mouse_move_event();
return false;
}
void PanelHolder::render() {
Widget::render();
// draw the handles
if (hastwo) {
if (is_horizontal) {
App::DrawRect(c1_x+c1_w, t_y, width_h*2, t_h, App::theme.main_background_color);
}else{
App::DrawRect(t_x, c1_y+c1_h, t_w, height_h*2, App::theme.main_background_color);
}
if (dragging_handle || hoveringHandle()) {
int rad = std::min(width_h, height_h)/2;
App::DrawRoundedRect(xpos_h, ypos_h, width_h, height_h, rad, App::theme.main_text_color);
}
}
}
void PanelHolder::setState(nlohmann::json state) {
for (auto c : state["children"]) {
if (c.contains("children")) {
auto ph = new PanelHolder(this);
ph->setState(c);
}else if (c == "FileTree"){
new FileTree(this);
}else if (c == "Compare"){
new Compare(this, [&](Widget* w){ return; });
}else if (c == "Settings"){
new Settings(this);
}else if (c == "WidgetChooser"){
new WidgetChooser(this);
}else if (c == "Chat"){
new Chat(this);
}else if (c == "LspDebug"){
new LspDebug(this);
}else if (c == "Terminal"){
new TerminalWidgetTabbed(this);
}else if (c == "MathWindow"){
new MathWindow(this);
}else if (c == "Asteroids"){
new Asteroids(this);
}else if (c == "GraphWindow"){
new GraphWindow(this);
}else { // any unknown, or editor
new Editor(this);
}
}
ratio = state["ratio"];
is_horizontal = state["horizontal"];
}
nlohmann::json PanelHolder::saveConfiguration() {
nlohmann::json thisitm = nlohmann::json::object();
for (int i = 0; i < children.size(); i++) {
auto c = children[i];
if (auto pe = dynamic_cast<PanelHolder*>(c)){
thisitm["children"][i] = pe->saveConfiguration();
}else if (auto pe = dynamic_cast<Editor*>(c)){
thisitm["children"][i] = "Editor";
}else if (auto pe = dynamic_cast<FileTree*>(c)){
thisitm["children"][i] = "FileTree";
}else if (auto pe = dynamic_cast<Settings*>(c)){
thisitm["children"][i] = "Settings";
}else if (auto pe = dynamic_cast<WidgetChooser*>(c)){
thisitm["children"][i] = "WidgetChooser";
}else if (auto pe = dynamic_cast<Chat*>(c)){
thisitm["children"][i] = "Chat";
}else if (auto pe = dynamic_cast<Compare*>(c)){
thisitm["children"][i] = "Compare";
}else if (auto pe = dynamic_cast<LspDebug*>(c)){
thisitm["children"][i] = "LspDebug";
}else if (auto pe = dynamic_cast<TerminalWidgetTabbed*>(c)){
thisitm["children"][i] = "Terminal";
}else if (auto pe = dynamic_cast<MathWindow*>(c)){
thisitm["children"][i] = "MathWindow";
}else if (auto pe = dynamic_cast<Asteroids*>(c)){
thisitm["children"][i] = "Asteroids";
}else if (auto pe = dynamic_cast<GraphWindow*>(c)){
thisitm["children"][i] = "GraphWindow";
}else {
thisitm["children"][i] = "Ehhhhh";
}
thisitm["ratio"] = ratio;
thisitm["horizontal"] = is_horizontal;
}
return thisitm;
}