Blender V2.61 - r43446

uvedit_buttons.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) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * Contributor(s): Blender Foundation, 2002-2009
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00030 #include <string.h>
00031 #include <stdio.h>
00032 
00033 #include "MEM_guardedalloc.h"
00034 
00035 #include "DNA_meshdata_types.h"
00036 #include "DNA_object_types.h"
00037 #include "DNA_scene_types.h"
00038 #include "DNA_screen_types.h"
00039 #include "DNA_space_types.h"
00040 
00041 #include "BLI_blenlib.h"
00042 #include "BLI_math.h"
00043 #include "BLI_editVert.h"
00044 #include "BLI_utildefines.h"
00045 
00046 #include "BKE_context.h"
00047 #include "BKE_customdata.h"
00048 #include "BKE_mesh.h"
00049 #include "BKE_screen.h"
00050 
00051 #include "ED_image.h"
00052 #include "ED_uvedit.h"
00053 
00054 #include "UI_interface.h"
00055 #include "UI_resources.h"
00056 
00057 #include "WM_api.h"
00058 #include "WM_types.h"
00059 
00060 #define B_UVEDIT_VERTEX     3
00061 
00062 /* UV Utilities */
00063 
00064 static int uvedit_center(Scene *scene, EditMesh *em, Image *ima, float center[2])
00065 {
00066     EditFace *efa;
00067     MTFace *tf;
00068     int tot= 0;
00069 
00070     zero_v2(center);
00071 
00072     for(efa= em->faces.first; efa; efa= efa->next) {
00073         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
00074 
00075         if(uvedit_face_visible(scene, ima, efa, tf)) {
00076             if(uvedit_uv_selected(scene, efa, tf, 0)) {
00077                 add_v2_v2(center, tf->uv[0]);
00078                 tot++;
00079             }
00080             if(uvedit_uv_selected(scene, efa, tf, 1)) {
00081                 add_v2_v2(center, tf->uv[1]);
00082                 tot++;
00083             }
00084             if(uvedit_uv_selected(scene, efa, tf, 2)) {
00085                 add_v2_v2(center, tf->uv[2]);
00086                 tot++;
00087             }
00088             if(efa->v4 && uvedit_uv_selected(scene, efa, tf, 3)) {
00089                 add_v2_v2(center, tf->uv[3]);
00090                 tot++;
00091             }
00092         }
00093     }
00094 
00095     if(tot > 0) {
00096         center[0] /= tot;
00097         center[1] /= tot;
00098     }
00099 
00100     return tot;
00101 }
00102 
00103 static void uvedit_translate(Scene *scene, EditMesh *em, Image *ima, float delta[2])
00104 {
00105     EditFace *efa;
00106     MTFace *tf;
00107 
00108     for(efa= em->faces.first; efa; efa= efa->next) {
00109         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
00110 
00111         if(uvedit_face_visible(scene, ima, efa, tf)) {
00112             if(uvedit_uv_selected(scene, efa, tf, 0))
00113                 add_v2_v2(tf->uv[0], delta);
00114             if(uvedit_uv_selected(scene, efa, tf, 1))
00115                 add_v2_v2(tf->uv[1], delta);
00116             if(uvedit_uv_selected(scene, efa, tf, 2))
00117                 add_v2_v2(tf->uv[2], delta);
00118             if(efa->v4 && uvedit_uv_selected(scene, efa, tf, 3))
00119                 add_v2_v2(tf->uv[3], delta);
00120         }
00121     }
00122 }
00123 
00124 /* Button Functions, using an evil static variable */
00125 
00126 static float uvedit_old_center[2];
00127 
00128 static void uvedit_vertex_buttons(const bContext *C, uiBlock *block)
00129 {
00130     SpaceImage *sima= CTX_wm_space_image(C);
00131     Scene *scene= CTX_data_scene(C);
00132     Object *obedit= CTX_data_edit_object(C);
00133     Image *ima= sima->image;
00134     float center[2];
00135     int imx, imy, step, digits;
00136     EditMesh *em;
00137 
00138     ED_space_image_size(sima, &imx, &imy);
00139     
00140     em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
00141 
00142     if(uvedit_center(scene, em, ima, center)) {
00143         copy_v2_v2(uvedit_old_center, center);
00144 
00145         if(!(sima->flag & SI_COORDFLOATS)) {
00146             uvedit_old_center[0] *= imx;
00147             uvedit_old_center[1] *= imy;
00148         }
00149 
00150         if(sima->flag & SI_COORDFLOATS) {
00151             step= 1;
00152             digits= 3;
00153         }
00154         else {
00155             step= 100;
00156             digits= 2;
00157         }
00158         
00159         uiBlockBeginAlign(block);
00160         uiDefButF(block, NUM, B_UVEDIT_VERTEX, "X:",    10, 10, 145, 19, &uvedit_old_center[0], -10*imx, 10.0*imx, step, digits, "");
00161         uiDefButF(block, NUM, B_UVEDIT_VERTEX, "Y:",    165, 10, 145, 19, &uvedit_old_center[1], -10*imy, 10.0*imy, step, digits, "");
00162         uiBlockEndAlign(block);
00163     }
00164 
00165     BKE_mesh_end_editmesh(obedit->data, em);
00166 }
00167 
00168 static void do_uvedit_vertex(bContext *C, void *UNUSED(arg), int event)
00169 {
00170     SpaceImage *sima= CTX_wm_space_image(C);
00171     Scene *scene= CTX_data_scene(C);
00172     Object *obedit= CTX_data_edit_object(C);
00173     Image *ima= sima->image;
00174     EditMesh *em;
00175     float center[2], delta[2];
00176     int imx, imy;
00177 
00178     if(event != B_UVEDIT_VERTEX)
00179         return;
00180 
00181     em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
00182 
00183     ED_space_image_size(sima, &imx, &imy);
00184     uvedit_center(scene, em, ima, center);
00185 
00186     if(sima->flag & SI_COORDFLOATS) {
00187         delta[0]= uvedit_old_center[0] - center[0];
00188         delta[1]= uvedit_old_center[1] - center[1];
00189     }
00190     else {
00191         delta[0]= uvedit_old_center[0]/imx - center[0];
00192         delta[1]= uvedit_old_center[1]/imy - center[1];
00193     }
00194 
00195     uvedit_translate(scene, em, ima, delta);
00196 
00197     BKE_mesh_end_editmesh(obedit->data, em);
00198 
00199     WM_event_add_notifier(C, NC_IMAGE, sima->image);
00200 }
00201 
00202 /* Panels */
00203 
00204 static int image_panel_uv_poll(const bContext *C, PanelType *UNUSED(pt))
00205 {
00206     Object *obedit= CTX_data_edit_object(C);
00207     return ED_uvedit_test(obedit);
00208 }
00209 
00210 static void image_panel_uv(const bContext *C, Panel *pa)
00211 {
00212     uiBlock *block;
00213     
00214     block= uiLayoutAbsoluteBlock(pa->layout);
00215     uiBlockSetHandleFunc(block, do_uvedit_vertex, NULL);
00216 
00217     uvedit_vertex_buttons(C, block);
00218 }   
00219 
00220 void ED_uvedit_buttons_register(ARegionType *art)
00221 {
00222     PanelType *pt;
00223 
00224     pt= MEM_callocN(sizeof(PanelType), "spacetype image panel uv");
00225     strcpy(pt->idname, "IMAGE_PT_uv");
00226     strcpy(pt->label, "UV Vertex");
00227     pt->draw= image_panel_uv;
00228     pt->poll= image_panel_uv_poll;
00229     BLI_addtail(&art->paneltypes, pt);
00230 }
00231