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