Update description of the screen_draw functions
[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 "BIF_gl.h"
26
27 #include "WM_api.h"
28
29 #include "ED_screen.h"
30
31 #include "screen_intern.h"
32
33 /**
34  * Draw horizontal shape visualizing future joining (left as well right direction of future joining).
35  */
36 static void draw_horizontal_join_shape(ScrArea *sa, char dir)
37 {
38         vec2f points[10];
39         short i;
40         float w, h;
41         float width = sa->v3->vec.x - sa->v1->vec.x;
42         float height = sa->v3->vec.y - sa->v1->vec.y;
43
44         if (height < width) {
45                 h = height / 8;
46                 w = height / 4;
47         }
48         else {
49                 h = width / 8;
50                 w = width / 4;
51         }
52
53         points[0].x = sa->v1->vec.x;
54         points[0].y = sa->v1->vec.y + height / 2;
55
56         points[1].x = sa->v1->vec.x;
57         points[1].y = sa->v1->vec.y;
58
59         points[2].x = sa->v4->vec.x - w;
60         points[2].y = sa->v4->vec.y;
61
62         points[3].x = sa->v4->vec.x - w;
63         points[3].y = sa->v4->vec.y + height / 2 - 2 * h;
64
65         points[4].x = sa->v4->vec.x - 2 * w;
66         points[4].y = sa->v4->vec.y + height / 2;
67
68         points[5].x = sa->v4->vec.x - w;
69         points[5].y = sa->v4->vec.y + height / 2 + 2 * h;
70
71         points[6].x = sa->v3->vec.x - w;
72         points[6].y = sa->v3->vec.y;
73
74         points[7].x = sa->v2->vec.x;
75         points[7].y = sa->v2->vec.y;
76
77         points[8].x = sa->v4->vec.x;
78         points[8].y = sa->v4->vec.y + height / 2 - h;
79
80         points[9].x = sa->v4->vec.x;
81         points[9].y = sa->v4->vec.y + height / 2 + h;
82
83         if (dir == 'l') {
84                 /* when direction is left, then we flip direction of arrow */
85                 float cx = sa->v1->vec.x + width;
86                 for (i = 0; i < 10; i++) {
87                         points[i].x -= cx;
88                         points[i].x = -points[i].x;
89                         points[i].x += sa->v1->vec.x;
90                 }
91         }
92
93         glBegin(GL_POLYGON);
94         for (i = 0; i < 5; i++)
95                 glVertex2f(points[i].x, points[i].y);
96         glEnd();
97         glBegin(GL_POLYGON);
98         for (i = 4; i < 8; i++)
99                 glVertex2f(points[i].x, points[i].y);
100         glVertex2f(points[0].x, points[0].y);
101         glEnd();
102
103         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
104         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
105 }
106
107 /**
108  * Draw vertical shape visualizing future joining (up/down direction).
109  */
110 static void draw_vertical_join_shape(ScrArea *sa, char dir)
111 {
112         vec2f points[10];
113         short i;
114         float w, h;
115         float width = sa->v3->vec.x - sa->v1->vec.x;
116         float height = sa->v3->vec.y - sa->v1->vec.y;
117
118         if (height < width) {
119                 h = height / 4;
120                 w = height / 8;
121         }
122         else {
123                 h = width / 4;
124                 w = width / 8;
125         }
126
127         points[0].x = sa->v1->vec.x + width / 2;
128         points[0].y = sa->v3->vec.y;
129
130         points[1].x = sa->v2->vec.x;
131         points[1].y = sa->v2->vec.y;
132
133         points[2].x = sa->v1->vec.x;
134         points[2].y = sa->v1->vec.y + h;
135
136         points[3].x = sa->v1->vec.x + width / 2 - 2 * w;
137         points[3].y = sa->v1->vec.y + h;
138
139         points[4].x = sa->v1->vec.x + width / 2;
140         points[4].y = sa->v1->vec.y + 2 * h;
141
142         points[5].x = sa->v1->vec.x + width / 2 + 2 * w;
143         points[5].y = sa->v1->vec.y + h;
144
145         points[6].x = sa->v4->vec.x;
146         points[6].y = sa->v4->vec.y + h;
147         
148         points[7].x = sa->v3->vec.x;
149         points[7].y = sa->v3->vec.y;
150
151         points[8].x = sa->v1->vec.x + width / 2 - w;
152         points[8].y = sa->v1->vec.y;
153
154         points[9].x = sa->v1->vec.x + width / 2 + w;
155         points[9].y = sa->v1->vec.y;
156
157         if (dir == 'u') {
158                 /* when direction is up, then we flip direction of arrow */
159                 float cy = sa->v1->vec.y + height;
160                 for (i = 0; i < 10; i++) {
161                         points[i].y -= cy;
162                         points[i].y = -points[i].y;
163                         points[i].y += sa->v1->vec.y;
164                 }
165         }
166
167         glBegin(GL_POLYGON);
168         for (i = 0; i < 5; i++)
169                 glVertex2f(points[i].x, points[i].y);
170         glEnd();
171         glBegin(GL_POLYGON);
172         for (i = 4; i < 8; i++)
173                 glVertex2f(points[i].x, points[i].y);
174         glVertex2f(points[0].x, points[0].y);
175         glEnd();
176
177         glRectf(points[2].x, points[2].y, points[8].x, points[8].y);
178         glRectf(points[6].x, points[6].y, points[9].x, points[9].y);
179 }
180
181 /**
182  * Draw join shape due to direction of joining.
183  */
184 static void draw_join_shape(ScrArea *sa, char dir)
185 {
186         if (dir == 'u' || dir == 'd') {
187                 draw_vertical_join_shape(sa, dir);
188         }
189         else {
190                 draw_horizontal_join_shape(sa, dir);
191         }
192 }
193
194 /**
195  * Draw screen area darker with arrow (visualization of future joining).
196  */
197 static void scrarea_draw_shape_dark(ScrArea *sa, char dir)
198 {
199         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
200         glColor4ub(0, 0, 0, 50);
201         draw_join_shape(sa, dir);
202 }
203
204 /**
205  * Draw screen area ligher with arrow shape ("eraser" of previous dark shape).
206  */
207 static void scrarea_draw_shape_light(ScrArea *sa, char UNUSED(dir))
208 {
209         glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
210         /* value 181 was hardly computed: 181~105 */
211         glColor4ub(255, 255, 255, 50);
212         /* draw_join_shape(sa, dir); */
213         glRecti(sa->v1->vec.x, sa->v1->vec.y, sa->v3->vec.x, sa->v3->vec.y);
214 }
215
216 static void drawscredge_area_draw(int sizex, int sizey, short x1, short y1, short x2, short y2)
217 {
218         /* right border area */
219         if (x2 < sizex - 1) {
220                 glVertex2s(x2, y1);
221                 glVertex2s(x2, y2);
222         }
223
224         /* left border area */
225         if (x1 > 0) { /* otherwise it draws the emboss of window over */
226                 glVertex2s(x1, y1);
227                 glVertex2s(x1, y2);
228         }
229
230         /* top border area */
231         if (y2 < sizey - 1) {
232                 glVertex2s(x1, y2);
233                 glVertex2s(x2, y2);
234         }
235
236         /* bottom border area */
237         if (y1 > 0) {
238                 glVertex2s(x1, y1);
239                 glVertex2s(x2, y1);
240         }
241 }
242
243 /**
244  * \brief Screen edges drawing.
245  */
246 static void drawscredge_area(ScrArea *sa, int sizex, int sizey)
247 {
248         short x1 = sa->v1->vec.x;
249         short y1 = sa->v1->vec.y;
250         short x2 = sa->v3->vec.x;
251         short y2 = sa->v3->vec.y;
252
253         drawscredge_area_draw(sizex, sizey, x1, y1, x2, y2);
254 }
255
256 /**
257  * Only for edge lines between areas.
258  */
259 void ED_screen_draw_edges(wmWindow *win)
260 {
261         const int winsize_x = WM_window_pixels_x(win);
262         const int winsize_y = WM_window_pixels_y(win);
263
264         ScrArea *sa;
265
266         wmSubWindowSet(win, win->screen->mainwin);
267
268         /* Note: first loop only draws if U.pixelsize > 1, skip otherwise */
269         if (U.pixelsize > 1.0f) {
270                 /* FIXME: doesn't our glLineWidth already scale by U.pixelsize? */
271                 glLineWidth((2.0f * U.pixelsize) - 1);
272                 glColor3ub(0x50, 0x50, 0x50);
273                 glBegin(GL_LINES);
274                 for (sa = win->screen->areabase.first; sa; sa = sa->next)
275                         drawscredge_area(sa, winsize_x, winsize_y);
276                 glEnd();
277         }
278
279         glLineWidth(1);
280         glColor3ub(0, 0, 0);
281         glBegin(GL_LINES);
282         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
283                 drawscredge_area(sa, winsize_x, winsize_y);
284         }
285         glEnd();
286
287         win->screen->do_draw = false;
288 }
289
290 /**
291  * The blended join arrows.
292  *
293  * \param sa1: Area from which the resultant originates.
294  * \param sa2: Target area that will be replaced.
295  */
296 void ED_screen_draw_join_shape(ScrArea *sa1, ScrArea *sa2)
297 {
298         glLineWidth(1);
299
300         /* blended join arrow */
301         int dir = area_getorientation(sa1, sa2);
302         int dira = -1;
303         if (dir != -1) {
304                 switch (dir) {
305                         case 0: /* W */
306                                 dir = 'r';
307                                 dira = 'l';
308                                 break;
309                         case 1: /* N */
310                                 dir = 'd';
311                                 dira = 'u';
312                                 break;
313                         case 2: /* E */
314                                 dir = 'l';
315                                 dira = 'r';
316                                 break;
317                         case 3: /* S */
318                                 dir = 'u';
319                                 dira = 'd';
320                                 break;
321                 }
322                 glEnable(GL_BLEND);
323                 scrarea_draw_shape_dark(sa2, dir);
324                 scrarea_draw_shape_light(sa1, dira);
325                 glDisable(GL_BLEND);
326         }
327 }
328
329 void ED_screen_draw_split_preview(ScrArea *sa, const int dir, const float fac)
330 {
331         /* splitpoint */
332         glEnable(GL_BLEND);
333         glBegin(GL_LINES);
334         glColor4ub(255, 255, 255, 100);
335
336         if (dir == 'h') {
337                 const float y = (1 - fac) * sa->totrct.ymin + fac * sa->totrct.ymax;
338                 glVertex2s(sa->totrct.xmin, y);
339                 glVertex2s(sa->totrct.xmax, y);
340                 glColor4ub(0, 0, 0, 100);
341                 glVertex2s(sa->totrct.xmin, y + 1);
342                 glVertex2s(sa->totrct.xmax, y + 1);
343         }
344         else {
345                 const float x = (1 - fac) * sa->totrct.xmin + fac * sa->totrct.xmax;
346                 glVertex2s(x, sa->totrct.ymin);
347                 glVertex2s(x, sa->totrct.ymax);
348                 glColor4ub(0, 0, 0, 100);
349                 glVertex2s(x + 1, sa->totrct.ymin);
350                 glVertex2s(x + 1, sa->totrct.ymax);
351         }
352         glEnd();
353         glDisable(GL_BLEND);
354 }
355