UI: disallow splitting of temporary screens
[blender.git] / source / blender / editors / screen / screen_draw.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup edscr
19  */
20
21 #include "ED_screen.h"
22
23 #include "GPU_batch_presets.h"
24 #include "GPU_extensions.h"
25 #include "GPU_framebuffer.h"
26 #include "GPU_immediate.h"
27 #include "GPU_matrix.h"
28 #include "GPU_state.h"
29
30 #include "BLI_listbase.h"
31 #include "BLI_math.h"
32 #include "BLI_rect.h"
33
34 #include "WM_api.h"
35 #include "WM_types.h"
36
37 #include "UI_interface.h"
38 #include "UI_resources.h"
39
40 #include "screen_intern.h"
41
42 /**
43  * Draw horizontal shape visualizing future joining (left as well right direction of future joining).
44  */
45 static void draw_horizontal_join_shape(ScrArea *sa, char dir, unsigned int pos)
46 {
47         const float width = screen_geom_area_width(sa) - 1;
48         const float height = screen_geom_area_height(sa) - 1;
49         vec2f points[10];
50         short i;
51         float w, h;
52
53         if (height < width) {
54                 h = height / 8;
55                 w = height / 4;
56         }
57         else {
58                 h = width / 8;
59                 w = width / 4;
60         }
61
62         points[0].x = sa->v1->vec.x;
63         points[0].y = sa->v1->vec.y + height / 2;
64
65         points[1].x = sa->v1->vec.x;
66         points[1].y = sa->v1->vec.y;
67
68         points[2].x = sa->v4->vec.x - w;
69         points[2].y = sa->v4->vec.y;
70
71         points[3].x = sa->v4->vec.x - w;
72         points[3].y = sa->v4->vec.y + height / 2 - 2 * h;
73
74         points[4].x = sa->v4->vec.x - 2 * w;
75         points[4].y = sa->v4->vec.y + height / 2;
76
77         points[5].x = sa->v4->vec.x - w;
78         points[5].y = sa->v4->vec.y + height / 2 + 2 * h;
79
80         points[6].x = sa->v3->vec.x - w;
81         points[6].y = sa->v3->vec.y;
82
83         points[7].x = sa->v2->vec.x;
84         points[7].y = sa->v2->vec.y;
85
86         points[8].x = sa->v4->vec.x;
87         points[8].y = sa->v4->vec.y + height / 2 - h;
88
89         points[9].x = sa->v4->vec.x;
90         points[9].y = sa->v4->vec.y + height / 2 + h;
91
92         if (dir == 'l') {
93                 /* when direction is left, then we flip direction of arrow */
94                 float cx = sa->v1->vec.x + width;
95                 for (i = 0; i < 10; i++) {
96                         points[i].x -= cx;
97                         points[i].x = -points[i].x;
98                         points[i].x += sa->v1->vec.x;
99                 }
100         }
101
102         immBegin(GPU_PRIM_TRI_FAN, 5);
103
104         for (i = 0; i < 5; i++) {
105                 immVertex2f(pos, points[i].x, points[i].y);
106         }
107
108         immEnd();
109
110         immBegin(GPU_PRIM_TRI_FAN, 5);
111
112         for (i = 4; i < 8; i++) {
113                 immVertex2f(pos, points[i].x, points[i].y);
114         }
115
116         immVertex2f(pos, points[0].x, points[0].y);
117         immEnd();
118
119         immRectf(pos, points[2].x, points[2].y, points[8].x, points[8].y);
120         immRectf(pos, points[6].x, points[6].y, points[9].x, points[9].y);
121 }
122
123 /**
124  * Draw vertical shape visualizing future joining (up/down direction).
125  */
126 static void draw_vertical_join_shape(ScrArea *sa, char dir, unsigned int pos)
127 {
128         const float width = screen_geom_area_width(sa) - 1;
129         const float height = screen_geom_area_height(sa) - 1;
130         vec2f points[10];
131         short i;
132         float w, h;
133
134         if (height < width) {
135                 h = height / 4;
136                 w = height / 8;
137         }
138         else {
139                 h = width / 4;
140                 w = width / 8;
141         }
142
143         points[0].x = sa->v1->vec.x + width / 2;
144         points[0].y = sa->v3->vec.y;
145
146         points[1].x = sa->v2->vec.x;
147         points[1].y = sa->v2->vec.y;
148
149         points[2].x = sa->v1->vec.x;
150         points[2].y = sa->v1->vec.y + h;
151
152         points[3].x = sa->v1->vec.x + width / 2 - 2 * w;
153         points[3].y = sa->v1->vec.y + h;
154
155         points[4].x = sa->v1->vec.x + width / 2;
156         points[4].y = sa->v1->vec.y + 2 * h;
157
158         points[5].x = sa->v1->vec.x + width / 2 + 2 * w;
159         points[5].y = sa->v1->vec.y + h;
160
161         points[6].x = sa->v4->vec.x;
162         points[6].y = sa->v4->vec.y + h;
163
164         points[7].x = sa->v3->vec.x;
165         points[7].y = sa->v3->vec.y;
166
167         points[8].x = sa->v1->vec.x + width / 2 - w;
168         points[8].y = sa->v1->vec.y;
169
170         points[9].x = sa->v1->vec.x + width / 2 + w;
171         points[9].y = sa->v1->vec.y;
172
173         if (dir == 'u') {
174                 /* when direction is up, then we flip direction of arrow */
175                 float cy = sa->v1->vec.y + height;
176                 for (i = 0; i < 10; i++) {
177                         points[i].y -= cy;
178                         points[i].y = -points[i].y;
179                         points[i].y += sa->v1->vec.y;
180                 }
181         }
182
183         immBegin(GPU_PRIM_TRI_FAN, 5);
184
185         for (i = 0; i < 5; i++) {
186                 immVertex2f(pos, points[i].x, points[i].y);
187         }
188
189         immEnd();
190
191         immBegin(GPU_PRIM_TRI_FAN, 5);
192
193         for (i = 4; i < 8; i++) {
194                 immVertex2f(pos, points[i].x, points[i].y);
195         }
196
197         immVertex2f(pos, points[0].x, points[0].y);
198         immEnd();
199
200         immRectf(pos, points[2].x, points[2].y, points[8].x, points[8].y);
201         immRectf(pos, points[6].x, points[6].y, points[9].x, points[9].y);
202 }
203
204 /**
205  * Draw join shape due to direction of joining.
206  */
207 static void draw_join_shape(ScrArea *sa, char dir, unsigned int pos)
208 {
209         if (dir == 'u' || dir == 'd') {
210                 draw_vertical_join_shape(sa, dir, pos);
211         }
212         else {
213                 draw_horizontal_join_shape(sa, dir, pos);
214         }
215 }
216
217 #define CORNER_RESOLUTION 3
218
219 static void do_vert_pair(GPUVertBuf *vbo, uint pos, uint *vidx, int corner, int i)
220 {
221         float inter[2], exter[2];
222         inter[0] = cosf(corner * M_PI_2 + (i * M_PI_2 / (CORNER_RESOLUTION - 1.0f)));
223         inter[1] = sinf(corner * M_PI_2 + (i * M_PI_2 / (CORNER_RESOLUTION - 1.0f)));
224
225         /* Snap point to edge */
226         float div = 1.0f / max_ff(fabsf(inter[0]), fabsf(inter[1]));
227         mul_v2_v2fl(exter, inter, div);
228         exter[0] = roundf(exter[0]);
229         exter[1] = roundf(exter[1]);
230
231         if (i == 0 || i == (CORNER_RESOLUTION - 1)) {
232                 copy_v2_v2(inter, exter);
233         }
234
235         /* Line width is 20% of the entire corner size. */
236         const float line_width = 0.2f; /* Keep in sync with shader */
237         mul_v2_fl(inter, 1.0f - line_width);
238         mul_v2_fl(exter, 1.0f + line_width);
239
240         switch (corner) {
241                 case 0:
242                         add_v2_v2(inter, (float[2]){-1.0f, -1.0f});
243                         add_v2_v2(exter, (float[2]){-1.0f, -1.0f});
244                         break;
245                 case 1:
246                         add_v2_v2(inter, (float[2]){1.0f, -1.0f});
247                         add_v2_v2(exter, (float[2]){1.0f, -1.0f});
248                         break;
249                 case 2:
250                         add_v2_v2(inter, (float[2]){1.0f, 1.0f});
251                         add_v2_v2(exter, (float[2]){1.0f, 1.0f});
252                         break;
253                 case 3:
254                         add_v2_v2(inter, (float[2]){-1.0f, 1.0f});
255                         add_v2_v2(exter, (float[2]){-1.0f, 1.0f});
256                         break;
257         }
258
259         GPU_vertbuf_attr_set(vbo, pos, (*vidx)++, inter);
260         GPU_vertbuf_attr_set(vbo, pos, (*vidx)++, exter);
261 }
262
263 static GPUBatch *batch_screen_edges_get(int *corner_len)
264 {
265         static GPUBatch *screen_edges_batch = NULL;
266
267         if (screen_edges_batch == NULL) {
268                 GPUVertFormat format = {0};
269                 uint pos = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
270
271                 GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);
272                 GPU_vertbuf_data_alloc(vbo, CORNER_RESOLUTION * 2 * 4 + 2);
273
274                 uint vidx = 0;
275                 for (int corner = 0; corner < 4; ++corner) {
276                         for (int c = 0; c < CORNER_RESOLUTION; ++c) {
277                                 do_vert_pair(vbo, pos, &vidx, corner, c);
278                         }
279                 }
280                 /* close the loop */
281                 do_vert_pair(vbo, pos, &vidx, 0, 0);
282
283                 screen_edges_batch = GPU_batch_create_ex(GPU_PRIM_TRI_STRIP, vbo, NULL, GPU_BATCH_OWNS_VBO);
284                 gpu_batch_presets_register(screen_edges_batch);
285         }
286
287         if (corner_len) {
288                 *corner_len = CORNER_RESOLUTION * 2;
289         }
290         return screen_edges_batch;
291 }
292
293 #undef CORNER_RESOLUTION
294
295 /**
296  * Draw screen area darker with arrow (visualization of future joining).
297  */
298 static void scrarea_draw_shape_dark(ScrArea *sa, char dir, unsigned int pos)
299 {
300         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
301         immUniformColor4ub(0, 0, 0, 50);
302
303         draw_join_shape(sa, dir, pos);
304 }
305
306 /**
307  * Draw screen area lighter with arrow shape ("eraser" of previous dark shape).
308  */
309 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir), unsigned int pos)
310 {
311         GPU_blend_set_func(GPU_DST_COLOR, GPU_SRC_ALPHA);
312         /* value 181 was hardly computed: 181~105 */
313         immUniformColor4ub(255, 255, 255, 50);
314         /* draw_join_shape(sa, dir); */
315
316         immRectf(pos, sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
317 }
318
319 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, float edge_thickness)
320 {
321         rctf rect;
322         BLI_rctf_init(&rect, (float)x1, (float)x2, (float)y1, (float)y2);
323
324         /* right border area */
325         if (x2 >= sizex - 1) {
326                 rect.xmax += edge_thickness * 0.5f;
327         }
328
329         /* left border area */
330         if (x1 <= 0) { /* otherwise it draws the emboss of window over */
331                 rect.xmin -= edge_thickness * 0.5f;
332         }
333
334         /* top border area */
335         if (y2 >= sizey - 1) {
336                 rect.ymax += edge_thickness * 0.5f;
337         }
338
339         /* bottom border area */
340         if (y1 <= 0) {
341                 rect.ymin -= edge_thickness * 0.5f;
342         }
343
344         GPUBatch *batch = batch_screen_edges_get(NULL);
345         GPU_batch_uniform_4fv(batch, "rect", (float *)&rect);
346         GPU_batch_draw(batch);
347 }
348
349 /**
350  * \brief Screen edges drawing.
351  */
352 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, float edge_thickness)
353 {
354         short x1 = sa->v1->vec.x;
355         short y1 = sa->v1->vec.y;
356         short x2 = sa->v3->vec.x;
357         short y2 = sa->v3->vec.y;
358
359         drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, edge_thickness);
360 }
361
362 /**
363  * Only for edge lines between areas.
364  */
365 void ED_screen_draw_edges(wmWindow *win)
366 {
367         bScreen *screen = WM_window_get_active_screen(win);
368         screen->do_draw = false;
369
370         if (screen->state == SCREENFULL) {
371                 return;
372         }
373
374         if (screen->temp && BLI_listbase_is_single(&screen->areabase)) {
375                 return;
376         }
377
378         const int winsize_x = WM_window_pixels_x(win);
379         const int winsize_y = WM_window_pixels_y(win);
380         float col[4], corner_scale, edge_thickness;
381         int verts_per_corner = 0;
382
383         ScrArea *sa;
384
385         rcti scissor_rect;
386         BLI_rcti_init_minmax(&scissor_rect);
387         for (sa = screen->areabase.first; sa; sa = sa->next) {
388                 BLI_rcti_do_minmax_v(&scissor_rect, (int[2]){sa->v1->vec.x, sa->v1->vec.y});
389                 BLI_rcti_do_minmax_v(&scissor_rect, (int[2]){sa->v3->vec.x, sa->v3->vec.y});
390         }
391
392         if (GPU_type_matches(GPU_DEVICE_INTEL_UHD, GPU_OS_UNIX, GPU_DRIVER_ANY)) {
393                 /* For some reason, on linux + Intel UHD Graphics 620 the driver
394                  * hangs if we don't flush before this. (See T57455) */
395                 GPU_flush();
396         }
397
398         GPU_scissor(scissor_rect.xmin,
399                     scissor_rect.ymin,
400                     BLI_rcti_size_x(&scissor_rect) + 1,
401                     BLI_rcti_size_y(&scissor_rect) + 1);
402
403         /* It seems that all areas gets smaller when pixelsize is > 1.
404          * So in order to avoid missing pixels we just disable de scissors. */
405         if (U.pixelsize <= 1.0f) {
406                 glEnable(GL_SCISSOR_TEST);
407         }
408
409         UI_GetThemeColor4fv(TH_EDITOR_OUTLINE, col);
410         col[3] = 1.0f;
411         corner_scale = U.pixelsize * 8.0f;
412         edge_thickness = corner_scale * 0.21f;
413
414         GPU_blend(true);
415
416         GPUBatch *batch = batch_screen_edges_get(&verts_per_corner);
417         GPU_batch_program_set_builtin(batch, GPU_SHADER_2D_AREA_EDGES);
418         GPU_batch_uniform_1i(batch, "cornerLen", verts_per_corner);
419         GPU_batch_uniform_1f(batch, "scale", corner_scale);
420         GPU_batch_uniform_4fv(batch, "color", col);
421
422         for (sa = screen->areabase.first; sa; sa = sa->next) {
423                 drawscredge_area(sa, winsize_x, winsize_y, edge_thickness);
424         }
425
426         GPU_blend(false);
427
428         if (U.pixelsize <= 1.0f) {
429                 glDisable(GL_SCISSOR_TEST);
430         }
431 }
432
433 /**
434  * The blended join arrows.
435  *
436  * \param sa1: Area from which the resultant originates.
437  * \param sa2: Target area that will be replaced.
438  */
439 void ED_screen_draw_join_shape(ScrArea *sa1, ScrArea *sa2)
440 {
441         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
442         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
443
444         GPU_line_width(1);
445
446         /* blended join arrow */
447         int dir = area_getorientation(sa1, sa2);
448         int dira = -1;
449         if (dir != -1) {
450                 switch (dir) {
451                         case 0: /* W */
452                                 dir = 'r';
453                                 dira = 'l';
454                                 break;
455                         case 1: /* N */
456                                 dir = 'd';
457                                 dira = 'u';
458                                 break;
459                         case 2: /* E */
460                                 dir = 'l';
461                                 dira = 'r';
462                                 break;
463                         case 3: /* S */
464                                 dir = 'u';
465                                 dira = 'd';
466                                 break;
467                 }
468
469                 GPU_blend(true);
470
471                 scrarea_draw_shape_dark(sa2, dir, pos);
472                 scrarea_draw_shape_light(sa1, dira, pos);
473
474                 GPU_blend(false);
475         }
476
477         immUnbindProgram();
478 }
479
480 void ED_screen_draw_split_preview(ScrArea *sa, const int dir, const float fac)
481 {
482         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
483         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
484
485         /* splitpoint */
486         GPU_blend(true);
487         immUniformColor4ub(255, 255, 255, 100);
488
489         immBegin(GPU_PRIM_LINES, 2);
490
491         if (dir == 'h') {
492                 const float y = (1 - fac) * sa->totrct.ymin + fac * sa->totrct.ymax;
493
494                 immVertex2f(pos, sa->totrct.xmin, y);
495                 immVertex2f(pos, sa->totrct.xmax, y);
496
497                 immEnd();
498
499                 immUniformColor4ub(0, 0, 0, 100);
500
501                 immBegin(GPU_PRIM_LINES, 2);
502
503                 immVertex2f(pos, sa->totrct.xmin, y + 1);
504                 immVertex2f(pos, sa->totrct.xmax, y + 1);
505
506                 immEnd();
507         }
508         else {
509                 BLI_assert(dir == 'v');
510                 const float x = (1 - fac) * sa->totrct.xmin + fac * sa->totrct.xmax;
511
512                 immVertex2f(pos, x, sa->totrct.ymin);
513                 immVertex2f(pos, x, sa->totrct.ymax);
514
515                 immEnd();
516
517                 immUniformColor4ub(0, 0, 0, 100);
518
519                 immBegin(GPU_PRIM_LINES, 2);
520
521                 immVertex2f(pos, x + 1, sa->totrct.ymin);
522                 immVertex2f(pos, x + 1, sa->totrct.ymax);
523
524                 immEnd();
525         }
526
527         GPU_blend(false);
528
529         immUnbindProgram();
530 }
531
532
533 /* -------------------------------------------------------------------- */
534 /* Screen Thumbnail Preview */
535
536 /**
537  * Calculates a scale factor to squash the preview for \a screen into a rectangle of given size and aspect.
538  */
539 static void screen_preview_scale_get(
540         const bScreen *screen, float size_x, float size_y,
541         const float asp[2],
542         float r_scale[2])
543 {
544         float max_x = 0, max_y = 0;
545
546         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
547                 max_x = MAX2(max_x, sa->totrct.xmax);
548                 max_y = MAX2(max_y, sa->totrct.ymax);
549         }
550         r_scale[0] = (size_x * asp[0]) / max_x;
551         r_scale[1] = (size_y * asp[1]) / max_y;
552 }
553
554 static void screen_preview_draw_areas(const bScreen *screen, const float scale[2], const float col[4],
555                                       const float ofs_between_areas)
556 {
557         const float ofs_h = ofs_between_areas * 0.5f;
558         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
559
560         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
561         immUniformColor4fv(col);
562
563         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
564                 rctf rect = {
565                         .xmin = sa->totrct.xmin * scale[0] + ofs_h,
566                         .xmax = sa->totrct.xmax * scale[0] - ofs_h,
567                         .ymin = sa->totrct.ymin * scale[1] + ofs_h,
568                         .ymax = sa->totrct.ymax * scale[1] - ofs_h,
569                 };
570
571                 immBegin(GPU_PRIM_TRI_FAN, 4);
572                 immVertex2f(pos, rect.xmin, rect.ymin);
573                 immVertex2f(pos, rect.xmax, rect.ymin);
574                 immVertex2f(pos, rect.xmax, rect.ymax);
575                 immVertex2f(pos, rect.xmin, rect.ymax);
576                 immEnd();
577         }
578
579         immUnbindProgram();
580 }
581
582 static void screen_preview_draw(const bScreen *screen, int size_x, int size_y)
583 {
584         const float asp[2] = {1.0f, 0.8f}; /* square previews look a bit ugly */
585         /* could use theme color (tui.wcol_menu_item.text), but then we'd need to regenerate all previews when changing */
586         const float col[4] = {1.0f, 1.0f, 1.0f, 1.0f};
587         float scale[2];
588
589         wmOrtho2(0.0f, size_x, 0.0f, size_y);
590         /* center */
591         GPU_matrix_push();
592         GPU_matrix_identity_set();
593         GPU_matrix_translate_2f(size_x * (1.0f - asp[0]) * 0.5f, size_y * (1.0f - asp[1]) * 0.5f);
594
595         screen_preview_scale_get(screen, size_x, size_y, asp, scale);
596         screen_preview_draw_areas(screen, scale, col, 1.5f);
597
598         GPU_matrix_pop();
599 }
600
601 /**
602  * Render the preview for a screen layout in \a screen.
603  */
604 void ED_screen_preview_render(const bScreen *screen, int size_x, int size_y, unsigned int *r_rect)
605 {
606         char err_out[256] = "unknown";
607         GPUOffScreen *offscreen = GPU_offscreen_create(size_x, size_y, 0, true, false, err_out);
608
609         GPU_offscreen_bind(offscreen, true);
610         GPU_clear_color(0.0, 0.0, 0.0, 0.0);
611         GPU_clear(GPU_COLOR_BIT | GPU_DEPTH_BIT);
612
613         screen_preview_draw(screen, size_x, size_y);
614
615         GPU_offscreen_read_pixels(offscreen, GL_UNSIGNED_BYTE, r_rect);
616         GPU_offscreen_unbind(offscreen, true);
617
618         GPU_offscreen_free(offscreen);
619 }