- themed all window types... phew!
[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 #include "BLI_editVert.h"
50
51 #include "DNA_ID.h"
52 #include "DNA_mesh_types.h"
53 #include "DNA_object_types.h"
54 #include "DNA_oops_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_screen_types.h"
57 #include "DNA_space_types.h"
58 #include "DNA_view2d_types.h"
59
60 #include "BKE_utildefines.h"
61 #include "BKE_global.h"
62
63 #include "BIF_gl.h"
64 #include "BIF_interface.h"
65 #include "BIF_glutil.h"
66 #include "BIF_resources.h"
67 #include "BIF_screen.h"
68 #include "BIF_mywindow.h"
69 #include "BIF_resources.h"
70
71 /*  #include "BIF_drawoops.h" bad name :(*/
72 #include "BIF_oops.h"
73
74 #include "BSE_drawipo.h"
75 #include "BSE_drawoops.h"
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];
135         
136         if(oops->type==ID_SCE) {
137                 if(oops->flag & SELECT) {
138                         if(oops->id->lib) cpack(0x4080A0);
139                         else cpack(0x808080);
140                 }
141                 else cpack(0x606060);
142         }
143         else {
144                 if(oops->flag & SELECT) {
145                         if(oops->id->lib) cpack(0x11AAFF);
146                         else cpack(0xFFFFFF);
147                 }
148                 else cpack(0x0);
149         }
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, vec+2);
156                         
157                         glBegin(GL_LINE_STRIP);
158                         glVertex2fv(vec);
159                         glVertex2fv(vec+2);
160                         glEnd();
161                 }
162                 ol= ol->next;
163         }
164 }
165
166 void draw_icon_oops(float *co, short type)
167 {
168         BIFIconID icon;
169         
170         switch(type) {
171         default: return;
172
173         case ID_OB:     icon= ICON_OBJECT_HLT; break;
174         case ID_ME:     icon= ICON_MESH_HLT; break;
175         case ID_CU:     icon= ICON_CURVE_HLT; break;
176         case ID_MB:     icon= ICON_MBALL_HLT; break;
177         case ID_LT:     icon= ICON_LATTICE_HLT; break;
178         case ID_LA:     icon= ICON_LAMP_HLT; break;
179         case ID_MA:     icon= ICON_MATERIAL_HLT; break;
180         case ID_TE:     icon= ICON_TEXTURE_HLT; break;
181         case ID_IP:     icon= ICON_IPO_HLT; break;
182         case ID_LI:     icon= ICON_LIBRARY_HLT; break;
183         case ID_IM:     icon= ICON_IMAGE_HLT; break;
184         }
185
186         glEnable(GL_BLEND);
187         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
188
189         glRasterPos2f(co[0],  co[1]-0.2);
190         BIF_draw_icon(icon);
191
192         glBlendFunc(GL_ONE,  GL_ZERO); 
193         glDisable(GL_BLEND);
194 }
195
196 void mysbox(float x1, float y1, float x2, float y2)
197 {
198         float vec[2];
199         
200         glBegin(GL_LINE_LOOP);
201         vec[0]= x1; vec[1]= y1;
202         glVertex2fv(vec);
203         vec[0]= x2;
204         glVertex2fv(vec);
205         vec[1]= y2;
206         glVertex2fv(vec);
207         vec[0]= x1;
208         glVertex2fv(vec);
209         glEnd();
210 }
211
212 unsigned int give_oops_color(short type, short sel, unsigned int *border)
213 {
214         unsigned int body;
215         /* also finds out if a dashed line should be drawn */
216
217         switch(type) {
218         case ID_OB:
219                 body= 0x707070; break;
220         case ID_SCE:
221                 body= 0x608060; break;
222         case ID_MA:
223                 body= 0x808060; break;
224         case ID_TE:
225                 body= 0x7080a0; break;
226         case ID_IP:
227                 body= 0x906050; break;
228         case ID_LA:
229                 body= 0x608080; break;
230         case ID_LI:
231                 body= 0x2198DC; break;
232         case ID_IM:
233                 body= 0x35659F; break;
234         default:
235                 body= 0x606070; break;
236         }
237
238         if(sel) {
239                 if(G.moving) *border= 0xf0f0f0;
240                 else *border= 0xc0c0c0; 
241         }
242         else *border= 0x0;
243
244         
245         return body;
246 }
247
248 void calc_oopstext(char *str, float *v1)
249 {
250         float f1, f2, size;
251         short mval[2], len, flen;
252         
253         ipoco_to_areaco_noclip(G.v2d, v1, mval);
254         f1= mval[0];
255         v1[0]+= OOPSX;
256         ipoco_to_areaco_noclip(G.v2d, v1, mval);
257         f2= mval[0];
258         size= f2-f1;
259
260         len= strlen(str);
261         
262         while( (flen= BMF_GetStringWidth(G.fonts, str)) > size) {
263                 if(flen < 10 || len<2) break;
264                 len--;
265                 str[len]= 0;
266         }
267         
268         mval[0]= (f1+f2-flen+1)/2;
269         mval[1]= 1;
270         areamouseco_to_ipoco(G.v2d, mval, &f1, &f2);
271         
272         v1[0]= f1;
273         
274 }
275
276 void draw_oops(Oops *oops)
277 {
278         OopsLink *ol;
279         float v1[2], x1, y1, x2, y2, f1, f2;
280         unsigned int body, border;
281         short line= 0;
282         char str[32];
283
284         x1= oops->x; 
285         x2= oops->x+OOPSX;
286         y1= oops->y; 
287         y2= oops->y+OOPSY;
288
289         if(x2 < G.v2d->cur.xmin || x1 > G.v2d->cur.xmax) return;
290         if(y2 < G.v2d->cur.ymin || y1 > G.v2d->cur.ymax) return;
291
292         body= give_oops_color(oops->type, oops->flag & SELECT, &border);
293         if(oops->id== (ID *)((G.scene->basact) ? (G.scene->basact->object) : 0)) line= 1;
294         else if(oops->id== (ID *)G.scene) line= 1;
295
296         if(oops->id->us) {
297                 cpack(body);
298
299                 glRectf(x1,  y1,  x2,  y2);
300         }
301         if(oops->id->lib) {
302                 if(oops->id->flag & LIB_INDIRECT) cpack(0x1144FF);
303                 else cpack(0x11AAFF);
304
305                 glRectf(x2-0.2*OOPSX,  y2-0.2*OOPSX,  x2-0.1*OOPSX,  y2-0.1*OOPSX);
306         }
307
308         v1[0]= x1; 
309         v1[1]= (y1+y2)/2 -0.3;
310         if(oops->type==ID_LI) {
311                 sprintf(str, "     %s", ((Library *)oops->id)->name);
312         }
313         else {
314                 sprintf(str, "     %s", oops->id->name+2);
315         }
316         calc_oopstext(str, v1);
317
318         /* ICON */
319         if(str[1] && oopscalex>1.1) {
320                 draw_icon_oops(v1, oops->type);
321         }
322         if(oops->flag & SELECT) cpack(0xFFFFFF); 
323         else cpack(0x0);
324         glRasterPos3f(v1[0],  v1[1], 0.0);
325         BMF_DrawString(G.fonts, str);
326
327         if(line) setlinestyle(2);
328         cpack(border);
329
330         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
331         glRectf(x1,  y1,  x2,  y2);
332         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
333         if(line) setlinestyle(0);
334
335         /* connection blocks */
336         ol= oops->link.first;
337         while(ol) {
338
339                 f1= x1+ol->xof; 
340                 f2= y1+ol->yof;
341
342                 body= give_oops_color(ol->type, oops->flag & SELECT, &border);
343                 cpack(body);
344
345                 glRectf(f1-.2,  f2-.2,  f1+.2,  f2+.2);
346                 cpack(border);
347
348                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
349
350                 glRectf(f1-.2,  f2-.2,  f1+.2,  f2+.2);
351
352                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
353
354                 ol= ol->next;
355         }
356
357         if(oops->flag & OOPS_REFER) {
358                         /* Draw the little rounded connection point */
359                 glColor3ub(0, 0, 0);
360                 glPushMatrix();
361
362                 glTranslatef(oops->x + 0.5*OOPSX, oops->y, 0.0);
363                 glutil_draw_filled_arc(0.0, M_PI, 0.05*OOPSX, 7);
364
365                 glPopMatrix();
366         }
367 }
368
369 void drawoopsspace(ScrArea *sa, void *spacedata)
370 {
371         Oops *oops;
372         float col[3];
373         int ofsx, ofsy;
374         
375         BIF_GetThemeColor3fv(TH_BACK, col);
376         glClearColor(col[0], col[1], col[2], 0.0);
377         glClear(GL_COLOR_BUFFER_BIT);
378         if(G.soops==0) return;  
379
380         boundbox_oops();
381         calc_scrollrcts(G.v2d, curarea->winx, curarea->winy);
382
383         if(curarea->winx>SCROLLB+10 && curarea->winy>SCROLLH+10) {
384                 if(G.v2d->scroll) {     
385                         ofsx= curarea->winrct.xmin;     /* because of mywin */
386                         ofsy= curarea->winrct.ymin;
387
388                         glViewport(ofsx+G.v2d->mask.xmin,  ofsy+G.v2d->mask.ymin, ( ofsx+G.v2d->mask.xmax-1)-(ofsx+G.v2d->mask.xmin)+1, ( ofsy+G.v2d->mask.ymax-1)-( ofsy+G.v2d->mask.ymin)+1); 
389                         glScissor(ofsx+G.v2d->mask.xmin,  ofsy+G.v2d->mask.ymin, ( ofsx+G.v2d->mask.xmax-1)-(ofsx+G.v2d->mask.xmin)+1, ( ofsy+G.v2d->mask.ymax-1)-( ofsy+G.v2d->mask.ymin)+1);
390                 }
391         }
392
393         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
394
395         oopscalex= .14*((float)curarea->winx)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
396         calc_ipogrid(); /* for scrollvariables */
397         build_oops();
398
399         oops= G.soops->oops.first;
400         while(oops) {
401                 if(oops->hide==0) {
402                         draw_oopslink(oops);
403                 }
404                 oops= oops->next;
405         }
406         oops= G.soops->oops.first;
407         while(oops) {
408                 if(oops->hide==0) {
409                         if(oops->flag & SELECT); else draw_oops(oops);
410                 }
411                 oops= oops->next;
412         }
413         oops= G.soops->oops.first;
414         while(oops) {
415                 if(oops->hide==0) {
416                         if(oops->flag & SELECT) draw_oops(oops);
417                 }
418                 oops= oops->next;
419         }
420         
421         /* restore viewport */
422         mywinset(curarea->win);
423
424         
425         /* ortho at pixel level curarea */
426         myortho2(-0.5, sa->winx-0.5, -0.5, sa->winy-0.5);
427         if(G.v2d->scroll) {     
428                 drawscroll(0);          
429         }
430         draw_area_emboss(sa);   
431         
432         curarea->win_swap= WIN_BACK_OK;
433 }
434
435
436