Cleanup: Move area geometry management into an own file
[blender.git] / source / blender / editors / screen / screen_draw.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/editors/screen/screen_draw.c
22  *  \ingroup edscr
23  */
24
25 #include "ED_screen.h"
26
27 #include "GPU_framebuffer.h"
28 #include "GPU_immediate.h"
29 #include "GPU_matrix.h"
30 #include "GPU_state.h"
31
32 #include "BLI_math.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(GWN_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(GWN_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(GWN_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(GWN_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 10
218 static void drawscredge_corner_geometry(
219         int sizex, int sizey,
220         int corner_x, int corner_y,
221         int center_x, int center_y,
222         double angle_offset,
223         const float *color)
224 {
225         const int radius = ABS(corner_x - center_x);
226         const int line_thickness = U.pixelsize;
227
228         if (corner_x < center_x) {
229                 if (corner_x > 0.0f) {
230                         /* Left (internal) edge. */
231                         corner_x += line_thickness;
232                         center_x += line_thickness;
233                 }
234         }
235         else {
236                 /* Right (internal) edge. */
237                 if (corner_x < sizex - 1) {
238                         corner_x += 1 - line_thickness;
239                         center_x += 1 - line_thickness;
240                 }
241                 else {
242                         /* Corner case, extreme right edge. */
243                         corner_x += 1;
244                         center_x += 1;
245                 }
246         }
247
248         if (corner_y < center_y) {
249                 if (corner_y > 0.0f) {
250                         /* Bottom (internal) edge. */
251                         corner_y += line_thickness;
252                         center_y += line_thickness;
253                 }
254         }
255         else {
256                 /* Top (internal) edge. */
257                 if (corner_y < sizey) {
258                         corner_y += 1 - line_thickness;
259                         center_y += 1 - line_thickness;
260                 }
261         }
262
263         float tri_array[CORNER_RESOLUTION + 1][2];
264
265         tri_array[0][0] = corner_x;
266         tri_array[0][1] = corner_y;
267
268         for (int i = 0; i < CORNER_RESOLUTION; i++) {
269                 double angle = angle_offset + (M_PI_2 * ((float)i / (CORNER_RESOLUTION - 1)));
270                 float x = center_x + (radius * cos(angle));
271                 float y = center_y + (radius * sin(angle));
272                 tri_array[i + 1][0] = x;
273                 tri_array[i + 1][1] = y;
274         }
275
276         UI_draw_anti_fan(tri_array, CORNER_RESOLUTION + 1, color);
277 }
278
279 #undef CORNER_RESOLUTION
280
281 static void drawscredge_corner(ScrArea *sa, int sizex, int sizey)
282 {
283         int size = 10 * U.pixelsize;
284         float color[4] = {0};
285         UI_GetThemeColor4fv(TH_EDITOR_OUTLINE, color);
286
287         /* Bottom-Left. */
288         drawscredge_corner_geometry(sizex, sizey,
289                                     sa->v1->vec.x,
290                                     sa->v1->vec.y,
291                                     sa->v1->vec.x + size,
292                                     sa->v1->vec.y + size,
293                                     M_PI_2 * 2.0f,
294                                     color);
295
296         /* Top-Left. */
297         drawscredge_corner_geometry(sizex, sizey,
298                                     sa->v2->vec.x,
299                                     sa->v2->vec.y,
300                                     sa->v2->vec.x + size,
301                                     sa->v2->vec.y - size,
302                                     M_PI_2,
303                                     color);
304
305         /* Top-Right. */
306         drawscredge_corner_geometry(sizex, sizey,
307                                     sa->v3->vec.x,
308                                     sa->v3->vec.y,
309                                     sa->v3->vec.x - size,
310                                     sa->v3->vec.y - size,
311                                     0.0f,
312                                     color);
313
314         /* Bottom-Right. */
315         drawscredge_corner_geometry(sizex, sizey,
316                                     sa->v4->vec.x,
317                                     sa->v4->vec.y,
318                                     sa->v4->vec.x - size,
319                                     sa->v4->vec.y + size,
320                                     M_PI_2 * 3.0f,
321                                     color);
322
323         /* Wrap up the corners with a nice embossing. */
324         rcti rect = sa->totrct;
325
326         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
327         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
328
329         immUniformColor4fv(color);
330         immBeginAtMost(GWN_PRIM_LINES, 8);
331
332         /* Right. */
333         immVertex2f(pos, rect.xmax, rect.ymax);
334         immVertex2f(pos, rect.xmax, rect.ymin);
335
336         /* Bottom. */
337         immVertex2f(pos, rect.xmax, rect.ymin);
338         immVertex2f(pos, rect.xmin, rect.ymin);
339
340         /* Left. */
341         immVertex2f(pos, rect.xmin, rect.ymin);
342         immVertex2f(pos, rect.xmin, rect.ymax);
343
344         /* Top. */
345         immVertex2f(pos, rect.xmin, rect.ymax);
346         immVertex2f(pos, rect.xmax, rect.ymax);
347
348         immEnd();
349         immUnbindProgram();
350 }
351
352 /**
353  * Draw screen area darker with arrow (visualization of future joining).
354  */
355 static void scrarea_draw_shape_dark(ScrArea *sa, char dir, unsigned int pos)
356 {
357         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
358         immUniformColor4ub(0, 0, 0, 50);
359
360         draw_join_shape(sa, dir, pos);
361 }
362
363 /**
364  * Draw screen area ligher with arrow shape ("eraser" of previous dark shape).
365  */
366 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir), unsigned int pos)
367 {
368         GPU_blend_set_func(GPU_DST_COLOR, GPU_SRC_ALPHA);
369         /* value 181 was hardly computed: 181~105 */
370         immUniformColor4ub(255, 255, 255, 50);
371         /* draw_join_shape(sa, dir); */
372
373         immRectf(pos, sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
374 }
375
376 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, unsigned int pos)
377 {
378         int count = 0;
379
380         if (x2 < sizex - 1) count += 2;
381         if (x1 > 0) count += 2;
382         if (y2 < sizey - 1) count += 2;
383         if (y1 > 0) count += 2;
384
385         if (count == 0) {
386                 return;
387         }
388
389         immBegin(GWN_PRIM_LINES, count);
390
391         /* right border area */
392         if (x2 < sizex - 1) {
393                 immVertex2f(pos, x2, y1);
394                 immVertex2f(pos, x2, y2);
395         }
396
397         /* left border area */
398         if (x1 > 0) { /* otherwise it draws the emboss of window over */
399                 immVertex2f(pos, x1, y1);
400                 immVertex2f(pos, x1, y2);
401         }
402
403         /* top border area */
404         if (y2 < sizey - 1) {
405                 immVertex2f(pos, x1, y2);
406                 immVertex2f(pos, x2, y2);
407         }
408
409         /* bottom border area */
410         if (y1 > 0) {
411                 immVertex2f(pos, x1, y1);
412                 immVertex2f(pos, x2, y1);
413         }
414
415         immEnd();
416 }
417
418 /**
419  * \brief Screen edges drawing.
420  */
421 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, unsigned int pos)
422 {
423         short x1 = sa->v1->vec.x;
424         short y1 = sa->v1->vec.y;
425         short x2 = sa->v3->vec.x;
426         short y2 = sa->v3->vec.y;
427
428         drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, pos);
429 }
430
431 /**
432  * Only for edge lines between areas.
433  */
434 void ED_screen_draw_edges(wmWindow *win)
435 {
436         bScreen *screen = WM_window_get_active_screen(win);
437         const int winsize_x = WM_window_pixels_x(win);
438         const int winsize_y = WM_window_pixels_y(win);
439
440         ScrArea *sa;
441
442         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
443         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
444
445         /* Note: first loop only draws if U.pixelsize > 1, skip otherwise */
446         if (U.pixelsize > 1.0f) {
447                 /* FIXME: doesn't our glLineWidth already scale by U.pixelsize? */
448                 GPU_line_width((2.0f * U.pixelsize) - 1);
449                 immUniformThemeColor(TH_EDITOR_OUTLINE);
450
451                 for (sa = screen->areabase.first; sa; sa = sa->next) {
452                         drawscredge_area(sa, winsize_x, winsize_y, pos);
453                 }
454         }
455
456         GPU_line_width(1);
457         immUniformThemeColor(TH_EDITOR_OUTLINE);
458
459         for (sa = screen->areabase.first; sa; sa = sa->next) {
460                 drawscredge_area(sa, winsize_x, winsize_y, pos);
461         }
462
463         immUnbindProgram();
464
465         for (sa = screen->areabase.first; sa; sa = sa->next) {
466                 drawscredge_corner(sa, winsize_x, winsize_y);
467         }
468
469         screen->do_draw = false;
470 }
471
472 /**
473  * The blended join arrows.
474  *
475  * \param sa1: Area from which the resultant originates.
476  * \param sa2: Target area that will be replaced.
477  */
478 void ED_screen_draw_join_shape(ScrArea *sa1, ScrArea *sa2)
479 {
480         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
481         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
482
483         GPU_line_width(1);
484
485         /* blended join arrow */
486         int dir = area_getorientation(sa1, sa2);
487         int dira = -1;
488         if (dir != -1) {
489                 switch (dir) {
490                         case 0: /* W */
491                                 dir = 'r';
492                                 dira = 'l';
493                                 break;
494                         case 1: /* N */
495                                 dir = 'd';
496                                 dira = 'u';
497                                 break;
498                         case 2: /* E */
499                                 dir = 'l';
500                                 dira = 'r';
501                                 break;
502                         case 3: /* S */
503                                 dir = 'u';
504                                 dira = 'd';
505                                 break;
506                 }
507
508                 GPU_blend(true);
509
510                 scrarea_draw_shape_dark(sa2, dir, pos);
511                 scrarea_draw_shape_light(sa1, dira, pos);
512
513                 GPU_blend(false);
514         }
515
516         immUnbindProgram();
517 }
518
519 void ED_screen_draw_split_preview(ScrArea *sa, const int dir, const float fac)
520 {
521         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
522         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
523
524         /* splitpoint */
525         GPU_blend(true);
526         immUniformColor4ub(255, 255, 255, 100);
527
528         immBegin(GWN_PRIM_LINES, 2);
529
530         if (dir == 'h') {
531                 const float y = (1 - fac) * sa->totrct.ymin + fac * sa->totrct.ymax;
532
533                 immVertex2f(pos, sa->totrct.xmin, y);
534                 immVertex2f(pos, sa->totrct.xmax, y);
535
536                 immEnd();
537
538                 immUniformColor4ub(0, 0, 0, 100);
539
540                 immBegin(GWN_PRIM_LINES, 2);
541
542                 immVertex2f(pos, sa->totrct.xmin, y + 1);
543                 immVertex2f(pos, sa->totrct.xmax, y + 1);
544
545                 immEnd();
546         }
547         else {
548                 BLI_assert(dir == 'v');
549                 const float x = (1 - fac) * sa->totrct.xmin + fac * sa->totrct.xmax;
550
551                 immVertex2f(pos, x, sa->totrct.ymin);
552                 immVertex2f(pos, x, sa->totrct.ymax);
553
554                 immEnd();
555
556                 immUniformColor4ub(0, 0, 0, 100);
557
558                 immBegin(GWN_PRIM_LINES, 2);
559
560                 immVertex2f(pos, x + 1, sa->totrct.ymin);
561                 immVertex2f(pos, x + 1, sa->totrct.ymax);
562
563                 immEnd();
564         }
565
566         GPU_blend(false);
567
568         immUnbindProgram();
569 }
570
571
572 /* -------------------------------------------------------------------- */
573 /* Screen Thumbnail Preview */
574
575 /**
576  * Calculates a scale factor to squash the preview for \a screen into a rectangle of given size and aspect.
577  */
578 static void screen_preview_scale_get(
579         const bScreen *screen, float size_x, float size_y,
580         const float asp[2],
581         float r_scale[2])
582 {
583         float max_x = 0, max_y = 0;
584
585         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
586                 max_x = MAX2(max_x, sa->totrct.xmax);
587                 max_y = MAX2(max_y, sa->totrct.ymax);
588         }
589         r_scale[0] = (size_x * asp[0]) / max_x;
590         r_scale[1] = (size_y * asp[1]) / max_y;
591 }
592
593 static void screen_preview_draw_areas(const bScreen *screen, const float scale[2], const float col[4],
594                                       const float ofs_between_areas)
595 {
596         const float ofs_h = ofs_between_areas * 0.5f;
597         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
598
599         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
600         immUniformColor4fv(col);
601
602         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
603                 rctf rect = {
604                         .xmin = sa->totrct.xmin * scale[0] + ofs_h,
605                         .xmax = sa->totrct.xmax * scale[0] - ofs_h,
606                         .ymin = sa->totrct.ymin * scale[1] + ofs_h,
607                         .ymax = sa->totrct.ymax * scale[1] - ofs_h
608                 };
609
610                 immBegin(GWN_PRIM_TRI_FAN, 4);
611                 immVertex2f(pos, rect.xmin, rect.ymin);
612                 immVertex2f(pos, rect.xmax, rect.ymin);
613                 immVertex2f(pos, rect.xmax, rect.ymax);
614                 immVertex2f(pos, rect.xmin, rect.ymax);
615                 immEnd();
616         }
617
618         immUnbindProgram();
619 }
620
621 static void screen_preview_draw(const bScreen *screen, int size_x, int size_y)
622 {
623         const float asp[2] = {1.0f, 0.8f}; /* square previews look a bit ugly */
624         /* could use theme color (tui.wcol_menu_item.text), but then we'd need to regenerate all previews when changing */
625         const float col[4] = {1.0f, 1.0f, 1.0f, 1.0f};
626         float scale[2];
627
628         wmOrtho2(0.0f, size_x, 0.0f, size_y);
629         /* center */
630         gpuPushMatrix();
631         gpuLoadIdentity();
632         gpuTranslate2f(size_x * (1.0f - asp[0]) * 0.5f, size_y * (1.0f - asp[1]) * 0.5f);
633
634         screen_preview_scale_get(screen, size_x, size_y, asp, scale);
635         screen_preview_draw_areas(screen, scale, col, 1.5f);
636
637         gpuPopMatrix();
638 }
639
640 /**
641  * Render the preview for a screen layout in \a screen.
642  */
643 void ED_screen_preview_render(const bScreen *screen, int size_x, int size_y, unsigned int *r_rect)
644 {
645         char err_out[256] = "unknown";
646         GPUOffScreen *offscreen = GPU_offscreen_create(size_x, size_y, 0, true, false, err_out);
647
648         GPU_offscreen_bind(offscreen, true);
649         GPU_clear_color(0.0, 0.0, 0.0, 0.0);
650         GPU_clear(GPU_COLOR_BIT | GPU_DEPTH_BIT);
651
652         screen_preview_draw(screen, size_x, size_y);
653
654         GPU_offscreen_read_pixels(offscreen, GL_UNSIGNED_BYTE, r_rect);
655         GPU_offscreen_unbind(offscreen, true);
656
657         GPU_offscreen_free(offscreen);
658 }