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