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