Fix different declaration and implementation of function names
[blender.git] / source / blender / editors / render / render_shading.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  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  *
17  * The Original Code is Copyright (C) 2009 Blender Foundation.
18  * All rights reserved.
19  *
20  * Contributor(s): Blender Foundation
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/render/render_shading.c
26  *  \ingroup edrend
27  */
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_curve_types.h"
35 #include "DNA_lamp_types.h"
36 #include "DNA_material_types.h"
37 #include "DNA_node_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_particle_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_space_types.h"
42 #include "DNA_world_types.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_editVert.h"
47 #include "BLI_utildefines.h"
48
49 #include "BKE_animsys.h"
50 #include "BKE_context.h"
51 #include "BKE_curve.h"
52 #include "BKE_depsgraph.h"
53 #include "BKE_font.h"
54 #include "BKE_global.h"
55 #include "BKE_icons.h"
56 #include "BKE_image.h"
57 #include "BKE_library.h"
58 #include "BKE_main.h"
59 #include "BKE_material.h"
60 #include "BKE_node.h"
61 #include "BKE_report.h"
62 #include "BKE_scene.h"
63 #include "BKE_texture.h"
64 #include "BKE_world.h"
65 #include "BKE_tessmesh.h"
66
67 #include "IMB_imbuf.h"
68 #include "IMB_imbuf_types.h"
69
70 #include "GPU_material.h"
71
72 #include "RNA_access.h"
73
74 #include "WM_api.h"
75 #include "WM_types.h"
76
77 #include "ED_curve.h"
78 #include "ED_mesh.h"
79 #include "ED_node.h"
80 #include "ED_render.h"
81 #include "ED_screen.h"
82
83 #include "RNA_define.h"
84
85 #include "UI_interface.h"
86
87 #include "RE_pipeline.h"
88
89 #include "render_intern.h"      // own include
90
91 /********************** material slot operators *********************/
92
93 static int material_slot_add_exec(bContext *C, wmOperator *UNUSED(op))
94 {
95         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
96
97         if(!ob)
98                 return OPERATOR_CANCELLED;
99
100         object_add_material_slot(ob);
101         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
102         WM_event_add_notifier(C, NC_OBJECT|ND_OB_SHADING, ob);
103         
104         return OPERATOR_FINISHED;
105 }
106
107 void OBJECT_OT_material_slot_add(wmOperatorType *ot)
108 {
109         /* identifiers */
110         ot->name= "Add Material Slot";
111         ot->idname= "OBJECT_OT_material_slot_add";
112         ot->description="Add a new material slot";
113         
114         /* api callbacks */
115         ot->exec= material_slot_add_exec;
116         ot->poll= ED_operator_object_active_editable;
117
118         /* flags */
119         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
120 }
121
122 static int material_slot_remove_exec(bContext *C, wmOperator *op)
123 {
124         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
125
126         if(!ob)
127                 return OPERATOR_CANCELLED;
128
129         /* Removing material slots in edit mode screws things up, see bug #21822.*/
130         if(ob == CTX_data_edit_object(C)) {
131                 BKE_report(op->reports, RPT_ERROR, "Unable to remove material slot in edit mode");
132                 return OPERATOR_CANCELLED;
133         }
134
135         object_remove_material_slot(ob);
136         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
137         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
138         WM_event_add_notifier(C, NC_OBJECT|ND_OB_SHADING, ob);
139         
140         return OPERATOR_FINISHED;
141 }
142
143 void OBJECT_OT_material_slot_remove(wmOperatorType *ot)
144 {
145         /* identifiers */
146         ot->name= "Remove Material Slot";
147         ot->idname= "OBJECT_OT_material_slot_remove";
148         ot->description="Remove the selected material slot";
149         
150         /* api callbacks */
151         ot->exec= material_slot_remove_exec;
152         ot->poll= ED_operator_object_active_editable;
153
154         /* flags */
155         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
156 }
157
158 static int material_slot_assign_exec(bContext *C, wmOperator *UNUSED(op))
159 {
160         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
161
162         if(!ob)
163                 return OPERATOR_CANCELLED;
164
165         if(ob && ob->actcol>0) {
166                 if(ob->type == OB_MESH) {
167                         BMEditMesh *em= ((Mesh*)ob->data)->edit_btmesh;
168                         BMFace *efa;
169                         BMIter iter;
170
171                         if(em) {
172                                 BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
173                                         if(BM_TestHFlag(efa, BM_SELECT))
174                                                 efa->mat_nr= ob->actcol-1;
175                                 }
176                         }
177                 }
178                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
179                         Nurb *nu;
180                         ListBase *nurbs= curve_editnurbs((Curve*)ob->data);
181
182                         if(nurbs) {
183                                 for(nu= nurbs->first; nu; nu= nu->next)
184                                         if(isNurbsel(nu))
185                                                 nu->mat_nr= nu->charidx= ob->actcol-1;
186                         }
187                 }
188                 else if(ob->type == OB_FONT) {
189                         EditFont *ef= ((Curve*)ob->data)->editfont;
190                         int i, selstart, selend;
191
192                         if(ef && BKE_font_getselection(ob, &selstart, &selend)) {
193                                 for(i=selstart; i<=selend; i++)
194                                         ef->textbufinfo[i].mat_nr = ob->actcol;
195                         }
196                 }
197         }
198
199         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
200         WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
201         
202         return OPERATOR_FINISHED;
203 }
204
205 void OBJECT_OT_material_slot_assign(wmOperatorType *ot)
206 {
207         /* identifiers */
208         ot->name= "Assign Material Slot";
209         ot->idname= "OBJECT_OT_material_slot_assign";
210         ot->description="Assign the material in the selected material slot to the selected vertices";
211         
212         /* api callbacks */
213         ot->exec= material_slot_assign_exec;
214         ot->poll= ED_operator_object_active_editable;
215
216         /* flags */
217         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
218 }
219
220 static int material_slot_de_select(bContext *C, int select)
221 {
222         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
223
224         if(!ob)
225                 return OPERATOR_CANCELLED;
226
227         if(ob->type == OB_MESH) {
228                 BMEditMesh *em= ((Mesh*)ob->data)->edit_btmesh;
229
230                 if(em) {
231                         EDBM_deselect_by_material(em, ob->actcol-1, select);
232                 }
233         }
234         else if ELEM(ob->type, OB_CURVE, OB_SURF) {
235                 ListBase *nurbs= curve_editnurbs((Curve*)ob->data);
236                 Nurb *nu;
237                 BPoint *bp;
238                 BezTriple *bezt;
239                 int a;
240
241                 if(nurbs) {
242                         for(nu= nurbs->first; nu; nu=nu->next) {
243                                 if(nu->mat_nr==ob->actcol-1) {
244                                         if(nu->bezt) {
245                                                 a= nu->pntsu;
246                                                 bezt= nu->bezt;
247                                                 while(a--) {
248                                                         if(bezt->hide==0) {
249                                                                 if(select) {
250                                                                         bezt->f1 |= SELECT;
251                                                                         bezt->f2 |= SELECT;
252                                                                         bezt->f3 |= SELECT;
253                                                                 }
254                                                                 else {
255                                                                         bezt->f1 &= ~SELECT;
256                                                                         bezt->f2 &= ~SELECT;
257                                                                         bezt->f3 &= ~SELECT;
258                                                                 }
259                                                         }
260                                                         bezt++;
261                                                 }
262                                         }
263                                         else if(nu->bp) {
264                                                 a= nu->pntsu*nu->pntsv;
265                                                 bp= nu->bp;
266                                                 while(a--) {
267                                                         if(bp->hide==0) {
268                                                                 if(select) bp->f1 |= SELECT;
269                                                                 else bp->f1 &= ~SELECT;
270                                                         }
271                                                         bp++;
272                                                 }
273                                         }
274                                 }
275                         }
276                 }
277         }
278
279         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
280
281         return OPERATOR_FINISHED;
282 }
283
284 static int material_slot_select_exec(bContext *C, wmOperator *UNUSED(op))
285 {
286         return material_slot_de_select(C, 1);
287 }
288
289 void OBJECT_OT_material_slot_select(wmOperatorType *ot)
290 {
291         /* identifiers */
292         ot->name= "Select Material Slot";
293         ot->idname= "OBJECT_OT_material_slot_select";
294         ot->description="Select vertices assigned to the selected material slot";
295         
296         /* api callbacks */
297         ot->exec= material_slot_select_exec;
298
299         /* flags */
300         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
301 }
302
303 static int material_slot_deselect_exec(bContext *C, wmOperator *UNUSED(op))
304 {
305         return material_slot_de_select(C, 0);
306 }
307
308 void OBJECT_OT_material_slot_deselect(wmOperatorType *ot)
309 {
310         /* identifiers */
311         ot->name= "Deselect Material Slot";
312         ot->idname= "OBJECT_OT_material_slot_deselect";
313         ot->description="Deselect vertices assigned to the selected material slot";
314         
315         /* api callbacks */
316         ot->exec= material_slot_deselect_exec;
317
318         /* flags */
319         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
320 }
321
322
323 static int material_slot_copy_exec(bContext *C, wmOperator *UNUSED(op))
324 {
325         Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
326         Material ***matar;
327
328         if(!ob || !(matar= give_matarar(ob)))
329                 return OPERATOR_CANCELLED;
330
331         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
332                 if(ob != ob_iter && give_matarar(ob_iter)) {
333                         if (ob->data != ob_iter->data)
334                                 assign_matarar(ob_iter, matar, ob->totcol);
335                         
336                         if(ob_iter->totcol==ob->totcol) {
337                                 ob_iter->actcol= ob->actcol;
338                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
339                         }
340                 }
341         }
342         CTX_DATA_END;
343
344         return OPERATOR_FINISHED;
345 }
346
347
348 void OBJECT_OT_material_slot_copy(wmOperatorType *ot)
349 {
350         /* identifiers */
351         ot->name= "Copy Material to Others";
352         ot->idname= "OBJECT_OT_material_slot_copy";
353         ot->description="Copies materials to other selected objects";
354
355         /* api callbacks */
356         ot->exec= material_slot_copy_exec;
357
358         /* flags */
359         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
360 }
361
362 /********************** new material operator *********************/
363
364 static int new_material_exec(bContext *C, wmOperator *UNUSED(op))
365 {
366         Scene *scene= CTX_data_scene(C);
367         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
368         PointerRNA ptr, idptr;
369         PropertyRNA *prop;
370
371         /* add or copy material */
372         if(ma) {
373                 ma= copy_material(ma);
374         }
375         else {
376                 ma= add_material("Material");
377
378                 if(scene_use_new_shading_nodes(scene)) {
379                         ED_node_shader_default(scene, &ma->id);
380                         ma->use_nodes= 1;
381                 }
382         }
383
384         /* hook into UI */
385         uiIDContextProperty(C, &ptr, &prop);
386
387         if(prop) {
388                 /* when creating new ID blocks, use is already 1, but RNA
389                  * pointer se also increases user, so this compensates it */
390                 ma->id.us--;
391
392                 RNA_id_pointer_create(&ma->id, &idptr);
393                 RNA_property_pointer_set(&ptr, prop, idptr);
394                 RNA_property_update(C, &ptr, prop);
395         }
396
397         WM_event_add_notifier(C, NC_MATERIAL|NA_ADDED, ma);
398         
399         return OPERATOR_FINISHED;
400 }
401
402 void MATERIAL_OT_new(wmOperatorType *ot)
403 {
404         /* identifiers */
405         ot->name= "New Material";
406         ot->idname= "MATERIAL_OT_new";
407         ot->description="Add a new material";
408         
409         /* api callbacks */
410         ot->exec= new_material_exec;
411
412         /* flags */
413         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
414 }
415
416 /********************** new texture operator *********************/
417
418 static int new_texture_exec(bContext *C, wmOperator *UNUSED(op))
419 {
420         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
421         PointerRNA ptr, idptr;
422         PropertyRNA *prop;
423
424         /* add or copy texture */
425         if(tex)
426                 tex= copy_texture(tex);
427         else
428                 tex= add_texture("Texture");
429
430         /* hook into UI */
431         uiIDContextProperty(C, &ptr, &prop);
432
433         if(prop) {
434                 /* when creating new ID blocks, use is already 1, but RNA
435                  * pointer se also increases user, so this compensates it */
436                 tex->id.us--;
437
438                 RNA_id_pointer_create(&tex->id, &idptr);
439                 RNA_property_pointer_set(&ptr, prop, idptr);
440                 RNA_property_update(C, &ptr, prop);
441         }
442
443         WM_event_add_notifier(C, NC_TEXTURE|NA_ADDED, tex);
444         
445         return OPERATOR_FINISHED;
446 }
447
448 void TEXTURE_OT_new(wmOperatorType *ot)
449 {
450         /* identifiers */
451         ot->name= "New Texture";
452         ot->idname= "TEXTURE_OT_new";
453         ot->description="Add a new texture";
454         
455         /* api callbacks */
456         ot->exec= new_texture_exec;
457
458         /* flags */
459         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
460 }
461
462 /********************** new world operator *********************/
463
464 static int new_world_exec(bContext *C, wmOperator *UNUSED(op))
465 {
466         Scene *scene= CTX_data_scene(C);
467         World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
468         PointerRNA ptr, idptr;
469         PropertyRNA *prop;
470
471         /* add or copy world */
472         if(wo) {
473                 wo= copy_world(wo);
474         }
475         else {
476                 wo= add_world("World");
477
478                 if(scene_use_new_shading_nodes(scene)) {
479                         ED_node_shader_default(scene, &wo->id);
480                         wo->use_nodes= 1;
481                 }
482         }
483
484         /* hook into UI */
485         uiIDContextProperty(C, &ptr, &prop);
486
487         if(prop) {
488                 /* when creating new ID blocks, use is already 1, but RNA
489                  * pointer se also increases user, so this compensates it */
490                 wo->id.us--;
491
492                 RNA_id_pointer_create(&wo->id, &idptr);
493                 RNA_property_pointer_set(&ptr, prop, idptr);
494                 RNA_property_update(C, &ptr, prop);
495         }
496
497         WM_event_add_notifier(C, NC_WORLD|NA_ADDED, wo);
498         
499         return OPERATOR_FINISHED;
500 }
501
502 void WORLD_OT_new(wmOperatorType *ot)
503 {
504         /* identifiers */
505         ot->name= "New World";
506         ot->idname= "WORLD_OT_new";
507         ot->description= "Add a new world";
508         
509         /* api callbacks */
510         ot->exec= new_world_exec;
511
512         /* flags */
513         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
514 }
515
516 /********************** render layer operators *********************/
517
518 static int render_layer_add_exec(bContext *C, wmOperator *UNUSED(op))
519 {
520         Scene *scene= CTX_data_scene(C);
521
522         scene_add_render_layer(scene, NULL);
523         scene->r.actlay= BLI_countlist(&scene->r.layers) - 1;
524
525         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
526         
527         return OPERATOR_FINISHED;
528 }
529
530 void SCENE_OT_render_layer_add(wmOperatorType *ot)
531 {
532         /* identifiers */
533         ot->name= "Add Render Layer";
534         ot->idname= "SCENE_OT_render_layer_add";
535         ot->description="Add a render layer";
536         
537         /* api callbacks */
538         ot->exec= render_layer_add_exec;
539
540         /* flags */
541         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
542 }
543
544 static int render_layer_remove_exec(bContext *C, wmOperator *UNUSED(op))
545 {
546         Scene *scene = CTX_data_scene(C);
547         SceneRenderLayer *rl = BLI_findlink(&scene->r.layers, scene->r.actlay);
548
549         if(!scene_remove_render_layer(CTX_data_main(C), scene, rl))
550                 return OPERATOR_CANCELLED;
551
552         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
553         
554         return OPERATOR_FINISHED;
555 }
556
557 void SCENE_OT_render_layer_remove(wmOperatorType *ot)
558 {
559         /* identifiers */
560         ot->name= "Remove Render Layer";
561         ot->idname= "SCENE_OT_render_layer_remove";
562         ot->description="Remove the selected render layer";
563         
564         /* api callbacks */
565         ot->exec= render_layer_remove_exec;
566
567         /* flags */
568         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
569 }
570
571 static int texture_slot_move(bContext *C, wmOperator *op)
572 {
573         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
574
575         if(id) {
576                 MTex **mtex_ar, *mtexswap;
577                 short act;
578                 int type= RNA_enum_get(op->ptr, "type");
579                 struct AnimData *adt= BKE_animdata_from_id(id);
580
581                 give_active_mtex(id, &mtex_ar, &act);
582
583                 if(type == -1) { /* Up */
584                         if(act > 0) {
585                                 mtexswap = mtex_ar[act];
586                                 mtex_ar[act] = mtex_ar[act-1];
587                                 mtex_ar[act-1] = mtexswap;
588                                 
589                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act-1, -1, 0);
590                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act, act-1, 0);
591                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, -1, act, 0);
592
593                                 if(GS(id->name)==ID_MA) {
594                                         Material *ma= (Material *)id;
595                                         int mtexuse = ma->septex & (1<<act);
596                                         ma->septex &= ~(1<<act);
597                                         ma->septex |= (ma->septex & (1<<(act-1))) << 1;
598                                         ma->septex &= ~(1<<(act-1));
599                                         ma->septex |= mtexuse >> 1;
600                                 }
601                                 
602                                 set_active_mtex(id, act-1);
603                         }
604                 }
605                 else { /* Down */
606                         if(act < MAX_MTEX-1) {
607                                 mtexswap = mtex_ar[act];
608                                 mtex_ar[act] = mtex_ar[act+1];
609                                 mtex_ar[act+1] = mtexswap;
610                                 
611                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act+1, -1, 0);
612                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, act, act+1, 0);
613                                 BKE_animdata_fix_paths_rename(id, adt, "texture_slots", NULL, NULL, -1, act, 0);
614
615                                 if(GS(id->name)==ID_MA) {
616                                         Material *ma= (Material *)id;
617                                         int mtexuse = ma->septex & (1<<act);
618                                         ma->septex &= ~(1<<act);
619                                         ma->septex |= (ma->septex & (1<<(act+1))) >> 1;
620                                         ma->septex &= ~(1<<(act+1));
621                                         ma->septex |= mtexuse << 1;
622                                 }
623                                 
624                                 set_active_mtex(id, act+1);
625                         }
626                 }
627
628                 DAG_id_tag_update(id, 0);
629                 WM_event_add_notifier(C, NC_TEXTURE, CTX_data_scene(C));
630         }
631
632         return OPERATOR_FINISHED;
633 }
634
635 void TEXTURE_OT_slot_move(wmOperatorType *ot)
636 {
637         static EnumPropertyItem slot_move[] = {
638                 {-1, "UP", 0, "Up", ""},
639                 {1, "DOWN", 0, "Down", ""},
640                 {0, NULL, 0, NULL, NULL}
641         };
642
643         /* identifiers */
644         ot->name= "Move Texture Slot";
645         ot->idname= "TEXTURE_OT_slot_move";
646         ot->description="Move texture slots up and down";
647
648         /* api callbacks */
649         ot->exec= texture_slot_move;
650
651         /* flags */
652         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
653
654         RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
655 }
656
657
658
659 /********************** environment map operators *********************/
660
661 static int save_envmap(wmOperator *op, Scene *scene, EnvMap *env, char *path, int imtype)
662 {
663         float layout[12];
664         if ( RNA_struct_find_property(op->ptr, "layout") )
665                 RNA_float_get_array(op->ptr, "layout",layout);
666         else
667                 memcpy(layout, default_envmap_layout, sizeof(layout));
668
669         if (RE_WriteEnvmapResult(op->reports, scene, env, path, imtype, layout)) {
670                 return OPERATOR_FINISHED;
671         }
672         else {
673                 return OPERATOR_CANCELLED;
674         }
675
676 }
677
678 static int envmap_save_exec(bContext *C, wmOperator *op)
679 {
680         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
681         Scene *scene = CTX_data_scene(C);
682         //int imtype = RNA_enum_get(op->ptr, "file_type");
683         int imtype = scene->r.imtype;
684         char path[FILE_MAX];
685         
686         RNA_string_get(op->ptr, "filepath", path);
687         
688         if(scene->r.scemode & R_EXTENSION)  {
689                 BKE_add_image_extension(path, imtype);
690         }
691         
692         WM_cursor_wait(1);
693         
694         save_envmap(op, scene, tex->env, path, imtype);
695         
696         WM_cursor_wait(0);
697         
698         WM_event_add_notifier(C, NC_TEXTURE, tex);
699         
700         return OPERATOR_FINISHED;
701 }
702
703 static int envmap_save_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
704 {
705         //Scene *scene= CTX_data_scene(C);
706         
707         if(RNA_property_is_set(op->ptr, "filepath"))
708                 return envmap_save_exec(C, op);
709
710         //RNA_enum_set(op->ptr, "file_type", scene->r.imtype);
711         RNA_string_set(op->ptr, "filepath", G.main->name);
712         WM_event_add_fileselect(C, op);
713         
714         return OPERATOR_RUNNING_MODAL;
715 }
716
717 static int envmap_save_poll(bContext *C)
718 {
719         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
720
721         if (!tex) 
722                 return 0;
723         if (!tex->env || !tex->env->ok)
724                 return 0;
725         if (tex->env->cube[1]==NULL)
726                 return 0;
727         
728         return 1;
729 }
730
731 void TEXTURE_OT_envmap_save(wmOperatorType *ot)
732 {
733         PropertyRNA *prop;
734         /* identifiers */
735         ot->name= "Save Environment Map";
736         ot->idname= "TEXTURE_OT_envmap_save";
737         ot->description="Save the current generated Environment map to an image file";
738         
739         /* api callbacks */
740         ot->exec= envmap_save_exec;
741         ot->invoke= envmap_save_invoke;
742         ot->poll= envmap_save_poll;
743         
744         /* flags */
745         ot->flag= OPTYPE_REGISTER; /* no undo since this doesnt modify the env-map */
746         
747         /* properties */
748         prop= RNA_def_float_array(ot->srna, "layout", 12, default_envmap_layout, 0.0f, 0.0f, "File layout", "Flat array describing the X,Y position of each cube face in the output image, where 1 is the size of a face - order is [+Z -Z +Y -X -Y +X] (use -1 to skip a face)", 0.0f, 0.0f);
749         RNA_def_property_flag(prop, PROP_HIDDEN);
750
751         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH);
752 }
753
754 static int envmap_clear_exec(bContext *C, wmOperator *UNUSED(op))
755 {
756         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
757         
758         BKE_free_envmapdata(tex->env);
759         
760         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
761         
762         return OPERATOR_FINISHED;
763 }
764
765 static int envmap_clear_poll(bContext *C)
766 {
767         Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
768         
769         if (!tex) 
770                 return 0;
771         if (!tex->env || !tex->env->ok)
772                 return 0;
773         if (tex->env->cube[1]==NULL)
774                 return 0;
775         
776         return 1;
777 }
778
779 void TEXTURE_OT_envmap_clear(wmOperatorType *ot)
780 {
781         /* identifiers */
782         ot->name= "Clear Environment Map";
783         ot->idname= "TEXTURE_OT_envmap_clear";
784         ot->description="Discard the environment map and free it from memory";
785         
786         /* api callbacks */
787         ot->exec= envmap_clear_exec;
788         ot->poll= envmap_clear_poll;
789         
790         /* flags */
791         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
792 }
793
794 static int envmap_clear_all_exec(bContext *C, wmOperator *UNUSED(op))
795 {
796         Main *bmain = CTX_data_main(C);
797         Tex *tex;
798         
799         for (tex=bmain->tex.first; tex; tex=tex->id.next)
800                 if (tex->env)
801                         BKE_free_envmapdata(tex->env);
802         
803         WM_event_add_notifier(C, NC_TEXTURE|NA_EDITED, tex);
804         
805         return OPERATOR_FINISHED;
806 }
807
808 void TEXTURE_OT_envmap_clear_all(wmOperatorType *ot)
809 {
810         /* identifiers */
811         ot->name= "Clear All Environment Maps";
812         ot->idname= "TEXTURE_OT_envmap_clear_all";
813         ot->description="Discard all environment maps in the .blend file and free them from memory";
814         
815         /* api callbacks */
816         ot->exec= envmap_clear_all_exec;
817         ot->poll= envmap_clear_poll;
818         
819         /* flags */
820         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
821 }
822
823 /********************** material operators *********************/
824
825 /* material copy/paste */
826 static int copy_material_exec(bContext *C, wmOperator *UNUSED(op))
827 {
828         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
829
830         if(ma==NULL)
831                 return OPERATOR_CANCELLED;
832
833         copy_matcopybuf(ma);
834
835         return OPERATOR_FINISHED;
836 }
837
838 void MATERIAL_OT_copy(wmOperatorType *ot)
839 {
840         /* identifiers */
841         ot->name= "Copy Material";
842         ot->idname= "MATERIAL_OT_copy";
843         ot->description="Copy the material settings and nodes";
844
845         /* api callbacks */
846         ot->exec= copy_material_exec;
847
848         /* flags */
849         ot->flag= OPTYPE_REGISTER; /* no undo needed since no changes are made to the material */
850 }
851
852 static int paste_material_exec(bContext *C, wmOperator *UNUSED(op))
853 {
854         Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
855
856         if(ma==NULL)
857                 return OPERATOR_CANCELLED;
858
859         paste_matcopybuf(ma);
860
861         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING_DRAW, ma);
862
863         return OPERATOR_FINISHED;
864 }
865
866 void MATERIAL_OT_paste(wmOperatorType *ot)
867 {
868         /* identifiers */
869         ot->name= "Paste Material";
870         ot->idname= "MATERIAL_OT_paste";
871         ot->description="Paste the material settings and nodes";
872
873         /* api callbacks */
874         ot->exec= paste_material_exec;
875
876         /* flags */
877         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
878 }
879
880
881 static short mtexcopied=0; /* must be reset on file load */
882 static MTex mtexcopybuf;
883
884 void ED_render_clear_mtex_copybuf(void)
885 {       /* use for file reload */
886         mtexcopied= 0;
887 }
888
889 static void copy_mtex_copybuf(ID *id)
890 {
891         MTex **mtex= NULL;
892         
893         switch(GS(id->name)) {
894                 case ID_MA:
895                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
896                         break;
897                 case ID_LA:
898                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
899                         // la->mtex[(int)la->texact] // TODO
900                         break;
901                 case ID_WO:
902                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
903                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
904                         break;
905                 case ID_PA:
906                         mtex= &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
907                         break;
908         }
909         
910         if(mtex && *mtex) {
911                 memcpy(&mtexcopybuf, *mtex, sizeof(MTex));
912                 mtexcopied= 1;
913         }
914         else {
915                 mtexcopied= 0;
916         }
917 }
918
919 static void paste_mtex_copybuf(ID *id)
920 {
921         MTex **mtex= NULL;
922         
923         if(mtexcopied == 0 || mtexcopybuf.tex==NULL)
924                 return;
925         
926         switch(GS(id->name)) {
927                 case ID_MA:
928                         mtex= &(((Material *)id)->mtex[(int)((Material *)id)->texact]);
929                         break;
930                 case ID_LA:
931                         mtex= &(((Lamp *)id)->mtex[(int)((Lamp *)id)->texact]);
932                         // la->mtex[(int)la->texact] // TODO
933                         break;
934                 case ID_WO:
935                         mtex= &(((World *)id)->mtex[(int)((World *)id)->texact]);
936                         // mtex= wrld->mtex[(int)wrld->texact]; // TODO
937                         break;
938                 case ID_PA:
939                         mtex= &(((ParticleSettings *)id)->mtex[(int)((ParticleSettings *)id)->texact]);
940                         break;
941                 default:
942                         BLI_assert("invalid id type");
943                         return;
944         }
945         
946         if(mtex) {
947                 if(*mtex==NULL) {
948                         *mtex= MEM_mallocN(sizeof(MTex), "mtex copy");
949                 }
950                 else if((*mtex)->tex) {
951                         (*mtex)->tex->id.us--;
952                 }
953                 
954                 memcpy(*mtex, &mtexcopybuf, sizeof(MTex));
955                 
956                 id_us_plus((ID *)mtexcopybuf.tex);
957         }
958 }
959
960
961 static int copy_mtex_exec(bContext *C, wmOperator *UNUSED(op))
962 {
963         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
964
965         if(id==NULL) {
966                 /* copying empty slot */
967                 ED_render_clear_mtex_copybuf();
968                 return OPERATOR_CANCELLED;
969         }
970
971         copy_mtex_copybuf(id);
972
973         return OPERATOR_FINISHED;
974 }
975
976 static int copy_mtex_poll(bContext *C)
977 {
978         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
979         
980         return (id != NULL);
981 }
982
983 void TEXTURE_OT_slot_copy(wmOperatorType *ot)
984 {
985         /* identifiers */
986         ot->name= "Copy Texture Slot Settings";
987         ot->idname= "TEXTURE_OT_slot_copy";
988         ot->description="Copy the material texture settings and nodes";
989
990         /* api callbacks */
991         ot->exec= copy_mtex_exec;
992         ot->poll= copy_mtex_poll;
993         
994         /* flags */
995         ot->flag= OPTYPE_REGISTER; /* no undo needed since no changes are made to the mtex */
996 }
997
998 static int paste_mtex_exec(bContext *C, wmOperator *UNUSED(op))
999 {
1000         ID *id= CTX_data_pointer_get_type(C, "texture_slot", &RNA_TextureSlot).id.data;
1001
1002         if(id==NULL) {
1003                 Material *ma= CTX_data_pointer_get_type(C, "material", &RNA_Material).data;
1004                 Lamp *la= CTX_data_pointer_get_type(C, "lamp", &RNA_Lamp).data;
1005                 World *wo= CTX_data_pointer_get_type(C, "world", &RNA_World).data;
1006                 ParticleSystem *psys= CTX_data_pointer_get_type(C, "particle_system", &RNA_ParticleSystem).data;
1007                 
1008                 if (ma)
1009                         id = &ma->id;
1010                 else if (la)
1011                         id = &la->id;
1012                 else if (wo)
1013                         id = &wo->id;
1014                 else if (psys)
1015                         id = &psys->part->id;
1016                 
1017                 if (id==NULL)
1018                         return OPERATOR_CANCELLED;
1019         }
1020
1021         paste_mtex_copybuf(id);
1022
1023         WM_event_add_notifier(C, NC_TEXTURE|ND_SHADING_DRAW, NULL);
1024
1025         return OPERATOR_FINISHED;
1026 }
1027
1028 void TEXTURE_OT_slot_paste(wmOperatorType *ot)
1029 {
1030         /* identifiers */
1031         ot->name= "Paste Texture Slot Settings";
1032         ot->idname= "TEXTURE_OT_slot_paste";
1033         ot->description="Copy the texture settings and nodes";
1034
1035         /* api callbacks */
1036         ot->exec= paste_mtex_exec;
1037
1038         /* flags */
1039         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1040 }
1041