467e4e84674371c91e04f65660f934c9cb1c4501
[blender.git] / source / blender / src / drawview.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 //#define NAN_LINEAR_PHYSICS
34
35 #include <math.h>
36 #include <string.h>
37
38 #ifndef WIN32
39 #include <unistd.h>
40 #include <sys/times.h>
41 #else
42 #include <io.h>
43 #endif   
44
45 #include "MEM_guardedalloc.h"
46
47 #include "PIL_time.h"
48
49 #include "BMF_Api.h"
50
51 #include "IMB_imbuf_types.h"
52 #include "IMB_imbuf.h"
53
54 #include "DNA_action_types.h"
55 #include "DNA_armature_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_constraint_types.h"
58 #include "DNA_curve_types.h"
59 #include "DNA_group_types.h"
60 #include "DNA_image_types.h"
61 #include "DNA_lattice_types.h"
62 #include "DNA_mesh_types.h"
63 #include "DNA_meshdata_types.h"
64 #include "DNA_meta_types.h"
65 #include "DNA_object_types.h"
66 #include "DNA_screen_types.h"
67 #include "DNA_scene_types.h"
68 #include "DNA_space_types.h"
69 #include "DNA_texture_types.h"
70 #include "DNA_userdef_types.h"
71 #include "DNA_view3d_types.h"
72 #include "DNA_world_types.h"
73
74 #include "BLI_blenlib.h"
75 #include "BLI_arithb.h"
76 #include "BLI_editVert.h"
77
78 #include "BKE_action.h"
79 #include "BKE_armature.h"
80 #include "BKE_anim.h"
81 #include "BKE_constraint.h"
82 #include "BKE_curve.h"
83 #include "BKE_displist.h"
84 #include "BKE_depsgraph.h"
85 #include "BKE_global.h"
86 #include "BKE_lattice.h"
87 #include "BKE_library.h"
88 #include "BKE_image.h"
89 #include "BKE_ipo.h"
90 #include "BKE_key.h"
91 #include "BKE_main.h"
92 #include "BKE_object.h"
93 #include "BKE_texture.h"
94 #include "BKE_utildefines.h"
95
96 #include "BIF_butspace.h"
97 #include "BIF_drawimage.h"
98 #include "BIF_editgroup.h"
99 #include "BIF_editarmature.h"
100 #include "BIF_editmesh.h"
101 #include "BIF_gl.h"
102 #include "BIF_glutil.h"
103 #include "BIF_interface.h"
104 #include "BIF_interface_icons.h"
105 #include "BIF_mywindow.h"
106 #include "BIF_poseobject.h"
107 #include "BIF_previewrender.h"
108 #include "BIF_resources.h"
109 #include "BIF_screen.h"
110 #include "BIF_space.h"
111
112 #include "BDR_drawmesh.h"
113 #include "BDR_drawobject.h"
114 #include "BDR_editobject.h"
115 #include "BDR_vpaint.h"
116
117 #include "BSE_drawview.h"
118 #include "BSE_filesel.h"
119 #include "BSE_headerbuttons.h"
120 #include "BSE_seqaudio.h"
121 #include "BSE_trans_types.h"
122 #include "BSE_time.h"
123 #include "BSE_view.h"
124
125 #include "BPY_extern.h"
126
127 #include "blendef.h"
128 #include "mydevice.h"
129 #include "butspace.h"  // event codes
130
131 #include "BIF_transform.h"
132
133 /* Modules used */
134 #include "radio.h"
135
136 /* locals */
137 void drawname(Object *ob);
138 void star_stuff_init_func(void);
139 void star_stuff_vertex_func(float* i);
140 void star_stuff_term_func(void);
141
142 void star_stuff_init_func(void)
143 {
144         cpack(-1);
145         glPointSize(1.0);
146         glBegin(GL_POINTS);
147 }
148 void star_stuff_vertex_func(float* i)
149 {
150         glVertex3fv(i);
151 }
152 void star_stuff_term_func(void)
153 {
154         glEnd();
155 }
156
157 void setalpha_bgpic(BGpic *bgpic)
158 {
159         int x, y, alph;
160         char *rect;
161         
162         alph= (int)(255.0*(1.0-bgpic->blend));
163         
164         rect= (char *)bgpic->rect;
165         for(y=0; y< bgpic->yim; y++) {
166                 for(x= bgpic->xim; x>0; x--, rect+=4) {
167                         rect[3]= alph;
168                 }
169         }
170 }
171
172
173 void default_gl_light(void)
174 {
175         int a;
176         
177         /* initialize */
178         if(U.light[0].flag==0 && U.light[1].flag==0 && U.light[2].flag==0) {
179                 U.light[0].flag= 1;
180                 U.light[0].vec[0]= -0.3; U.light[0].vec[1]= 0.3; U.light[0].vec[2]= 0.9;
181                 U.light[0].col[0]= 0.8; U.light[0].col[1]= 0.8; U.light[0].col[2]= 0.8;
182                 U.light[0].spec[0]= 0.5; U.light[0].spec[1]= 0.5; U.light[0].spec[2]= 0.5;
183                 U.light[0].spec[3]= 1.0;
184                 
185                 U.light[1].flag= 0;
186                 U.light[1].vec[0]= 0.5; U.light[1].vec[1]= 0.5; U.light[1].vec[2]= 0.1;
187                 U.light[1].col[0]= 0.4; U.light[1].col[1]= 0.4; U.light[1].col[2]= 0.8;
188                 U.light[1].spec[0]= 0.3; U.light[1].spec[1]= 0.3; U.light[1].spec[2]= 0.5;
189                 U.light[1].spec[3]= 1.0;
190         
191                 U.light[2].flag= 0;
192                 U.light[2].vec[0]= 0.3; U.light[2].vec[1]= -0.3; U.light[2].vec[2]= -0.2;
193                 U.light[2].col[0]= 0.8; U.light[2].col[1]= 0.5; U.light[2].col[2]= 0.4;
194                 U.light[2].spec[0]= 0.5; U.light[2].spec[1]= 0.4; U.light[2].spec[2]= 0.3;
195                 U.light[2].spec[3]= 1.0;
196         }
197         
198
199         glLightfv(GL_LIGHT0, GL_POSITION, U.light[0].vec); 
200         glLightfv(GL_LIGHT0, GL_DIFFUSE, U.light[0].col); 
201         glLightfv(GL_LIGHT0, GL_SPECULAR, U.light[0].spec); 
202
203         glLightfv(GL_LIGHT1, GL_POSITION, U.light[1].vec); 
204         glLightfv(GL_LIGHT1, GL_DIFFUSE, U.light[1].col); 
205         glLightfv(GL_LIGHT1, GL_SPECULAR, U.light[1].spec); 
206
207         glLightfv(GL_LIGHT2, GL_POSITION, U.light[2].vec); 
208         glLightfv(GL_LIGHT2, GL_DIFFUSE, U.light[2].col); 
209         glLightfv(GL_LIGHT2, GL_SPECULAR, U.light[2].spec); 
210
211         for(a=0; a<8; a++) {
212                 if(a<3) {
213                         if(U.light[a].flag) glEnable(GL_LIGHT0+a);
214                         else glDisable(GL_LIGHT0+a);
215                         
216                         // clear stuff from other opengl lamp usage
217                         glLightf(GL_LIGHT0+a, GL_SPOT_CUTOFF, 180.0);
218                         glLightf(GL_LIGHT0+a, GL_CONSTANT_ATTENUATION, 1.0);
219                         glLightf(GL_LIGHT0+a, GL_LINEAR_ATTENUATION, 0.0);
220                 }
221                 else glDisable(GL_LIGHT0+a);
222         }
223         
224         glDisable(GL_LIGHTING);
225
226         glDisable(GL_COLOR_MATERIAL);
227 }
228
229 /* also called when render 'ogl' */
230 void init_gl_stuff(void)        
231 {
232         float mat_ambient[] = { 0.0, 0.0, 0.0, 0.0 };
233         float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
234         float mat_shininess[] = { 35.0 };
235         int a, x, y;
236         GLubyte pat[32*32];
237         const GLubyte *patc= pat;
238         
239         
240         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
241         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular);
242         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
243         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
244
245         default_gl_light();
246         
247         /* no local viewer, looks ugly in ortho mode */
248         /* glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, &one); */
249         
250         glDepthFunc(GL_LEQUAL);
251         /* scaling matrices */
252         glEnable(GL_NORMALIZE);
253
254         glShadeModel(GL_FLAT);
255
256         glDisable(GL_ALPHA_TEST);
257         glDisable(GL_BLEND);
258         glDisable(GL_DEPTH_TEST);
259         glDisable(GL_FOG);
260         glDisable(GL_LIGHTING);
261         glDisable(GL_LOGIC_OP);
262         glDisable(GL_STENCIL_TEST);
263         glDisable(GL_TEXTURE_1D);
264         glDisable(GL_TEXTURE_2D);
265
266         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
267         glPixelTransferi(GL_RED_SCALE, 1);
268         glPixelTransferi(GL_RED_BIAS, 0);
269         glPixelTransferi(GL_GREEN_SCALE, 1);
270         glPixelTransferi(GL_GREEN_BIAS, 0);
271         glPixelTransferi(GL_BLUE_SCALE, 1);
272         glPixelTransferi(GL_BLUE_BIAS, 0);
273         glPixelTransferi(GL_ALPHA_SCALE, 1);
274         glPixelTransferi(GL_ALPHA_BIAS, 0);
275         
276         glPixelTransferi(GL_DEPTH_BIAS, 0);
277         glPixelTransferi(GL_DEPTH_SCALE, 1);
278         glDepthRange(0.0, 1.0);
279         
280         a= 0;
281         for(x=0; x<32; x++) {
282                 for(y=0; y<4; y++) {
283                         if( (x) & 1) pat[a++]= 0x88;
284                         else pat[a++]= 0x22;
285                 }
286         }
287         
288         glPolygonStipple(patc);
289
290
291         init_realtime_GL();     
292 }
293
294 void circf(float x, float y, float rad)
295 {
296         GLUquadricObj *qobj = gluNewQuadric(); 
297         
298         gluQuadricDrawStyle(qobj, GLU_FILL); 
299         
300         glPushMatrix(); 
301         
302         glTranslatef(x,  y, 0.); 
303         
304         gluDisk( qobj, 0.0,  rad, 32, 1); 
305         
306         glPopMatrix(); 
307         
308         gluDeleteQuadric(qobj);
309 }
310
311 void circ(float x, float y, float rad)
312 {
313         GLUquadricObj *qobj = gluNewQuadric(); 
314         
315         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
316         
317         glPushMatrix(); 
318         
319         glTranslatef(x,  y, 0.); 
320         
321         gluDisk( qobj, 0.0,  rad, 32, 1); 
322         
323         glPopMatrix(); 
324         
325         gluDeleteQuadric(qobj);
326 }
327
328 /* **********  ********** */
329
330 static void draw_bgpic(void)
331 {
332         BGpic *bgpic;
333         Image *ima;
334         float vec[4], fac, asp, zoomx, zoomy;
335         float x1, y1, x2, y2, cx, cy;
336         
337         bgpic= G.vd->bgpic;
338         if(bgpic==0) return;
339         
340         if(bgpic->tex) {
341 //              init_render_texture(bgpic->tex);
342                 free_unused_animimages();
343                 ima= bgpic->tex->ima;
344         }
345         else {
346                 ima= bgpic->ima;
347         }
348         
349         if(ima==0) return;
350         if(ima->ok==0) return;
351
352         tag_image_time(ima);
353         
354         /* test for image */
355         if(ima->ibuf==0) {
356         
357                 if(bgpic->rect) MEM_freeN(bgpic->rect);
358                 bgpic->rect= 0;
359                 
360                 if(bgpic->tex) {
361                         ima_ibuf_is_nul(bgpic->tex, bgpic->tex->ima);
362                 }
363                 else {
364                         waitcursor(1);
365                         load_image(ima, IB_rect, G.sce, G.scene->r.cfra);
366                         waitcursor(0);
367                 }
368                 if(ima->ibuf==0) {
369                         ima->ok= 0;
370                         return;
371                 }
372         }
373
374         if(bgpic->rect==0) {
375                 
376                 bgpic->rect= MEM_dupallocN(ima->ibuf->rect);
377                 bgpic->xim= ima->ibuf->x;
378                 bgpic->yim= ima->ibuf->y;
379                 setalpha_bgpic(bgpic);
380         }
381
382         if(G.vd->persp==2) {
383                 rcti vb;
384
385                 calc_viewborder(G.vd, &vb);
386
387                 x1= vb.xmin;
388                 y1= vb.ymin;
389                 x2= vb.xmax;
390                 y2= vb.ymax;
391         }
392         else {
393                 float sco[2];
394
395                 /* calc window coord */
396                 initgrabz(0.0, 0.0, 0.0);
397                 window_to_3d(vec, 1, 0);
398                 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
399                 fac= 1.0/fac;
400         
401                 asp= ( (float)ima->ibuf->y)/(float)ima->ibuf->x;
402
403                 vec[0] = vec[1] = vec[2] = 0.0;
404                 view3d_project_float(curarea, vec, sco, G.vd->persmat);
405                 cx = sco[0];
406                 cy = sco[1];
407         
408                 x1=  cx+ fac*(bgpic->xof-bgpic->size);
409                 y1=  cy+ asp*fac*(bgpic->yof-bgpic->size);
410                 x2=  cx+ fac*(bgpic->xof+bgpic->size);
411                 y2=  cy+ asp*fac*(bgpic->yof+bgpic->size);
412         }
413         
414         /* complete clip? */
415         
416         if(x2 < 0 ) return;
417         if(y2 < 0 ) return;
418         if(x1 > curarea->winx ) return;
419         if(y1 > curarea->winy ) return;
420         
421         zoomx= (x2-x1)/ima->ibuf->x;
422         zoomy= (y2-y1)/ima->ibuf->y;
423
424         glEnable(GL_BLEND);
425         if(G.vd->zbuf) glDisable(GL_DEPTH_TEST);
426
427         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
428          
429         glMatrixMode(GL_PROJECTION);
430         glPushMatrix();
431         glMatrixMode(GL_MODELVIEW);
432         glPushMatrix();
433         
434         glaDefine2DArea(&curarea->winrct);
435         glPixelZoom(zoomx, zoomy);
436         glaDrawPixelsSafe(x1, y1, ima->ibuf->x, ima->ibuf->y, ima->ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, bgpic->rect);
437         glPixelZoom(1.0, 1.0);
438
439         glMatrixMode(GL_PROJECTION);
440         glPopMatrix();
441         glMatrixMode(GL_MODELVIEW);
442         glPopMatrix();
443         
444         glBlendFunc(GL_ONE,  GL_ZERO); 
445         glDisable(GL_BLEND);
446         if(G.vd->zbuf) glEnable(GL_DEPTH_TEST);
447         
448         areawinset(curarea->win);       // restore viewport / scissor
449 }
450
451 static void drawgrid_draw(float wx, float wy, float x, float y, float dx)
452 {
453         float fx, fy;
454         
455         x+= (wx); 
456         y+= (wy);
457         fx= x/dx;
458         fx= x-dx*floor(fx);
459         
460         while(fx< curarea->winx) {
461                 fdrawline(fx,  0.0,  fx,  (float)curarea->winy); 
462                 fx+= dx; 
463         }
464
465         fy= y/dx;
466         fy= y-dx*floor(fy);
467         
468
469         while(fy< curarea->winy) {
470                 fdrawline(0.0,  fy,  (float)curarea->winx,  fy); 
471                 fy+= dx;
472         }
473
474 }
475
476 // not intern, called in editobject for constraint axis too
477 void make_axis_color(char *col, char *col2, char axis)
478 {
479         if(axis=='x') {
480                 col2[0]= col[0]>219?255:col[0]+36;
481                 col2[1]= col[1]<26?0:col[1]-26;
482                 col2[2]= col[2]<26?0:col[2]-26;
483         }
484         else if(axis=='y') {
485                 col2[0]= col[0]<46?0:col[0]-36;
486                 col2[1]= col[1]>189?255:col[1]+66;
487                 col2[2]= col[2]<46?0:col[2]-36; 
488         }
489         else {
490                 col2[0]= col[0]<26?0:col[0]-26; 
491                 col2[1]= col[1]<26?0:col[1]-26; 
492                 col2[2]= col[2]>209?255:col[2]+46;
493         }
494         
495 }
496
497 static void drawgrid(void)
498 {
499         /* extern short bgpicmode; */
500         float wx, wy, x, y, fw, fx, fy, dx;
501         float vec4[4];
502         char col[3], col2[3];
503         
504         vec4[0]=vec4[1]=vec4[2]=0.0; 
505         vec4[3]= 1.0;
506         Mat4MulVec4fl(G.vd->persmat, vec4);
507         fx= vec4[0]; 
508         fy= vec4[1]; 
509         fw= vec4[3];
510
511         wx= (curarea->winx/2.0);        /* because of rounding errors, grid at wrong location */
512         wy= (curarea->winy/2.0);
513
514         x= (wx)*fx/fw;
515         y= (wy)*fy/fw;
516
517         vec4[0]=vec4[1]=G.vd->grid; 
518         vec4[2]= 0.0;
519         vec4[3]= 1.0;
520         Mat4MulVec4fl(G.vd->persmat, vec4);
521         fx= vec4[0]; 
522         fy= vec4[1]; 
523         fw= vec4[3];
524
525         dx= fabs(x-(wx)*fx/fw);
526         if(dx==0) dx= fabs(y-(wy)*fy/fw);
527         
528         glDepthMask(0);         // disable write in zbuffer
529
530         /* check zoom out */
531         BIF_ThemeColor(TH_GRID);
532         persp(PERSP_WIN);
533         
534         if(dx<6.0) {
535                 G.vd->gridview*= 10.0;
536                 dx*= 10.0;
537                 
538                 if(dx<6.0) {    
539                         G.vd->gridview*= 10.0;
540                         dx*= 10.0;
541                         
542                         if(dx<6.0) {
543                                 G.vd->gridview*= 10.0;
544                                 dx*=10;
545                                 if(dx<6.0);
546                                 else {
547                                         BIF_ThemeColor(TH_GRID);
548                                         drawgrid_draw(wx, wy, x, y, dx);
549                                 }
550                         }
551                         else {  // start blending out
552                                 BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
553                                 drawgrid_draw(wx, wy, x, y, dx);
554                         
555                                 BIF_ThemeColor(TH_GRID);
556                                 drawgrid_draw(wx, wy, x, y, 10*dx);
557                         }
558                 }
559                 else {  // start blending out (6 < dx < 60)
560                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
561                         drawgrid_draw(wx, wy, x, y, dx);
562                         
563                         BIF_ThemeColor(TH_GRID);
564                         drawgrid_draw(wx, wy, x, y, 10*dx);
565                 }
566         }
567         else {
568                 if(dx>60.0) {           // start blending in
569                         G.vd->gridview/= 10.0;
570                         dx/= 10.0;                      
571                         if(dx>60.0) {           // start blending in
572                                 G.vd->gridview/= 10.0;
573                                 dx/= 10.0;
574                                 if(dx>60.0) {
575                                         BIF_ThemeColor(TH_GRID);
576                                         drawgrid_draw(wx, wy, x, y, dx);
577                                 }
578                                 else {
579                                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
580                                         drawgrid_draw(wx, wy, x, y, dx);
581                                         BIF_ThemeColor(TH_GRID);
582                                         drawgrid_draw(wx, wy, x, y, dx*10);
583                                 }
584                         }
585                         else {
586                                 BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
587                                 drawgrid_draw(wx, wy, x, y, dx);
588                                 BIF_ThemeColor(TH_GRID);                                
589                                 drawgrid_draw(wx, wy, x, y, dx*10);
590                         }
591                 }
592                 else {
593                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
594                         drawgrid_draw(wx, wy, x, y, dx);
595                         BIF_ThemeColor(TH_GRID);
596                         drawgrid_draw(wx, wy, x, y, dx*10);
597                 }
598         }
599
600         x+= (wx); 
601         y+= (wy);
602         BIF_GetThemeColor3ubv(TH_GRID, col);
603
604         setlinestyle(0);
605         
606         /* center cross */
607         if(G.vd->view==3) make_axis_color(col, col2, 'y');
608         else make_axis_color(col, col2, 'x');
609         glColor3ubv(col2);
610         
611         fdrawline(0.0,  y,  (float)curarea->winx,  y); 
612         
613         if(G.vd->view==7) make_axis_color(col, col2, 'y');
614         else make_axis_color(col, col2, 'z');
615         glColor3ubv(col2);
616
617         fdrawline(x, 0.0, x, (float)curarea->winy); 
618
619         glDepthMask(1);         // enable write in zbuffer
620         persp(PERSP_VIEW);
621 }
622
623
624 static void drawfloor(void)
625 {
626         View3D *vd;
627         float vert[3], grid;
628         int a, gridlines;
629         char col[3], col2[3];
630         short draw_line = 0;
631                 
632         vd= curarea->spacedata.first;
633
634         vert[2]= 0.0;
635
636         if(vd->gridlines<3) return;
637         
638         if(G.vd->zbuf && G.obedit) glDepthMask(0);      // for zbuffer-select
639         
640         gridlines= vd->gridlines/2;
641         grid= gridlines*vd->grid;
642         
643         BIF_GetThemeColor3ubv(TH_GRID, col);
644         
645         /* draw the Y axis and/or grid lines */
646         for(a= -gridlines;a<=gridlines;a++) {
647                 if(a==0) {
648                         /* check for the 'show Y axis' preference */
649                         if (vd->gridflag & V3D_SHOW_Y) { 
650                                 make_axis_color(col, col2, 'y');
651                                 glColor3ubv(col2);
652                                 
653                                 draw_line = 1;
654                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
655                                 BIF_ThemeColorShade(TH_GRID, -10);
656                         } else {
657                                 draw_line = 0;
658                         }
659                 } else {
660                         /* check for the 'show grid floor' preference */
661                         if (vd->gridflag & V3D_SHOW_FLOOR) {
662                                 if( (a % 10)==0) {
663                                         BIF_ThemeColorShade(TH_GRID, -10);
664                                 }
665                                 else BIF_ThemeColorShade(TH_GRID, 10);
666                                 
667                                 draw_line = 1;
668                         } else {
669                                 draw_line = 0;
670                         }
671                 }
672                 
673                 if (draw_line) {
674                         glBegin(GL_LINE_STRIP);
675                 vert[0]= a*vd->grid;
676                 vert[1]= grid;
677                 glVertex3fv(vert);
678                 vert[1]= -grid;
679                 glVertex3fv(vert);
680                         glEnd();
681                 }
682         }
683         
684         /* draw the X axis and/or grid lines */
685         for(a= -gridlines;a<=gridlines;a++) {
686                 if(a==0) {
687                         /* check for the 'show X axis' preference */
688                         if (vd->gridflag & V3D_SHOW_X) { 
689                                 make_axis_color(col, col2, 'x');
690                                 glColor3ubv(col2);
691                                 
692                                 draw_line = 1;
693                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
694                                 BIF_ThemeColorShade(TH_GRID, -10);
695                         } else {
696                                 draw_line = 0;
697                         }
698                 } else {
699                         /* check for the 'show grid floor' preference */
700                         if (vd->gridflag & V3D_SHOW_FLOOR) {
701                                 if( (a % 10)==0) {
702                                         BIF_ThemeColorShade(TH_GRID, -10);
703                                 }
704                                 else BIF_ThemeColorShade(TH_GRID, 10);
705                                 
706                                 draw_line = 1;
707                         } else {
708                                 draw_line = 0;
709                         }
710                 }
711                 
712                 if (draw_line) {
713                         glBegin(GL_LINE_STRIP);
714                 vert[1]= a*vd->grid;
715                 vert[0]= grid;
716                 glVertex3fv(vert );
717                 vert[0]= -grid;
718                 glVertex3fv(vert);
719                         glEnd();
720                 }
721         }
722         
723         /* draw the Z axis line */      
724         /* check for the 'show Z axis' preference */
725         if (vd->gridflag & V3D_SHOW_Z) {
726                 make_axis_color(col, col2, 'z');
727                 glColor3ubv(col2);
728                 
729                 glBegin(GL_LINE_STRIP);
730                 vert[0]= 0;
731                 vert[1]= 0;
732                 vert[2]= grid;
733                 glVertex3fv(vert );
734                 vert[2]= -grid;
735                 glVertex3fv(vert);
736                 glEnd();
737         }
738
739         if(G.vd->zbuf && G.obedit) glDepthMask(1);      
740
741 }
742
743 static void drawcursor(View3D *v3d)
744 {
745         short mx,my,co[2];
746         int flag;
747         
748         /* we dont want the clipping for cursor */
749         flag= v3d->flag;
750         v3d->flag= 0;
751         project_short( give_cursor(), co);
752         v3d->flag= flag;
753         
754         mx = co[0];
755         my = co[1];
756
757         if(mx!=IS_CLIPPED) {
758                 setlinestyle(0); 
759                 cpack(0xFF);
760                 circ((float)mx, (float)my, 10.0);
761                 setlinestyle(4); 
762                 cpack(0xFFFFFF);
763                 circ((float)mx, (float)my, 10.0);
764                 setlinestyle(0);
765                 cpack(0x0);
766                 
767                 sdrawline(mx-20, my, mx-5, my);
768                 sdrawline(mx+5, my, mx+20, my);
769                 sdrawline(mx, my-20, mx, my-5);
770                 sdrawline(mx, my+5, mx, my+20);
771         }
772 }
773
774 /* ********* custom clipping *********** */
775
776 static void view3d_draw_clipping(View3D *v3d)
777 {
778         BoundBox *bb= v3d->clipbb;
779         
780         BIF_ThemeColorShade(TH_BACK, -8);
781         
782         glBegin(GL_QUADS);
783
784         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[3]);
785         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[1]);
786         glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[5]);
787         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[3]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[6]);
788         glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[2]);
789         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[3]);
790         
791         glEnd();
792 }
793
794 void view3d_set_clipping(View3D *v3d)
795 {
796         double plane[4];
797         int a;
798         
799         for(a=0; a<4; a++) {
800                 QUATCOPY(plane, v3d->clip[a]);
801                 glClipPlane(GL_CLIP_PLANE0+a, plane);
802                 glEnable(GL_CLIP_PLANE0+a);
803         }
804 }
805
806 void view3d_clr_clipping(void)
807 {
808         int a;
809         
810         for(a=0; a<4; a++) {
811                 glDisable(GL_CLIP_PLANE0+a);
812         }
813 }
814
815 int view3d_test_clipping(View3D *v3d, float *vec)
816 {
817         /* vec in world coordinates, returns 1 if clipped */
818         float view[3];
819         
820         VECCOPY(view, vec);
821         
822         if(0.0f < v3d->clip[0][3] + INPR(view, v3d->clip[0]))
823                 if(0.0f < v3d->clip[1][3] + INPR(view, v3d->clip[1]))
824                         if(0.0f < v3d->clip[2][3] + INPR(view, v3d->clip[2]))
825                                 if(0.0f < v3d->clip[3][3] + INPR(view, v3d->clip[3]))
826                                         return 0;
827
828         return 1;
829 }
830
831 /* ********* end custom clipping *********** */
832
833 static void view3d_get_viewborder_size(View3D *v3d, float size_r[2])
834 {
835         float winmax= MAX2(v3d->area->winx, v3d->area->winy);
836         float aspect= (float) (G.scene->r.xsch*G.scene->r.xasp)/(G.scene->r.ysch*G.scene->r.yasp);
837
838         if(aspect>1.0) {
839                 size_r[0]= winmax;
840                 size_r[1]= winmax/aspect;
841         } else {
842                 size_r[0]= winmax*aspect;
843                 size_r[1]= winmax;
844         }
845 }
846
847 void calc_viewborder(struct View3D *v3d, rcti *viewborder_r)
848 {
849         float zoomfac, size[2];
850         float dx= 0.0f, dy= 0.0f;
851         
852         view3d_get_viewborder_size(v3d, size);
853
854                 /* magic zoom calculation, no idea what
855              * it signifies, if you find out, tell me! -zr
856                  */
857         /* simple, its magic dude!
858          * well, to be honest, this gives a natural feeling zooming
859          * with multiple keypad presses (ton)
860          */
861         
862         zoomfac= (M_SQRT2 + v3d->camzoom/50.0);
863         zoomfac= (zoomfac*zoomfac)*0.25;
864         
865         size[0]= size[0]*zoomfac;
866         size[1]= size[1]*zoomfac;
867
868                 /* center in window */
869         viewborder_r->xmin= 0.5*v3d->area->winx - 0.5*size[0];
870         viewborder_r->ymin= 0.5*v3d->area->winy - 0.5*size[1];
871         viewborder_r->xmax= viewborder_r->xmin + size[0];
872         viewborder_r->ymax= viewborder_r->ymin + size[1];
873         
874         dx= v3d->area->winx*G.vd->camdx;
875         dy= v3d->area->winy*G.vd->camdy;
876         
877         /* apply offset */
878         viewborder_r->xmin-= dx;
879         viewborder_r->ymin-= dy;
880         viewborder_r->xmax-= dx;
881         viewborder_r->ymax-= dy;
882 }
883
884 void view3d_set_1_to_1_viewborder(View3D *v3d)
885 {
886         float size[2];
887         int im_width= (G.scene->r.size*G.scene->r.xsch)/100;
888
889         view3d_get_viewborder_size(v3d, size);
890
891         v3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
892         v3d->camzoom= CLAMPIS(v3d->camzoom, -30, 300);
893 }
894
895 static void drawviewborder(void)
896 {
897         extern void gl_round_box(int mode, float minx, float miny, float maxx, float maxy, float rad);          // interface_panel.c
898         float fac, a;
899         float x1, x2, y1, y2;
900         float x3, y3, x4, y4;
901         rcti viewborder;
902         Camera *ca= NULL;
903
904         if(G.vd->camera==NULL)
905                 return;
906         if(G.vd->camera->type==OB_CAMERA)
907                 ca = G.vd->camera->data;
908         
909         calc_viewborder(G.vd, &viewborder);
910         x1= viewborder.xmin;
911         y1= viewborder.ymin;
912         x2= viewborder.xmax;
913         y2= viewborder.ymax;
914
915         /* passepartout, specified in camera edit buttons */
916         if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT)) {
917                 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
918                 glEnable(GL_BLEND);
919                 glColor4f(0, 0, 0, ca->passepartalpha);
920                 
921                 if (x1 > 0.0)
922                         glRectf(0.0, (float)curarea->winy, x1, 0.0);
923                 if (x2 < (float)curarea->winx)
924                         glRectf(x2, (float)curarea->winy, (float)curarea->winx, 0.0);
925                 if (y2 < (float)curarea->winy)
926                         glRectf(x1, (float)curarea->winy, x2, y2);
927                 if (y2 > 0.0) 
928                         glRectf(x1, y1, x2, 0.0);
929
930                 glDisable(GL_BLEND);
931         }
932         
933         /* edge */
934         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
935         
936         setlinestyle(0);
937         BIF_ThemeColor(TH_BACK);
938         glRectf(x1, y1, x2, y2);
939         
940         setlinestyle(3);
941         BIF_ThemeColor(TH_WIRE);
942         glRectf(x1, y1, x2, y2);
943                 
944         /* camera name */
945         if (ca && (ca->flag & CAM_SHOWNAME)) {
946                 glRasterPos2f(x1, y1-15);
947                 
948                 BMF_DrawString(G.font, G.vd->camera->id.name+2);
949         }
950
951
952         /* border */
953         if(G.scene->r.mode & R_BORDER) {
954                 
955                 cpack(0);
956                 x3= x1+ G.scene->r.border.xmin*(x2-x1);
957                 y3= y1+ G.scene->r.border.ymin*(y2-y1);
958                 x4= x1+ G.scene->r.border.xmax*(x2-x1);
959                 y4= y1+ G.scene->r.border.ymax*(y2-y1);
960                 
961                 cpack(0x4040FF);
962                 glRectf(x3,  y3,  x4,  y4); 
963         }
964
965         /* safety border */
966         if (ca && (ca->flag & CAM_SHOWTITLESAFE)) {
967                 fac= 0.1;
968                 
969                 a= fac*(x2-x1);
970                 x1+= a; 
971                 x2-= a;
972         
973                 a= fac*(y2-y1);
974                 y1+= a;
975                 y2-= a;
976         
977                 BIF_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
978                 
979                 uiSetRoundBox(15);
980                 gl_round_box(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
981         }
982         
983         setlinestyle(0);
984         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
985
986 }
987
988
989 void backdrawview3d(int test)
990 {
991         struct Base *base;
992
993         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT));
994         else if(G.obedit && G.vd->drawtype>OB_WIRE && (G.vd->flag & V3D_ZBUF_SELECT));
995         else {
996                 G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
997                 return;
998         }
999
1000         if( !(G.vd->flag & V3D_NEEDBACKBUFDRAW) ) return;
1001
1002         if(test) {
1003                 if(qtest()) {
1004                         addafterqueue(curarea->win, BACKBUFDRAW, 1);
1005                         return;
1006                 }
1007         }
1008         persp(PERSP_VIEW);
1009
1010 #ifdef __APPLE__
1011         glDrawBuffer(GL_AUX0);
1012 #endif  
1013         if(G.vd->drawtype > OB_WIRE) G.vd->zbuf= TRUE;
1014         curarea->win_swap &= ~WIN_BACK_OK;
1015         
1016         glDisable(GL_DITHER);
1017
1018         glClearColor(0.0, 0.0, 0.0, 0.0); 
1019         if(G.vd->zbuf) {
1020                 glEnable(GL_DEPTH_TEST);
1021                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1022         }
1023         else {
1024                 glClear(GL_COLOR_BUFFER_BIT);
1025                 glDisable(GL_DEPTH_TEST);
1026         }
1027         
1028         if(G.vd->flag & V3D_CLIPPING)
1029                 view3d_set_clipping(G.vd);
1030         
1031         G.f |= G_BACKBUFSEL;
1032         
1033         base= (G.scene->basact);
1034         if(base && (base->lay & G.vd->lay)) {
1035                 draw_object_backbufsel(base->object);
1036         }
1037
1038         G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
1039
1040         G.f &= ~G_BACKBUFSEL;
1041         G.vd->zbuf= FALSE; 
1042         glDisable(GL_DEPTH_TEST);
1043         glEnable(GL_DITHER);
1044
1045 #ifdef __APPLE__
1046         glDrawBuffer(GL_BACK); /* we were in aux buffers */
1047 #endif
1048
1049         if(G.vd->flag & V3D_CLIPPING)
1050                 view3d_clr_clipping();
1051         
1052         /* it is important to end a view in a transform compatible with buttons */
1053         persp(PERSP_WIN);  // set ortho
1054         bwin_scalematrix(curarea->win, G.vd->blockscale, G.vd->blockscale, G.vd->blockscale);
1055
1056 }
1057
1058 void check_backbuf(void)
1059 {
1060         if(G.vd->flag & V3D_NEEDBACKBUFDRAW)
1061                 backdrawview3d(0);
1062 }
1063
1064 /* samples a single pixel (copied from vpaint) */
1065 unsigned int sample_backbuf(int x, int y)
1066 {
1067         unsigned int col;
1068         
1069         if(x>=curarea->winx || y>=curarea->winy) return 0;
1070         x+= curarea->winrct.xmin;
1071         y+= curarea->winrct.ymin;
1072         
1073         check_backbuf(); // actually not needed for apple
1074
1075 #ifdef __APPLE__
1076         glReadBuffer(GL_AUX0);
1077 #endif
1078         glReadPixels(x,  y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,  &col);
1079         glReadBuffer(GL_BACK);  
1080         
1081         if(G.order==B_ENDIAN) SWITCH_INT(col);
1082         
1083         return framebuffer_to_index(col);
1084 }
1085
1086 /* reads full rect, converts indices */
1087 ImBuf *read_backbuf(short xmin, short ymin, short xmax, short ymax)
1088 {
1089         unsigned int *dr, *rd;
1090         struct ImBuf *ibuf, *ibuf1;
1091         int a;
1092         short xminc, yminc, xmaxc, ymaxc, xs, ys;
1093         
1094         /* clip */
1095         if(xmin<0) xminc= 0; else xminc= xmin;
1096         if(xmax>=curarea->winx) xmaxc= curarea->winx-1; else xmaxc= xmax;
1097         if(xminc > xmaxc) return NULL;
1098
1099         if(ymin<0) yminc= 0; else yminc= ymin;
1100         if(ymax>=curarea->winy) ymaxc= curarea->winy-1; else ymaxc= ymax;
1101         if(yminc > ymaxc) return NULL;
1102         
1103         ibuf= IMB_allocImBuf((xmaxc-xminc+1),(ymaxc-yminc+1),32,IB_rect,0);
1104
1105         check_backbuf(); // actually not needed for apple
1106         
1107 #ifdef __APPLE__
1108         glReadBuffer(GL_AUX0);
1109 #endif
1110         glReadPixels(curarea->winrct.xmin+xminc, curarea->winrct.ymin+yminc, (xmaxc-xminc+1), (ymaxc-yminc+1), GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1111         glReadBuffer(GL_BACK);  
1112
1113         if(G.order==B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1114
1115         a= (xmaxc-xminc+1)*(ymaxc-yminc+1);
1116         dr= ibuf->rect;
1117         while(a--) {
1118                 if(*dr) *dr= framebuffer_to_index(*dr);
1119                 dr++;
1120         }
1121         
1122         /* put clipped result back, if needed */
1123         if(xminc==xmin && xmaxc==xmax && yminc==ymin && ymaxc==ymax) return ibuf;
1124         ibuf1= IMB_allocImBuf( (xmax-xmin+1),(ymax-ymin+1),32,IB_rect,0);
1125         rd= ibuf->rect;
1126         dr= ibuf1->rect;
1127                 
1128         for(ys= ymin; ys<=ymax; ys++) {
1129                 for(xs= xmin; xs<=xmax; xs++, dr++) {
1130                         if( xs>=xminc && xs<=xmaxc && ys>=yminc && ys<=ymaxc) {
1131                                 *dr= *rd;
1132                                 rd++;
1133                         }
1134                 }
1135         }
1136         IMB_freeImBuf(ibuf);
1137         return ibuf1;
1138 }
1139
1140 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
1141 unsigned int sample_backbuf_rect(short mval[2], int size, unsigned int min, unsigned int max, short *dist)
1142 {
1143         struct ImBuf *buf;
1144         unsigned int *bufmin, *bufmax, *tbuf;
1145         int minx, miny;
1146         int a, b, rc, nr, amount, dirvec[4][2];
1147         short distance=0;
1148         unsigned int index = 0;
1149         
1150         amount= (size-1)/2;
1151
1152         minx = mval[0]-(amount+1);
1153         miny = mval[1]-(amount+1);
1154         buf = read_backbuf(minx, miny, minx+size-1, miny+size-1);
1155         if (!buf) return 0;
1156
1157         rc= 0;
1158         
1159         dirvec[0][0]= 1; dirvec[0][1]= 0;
1160         dirvec[1][0]= 0; dirvec[1][1]= -size;
1161         dirvec[2][0]= -1; dirvec[2][1]= 0;
1162         dirvec[3][0]= 0; dirvec[3][1]= size;
1163         
1164         bufmin = buf->rect;
1165         tbuf = buf->rect;
1166         bufmax = buf->rect + size*size;
1167         tbuf+= amount*size+ amount;
1168         
1169         for(nr=1; nr<=size; nr++) {
1170                 
1171                 for(a=0; a<2; a++) {
1172                         for(b=0; b<nr; b++, distance++) {
1173                                 if (*tbuf && *tbuf>=min && *tbuf<max) {
1174                                         *dist= (short) sqrt( (float)distance ); // XXX, this distance is wrong - zr
1175                                         index = *tbuf - min+1; // messy yah, but indices start at 1
1176                                         goto exit;
1177                                 }
1178                                 
1179                                 tbuf+= (dirvec[rc][0]+dirvec[rc][1]);
1180                                 
1181                                 if(tbuf<bufmin || tbuf>=bufmax) {
1182                                         goto exit;
1183                                 }
1184                         }
1185                         rc++;
1186                         rc &= 3;
1187                 }
1188         }
1189
1190 exit:
1191         IMB_freeImBuf(buf);
1192         return index;
1193 }
1194
1195 void drawname(Object *ob)
1196 {
1197         cpack(0x404040);
1198         glRasterPos3f(0.0,  0.0,  0.0);
1199         
1200         BMF_DrawString(G.font, " ");
1201         BMF_DrawString(G.font, ob->id.name+2);
1202 }
1203
1204
1205 static void draw_selected_name(Object *ob)
1206 {
1207         char info[128];
1208
1209         if(ob->type==OB_ARMATURE) {
1210                 bArmature *arm= ob->data;
1211                 char *name= NULL;
1212                 
1213                 if(ob==G.obedit) {
1214                         EditBone *ebo;
1215                         for (ebo=G.edbo.first; ebo; ebo=ebo->next){
1216                                 if ((ebo->flag & BONE_ACTIVE) && (ebo->layer & arm->layer)) {
1217                                         name= ebo->name;
1218                                         break;
1219                                 }
1220                         }
1221                 }
1222                 else if(ob->pose && (ob->flag & OB_POSEMODE)) {
1223                         bPoseChannel *pchan;
1224                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1225                                 if((pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer)) {
1226                                         name= pchan->name;
1227                                         break;
1228                                 }
1229                         }
1230                 }
1231                 if(name)
1232                         sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, name);
1233                 else
1234                         sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
1235         }
1236         else sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
1237
1238         BIF_ThemeColor(TH_TEXT_HI);
1239         glRasterPos2i(30,  10);
1240         BMF_DrawString(G.fonts, info);
1241 }
1242
1243
1244 static void draw_view_icon(void)
1245 {
1246         BIFIconID icon;
1247         
1248         if(G.vd->view==7) icon= ICON_AXIS_TOP;
1249         else if(G.vd->view==1) icon= ICON_AXIS_FRONT;
1250         else if(G.vd->view==3) icon= ICON_AXIS_SIDE;
1251         else return ;
1252
1253         glEnable(GL_BLEND);
1254         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
1255         
1256         BIF_icon_draw(5.0, 5.0, icon);
1257         
1258         glBlendFunc(GL_ONE,  GL_ZERO); 
1259         glDisable(GL_BLEND);
1260 }
1261
1262 /* ******************* view3d space & buttons ************** */
1263
1264 static void view3d_change_bgpic_ima(View3D *v3d, Image *newima) {
1265         if (v3d->bgpic && v3d->bgpic->ima!=newima) {
1266                 if (newima)
1267                         id_us_plus((ID*) newima);
1268                 if (v3d->bgpic->ima)
1269                         v3d->bgpic->ima->id.us--;
1270                 v3d->bgpic->ima= newima;
1271
1272                 if(v3d->bgpic->rect) MEM_freeN(v3d->bgpic->rect);
1273                 v3d->bgpic->rect= NULL;
1274                 
1275                 allqueue(REDRAWVIEW3D, 0);
1276         }
1277 }
1278 static void view3d_change_bgpic_tex(View3D *v3d, Tex *newtex) {
1279         if (v3d->bgpic && v3d->bgpic->tex!=newtex) {
1280                 if (newtex)
1281                         id_us_plus((ID*) newtex);
1282                 if (v3d->bgpic->tex)
1283                         v3d->bgpic->tex->id.us--;
1284                 v3d->bgpic->tex= newtex;
1285                 
1286                 allqueue(REDRAWVIEW3D, 0);
1287         }
1288 }
1289
1290 static void load_bgpic_image(char *name)
1291 {
1292         Image *ima;
1293         View3D *vd;
1294         
1295         areawinset(curarea->win);
1296         vd= G.vd;
1297         if(vd==0 || vd->bgpic==0) return;
1298         
1299         ima= add_image(name);
1300         if(ima) {
1301                 if(vd->bgpic->ima) {
1302                         vd->bgpic->ima->id.us--;
1303                 }
1304                 vd->bgpic->ima= ima;
1305                 
1306                 free_image_buffers(ima);        /* force read again */
1307                 ima->ok= 1;
1308         }
1309         allqueue(REDRAWVIEW3D, 0);
1310         
1311 }
1312
1313 /* this one assumes there is only one global active object in blender...  (for object panel) */
1314 static float ob_eul[4]; // used for quat too....
1315 /* this one assumes there is only one editmode in blender...  (for object panel) */
1316 static float ve_median[5];
1317
1318 /* is used for both read and write... */
1319 static void v3d_editvertex_buts(uiBlock *block, Object *ob, float lim)
1320 {
1321         static int curdef=0;
1322         static float *defweightp= NULL;
1323         EditMesh *em = G.editMesh;
1324         EditVert *eve, *evedef=NULL;
1325         EditEdge *eed;
1326         float median[5];
1327         int tot, totw, totweight, totedge;
1328         char defstr[320];
1329         
1330         median[0]= median[1]= median[2]= median[3]= median[4]= 0.0;
1331         tot= totw= totweight= totedge= 0;
1332         defstr[0]= 0;
1333
1334         if(ob->type==OB_MESH) {         
1335                 eve= em->verts.first;
1336                 while(eve) {
1337                         if(eve->f & 1) {
1338                                 evedef= eve;
1339                                 tot++;
1340                                 VecAddf(median, median, eve->co);
1341                         }
1342                         eve= eve->next;
1343                 }
1344                 eed= em->edges.first;
1345                 while(eed) {
1346                         if((eed->f & SELECT)) {
1347                                 totedge++;
1348                                 median[3]+= eed->crease;
1349                         }
1350                         eed= eed->next;
1351                 }
1352                 /* check for defgroups */
1353                 if(tot==1 && evedef->totweight) {
1354                         bDeformGroup *dg;
1355                         int i, max=1, init=1;
1356                         char str[32];
1357                         
1358                         for (i=0; i<evedef->totweight; i++){
1359                                 dg = BLI_findlink (&ob->defbase, evedef->dw[i].def_nr);
1360                                 max+= sprintf(str, "%s %%x%d|", dg->name, evedef->dw[i].def_nr); 
1361                                 if(max<320) strcat(defstr, str);
1362                                 
1363                                 if(curdef==evedef->dw[i].def_nr) {
1364                                         init= 0;
1365                                         defweightp= &evedef->dw[i].weight;
1366                                 }
1367                         }
1368                         
1369                         if(init) {      // needs new initialized 
1370                                 curdef= evedef->dw[0].def_nr;
1371                                 defweightp= &evedef->dw[0].weight;
1372                         }
1373                 }
1374         }
1375         else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1376                 extern ListBase editNurb; /* editcurve.c */
1377                 Nurb *nu;
1378                 BPoint *bp;
1379                 BezTriple *bezt;
1380                 int a;
1381                 
1382                 nu= editNurb.first;
1383                 while(nu) {
1384                         if((nu->type & 7)==1) {
1385                                 bezt= nu->bezt;
1386                                 a= nu->pntsu;
1387                                 while(a--) {
1388                                         if(bezt->f2 & 1) {
1389                                                 VecAddf(median, median, bezt->vec[1]);
1390                                                 tot++;
1391                                                 median[4]+= bezt->weight;
1392                                                 totweight++;
1393                                         }
1394                                         else {
1395                                                 if(bezt->f1 & 1) {
1396                                                         VecAddf(median, median, bezt->vec[0]);
1397                                                         tot++;
1398                                                 }
1399                                                 if(bezt->f3 & 1) {
1400                                                         VecAddf(median, median, bezt->vec[2]);
1401                                                         tot++;
1402                                                 }
1403                                         }
1404                                         bezt++;
1405                                 }
1406                         }
1407                         else {
1408                                 bp= nu->bp;
1409                                 a= nu->pntsu*nu->pntsv;
1410                                 while(a--) {
1411                                         if(bp->f1 & 1) {
1412                                                 VecAddf(median, median, bp->vec);
1413                                                 median[3]+= bp->vec[3];
1414                                                 totw++;
1415                                                 tot++;
1416                                                 median[4]+= bp->weight;
1417                                                 totweight++;
1418                                         }
1419                                         bp++;
1420                                 }
1421                         }
1422                         nu= nu->next;
1423                 }
1424         }
1425         else if(ob->type==OB_LATTICE) {
1426                 BPoint *bp;
1427                 int a;
1428                 
1429                 a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1430                 bp= editLatt->def;
1431                 while(a--) {
1432                         if(bp->f1 & SELECT) {
1433                                 VecAddf(median, median, bp->vec);
1434                                 tot++;
1435                                 median[4]+= bp->weight;
1436                                 totweight++;
1437                         }
1438                         bp++;
1439                 }
1440         }
1441         
1442         if(tot==0) return;
1443
1444         median[0] /= (float)tot;
1445         median[1] /= (float)tot;
1446         median[2] /= (float)tot;
1447         if(totedge) median[3] /= (float)totedge;
1448         else if(totw) median[3] /= (float)totw;
1449         if(totweight) median[4] /= (float)totweight;
1450         
1451         if(G.vd->flag & V3D_GLOBAL_STATS)
1452                 Mat4MulVecfl(ob->obmat, median);
1453         
1454         if(block) {     // buttons
1455         
1456                 uiBlockBeginAlign(block);
1457                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, REDRAWVIEW3D, "Global",              160, 150, 70, 19, &G.vd->flag, 0, 0, 0, 0, "Displays global values");
1458                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, REDRAWVIEW3D, "Local",              230, 150, 70, 19, &G.vd->flag, 0, 0, 0, 0, "Displays local values");
1459                 
1460                 memcpy(ve_median, median, sizeof(ve_median));
1461                 
1462                 uiBlockBeginAlign(block);
1463                 if(tot==1) {
1464                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex X:", 10, 110, 290, 19, &(ve_median[0]), -lim, lim, 10, 3, "");
1465                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Y:", 10, 90, 290, 19, &(ve_median[1]), -lim, lim, 10, 3, "");
1466                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Z:", 10, 70, 290, 19, &(ve_median[2]), -lim, lim, 10, 3, "");
1467                         if(totw==1)
1468                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex W:", 10, 50, 290, 19, &(ve_median[3]), 0.01, 100.0, 10, 3, "");
1469                         uiBlockEndAlign(block);
1470         
1471                         if(defstr[0]) {
1472                                 uiDefBut(block, LABEL, 1, "Vertex Deform Groups",               10, 40, 290, 20, NULL, 0.0, 0.0, 0, 0, "");
1473
1474                                 uiBlockBeginAlign(block);
1475                                 uiDefButF(block, NUM, B_NOP, "Weight:",                 10, 20, 150, 19, defweightp, 0.0f, 1.0f, 10, 3, "Weight value");
1476                                 uiDefButI(block, MENU, REDRAWVIEW3D, defstr,    160, 20, 140, 19, &curdef, 0.0, 0.0, 0, 0, "Current Vertex Group");
1477                                 uiBlockEndAlign(block);
1478                         }
1479                         else if(totweight)
1480                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   10, 20, 290, 19, &(ve_median[4]), 0.0, 1.0, 10, 3, "");
1481
1482                 }
1483                 else {
1484                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median X:", 10, 110, 290, 19, &(ve_median[0]), -lim, lim, 10, 3, "");
1485                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Y:", 10, 90, 290, 19, &(ve_median[1]), -lim, lim, 10, 3, "");
1486                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Z:", 10, 70, 290, 19, &(ve_median[2]), -lim, lim, 10, 3, "");
1487                         if(totw==tot)
1488                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median W:", 10, 50, 290, 19, &(ve_median[3]), 0.01, 100.0, 10, 3, "");
1489                         uiBlockEndAlign(block);
1490                         if(totweight)
1491                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   10, 20, 290, 19, &(ve_median[4]), 0.0, 1.0, 10, 3, "Weight is used for SoftBody Goal");
1492                 }
1493                 
1494                 if(totedge==1)
1495                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Crease W:", 10, 30, 290, 19, &(ve_median[3]), 0.0, 1.0, 10, 3, "");
1496                 else if(totedge>1)
1497                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Crease W:",  10, 30, 290, 19, &(ve_median[3]), 0.0, 1.0, 10, 3, "");
1498                 
1499         }
1500         else {  // apply
1501                 
1502                 if(G.vd->flag & V3D_GLOBAL_STATS) {
1503                         Mat4Invert(ob->imat, ob->obmat);
1504                         Mat4MulVecfl(ob->imat, median);
1505                         Mat4MulVecfl(ob->imat, ve_median);
1506                 }
1507                 VecSubf(median, ve_median, median);
1508                 median[3]= ve_median[3]-median[3];
1509                 median[4]= ve_median[4]-median[4];
1510                 
1511                 if(ob->type==OB_MESH) {
1512                         float diffac= 1.0;
1513                         
1514                         eve= em->verts.first;
1515                         while(eve) {
1516                                 if(eve->f & 1) {
1517                                         VecAddf(eve->co, eve->co, median);
1518                                 }
1519                                 eve= eve->next;
1520                         }
1521                         
1522                         /* calculate the differences to squeeze a range smaller when values are close to 1 or 0 */
1523                         /* this way you can edit a median value which is applied on clipped values :) */
1524                         if(totedge>1) {
1525                                 float max= 0.0;
1526                                 for(eed= em->edges.first; eed; eed= eed->next) {
1527                                         if(eed->f & SELECT) {
1528                                                 if(max < ABS(eed->crease-ve_median[3])) max= ABS(eed->crease-ve_median[3]);
1529                                         }
1530                                 }
1531                                 if(max>0.0) {
1532                                         if(ve_median[3]> 0.5) diffac= (1.0-ve_median[3])/max;
1533                                         else diffac= (ve_median[3])/max;
1534                                         if(diffac>1.0) diffac= 1.0;
1535                                 }
1536                         }
1537                         
1538                         for(eed= em->edges.first; eed; eed= eed->next) {
1539                                 if(eed->f & SELECT) {
1540                                         eed->crease+= median[3];
1541                                         eed->crease= ve_median[3] + diffac*(eed->crease-ve_median[3]);
1542                                         
1543                                         CLAMP(eed->crease, 0.0, 1.0);
1544                                 }
1545                         }
1546                         
1547                         recalc_editnormals();
1548                 }
1549                 else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1550                         extern ListBase editNurb; /* editcurve.c */
1551                         Nurb *nu;
1552                         BPoint *bp;
1553                         BezTriple *bezt;
1554                         int a;
1555                         
1556                         nu= editNurb.first;
1557                         while(nu) {
1558                                 if((nu->type & 7)==1) {
1559                                         bezt= nu->bezt;
1560                                         a= nu->pntsu;
1561                                         while(a--) {
1562                                                 if(bezt->f2 & 1) {
1563                                                         VecAddf(bezt->vec[0], bezt->vec[0], median);
1564                                                         VecAddf(bezt->vec[1], bezt->vec[1], median);
1565                                                         VecAddf(bezt->vec[2], bezt->vec[2], median);
1566                                                         bezt->weight+= median[4];
1567                                                 }
1568                                                 else {
1569                                                         if(bezt->f1 & 1) {
1570                                                                 VecAddf(bezt->vec[0], bezt->vec[0], median);
1571                                                         }
1572                                                         if(bezt->f3 & 1) {
1573                                                                 VecAddf(bezt->vec[2], bezt->vec[2], median);
1574                                                         }
1575                                                 }
1576                                                 bezt++;
1577                                         }
1578                                 }
1579                                 else {
1580                                         bp= nu->bp;
1581                                         a= nu->pntsu*nu->pntsv;
1582                                         while(a--) {
1583                                                 if(bp->f1 & 1) {
1584                                                         VecAddf(bp->vec, bp->vec, median);
1585                                                         bp->vec[3]+= median[3];
1586                                                         bp->weight+= median[4];
1587                                                 }
1588                                                 bp++;
1589                                         }
1590                                 }
1591                                 test2DNurb(nu);
1592                                 testhandlesNurb(nu); /* test for bezier too */
1593
1594                                 nu= nu->next;
1595                         }
1596                 }
1597                 else if(ob->type==OB_LATTICE) {
1598                         BPoint *bp;
1599                         int a;
1600                         
1601                         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1602                         bp= editLatt->def;
1603                         while(a--) {
1604                                 if(bp->f1 & SELECT) {
1605                                         VecAddf(bp->vec, bp->vec, median);
1606                                         bp->weight+= median[4];
1607                                 }
1608                                 bp++;
1609                         }
1610                 }
1611                 
1612                 BIF_undo_push("Transform properties");
1613         }
1614 }
1615
1616 /* assumes armature active */
1617 static void validate_bonebutton_cb(void *bonev, void *namev)
1618 {
1619         Object *ob= OBACT;
1620         
1621         if(ob && ob->type==OB_ARMATURE) {
1622                 Bone *bone= bonev;
1623                 char oldname[32], newname[32];
1624                 
1625                 /* need to be on the stack */
1626                 BLI_strncpy(newname, bone->name, 32);
1627                 BLI_strncpy(oldname, (char *)namev, 32);
1628                 /* restore */
1629                 BLI_strncpy(bone->name, oldname, 32);
1630                 
1631                 armature_bone_rename(ob->data, oldname, newname); // editarmature.c
1632                 allqueue(REDRAWALL, 0);
1633         }
1634 }
1635
1636
1637 static void v3d_posearmature_buts(uiBlock *block, Object *ob, float lim)
1638 {
1639         uiBut *but;
1640         bArmature *arm;
1641         bPoseChannel *pchan;
1642         Bone *bone;
1643
1644         arm = get_armature(OBACT);
1645         if (!arm || !ob->pose) return;
1646
1647         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1648                 bone = pchan->bone;
1649                 if(bone && (bone->flag & BONE_ACTIVE) && (bone->layer & arm->layer))
1650                         break;
1651         }
1652         if (!pchan) return;
1653         
1654         but= uiDefBut(block, TEX, B_DIFF, "Bone:",                              160, 140, 140, 19, bone->name, 1, 31, 0, 0, "");
1655         uiButSetFunc(but, validate_bonebutton_cb, bone, NULL);
1656         
1657         QuatToEul(pchan->quat, ob_eul);
1658         ob_eul[0]*= 180.0/M_PI;
1659         ob_eul[1]*= 180.0/M_PI;
1660         ob_eul[2]*= 180.0/M_PI;
1661         
1662         uiBlockBeginAlign(block);
1663         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCX, REDRAWVIEW3D, ICON_UNLOCKED,     10,140,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1664         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocX:",        30, 140, 120, 19, pchan->loc, -lim, lim, 100, 3, "");
1665         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCY, REDRAWVIEW3D, ICON_UNLOCKED,     10,120,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1666         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocY:",        30, 120, 120, 19, pchan->loc+1, -lim, lim, 100, 3, "");
1667         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,100,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1668         uiDefButF(block, NUM, B_ARMATUREPANEL2, "locZ:",        30, 100, 120, 19, pchan->loc+2, -lim, lim, 100, 3, "");
1669
1670         uiBlockBeginAlign(block);
1671         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, REDRAWVIEW3D, ICON_UNLOCKED,     10,70,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1672         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotX:",        30, 70, 120, 19, ob_eul, -1000.0, 1000.0, 100, 3, "");
1673         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, REDRAWVIEW3D, ICON_UNLOCKED,     10,50,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1674         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotY:",        30, 50, 120, 19, ob_eul+1, -1000.0, 1000.0, 100, 3, "");
1675         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,30,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1676         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotZ:",        30, 30, 120, 19, ob_eul+2, -1000.0, 1000.0, 100, 3, "");
1677         
1678         uiBlockBeginAlign(block);
1679         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEX, REDRAWVIEW3D, ICON_UNLOCKED,    160,70,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1680         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeX:",       180, 70, 120, 19, pchan->size, -lim, lim, 10, 3, "");
1681         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEY, REDRAWVIEW3D, ICON_UNLOCKED,    160,50,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1682         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeY:",       180, 50, 120, 19, pchan->size+1, -lim, lim, 10, 3, "");
1683         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEZ, REDRAWVIEW3D, ICON_UNLOCKED,    160,30,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1684         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeZ:",       180, 30, 120, 19, pchan->size+2, -lim, lim, 10, 3, "");
1685         uiBlockEndAlign(block);
1686 }
1687
1688 static void v3d_editarmature_buts(uiBlock *block, Object *ob, float lim)
1689 {
1690         bArmature *arm= G.obedit->data;
1691         EditBone *ebone;
1692         uiBut *but;
1693         
1694         ebone= G.edbo.first;
1695
1696         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1697                 if ((ebone->flag & BONE_ACTIVE) && (ebone->layer & arm->layer))
1698                         break;
1699         }
1700
1701         if (!ebone)
1702                 return;
1703         
1704         but= uiDefBut(block, TEX, B_DIFF, "Bone:",                      160, 140, 140, 19, ebone->name, 1, 31, 0, 0, "");
1705         uiButSetFunc(but, validate_editbonebutton_cb, ebone, NULL);
1706
1707         uiBlockBeginAlign(block);
1708         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootX:",       10, 70, 140, 19, ebone->head, -lim, lim, 10, 3, "");
1709         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootY:",       10, 50, 140, 19, ebone->head+1, -lim, lim, 10, 3, "");
1710         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootZ:",       10, 30, 140, 19, ebone->head+2, -lim, lim, 10, 3, "");
1711         uiBlockBeginAlign(block);
1712         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipX:",        160, 70, 140, 19, ebone->tail, -lim, lim, 10, 3, "");
1713         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipY:",        160, 50, 140, 19, ebone->tail+1, -lim, lim, 10, 3, "");
1714         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipZ:",        160, 30, 140, 19, ebone->tail+2, -lim, lim, 10, 3, "");
1715         uiBlockEndAlign(block);
1716         ob_eul[0]= 180.0*ebone->roll/M_PI;
1717         uiDefButF(block, NUM, B_ARMATUREPANEL1, "Roll:",        10, 100, 140, 19, ob_eul, -lim, lim, 1000, 3, "");
1718
1719 }
1720
1721 static void v3d_editmetaball_buts(uiBlock *block, Object *ob, float lim)
1722 {
1723         extern MetaElem *lastelem;
1724
1725         if(lastelem) {
1726                 uiBlockBeginAlign(block);
1727                 uiDefButF(block, NUM, B_RECALCMBALL, "LocX:", 10, 70, 140, 19, &lastelem->x, -lim, lim, 100, 3, "");
1728                 uiDefButF(block, NUM, B_RECALCMBALL, "LocY:", 10, 50, 140, 19, &lastelem->y, -lim, lim, 100, 3, "");
1729                 uiDefButF(block, NUM, B_RECALCMBALL, "LocZ:", 10, 30, 140, 19, &lastelem->z, -lim, lim, 100, 3, "");
1730
1731                 uiBlockBeginAlign(block);
1732                 if(lastelem->type!=MB_BALL)
1733                 uiDefButF(block, NUM, B_RECALCMBALL, "dx:", 160, 70, 140, 19, &lastelem->expx, 0, lim, 100, 3, "");
1734                 if((lastelem->type!=MB_BALL) && (lastelem->type!=MB_TUBE))
1735                 uiDefButF(block, NUM, B_RECALCMBALL, "dy:", 160, 50, 140, 19, &lastelem->expy, 0, lim, 100, 3, "");
1736                 if((lastelem->type==MB_ELIPSOID) || (lastelem->type==MB_CUBE))
1737                 uiDefButF(block, NUM, B_RECALCMBALL, "dz:", 160, 30, 140, 19, &lastelem->expz, 0, lim, 100, 3, "");
1738
1739                 uiBlockEndAlign(block);
1740
1741                 uiDefButF(block, NUM, B_RECALCMBALL, "Stiffness:", 10, 100, 140, 19, &lastelem->s, 0, lim, 100, 3, "");
1742         }
1743 }
1744
1745 void do_viewbuts(unsigned short event)
1746 {
1747         View3D *vd;
1748         Object *ob= OBACT;
1749         char *name;
1750         
1751         vd= G.vd;
1752         if(vd==0) return;
1753
1754         switch(event) {
1755         case B_LOADBGPIC:
1756                 if(vd->bgpic && vd->bgpic->ima) name= vd->bgpic->ima->name;
1757                 else name= G.ima;
1758                 
1759                 if(G.qual==LR_CTRLKEY)
1760                         activate_imageselect(FILE_SPECIAL, "Select Image", name, load_bgpic_image);
1761                 else
1762                         activate_fileselect(FILE_SPECIAL, "Select Image", name, load_bgpic_image);
1763                 break;
1764                 
1765         case B_BLENDBGPIC:
1766                 if(vd->bgpic && vd->bgpic->rect) setalpha_bgpic(vd->bgpic);
1767                 addqueue(curarea->win, REDRAW, 1);
1768                 break;
1769                 
1770         case B_BGPICBROWSE:
1771                 if(vd->bgpic) {
1772                         if (vd->menunr==-2) {
1773                                 activate_databrowse((ID*) vd->bgpic->ima, ID_IM, 0, B_BGPICBROWSE, &vd->menunr, do_viewbuts);
1774                         } else if (vd->menunr>0) {
1775                                 Image *newima= (Image*) BLI_findlink(&G.main->image, vd->menunr-1);
1776
1777                                 if (newima)
1778                                         view3d_change_bgpic_ima(vd, newima);
1779                         }
1780                 }
1781                 break;
1782                 
1783         case B_BGPICCLEAR:
1784                 if (vd->bgpic)
1785                         view3d_change_bgpic_ima(vd, NULL);
1786                 break;
1787                 
1788         case B_BGPICTEX:
1789                 if (vd->bgpic) {
1790                         if (vd->texnr==-2) {
1791                                 activate_databrowse((ID*) vd->bgpic->tex, ID_TE, 0, B_BGPICTEX, &vd->texnr, do_viewbuts);
1792                         } else if (vd->texnr>0) {
1793                                 Tex *newtex= (Tex*) BLI_findlink(&G.main->tex, vd->texnr-1);
1794                                 
1795                                 if (newtex)
1796                                         view3d_change_bgpic_tex(vd, newtex);
1797                         }
1798                 }
1799                 break;
1800                 
1801         case B_BGPICTEXCLEAR:
1802                 if (vd->bgpic)
1803                         view3d_change_bgpic_tex(vd, NULL);
1804                 break;
1805         
1806         case B_OBJECTPANEL:
1807                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1808                 allqueue(REDRAWVIEW3D, 1);
1809                 break;
1810                 
1811         case B_OBJECTPANELROT:
1812                 if(ob) {
1813                         ob->rot[0]= M_PI*ob_eul[0]/180.0;
1814                         ob->rot[1]= M_PI*ob_eul[1]/180.0;
1815                         ob->rot[2]= M_PI*ob_eul[2]/180.0;
1816                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1817                         allqueue(REDRAWVIEW3D, 1);
1818                 }
1819                 break;
1820         
1821         case B_OBJECTPANELMEDIAN:
1822                 if(ob) {
1823                         v3d_editvertex_buts(NULL, ob, 1.0);
1824                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1825                         allqueue(REDRAWVIEW3D, 1);
1826                 }
1827                 break;
1828         case B_OBJECTPANELPARENT:
1829                 if(ob) {
1830                         if( test_parent_loop(ob->parent, ob) ) 
1831                                 ob->parent= NULL;
1832                         else {
1833                                 DAG_scene_sort(G.scene);
1834                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1835                                 allqueue(REDRAWVIEW3D, 1);
1836                                 allqueue(REDRAWBUTSOBJECT, 0);
1837                         }
1838                 }
1839                 break;
1840                 
1841         case B_ARMATUREPANEL1:
1842                 {
1843                         bArmature *arm= G.obedit->data;
1844                         EditBone *ebone, *child;
1845                         
1846                         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1847                                 if ((ebone->flag & BONE_ACTIVE) && (ebone->layer & arm->layer))
1848                                         break;
1849                         }
1850                         if (ebone) {
1851                                 ebone->roll= M_PI*ob_eul[0]/180.0;
1852                                 //      Update our parent
1853                                 if (ebone->parent && ebone->flag & BONE_CONNECTED){
1854                                         VECCOPY (ebone->parent->tail, ebone->head);
1855                                 }
1856                         
1857                                 //      Update our children if necessary
1858                                 for (child = G.edbo.first; child; child=child->next){
1859                                         if (child->parent == ebone && (child->flag & BONE_CONNECTED)){
1860                                                 VECCOPY (child->head, ebone->tail);
1861                                         }
1862                                 }
1863                                 if(arm->flag & ARM_MIRROR_EDIT) {
1864                                         EditBone *eboflip= armature_bone_get_mirrored(ebone);
1865                                         if(eboflip) {
1866                                                 eboflip->roll= -ebone->roll;
1867                                                 eboflip->head[0]= -ebone->head[0];
1868                                                 eboflip->tail[0]= -ebone->tail[0];
1869                                                 
1870                                                 //      Update our parent
1871                                                 if (eboflip->parent && eboflip->flag & BONE_CONNECTED){
1872                                                         VECCOPY (eboflip->parent->tail, eboflip->head);
1873                                                 }
1874                                                 
1875                                                 //      Update our children if necessary
1876                                                 for (child = G.edbo.first; child; child=child->next){
1877                                                         if (child->parent == eboflip && (child->flag & BONE_CONNECTED)){
1878                                                                 VECCOPY (child->head, eboflip->tail);
1879                                                         }
1880                                                 }
1881                                         }
1882                                 }
1883                                 
1884                                 allqueue(REDRAWVIEW3D, 1);
1885                         }
1886                 }
1887                 break;
1888         case B_ARMATUREPANEL3:  // rotate button on channel
1889                 {
1890                         bArmature *arm;
1891                         bPoseChannel *pchan;
1892                         Bone *bone;
1893                         
1894                         arm = get_armature(OBACT);
1895                         if (!arm || !ob->pose) return;
1896                                 
1897                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1898                                 bone = pchan->bone;
1899                                 if(bone && (bone->flag & BONE_ACTIVE) && (bone->layer & arm->layer))
1900                                         break;
1901                         }
1902                         if (!pchan) return;
1903                         
1904                         ob_eul[0]*= M_PI/180.0;
1905                         ob_eul[1]*= M_PI/180.0;
1906                         ob_eul[2]*= M_PI/180.0;
1907                         EulToQuat(ob_eul, pchan->quat);
1908                 }
1909                 /* no break, pass on */
1910         case B_ARMATUREPANEL2:
1911                 {
1912                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1913                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1914                         allqueue(REDRAWVIEW3D, 1);
1915                 }
1916                 break;
1917         }
1918 }
1919
1920
1921 static void view3d_panel_object(short cntrl)    // VIEW3D_HANDLER_OBJECT
1922 {
1923         uiBlock *block;
1924         uiBut *bt;
1925         Object *ob= OBACT;
1926         float lim;
1927         static char hexcol[128];
1928         
1929         if(ob==NULL) return;
1930
1931         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_object", UI_EMBOSS, UI_HELV, curarea->win);
1932         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1933         uiSetPanelHandler(VIEW3D_HANDLER_OBJECT);  // for close and esc
1934         
1935 /* (ton) can't use the rename trick for paint... panel names and settings are stored in the files and
1936    used to find previous locations when re-open. This causes flipping */
1937
1938         if(uiNewPanel(curarea, block, "Transform Properties", "View3d", 10, 230, 318, 204)==0) return;
1939         
1940         if(ob->id.lib) uiSetButLock(1, "Can't edit library data");
1941         
1942         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
1943                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
1944         }
1945         else {
1946                 bt= uiDefBut(block, TEX, B_IDNAME, "OB: ",      10,180,140,20, ob->id.name+2, 0.0, 19.0, 0, 0, "");
1947                 uiButSetFunc(bt, test_idbutton_cb, ob->id.name, NULL);
1948
1949                 uiDefIDPoinBut(block, test_obpoin_but, ID_OB, B_OBJECTPANELPARENT, "Par:", 160, 180, 140, 20, &ob->parent, "Parent Object"); 
1950         }
1951
1952         lim= 10000.0f*MAX2(1.0, G.vd->grid);
1953
1954         if(ob==G.obedit) {
1955                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(block, ob, lim);
1956                 if(ob->type==OB_MBALL) v3d_editmetaball_buts(block, ob, lim);
1957                 else v3d_editvertex_buts(block, ob, lim);
1958         }
1959         else if(ob->flag & OB_POSEMODE) {
1960                 v3d_posearmature_buts(block, ob, lim);
1961         }
1962         else if(G.f & (G_VERTEXPAINT|G_TEXTUREPAINT)) {
1963                 extern VPaint Gvp;         /* from vpaint */
1964                 static float hsv[3], old[3];    // used as temp mem for picker
1965                 uiBlockPickerButtons(block, &Gvp.r, hsv, old, hexcol, 'f', REDRAWBUTSEDIT);     /* 'f' is for floating panel */
1966         }
1967         else {
1968                 uiBlockBeginAlign(block);
1969                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCX, REDRAWVIEW3D, ICON_UNLOCKED,     10,140,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1970                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocX:",           30, 140, 120, 19, &(ob->loc[0]), -lim, lim, 100, 3, "");
1971                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCY, REDRAWVIEW3D, ICON_UNLOCKED,     10,120,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1972                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocY:",           30, 120, 120, 19, &(ob->loc[1]), -lim, lim, 100, 3, "");
1973                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,100,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1974                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocZ:",           30, 100, 120, 19, &(ob->loc[2]), -lim, lim, 100, 3, "");
1975                 
1976                 ob_eul[0]= 180.0*ob->rot[0]/M_PI;
1977                 ob_eul[1]= 180.0*ob->rot[1]/M_PI;
1978                 ob_eul[2]= 180.0*ob->rot[2]/M_PI;
1979                 
1980                 uiBlockBeginAlign(block);
1981                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, REDRAWVIEW3D, ICON_UNLOCKED,     10,70,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1982                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotX:",        30, 70, 120, 19, &(ob_eul[0]), -lim, lim, 1000, 3, "");
1983                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, REDRAWVIEW3D, ICON_UNLOCKED,     10,50,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1984                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotY:",        30, 50, 120, 19, &(ob_eul[1]), -lim, lim, 1000, 3, "");
1985                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,30,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1986                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotZ:",        30, 30, 120, 19, &(ob_eul[2]), -lim, lim, 1000, 3, "");
1987                 
1988                 uiBlockBeginAlign(block);
1989                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEX, REDRAWVIEW3D, ICON_UNLOCKED,    160,70,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1990                 uiDefButF(block, NUM, B_OBJECTPANEL, "SizeX:",          180, 70, 120, 19, &(ob->size[0]), -lim, lim, 10, 3, "");
1991                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEY, REDRAWVIEW3D, ICON_UNLOCKED,    160,50,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1992                 uiDefButF(block, NUM, B_OBJECTPANEL, "SizeY:",          180, 50, 120, 19, &(ob->size[1]), -lim, lim, 10, 3, "");
1993                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEZ, REDRAWVIEW3D, ICON_UNLOCKED,    160,30,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1994                 uiDefButF(block, NUM, B_OBJECTPANEL, "SizeZ:",          180, 30, 120, 19, &(ob->size[2]), -lim, lim, 10, 3, "");
1995                 uiBlockEndAlign(block);
1996         }
1997         uiClearButLock();
1998 }
1999
2000 static void view3d_panel_background(short cntrl)        // VIEW3D_HANDLER_BACKGROUND
2001 {
2002         uiBlock *block;
2003         View3D *vd;
2004         ID *id;
2005         char *strp;
2006         
2007         vd= G.vd;
2008
2009         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_background", UI_EMBOSS, UI_HELV, curarea->win);
2010         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2011         uiSetPanelHandler(VIEW3D_HANDLER_BACKGROUND);  // for close and esc
2012         if(uiNewPanel(curarea, block, "Background Image", "View3d", 340, 10, 318, 204)==0) return;
2013
2014         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
2015                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
2016         }
2017         
2018         if(vd->flag & V3D_DISPBGPIC) {
2019                 if(vd->bgpic==0) {
2020                         vd->bgpic= MEM_callocN(sizeof(BGpic), "bgpic");
2021                         vd->bgpic->size= 5.0;
2022                         vd->bgpic->blend= 0.5;
2023                 }
2024         }
2025         
2026         uiDefButBitS(block, TOG, V3D_DISPBGPIC, REDRAWVIEW3D, "Use Background Image", 0, 162, 200, 20, &vd->flag, 0, 0, 0, 0, "Display an image in the background of the 3D View");
2027         
2028         uiDefBut(block, LABEL, 1, " ",  206, 162, 84, 20, NULL, 0.0, 0.0, 0, 0, "");
2029         
2030         
2031         if(vd->flag & V3D_DISPBGPIC) {
2032
2033                 /* Background Image */
2034                 uiDefBut(block, LABEL, 1, "Image:",     0, 128, 76, 19, NULL, 0.0, 0.0, 0, 0, "");
2035                 
2036                 uiBlockBeginAlign(block);
2037                 uiDefIconBut(block, BUT, B_LOADBGPIC, ICON_FILESEL,     90, 128, 20, 20, 0, 0, 0, 0, 0, "Open a new background image");
2038
2039                 id= (ID *)vd->bgpic->ima;
2040                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->image), id, &(vd->menunr));
2041                 if(strp[0]) {
2042                 
2043                         uiDefButS(block, MENU, B_BGPICBROWSE, strp,     110, 128, 20, 20, &(vd->menunr), 0, 0, 0, 0, "Select a background image");
2044                 
2045                         if(vd->bgpic->ima)  {
2046                                 uiDefBut(block, TEX,        0,"BG: ",           130, 128, 140, 20, &vd->bgpic->ima->name,0.0,100.0, 0, 0, "The currently selected background image");
2047                                 uiDefIconBut(block, BUT, B_BGPICCLEAR, ICON_X, 270, 128, 20, 20, 0, 0, 0, 0, 0, "Remove background image link");
2048                         }
2049                         uiBlockEndAlign(block);
2050                 } else {
2051                         uiBlockEndAlign(block);
2052                 }
2053                 MEM_freeN(strp);
2054
2055
2056                 /* Background texture */
2057                 uiDefBut(block, LABEL, 1, "Texture:",   0, 100, 76, 19, NULL, 0.0, 0.0, 0, 0, "");
2058                 
2059                 id= (ID *)vd->bgpic->tex;
2060                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->tex), id, &(vd->texnr));
2061                 if (strp[0])
2062                         uiBlockBeginAlign(block);
2063                         uiDefButS(block, MENU, B_BGPICTEX, strp,                        90, 100, 20,20, &(vd->texnr), 0, 0, 0, 0, "Select a texture to use as an animated background image");
2064                 MEM_freeN(strp);
2065                 
2066                 if (id) {
2067                         uiDefBut(block, TEX, B_IDNAME, "TE:",                           110, 100, 160, 20, id->name+2, 0.0, 18.0, 0, 0, "");
2068                         uiDefIconBut(block, BUT, B_BGPICTEXCLEAR, ICON_X,       270, 100, 20, 20, 0, 0, 0, 0, 0, "Remove background texture link");
2069                         uiBlockEndAlign(block);
2070                 } else {
2071                         uiBlockEndAlign(block);
2072                 }
2073
2074                 uiDefButF(block, NUMSLI, B_BLENDBGPIC, "Blend:",        0, 60 , 290, 19, &vd->bgpic->blend, 0.0,1.0, 0, 0, "Set the transparency of the background image");
2075
2076                 uiDefButF(block, NUM, REDRAWVIEW3D, "Size:",            0, 28, 140, 19, &vd->bgpic->size, 0.1, 250.0*vd->grid, 100, 0, "Set the size (width) of the background image");
2077
2078                 uiDefButF(block, NUM, REDRAWVIEW3D, "X Offset:",        0, 6, 140, 19, &vd->bgpic->xof, -250.0*vd->grid,250.0*vd->grid, 10, 2, "Set the horizontal offset of the background image");
2079                 uiDefButF(block, NUM, REDRAWVIEW3D, "Y Offset:",        150, 6, 140, 19, &vd->bgpic->yof, -250.0*vd->grid,250.0*vd->grid, 10, 2, "Set the vertical offset of the background image");
2080
2081         
2082                 
2083                 // uiDefButF(block, NUM, REDRAWVIEW3D, "Size:",                 160,160,150,20, &vd->bgpic->size, 0.1, 250.0, 100, 0, "Set the size for the width of the BackGroundPic");
2084                 
2085
2086
2087 //              uiDefButF(block, NUMSLI, B_BLENDBGPIC, "Blend:",        120,100,190,20,&vd->bgpic->blend, 0.0,1.0, 0, 0, "Set the BackGroundPic transparency");
2088                 
2089 //              uiDefButF(block, NUM, B_DIFF, "Center X: ",     10,70,140,20,&vd->bgpic->xof, -20.0,20.0, 10, 2, "Set the BackGroundPic X Offset");
2090 //              uiDefButF(block, NUM, B_DIFF, "Center Y: ",     160,70,140,20,&vd->bgpic->yof, -20.0,20.0, 10, 2, "Set the BackGroundPic Y Offset");
2091
2092         }
2093 }
2094
2095
2096 static void view3d_panel_properties(short cntrl)        // VIEW3D_HANDLER_SETTINGS
2097 {
2098         uiBlock *block;
2099         View3D *vd;
2100         float *curs;
2101         
2102         vd= G.vd;
2103
2104         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
2105         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2106         uiSetPanelHandler(VIEW3D_HANDLER_PROPERTIES);  // for close and esc
2107         if(uiNewPanel(curarea, block, "View Properties", "View3d", 340, 30, 318, 254)==0) return;
2108
2109         /* to force height */
2110         uiNewPanelHeight(block, 254);
2111
2112         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
2113                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
2114         }
2115
2116         uiDefBut(block, LABEL, 1, "Grid:",                                      10, 220, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2117         uiDefButF(block, NUM, REDRAWVIEW3D, "Spacing:",         10, 200, 140, 19, &vd->grid, 0.001, 100.0, 10, 0, "Set the distance between grid lines");
2118         uiDefButS(block, NUM, REDRAWVIEW3D, "Lines:",           10, 176, 140, 19, &vd->gridlines, 0.0, 100.0, 100, 0, "Set the number of grid lines");
2119
2120         uiDefBut(block, LABEL, 1, "3D Display:",                                                        160, 220, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2121         uiDefButBitS(block, TOG, V3D_SHOW_FLOOR, REDRAWVIEW3D, "Grid Floor",160, 200, 150, 19, &vd->gridflag, 0, 0, 0, 0, "Show the grid floor in free camera mode");
2122         uiDefButBitS(block, TOG, V3D_SHOW_X, REDRAWVIEW3D, "X Axis",            160, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the X Axis line");
2123         uiDefButBitS(block, TOG, V3D_SHOW_Y, REDRAWVIEW3D, "Y Axis",            212, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Y Axis line");
2124         uiDefButBitS(block, TOG, V3D_SHOW_Z, REDRAWVIEW3D, "Z Axis",            262, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Z Axis line");
2125
2126         uiDefBut(block, LABEL, 1, "View Camera:",                       10, 150, 140, 19, NULL, 0.0, 0.0, 0, 0, "");
2127         
2128         uiDefButF(block, NUM, REDRAWVIEW3D, "Lens:",            10, 130, 140, 19, &vd->lens, 10.0, 120.0, 100, 0, "The lens angle in perspective view");
2129         uiBlockBeginAlign(block);
2130         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip Start:",      10, 106, 140, 19, &vd->near, vd->grid/10.0, 100.0, 10, 0, "Set the beginning of the range in which 3D objects are displayed (perspective view)");
2131         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip End:",        10, 86, 140, 19, &vd->far, 1.0, 1000.0*vd->grid, 100, 0, "Set the end of the range in which 3D objects are displayed (perspective view)");
2132         uiBlockEndAlign(block);
2133
2134         uiDefBut(block, LABEL, 1, "3D Cursor:",                         160, 150, 140, 19, NULL, 0.0, 0.0, 0, 0, "");
2135
2136         uiBlockBeginAlign(block);
2137         curs= give_cursor();
2138         uiDefButF(block, NUM, REDRAWVIEW3D, "X:",                       160, 130, 150, 22, curs, -1000.0*vd->grid, 1000.0*vd->grid, 10, 0, "X co-ordinate of the 3D cursor");
2139         uiDefButF(block, NUM, REDRAWVIEW3D, "Y:",                       160, 108, 150, 22, curs+1, -1000.0*vd->grid, 1000.0*vd->grid, 10, 0, "Y co-ordinate of the 3D cursor");
2140         uiDefButF(block, NUM, REDRAWVIEW3D, "Z:",                       160, 86, 150, 22, curs+2, -1000.0*vd->grid, 1000.0*vd->grid, 10, 0, "Z co-ordinate of the 3D cursor");
2141         uiBlockEndAlign(block);
2142
2143         uiDefBut(block, LABEL, 1, "Display:",                           10, 50, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2144         
2145         uiDefButBitS(block, TOG, V3D_SELECT_OUTLINE, REDRAWVIEW3D, "Outline Selected", 10, 30, 140, 19, &vd->flag, 0, 0, 0, 0, "Highlight selected objects with an outline, in Solid, Shaded or Textured viewport shading modes");
2146         uiDefButBitS(block, TOG, V3D_DRAW_CENTERS, REDRAWVIEW3D, "All Object Centers", 160, 30, 150, 19, &vd->flag, 0, 0, 0, 0, "Draw the center points on all objects");
2147
2148         uiDefButBitS(block, TOGN, V3D_HIDE_HELPLINES, REDRAWVIEW3D, "Relationship Lines", 10, 6, 140, 19, &vd->flag, 0, 0, 0, 0, "Draw dashed lines indicating Parent, Constraint, or Hook relationships");
2149
2150 }
2151
2152 static void view3d_panel_preview(ScrArea *sa, short cntrl)      // VIEW3D_HANDLER_PREVIEW
2153 {
2154         uiBlock *block;
2155         View3D *v3d= sa->spacedata.first;
2156         int ofsx, ofsy;
2157         
2158         block= uiNewBlock(&sa->uiblocks, "view3d_panel_preview", UI_EMBOSS, UI_HELV, sa->win);
2159         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
2160         uiSetPanelHandler(VIEW3D_HANDLER_PREVIEW);  // for close and esc
2161         
2162         ofsx= -150+(sa->winx/2)/v3d->blockscale;
2163         ofsy= -100+(sa->winy/2)/v3d->blockscale;
2164         if(uiNewPanel(sa, block, "Preview", "View3d", ofsx, ofsy, 300, 200)==0) return;
2165
2166         uiBlockSetDrawExtraFunc(block, BIF_view3d_previewdraw);
2167         
2168         if(G.scene->recalc & SCE_PRV_CHANGED) {
2169                 G.scene->recalc &= ~SCE_PRV_CHANGED;
2170                 //printf("found recalc\n");
2171                 BIF_view3d_previewrender_free(sa->spacedata.first);
2172                 BIF_preview_changed(0);
2173         }
2174 }
2175
2176
2177 static void view3d_blockhandlers(ScrArea *sa)
2178 {
2179         View3D *v3d= sa->spacedata.first;
2180         short a;
2181         
2182         /* warning; blocks need to be freed each time, handlers dont remove */
2183         uiFreeBlocksWin(&sa->uiblocks, sa->win);
2184
2185         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
2186         
2187                 switch(v3d->blockhandler[a]) {
2188
2189                 case VIEW3D_HANDLER_PROPERTIES:
2190                         view3d_panel_properties(v3d->blockhandler[a+1]);
2191                         break;
2192                 case VIEW3D_HANDLER_BACKGROUND:
2193                         view3d_panel_background(v3d->blockhandler[a+1]);
2194                         break;
2195                 case VIEW3D_HANDLER_OBJECT:
2196                         view3d_panel_object(v3d->blockhandler[a+1]);
2197                         break;
2198                 case VIEW3D_HANDLER_PREVIEW:
2199                         view3d_panel_preview(sa, v3d->blockhandler[a+1]);
2200                         break;
2201                         
2202                 }
2203                 /* clear action value for event */
2204                 v3d->blockhandler[a+1]= 0;
2205         }
2206         uiDrawBlocksPanels(sa, 0);
2207
2208 }
2209
2210 /* ****************** View3d afterdraw *************** */
2211
2212 typedef struct View3DAfter {
2213         struct View3DAfter *next, *prev;
2214         struct Base *base;
2215         int type;
2216 } View3DAfter;
2217
2218 /* temp storage of Objects that need to be drawn as last */
2219 void add_view3d_after(View3D *v3d, Base *base, int type)
2220 {
2221         View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
2222
2223         BLI_addtail(&v3d->afterdraw, v3da);
2224         v3da->base= base;
2225         v3da->type= type;
2226 }
2227
2228 /* clears zbuffer and draws it over */
2229 static void view3d_draw_xray(View3D *v3d, int flag)
2230 {
2231         View3DAfter *v3da, *next;
2232         int doit= 0;
2233         
2234         for(v3da= v3d->afterdraw.first; v3da; v3da= v3da->next)
2235                 if(v3da->type==V3D_XRAY) doit= 1;
2236         
2237         if(doit) {
2238                 if(v3d->zbuf) glClear(GL_DEPTH_BUFFER_BIT);
2239                 v3d->xray= TRUE;
2240                 
2241                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2242                         next= v3da->next;
2243                         if(v3da->type==V3D_XRAY) {
2244                                 draw_object(v3da->base, flag);
2245                                 BLI_remlink(&v3d->afterdraw, v3da);
2246                                 MEM_freeN(v3da);
2247                         }
2248                 }
2249                 v3d->xray= FALSE;
2250         }
2251 }
2252
2253 /* disables write in zbuffer and draws it over */
2254 static void view3d_draw_transp(View3D *v3d, int flag)
2255 {
2256         View3DAfter *v3da, *next;
2257
2258         glDepthMask(0);
2259         v3d->transp= TRUE;
2260                 
2261         for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2262                 next= v3da->next;
2263                 if(v3da->type==V3D_TRANSP) {
2264                         draw_object(v3da->base, flag);
2265                         BLI_remlink(&v3d->afterdraw, v3da);
2266                         MEM_freeN(v3da);
2267                 }
2268         }
2269         v3d->transp= FALSE;
2270
2271         glDepthMask(1);
2272
2273 }
2274
2275 /* *********************** */
2276
2277 static void draw_dupli_objects(View3D *v3d, Base *base)
2278 {
2279         ListBase *lb;
2280         DupliObject *dob;
2281         Base tbase;
2282         int color= (base->flag & SELECT)?TH_SELECT:TH_WIRE;
2283         char dt, dtx;
2284         
2285         /* debug */
2286         if(base->object->dup_group && base->object->dup_group->id.us<1)
2287                 color= TH_REDALERT;
2288         
2289         tbase.flag= OB_FROMDUPLI|base->flag;
2290         lb= object_duplilist(G.scene, base->object);
2291
2292         for(dob= lb->first; dob; dob= dob->next) {
2293                 tbase.object= dob->ob;
2294                 
2295                 Mat4CpyMat4(dob->ob->obmat, dob->mat);
2296                 /* extra service: draw the duplicator in drawtype of parent */
2297                 dt= tbase.object->dt; tbase.object->dt= base->object->dt;
2298                 dtx= tbase.object->dtx; tbase.object->dtx= base->object->dtx;
2299                 
2300                 BIF_ThemeColorBlend(color, TH_BACK, 0.5);
2301                 draw_object(&tbase, DRAW_CONSTCOLOR);
2302                 
2303                 /* restore */
2304                 Mat4CpyMat4(dob->ob->obmat, dob->omat);
2305                 tbase.object->dt= dt;
2306                 tbase.object->dtx= dtx;
2307         }
2308         /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
2309
2310         free_object_duplilist(lb);
2311                                 
2312 }
2313
2314 void drawview3dspace(ScrArea *sa, void *spacedata)
2315 {
2316         View3D *v3d= spacedata;
2317         Base *base;
2318         Object *ob;
2319         Scene *setscene;
2320         
2321         setwinmatrixview3d(sa->winx, sa->winy, NULL);   /* 0= no pick rect */
2322         setviewmatrixview3d();  /* note: calls where_is_object for camera... */
2323
2324         Mat4MulMat4(v3d->persmat, v3d->viewmat, sa->winmat);
2325         Mat4Invert(v3d->persinv, v3d->persmat);
2326         Mat4Invert(v3d->viewinv, v3d->viewmat);
2327
2328         /* calculate pixelsize factor once, is used for lamps and obcenters */
2329         {
2330                 float len1, len2, vec[3];
2331
2332                 VECCOPY(vec, v3d->persinv[0]);
2333                 len1= Normalise(vec);
2334                 VECCOPY(vec, v3d->persinv[1]);
2335                 len2= Normalise(vec);
2336                 
2337                 v3d->pixsize= 2.0f*(len1>len2?len1:len2);
2338                 
2339                 /* correct for window size */
2340                 if(sa->winx > sa->winy) v3d->pixsize/= (float)sa->winx;
2341                 else v3d->pixsize/= (float)sa->winy;
2342         }
2343         
2344         if(v3d->drawtype > OB_WIRE) {
2345                 if(G.f & G_SIMULATION)
2346                         glClearColor(0.0, 0.0, 0.0, 0.0); 
2347                 else {
2348                         float col[3];
2349                         BIF_GetThemeColor3fv(TH_BACK, col);
2350                         glClearColor(col[0], col[1], col[2], 0.0); 
2351                 }
2352                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2353                 
2354                 glLoadIdentity();
2355         }
2356         else {
2357                 float col[3];
2358                 BIF_GetThemeColor3fv(TH_BACK, col);
2359                 glClearColor(col[0], col[1], col[2], 0.0);
2360                 glClear(GL_COLOR_BUFFER_BIT);
2361         }
2362         
2363         myloadmatrix(v3d->viewmat);
2364         persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
2365
2366         if(v3d->flag & V3D_CLIPPING)
2367                 view3d_draw_clipping(v3d);
2368         
2369         /* set zbuffer after we draw clipping region */
2370         if(v3d->drawtype > OB_WIRE) {
2371                 v3d->zbuf= TRUE;
2372                 glEnable(GL_DEPTH_TEST);
2373         }
2374         
2375         // needs to be done always, gridview is adjusted in drawgrid() now
2376         v3d->gridview= v3d->grid;
2377         
2378         if(v3d->view==0 || v3d->persp!=0) {
2379                 drawfloor();
2380                 if(v3d->persp==2) {
2381                         if(G.scene->world) {
2382                                 if(G.scene->world->mode & WO_STARS) {
2383 //                                      RE_make_stars(star_stuff_init_func, star_stuff_vertex_func,
2384 //                                                                star_stuff_term_func);
2385                                 }
2386                         }
2387                         if(v3d->flag & V3D_DISPBGPIC) draw_bgpic();
2388                 }
2389         }
2390         else {
2391                 drawgrid();
2392
2393                 if(v3d->flag & V3D_DISPBGPIC) {
2394                         draw_bgpic();
2395                 }
2396         }
2397         
2398         if(v3d->flag & V3D_CLIPPING)
2399                 view3d_set_clipping(v3d);
2400         
2401         /* draw set first */
2402         setscene= G.scene->set;
2403         if(setscene) { /* if(G.scene->set) { */
2404         
2405                 base= setscene->base.first; /* base= G.scene->set->base.first; */
2406                 while(base) {
2407                         
2408                         if(v3d->lay & base->lay) {
2409
2410                                 object_handle_update(base->object);
2411                                 
2412                                 BIF_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2413                                 draw_object(base, DRAW_CONSTCOLOR);
2414
2415                                 if(base->object->transflag & OB_DUPLI) {
2416                                         draw_dupli_objects(v3d, base);
2417                                 }
2418                         }
2419                         
2420                         base= base->next;
2421                         if(base==0 && setscene && setscene->set) {
2422                                 setscene= setscene->set;
2423                                 base= setscene->base.first;
2424                         }
2425                 }
2426
2427                 /* Transp and X-ray afterdraw stuff */
2428                 view3d_draw_xray(v3d, DRAW_CONSTCOLOR); // clears zbuffer if it is used!
2429                 view3d_draw_transp(v3d, DRAW_CONSTCOLOR);
2430         }
2431         
2432         /* update all objects, ipos, matrices, displists, etc. Flags set by depgraph or manual, no layer check here, gets correct flushed */
2433         for(base= G.scene->base.first; base; base= base->next) {
2434                 object_handle_update(base->object);   // bke_object.h
2435         }
2436         
2437         /* then draw not selected and the duplis, but skip editmode object */
2438         for(base= G.scene->base.first; base; base= base->next) {
2439                 if(v3d->lay & base->lay) {
2440                         
2441                         /* dupli drawing */
2442                         if(base->object->transflag & OB_DUPLI) {
2443                                 draw_dupli_objects(v3d, base);
2444                         }
2445                         if((base->flag & SELECT)==0) {
2446                                 if(base->object!=G.obedit) draw_object(base, 0);
2447                         }
2448                 }
2449         }
2450         /* draw selected and editmode */
2451         for(base= G.scene->base.first; base; base= base->next) {
2452                 if(v3d->lay & base->lay) {
2453                         if (base->object==G.obedit || ( base->flag & SELECT) ) 
2454                                 draw_object(base, 0);
2455                 }
2456         }
2457
2458         if(G.moving) {
2459                 BIF_drawConstraint();
2460                 if(G.obedit) BIF_drawPropCircle();      // only editmode has proportional edit
2461         }
2462
2463         if(G.scene->radio) RAD_drawall(v3d->drawtype>=OB_SOLID);
2464         
2465         /* Transp and X-ray afterdraw stuff */
2466         view3d_draw_xray(v3d, 0);       // clears zbuffer if it is used!
2467         view3d_draw_transp(v3d, 0);
2468         
2469         if(v3d->flag & V3D_CLIPPING)
2470                 view3d_clr_clipping();
2471         
2472         BIF_draw_manipulator(sa);
2473                 
2474         if(v3d->zbuf) {
2475                 v3d->zbuf= FALSE;
2476                 glDisable(GL_DEPTH_TEST);
2477         }
2478
2479         persp(PERSP_WIN);  // set ortho
2480         
2481         if(v3d->persp>1) drawviewborder();
2482         if(!(G.f & G_PLAYANIM)) drawcursor(v3d);
2483         draw_view_icon();
2484
2485         ob= OBACT;
2486         if(ob && (U.uiflag & USER_DRAWVIEWINFO)) 
2487                 draw_selected_name(ob);
2488         
2489         draw_area_emboss(sa);
2490         
2491         /* it is important to end a view in a transform compatible with buttons */
2492
2493         bwin_scalematrix(sa->win, v3d->blockscale, v3d->blockscale, v3d->blockscale);
2494         view3d_blockhandlers(sa);
2495
2496         sa->win_swap= WIN_BACK_OK;
2497         
2498         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
2499                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2500                 addafterqueue(sa->win, BACKBUFDRAW, 1);
2501         }
2502         // test for backbuf select
2503         if(G.obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT)) {
2504                 extern int afterqtest(short win, unsigned short evt);   //editscreen.c
2505
2506                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2507                 if(afterqtest(sa->win, BACKBUFDRAW)==0) {
2508                         addafterqueue(sa->win, BACKBUFDRAW, 1);
2509                 }
2510         }
2511
2512         /* run any view3d draw handler script links */
2513         if (sa->scriptlink.totscript)
2514                 BPY_do_spacehandlers(sa, 0, SPACEHANDLER_VIEW3D_DRAW);
2515
2516         /* run scene redraw script links */
2517         if((G.f & G_DOSCRIPTLINKS) && G.scene->scriptlink.totscript &&
2518                         !during_script()) {
2519                 BPY_do_pyscript((ID *)G.scene, SCRIPT_REDRAW);
2520         }
2521
2522 }
2523
2524
2525 void drawview3d_render(struct View3D *v3d, int winx, int winy)
2526 {
2527         Base *base;
2528         Scene *setscene;
2529         float winmat[4][4];
2530         
2531         update_for_newframe_muted();    /* first, since camera can be animated */
2532
2533         setwinmatrixview3d(winx, winy, NULL);
2534         
2535         setviewmatrixview3d();
2536         myloadmatrix(v3d->viewmat);
2537         glMatrixMode(GL_PROJECTION);
2538         mygetmatrix(winmat);
2539         glMatrixMode(GL_MODELVIEW);
2540         Mat4MulMat4(v3d->persmat, v3d->viewmat, winmat);
2541         Mat4Invert(v3d->persinv, v3d->persmat);
2542         Mat4Invert(v3d->viewinv, v3d->viewmat);
2543
2544         free_all_realtime_images();
2545         reshadeall_displist();
2546         
2547         if(v3d->drawtype > OB_WIRE) {
2548                 v3d->zbuf= TRUE;
2549                 glEnable(GL_DEPTH_TEST);
2550         }
2551
2552         if(v3d->flag & V3D_CLIPPING)
2553                 view3d_set_clipping(v3d);
2554
2555         if (v3d->drawtype==OB_TEXTURE && G.scene->world) {
2556                 glClearColor(G.scene->world->horr, G.scene->world->horg, G.scene->world->horb, 0.0); 
2557         } else {
2558                 float col[3];
2559                 BIF_GetThemeColor3fv(TH_BACK, col);
2560                 glClearColor(col[0], col[1], col[2], 0.0); 
2561         }
2562         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2563
2564         /* abuse! to make sure it doesnt draw the helpstuff */
2565         G.f |= G_SIMULATION;
2566
2567         /* first draw set */
2568         setscene= G.scene->set;
2569         if(setscene) { /* if(G.scene->set) { */
2570         
2571                 base= setscene->base.first; /* base= G.scene->set->base.first; */
2572                 while(base) {
2573                         if(v3d->lay & base->lay) {
2574                                 if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
2575                                 else {
2576                                         where_is_object(base->object);
2577         
2578                                         BIF_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2579                                         draw_object(base, DRAW_CONSTCOLOR);
2580         
2581                                         if(base->object->transflag & OB_DUPLI) {
2582                                                 draw_dupli_objects(v3d, base);
2583                                         }
2584                                 }
2585                         }
2586                         base= base->next;
2587                         if(base==0 && setscene && setscene->set) {
2588                                 setscene= setscene->set;
2589                                 base= setscene->base.first;
2590                         }
2591                 }
2592                 
2593                 /* Transp and X-ray afterdraw stuff */
2594                 view3d_draw_xray(v3d, DRAW_CONSTCOLOR); // clears zbuffer if it is used!
2595                 view3d_draw_transp(v3d, DRAW_CONSTCOLOR);
2596         }
2597
2598         /* first not selected and duplis */
2599         base= G.scene->base.first;
2600         while(base) {
2601                 
2602                 if(v3d->lay & base->lay) {
2603                         if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
2604                         else {
2605         
2606                                 if(base->object->transflag & OB_DUPLI) {
2607                                         draw_dupli_objects(v3d, base);
2608                                 }
2609                                 else if((base->flag & SELECT)==0) {
2610                                         draw_object(base, 0);
2611                                 }
2612                         }
2613                 }
2614                 
2615                 base= base->next;
2616         }
2617
2618         /* draw selected */
2619         base= G.scene->base.first;
2620         while(base) {
2621                 
2622                 if ( ((base)->flag & SELECT) && ((base)->lay & v3d->lay) ) {
2623                         if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
2624                         else draw_object(base, 0);
2625                 }
2626                 
2627                 base= base->next;
2628         }
2629
2630         if(G.scene->radio) RAD_drawall(v3d->drawtype>=OB_SOLID);
2631
2632         /* Transp and X-ray afterdraw stuff */
2633         view3d_draw_xray(v3d, 0);       // clears zbuffer if it is used!
2634         view3d_draw_transp(v3d, 0);
2635         
2636         if(v3d->flag & V3D_CLIPPING)
2637                 view3d_clr_clipping();
2638         
2639         if(v3d->zbuf) {
2640                 v3d->zbuf= FALSE;
2641                 glDisable(GL_DEPTH_TEST);
2642         }
2643         
2644         G.f &= ~G_SIMULATION;
2645
2646         glFlush();
2647
2648         glLoadIdentity();
2649
2650         free_all_realtime_images();
2651 }
2652
2653
2654 double tottime = 0.0;
2655
2656 int update_time(void)
2657 {
2658         static double ltime;
2659         double time;
2660
2661         if ((U.mixbufsize)&&(audiostream_pos() != CFRA)&&(G.scene->audio.flag & AUDIO_SYNC)) return 0;
2662
2663         time = PIL_check_seconds_timer();
2664         
2665         tottime += (time - ltime);
2666         ltime = time;
2667         return (tottime < 0.0);
2668 }
2669
2670 void inner_play_anim_loop(int init, int mode)
2671 {
2672         ScrArea *sa;
2673         static ScrArea *oldsa;
2674         static double swaptime;
2675         static int curmode;
2676
2677         /* init */
2678         if(init) {
2679                 oldsa= curarea;
2680                 swaptime= 1.0/(float)G.scene->r.frs_sec;
2681                 tottime= 0.0;
2682                 curmode= mode;
2683
2684                 return;
2685         }
2686
2687         set_timecursor(CFRA);
2688         
2689         update_for_newframe_muted();
2690
2691         sa= G.curscreen->areabase.first;
2692         while(sa) {
2693                 if(sa==oldsa) {
2694                         scrarea_do_windraw(sa);
2695                 }
2696                 else if(curmode & 1) { /* catch modes 1 and 3 */
2697                         if ELEM(sa->spacetype, SPACE_VIEW3D, SPACE_SEQ) {
2698                                 scrarea_do_windraw(sa);
2699                         }
2700                 }
2701                 
2702                 sa= sa->next;   
2703         }
2704         
2705         /* make sure that swaptime passed by */
2706         tottime -= swaptime;
2707         while (update_time()) PIL_sleep_ms(1);
2708
2709         if(CFRA>=EFRA) {
2710                 if (tottime > 0.0) tottime = 0.0;
2711                 CFRA= SFRA;
2712                 audiostream_stop();
2713                 audiostream_start( CFRA );
2714         }
2715         else {
2716                 if (U.mixbufsize && (G.scene->audio.flag & AUDIO_SYNC)) CFRA = audiostream_pos();
2717                 else CFRA++;
2718         }
2719 }
2720
2721 /* play_anim: 'mode' defines where to play and if repeat is on:
2722  * - 0: current area
2723  * - 1: all view3d and seq areas
2724  * - 2: current area, no replay
2725  * - 3: all view3d and seq areas, no replay */
2726 int play_anim(int mode)
2727 {
2728         ScrArea *sa, *oldsa;
2729         int cfraont;
2730         unsigned short event=0;
2731         short val;
2732
2733         /* patch for very very old scenes */
2734         if(SFRA==0) SFRA= 1;
2735         if(EFRA==0) EFRA= 250;
2736
2737         if(SFRA>EFRA) return 0;
2738         
2739         update_time();
2740
2741         /* waitcursor(1); */
2742         G.f |= G_PLAYANIM;              /* in sequence.c and view.c this is handled */
2743
2744         cfraont= CFRA;
2745         oldsa= curarea;
2746
2747         audiostream_start( CFRA );
2748         
2749         inner_play_anim_loop(1, mode);  /* 1==init */
2750         
2751          /* forces all buffers to be OK for current frame (otherwise other windows get redrawn with CFRA+1) */
2752         curarea->win_swap= WIN_BACK_OK;
2753         screen_swapbuffers();
2754         
2755         while(TRUE) {
2756
2757                 while(qtest()) {
2758                         
2759                         /* we test events first because of MKEY event */
2760                         
2761                         event= extern_qread(&val);
2762                         if(event==ESCKEY) break;
2763                         else if(event==MIDDLEMOUSE) {
2764                                 if(U.flag & USER_VIEWMOVE) {
2765                                         if(G.qual & LR_SHIFTKEY) viewmove(0);
2766                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
2767                                         else viewmove(1);
2768                                 }
2769                                 else {
2770                                         if(G.qual & LR_SHIFTKEY) viewmove(1);
2771                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
2772                                         else viewmove(0);
2773                                 }
2774                         }
2775                         else if(event==MKEY) {
2776                                 if(val) add_timeline_marker(CFRA-1);
2777                         }
2778                 }
2779                 if(ELEM3(event, ESCKEY, SPACEKEY, RIGHTMOUSE)) break;
2780                 
2781                 inner_play_anim_loop(0, 0);
2782                 screen_swapbuffers();
2783                                 
2784                 if((mode > 1) && CFRA==EFRA) break; /* no replay */     
2785         }
2786
2787         if(event==SPACEKEY);
2788         else CFRA= cfraont;
2789         
2790         audiostream_stop();
2791
2792         if(oldsa!=curarea) areawinset(oldsa->win);
2793         
2794         /* restore all areas */
2795         sa= G.curscreen->areabase.first;
2796         while(sa) {
2797                 if( (mode && sa->spacetype==SPACE_VIEW3D) || sa==curarea) addqueue(sa->win, REDRAW, 1);
2798                 sa= sa->next;   
2799         }
2800         
2801         /* groups could have changed ipo */
2802         allspace(REMAKEIPO, 0);
2803         allqueue(REDRAWIPO, 0);
2804         allqueue(REDRAWNLA, 0);
2805         allqueue (REDRAWACTION, 0);
2806         
2807         /* restore for cfra */
2808         update_for_newframe_muted();
2809
2810         waitcursor(0);
2811         G.f &= ~G_PLAYANIM;
2812         
2813         if (event==ESCKEY || event==SPACEKEY) return 1;
2814         else return 0;
2815 }