Merge branch 'master' into blender2.8
[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
31 #include "WM_api.h"
32 #include "WM_types.h"
33
34 #include "screen_intern.h"
35
36 /**
37  * Draw horizontal shape visualizing future joining (left as well right direction of future joining).
38  */
39 static void draw_horizontal_join_shape(ScrArea *sa, char dir, unsigned int pos)
40 {
41         vec2f points[10];
42         short i;
43         float w, h;
44         float width = sa->v3->vec.x - sa->v1->vec.x;
45         float height = sa->v3->vec.y - sa->v1->vec.y;
46
47         if (height < width) {
48                 h = height / 8;
49                 w = height / 4;
50         }
51         else {
52                 h = width / 8;
53                 w = width / 4;
54         }
55
56         points[0].x = sa->v1->vec.x;
57         points[0].y = sa->v1->vec.y + height / 2;
58
59         points[1].x = sa->v1->vec.x;
60         points[1].y = sa->v1->vec.y;
61
62         points[2].x = sa->v4->vec.x - w;
63         points[2].y = sa->v4->vec.y;
64
65         points[3].x = sa->v4->vec.x - w;
66         points[3].y = sa->v4->vec.y + height / 2 - 2 * h;
67
68         points[4].x = sa->v4->vec.x - 2 * w;
69         points[4].y = sa->v4->vec.y + height / 2;
70
71         points[5].x = sa->v4->vec.x - w;
72         points[5].y = sa->v4->vec.y + height / 2 + 2 * h;
73
74         points[6].x = sa->v3->vec.x - w;
75         points[6].y = sa->v3->vec.y;
76
77         points[7].x = sa->v2->vec.x;
78         points[7].y = sa->v2->vec.y;
79
80         points[8].x = sa->v4->vec.x;
81         points[8].y = sa->v4->vec.y + height / 2 - h;
82
83         points[9].x = sa->v4->vec.x;
84         points[9].y = sa->v4->vec.y + height / 2 + h;
85
86         if (dir == 'l') {
87                 /* when direction is left, then we flip direction of arrow */
88                 float cx = sa->v1->vec.x + width;
89                 for (i = 0; i < 10; i++) {
90                         points[i].x -= cx;
91                         points[i].x = -points[i].x;
92                         points[i].x += sa->v1->vec.x;
93                 }
94         }
95
96         immBegin(GWN_PRIM_TRI_FAN, 5);
97
98         for (i = 0; i < 5; i++) {
99                 immVertex2f(pos, points[i].x, points[i].y);
100         }
101
102         immEnd();
103
104         immBegin(GWN_PRIM_TRI_FAN, 5);
105
106         for (i = 4; i < 8; i++) {
107                 immVertex2f(pos, points[i].x, points[i].y);
108         }
109
110         immVertex2f(pos, points[0].x, points[0].y);
111         immEnd();
112
113         immRectf(pos, points[2].x, points[2].y, points[8].x, points[8].y);
114         immRectf(pos, points[6].x, points[6].y, points[9].x, points[9].y);
115 }
116
117 /**
118  * Draw vertical shape visualizing future joining (up/down direction).
119  */
120 static void draw_vertical_join_shape(ScrArea *sa, char dir, unsigned int pos)
121 {
122         vec2f points[10];
123         short i;
124         float w, h;
125         float width = sa->v3->vec.x - sa->v1->vec.x;
126         float height = sa->v3->vec.y - sa->v1->vec.y;
127
128         if (height < width) {
129                 h = height / 4;
130                 w = height / 8;
131         }
132         else {
133                 h = width / 4;
134                 w = width / 8;
135         }
136
137         points[0].x = sa->v1->vec.x + width / 2;
138         points[0].y = sa->v3->vec.y;
139
140         points[1].x = sa->v2->vec.x;
141         points[1].y = sa->v2->vec.y;
142
143         points[2].x = sa->v1->vec.x;
144         points[2].y = sa->v1->vec.y + h;
145
146         points[3].x = sa->v1->vec.x + width / 2 - 2 * w;
147         points[3].y = sa->v1->vec.y + h;
148
149         points[4].x = sa->v1->vec.x + width / 2;
150         points[4].y = sa->v1->vec.y + 2 * h;
151
152         points[5].x = sa->v1->vec.x + width / 2 + 2 * w;
153         points[5].y = sa->v1->vec.y + h;
154
155         points[6].x = sa->v4->vec.x;
156         points[6].y = sa->v4->vec.y + h;
157
158         points[7].x = sa->v3->vec.x;
159         points[7].y = sa->v3->vec.y;
160
161         points[8].x = sa->v1->vec.x + width / 2 - w;
162         points[8].y = sa->v1->vec.y;
163
164         points[9].x = sa->v1->vec.x + width / 2 + w;
165         points[9].y = sa->v1->vec.y;
166
167         if (dir == 'u') {
168                 /* when direction is up, then we flip direction of arrow */
169                 float cy = sa->v1->vec.y + height;
170                 for (i = 0; i < 10; i++) {
171                         points[i].y -= cy;
172                         points[i].y = -points[i].y;
173                         points[i].y += sa->v1->vec.y;
174                 }
175         }
176
177         immBegin(GWN_PRIM_TRI_FAN, 5);
178
179         for (i = 0; i < 5; i++) {
180                 immVertex2f(pos, points[i].x, points[i].y);
181         }
182
183         immEnd();
184
185         immBegin(GWN_PRIM_TRI_FAN, 5);
186
187         for (i = 4; i < 8; i++) {
188                 immVertex2f(pos, points[i].x, points[i].y);
189         }
190
191         immVertex2f(pos, points[0].x, points[0].y);
192         immEnd();
193
194         immRectf(pos, points[2].x, points[2].y, points[8].x, points[8].y);
195         immRectf(pos, points[6].x, points[6].y, points[9].x, points[9].y);
196 }
197
198 /**
199  * Draw join shape due to direction of joining.
200  */
201 static void draw_join_shape(ScrArea *sa, char dir, unsigned int pos)
202 {
203         if (dir == 'u' || dir == 'd') {
204                 draw_vertical_join_shape(sa, dir, pos);
205         }
206         else {
207                 draw_horizontal_join_shape(sa, dir, pos);
208         }
209 }
210
211 /**
212  * Draw screen area darker with arrow (visualization of future joining).
213  */
214 static void scrarea_draw_shape_dark(ScrArea *sa, char dir, unsigned int pos)
215 {
216         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
217         immUniformColor4ub(0, 0, 0, 50);
218
219         draw_join_shape(sa, dir, pos);
220 }
221
222 /**
223  * Draw screen area ligher with arrow shape ("eraser" of previous dark shape).
224  */
225 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir), unsigned int pos)
226 {
227         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
228         /* value 181 was hardly computed: 181~105 */
229         immUniformColor4ub(255, 255, 255, 50);
230         /* draw_join_shape(sa, dir); */
231
232         immRectf(pos, sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
233 }
234
235 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2, unsigned int pos)
236 {
237         int count = 0;
238
239         if (x2 < sizex - 1) count += 2;
240         if (x1 > 0) count += 2;
241         if (y2 < sizey - 1) count += 2;
242         if (y1 > 0) count += 2;
243
244         if (count == 0) {
245                 return;
246         }
247
248         immBegin(GWN_PRIM_LINES, count);
249
250         /* right border area */
251         if (x2 < sizex - 1) {
252                 immVertex2f(pos, x2, y1);
253                 immVertex2f(pos, x2, y2);
254         }
255
256         /* left border area */
257         if (x1 > 0) { /* otherwise it draws the emboss of window over */
258                 immVertex2f(pos, x1, y1);
259                 immVertex2f(pos, x1, y2);
260         }
261
262         /* top border area */
263         if (y2 < sizey - 1) {
264                 immVertex2f(pos, x1, y2);
265                 immVertex2f(pos, x2, y2);
266         }
267
268         /* bottom border area */
269         if (y1 > 0) {
270                 immVertex2f(pos, x1, y1);
271                 immVertex2f(pos, x2, y1);
272         }
273
274         immEnd();
275 }
276
277 /**
278  * \brief Screen edges drawing.
279  */
280 static void drawscredge_area(ScrArea *sa, int sizex, int sizey, unsigned int pos)
281 {
282         short x1 = sa->v1->vec.x;
283         short y1 = sa->v1->vec.y;
284         short x2 = sa->v3->vec.x;
285         short y2 = sa->v3->vec.y;
286
287         drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2, pos);
288 }
289
290 /**
291  * Only for edge lines between areas, and the blended join arrows.
292  */
293 void ED_screen_draw_edges(wmWindow *win)
294 {
295         bScreen *screen = WM_window_get_active_screen(win);
296         const int winsize_x = WM_window_pixels_x(win);
297         const int winsize_y = WM_window_pixels_y(win);
298
299         ScrArea *sa;
300
301         wmSubWindowSet(win, screen->mainwin);
302
303         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
304         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
305
306         /* Note: first loop only draws if U.pixelsize > 1, skip otherwise */
307         if (U.pixelsize > 1.0f) {
308                 /* FIXME: doesn't our glLineWidth already scale by U.pixelsize? */
309                 glLineWidth((2.0f * U.pixelsize) - 1);
310                 immUniformColor3ub(0x50, 0x50, 0x50);
311
312                 for (sa = screen->areabase.first; sa; sa = sa->next) {
313                         drawscredge_area(sa, winsize_x, winsize_y, pos);
314                 }
315         }
316
317         glLineWidth(1);
318         immUniformColor3ub(0, 0, 0);
319
320         for (sa = screen->areabase.first; sa; sa = sa->next) {
321                 drawscredge_area(sa, winsize_x, winsize_y, pos);
322         }
323
324         immUnbindProgram();
325
326         screen->do_draw = false;
327 }
328
329 void ED_screen_draw_join_shape(ScrArea *sa1, ScrArea *sa2)
330 {
331         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
332         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
333
334         glLineWidth(1);
335
336         /* blended join arrow */
337         int dir = area_getorientation(sa1, sa2);
338         int dira = -1;
339         if (dir != -1) {
340                 switch (dir) {
341                         case 0: /* W */
342                                 dir = 'r';
343                                 dira = 'l';
344                                 break;
345                         case 1: /* N */
346                                 dir = 'd';
347                                 dira = 'u';
348                                 break;
349                         case 2: /* E */
350                                 dir = 'l';
351                                 dira = 'r';
352                                 break;
353                         case 3: /* S */
354                                 dir = 'u';
355                                 dira = 'd';
356                                 break;
357                 }
358
359                 glEnable(GL_BLEND);
360
361                 scrarea_draw_shape_dark(sa2, dir, pos);
362                 scrarea_draw_shape_light(sa1, dira, pos);
363
364                 glDisable(GL_BLEND);
365         }
366
367         immUnbindProgram();
368 }
369
370 void ED_screen_draw_split_preview(ScrArea *sa, const int dir, const float fac)
371 {
372         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
373         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
374
375         /* splitpoint */
376         glEnable(GL_BLEND);
377         immUniformColor4ub(255, 255, 255, 100);
378
379         immBegin(GWN_PRIM_LINES, 2);
380
381         if (dir == 'h') {
382                 const float y = (1 - fac) * sa->totrct.ymin + fac * sa->totrct.ymax;
383
384                 immVertex2f(pos, sa->totrct.xmin, y);
385                 immVertex2f(pos, sa->totrct.xmax, y);
386
387                 immEnd();
388
389                 immUniformColor4ub(0, 0, 0, 100);
390
391                 immBegin(GWN_PRIM_LINES, 2);
392
393                 immVertex2f(pos, sa->totrct.xmin, y + 1);
394                 immVertex2f(pos, sa->totrct.xmax, y + 1);
395
396                 immEnd();
397         }
398         else {
399                 BLI_assert(dir == 'v');
400                 const float x = (1 - fac) * sa->totrct.xmin + fac * sa->totrct.xmax;
401
402                 immVertex2f(pos, x, sa->totrct.ymin);
403                 immVertex2f(pos, x, sa->totrct.ymax);
404
405                 immEnd();
406
407                 immUniformColor4ub(0, 0, 0, 100);
408
409                 immBegin(GWN_PRIM_LINES, 2);
410
411                 immVertex2f(pos, x + 1, sa->totrct.ymin);
412                 immVertex2f(pos, x + 1, sa->totrct.ymax);
413
414                 immEnd();
415         }
416
417         glDisable(GL_BLEND);
418
419         immUnbindProgram();
420 }
421
422
423 /* -------------------------------------------------------------------- */
424 /* Screen Thumbnail Preview */
425
426 /**
427  * Calculates a scale factor to squash the preview for \a screen into a rectangle of given size and aspect.
428  */
429 static void screen_preview_scale_get(
430         const bScreen *screen, float size_x, float size_y,
431         const float asp[2],
432         float r_scale[2])
433 {
434         float max_x = 0, max_y = 0;
435
436         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
437                 max_x = MAX2(max_x, sa->totrct.xmax);
438                 max_y = MAX2(max_y, sa->totrct.ymax);
439         }
440         r_scale[0] = (size_x * asp[0]) / max_x;
441         r_scale[1] = (size_y * asp[1]) / max_y;
442 }
443
444 static void screen_preview_draw_areas(const bScreen *screen, const float scale[2], const float col[4],
445                                       const float ofs_between_areas)
446 {
447         const float ofs_h = ofs_between_areas * 0.5f;
448         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
449
450         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
451         immUniformColor4fv(col);
452
453         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
454                 rctf rect = {
455                         .xmin = sa->totrct.xmin * scale[0] + ofs_h,
456                         .xmax = sa->totrct.xmax * scale[0] - ofs_h,
457                         .ymin = sa->totrct.ymin * scale[1] + ofs_h,
458                         .ymax = sa->totrct.ymax * scale[1] - ofs_h
459                 };
460
461                 immBegin(GWN_PRIM_TRI_FAN, 4);
462                 immVertex2f(pos, rect.xmin, rect.ymin);
463                 immVertex2f(pos, rect.xmax, rect.ymin);
464                 immVertex2f(pos, rect.xmax, rect.ymax);
465                 immVertex2f(pos, rect.xmin, rect.ymax);
466                 immEnd();
467         }
468
469         immUnbindProgram();
470 }
471
472 static void screen_preview_draw(const bScreen *screen, int size_x, int size_y)
473 {
474         const float asp[2] = {1.0f, 0.8f}; /* square previews look a bit ugly */
475         /* could use theme color (tui.wcol_menu_item.text), but then we'd need to regenerate all previews when changing */
476         const float col[4] = {1.0f, 1.0f, 1.0f, 1.0f};
477         float scale[2];
478
479         wmOrtho2(0.0f, size_x, 0.0f, size_y);
480         /* center */
481         gpuPushMatrix();
482         gpuTranslate2f(size_x * (1.0f - asp[0]) * 0.5f, size_y * (1.0f - asp[1]) * 0.5f);
483
484         screen_preview_scale_get(screen, size_x, size_y, asp, scale);
485         screen_preview_draw_areas(screen, scale, col, 1.5f);
486
487         gpuPopMatrix();
488 }
489
490 /**
491  * Render the preview for a screen layout in \a screen.
492  */
493 void ED_screen_preview_render(const bScreen *screen, int size_x, int size_y, unsigned int *r_rect)
494 {
495         char err_out[256] = "unknown";
496         GPUOffScreen *offscreen = GPU_offscreen_create(size_x, size_y, 0, false, err_out);
497
498         GPU_offscreen_bind(offscreen, true);
499         glClearColor(0.0, 0.0, 0.0, 0.0);
500         glClear(GL_COLOR_BUFFER_BIT);
501
502         screen_preview_draw(screen, size_x, size_y);
503
504         GPU_offscreen_read_pixels(offscreen, GL_UNSIGNED_BYTE, r_rect);
505         GPU_offscreen_unbind(offscreen, true);
506
507         GPU_offscreen_free(offscreen);
508 }