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