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