When adding a Mesh in python a file saved caused blender to segfault when reloading.
[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 float oopscalex;
72 struct BMF_Font *font; /* for using different sized fonts */
73
74 void boundbox_oops(short sel)
75 {
76         Oops *oops;
77         float min[2], max[2];
78         int ok= 0;
79         
80         if(G.soops==0) return;
81         
82         min[0]= 1000.0;
83         max[0]= -1000.0;
84         min[1]= 1000.0;
85         max[1]= -1000.0;
86         
87         oops= G.soops->oops.first;
88         while(oops) {
89                 if ((oops->hide==0 && !sel) || (sel && oops->flag & SELECT )) {
90                         ok= 1;
91                         
92                         min[0]= MIN2(min[0], oops->x);
93                         max[0]= MAX2(max[0], oops->x+OOPSX);
94                         min[1]= MIN2(min[1], oops->y);
95                         max[1]= MAX2(max[1], oops->y+OOPSY);
96                 }
97                 oops= oops->next;
98         }
99         
100         if(ok==0) return;
101         
102         G.v2d->tot.xmin= min[0];
103         G.v2d->tot.xmax= max[0];
104         G.v2d->tot.ymin= min[1];
105         G.v2d->tot.ymax= max[1];
106         
107 }
108
109 void give_oopslink_line(Oops *oops, OopsLink *ol, float *v1, float *v2)
110 {
111
112         if(ol->to && ol->to->hide==0) {
113                 v1[0]= oops->x+ol->xof;
114                 v1[1]= oops->y+ol->yof;
115                 v2[0]= ol->to->x+OOPSX/2;
116                 v2[1]= ol->to->y;
117         }
118         else if(ol->from && ol->from->hide==0) {
119                 v1[0]= ol->from->x + ol->xof;
120                 v1[1]= ol->from->y + ol->xof;
121                 v2[0]= oops->x+OOPSX/2;
122                 v2[1]= oops->y;
123         }
124 }
125
126 void draw_oopslink(Oops *oops)
127 {
128         OopsLink *ol;
129         float vec[4][3], dist, spline_step;
130         short curve_res;
131         
132         if(oops->type==ID_SCE) {
133                 if(oops->flag & SELECT) {
134                         /* when using python Mesh to make meshes a file was saved
135                         that had an oops with no ID, stops a segfault when looking for lib */
136                         if(oops->id && oops->id->lib) cpack(0x4080A0);
137                         else cpack(0x808080);
138                 }
139                 else cpack(0x606060);
140         }
141         else {
142                 if(oops->flag & SELECT) {
143                         if(oops->id && oops->id->lib) cpack(0x11AAFF);
144                         else cpack(0xFFFFFF);
145                 }
146                 else cpack(0x0);
147         }
148         
149         glEnable(GL_MAP1_VERTEX_3);
150         vec[0][2]= vec[1][2]= vec[2][2]= vec[3][2]= 0.0; /* only 2d spline, set the Z to 0*/
151         
152         ol= oops->link.first;
153         while(ol) {
154                 if(ol->to && ol->to->hide==0) {
155                         
156                         give_oopslink_line(oops, ol, vec[0], vec[3]);
157                         
158                         dist= 0.5*VecLenf(vec[0], vec[3]);
159
160                         /* check ol->xof and yof for direction */
161                         if(ol->xof == 0.0) {
162                                 vec[1][0]= vec[0][0]-dist;
163                                 vec[1][1]= vec[0][1];
164                         }
165                         else if(ol->xof==OOPSX) {
166                                 vec[1][0]= vec[0][0]+dist;
167                                 vec[1][1]= vec[0][1];
168                         }
169                         else {
170                                 vec[1][0]= vec[0][0];
171                                 vec[1][1]= vec[0][1]+dist;
172                         }
173                         
174                         /* v3 is always pointing down */
175                         vec[2][0]= vec[3][0];
176                         vec[2][1]= vec[3][1] - dist;
177                         
178                         if( MIN4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) > G.v2d->cur.xmax); /* clipped */  
179                                 else if ( MAX4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) < G.v2d->cur.xmin); /* clipped */
180                                         else {
181                                                 /* calculate a curve resolution to use based on the length of the curve.*/
182                                                 curve_res = MIN2(40, MAX2(2, (short)((dist*2) * (oopscalex))));
183                                                 
184                                                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
185                                                 dist = (float)1/curve_res;
186                                                 spline_step = 0.0;
187                                                 
188                                                 glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, vec[0]);
189                                                 glBegin(GL_LINE_STRIP);
190                                                 while (spline_step < 1.000001) {
191                                                         glEvalCoord1f(spline_step);
192                                                         spline_step += dist;
193                                                 }
194                                                 glEnd();
195                                         }
196                         
197                 }
198                 ol= ol->next;
199         }
200 }
201
202 void draw_icon_oops(float *co, short type)
203 {
204         BIFIconID icon;
205         
206         switch(type) {
207         default: return;
208
209         case ID_OB:     icon= ICON_OBJECT_HLT; break;
210         case ID_ME:     icon= ICON_MESH_HLT; break;
211         case ID_CU:     icon= ICON_CURVE_HLT; break;
212         case ID_MB:     icon= ICON_MBALL_HLT; break;
213         case ID_LT:     icon= ICON_LATTICE_HLT; break;
214         case ID_LA:     icon= ICON_LAMP_HLT; break;
215         case ID_MA:     icon= ICON_MATERIAL_HLT; break;
216         case ID_TE:     icon= ICON_TEXTURE_HLT; break;
217         case ID_IP:     icon= ICON_IPO_HLT; break;
218         case ID_LI:     icon= ICON_LIBRARY_HLT; break;
219         case ID_IM:     icon= ICON_IMAGE_HLT; break;
220         }
221
222         glEnable(GL_BLEND);
223         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
224
225         BIF_draw_icon(co[0], co[1]-0.2, icon);
226
227         glBlendFunc(GL_ONE,  GL_ZERO); 
228         glDisable(GL_BLEND);
229 }
230
231 void mysbox(float x1, float y1, float x2, float y2)
232 {
233         float vec[2];
234         
235         glBegin(GL_LINE_LOOP);
236         vec[0]= x1; vec[1]= y1;
237         glVertex2fv(vec);
238         vec[0]= x2;
239         glVertex2fv(vec);
240         vec[1]= y2;
241         glVertex2fv(vec);
242         vec[0]= x1;
243         glVertex2fv(vec);
244         glEnd();
245 }
246
247 unsigned int give_oops_color(short type, short sel, unsigned int *border)
248 {
249         unsigned int body;
250         /* also finds out if a dashed line should be drawn */
251
252         switch(type) {
253         case ID_OB:
254                 body= 0x707070; break;
255         case ID_SCE:
256                 body= 0x608060; break;
257         case ID_MA:
258                 body= 0x808060; break;
259         case ID_TE:
260                 body= 0x7080a0; break;
261         case ID_IP:
262                 body= 0x906050; break;
263         case ID_LA:
264                 body= 0x608080; break;
265         case ID_LI:
266                 body= 0x2198DC; break;
267         case ID_IM:
268                 body= 0x35659F; break;
269         default:
270                 body= 0x606070; break;
271         }
272
273         if(sel) {
274                 if(G.moving) *border= 0xf0f0f0;
275                 else *border= 0xc0c0c0; 
276         }
277         else *border= 0x0;
278
279         
280         return body;
281 }
282
283 void calc_oopstext(char *str, float *v1)
284 {
285         float f1, f2, size; /* f1 is the lhs of the oops block, f2 is the rhs */
286         short mval[2], len, flen;
287         
288         ipoco_to_areaco_noclip(G.v2d, v1, mval);
289         f1= mval[0];
290         v1[0]+= OOPSX;
291         ipoco_to_areaco_noclip(G.v2d, v1, mval);
292         f2= mval[0];
293         
294         if (oopscalex>1.1) { /* Make text area wider if we have no icon.*/
295                 size= f2-f1;
296         } else {
297                 size= (f2-f1)*1.7; 
298         }
299
300         len= strlen(str);
301         
302         while( (flen= BMF_GetStringWidth(G.fonts, str)) > size) {
303                 if(flen < 10 || len<2) break;
304                 len--;
305                 str[len]= 0;
306         }
307         
308         flen= BMF_GetStringWidth(font, str);
309         
310         /* calc centred location for icon and text,
311         else if were zoomed too far out, just push text to the left of the oops block. */
312         if (oopscalex>1.1) { 
313                 mval[0]= (f1+f2-flen+1)/2;
314         } else {
315                 mval[0]=  f1; 
316         }
317         
318         mval[1]= 1;
319         areamouseco_to_ipoco(G.v2d, mval, &f1, &f2);
320         
321         v1[0]= f1;
322         
323 }
324
325 void draw_oops(Oops *oops)
326 {
327         OopsLink *ol;
328         float v1[2], x1, y1, x2, y2, f1, f2;
329         unsigned int body, border;
330         short line= 0;
331         char str[32];
332
333         x1= oops->x; 
334         x2= oops->x+OOPSX;
335         y1= oops->y; 
336         y2= oops->y+OOPSY;
337
338         if(x2 < G.v2d->cur.xmin || x1 > G.v2d->cur.xmax) return;
339         if(y2 < G.v2d->cur.ymin || y1 > G.v2d->cur.ymax) return;
340
341         body= give_oops_color(oops->type, oops->flag & SELECT, &border);
342         if(oops->id== (ID *)((G.scene->basact) ? (G.scene->basact->object) : 0)) line= 1;
343         else if(oops->id== (ID *)G.scene) line= 1;
344
345         if(oops->id->us) {
346                 cpack(body);
347
348                 glRectf(x1,  y1,  x2,  y2);
349         }
350         
351         /* it has never happened that an oops was missing an ID at
352         this point but has occured elseware so lets be safe */
353         if(oops->id && oops->id->lib) { 
354                 if(oops->id->flag & LIB_INDIRECT) cpack(0x1144FF);
355                 else cpack(0x11AAFF);
356
357                 glRectf(x2-0.2*OOPSX,  y2-0.2*OOPSX,  x2-0.1*OOPSX,  y2-0.1*OOPSX);
358         }
359
360         v1[0]= x1; 
361         v1[1]= (y1+y2)/2 -0.3;
362         if(oops->type==ID_LI) {
363                 sprintf(str, "     %s", ((Library *)oops->id)->name);
364         }
365         else {
366                 sprintf(str, "     %s", oops->id->name+2);
367         }
368         calc_oopstext(str, v1);
369
370         /* ICON */
371         if(str[1] && oopscalex>1.1) { /* HAS ICON */
372                 draw_icon_oops(v1, oops->type);
373         } else { /* NO ICON, UNINDENT*/
374                 v1[0] -= 1.3 / oopscalex;
375         }
376         if(oops->flag & SELECT) cpack(0xFFFFFF); 
377         else cpack(0x0);
378         glRasterPos3f(v1[0],  v1[1], 0.0);
379         BMF_DrawString(font, str);
380
381         if(line) setlinestyle(2);
382         cpack(border);
383
384         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
385         glRectf(x1,  y1,  x2,  y2);
386         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
387         if(line) setlinestyle(0);
388
389         /* connection blocks */
390         ol= oops->link.first;
391         while(ol) {
392
393                 f1= x1+ol->xof; 
394                 f2= y1+ol->yof;
395
396                 body= give_oops_color(ol->type, oops->flag & SELECT, &border);
397                 cpack(body);
398
399                 glRectf(f1-.2,  f2-.2,  f1+.2,  f2+.2);
400                 cpack(border);
401
402                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
403
404                 glRectf(f1-.2,  f2-.2,  f1+.2,  f2+.2);
405
406                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
407
408                 ol= ol->next;
409         }
410
411         if(oops->flag & OOPS_REFER) {
412                         /* Draw the little rounded connection point */
413                 glColor3ub(0, 0, 0);
414                 glPushMatrix();
415
416                 glTranslatef(oops->x + 0.5*OOPSX, oops->y, 0.0);
417                 glutil_draw_filled_arc(0.0, M_PI, 0.05*OOPSX, 7);
418
419                 glPopMatrix();
420         }
421 }
422
423 void drawoopsspace(ScrArea *sa, void *spacedata)
424 {
425         SpaceOops *soops= spacedata;
426         Oops *oops;
427         float col[3];
428         
429         BIF_GetThemeColor3fv(TH_BACK, col);
430         glClearColor(col[0], col[1], col[2], 0.0);
431         glClear(GL_COLOR_BUFFER_BIT);
432         if(soops==0) return;    
433         
434         if(soops->type==SO_OUTLINER) draw_outliner(sa, soops);
435         else {
436                 build_oops();   /* changed to become first call... */
437                 
438                 boundbox_oops(0);
439                 calc_scrollrcts(sa, G.v2d, curarea->winx, curarea->winy);
440
441                 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
442
443                 oopscalex= .14*((float)curarea->winx)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
444                 calc_ipogrid(); /* for scrollvariables */
445
446                 /* Set the font size for the oops based on the zoom level */
447                 if (oopscalex > 6.0) font = BMF_GetFont(BMF_kScreen15);
448                 else if (oopscalex > 3.5) font = G.font;
449                 else if (oopscalex > 2.5) font = G.fonts;
450                 else font = G.fontss;           
451                 
452                 /* Draw unselected oops links */
453                 oops= soops->oops.first;
454                 while(oops) {
455                         if(oops->hide==0 && (oops->flag & SELECT)); else {
456                                 draw_oopslink(oops);
457                         }
458                         oops= oops->next;
459                 }
460                 
461                 /* Draw selected oops links */
462                 oops= soops->oops.first;
463                 while(oops) {
464                         if(oops->hide==0 && (oops->flag & SELECT)) {
465                                 draw_oopslink(oops);
466                         }
467                         oops= oops->next;
468                 }                       
469                 
470                 oops= soops->oops.first;
471                 while(oops) {
472                         if(oops->hide==0) {
473                                 if(oops->flag & SELECT); else draw_oops(oops);
474                         }
475                         oops= oops->next;
476                 }
477                 
478                 oops= soops->oops.first;
479                 while(oops) {
480                         if(oops->hide==0) {
481                                 if(oops->flag & SELECT) draw_oops(oops);
482                         }
483                         oops= oops->next;
484                 }
485         }
486         
487         /* restore viewport */
488         mywinset(curarea->win);
489         
490         /* ortho at pixel level curarea */
491         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
492
493         if(soops->type==SO_OUTLINER) {
494                 if(sa->winx>SCROLLB+10 && sa->winy>SCROLLH+10) {
495                         if(G.v2d->scroll) drawscroll(0);                
496                 }
497         }
498         draw_area_emboss(sa);   
499         
500         curarea->win_swap= WIN_BACK_OK;
501 }
502
503
504