Bug fix #1893
[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 #ifdef _WIN32
42 #include "BLI_winstuff.h"
43 #endif
44
45 #include "BMF_Api.h"
46
47 #include "BLI_blenlib.h"
48 #include "BLI_arithb.h"
49
50 #include "DNA_ID.h"
51 #include "DNA_object_types.h"
52 #include "DNA_oops_types.h"
53 #include "DNA_scene_types.h"
54 #include "DNA_screen_types.h"
55 #include "DNA_space_types.h"
56 #include "DNA_view2d_types.h"
57
58 #include "BKE_utildefines.h"
59 #include "BKE_global.h"
60
61 #include "BIF_interface.h"
62 #include "BIF_gl.h"
63 #include "BIF_glutil.h"
64 #include "BIF_mywindow.h"
65 #include "BIF_outliner.h"
66 #include "BIF_resources.h"
67 #include "BIF_screen.h"
68
69 /*  #include "BIF_drawoops.h" bad name :(*/
70 #include "BIF_oops.h"
71
72 #include "BSE_drawipo.h"
73 #include "BSE_drawoops.h"
74
75 float oopscalex;
76
77 void boundbox_oops()
78 {
79         Oops *oops;
80         float min[2], max[2];
81         int ok= 0;
82         
83         if(G.soops==0) return;
84         
85         min[0]= 1000.0;
86         max[0]= -10000.0;
87         min[1]= 1000.0;
88         max[1]= -1000.0;
89         
90         oops= G.soops->oops.first;
91         while(oops) {
92                 if(oops->hide==0) {
93                         ok= 1;
94                         
95                         min[0]= MIN2(min[0], oops->x);
96                         max[0]= MAX2(max[0], oops->x+OOPSX);
97                         min[1]= MIN2(min[1], oops->y);
98                         max[1]= MAX2(max[1], oops->y+OOPSY);
99                 }
100                 oops= oops->next;
101         }
102         
103         if(ok==0) return;
104         
105         G.v2d->tot.xmin= min[0];
106         G.v2d->tot.xmax= max[0];
107         G.v2d->tot.ymin= min[1];
108         G.v2d->tot.ymax= max[1];
109         
110 }
111
112 void give_oopslink_line(Oops *oops, OopsLink *ol, float *v1, float *v2)
113 {
114
115         if(ol->to && ol->to->hide==0) {
116                 v1[0]= oops->x+ol->xof;
117                 v1[1]= oops->y+ol->yof;
118                 v2[0]= ol->to->x+OOPSX/2;
119                 v2[1]= ol->to->y;
120         }
121         else if(ol->from && ol->from->hide==0) {
122                 v1[0]= ol->from->x + ol->xof;
123                 v1[1]= ol->from->y + ol->xof;
124                 v2[0]= oops->x+OOPSX/2;
125                 v2[1]= oops->y;
126         }
127 }
128
129 void draw_oopslink(Oops *oops)
130 {
131         OopsLink *ol;
132         float vec[4][3], dist;
133         int a;
134         
135         if(oops->type==ID_SCE) {
136                 if(oops->flag & SELECT) {
137                         if(oops->id->lib) cpack(0x4080A0);
138                         else cpack(0x808080);
139                 }
140                 else cpack(0x606060);
141         }
142         else {
143                 if(oops->flag & SELECT) {
144                         if(oops->id->lib) cpack(0x11AAFF);
145                         else cpack(0xFFFFFF);
146                 }
147                 else cpack(0x0);
148         }
149         
150         glEnable(GL_MAP1_VERTEX_3);
151         vec[0][2]= vec[1][2]= vec[2][2]= vec[3][2]= 0.0;
152         
153         ol= oops->link.first;
154         while(ol) {
155                 if(ol->to && ol->to->hide==0) {
156                         
157                         give_oopslink_line(oops, ol, vec[0], vec[3]);
158                         
159                         dist= 0.5*VecLenf(vec[0], vec[3]);
160
161                         /* check ol->xof and yof for direction */
162                         if(ol->xof == 0.0) {
163                                 vec[1][0]= vec[0][0]-dist;
164                                 vec[1][1]= vec[0][1];
165                         }
166                         else if(ol->xof==OOPSX) {
167                                 vec[1][0]= vec[0][0]+dist;
168                                 vec[1][1]= vec[0][1];
169                         }
170                         else {
171                                 vec[1][0]= vec[0][0];
172                                 vec[1][1]= vec[0][1]+dist;
173                         }
174                         
175                         /* v3 is always pointing down */
176                         vec[2][0]= vec[3][0];
177                         vec[2][1]= vec[3][1] - dist;
178
179                         glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, vec[0]);
180                         
181                         glBegin(GL_LINE_STRIP);
182                         for(a=0; a<=30; a++) {
183                                 glEvalCoord1f((float)a/30.0);
184                         }
185                         glEnd();
186                 }
187                 ol= ol->next;
188         }
189 }
190
191 void draw_icon_oops(float *co, short type)
192 {
193         BIFIconID icon;
194         
195         switch(type) {
196         default: return;
197
198         case ID_OB:     icon= ICON_OBJECT_HLT; break;
199         case ID_ME:     icon= ICON_MESH_HLT; break;
200         case ID_CU:     icon= ICON_CURVE_HLT; break;
201         case ID_MB:     icon= ICON_MBALL_HLT; break;
202         case ID_LT:     icon= ICON_LATTICE_HLT; break;
203         case ID_LA:     icon= ICON_LAMP_HLT; break;
204         case ID_MA:     icon= ICON_MATERIAL_HLT; break;
205         case ID_TE:     icon= ICON_TEXTURE_HLT; break;
206         case ID_IP:     icon= ICON_IPO_HLT; break;
207         case ID_LI:     icon= ICON_LIBRARY_HLT; break;
208         case ID_IM:     icon= ICON_IMAGE_HLT; break;
209         }
210
211         glEnable(GL_BLEND);
212         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
213
214         glRasterPos2f(co[0],  co[1]-0.2);
215         BIF_draw_icon(icon);
216
217         glBlendFunc(GL_ONE,  GL_ZERO); 
218         glDisable(GL_BLEND);
219 }
220
221 void mysbox(float x1, float y1, float x2, float y2)
222 {
223         float vec[2];
224         
225         glBegin(GL_LINE_LOOP);
226         vec[0]= x1; vec[1]= y1;
227         glVertex2fv(vec);
228         vec[0]= x2;
229         glVertex2fv(vec);
230         vec[1]= y2;
231         glVertex2fv(vec);
232         vec[0]= x1;
233         glVertex2fv(vec);
234         glEnd();
235 }
236
237 unsigned int give_oops_color(short type, short sel, unsigned int *border)
238 {
239         unsigned int body;
240         /* also finds out if a dashed line should be drawn */
241
242         switch(type) {
243         case ID_OB:
244                 body= 0x707070; break;
245         case ID_SCE:
246                 body= 0x608060; break;
247         case ID_MA:
248                 body= 0x808060; break;
249         case ID_TE:
250                 body= 0x7080a0; break;
251         case ID_IP:
252                 body= 0x906050; break;
253         case ID_LA:
254                 body= 0x608080; break;
255         case ID_LI:
256                 body= 0x2198DC; break;
257         case ID_IM:
258                 body= 0x35659F; break;
259         default:
260                 body= 0x606070; break;
261         }
262
263         if(sel) {
264                 if(G.moving) *border= 0xf0f0f0;
265                 else *border= 0xc0c0c0; 
266         }
267         else *border= 0x0;
268
269         
270         return body;
271 }
272
273 void calc_oopstext(char *str, float *v1)
274 {
275         float f1, f2, size;
276         short mval[2], len, flen;
277         
278         ipoco_to_areaco_noclip(G.v2d, v1, mval);
279         f1= mval[0];
280         v1[0]+= OOPSX;
281         ipoco_to_areaco_noclip(G.v2d, v1, mval);
282         f2= mval[0];
283         size= f2-f1;
284
285         len= strlen(str);
286         
287         while( (flen= BMF_GetStringWidth(G.fonts, str)) > size) {
288                 if(flen < 10 || len<2) break;
289                 len--;
290                 str[len]= 0;
291         }
292         
293         mval[0]= (f1+f2-flen+1)/2;
294         mval[1]= 1;
295         areamouseco_to_ipoco(G.v2d, mval, &f1, &f2);
296         
297         v1[0]= f1;
298         
299 }
300
301 void draw_oops(Oops *oops)
302 {
303         OopsLink *ol;
304         float v1[2], x1, y1, x2, y2, f1, f2;
305         unsigned int body, border;
306         short line= 0;
307         char str[32];
308
309         x1= oops->x; 
310         x2= oops->x+OOPSX;
311         y1= oops->y; 
312         y2= oops->y+OOPSY;
313
314         if(x2 < G.v2d->cur.xmin || x1 > G.v2d->cur.xmax) return;
315         if(y2 < G.v2d->cur.ymin || y1 > G.v2d->cur.ymax) return;
316
317         body= give_oops_color(oops->type, oops->flag & SELECT, &border);
318         if(oops->id== (ID *)((G.scene->basact) ? (G.scene->basact->object) : 0)) line= 1;
319         else if(oops->id== (ID *)G.scene) line= 1;
320
321         if(oops->id->us) {
322                 cpack(body);
323
324                 glRectf(x1,  y1,  x2,  y2);
325         }
326         if(oops->id->lib) {
327                 if(oops->id->flag & LIB_INDIRECT) cpack(0x1144FF);
328                 else cpack(0x11AAFF);
329
330                 glRectf(x2-0.2*OOPSX,  y2-0.2*OOPSX,  x2-0.1*OOPSX,  y2-0.1*OOPSX);
331         }
332
333         v1[0]= x1; 
334         v1[1]= (y1+y2)/2 -0.3;
335         if(oops->type==ID_LI) {
336                 sprintf(str, "     %s", ((Library *)oops->id)->name);
337         }
338         else {
339                 sprintf(str, "     %s", oops->id->name+2);
340         }
341         calc_oopstext(str, v1);
342
343         /* ICON */
344         if(str[1] && oopscalex>1.1) {
345                 draw_icon_oops(v1, oops->type);
346         }
347         if(oops->flag & SELECT) cpack(0xFFFFFF); 
348         else cpack(0x0);
349         glRasterPos3f(v1[0],  v1[1], 0.0);
350         BMF_DrawString(G.fonts, str);
351
352         if(line) setlinestyle(2);
353         cpack(border);
354
355         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
356         glRectf(x1,  y1,  x2,  y2);
357         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
358         if(line) setlinestyle(0);
359
360         /* connection blocks */
361         ol= oops->link.first;
362         while(ol) {
363
364                 f1= x1+ol->xof; 
365                 f2= y1+ol->yof;
366
367                 body= give_oops_color(ol->type, oops->flag & SELECT, &border);
368                 cpack(body);
369
370                 glRectf(f1-.2,  f2-.2,  f1+.2,  f2+.2);
371                 cpack(border);
372
373                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
374
375                 glRectf(f1-.2,  f2-.2,  f1+.2,  f2+.2);
376
377                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
378
379                 ol= ol->next;
380         }
381
382         if(oops->flag & OOPS_REFER) {
383                         /* Draw the little rounded connection point */
384                 glColor3ub(0, 0, 0);
385                 glPushMatrix();
386
387                 glTranslatef(oops->x + 0.5*OOPSX, oops->y, 0.0);
388                 glutil_draw_filled_arc(0.0, M_PI, 0.05*OOPSX, 7);
389
390                 glPopMatrix();
391         }
392 }
393
394 void drawoopsspace(ScrArea *sa, void *spacedata)
395 {
396         SpaceOops *soops= spacedata;
397         Oops *oops;
398         float col[3];
399         
400         BIF_GetThemeColor3fv(TH_BACK, col);
401         glClearColor(col[0], col[1], col[2], 0.0);
402         glClear(GL_COLOR_BUFFER_BIT);
403         if(soops==0) return;    
404         
405         if(soops->type==SO_OUTLINER) draw_outliner(sa, soops);
406         else {
407                 boundbox_oops();
408                 calc_scrollrcts(G.v2d, curarea->winx, curarea->winy);
409
410                 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
411
412                 oopscalex= .14*((float)curarea->winx)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
413                 calc_ipogrid(); /* for scrollvariables */
414                 build_oops();
415
416                 oops= soops->oops.first;
417                 while(oops) {
418                         if(oops->hide==0) {
419                                 draw_oopslink(oops);
420                         }
421                         oops= oops->next;
422                 }
423                 oops= soops->oops.first;
424                 while(oops) {
425                         if(oops->hide==0) {
426                                 if(oops->flag & SELECT); else draw_oops(oops);
427                         }
428                         oops= oops->next;
429                 }
430                 oops= soops->oops.first;
431                 while(oops) {
432                         if(oops->hide==0) {
433                                 if(oops->flag & SELECT) draw_oops(oops);
434                         }
435                         oops= oops->next;
436                 }
437         }
438         
439         /* restore viewport */
440         mywinset(curarea->win);
441         
442         /* ortho at pixel level curarea */
443         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
444
445         if(soops->type==SO_OUTLINER) {
446                 if(sa->winx>SCROLLB+10 && sa->winy>SCROLLH+10) {
447                         if(G.v2d->scroll) drawscroll(0);                
448                 }
449         }
450         draw_area_emboss(sa);   
451         
452         curarea->win_swap= WIN_BACK_OK;
453 }
454
455
456