Blender V2.61 - r43446

ed_util.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version. 
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2008 Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * 
00022  * Contributor(s): Blender Foundation
00023  *
00024  * ***** END GPL LICENSE BLOCK *****
00025  */
00026 
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <math.h>
00035 
00036 #include "MEM_guardedalloc.h"
00037 
00038 #include "DNA_object_types.h"
00039 #include "DNA_scene_types.h"
00040 #include "DNA_packedFile_types.h"
00041 
00042 #include "BLI_blenlib.h"
00043 #include "BLI_editVert.h"
00044 
00045 #include "BKE_context.h"
00046 #include "BKE_global.h"
00047 #include "BKE_main.h"
00048 #include "BKE_utildefines.h"
00049 #include "BKE_packedFile.h"
00050 
00051 #include "ED_armature.h"
00052 #include "ED_mesh.h"
00053 #include "ED_object.h"
00054 #include "ED_sculpt.h"
00055 #include "ED_util.h"
00056 
00057 #include "UI_interface.h"
00058 #include "UI_resources.h"
00059 
00060 #include "WM_types.h"
00061 #include "RNA_access.h"
00062 
00063 
00064 
00065 /* ********* general editor util funcs, not BKE stuff please! ********* */
00066 
00067 void ED_editors_init(bContext *C)
00068 {
00069     Main *bmain= CTX_data_main(C);
00070     Scene *sce= CTX_data_scene(C);
00071     Object *ob, *obact= (sce && sce->basact)? sce->basact->object: NULL;
00072     ID *data;
00073 
00074     /* toggle on modes for objects that were saved with these enabled. for
00075        e.g. linked objects we have to ensure that they are actually the
00076        active object in this scene. */
00077     for(ob=bmain->object.first; ob; ob=ob->id.next) {
00078         int mode= ob->mode;
00079 
00080         if(mode && (mode != OB_MODE_POSE)) {
00081             ob->mode= 0;
00082             data= ob->data;
00083 
00084             if(ob == obact && !ob->id.lib && !(data && data->lib))
00085                 ED_object_toggle_modes(C, mode);
00086         }
00087     }
00088 }
00089 
00090 /* frees all editmode stuff */
00091 void ED_editors_exit(bContext *C)
00092 {
00093     Main *bmain= CTX_data_main(C);
00094     Scene *sce;
00095 
00096     if(!bmain)
00097         return;
00098     
00099     /* frees all editmode undos */
00100     undo_editmode_clear();
00101     ED_undo_paint_free();
00102     
00103     for(sce=bmain->scene.first; sce; sce= sce->id.next) {
00104         if(sce->obedit) {
00105             Object *ob= sce->obedit;
00106         
00107             if(ob) {
00108                 if(ob->type==OB_MESH) {
00109                     Mesh *me= ob->data;
00110                     if(me->edit_mesh) {
00111                         free_editMesh(me->edit_mesh);
00112                         MEM_freeN(me->edit_mesh);
00113                         me->edit_mesh= NULL;
00114                     }
00115                 }
00116                 else if(ob->type==OB_ARMATURE) {
00117                     ED_armature_edit_free(ob);
00118                 }
00119             }
00120         }
00121     }
00122 
00123     /* global in meshtools... */
00124     mesh_octree_table(NULL, NULL, NULL, 'e');
00125     mesh_mirrtopo_table(NULL, 'e');
00126 }
00127 
00128 
00129 /* ***** XXX: functions are using old blender names, cleanup later ***** */
00130 
00131 
00132 /* now only used in 2d spaces, like time, ipo, nla, sima... */
00133 /* XXX shift/ctrl not configurable */
00134 void apply_keyb_grid(int shift, int ctrl, float *val, float fac1, float fac2, float fac3, int invert)
00135 {
00136     /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
00137     if(invert)
00138         ctrl= !ctrl;
00139     
00140     if(ctrl && shift) {
00141         if(fac3 != 0.0f) *val= fac3*floorf(*val/fac3 +0.5f);
00142     }
00143     else if(ctrl) {
00144         if(fac2 != 0.0f) *val= fac2*floorf(*val/fac2 +0.5f);
00145     }
00146     else {
00147         if(fac1 != 0.0f) *val= fac1*floorf(*val/fac1 +0.5f);
00148     }
00149 }
00150 
00151 
00152 int GetButStringLength(const char *str) 
00153 {
00154     int rt;
00155     
00156     rt= UI_GetStringWidth(str);
00157     
00158     return rt + 15;
00159 }
00160 
00161 
00162 void unpack_menu(bContext *C, const char *opname, const char *id_name, const char *abs_name, const char *folder, struct PackedFile *pf)
00163 {
00164     PointerRNA props_ptr;
00165     uiPopupMenu *pup;
00166     uiLayout *layout;
00167     char line[FILE_MAX + 100];
00168 
00169     pup= uiPupMenuBegin(C, "Unpack file", ICON_NONE);
00170     layout= uiPupMenuLayout(pup);
00171 
00172     strcpy(line, "Remove Pack");
00173     props_ptr= uiItemFullO(layout, opname, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
00174     RNA_enum_set(&props_ptr, "method", PF_REMOVE);
00175     RNA_string_set(&props_ptr, "id", id_name);
00176 
00177     if(G.relbase_valid) {
00178         char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
00179 
00180         BLI_strncpy(local_name, abs_name, sizeof(local_name));
00181         BLI_splitdirstring(local_name, fi);
00182         BLI_snprintf(local_name, sizeof(local_name), "//%s/%s", folder, fi);
00183         if(strcmp(abs_name, local_name)!=0) {
00184             switch(checkPackedFile(local_name, pf)) {
00185                 case PF_NOFILE:
00186                     BLI_snprintf(line, sizeof(line), "Create %s", local_name);
00187                     props_ptr= uiItemFullO(layout, opname, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
00188                     RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
00189                     RNA_string_set(&props_ptr, "id", id_name);
00190 
00191                     break;
00192                 case PF_EQUAL:
00193                     BLI_snprintf(line, sizeof(line), "Use %s (identical)", local_name);
00194                     //uiItemEnumO(layout, opname, line, 0, "method", PF_USE_LOCAL);
00195                     props_ptr= uiItemFullO(layout, opname, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
00196                     RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
00197                     RNA_string_set(&props_ptr, "id", id_name);
00198 
00199                     break;
00200                 case PF_DIFFERS:
00201                     BLI_snprintf(line, sizeof(line), "Use %s (differs)", local_name);
00202                     //uiItemEnumO(layout, opname, line, 0, "method", PF_USE_LOCAL);
00203                     props_ptr= uiItemFullO(layout, opname, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
00204                     RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
00205                     RNA_string_set(&props_ptr, "id", id_name);
00206 
00207                     BLI_snprintf(line, sizeof(line), "Overwrite %s", local_name);
00208                     //uiItemEnumO(layout, opname, line, 0, "method", PF_WRITE_LOCAL);
00209                     props_ptr= uiItemFullO(layout, opname, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
00210                     RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
00211                     RNA_string_set(&props_ptr, "id", id_name);
00212                     break;
00213             }
00214         }
00215     }
00216 
00217     switch(checkPackedFile(abs_name, pf)) {
00218         case PF_NOFILE:
00219             BLI_snprintf(line, sizeof(line), "Create %s", abs_name);
00220             //uiItemEnumO(layout, opname, line, 0, "method", PF_WRITE_ORIGINAL);
00221             props_ptr= uiItemFullO(layout, opname, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
00222             RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
00223             RNA_string_set(&props_ptr, "id", id_name);
00224             break;
00225         case PF_EQUAL:
00226             BLI_snprintf(line, sizeof(line), "Use %s (identical)", abs_name);
00227             //uiItemEnumO(layout, opname, line, 0, "method", PF_USE_ORIGINAL);
00228             props_ptr= uiItemFullO(layout, opname, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
00229             RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
00230             RNA_string_set(&props_ptr, "id", id_name);
00231             break;
00232         case PF_DIFFERS:
00233             BLI_snprintf(line, sizeof(line), "Use %s (differs)", abs_name);
00234             //uiItemEnumO(layout, opname, line, 0, "method", PF_USE_ORIGINAL);
00235             props_ptr= uiItemFullO(layout, opname, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
00236             RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
00237             RNA_string_set(&props_ptr, "id", id_name);
00238 
00239             BLI_snprintf(line, sizeof(line), "Overwrite %s", abs_name);
00240             //uiItemEnumO(layout, opname, line, 0, "method", PF_WRITE_ORIGINAL);
00241             props_ptr= uiItemFullO(layout, opname, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
00242             RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
00243             RNA_string_set(&props_ptr, "id", id_name);
00244             break;
00245     }
00246 
00247     uiPupMenuEnd(C, pup);
00248 }