Dependency graph patch, provided by Jean-Luc Peuriere.
[blender.git] / source / blender / src / drawoops.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <math.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include "BMF_Api.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_arithb.h"
45
46 #include "DNA_ID.h"
47 #include "DNA_object_types.h"
48 #include "DNA_oops_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
52 #include "DNA_view2d_types.h"
53
54 #include "BKE_utildefines.h"
55 #include "BKE_global.h"
56
57 #include "BIF_interface.h"
58 #include "BIF_gl.h"
59 #include "BIF_glutil.h"
60 #include "BIF_mywindow.h"
61 #include "BIF_outliner.h"
62 #include "BIF_resources.h"
63 #include "BIF_screen.h"
64
65 /*  #include "BIF_drawoops.h" bad name :(*/
66 #include "BIF_oops.h"
67
68 #include "BSE_drawipo.h"
69 #include "BSE_drawoops.h"
70
71 #include "BKE_depsgraph.h"
72
73 extern void build_deps(short mask);
74 //extern void draw_deps(DagNode *node);
75
76
77 float oopscalex;
78
79 void boundbox_oops()
80 {
81         Oops *oops;
82         float min[2], max[2];
83         int ok= 0;
84         
85         if(G.soops==0) return;
86         
87         min[0]= 1000.0;
88         max[0]= -10000.0;
89         min[1]= 1000.0;
90         max[1]= -1000.0;
91         
92         oops= G.soops->oops.first;
93         while(oops) {
94                 if(oops->hide==0) {
95                         ok= 1;
96                         
97                         min[0]= MIN2(min[0], oops->x);
98                         max[0]= MAX2(max[0], oops->x+OOPSX);
99                         min[1]= MIN2(min[1], oops->y);
100                         max[1]= MAX2(max[1], oops->y+OOPSY);
101                 }
102                 oops= oops->next;
103         }
104         
105         if(ok==0) return;
106         
107         G.v2d->tot.xmin= min[0];
108         G.v2d->tot.xmax= max[0];
109         G.v2d->tot.ymin= min[1];
110         G.v2d->tot.ymax= max[1];
111         
112 }
113
114 void give_oopslink_line(Oops *oops, OopsLink *ol, float *v1, float *v2)
115 {
116
117         if(ol->to && ol->to->hide==0) {
118                 v1[0]= oops->x+ol->xof;
119                 v1[1]= oops->y+ol->yof;
120                 v2[0]= ol->to->x+OOPSX/2;
121                 v2[1]= ol->to->y;
122         }
123         else if(ol->from && ol->from->hide==0) {
124                 v1[0]= ol->from->x + ol->xof;
125                 v1[1]= ol->from->y + ol->xof;
126                 v2[0]= oops->x+OOPSX/2;
127                 v2[1]= oops->y;
128         }
129 }
130
131 void draw_oopslink(Oops *oops)
132 {
133         OopsLink *ol;
134         float vec[4][3], dist;
135         int a;
136         
137         if(oops->type==ID_SCE) {
138                 if(oops->flag & SELECT) {
139                         if(oops->id->lib) cpack(0x4080A0);
140                         else cpack(0x808080);
141                 }
142                 else cpack(0x606060);
143         }
144         else {
145                 if(oops->flag & SELECT) {
146                         if(oops->id->lib) cpack(0x11AAFF);
147                         else cpack(0xFFFFFF);
148                 }
149                 else cpack(0x0);
150         }
151         
152         glEnable(GL_MAP1_VERTEX_3);
153         vec[0][2]= vec[1][2]= vec[2][2]= vec[3][2]= 0.0;
154         
155         ol= oops->link.first;
156         while(ol) {
157                 if(ol->to && ol->to->hide==0) {
158                         
159                         give_oopslink_line(oops, ol, vec[0], vec[3]);
160                         
161                         dist= 0.5*VecLenf(vec[0], vec[3]);
162
163                         /* check ol->xof and yof for direction */
164                         if(ol->xof == 0.0) {
165                                 vec[1][0]= vec[0][0]-dist;
166                                 vec[1][1]= vec[0][1];
167                         }
168                         else if(ol->xof==OOPSX) {
169                                 vec[1][0]= vec[0][0]+dist;
170                                 vec[1][1]= vec[0][1];
171                         }
172                         else {
173                                 vec[1][0]= vec[0][0];
174                                 vec[1][1]= vec[0][1]+dist;
175                         }
176                         
177                         /* v3 is always pointing down */
178                         vec[2][0]= vec[3][0];
179                         vec[2][1]= vec[3][1] - dist;
180
181                         glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, vec[0]);
182                         
183                         glBegin(GL_LINE_STRIP);
184                         for(a=0; a<=30; a++) {
185                                 glEvalCoord1f((float)a/30.0);
186                         }
187                         glEnd();
188                 }
189                 ol= ol->next;
190         }
191 }
192
193 void draw_icon_oops(float *co, short type)
194 {
195         BIFIconID icon;
196         
197         switch(type) {
198         default: return;
199
200         case ID_OB:     icon= ICON_OBJECT_HLT; break;
201         case ID_ME:     icon= ICON_MESH_HLT; break;
202         case ID_CU:     icon= ICON_CURVE_HLT; break;
203         case ID_MB:     icon= ICON_MBALL_HLT; break;
204         case ID_LT:     icon= ICON_LATTICE_HLT; break;
205         case ID_LA:     icon= ICON_LAMP_HLT; break;
206         case ID_MA:     icon= ICON_MATERIAL_HLT; break;
207         case ID_TE:     icon= ICON_TEXTURE_HLT; break;
208         case ID_IP:     icon= ICON_IPO_HLT; break;
209         case ID_LI:     icon= ICON_LIBRARY_HLT; break;
210         case ID_IM:     icon= ICON_IMAGE_HLT; break;
211         }
212
213         glEnable(GL_BLEND);
214         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
215
216         glRasterPos2f(co[0],  co[1]-0.2);
217         BIF_draw_icon(icon);
218
219         glBlendFunc(GL_ONE,  GL_ZERO); 
220         glDisable(GL_BLEND);
221 }
222
223 void mysbox(float x1, float y1, float x2, float y2)
224 {
225         float vec[2];
226         
227         glBegin(GL_LINE_LOOP);
228         vec[0]= x1; vec[1]= y1;
229         glVertex2fv(vec);
230         vec[0]= x2;
231         glVertex2fv(vec);
232         vec[1]= y2;
233         glVertex2fv(vec);
234         vec[0]= x1;
235         glVertex2fv(vec);
236         glEnd();
237 }
238
239 unsigned int give_oops_color(short type, short sel, unsigned int *border)
240 {
241         unsigned int body;
242         /* also finds out if a dashed line should be drawn */
243
244         switch(type) {
245         case ID_OB:
246                 body= 0x707070; break;
247         case ID_SCE:
248                 body= 0x608060; break;
249         case ID_MA:
250                 body= 0x808060; break;
251         case ID_TE:
252                 body= 0x7080a0; break;
253         case ID_IP:
254                 body= 0x906050; break;
255         case ID_LA:
256                 body= 0x608080; break;
257         case ID_LI:
258                 body= 0x2198DC; break;
259         case ID_IM:
260                 body= 0x35659F; break;
261         default:
262                 body= 0x606070; break;
263         }
264
265         if(sel) {
266                 if(G.moving) *border= 0xf0f0f0;
267                 else *border= 0xc0c0c0; 
268         }
269         else *border= 0x0;
270
271         
272         return body;
273 }
274
275 void calc_oopstext(char *str, float *v1)
276 {
277         float f1, f2, size;
278         short mval[2], len, flen;
279         
280         ipoco_to_areaco_noclip(G.v2d, v1, mval);
281         f1= mval[0];
282         v1[0]+= OOPSX;
283         ipoco_to_areaco_noclip(G.v2d, v1, mval);
284         f2= mval[0];
285         size= f2-f1;
286
287         len= strlen(str);
288         
289         while( (flen= BMF_GetStringWidth(G.fonts, str)) > size) {
290                 if(flen < 10 || len<2) break;
291                 len--;
292                 str[len]= 0;
293         }
294         
295         mval[0]= (f1+f2-flen+1)/2;
296         mval[1]= 1;
297         areamouseco_to_ipoco(G.v2d, mval, &f1, &f2);
298         
299         v1[0]= f1;
300         
301 }
302
303 void draw_oops(Oops *oops)
304 {
305         OopsLink *ol;
306         float v1[2], x1, y1, x2, y2, f1, f2;
307         unsigned int body, border;
308         short line= 0;
309         char str[32];
310
311         x1= oops->x; 
312         x2= oops->x+OOPSX;
313         y1= oops->y; 
314         y2= oops->y+OOPSY;
315
316         if(x2 < G.v2d->cur.xmin || x1 > G.v2d->cur.xmax) return;
317         if(y2 < G.v2d->cur.ymin || y1 > G.v2d->cur.ymax) return;
318
319         body= give_oops_color(oops->type, oops->flag & SELECT, &border);
320         if(oops->id== (ID *)((G.scene->basact) ? (G.scene->basact->object) : 0)) line= 1;
321         else if(oops->id== (ID *)G.scene) line= 1;
322
323         if(oops->id->us) {
324                 cpack(body);
325
326                 glRectf(x1,  y1,  x2,  y2);
327         }
328         if(oops->id->lib) {
329                 if(oops->id->flag & LIB_INDIRECT) cpack(0x1144FF);
330                 else cpack(0x11AAFF);
331
332                 glRectf(x2-0.2*OOPSX,  y2-0.2*OOPSX,  x2-0.1*OOPSX,  y2-0.1*OOPSX);
333         }
334
335         v1[0]= x1; 
336         v1[1]= (y1+y2)/2 -0.3;
337         if(oops->type==ID_LI) {
338                 sprintf(str, "     %s", ((Library *)oops->id)->name);
339         }
340         else {
341                 sprintf(str, "     %s", oops->id->name+2);
342         }
343         calc_oopstext(str, v1);
344
345         /* ICON */
346         if(str[1] && oopscalex>1.1) {
347                 draw_icon_oops(v1, oops->type);
348         }
349         if(oops->flag & SELECT) cpack(0xFFFFFF); 
350         else cpack(0x0);
351         glRasterPos3f(v1[0],  v1[1], 0.0);
352         BMF_DrawString(G.fonts, str);
353
354         if(line) setlinestyle(2);
355         cpack(border);
356
357         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
358         glRectf(x1,  y1,  x2,  y2);
359         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
360         if(line) setlinestyle(0);
361
362         /* connection blocks */
363         ol= oops->link.first;
364         while(ol) {
365
366                 f1= x1+ol->xof; 
367                 f2= y1+ol->yof;
368
369                 body= give_oops_color(ol->type, oops->flag & SELECT, &border);
370                 cpack(body);
371
372                 glRectf(f1-.2,  f2-.2,  f1+.2,  f2+.2);
373                 cpack(border);
374
375                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
376
377                 glRectf(f1-.2,  f2-.2,  f1+.2,  f2+.2);
378
379                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
380
381                 ol= ol->next;
382         }
383
384         if(oops->flag & OOPS_REFER) {
385                         /* Draw the little rounded connection point */
386                 glColor3ub(0, 0, 0);
387                 glPushMatrix();
388
389                 glTranslatef(oops->x + 0.5*OOPSX, oops->y, 0.0);
390                 glutil_draw_filled_arc(0.0, M_PI, 0.05*OOPSX, 7);
391
392                 glPopMatrix();
393         }
394 }
395
396 void drawoopsspace(ScrArea *sa, void *spacedata)
397 {
398         SpaceOops *soops= spacedata;
399         Oops *oops;
400         float col[3];
401         
402         BIF_GetThemeColor3fv(TH_BACK, col);
403         glClearColor(col[0], col[1], col[2], 0.0);
404         glClear(GL_COLOR_BUFFER_BIT);
405         if(soops==0) return;    
406         
407         if(soops->type==SO_OUTLINER) draw_outliner(sa, soops);
408         else if (soops->type==SO_DEPSGRAPH) {
409                 build_deps(soops->deps_flags);
410                 boundbox_deps();
411                 calc_scrollrcts(sa,G.v2d, curarea->winx, curarea->winy);
412
413                 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
414
415                 oopscalex= .14*((float)curarea->winx)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
416                 calc_ipogrid(); /* for scrollvariables */
417                 draw_all_deps();
418         } else {
419                 boundbox_oops();
420                 calc_scrollrcts(sa, G.v2d, curarea->winx, curarea->winy);
421
422                 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
423
424                 oopscalex= .14*((float)curarea->winx)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
425                 calc_ipogrid(); /* for scrollvariables */
426                 build_oops();
427
428                 oops= soops->oops.first;
429                 while(oops) {
430                         if(oops->hide==0) {
431                                 draw_oopslink(oops);
432                         }
433                         oops= oops->next;
434                 }
435                 oops= soops->oops.first;
436                 while(oops) {
437                         if(oops->hide==0) {
438                                 if(oops->flag & SELECT); else draw_oops(oops);
439                         }
440                         oops= oops->next;
441                 }
442                 oops= soops->oops.first;
443                 while(oops) {
444                         if(oops->hide==0) {
445                                 if(oops->flag & SELECT) draw_oops(oops);
446                         }
447                         oops= oops->next;
448                 }
449         }
450         
451         /* restore viewport */
452         mywinset(curarea->win);
453         
454         /* ortho at pixel level curarea */
455         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
456
457         if(soops->type==SO_OUTLINER) {
458                 if(sa->winx>SCROLLB+10 && sa->winy>SCROLLH+10) {
459                         if(G.v2d->scroll) drawscroll(0);                
460                 }
461         }
462         draw_area_emboss(sa);   
463         
464         curarea->win_swap= WIN_BACK_OK;
465 }
466
467
468