Cycles: svn merge -r40266:40358 https://svn.blender.org/svnroot/bf-blender/trunk...
[blender-staging.git] / source / blender / editors / object / object_edit.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2008 full recode
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/object/object_edit.c
29  *  \ingroup edobj
30  */
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <math.h>
35 #include <time.h>
36 #include <float.h>
37 #include <ctype.h>
38 #include <stddef.h> //for offsetof
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_utildefines.h"
45 #include "BLI_editVert.h"
46 #include "BLI_ghash.h"
47 #include "BLI_rand.h"
48
49 #include "DNA_armature_types.h"
50 #include "DNA_curve_types.h"
51 #include "DNA_group_types.h"
52 #include "DNA_lattice_types.h"
53 #include "DNA_material_types.h"
54 #include "DNA_meta_types.h"
55 #include "DNA_property_types.h"
56 #include "DNA_scene_types.h"
57 #include "DNA_object_types.h"
58 #include "DNA_object_force.h"
59 #include "DNA_meshdata_types.h"
60 #include "DNA_vfont_types.h"
61
62 #include "IMB_imbuf_types.h"
63
64 #include "BKE_anim.h"
65 #include "BKE_constraint.h"
66 #include "BKE_context.h"
67 #include "BKE_curve.h"
68 #include "BKE_effect.h"
69 #include "BKE_depsgraph.h"
70 #include "BKE_font.h"
71 #include "BKE_image.h"
72 #include "BKE_library.h"
73 #include "BKE_main.h"
74 #include "BKE_material.h"
75 #include "BKE_mball.h"
76 #include "BKE_mesh.h"
77 #include "BKE_object.h"
78 #include "BKE_paint.h"
79 #include "BKE_pointcache.h"
80 #include "BKE_property.h"
81 #include "BKE_sca.h"
82 #include "BKE_softbody.h"
83 #include "BKE_modifier.h"
84
85 #include "ED_armature.h"
86 #include "ED_curve.h"
87 #include "ED_mesh.h"
88 #include "ED_mball.h"
89 #include "ED_lattice.h"
90 #include "ED_object.h"
91 #include "ED_screen.h"
92 #include "ED_util.h"
93
94
95 #include "RNA_access.h"
96 #include "RNA_define.h"
97 #include "RNA_enum_types.h"
98
99 /* for menu/popup icons etc etc*/
100
101 #include "UI_interface.h"
102 #include "WM_api.h"
103 #include "WM_types.h"
104
105 #include "object_intern.h"      // own include
106
107 /* ************* XXX **************** */
108 static void error(const char *UNUSED(arg)) {}
109 static void waitcursor(int UNUSED(val)) {}
110 static int pupmenu(const char *UNUSED(msg)) {return 0;}
111
112 /* port over here */
113 static bContext *evil_C;
114 static void error_libdata(void) {}
115
116
117 /* find the correct active object per context
118  * note: context can be NULL when called from a enum with PROP_ENUM_NO_CONTEXT */
119 Object *ED_object_active_context(bContext *C)
120 {
121         Object *ob= NULL;
122         if(C) {
123                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
124                 if (!ob) ob= CTX_data_active_object(C);
125         }
126         return ob;
127 }
128
129
130 /* ********* clear/set restrict view *********/
131 static int object_hide_view_clear_exec(bContext *C, wmOperator *UNUSED(op))
132 {
133         Main *bmain= CTX_data_main(C);
134         ScrArea *sa= CTX_wm_area(C);
135         View3D *v3d= sa->spacedata.first;
136         Scene *scene= CTX_data_scene(C);
137         Base *base;
138         int changed = 0;
139         
140         /* XXX need a context loop to handle such cases */
141         for(base = FIRSTBASE; base; base=base->next){
142                 if((base->lay & v3d->lay) && base->object->restrictflag & OB_RESTRICT_VIEW) {
143                         base->flag |= SELECT;
144                         base->object->flag = base->flag;
145                         base->object->restrictflag &= ~OB_RESTRICT_VIEW; 
146                         changed = 1;
147                 }
148         }
149         if (changed) {
150                 DAG_id_type_tag(bmain, ID_OB);
151                 DAG_scene_sort(bmain, scene);
152                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
153         }
154
155         return OPERATOR_FINISHED;
156 }
157
158 void OBJECT_OT_hide_view_clear(wmOperatorType *ot)
159 {
160         
161         /* identifiers */
162         ot->name= "Clear Restrict View";
163         ot->description = "Reveal the object by setting the hide flag";
164         ot->idname= "OBJECT_OT_hide_view_clear";
165         
166         /* api callbacks */
167         ot->exec= object_hide_view_clear_exec;
168         ot->poll= ED_operator_view3d_active;
169         
170         /* flags */
171         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
172 }
173
174 static int object_hide_view_set_exec(bContext *C, wmOperator *op)
175 {
176         Main *bmain= CTX_data_main(C);
177         Scene *scene= CTX_data_scene(C);
178         short changed = 0;
179         const int unselected= RNA_boolean_get(op->ptr, "unselected");
180         
181         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
182                 if(!unselected) {
183                         if (base->flag & SELECT){
184                                 base->flag &= ~SELECT;
185                                 base->object->flag = base->flag;
186                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
187                                 changed = 1;
188                                 if (base==BASACT) {
189                                         ED_base_object_activate(C, NULL);
190                                 }
191                         }
192                 }
193                 else {
194                         if (!(base->flag & SELECT)){
195                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
196                                 changed = 1;
197                         }
198                 }       
199         }
200         CTX_DATA_END;
201
202         if (changed) {
203                 DAG_id_type_tag(bmain, ID_OB);
204                 DAG_scene_sort(bmain, scene);
205                 
206                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
207                 
208         }
209
210         return OPERATOR_FINISHED;
211 }
212
213 void OBJECT_OT_hide_view_set(wmOperatorType *ot)
214 {
215         /* identifiers */
216         ot->name= "Set Restrict View";
217         ot->description = "Hide the object by setting the hide flag";
218         ot->idname= "OBJECT_OT_hide_view_set";
219         
220         /* api callbacks */
221         ot->exec= object_hide_view_set_exec;
222         ot->poll= ED_operator_view3d_active;
223         
224         /* flags */
225         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
226         
227         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
228         
229 }
230
231 /* 99% same as above except no need for scene refreshing (TODO, update render preview) */
232 static int object_hide_render_clear_exec(bContext *C, wmOperator *UNUSED(op))
233 {
234         short changed= 0;
235
236         /* XXX need a context loop to handle such cases */
237         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
238                 if(ob->restrictflag & OB_RESTRICT_RENDER) {
239                         ob->restrictflag &= ~OB_RESTRICT_RENDER;
240                         changed= 1;
241                 }
242         }
243         CTX_DATA_END;
244
245         if(changed)
246                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_OUTLINER, NULL);
247
248         return OPERATOR_FINISHED;
249 }
250
251 void OBJECT_OT_hide_render_clear(wmOperatorType *ot)
252 {
253
254         /* identifiers */
255         ot->name= "Clear Restrict Render";
256         ot->description = "Reveal the render object by setting the hide render flag";
257         ot->idname= "OBJECT_OT_hide_render_clear";
258
259         /* api callbacks */
260         ot->exec= object_hide_render_clear_exec;
261         ot->poll= ED_operator_view3d_active;
262
263         /* flags */
264         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
265 }
266
267 static int object_hide_render_set_exec(bContext *C, wmOperator *op)
268 {
269         const int unselected= RNA_boolean_get(op->ptr, "unselected");
270
271         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
272                 if(!unselected) {
273                         if (base->flag & SELECT){
274                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
275                         }
276                 }
277                 else {
278                         if (!(base->flag & SELECT)){
279                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
280                         }
281                 }
282         }
283         CTX_DATA_END;
284         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_OUTLINER, NULL);
285         return OPERATOR_FINISHED;
286 }
287
288 void OBJECT_OT_hide_render_set(wmOperatorType *ot)
289 {
290         /* identifiers */
291         ot->name= "Set Restrict Render";
292         ot->description = "Hide the render object by setting the hide render flag";
293         ot->idname= "OBJECT_OT_hide_render_set";
294
295         /* api callbacks */
296         ot->exec= object_hide_render_set_exec;
297         ot->poll= ED_operator_view3d_active;
298
299         /* flags */
300         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
301
302         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
303 }
304
305 /* ******************* toggle editmode operator  ***************** */
306
307 void ED_object_exit_editmode(bContext *C, int flag)
308 {
309         /* Note! only in exceptional cases should 'EM_DO_UNDO' NOT be in the flag */
310
311         Scene *scene= CTX_data_scene(C);
312         Object *obedit= CTX_data_edit_object(C);
313         int freedata = flag & EM_FREEDATA;
314         
315         if(obedit==NULL) return;
316         
317         if(flag & EM_WAITCURSOR) waitcursor(1);
318         if(obedit->type==OB_MESH) {
319                 Mesh *me= obedit->data;
320                 
321 //              if(EM_texFaceCheck())
322                 
323 //              if(retopo_mesh_paint_check())
324 //                      retopo_end_okee();
325                 
326                 if(me->edit_mesh->totvert>MESH_MAX_VERTS) {
327                         error("Too many vertices");
328                         return;
329                 }
330                 load_editMesh(scene, obedit);
331                 
332                 if(freedata) {
333                         free_editMesh(me->edit_mesh);
334                         MEM_freeN(me->edit_mesh);
335                         me->edit_mesh= NULL;
336                 }
337                 
338                 if(obedit->restore_mode & OB_MODE_WEIGHT_PAINT) {
339                         mesh_octree_table(NULL, NULL, NULL, 'e');
340                         mesh_mirrtopo_table(NULL, 'e');
341                 }
342         }
343         else if (obedit->type==OB_ARMATURE) {   
344                 ED_armature_from_edit(obedit);
345                 if(freedata)
346                         ED_armature_edit_free(obedit);
347         }
348         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
349                 load_editNurb(obedit);
350                 if(freedata) free_editNurb(obedit);
351         }
352         else if(obedit->type==OB_FONT && freedata) {
353                 load_editText(obedit);
354                 if(freedata) free_editText(obedit);
355         }
356         else if(obedit->type==OB_LATTICE) {
357                 load_editLatt(obedit);
358                 if(freedata) free_editLatt(obedit);
359         }
360         else if(obedit->type==OB_MBALL) {
361                 load_editMball(obedit);
362                 if(freedata) free_editMball(obedit);
363         }
364
365         /* freedata only 0 now on file saves and render */
366         if(freedata) {
367                 ListBase pidlist;
368                 PTCacheID *pid;
369
370                 /* for example; displist make is different in editmode */
371                 scene->obedit= NULL; // XXX for context
372
373                 /* flag object caches as outdated */
374                 BKE_ptcache_ids_from_object(&pidlist, obedit, NULL, 0);
375                 for(pid=pidlist.first; pid; pid=pid->next) {
376                         if(pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
377                                 pid->cache->flag |= PTCACHE_OUTDATED;
378                 }
379                 BLI_freelistN(&pidlist);
380                 
381                 BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_OUTDATED);
382
383                 /* also flush ob recalc, doesn't take much overhead, but used for particles */
384                 DAG_id_tag_update(&obedit->id, OB_RECALC_OB|OB_RECALC_DATA);
385         
386                 if(flag & EM_DO_UNDO)
387                         ED_undo_push(C, "Editmode");
388         
389                 if(flag & EM_WAITCURSOR) waitcursor(0);
390         
391                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
392
393                 obedit->mode &= ~OB_MODE_EDIT;
394         }
395 }
396
397
398 void ED_object_enter_editmode(bContext *C, int flag)
399 {
400         Scene *scene= CTX_data_scene(C);
401         Base *base= NULL;
402         Object *ob;
403         ScrArea *sa= CTX_wm_area(C);
404         View3D *v3d= NULL;
405         int ok= 0;
406         
407         if(scene->id.lib) return;
408         
409         if(sa && sa->spacetype==SPACE_VIEW3D)
410                 v3d= sa->spacedata.first;
411         
412         if((flag & EM_IGNORE_LAYER)==0) {
413                 base= CTX_data_active_base(C); /* active layer checked here for view3d */
414
415                 if(base==NULL) return;
416                 else if(v3d && (base->lay & v3d->lay)==0) return;
417                 else if(!v3d && (base->lay & scene->lay)==0) return;
418         }
419         else {
420                 base= scene->basact;
421         }
422
423         if (ELEM3(NULL, base, base->object, base->object->data)) return;
424
425         ob = base->object;
426         
427         if (object_data_is_libdata(ob)) {
428                 error_libdata();
429                 return;
430         }
431         
432         if(flag & EM_WAITCURSOR) waitcursor(1);
433
434         ob->restore_mode = ob->mode;
435
436         /* note, when switching scenes the object can have editmode data but
437          * not be scene->obedit: bug 22954, this avoids calling self eternally */
438         if((ob->restore_mode & OB_MODE_EDIT)==0)
439                 ED_object_toggle_modes(C, ob->mode);
440
441         ob->mode= OB_MODE_EDIT;
442         
443         if(ob->type==OB_MESH) {
444                 Mesh *me= ob->data;
445                 
446                 if(me->pv) mesh_pmv_off(me);
447                 ok= 1;
448                 scene->obedit= ob;      // context sees this
449                 
450                 make_editMesh(scene, ob);
451
452                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MESH, scene);
453         }
454         else if (ob->type==OB_ARMATURE){
455                 bArmature *arm= base->object->data;
456                 if (!arm) return;
457                 /*
458                  * The function object_data_is_libdata make a problem here, the
459                  * check for ob->proxy return 0 and let blender enter to edit mode
460                  * this causa a crash when you try leave the edit mode.
461                  * The problem is that i can't remove the ob->proxy check from
462                  * object_data_is_libdata that prevent the bugfix #6614, so
463                  * i add this little hack here.
464                  */
465                 if(arm->id.lib) {
466                         error_libdata();
467                         return;
468                 }
469                 ok=1;
470                 scene->obedit= ob;
471                 ED_armature_to_edit(ob);
472                 /* to ensure all goes in restposition and without striding */
473                 DAG_id_tag_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME); // XXX: should this be OB_RECALC_DATA?
474
475                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_ARMATURE, scene);
476         }
477         else if(ob->type==OB_FONT) {
478                 scene->obedit= ob; // XXX for context
479                 ok= 1;
480                  make_editText(ob);
481
482                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_TEXT, scene);
483         }
484         else if(ob->type==OB_MBALL) {
485                 scene->obedit= ob; // XXX for context
486                 ok= 1;
487                 make_editMball(ob);
488
489                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MBALL, scene);
490         }
491         else if(ob->type==OB_LATTICE) {
492                 scene->obedit= ob; // XXX for context
493                 ok= 1;
494                 make_editLatt(ob);
495                 
496                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_LATTICE, scene);
497         }
498         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
499                 ok= 1;
500                 scene->obedit= ob; // XXX for context
501                 make_editNurb(ob);
502                 
503                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_CURVE, scene);
504         }
505         
506         if(ok) {
507                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
508         }
509         else {
510                 scene->obedit= NULL; // XXX for context
511                 ob->mode &= ~OB_MODE_EDIT;
512                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
513         }
514         
515         if(flag & EM_DO_UNDO) ED_undo_push(C, "Enter Editmode");
516         if(flag & EM_WAITCURSOR) waitcursor(0);
517 }
518
519 static int editmode_toggle_exec(bContext *C, wmOperator *UNUSED(op))
520 {
521         if(!CTX_data_edit_object(C))
522                 ED_object_enter_editmode(C, EM_WAITCURSOR);
523         else
524                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* had EM_DO_UNDO but op flag calls undo too [#24685] */
525         
526         return OPERATOR_FINISHED;
527 }
528
529 static int editmode_toggle_poll(bContext *C)
530 {
531         Object *ob = CTX_data_active_object(C);
532
533         /* covers proxies too */
534         if(ELEM(NULL, ob, ob->data) || ((ID *)ob->data)->lib)
535                 return 0;
536
537         if (ob->restrictflag & OB_RESTRICT_VIEW)
538                 return 0;
539
540         return (ob->type == OB_MESH || ob->type == OB_ARMATURE ||
541                           ob->type == OB_FONT || ob->type == OB_MBALL ||
542                           ob->type == OB_LATTICE || ob->type == OB_SURF ||
543                           ob->type == OB_CURVE);
544 }
545
546 void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
547 {
548         
549         /* identifiers */
550         ot->name= "Toggle Editmode";
551         ot->description = "Toggle object's editmode";
552         ot->idname= "OBJECT_OT_editmode_toggle";
553         
554         /* api callbacks */
555         ot->exec= editmode_toggle_exec;
556         
557         ot->poll= editmode_toggle_poll;
558         
559         /* flags */
560         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
561 }
562
563 /* *************************** */
564
565 static int posemode_exec(bContext *C, wmOperator *UNUSED(op))
566 {
567         Base *base= CTX_data_active_base(C);
568         
569         if(base->object->type==OB_ARMATURE) {
570                 if(base->object==CTX_data_edit_object(C)) {
571                         ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
572                         ED_armature_enter_posemode(C, base);
573                 }
574                 else if(base->object->mode & OB_MODE_POSE)
575                         ED_armature_exit_posemode(C, base);
576                 else
577                         ED_armature_enter_posemode(C, base);
578                 
579                 return OPERATOR_FINISHED;
580         }
581         
582         return OPERATOR_PASS_THROUGH;
583 }
584
585 void OBJECT_OT_posemode_toggle(wmOperatorType *ot) 
586 {
587         /* identifiers */
588         ot->name= "Toggle Pose Mode";
589         ot->idname= "OBJECT_OT_posemode_toggle";
590         ot->description= "Enables or disables posing/selecting bones";
591         
592         /* api callbacks */
593         ot->exec= posemode_exec;
594         ot->poll= ED_operator_object_active_editable;
595         
596         /* flag */
597         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
598 }
599
600 /* *********************** */
601
602 #if 0
603 // XXX should be in view3d?
604
605 /* context: ob = lamp */
606 /* code should be replaced with proper (custom) transform handles for lamp properties */
607 static void spot_interactive(Object *ob, int mode)
608 {
609         Lamp *la= ob->data;
610         float transfac, dx, dy, ratio, origval;
611         int keep_running= 1, center2d[2];
612         int mval[2], mvalo[2];
613         
614 //      getmouseco_areawin(mval);
615 //      getmouseco_areawin(mvalo);
616         
617         project_int(ob->obmat[3], center2d);
618         if( center2d[0] > 100000 ) {            /* behind camera */
619 //              center2d[0]= curarea->winx/2;
620 //              center2d[1]= curarea->winy/2;
621         }
622
623 //      helpline(mval, center2d);
624         
625         /* ratio is like scaling */
626         dx = (float)(center2d[0] - mval[0]);
627         dy = (float)(center2d[1] - mval[1]);
628         transfac = (float)sqrt( dx*dx + dy*dy);
629         if(transfac==0.0f) transfac= 1.0f;
630         
631         if(mode==1)     
632                 origval= la->spotsize;
633         else if(mode==2)        
634                 origval= la->dist;
635         else if(mode==3)        
636                 origval= la->clipsta;
637         else    
638                 origval= la->clipend;
639         
640         while (keep_running>0) {
641                 
642 //              getmouseco_areawin(mval);
643                 
644                 /* essential for idling subloop */
645                 if(mval[0]==mvalo[0] && mval[1]==mvalo[1]) {
646                         PIL_sleep_ms(2);
647                 }
648                 else {
649                         char str[32];
650                         
651                         dx = (float)(center2d[0] - mval[0]);
652                         dy = (float)(center2d[1] - mval[1]);
653                         ratio = (float)(sqrt( dx*dx + dy*dy))/transfac;
654                         
655                         /* do the trick */
656                         
657                         if(mode==1) {   /* spot */
658                                 la->spotsize = ratio*origval;
659                                 CLAMP(la->spotsize, 1.0f, 180.0f);
660                                 sprintf(str, "Spot size %.2f\n", la->spotsize);
661                         }
662                         else if(mode==2) {      /* dist */
663                                 la->dist = ratio*origval;
664                                 CLAMP(la->dist, 0.01f, 5000.0f);
665                                 sprintf(str, "Distance %.2f\n", la->dist);
666                         }
667                         else if(mode==3) {      /* sta */
668                                 la->clipsta = ratio*origval;
669                                 CLAMP(la->clipsta, 0.001f, 5000.0f);
670                                 sprintf(str, "Distance %.2f\n", la->clipsta);
671                         }
672                         else if(mode==4) {      /* end */
673                                 la->clipend = ratio*origval;
674                                 CLAMP(la->clipend, 0.1f, 5000.0f);
675                                 sprintf(str, "Clip End %.2f\n", la->clipend);
676                         }
677
678                         /* cleanup */
679                         mvalo[0]= mval[0];
680                         mvalo[1]= mval[1];
681                         
682                         /* handle shaded mode */
683 // XXX                  shade_buttons_change_3d();
684
685                         /* DRAW */      
686                         headerprint(str);
687                         force_draw_plus(SPACE_BUTS, 0);
688
689 //                      helpline(mval, center2d);
690                 }
691                 
692                 while( qtest() ) {
693                         short val;
694                         unsigned short event= extern_qread(&val);
695                         
696                         switch (event){
697                                 case ESCKEY:
698                                 case RIGHTMOUSE:
699                                         keep_running= 0;
700                                         break;
701                                 case LEFTMOUSE:
702                                 case SPACEKEY:
703                                 case PADENTER:
704                                 case RETKEY:
705                                         if(val)
706                                                 keep_running= -1;
707                                         break;
708                         }
709                 }
710         }
711
712         if(keep_running==0) {
713                 if(mode==1)     
714                         la->spotsize= origval;
715                 else if(mode==2)        
716                         la->dist= origval;
717                 else if(mode==3)        
718                         la->clipsta= origval;
719                 else    
720                         la->clipend= origval;
721         }
722
723 }
724 #endif
725
726 static void UNUSED_FUNCTION(special_editmenu)(Scene *scene, View3D *v3d)
727 {
728 // XXX  static short numcuts= 2;
729         Object *ob= OBACT;
730         Object *obedit= NULL; // XXX
731         int nr,ret=0;
732         
733         if(ob==NULL) return;
734         
735         if(obedit==NULL) {
736                 
737                 if(ob->mode & OB_MODE_POSE) {
738 // XXX                  pose_special_editmenu();
739                 }
740                 else if(paint_facesel_test(ob)) {
741                         Mesh *me= get_mesh(ob);
742                         MTFace *tface;
743                         MFace *mface;
744                         int a;
745
746                         if(me==NULL || me->mtface==NULL) return;
747
748                         nr= pupmenu("Specials%t|Set     Tex%x1|         Shared%x2|         Light%x3|         Invisible%x4|         Collision%x5|         TwoSide%x6|Clr     Tex%x7|         Shared%x8|         Light%x9|         Invisible%x10|         Collision%x11|         TwoSide%x12");
749                         
750                         tface= me->mtface;
751                         mface= me->mface;
752                         for(a=me->totface; a>0; a--, tface++, mface++) {
753                                 if(mface->flag & ME_FACE_SEL) {
754                                         switch(nr) {
755                                         case 1:
756                                                 tface->mode |= TF_TEX; break;
757                                         case 2:
758                                                 tface->mode |= TF_SHAREDCOL; break;
759                                         case 3:
760                                                 tface->mode |= TF_LIGHT; break; 
761                                         case 4:
762                                                 tface->mode |= TF_INVISIBLE; break;
763                                         case 5:
764                                                 tface->mode |= TF_DYNAMIC; break;
765                                         case 6:
766                                                 tface->mode |= TF_TWOSIDE; break;
767                                         case 7:
768                                                 tface->mode &= ~TF_TEX;
769                                                 tface->tpage= NULL;
770                                                 break;
771                                         case 8:
772                                                 tface->mode &= ~TF_SHAREDCOL; break;
773                                         case 9:
774                                                 tface->mode &= ~TF_LIGHT; break;
775                                         case 10:
776                                                 tface->mode &= ~TF_INVISIBLE; break;
777                                         case 11:
778                                                 tface->mode &= ~TF_DYNAMIC; break;
779                                         case 12:
780                                                 tface->mode &= ~TF_TWOSIDE; break;
781                                         }
782                                 }
783                         }
784                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
785                 }
786                 else if(ob->mode & OB_MODE_VERTEX_PAINT) {
787                         Mesh *me= get_mesh(ob);
788                         
789                         if(me==NULL || (me->mcol==NULL && me->mtface==NULL) ) return;
790                         
791                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
792                         if(nr==1) {
793                                 
794 // XXX                          do_shared_vertexcol(me);
795                                 
796                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
797                         }
798                 }
799                 else if(ob->mode & OB_MODE_WEIGHT_PAINT) {
800                         Object *par= modifiers_isDeformedByArmature(ob);
801
802                         if(par && (par->mode & OB_MODE_POSE)) {
803                                 nr= pupmenu("Specials%t|Apply Bone Envelopes to Vertex Groups %x1|Apply Bone Heat Weights to Vertex Groups %x2");
804
805 // XXX                          if(nr==1 || nr==2)
806 // XXX                                  pose_adds_vgroups(ob, (nr == 2));
807                         }
808                 }
809                 else if(ob->mode & OB_MODE_PARTICLE_EDIT) {
810 #if 0
811                         // XXX
812                         ParticleSystem *psys = PE_get_current(ob);
813                         ParticleEditSettings *pset = PE_settings();
814
815                         if(!psys)
816                                 return;
817
818                         if(pset->selectmode & SCE_SELECT_POINT)
819                                 nr= pupmenu("Specials%t|Rekey%x1|Subdivide%x2|Select First%x3|Select Last%x4|Remove Doubles%x5");
820                         else
821                                 nr= pupmenu("Specials%t|Rekey%x1|Remove Doubles%x5");
822                         
823                         switch(nr) {
824                         case 1:
825 // XXX                          if(button(&pset->totrekey, 2, 100, "Number of Keys:")==0) return;
826                                 waitcursor(1);
827                                 PE_rekey();
828                                 break;
829                         case 2:
830                                 PE_subdivide();
831                                 break;
832                         case 3:
833                                 PE_select_root();
834                                 break;
835                         case 4:
836                                 PE_select_tip();
837                                 break;
838                         case 5:
839                                 PE_remove_doubles();
840                                 break;
841                         }
842                         
843                         DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
844                         
845                         if(nr>0) waitcursor(0);
846 #endif
847                 }
848                 else {
849                         Base *base, *base_select= NULL;
850                         
851                         /* Get the active object mesh. */
852                         Mesh *me= get_mesh(ob);
853
854                         /* Booleans, if the active object is a mesh... */
855                         if (me && ob->id.lib==NULL) {
856                                 
857                                 /* Bring up a little menu with the boolean operation choices on. */
858                                 nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6");
859
860                                 if (nr > 0) {
861                                         /* user has made a choice of a menu element.
862                                            All of the boolean functions require 2 mesh objects 
863                                            we search through the object list to find the other 
864                                            selected item and make sure it is distinct and a mesh. */
865
866                                         for(base= FIRSTBASE; base; base= base->next) {
867                                                 if(TESTBASELIB(v3d, base)) {
868                                                         if(base->object != ob) base_select= base;
869                                                 }
870                                         }
871
872                                         if (base_select) {
873                                                 if (get_mesh(base_select->object)) {
874                                                         if(nr <= 3){
875                                                                 waitcursor(1);
876 // XXX                                                          ret = NewBooleanMesh(BASACT,base_select,nr);
877                                                                 if (ret==0) {
878                                                                         error("An internal error occurred");
879                                                                 } else if(ret==-1) {
880                                                                         error("Selected meshes must have faces to perform boolean operations");
881                                                                 } else if (ret==-2) {
882                                                                         error("Both meshes must be a closed mesh");
883                                                                 }
884                                                                 waitcursor(0);
885                                                         } else {
886                                                                 BooleanModifierData *bmd = NULL;
887                                                                 bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean);
888                                                                 BLI_addtail(&ob->modifiers, bmd);
889                                                                 modifier_unique_name(&ob->modifiers, (ModifierData*)bmd);
890                                                                 bmd->object = base_select->object;
891                                                                 bmd->modifier.mode |= eModifierMode_Realtime;
892                                                                 switch(nr){
893                                                                         case 4: bmd->operation = eBooleanModifierOp_Intersect; break;
894                                                                         case 5: bmd->operation = eBooleanModifierOp_Union; break;
895                                                                         case 6: bmd->operation = eBooleanModifierOp_Difference; break;
896                                                                 }
897 // XXX                                                          do_common_editbuts(B_CHANGEDEP);
898                                                         }                                                               
899                                                 } else {
900                                                         error("Please select 2 meshes");
901                                                 }
902                                         } else {
903                                                 error("Please select 2 meshes");
904                                         }
905                                 }
906
907                         }
908                         else if (ob->type == OB_FONT) {
909                                 /* removed until this gets a decent implementation (ton) */
910 /*                              nr= pupmenu("Split %t|Characters%x1");
911                                 if (nr > 0) {
912                                         switch(nr) {
913                                                 case 1: split_font();
914                                         }
915                                 }
916 */
917                         }                       
918                 }
919         }
920         else if(obedit->type==OB_MESH) {
921         }
922         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
923         }
924         else if(obedit->type==OB_ARMATURE) {
925                 nr= pupmenu("Specials%t|Subdivide %x1|Subdivide Multi%x2|Switch Direction%x7|Flip Left-Right Names%x3|%l|AutoName Left-Right%x4|AutoName Front-Back%x5|AutoName Top-Bottom%x6");
926 //              if(nr==1)
927 // XXX                  subdivide_armature(1);
928                 if(nr==2) {
929 // XXX                  if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
930                         waitcursor(1);
931 // XXX                  subdivide_armature(numcuts);
932                 }
933 //              else if(nr==3)
934 // XXX                  armature_flip_names();
935                 else if(ELEM3(nr, 4, 5, 6)) {
936 // XXX                  armature_autoside_names(nr-4);
937                 }
938 //              else if(nr == 7)
939 // XXX                  switch_direction_armature();
940         }
941         else if(obedit->type==OB_LATTICE) {
942                 Lattice *lt= obedit->data;
943                 static float weight= 1.0f;
944                 { // XXX
945 // XXX          if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) {
946                         Lattice *editlt= lt->editlatt->latt;
947                         int a= editlt->pntsu*editlt->pntsv*editlt->pntsw;
948                         BPoint *bp= editlt->def;
949                         
950                         while(a--) {
951                                 if(bp->f1 & SELECT)
952                                         bp->weight= weight;
953                                 bp++;
954                         }
955                 }
956         }
957
958 }
959
960 static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob)
961 {       
962 //XXX no longer used - to be removed - replaced by game_properties_copy_exec
963         bProperty *prop;
964         Base *base;
965         int nr, tot=0;
966         char *str;
967         
968         prop= ob->prop.first;
969         while(prop) {
970                 tot++;
971                 prop= prop->next;
972         }
973         
974         str= MEM_callocN(50 + 33*tot, "copymenu prop");
975         
976         if (tot)
977                 strcpy(str, "Copy Property %t|Replace All|Merge All|%l");
978         else
979                 strcpy(str, "Copy Property %t|Clear All (no properties on active)");
980         
981         tot= 0; 
982         prop= ob->prop.first;
983         while(prop) {
984                 tot++;
985                 strcat(str, "|");
986                 strcat(str, prop->name);
987                 prop= prop->next;
988         }
989
990         nr= pupmenu(str);
991         
992         if ( nr==1 || nr==2 ) {
993                 for(base= FIRSTBASE; base; base= base->next) {
994                         if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
995                                 if (nr==1) { /* replace */
996                                         copy_properties( &base->object->prop, &ob->prop );
997                                 } else {
998                                         for(prop = ob->prop.first; prop; prop= prop->next ) {
999                                                 set_ob_property(base->object, prop);
1000                                         }
1001                                 }
1002                         }
1003                 }
1004         } else if(nr>0) {
1005                 prop = BLI_findlink(&ob->prop, nr-4); /* account for first 3 menu items & menu index starting at 1*/
1006                 
1007                 if(prop) {
1008                         for(base= FIRSTBASE; base; base= base->next) {
1009                                 if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
1010                                         set_ob_property(base->object, prop);
1011                                 }
1012                         }
1013                 }
1014         }
1015         MEM_freeN(str);
1016         
1017 }
1018
1019 static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob)
1020 {
1021 //XXX no longer used - to be removed - replaced by logicbricks_copy_exec
1022         Base *base;
1023         
1024         for(base= FIRSTBASE; base; base= base->next) {
1025                 if(base->object != ob) {
1026                         if(TESTBASELIB(v3d, base)) {
1027                                 
1028                                 /* first: free all logic */
1029                                 free_sensors(&base->object->sensors);                           
1030                                 unlink_controllers(&base->object->controllers);
1031                                 free_controllers(&base->object->controllers);
1032                                 unlink_actuators(&base->object->actuators);
1033                                 free_actuators(&base->object->actuators);
1034                                 
1035                                 /* now copy it, this also works without logicbricks! */
1036                                 clear_sca_new_poins_ob(ob);
1037                                 copy_sensors(&base->object->sensors, &ob->sensors);
1038                                 copy_controllers(&base->object->controllers, &ob->controllers);
1039                                 copy_actuators(&base->object->actuators, &ob->actuators);
1040                                 set_sca_new_poins_ob(base->object);
1041                                 
1042                                 /* some menu settings */
1043                                 base->object->scavisflag= ob->scavisflag;
1044                                 base->object->scaflag= ob->scaflag;
1045                                 
1046                                 /* set the initial state */
1047                                 base->object->state= ob->state;
1048                                 base->object->init_state= ob->init_state;
1049                         }
1050                 }
1051         }
1052 }
1053
1054 /* both pointers should exist */
1055 static void copy_texture_space(Object *to, Object *ob)
1056 {
1057         float *poin1= NULL, *poin2= NULL;
1058         short texflag= 0;
1059         
1060         if(ob->type==OB_MESH) {
1061                 texflag= ((Mesh *)ob->data)->texflag;
1062                 poin2= ((Mesh *)ob->data)->loc;
1063         }
1064         else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1065                 texflag= ((Curve *)ob->data)->texflag;
1066                 poin2= ((Curve *)ob->data)->loc;
1067         }
1068         else if(ob->type==OB_MBALL) {
1069                 texflag= ((MetaBall *)ob->data)->texflag;
1070                 poin2= ((MetaBall *)ob->data)->loc;
1071         }
1072         else
1073                 return;
1074                 
1075         if(to->type==OB_MESH) {
1076                 ((Mesh *)to->data)->texflag= texflag;
1077                 poin1= ((Mesh *)to->data)->loc;
1078         }
1079         else if (ELEM3(to->type, OB_CURVE, OB_SURF, OB_FONT)) {
1080                 ((Curve *)to->data)->texflag= texflag;
1081                 poin1= ((Curve *)to->data)->loc;
1082         }
1083         else if(to->type==OB_MBALL) {
1084                 ((MetaBall *)to->data)->texflag= texflag;
1085                 poin1= ((MetaBall *)to->data)->loc;
1086         }
1087         else
1088                 return;
1089         
1090         memcpy(poin1, poin2, 9*sizeof(float));  /* this was noted in DNA_mesh, curve, mball */
1091         
1092         if(to->type==OB_MESH) ;
1093         else if(to->type==OB_MBALL) tex_space_mball(to);
1094         else tex_space_curve(to->data);
1095         
1096 }
1097
1098 /* UNUSED, keep incase we want to copy functionality for use elsewhere */
1099 static void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event)
1100 {
1101         Object *ob;
1102         Base *base;
1103         Curve *cu, *cu1;
1104         Nurb *nu;
1105         int do_scene_sort= 0;
1106         
1107         if(scene->id.lib) return;
1108
1109         if(!(ob=OBACT)) return;
1110         
1111         if(scene->obedit) { // XXX get from context
1112                 /* obedit_copymenu(); */
1113                 return;
1114         }
1115         if(event==9) {
1116                 copymenu_properties(scene, v3d, ob);
1117                 return;
1118         }
1119         else if(event==10) {
1120                 copymenu_logicbricks(scene, v3d, ob);
1121                 return;
1122         }
1123         else if(event==24) {
1124                 /* moved to object_link_modifiers */
1125                 /* copymenu_modifiers(bmain, scene, v3d, ob); */
1126                 return;
1127         }
1128
1129         for(base= FIRSTBASE; base; base= base->next) {
1130                 if(base != BASACT) {
1131                         if(TESTBASELIB(v3d, base)) {
1132                                 base->object->recalc |= OB_RECALC_OB;
1133                                 
1134                                 if(event==1) {  /* loc */
1135                                         VECCOPY(base->object->loc, ob->loc);
1136                                         VECCOPY(base->object->dloc, ob->dloc);
1137                                 }
1138                                 else if(event==2) {  /* rot */
1139                                         VECCOPY(base->object->rot, ob->rot);
1140                                         VECCOPY(base->object->drot, ob->drot);
1141
1142                                         QUATCOPY(base->object->quat, ob->quat);
1143                                         QUATCOPY(base->object->dquat, ob->dquat);
1144                                 }
1145                                 else if(event==3) {  /* size */
1146                                         VECCOPY(base->object->size, ob->size);
1147                                         VECCOPY(base->object->dsize, ob->dsize);
1148                                 }
1149                                 else if(event==4) {  /* drawtype */
1150                                         base->object->dt= ob->dt;
1151                                         base->object->dtx= ob->dtx;
1152                                         base->object->empty_drawtype= ob->empty_drawtype;
1153                                         base->object->empty_drawsize= ob->empty_drawsize;
1154                                 }
1155                                 else if(event==5) {  /* time offs */
1156                                         base->object->sf= ob->sf;
1157                                 }
1158                                 else if(event==6) {  /* dupli */
1159                                         base->object->dupon= ob->dupon;
1160                                         base->object->dupoff= ob->dupoff;
1161                                         base->object->dupsta= ob->dupsta;
1162                                         base->object->dupend= ob->dupend;
1163                                         
1164                                         base->object->transflag &= ~OB_DUPLI;
1165                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
1166
1167                                         base->object->dup_group= ob->dup_group;
1168                                         if(ob->dup_group)
1169                                                 id_lib_extern(&ob->dup_group->id);
1170                                 }
1171                                 else if(event==7) {     /* mass */
1172                                         base->object->mass= ob->mass;
1173                                 }
1174                                 else if(event==8) {     /* damping */
1175                                         base->object->damping= ob->damping;
1176                                         base->object->rdamping= ob->rdamping;
1177                                 }
1178                                 else if(event==11) {    /* all physical attributes */
1179                                         base->object->gameflag = ob->gameflag;
1180                                         base->object->inertia = ob->inertia;
1181                                         base->object->formfactor = ob->formfactor;
1182                                         base->object->damping= ob->damping;
1183                                         base->object->rdamping= ob->rdamping;
1184                                         base->object->min_vel= ob->min_vel;
1185                                         base->object->max_vel= ob->max_vel;
1186                                         if (ob->gameflag & OB_BOUNDS) {
1187                                                 base->object->boundtype = ob->boundtype;
1188                                         }
1189                                         base->object->margin= ob->margin;
1190                                         base->object->bsoft= copy_bulletsoftbody(ob->bsoft);
1191
1192                                 }
1193                                 else if(event==17) {    /* tex space */
1194                                         copy_texture_space(base->object, ob);
1195                                 }
1196                                 else if(event==18) {    /* font settings */
1197                                         
1198                                         if(base->object->type==ob->type) {
1199                                                 cu= ob->data;
1200                                                 cu1= base->object->data;
1201                                                 
1202                                                 cu1->spacemode= cu->spacemode;
1203                                                 cu1->spacing= cu->spacing;
1204                                                 cu1->linedist= cu->linedist;
1205                                                 cu1->shear= cu->shear;
1206                                                 cu1->fsize= cu->fsize;
1207                                                 cu1->xof= cu->xof;
1208                                                 cu1->yof= cu->yof;
1209                                                 cu1->textoncurve= cu->textoncurve;
1210                                                 cu1->wordspace= cu->wordspace;
1211                                                 cu1->ulpos= cu->ulpos;
1212                                                 cu1->ulheight= cu->ulheight;
1213                                                 if(cu1->vfont) cu1->vfont->id.us--;
1214                                                 cu1->vfont= cu->vfont;
1215                                                 id_us_plus((ID *)cu1->vfont);
1216                                                 if(cu1->vfontb) cu1->vfontb->id.us--;
1217                                                 cu1->vfontb= cu->vfontb;
1218                                                 id_us_plus((ID *)cu1->vfontb);
1219                                                 if(cu1->vfonti) cu1->vfonti->id.us--;
1220                                                 cu1->vfonti= cu->vfonti;
1221                                                 id_us_plus((ID *)cu1->vfonti);
1222                                                 if(cu1->vfontbi) cu1->vfontbi->id.us--;
1223                                                 cu1->vfontbi= cu->vfontbi;
1224                                                 id_us_plus((ID *)cu1->vfontbi);                                         
1225
1226                                                 BKE_text_to_curve(scene, base->object, 0);              /* needed? */
1227
1228                                                 
1229                                                 BLI_strncpy(cu1->family, cu->family, sizeof(cu1->family));
1230                                                 
1231                                                 base->object->recalc |= OB_RECALC_DATA;
1232                                         }
1233                                 }
1234                                 else if(event==19) {    /* bevel settings */
1235                                         
1236                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1237                                                 cu= ob->data;
1238                                                 cu1= base->object->data;
1239                                                 
1240                                                 cu1->bevobj= cu->bevobj;
1241                                                 cu1->taperobj= cu->taperobj;
1242                                                 cu1->width= cu->width;
1243                                                 cu1->bevresol= cu->bevresol;
1244                                                 cu1->ext1= cu->ext1;
1245                                                 cu1->ext2= cu->ext2;
1246                                                 
1247                                                 base->object->recalc |= OB_RECALC_DATA;
1248                                         }
1249                                 }
1250                                 else if(event==25) {    /* curve resolution */
1251
1252                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1253                                                 cu= ob->data;
1254                                                 cu1= base->object->data;
1255                                                 
1256                                                 cu1->resolu= cu->resolu;
1257                                                 cu1->resolu_ren= cu->resolu_ren;
1258                                                 
1259                                                 nu= cu1->nurb.first;
1260                                                 
1261                                                 while(nu) {
1262                                                         nu->resolu= cu1->resolu;
1263                                                         nu= nu->next;
1264                                                 }
1265                                                 
1266                                                 base->object->recalc |= OB_RECALC_DATA;
1267                                         }
1268                                 }
1269                                 else if(event==21){
1270                                         if (base->object->type==OB_MESH) {
1271                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
1272
1273                                                 if (md) {
1274                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
1275
1276                                                         if (!tmd) {
1277                                                                 tmd = modifier_new(eModifierType_Subsurf);
1278                                                                 BLI_addtail(&base->object->modifiers, tmd);
1279                                                         }
1280
1281                                                         modifier_copyData(md, tmd);
1282                                                         base->object->recalc |= OB_RECALC_DATA;
1283                                                 }
1284                                         }
1285                                 }
1286                                 else if(event==22) {
1287                                         /* Copy the constraint channels over */
1288                                         copy_constraints(&base->object->constraints, &ob->constraints, TRUE);
1289                                         
1290                                         do_scene_sort= 1;
1291                                 }
1292                                 else if(event==23) {
1293                                         base->object->softflag= ob->softflag;
1294                                         if(base->object->soft) sbFree(base->object->soft);
1295                                         
1296                                         base->object->soft= copy_softbody(ob->soft);
1297
1298                                         if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
1299                                                 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
1300                                         }
1301                                 }
1302                                 else if(event==26) {
1303 #if 0 // XXX old animation system
1304                                         copy_nlastrips(&base->object->nlastrips, &ob->nlastrips);
1305 #endif // XXX old animation system
1306                                 }
1307                                 else if(event==27) {    /* autosmooth */
1308                                         if (base->object->type==OB_MESH) {
1309                                                 Mesh *me= ob->data;
1310                                                 Mesh *cme= base->object->data;
1311                                                 cme->smoothresh= me->smoothresh;
1312                                                 if(me->flag & ME_AUTOSMOOTH)
1313                                                         cme->flag |= ME_AUTOSMOOTH;
1314                                                 else
1315                                                         cme->flag &= ~ME_AUTOSMOOTH;
1316                                         }
1317                                 }
1318                                 else if(event==28) { /* UV orco */
1319                                         if(ELEM(base->object->type, OB_CURVE, OB_SURF)) {
1320                                                 cu= ob->data;
1321                                                 cu1= base->object->data;
1322                                                 
1323                                                 if(cu->flag & CU_UV_ORCO)
1324                                                         cu1->flag |= CU_UV_ORCO;
1325                                                 else
1326                                                         cu1->flag &= ~CU_UV_ORCO;
1327                                         }               
1328                                 }
1329                                 else if(event==29) { /* protected bits */
1330                                         base->object->protectflag= ob->protectflag;
1331                                 }
1332                                 else if(event==30) { /* index object */
1333                                         base->object->index= ob->index;
1334                                 }
1335                                 else if(event==31) { /* object color */
1336                                         QUATCOPY(base->object->col, ob->col);
1337                                 }
1338                         }
1339                 }
1340         }
1341         
1342         if(do_scene_sort)
1343                 DAG_scene_sort(bmain, scene);
1344
1345         DAG_ids_flush_update(bmain, 0);
1346 }
1347
1348 static void UNUSED_FUNCTION(copy_attr_menu)(Main *bmain, Scene *scene, View3D *v3d)
1349 {
1350         Object *ob;
1351         short event;
1352         char str[512];
1353         
1354         if(!(ob=OBACT)) return;
1355         
1356         if (scene->obedit) { // XXX get from context
1357 //              if (ob->type == OB_MESH)
1358 // XXX                  mesh_copy_menu();
1359                 return;
1360         }
1361         
1362         /* Object Mode */
1363         
1364         /* If you change this menu, don't forget to update the menu in header_view3d.c
1365          * view3d_edit_object_copyattrmenu() and in toolbox.c
1366          */
1367         
1368         strcpy(str, "Copy Attributes %t|Location%x1|Rotation%x2|Size%x3|Draw Options%x4|Time Offset%x5|Dupli%x6|Object Color%x31|%l|Mass%x7|Damping%x8|All Physical Attributes%x11|Properties%x9|Logic Bricks%x10|Protected Transform%x29|%l");
1369         
1370         strcat (str, "|Object Constraints%x22");
1371         strcat (str, "|NLA Strips%x26");
1372         
1373 // XXX  if (OB_SUPPORT_MATERIAL(ob)) {
1374 //              strcat(str, "|Texture Space%x17");
1375 //      }       
1376         
1377         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
1378         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19|UV Orco%x28");
1379         
1380         if((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
1381                         strcat(str, "|Curve Resolution%x25");
1382         }
1383
1384         if(ob->type==OB_MESH){
1385                 strcat(str, "|Subsurf Settings%x21|AutoSmooth%x27");
1386         }
1387
1388         if(ob->soft) strcat(str, "|Soft Body Settings%x23");
1389         
1390         strcat(str, "|Pass Index%x30");
1391         
1392         if(ob->type==OB_MESH || ob->type==OB_CURVE || ob->type==OB_LATTICE || ob->type==OB_SURF){
1393                 strcat(str, "|Modifiers ...%x24");
1394         }
1395
1396         event= pupmenu(str);
1397         if(event<= 0) return;
1398         
1399         copy_attr(bmain, scene, v3d, event);
1400 }
1401
1402 /* ******************* force field toggle operator ***************** */
1403
1404 static int forcefield_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1405 {
1406         Object *ob = CTX_data_active_object(C);
1407
1408         if(ob->pd == NULL)
1409                 ob->pd = object_add_collision_fields(PFIELD_FORCE);
1410
1411         if(ob->pd->forcefield == 0)
1412                 ob->pd->forcefield = PFIELD_FORCE;
1413         else
1414                 ob->pd->forcefield = 0;
1415         
1416         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, NULL);
1417
1418         return OPERATOR_FINISHED;
1419 }
1420
1421 void OBJECT_OT_forcefield_toggle(wmOperatorType *ot)
1422 {
1423         
1424         /* identifiers */
1425         ot->name= "Toggle Force Field";
1426         ot->description = "Toggle object's force field";
1427         ot->idname= "OBJECT_OT_forcefield_toggle";
1428         
1429         /* api callbacks */
1430         ot->exec= forcefield_toggle_exec;
1431         ot->poll= ED_operator_object_active_editable;
1432         
1433         /* flags */
1434         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1435 }
1436
1437 /* ********************************************** */
1438 /* Motion Paths */
1439
1440 /* For the object with pose/action: update paths for those that have got them
1441  * This should selectively update paths that exist...
1442  *
1443  * To be called from various tools that do incremental updates 
1444  */
1445 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
1446 {
1447         ListBase targets = {NULL, NULL};
1448         
1449         /* loop over objects in scene */
1450         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1451         {
1452                 /* set flag to force recalc, then grab the relevant bones to target */
1453                 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
1454                 animviz_get_object_motionpaths(ob, &targets);
1455         }
1456         CTX_DATA_END;
1457         
1458         /* recalculate paths, then free */
1459         animviz_calc_motionpaths(scene, &targets);
1460         BLI_freelistN(&targets);
1461 }
1462
1463 /* For the object with pose/action: create path curves for selected bones 
1464  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
1465  */
1466 static int object_calculate_paths_exec (bContext *C, wmOperator *UNUSED(op))
1467 {
1468         Scene *scene= CTX_data_scene(C);
1469         
1470         /* set up path data for bones being calculated */
1471         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects)  
1472         {
1473                 /* verify makes sure that the selected bone has a bone with the appropriate settings */
1474                 animviz_verify_motionpaths(scene, ob, NULL);
1475         }
1476         CTX_DATA_END;
1477         
1478         /* calculate the bones that now have motionpaths... */
1479         // TODO: only make for the selected bones?
1480         ED_objects_recalculate_paths(C, scene);
1481         
1482         /* notifiers for updates */
1483         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1484         
1485         return OPERATOR_FINISHED; 
1486 }
1487
1488 void OBJECT_OT_paths_calculate (wmOperatorType *ot)
1489 {
1490         /* identifiers */
1491         ot->name= "Calculate Object Paths";
1492         ot->idname= "OBJECT_OT_paths_calculate";
1493         ot->description= "Calculate paths for the selected bones";
1494         
1495         /* api callbacks */
1496         ot->exec= object_calculate_paths_exec;
1497         ot->poll= ED_operator_object_active_editable;
1498         
1499         /* flags */
1500         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1501 }
1502
1503 /* --------- */
1504
1505 /* for the object with pose/action: clear path curves for selected bones only */
1506 void ED_objects_clear_paths(bContext *C)
1507 {
1508         /* loop over objects in scene */
1509         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1510         {
1511                 if (ob->mpath) {
1512                         animviz_free_motionpath(ob->mpath);
1513                         ob->mpath= NULL;
1514                         ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
1515                 }
1516         }
1517         CTX_DATA_END;
1518 }
1519
1520 /* operator callback for this */
1521 static int object_clear_paths_exec (bContext *C, wmOperator *UNUSED(op))
1522 {       
1523         /* use the backend function for this */
1524         ED_objects_clear_paths(C);
1525         
1526         /* notifiers for updates */
1527         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1528         
1529         return OPERATOR_FINISHED; 
1530 }
1531
1532 void OBJECT_OT_paths_clear (wmOperatorType *ot)
1533 {
1534         /* identifiers */
1535         ot->name= "Clear Object Paths";
1536         ot->idname= "OBJECT_OT_paths_clear";
1537         ot->description= "Clear path caches for selected bones";
1538         
1539         /* api callbacks */
1540         ot->exec= object_clear_paths_exec;
1541         ot->poll= ED_operator_object_active_editable;
1542         
1543         /* flags */
1544         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1545 }
1546
1547
1548 /********************** Smooth/Flat *********************/
1549
1550 static int shade_smooth_exec(bContext *C, wmOperator *op)
1551 {
1552         Curve *cu;
1553         Nurb *nu;
1554         int clear= (strcmp(op->idname, "OBJECT_OT_shade_flat") == 0);
1555         int done= 0;
1556
1557         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1558
1559                 if(ob->type==OB_MESH) {
1560                         mesh_set_smooth_flag(ob, !clear);
1561
1562                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1563                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1564
1565                         done= 1;
1566                 }
1567                 else if ELEM(ob->type, OB_SURF, OB_CURVE) {
1568                         cu= ob->data;
1569
1570                         for(nu=cu->nurb.first; nu; nu=nu->next) {
1571                                 if(!clear) nu->flag |= ME_SMOOTH;
1572                                 else nu->flag &= ~ME_SMOOTH;
1573                         }
1574
1575                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1576                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1577
1578                         done= 1;
1579                 }
1580         }
1581         CTX_DATA_END;
1582
1583         return (done)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
1584 }
1585
1586 static int shade_poll(bContext *C)
1587 {
1588         return (ED_operator_object_active_editable(C) && !ED_operator_editmesh(C));
1589 }
1590
1591 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1592 {
1593         /* identifiers */
1594         ot->name= "Shade Flat";
1595         ot->description= "Display faces 'flat'";
1596         ot->idname= "OBJECT_OT_shade_flat";
1597         
1598         /* api callbacks */
1599         ot->poll= shade_poll;
1600         ot->exec= shade_smooth_exec;
1601
1602         /* flags */
1603         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1604 }
1605
1606 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1607 {
1608         /* identifiers */
1609         ot->name= "Shade Smooth";
1610         ot->description= "Display faces 'smooth' (using vertex normals)";
1611         ot->idname= "OBJECT_OT_shade_smooth";
1612         
1613         /* api callbacks */
1614         ot->poll= shade_poll;
1615         ot->exec= shade_smooth_exec;
1616         
1617         /* flags */
1618         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1619 }
1620
1621 /* ********************** */
1622
1623 static void UNUSED_FUNCTION(image_aspect)(Scene *scene, View3D *v3d)
1624 {
1625         /* all selected objects with an image map: scale in image aspect */
1626         Base *base;
1627         Object *ob;
1628         Material *ma;
1629         Tex *tex;
1630         float x, y, space;
1631         int a, b, done;
1632         
1633         if(scene->obedit) return; // XXX get from context
1634         if(scene->id.lib) return;
1635         
1636         for(base= FIRSTBASE; base; base= base->next) {
1637                 if(TESTBASELIB(v3d, base)) {
1638                         ob= base->object;
1639                         done= 0;
1640                         
1641                         for(a=1; a<=ob->totcol; a++) {
1642                                 ma= give_current_material(ob, a);
1643                                 if(ma) {
1644                                         for(b=0; b<MAX_MTEX; b++) {
1645                                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
1646                                                         tex= ma->mtex[b]->tex;
1647                                                         if(tex->type==TEX_IMAGE && tex->ima) {
1648                                                                 ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, NULL);
1649                                                                 
1650                                                                 /* texturespace */
1651                                                                 space= 1.0;
1652                                                                 if(ob->type==OB_MESH) {
1653                                                                         float size[3];
1654                                                                         mesh_get_texspace(ob->data, NULL, NULL, size);
1655                                                                         space= size[0]/size[1];
1656                                                                 }
1657                                                                 else if(ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1658                                                                         Curve *cu= ob->data;
1659                                                                         space= cu->size[0]/cu->size[1];
1660                                                                 }
1661                                                         
1662                                                                 x= ibuf->x/space;
1663                                                                 y= ibuf->y;
1664                                                                 
1665                                                                 if(x>y) ob->size[0]= ob->size[1]*x/y;
1666                                                                 else ob->size[1]= ob->size[0]*y/x;
1667                                                                 
1668                                                                 done= 1;
1669                                                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);                                                               
1670                                                         }
1671                                                 }
1672                                                 if(done) break;
1673                                         }
1674                                 }
1675                                 if(done) break;
1676                         }
1677                 }
1678         }
1679         
1680 }
1681
1682 static int vergbaseco(const void *a1, const void *a2)
1683 {
1684         Base **x1, **x2;
1685         
1686         x1= (Base **) a1;
1687         x2= (Base **) a2;
1688         
1689         if( (*x1)->sy > (*x2)->sy ) return 1;
1690         else if( (*x1)->sy < (*x2)->sy) return -1;
1691         else if( (*x1)->sx > (*x2)->sx ) return 1;
1692         else if( (*x1)->sx < (*x2)->sx ) return -1;
1693
1694         return 0;
1695 }
1696
1697
1698 static void UNUSED_FUNCTION(auto_timeoffs)(Scene *scene, View3D *v3d)
1699 {
1700         Base *base, **basesort, **bs;
1701         float start, delta;
1702         int tot=0, a;
1703         short offset=25;
1704
1705         if(BASACT==NULL || v3d==NULL) return;
1706 // XXX  if(button(&offset, 0, 1000,"Total time")==0) return;
1707
1708         /* make array of all bases, xco yco (screen) */
1709         for(base= FIRSTBASE; base; base= base->next) {
1710                 if(TESTBASELIB(v3d, base)) {
1711                         tot++;
1712                 }
1713         }
1714
1715         delta= (float)offset/(float)tot;
1716         start= OBACT->sf;
1717
1718         bs= basesort= MEM_mallocN(sizeof(void *)*tot,"autotimeoffs");
1719         for(base= FIRSTBASE; base; base= base->next) {
1720                 if(TESTBASELIB(v3d, base)) {
1721                         *bs= base;
1722                         bs++;
1723                 }
1724         }
1725         qsort(basesort, tot, sizeof(void *), vergbaseco);
1726
1727         bs= basesort;
1728         for(a=0; a<tot; a++) {
1729                 
1730                 (*bs)->object->sf= start;
1731                 start+= delta;
1732
1733                 bs++;
1734         }
1735         MEM_freeN(basesort);
1736
1737 }
1738
1739 static void UNUSED_FUNCTION(ofs_timeoffs)(Scene *scene, View3D *v3d)
1740 {
1741         float offset=0.0f;
1742
1743         if(BASACT==NULL || v3d==NULL) return;
1744         
1745 // XXX  if(fbutton(&offset, -10000.0f, 10000.0f, 10, 10, "Offset")==0) return;
1746
1747         /* make array of all bases, xco yco (screen) */
1748         CTX_DATA_BEGIN(evil_C, Object*, ob, selected_editable_objects) {
1749                 ob->sf += offset;
1750                 if (ob->sf < -MAXFRAMEF)                ob->sf = -MAXFRAMEF;
1751                 else if (ob->sf > MAXFRAMEF)    ob->sf = MAXFRAMEF;
1752         }
1753         CTX_DATA_END;
1754
1755 }
1756
1757
1758 static void UNUSED_FUNCTION(rand_timeoffs)(Scene *scene, View3D *v3d)
1759 {
1760         Base *base;
1761         float rand_ofs=0.0f;
1762
1763         if(BASACT==NULL || v3d==NULL) return;
1764         
1765 // XXX  if(fbutton(&rand_ofs, 0.0f, 10000.0f, 10, 10, "Randomize")==0) return;
1766         
1767         rand_ofs *= 2;
1768         
1769         for(base= FIRSTBASE; base; base= base->next) {
1770                 if(TESTBASELIB(v3d, base)) {
1771                         base->object->sf += ((float)BLI_drand()-0.5f) * rand_ofs;
1772                         if (base->object->sf < -MAXFRAMEF)              base->object->sf = -MAXFRAMEF;
1773                         else if (base->object->sf > MAXFRAMEF)  base->object->sf = MAXFRAMEF;
1774                 }
1775         }
1776
1777 }
1778
1779 static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
1780 {       
1781         EnumPropertyItem *input = object_mode_items;
1782         EnumPropertyItem *item= NULL;
1783         Object *ob;
1784         int totitem= 0;
1785         
1786         if(!C) /* needed for docs */
1787                 return object_mode_items;
1788
1789         ob = CTX_data_active_object(C);
1790         while(ob && input->identifier) {
1791                 if((input->value == OB_MODE_EDIT && ((ob->type == OB_MESH) || (ob->type == OB_ARMATURE) ||
1792                                                         (ob->type == OB_CURVE) || (ob->type == OB_SURF) ||
1793                                                          (ob->type == OB_FONT) || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) ||
1794                    (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1795                    (input->value == OB_MODE_PARTICLE_EDIT && ob->particlesystem.first) ||
1796                    ((input->value == OB_MODE_SCULPT || input->value == OB_MODE_VERTEX_PAINT ||
1797                          input->value == OB_MODE_WEIGHT_PAINT || input->value == OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1798                    (input->value == OB_MODE_OBJECT))
1799                         RNA_enum_item_add(&item, &totitem, input);
1800                 ++input;
1801         }
1802
1803         RNA_enum_item_end(&item, &totitem);
1804
1805         *free= 1;
1806
1807         return item;
1808 }
1809
1810 static const char *object_mode_op_string(int mode)
1811 {
1812         if(mode & OB_MODE_EDIT)
1813                 return "OBJECT_OT_editmode_toggle";
1814         if(mode == OB_MODE_SCULPT)
1815                 return "SCULPT_OT_sculptmode_toggle";
1816         if(mode == OB_MODE_VERTEX_PAINT)
1817                 return "PAINT_OT_vertex_paint_toggle";
1818         if(mode == OB_MODE_WEIGHT_PAINT)
1819                 return "PAINT_OT_weight_paint_toggle";
1820         if(mode == OB_MODE_TEXTURE_PAINT)
1821                 return "PAINT_OT_texture_paint_toggle";
1822         if(mode == OB_MODE_PARTICLE_EDIT)
1823                 return "PARTICLE_OT_particle_edit_toggle";
1824         if(mode == OB_MODE_POSE)
1825                 return "OBJECT_OT_posemode_toggle";
1826         return NULL;
1827 }
1828
1829 /* checks the mode to be set is compatible with the object
1830  * should be made into a generic function */
1831 static int object_mode_set_compat(bContext *UNUSED(C), wmOperator *op, Object *ob)
1832 {
1833         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1834
1835         if(ob) {
1836                 if(mode == OB_MODE_OBJECT)
1837                         return 1;
1838
1839                 switch(ob->type) {
1840                 case OB_MESH:
1841                         if(mode & (OB_MODE_EDIT|OB_MODE_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT|OB_MODE_PARTICLE_EDIT))
1842                                 return 1;
1843                         return 0;
1844                 case OB_CURVE:
1845                 case OB_SURF:
1846                 case OB_FONT:
1847                 case OB_MBALL:
1848                         if(mode & (OB_MODE_EDIT))
1849                                 return 1;
1850                         return 0;
1851                 case OB_LATTICE:
1852                         if(mode & (OB_MODE_EDIT|OB_MODE_WEIGHT_PAINT))
1853                                 return 1;
1854                 case OB_ARMATURE:
1855                         if(mode & (OB_MODE_EDIT|OB_MODE_POSE))
1856                                 return 1;
1857                 }
1858         }
1859
1860         return 0;
1861 }
1862
1863 static int object_mode_set_exec(bContext *C, wmOperator *op)
1864 {
1865         Object *ob= CTX_data_active_object(C);
1866         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1867         ObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
1868         int toggle = RNA_boolean_get(op->ptr, "toggle");
1869
1870         if(!ob || !object_mode_set_compat(C, op, ob))
1871                 return OPERATOR_PASS_THROUGH;
1872
1873         /* Exit current mode if it's not the mode we're setting */
1874         if(ob->mode != OB_MODE_OBJECT && ob->mode != mode)
1875                 WM_operator_name_call(C, object_mode_op_string(ob->mode), WM_OP_EXEC_REGION_WIN, NULL);
1876
1877         if(mode != OB_MODE_OBJECT) {
1878                 /* Enter new mode */
1879                 if(ob->mode != mode || toggle)
1880                         WM_operator_name_call(C, object_mode_op_string(mode), WM_OP_EXEC_REGION_WIN, NULL);
1881
1882                 if(toggle) {
1883                         if(ob->mode == mode)
1884                                 /* For toggling, store old mode so we know what to go back to */
1885                                 ob->restore_mode = restore_mode;
1886                         else if(ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
1887                                 WM_operator_name_call(C, object_mode_op_string(ob->restore_mode), WM_OP_EXEC_REGION_WIN, NULL);
1888                         }
1889                 }
1890         }
1891
1892         return OPERATOR_FINISHED;
1893 }
1894
1895 void OBJECT_OT_mode_set(wmOperatorType *ot)
1896 {
1897         PropertyRNA *prop;
1898
1899         /* identifiers */
1900         ot->name= "Set Object Mode";
1901         ot->description = "Sets the object interaction mode";
1902         ot->idname= "OBJECT_OT_mode_set";
1903         
1904         /* api callbacks */
1905         ot->exec= object_mode_set_exec;
1906         
1907         ot->poll= ED_operator_object_active_editable;
1908         
1909         /* flags */
1910         ot->flag= 0; /* no register/undo here, leave it to operators being called */
1911         
1912         prop= RNA_def_enum(ot->srna, "mode", object_mode_items, OB_MODE_OBJECT, "Mode", "");
1913         RNA_def_enum_funcs(prop, object_mode_set_itemsf);
1914
1915         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
1916 }
1917
1918
1919
1920 void ED_object_toggle_modes(bContext *C, int mode)
1921 {
1922         if(mode & OB_MODE_SCULPT)
1923                 WM_operator_name_call(C, "SCULPT_OT_sculptmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1924         if(mode & OB_MODE_VERTEX_PAINT)
1925                 WM_operator_name_call(C, "PAINT_OT_vertex_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1926         if(mode & OB_MODE_WEIGHT_PAINT)
1927                 WM_operator_name_call(C, "PAINT_OT_weight_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1928         if(mode & OB_MODE_TEXTURE_PAINT)
1929                 WM_operator_name_call(C, "PAINT_OT_texture_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1930         if(mode & OB_MODE_PARTICLE_EDIT)
1931                 WM_operator_name_call(C, "PARTICLE_OT_particle_edit_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1932         if(mode & OB_MODE_POSE)
1933                 WM_operator_name_call(C, "OBJECT_OT_posemode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1934         if(mode & OB_MODE_EDIT)
1935                 WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1936 }
1937
1938 /************************ Game Properties ***********************/
1939
1940 static int game_property_new(bContext *C, wmOperator *UNUSED(op))
1941 {
1942         Object *ob= CTX_data_active_object(C);
1943         bProperty *prop;
1944
1945         if(!ob)
1946                 return OPERATOR_CANCELLED;
1947
1948         prop= new_property(PROP_FLOAT);
1949         BLI_addtail(&ob->prop, prop);
1950         unique_property(NULL, prop, 0); // make_unique_prop_names(prop->name);
1951
1952         WM_event_add_notifier(C, NC_LOGIC, NULL);
1953         return OPERATOR_FINISHED;
1954 }
1955
1956
1957 void OBJECT_OT_game_property_new(wmOperatorType *ot)
1958 {
1959         /* identifiers */
1960         ot->name= "New Game Property";
1961         ot->description= "Create a new property available to the game engine";
1962         ot->idname= "OBJECT_OT_game_property_new";
1963
1964         /* api callbacks */
1965         ot->exec= game_property_new;
1966         ot->poll= ED_operator_object_active_editable;
1967
1968         /* flags */
1969         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1970 }
1971
1972 static int game_property_remove(bContext *C, wmOperator *op)
1973 {
1974         Object *ob= CTX_data_active_object(C);
1975         bProperty *prop;
1976         int index= RNA_int_get(op->ptr, "index");
1977
1978         if(!ob)
1979                 return OPERATOR_CANCELLED;
1980
1981         prop= BLI_findlink(&ob->prop, index);
1982
1983         if(prop) {
1984                 BLI_remlink(&ob->prop, prop);
1985                 free_property(prop);
1986
1987                 WM_event_add_notifier(C, NC_LOGIC, NULL);
1988                 return OPERATOR_FINISHED;
1989         }
1990         else {
1991                 return OPERATOR_CANCELLED;
1992         }
1993 }
1994
1995 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
1996 {
1997         /* identifiers */
1998         ot->name= "Remove Game Property";
1999         ot->description= "Remove game property";
2000         ot->idname= "OBJECT_OT_game_property_remove";
2001
2002         /* api callbacks */
2003         ot->exec= game_property_remove;
2004         ot->poll= ED_operator_object_active_editable;
2005
2006         /* flags */
2007         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2008
2009         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
2010 }
2011
2012 #define COPY_PROPERTIES_REPLACE 1
2013 #define COPY_PROPERTIES_MERGE   2
2014 #define COPY_PROPERTIES_COPY    3
2015
2016 static EnumPropertyItem game_properties_copy_operations[] ={
2017         {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
2018         {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
2019         {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
2020         {0, NULL, 0, NULL, NULL}};
2021
2022 static EnumPropertyItem gameprops_items[]= {
2023         {0, NULL, 0, NULL, NULL}};
2024
2025 static EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
2026 {       
2027         Object *ob= ED_object_active_context(C);
2028         EnumPropertyItem tmp = {0, "", 0, "", ""};
2029         EnumPropertyItem *item= NULL;
2030         bProperty *prop;
2031         int a, totitem= 0;
2032         
2033         if(!ob)
2034                 return gameprops_items;
2035
2036         for(a=1, prop= ob->prop.first; prop; prop=prop->next, a++) {
2037                 tmp.value= a;
2038                 tmp.identifier= prop->name;
2039                 tmp.name= prop->name;
2040                 RNA_enum_item_add(&item, &totitem, &tmp);
2041         }
2042
2043         RNA_enum_item_end(&item, &totitem);
2044         *free= 1;
2045
2046         return item;
2047 }
2048
2049 static int game_property_copy_exec(bContext *C, wmOperator *op)
2050 {
2051         Object *ob=ED_object_active_context(C);
2052         bProperty *prop;
2053         int type = RNA_enum_get(op->ptr, "operation");
2054         int propid= RNA_enum_get(op->ptr, "property");
2055
2056         if(propid > 0) { /* copy */
2057                 prop = BLI_findlink(&ob->prop, propid-1);
2058                 
2059                 if(prop) {
2060                         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2061                                 if (ob != ob_iter) {
2062                                         if (ob->data != ob_iter->data)
2063                                                 set_ob_property(ob_iter, prop);
2064                                 }
2065                         } CTX_DATA_END;
2066                 }
2067         }
2068
2069         else {
2070                 CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2071                         if (ob != ob_iter) {
2072                                 if (ob->data != ob_iter->data){
2073                                         if (type == COPY_PROPERTIES_REPLACE)
2074                                                 copy_properties( &ob_iter->prop, &ob->prop );
2075
2076                                         /* merge - the default when calling with no argument */
2077                                         else {
2078                                                 for(prop = ob->prop.first; prop; prop= prop->next ) {
2079                                                         set_ob_property(ob_iter, prop);
2080                                                 }
2081                                         }
2082                                 }
2083                         }
2084                 }
2085                 CTX_DATA_END;
2086         }
2087
2088         return OPERATOR_FINISHED;
2089 }
2090
2091 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
2092 {
2093         PropertyRNA *prop;
2094         /* identifiers */
2095         ot->name= "Copy Game Property";
2096         ot->idname= "OBJECT_OT_game_property_copy";
2097
2098         /* api callbacks */
2099         ot->exec= game_property_copy_exec;
2100         ot->poll= ED_operator_object_active_editable;
2101
2102         /* flags */
2103         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2104
2105         RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
2106         prop=RNA_def_enum(ot->srna, "property", gameprops_items, 0, "Property", "Properties to copy");
2107         RNA_def_enum_funcs(prop, gameprops_itemf);
2108         ot->prop=prop;
2109 }
2110
2111 static int game_property_clear_exec(bContext *C, wmOperator *UNUSED(op))
2112 {
2113         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2114                 free_properties(&ob_iter->prop);
2115         }
2116         CTX_DATA_END;
2117
2118         WM_event_add_notifier(C, NC_LOGIC, NULL);
2119         return OPERATOR_FINISHED;
2120 }
2121 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
2122 {
2123         /* identifiers */
2124         ot->name= "Clear Game Property";
2125         ot->idname= "OBJECT_OT_game_property_clear";
2126
2127         /* api callbacks */
2128         ot->exec= game_property_clear_exec;
2129         ot->poll= ED_operator_object_active_editable;
2130
2131         /* flags */
2132         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2133 }
2134
2135 /************************ Copy Logic Bricks ***********************/
2136
2137 static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op))
2138 {
2139         Object *ob=ED_object_active_context(C);
2140
2141         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2142                 if(ob != ob_iter) {
2143                         /* first: free all logic */
2144                         free_sensors(&ob_iter->sensors);                                
2145                         unlink_controllers(&ob_iter->controllers);
2146                         free_controllers(&ob_iter->controllers);
2147                         unlink_actuators(&ob_iter->actuators);
2148                         free_actuators(&ob_iter->actuators);
2149                 
2150                         /* now copy it, this also works without logicbricks! */
2151                         clear_sca_new_poins_ob(ob);
2152                         copy_sensors(&ob_iter->sensors, &ob->sensors);
2153                         copy_controllers(&ob_iter->controllers, &ob->controllers);
2154                         copy_actuators(&ob_iter->actuators, &ob->actuators);
2155                         set_sca_new_poins_ob(ob_iter);
2156                 
2157                         /* some menu settings */
2158                         ob_iter->scavisflag= ob->scavisflag;
2159                         ob_iter->scaflag= ob->scaflag;
2160                 
2161                         /* set the initial state */
2162                         ob_iter->state= ob->state;
2163                         ob_iter->init_state= ob->init_state;
2164
2165                         if(ob_iter->totcol==ob->totcol) {
2166                                 ob_iter->actcol= ob->actcol;
2167                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
2168                         }
2169                 }
2170         }
2171         CTX_DATA_END;
2172
2173         WM_event_add_notifier(C, NC_LOGIC, NULL);
2174
2175         return OPERATOR_FINISHED;
2176 }
2177
2178 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
2179 {
2180         /* identifiers */
2181         ot->name= "Copy Logic Bricks to Selected";
2182         ot->description = "Copy logic bricks to other selected objects";
2183         ot->idname= "OBJECT_OT_logic_bricks_copy";
2184
2185         /* api callbacks */
2186         ot->exec= logicbricks_copy_exec;
2187         ot->poll= ED_operator_object_active_editable;
2188
2189         /* flags */
2190         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2191 }