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