6f8d710ed3eb6fb85a6804ca0b189b95e8bb2124
[blender-staging.git] / source / blender / editors / uvedit / uvedit_ops.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_object_types.h"
37 #include "DNA_mesh_types.h"
38 #include "DNA_meshdata_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_windowmanager_types.h"
43
44 #include "BLI_arithb.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_editVert.h"
47
48 #include "BKE_context.h"
49 #include "BKE_customdata.h"
50 #include "BKE_depsgraph.h"
51 #include "BKE_image.h"
52 #include "BKE_library.h"
53 #include "BKE_mesh.h"
54 #include "BKE_report.h"
55 #include "BKE_utildefines.h"
56
57 #include "ED_image.h"
58 #include "ED_mesh.h"
59 #include "ED_screen.h"
60 #include "ED_transform.h"
61
62 #include "RNA_access.h"
63 #include "RNA_define.h"
64 #include "RNA_types.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "UI_view2d.h"
70
71 #include "uvedit_intern.h"
72
73 /************************* state testing ************************/
74
75 int ED_uvedit_test(Object *obedit)
76 {
77         EditMesh *em;
78         int ret;
79
80         if(obedit->type != OB_MESH)
81                 return 0;
82
83         em = BKE_mesh_get_editmesh(obedit->data);
84         ret = EM_texFaceCheck(em);
85         BKE_mesh_end_editmesh(obedit->data, em);
86         
87         return ret;
88 }
89
90 /************************* assign image ************************/
91
92 void ED_uvedit_assign_image(Scene *scene, Object *obedit, Image *ima, Image *previma)
93 {
94         EditMesh *em;
95         EditFace *efa;
96         MTFace *tf;
97         int update= 0;
98         
99         /* skip assigning these procedural images... */
100         if(ima && (ima->type==IMA_TYPE_R_RESULT || ima->type==IMA_TYPE_COMPOSITE))
101                 return;
102
103         /* verify we have a mesh we can work with */
104         if(!obedit || (obedit->type != OB_MESH))
105                 return;
106
107         em= BKE_mesh_get_editmesh(((Mesh*)obedit->data));
108         if(!em || !em->faces.first) {
109                 BKE_mesh_end_editmesh(obedit->data, em);
110                 return;
111         }
112         
113         /* ensure we have a uv layer */
114         if(!CustomData_has_layer(&em->fdata, CD_MTFACE)) {
115                 EM_add_data_layer(em, &em->fdata, CD_MTFACE);
116                 update= 1;
117         }
118
119         /* now assign to all visible faces */
120         for(efa= em->faces.first; efa; efa= efa->next) {
121                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
122
123                 if(uvedit_face_visible(scene, previma, efa, tf)) {
124                         if(ima) {
125                                 tf->tpage= ima;
126                                 tf->mode |= TF_TEX;
127                                 
128                                 if(ima->id.us==0) id_us_plus(&ima->id);
129                                 else id_lib_extern(&ima->id);
130                         }
131                         else {
132                                 tf->tpage= NULL;
133                                 tf->mode &= ~TF_TEX;
134                         }
135
136                         update = 1;
137                 }
138         }
139
140         /* and update depdency graph */
141         if(update)
142                 DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
143
144         BKE_mesh_end_editmesh(obedit->data, em);
145 }
146
147 /* dotile -     1, set the tile flag (from the space image)
148  *                      2, set the tile index for the faces. */
149 void ED_uvedit_set_tile(bContext *C, Scene *scene, Object *obedit, Image *ima, int curtile)
150 {
151         EditMesh *em;
152         EditFace *efa;
153         MTFace *tf;
154         
155         /* verify if we have something to do */
156         if(!ima || !ED_uvedit_test(obedit))
157                 return;
158         
159         /* skip assigning these procedural images... */
160         if(ima->type==IMA_TYPE_R_RESULT || ima->type==IMA_TYPE_COMPOSITE)
161                 return;
162         
163         em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
164
165         for(efa= em->faces.first; efa; efa= efa->next) {
166                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
167
168                 if(efa->h==0 && efa->f & SELECT)
169                         tf->tile= curtile; /* set tile index */
170         }
171
172         DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
173         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, obedit);
174         BKE_mesh_end_editmesh(obedit->data, em);
175 }
176
177 /*********************** space conversion *********************/
178
179 static void uvedit_pixel_to_float(SpaceImage *sima, float *dist, float pixeldist)
180 {
181         int width, height;
182
183         ED_space_image_size(sima, &width, &height);
184
185         dist[0]= pixeldist/width;
186         dist[1]= pixeldist/height;
187 }
188
189 /*************** visibility and selection utilities **************/
190
191 int uvedit_face_visible_nolocal(Scene *scene, EditFace *efa)
192 {
193         ToolSettings *ts= scene->toolsettings;
194
195         if(ts->uv_flag & UV_SYNC_SELECTION)
196                 return (efa->h==0);
197         else
198                 return (efa->h==0 && (efa->f & SELECT));
199 }
200
201 int uvedit_face_visible(Scene *scene, Image *ima, EditFace *efa, MTFace *tf)
202 {
203         ToolSettings *ts= scene->toolsettings;
204
205         if(ts->uv_flag & UV_SHOW_SAME_IMAGE)
206                 return (tf->tpage==ima)? uvedit_face_visible_nolocal(scene, efa): 0;
207         else
208                 return uvedit_face_visible_nolocal(scene, efa);
209 }
210
211 int uvedit_face_selected(Scene *scene, EditFace *efa, MTFace *tf)
212 {
213         ToolSettings *ts= scene->toolsettings;
214
215         if(ts->uv_flag & UV_SYNC_SELECTION)
216                 return (efa->f & SELECT);
217         else
218                 return (!(~tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) &&(!efa->v4 || tf->flag & TF_SEL4));
219 }
220
221 void uvedit_face_select(Scene *scene, EditFace *efa, MTFace *tf)
222 {
223         ToolSettings *ts= scene->toolsettings;
224
225         if(ts->uv_flag & UV_SYNC_SELECTION)
226                 EM_select_face(efa, 1);
227         else
228                 tf->flag |= (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
229 }
230
231 void uvedit_face_deselect(Scene *scene, EditFace *efa, MTFace *tf)
232 {
233         ToolSettings *ts= scene->toolsettings;
234
235         if(ts->uv_flag & UV_SYNC_SELECTION)
236                 EM_select_face(efa, 0);
237         else
238                 tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
239 }
240
241 int uvedit_edge_selected(Scene *scene, EditFace *efa, MTFace *tf, int i)
242 {
243         ToolSettings *ts= scene->toolsettings;
244         int nvert= (efa->v4)? 4: 3;
245
246         if(ts->uv_flag & UV_SYNC_SELECTION) {
247                 if(ts->selectmode == SCE_SELECT_FACE)
248                         return (efa->f & SELECT);
249                 else if(ts->selectmode == SCE_SELECT_EDGE)
250                         return (*(&efa->e1 + i))->f & SELECT;
251                 else
252                         return (((efa->v1 + i)->f & SELECT) && ((efa->v1 + (i+1)%nvert)->f & SELECT));
253         }
254         else
255                 return (tf->flag & TF_SEL_MASK(i)) && (tf->flag & TF_SEL_MASK((i+1)%nvert));
256 }
257
258 void uvedit_edge_select(Scene *scene, EditFace *efa, MTFace *tf, int i)
259 {
260         ToolSettings *ts= scene->toolsettings;
261         int nvert= (efa->v4)? 4: 3;
262
263         if(ts->uv_flag & UV_SYNC_SELECTION) {
264                 if(ts->selectmode == SCE_SELECT_FACE)
265                         EM_select_face(efa, 1);
266                 else if(ts->selectmode == SCE_SELECT_EDGE)
267                         EM_select_edge((*(&efa->e1 + i)), 1);
268                 else {
269                         (efa->v1 + i)->f |= SELECT;
270                         (efa->v1 + (i+1)%nvert)->f |= SELECT;
271                 }
272         }
273         else
274                 tf->flag |= TF_SEL_MASK(i)|TF_SEL_MASK((i+1)%nvert);
275 }
276
277 void uvedit_edge_deselect(Scene *scene, EditFace *efa, MTFace *tf, int i)
278 {
279         ToolSettings *ts= scene->toolsettings;
280         int nvert= (efa->v4)? 4: 3;
281
282         if(ts->uv_flag & UV_SYNC_SELECTION) {
283                 if(ts->selectmode == SCE_SELECT_FACE)
284                         EM_select_face(efa, 0);
285                 else if(ts->selectmode == SCE_SELECT_EDGE)
286                         EM_select_edge((*(&efa->e1 + i)), 0);
287                 else {
288                         (efa->v1 + i)->f &= ~SELECT;
289                         (efa->v1 + (i+1)%nvert)->f &= ~SELECT;
290                 }
291         }
292         else
293                 tf->flag &= ~(TF_SEL_MASK(i)|TF_SEL_MASK((i+1)%nvert));
294 }
295
296 int uvedit_uv_selected(Scene *scene, EditFace *efa, MTFace *tf, int i)
297 {
298         ToolSettings *ts= scene->toolsettings;
299
300         if(ts->uv_flag & UV_SYNC_SELECTION) {
301                 if(ts->selectmode == SCE_SELECT_FACE)
302                         return (efa->f & SELECT);
303                 else
304                         return (*(&efa->v1 + i))->f & SELECT;
305         }
306         else
307                 return tf->flag & TF_SEL_MASK(i);
308 }
309
310 void uvedit_uv_select(Scene *scene, EditFace *efa, MTFace *tf, int i)
311 {
312         ToolSettings *ts= scene->toolsettings;
313
314         if(ts->uv_flag & UV_SYNC_SELECTION) {
315                 if(ts->selectmode == SCE_SELECT_FACE)
316                         EM_select_face(efa, 1);
317                 else
318                         (*(&efa->v1 + i))->f |= SELECT;
319         }
320         else
321                 tf->flag |= TF_SEL_MASK(i);
322 }
323
324 void uvedit_uv_deselect(Scene *scene, EditFace *efa, MTFace *tf, int i)
325 {
326         ToolSettings *ts= scene->toolsettings;
327
328         if(ts->uv_flag & UV_SYNC_SELECTION) {
329                 if(ts->selectmode == SCE_SELECT_FACE)
330                         EM_select_face(efa, 0);
331                 else
332                         (*(&efa->v1 + i))->f &= ~SELECT;
333         }
334         else
335                 tf->flag &= ~TF_SEL_MASK(i);
336 }
337
338 /*********************** geometric utilities ***********************/
339
340 void uv_center(float uv[][2], float cent[2], int quad)
341 {
342         if(quad) {
343                 cent[0] = (uv[0][0] + uv[1][0] + uv[2][0] + uv[3][0]) / 4.0;
344                 cent[1] = (uv[0][1] + uv[1][1] + uv[2][1] + uv[3][1]) / 4.0;            
345         }
346         else {
347                 cent[0] = (uv[0][0] + uv[1][0] + uv[2][0]) / 3.0;
348                 cent[1] = (uv[0][1] + uv[1][1] + uv[2][1]) / 3.0;               
349         }
350 }
351
352 float uv_area(float uv[][2], int quad)
353 {
354         if(quad)
355                 return AreaF2Dfl(uv[0], uv[1], uv[2]) + AreaF2Dfl(uv[0], uv[2], uv[3]); 
356         else
357                 return AreaF2Dfl(uv[0], uv[1], uv[2]); 
358 }
359
360 void uv_copy_aspect(float uv_orig[][2], float uv[][2], float aspx, float aspy)
361 {
362         uv[0][0] = uv_orig[0][0]*aspx;
363         uv[0][1] = uv_orig[0][1]*aspy;
364         
365         uv[1][0] = uv_orig[1][0]*aspx;
366         uv[1][1] = uv_orig[1][1]*aspy;
367         
368         uv[2][0] = uv_orig[2][0]*aspx;
369         uv[2][1] = uv_orig[2][1]*aspy;
370         
371         uv[3][0] = uv_orig[3][0]*aspx;
372         uv[3][1] = uv_orig[3][1]*aspy;
373 }
374
375 int ED_uvedit_minmax(Scene *scene, Image *ima, Object *obedit, float *min, float *max)
376 {
377         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
378         EditFace *efa;
379         MTFace *tf;
380         int sel;
381
382         INIT_MINMAX2(min, max);
383
384         sel= 0;
385         for(efa= em->faces.first; efa; efa= efa->next) {
386                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
387                 if(uvedit_face_visible(scene, ima, efa, tf)) {
388                         if(uvedit_uv_selected(scene, efa, tf, 0))                               { DO_MINMAX2(tf->uv[0], min, max); sel = 1; }
389                         if(uvedit_uv_selected(scene, efa, tf, 1))                               { DO_MINMAX2(tf->uv[1], min, max); sel = 1; }
390                         if(uvedit_uv_selected(scene, efa, tf, 2))                               { DO_MINMAX2(tf->uv[2], min, max); sel = 1; }
391                         if(efa->v4 && (uvedit_uv_selected(scene, efa, tf, 3)))  { DO_MINMAX2(tf->uv[3], min, max); sel = 1; }
392                 }
393         }
394         
395         BKE_mesh_end_editmesh(obedit->data, em);
396         return sel;
397 }
398
399 int uvedit_center(Scene *scene, Image *ima, Object *obedit, float *cent, int mode)
400 {
401         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
402         EditFace *efa;
403         MTFace *tf;
404         float min[2], max[2];
405         int change= 0;
406         
407         if(mode==0) {
408                 if(ED_uvedit_minmax(scene, ima, obedit, min, max))
409                         change = 1;
410         }
411         else if(mode==1) {
412                 INIT_MINMAX2(min, max);
413                 
414                 for(efa= em->faces.first; efa; efa= efa->next) {
415                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
416
417                         if(uvedit_face_visible(scene, ima, efa, tf)) {
418                                 if(uvedit_uv_selected(scene, efa, tf, 0))                               { DO_MINMAX2(tf->uv[0], min, max);      change= 1;}
419                                 if(uvedit_uv_selected(scene, efa, tf, 1))                               { DO_MINMAX2(tf->uv[1], min, max);      change= 1;}
420                                 if(uvedit_uv_selected(scene, efa, tf, 2))                               { DO_MINMAX2(tf->uv[2], min, max);      change= 1;}
421                                 if(efa->v4 && (uvedit_uv_selected(scene, efa, tf, 3)))  { DO_MINMAX2(tf->uv[3], min, max);      change= 1;}
422                         }
423                 }
424         }
425         
426         if(change) {
427                 cent[0]= (min[0]+max[0])/2.0;
428                 cent[1]= (min[1]+max[1])/2.0;
429                 
430                 BKE_mesh_end_editmesh(obedit->data, em);
431                 return 1;
432         }
433
434         BKE_mesh_end_editmesh(obedit->data, em);
435         return 0;
436 }
437
438 /************************** find nearest ****************************/
439
440 typedef struct NearestHit {
441         EditFace *efa;
442         MTFace *tf;
443
444         int vert, uv;
445         int edge, vert2;
446 } NearestHit;
447
448 static void find_nearest_uv_edge(Scene *scene, Image *ima, EditMesh *em, float co[2], NearestHit *hit)
449 {
450         MTFace *tf;
451         EditFace *efa;
452         EditVert *eve;
453         float mindist, dist;
454         int i, nverts;
455
456         mindist= 1e10f;
457         memset(hit, 0, sizeof(*hit));
458
459         for(i=0, eve=em->verts.first; eve; eve=eve->next, i++)
460                 eve->tmp.l = i;
461         
462         for(efa= em->faces.first; efa; efa= efa->next) {
463                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
464
465                 if(uvedit_face_visible(scene, ima, efa, tf)) {
466                         nverts= efa->v4? 4: 3;
467
468                         for(i=0; i<nverts; i++) {
469                                 dist= PdistVL2Dfl(co, tf->uv[i], tf->uv[(i+1)%nverts]);
470
471                                 if(dist < mindist) {
472                                         hit->tf= tf;
473                                         hit->efa= efa;
474                                         hit->edge= i;
475                                         mindist= dist;
476
477                                         hit->vert= (*(&efa->v1 + i))->tmp.l;
478                                         hit->vert2= (*(&efa->v1 + ((i+1)%nverts)))->tmp.l;
479                                 }
480                         }
481                 }
482         }
483 }
484
485 static void find_nearest_uv_face(Scene *scene, Image *ima, EditMesh *em, float co[2], NearestHit *hit)
486 {
487         MTFace *tf;
488         EditFace *efa;
489         float mindist, dist, cent[2];
490         int i, nverts;
491
492         mindist= 1e10f;
493         memset(hit, 0, sizeof(*hit));
494         
495         for(efa= em->faces.first; efa; efa= efa->next) {
496                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
497
498                 if(uvedit_face_visible(scene, ima, efa, tf)) {
499                         nverts= efa->v4? 4: 3;
500                         cent[0]= cent[1]= 0.0f;
501
502                         for(i=0; i<nverts; i++) {
503                                 cent[0] += tf->uv[i][0];
504                                 cent[1] += tf->uv[i][1];
505                         }
506
507                         cent[0] /= nverts;
508                         cent[1] /= nverts;
509                         dist= fabs(co[0]- cent[0]) + fabs(co[1]- cent[1]);
510
511                         if(dist < mindist) {
512                                 hit->tf= tf;
513                                 hit->efa= efa;
514                                 mindist= dist;
515                         }
516                 }
517         }
518 }
519
520 static int nearest_uv_between(MTFace *tf, int nverts, int id, float co[2], float uv[2])
521 {
522         float m[3], v1[3], v2[3], c1, c2;
523         int id1, id2;
524
525         id1= (id+nverts-1)%nverts;
526         id2= (id+nverts+1)%nverts;
527
528         m[0]= co[0]-uv[0];
529         m[1]= co[1]-uv[1];
530         Vec2Subf(v1, tf->uv[id1], tf->uv[id]);
531         Vec2Subf(v2, tf->uv[id2], tf->uv[id]);
532
533         /* m and v2 on same side of v-v1? */
534         c1= v1[0]*m[1] - v1[1]*m[0];
535         c2= v1[0]*v2[1] - v1[1]*v2[0];
536
537         if(c1*c2 < 0.0f)
538                 return 0;
539
540         /* m and v1 on same side of v-v2? */
541         c1= v2[0]*m[1] - v2[1]*m[0];
542         c2= v2[0]*v1[1] - v2[1]*v1[0];
543
544         return (c1*c2 >= 0.0f);
545 }
546
547 static void find_nearest_uv_vert(Scene *scene, Image *ima, EditMesh *em, float co[2], float penalty[2], NearestHit *hit)
548 {
549         EditFace *efa;
550         EditVert *eve;
551         MTFace *tf;
552         float mindist, dist;
553         int i, nverts;
554
555         mindist= 1e10f;
556         memset(hit, 0, sizeof(*hit));
557         
558         for(i=0, eve=em->verts.first; eve; eve=eve->next, i++)
559                 eve->tmp.l = i;
560         
561         for(efa= em->faces.first; efa; efa= efa->next) {
562                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
563
564                 if(uvedit_face_visible(scene, ima, efa, tf)) {
565                         nverts= efa->v4? 4: 3;
566
567                         for(i=0; i<nverts; i++) {
568                                 if(penalty && uvedit_uv_selected(scene, efa, tf, i))
569                                         dist= fabs(co[0]-tf->uv[i][0])+penalty[0] + fabs(co[1]-tf->uv[i][1])+penalty[1];
570                                 else
571                                         dist= fabs(co[0]-tf->uv[i][0]) + fabs(co[1]-tf->uv[i][1]);
572
573                                 if(dist<=mindist) {
574                                         if(dist==mindist)
575                                                 if(!nearest_uv_between(tf, nverts, i, co, tf->uv[i]))
576                                                         continue;
577
578                                         mindist= dist;
579
580                                         hit->uv= i;
581                                         hit->tf= tf;
582                                         hit->efa= efa;
583
584                                         hit->vert= (*(&efa->v1 + i))->tmp.l;
585                                 }
586                         }
587                 }
588         }
589 }
590
591 int ED_uvedit_nearest_uv(Scene *scene, Object *obedit, Image *ima, float co[2], float uv[2])
592 {
593         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
594         EditFace *efa;
595         MTFace *tf;
596         float mindist, dist;
597         int i, nverts, found= 0;
598
599         mindist= 1e10f;
600         uv[0]= co[0];
601         uv[1]= co[1];
602         
603         for(efa= em->faces.first; efa; efa= efa->next) {
604                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
605
606                 if(uvedit_face_visible(scene, ima, efa, tf)) {
607                         nverts= efa->v4? 4: 3;
608
609                         for(i=0; i<nverts; i++) {
610                                 dist= fabs(co[0]-tf->uv[i][0]) + fabs(co[1]-tf->uv[i][1]);
611
612                                 if(dist<=mindist) {
613                                         mindist= dist;
614
615                                         uv[0]= tf->uv[i][0];
616                                         uv[1]= tf->uv[i][1];
617                                         found= 1;
618                                 }
619                         }
620                 }
621         }
622
623         BKE_mesh_end_editmesh(obedit->data, em);
624         return found;
625 }
626
627 /*********************** loop select ***********************/
628
629 static void uv_vertex_loop_flag(UvMapVert *first)
630 {
631         UvMapVert *iterv;
632         int count= 0;
633
634         for(iterv=first; iterv; iterv=iterv->next) {
635                 if(iterv->separate && iterv!=first)
636                         break;
637
638                 count++;
639         }
640         
641         if(count < 5)
642                 first->flag= 1;
643 }
644
645 static UvMapVert *uv_vertex_map_get(UvVertMap *vmap, EditFace *efa, int a)
646 {
647         UvMapVert *iterv, *first;
648         
649         first= EM_get_uv_map_vert(vmap, (*(&efa->v1 + a))->tmp.l);
650
651         for(iterv=first; iterv; iterv=iterv->next) {
652                 if(iterv->separate)
653                         first= iterv;
654                 if(iterv->f == efa->tmp.l)
655                         return first;
656         }
657         
658         return NULL;
659 }
660
661 static int uv_edge_tag_faces(UvMapVert *first1, UvMapVert *first2, int *totface)
662 {
663         UvMapVert *iterv1, *iterv2;
664         EditFace *efa;
665         int tot = 0;
666
667         /* count number of faces this edge has */
668         for(iterv1=first1; iterv1; iterv1=iterv1->next) {
669                 if(iterv1->separate && iterv1 != first1)
670                         break;
671
672                 for(iterv2=first2; iterv2; iterv2=iterv2->next) {
673                         if(iterv2->separate && iterv2 != first2)
674                                 break;
675
676                         if(iterv1->f == iterv2->f) {
677                                 /* if face already tagged, don't do this edge */
678                                 efa= EM_get_face_for_index(iterv1->f);
679                                 if(efa->f1)
680                                         return 0;
681
682                                 tot++;
683                                 break;
684                         }
685                 }
686         }
687
688         if(*totface == 0) /* start edge */
689                 *totface= tot;
690         else if(tot != *totface) /* check for same number of faces as start edge */
691                 return 0;
692
693         /* tag the faces */
694         for(iterv1=first1; iterv1; iterv1=iterv1->next) {
695                 if(iterv1->separate && iterv1 != first1)
696                         break;
697
698                 for(iterv2=first2; iterv2; iterv2=iterv2->next) {
699                         if(iterv2->separate && iterv2 != first2)
700                                 break;
701
702                         if(iterv1->f == iterv2->f) {
703                                 efa= EM_get_face_for_index(iterv1->f);
704                                 efa->f1= 1;
705                                 break;
706                         }
707                 }
708         }
709
710         return 1;
711 }
712
713 static int select_edgeloop(Scene *scene, Image *ima, EditMesh *em, NearestHit *hit, float limit[2], int extend)
714 {
715         EditVert *eve;
716         EditFace *efa;
717         MTFace *tf;
718         UvVertMap *vmap;
719         UvMapVert *iterv1, *iterv2;
720         int a, count, looking, nverts, starttotf, select;
721
722         /* setup */
723         EM_init_index_arrays(em, 0, 0, 1);
724         vmap= EM_make_uv_vert_map(em, 0, 0, limit);
725
726         for(count=0, eve=em->verts.first; eve; count++, eve= eve->next)
727                 eve->tmp.l = count;
728
729         for(count=0, efa= em->faces.first; efa; count++, efa= efa->next) {
730                 if(!extend) {
731                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
732                         uvedit_face_deselect(scene, efa, tf);
733                 }
734
735                 efa->tmp.l= count;
736                 efa->f1= 0;
737         }
738         
739         /* set flags for first face and verts */
740         nverts= (hit->efa->v4)? 4: 3;
741         iterv1= uv_vertex_map_get(vmap, hit->efa, hit->edge);
742         iterv2= uv_vertex_map_get(vmap, hit->efa, (hit->edge+1)%nverts);
743         uv_vertex_loop_flag(iterv1);
744         uv_vertex_loop_flag(iterv2);
745
746         starttotf= 0;
747         uv_edge_tag_faces(iterv1, iterv2, &starttotf);
748
749         /* sorry, first edge isnt even ok */
750         if(iterv1->flag==0 && iterv2->flag==0) looking= 0;
751         else looking= 1;
752
753         /* iterate */
754         while(looking) {
755                 looking= 0;
756
757                 /* find correct valence edges which are not tagged yet, but connect to tagged one */
758                 for(efa= em->faces.first; efa; efa=efa->next) {
759                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
760
761                         if(!efa->f1 && uvedit_face_visible(scene, ima, efa, tf)) {
762                                 nverts= (efa->v4)? 4: 3;
763                                 for(a=0; a<nverts; a++) {
764                                         /* check face not hidden and not tagged */
765                                         iterv1= uv_vertex_map_get(vmap, efa, a);
766                                         iterv2= uv_vertex_map_get(vmap, efa, (a+1)%nverts);
767
768                                         /* check if vertex is tagged and has right valence */
769                                         if(iterv1->flag || iterv2->flag) {
770                                                 if(uv_edge_tag_faces(iterv1, iterv2, &starttotf)) {
771                                                         looking= 1;
772                                                         efa->f1= 1;
773
774                                                         uv_vertex_loop_flag(iterv1);
775                                                         uv_vertex_loop_flag(iterv2);
776                                                         break;
777                                                 }
778                                         }
779                                 }
780                         }
781                 }
782         }
783
784         /* do the actual select/deselect */
785         nverts= (hit->efa->v4)? 4: 3;
786         iterv1= uv_vertex_map_get(vmap, hit->efa, hit->edge);
787         iterv2= uv_vertex_map_get(vmap, hit->efa, (hit->edge+1)%nverts);
788         iterv1->flag= 1;
789         iterv2->flag= 1;
790
791         if(extend) {
792                 tf= CustomData_em_get(&em->fdata, hit->efa->data, CD_MTFACE);
793
794                 if(uvedit_uv_selected(scene, hit->efa, tf, hit->edge) && uvedit_uv_selected(scene, hit->efa, tf, hit->edge))
795                         select= 0;
796                 else
797                         select= 1;
798         }
799         else
800                 select= 1;
801         
802         for(efa= em->faces.first; efa; efa=efa->next) {
803                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
804
805                 nverts= (efa->v4)? 4: 3;
806                 for(a=0; a<nverts; a++) {
807                         iterv1= uv_vertex_map_get(vmap, efa, a);
808
809                         if(iterv1->flag) {
810                                 if(select) uvedit_uv_select(scene, efa, tf, a);
811                                 else uvedit_uv_deselect(scene, efa, tf, a);
812                         }
813                 }
814         }
815
816         /* cleanup */
817         EM_free_uv_vert_map(vmap);
818         EM_free_index_arrays();
819
820         return (select)? 1: -1;
821 }
822
823 /*********************** linked select ***********************/
824
825 static void select_linked(Scene *scene, Image *ima, EditMesh *em, float limit[2], NearestHit *hit, int extend)
826 {
827         EditFace *efa;
828         MTFace *tf;
829         UvVertMap *vmap;
830         UvMapVert *vlist, *iterv, *startv;
831         int a, i, nverts, j, stacksize= 0, *stack;
832         char *flag;
833
834         vmap= EM_make_uv_vert_map(em, 1, 1, limit);
835         if(vmap == NULL)
836                 return;
837
838         stack= MEM_mallocN(sizeof(*stack)* BLI_countlist(&em->faces), "UvLinkStack");
839         flag= MEM_callocN(sizeof(*flag)*BLI_countlist(&em->faces), "UvLinkFlag");
840
841         if(!hit) {
842                 for(a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
843                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
844
845                         if(uvedit_face_visible(scene, ima, efa, tf)) {
846                                 if(tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)) {
847                                         stack[stacksize]= a;
848                                         stacksize++;
849                                         flag[a]= 1;
850                                 }
851                         }
852                 }
853         }
854         else {
855                 for(a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
856                         if(efa == hit->efa) {
857                                 stack[stacksize]= a;
858                                 stacksize++;
859                                 flag[a]= 1;
860                                 break;
861                         }
862                 }
863         }
864
865         while(stacksize > 0) {
866                 stacksize--;
867                 a= stack[stacksize];
868                 
869                 for(j=0, efa= em->faces.first; efa; efa= efa->next, j++)
870                         if(j==a)
871                                 break;
872
873                 nverts= efa->v4? 4: 3;
874
875                 for(i=0; i<nverts; i++) {
876                         /* make_uv_vert_map_EM sets verts tmp.l to the indicies */
877                         vlist= EM_get_uv_map_vert(vmap, (*(&efa->v1 + i))->tmp.l);
878                         
879                         startv= vlist;
880
881                         for(iterv=vlist; iterv; iterv=iterv->next) {
882                                 if(iterv->separate)
883                                         startv= iterv;
884                                 if(iterv->f == a)
885                                         break;
886                         }
887
888                         for(iterv=startv; iterv; iterv=iterv->next) {
889                                 if((startv != iterv) && (iterv->separate))
890                                         break;
891                                 else if(!flag[iterv->f]) {
892                                         flag[iterv->f]= 1;
893                                         stack[stacksize]= iterv->f;;
894                                         stacksize++;
895                                 }
896                         }
897                 }
898         }
899
900         if(!extend || hit) {
901                 for(a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
902                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
903                         if(flag[a])
904                                 tf->flag |= (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
905                         else
906                                 tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
907                 }
908         }
909         else if(extend && hit) {
910                 for(a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
911                         if(flag[a]) {
912                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
913                                 if(efa->v4) {
914                                         if((tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)))
915                                                 break;
916                                 }
917                                 else if(tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
918                                         break;
919                         }
920                 }
921
922                 if(efa) {
923                         for(a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
924                                 if(flag[a]) {
925                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
926                                         tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
927                                 }
928                         }
929                 }
930                 else {
931                         for(a=0, efa= em->faces.first; efa; efa= efa->next, a++) {
932                                 if(flag[a]) {
933                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
934                                         tf->flag |= (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
935                                 }
936                         }
937                 }
938         }
939         
940         MEM_freeN(stack);
941         MEM_freeN(flag);
942         EM_free_uv_vert_map(vmap);
943 }
944
945 /* ******************** mirror operator **************** */
946
947 static int mirror_exec(bContext *C, wmOperator *op)
948 {
949         float mat[3][3];
950         int axis;
951         
952         Mat3One(mat);
953         axis= RNA_enum_get(op->ptr, "axis");
954
955         if(axis == 'x') {
956                 /* XXX initTransform(TFM_MIRROR, CTX_NO_PET|CTX_AUTOCONFIRM);
957                 BIF_setSingleAxisConstraint(mat[0], " on X axis");
958                 Transform(); */
959         }
960         else {
961                 /* XXX initTransform(TFM_MIRROR, CTX_NO_PET|CTX_AUTOCONFIRM);
962                 BIF_setSingleAxisConstraint(mat[1], " on Y axis");
963                 Transform(); */
964         }
965
966         return OPERATOR_FINISHED;
967 }
968
969 void UV_OT_mirror(wmOperatorType *ot)
970 {
971         static EnumPropertyItem axis_items[] = {
972                 {'x', "MIRROR_X", 0, "Mirror X", "Mirror UVs over X axis."},
973                 {'y', "MIRROR_Y", 0, "Mirror Y", "Mirror UVs over Y axis."},
974                 {0, NULL, 0, NULL, NULL}};
975
976         /* identifiers */
977         ot->name= "Mirror";
978         ot->idname= "UV_OT_mirror";
979         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
980         
981         /* api callbacks */
982         ot->exec= mirror_exec;
983         ot->poll= ED_operator_uvedit;
984
985         /* properties */
986         RNA_def_enum(ot->srna, "axis", axis_items, 'x', "Axis", "Axis to mirror UV locations over.");
987 }
988
989 /* ******************** align operator **************** */
990
991 static void weld_align_uv(bContext *C, int tool)
992 {
993         Scene *scene;
994         Object *obedit;
995         Image *ima;
996         EditMesh *em;
997         EditFace *efa;
998         MTFace *tf;
999         float cent[2], min[2], max[2];
1000         
1001         scene= CTX_data_scene(C);
1002         obedit= CTX_data_edit_object(C);
1003         em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
1004         ima= CTX_data_edit_image(C);
1005
1006         INIT_MINMAX2(min, max);
1007
1008         if(tool == 'a') {
1009                 for(efa= em->faces.first; efa; efa= efa->next) {
1010                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1011
1012                         if(uvedit_face_visible(scene, ima, efa, tf)) {
1013                                 if(uvedit_uv_selected(scene, efa, tf, 0))
1014                                         DO_MINMAX2(tf->uv[0], min, max)
1015                                 if(uvedit_uv_selected(scene, efa, tf, 1))
1016                                         DO_MINMAX2(tf->uv[1], min, max)
1017                                 if(uvedit_uv_selected(scene, efa, tf, 2))
1018                                         DO_MINMAX2(tf->uv[2], min, max)
1019                                 if(efa->v4 && uvedit_uv_selected(scene, efa, tf, 3))
1020                                         DO_MINMAX2(tf->uv[3], min, max)
1021                         }
1022                 }
1023
1024                 tool= (max[0]-min[0] >= max[1]-min[1])? 'y': 'x';
1025         }
1026
1027         uvedit_center(scene, ima, obedit, cent, 0);
1028
1029         if(tool == 'x' || tool == 'w') {
1030                 for(efa= em->faces.first; efa; efa= efa->next) {
1031                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1032                         if(uvedit_face_visible(scene, ima, efa, tf)) {
1033                                 if(uvedit_uv_selected(scene, efa, tf, 0))
1034                                         tf->uv[0][0]= cent[0];
1035                                 if(uvedit_uv_selected(scene, efa, tf, 1))
1036                                         tf->uv[1][0]= cent[0];
1037                                 if(uvedit_uv_selected(scene, efa, tf, 2))
1038                                         tf->uv[2][0]= cent[0];
1039                                 if(efa->v4 && uvedit_uv_selected(scene, efa, tf, 3))
1040                                         tf->uv[3][0]= cent[0];
1041                         }
1042                 }
1043         }
1044
1045         if(tool == 'y' || tool == 'w') {
1046                 for(efa= em->faces.first; efa; efa= efa->next) {
1047                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1048                         if(uvedit_face_visible(scene, ima, efa, tf)) {
1049                                 if(uvedit_uv_selected(scene, efa, tf, 0))
1050                                         tf->uv[0][1]= cent[1];
1051                                 if(uvedit_uv_selected(scene, efa, tf, 1))
1052                                         tf->uv[1][1]= cent[1];
1053                                 if(uvedit_uv_selected(scene, efa, tf, 2))
1054                                         tf->uv[2][1]= cent[1];
1055                                 if(efa->v4 && uvedit_uv_selected(scene, efa, tf, 3))
1056                                         tf->uv[3][1]= cent[1];
1057                         }
1058                 }
1059         }
1060
1061         DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
1062         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, obedit);
1063
1064         BKE_mesh_end_editmesh(obedit->data, em);
1065 }
1066
1067 static int align_exec(bContext *C, wmOperator *op)
1068 {
1069         weld_align_uv(C, RNA_enum_get(op->ptr, "axis"));
1070
1071         return OPERATOR_FINISHED;
1072 }
1073
1074 void UV_OT_align(wmOperatorType *ot)
1075 {
1076         static EnumPropertyItem axis_items[] = {
1077                 {'a', "ALIGN_AUTO", 0, "Align Auto", "Automatically choose the axis on which there is most alignment already."},
1078                 {'x', "ALIGN_X", 0, "Align X", "Align UVs on X axis."},
1079                 {'y', "ALIGN_Y", 0, "Align Y", "Align UVs on Y axis."},
1080                 {0, NULL, 0, NULL, NULL}};
1081
1082         /* identifiers */
1083         ot->name= "Align";
1084         ot->idname= "UV_OT_align";
1085         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1086         
1087         /* api callbacks */
1088         ot->exec= align_exec;
1089         ot->poll= ED_operator_uvedit;
1090
1091         /* properties */
1092         RNA_def_enum(ot->srna, "axis", axis_items, 'a', "Axis", "Axis to align UV locations on.");
1093 }
1094
1095 /* ******************** weld operator **************** */
1096
1097 static int weld_exec(bContext *C, wmOperator *op)
1098 {
1099         weld_align_uv(C, 'w');
1100
1101         return OPERATOR_FINISHED;
1102 }
1103
1104 void UV_OT_weld(wmOperatorType *ot)
1105 {
1106         /* identifiers */
1107         ot->name= "Weld";
1108         ot->idname= "UV_OT_weld";
1109         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1110         
1111         /* api callbacks */
1112         ot->exec= weld_exec;
1113         ot->poll= ED_operator_uvedit;
1114 }
1115
1116 /* ******************** stitch operator **************** */
1117
1118 /* just for averaging UVs */
1119 typedef struct UVVertAverage {
1120         float uv[2];
1121         int count;
1122 } UVVertAverage;
1123
1124 static int stitch_exec(bContext *C, wmOperator *op)
1125 {
1126         SpaceImage *sima;
1127         Scene *scene;
1128         Object *obedit;
1129         EditMesh *em;
1130         EditFace *efa;
1131         EditVert *eve;
1132         Image *ima;
1133         MTFace *tf;
1134         
1135         sima= (SpaceImage*)CTX_wm_space_data(C);
1136         scene= CTX_data_scene(C);
1137         obedit= CTX_data_edit_object(C);
1138         em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
1139         ima= CTX_data_edit_image(C);
1140         
1141         if(RNA_boolean_get(op->ptr, "use_limit")) {
1142                 UvVertMap *vmap;
1143                 UvMapVert *vlist, *iterv;
1144                 float newuv[2], limit[2], pixels;
1145                 int a, vtot;
1146
1147                 pixels= RNA_float_get(op->ptr, "limit");
1148                 uvedit_pixel_to_float(sima, limit, pixels);
1149
1150                 EM_init_index_arrays(em, 0, 0, 1);
1151                 vmap= EM_make_uv_vert_map(em, 1, 0, limit);
1152
1153                 if(vmap == NULL) {
1154                         BKE_mesh_end_editmesh(obedit->data, em);
1155                         return OPERATOR_CANCELLED;
1156                 }
1157
1158                 for(a=0, eve= em->verts.first; eve; a++, eve= eve->next) {
1159                         vlist= EM_get_uv_map_vert(vmap, a);
1160
1161                         while(vlist) {
1162                                 newuv[0]= 0; newuv[1]= 0;
1163                                 vtot= 0;
1164
1165                                 for(iterv=vlist; iterv; iterv=iterv->next) {
1166                                         if((iterv != vlist) && iterv->separate)
1167                                                 break;
1168
1169                                         efa = EM_get_face_for_index(iterv->f);
1170                                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1171                                         
1172                                         if(uvedit_uv_selected(scene, efa, tf, iterv->tfindex)) {
1173                                                 newuv[0] += tf->uv[iterv->tfindex][0];
1174                                                 newuv[1] += tf->uv[iterv->tfindex][1];
1175                                                 vtot++;
1176                                         }
1177                                 }
1178
1179                                 if(vtot > 1) {
1180                                         newuv[0] /= vtot; newuv[1] /= vtot;
1181
1182                                         for(iterv=vlist; iterv; iterv=iterv->next) {
1183                                                 if((iterv != vlist) && iterv->separate)
1184                                                         break;
1185
1186                                                 efa = EM_get_face_for_index(iterv->f);
1187                                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1188
1189                                                 if(uvedit_uv_selected(scene, efa, tf, iterv->tfindex)) {
1190                                                         tf->uv[iterv->tfindex][0]= newuv[0];
1191                                                         tf->uv[iterv->tfindex][1]= newuv[1];
1192                                                 }
1193                                         }
1194                                 }
1195
1196                                 vlist= iterv;
1197                         }
1198                 }
1199
1200                 EM_free_uv_vert_map(vmap);
1201                 EM_free_index_arrays();
1202         }
1203         else {
1204                 UVVertAverage *uv_average, *uvav;
1205                 int count;
1206
1207                 // index and count verts
1208                 for(count=0, eve=em->verts.first; eve; count++, eve= eve->next)
1209                         eve->tmp.l = count;
1210                 
1211                 uv_average= MEM_callocN(sizeof(UVVertAverage)*count, "Stitch");
1212                 
1213                 // gather uv averages per vert
1214                 for(efa= em->faces.first; efa; efa= efa->next) {
1215                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1216
1217                         if(uvedit_face_visible(scene, ima, efa, tf)) {
1218                                 if(uvedit_uv_selected(scene, efa, tf, 0)) {
1219                                         uvav = uv_average + efa->v1->tmp.l;
1220                                         uvav->count++;
1221                                         uvav->uv[0] += tf->uv[0][0];
1222                                         uvav->uv[1] += tf->uv[0][1];
1223                                 }
1224
1225                                 if(uvedit_uv_selected(scene, efa, tf, 1)) {
1226                                         uvav = uv_average + efa->v2->tmp.l;
1227                                         uvav->count++;
1228                                         uvav->uv[0] += tf->uv[1][0];
1229                                         uvav->uv[1] += tf->uv[1][1];
1230                                 }
1231
1232                                 if(uvedit_uv_selected(scene, efa, tf, 2)) {
1233                                         uvav = uv_average + efa->v3->tmp.l;
1234                                         uvav->count++;
1235                                         uvav->uv[0] += tf->uv[2][0];
1236                                         uvav->uv[1] += tf->uv[2][1];
1237                                 }
1238
1239                                 if(efa->v4 && uvedit_uv_selected(scene, efa, tf, 3)) {
1240                                         uvav = uv_average + efa->v4->tmp.l;
1241                                         uvav->count++;
1242                                         uvav->uv[0] += tf->uv[3][0];
1243                                         uvav->uv[1] += tf->uv[3][1];
1244                                 }
1245                         }
1246                 }
1247                 
1248                 // apply uv welding
1249                 for(efa= em->faces.first; efa; efa= efa->next) {
1250                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1251
1252                         if(uvedit_face_visible(scene, ima, efa, tf)) {
1253                                 if(uvedit_uv_selected(scene, efa, tf, 0)) {
1254                                         uvav = uv_average + efa->v1->tmp.l;
1255                                         tf->uv[0][0] = uvav->uv[0]/uvav->count;
1256                                         tf->uv[0][1] = uvav->uv[1]/uvav->count;
1257                                 }
1258
1259                                 if(uvedit_uv_selected(scene, efa, tf, 1)) {
1260                                         uvav = uv_average + efa->v2->tmp.l;
1261                                         tf->uv[1][0] = uvav->uv[0]/uvav->count;
1262                                         tf->uv[1][1] = uvav->uv[1]/uvav->count;
1263                                 }
1264
1265                                 if(uvedit_uv_selected(scene, efa, tf, 2)) {
1266                                         uvav = uv_average + efa->v3->tmp.l;
1267                                         tf->uv[2][0] = uvav->uv[0]/uvav->count;
1268                                         tf->uv[2][1] = uvav->uv[1]/uvav->count;
1269                                 }
1270
1271                                 if(efa->v4 && uvedit_uv_selected(scene, efa, tf, 3)) {
1272                                         uvav = uv_average + efa->v4->tmp.l;
1273                                         tf->uv[3][0] = uvav->uv[0]/uvav->count;
1274                                         tf->uv[3][1] = uvav->uv[1]/uvav->count;
1275                                 }
1276                         }
1277                 }
1278
1279                 MEM_freeN(uv_average);
1280         }
1281
1282         DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
1283         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, obedit);
1284
1285         BKE_mesh_end_editmesh(obedit->data, em);
1286         return OPERATOR_FINISHED;
1287 }
1288
1289 void UV_OT_stitch(wmOperatorType *ot)
1290 {
1291         /* identifiers */
1292         ot->name= "Stitch";
1293         ot->idname= "UV_OT_stitch";
1294         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1295         
1296         /* api callbacks */
1297         ot->exec= stitch_exec;
1298         ot->poll= ED_operator_uvedit;
1299
1300         /* properties */
1301         RNA_def_boolean(ot->srna, "use_limit", 1, "Use Limit", "Stitch UVs within a specified limit distance.");
1302         RNA_def_float(ot->srna, "limit", 20.0, 0.0f, FLT_MAX, "Limit", "Limit distance in image pixels.", -FLT_MAX, FLT_MAX);
1303 }
1304
1305 /* ******************** (de)select all operator **************** */
1306
1307 static int select_inverse_exec(bContext *C, wmOperator *op)
1308 {
1309         Scene *scene;
1310         ToolSettings *ts;
1311         Object *obedit;
1312         EditMesh *em;
1313         EditFace *efa;
1314         Image *ima;
1315         MTFace *tf;
1316         
1317         scene= CTX_data_scene(C);
1318         ts= CTX_data_tool_settings(C);
1319         obedit= CTX_data_edit_object(C);
1320         em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
1321         ima= CTX_data_edit_image(C);
1322
1323         if(ts->uv_flag & UV_SYNC_SELECTION) {
1324                 EM_select_swap(em);
1325         }
1326         else {
1327                 for(efa= em->faces.first; efa; efa= efa->next) {
1328                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1329
1330                         if(uvedit_face_visible(scene, ima, efa, tf)) {
1331                                 tf->flag ^= TF_SEL1;
1332                                 tf->flag ^= TF_SEL2;
1333                                 tf->flag ^= TF_SEL3;
1334                                 if(efa->v4) tf->flag ^= TF_SEL4;
1335                         }
1336                 }
1337         }
1338
1339         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
1340
1341         BKE_mesh_end_editmesh(obedit->data, em);
1342         return OPERATOR_FINISHED;
1343 }
1344
1345 void UV_OT_select_inverse(wmOperatorType *ot)
1346 {
1347         /* identifiers */
1348         ot->name= "Select Inverse";
1349         ot->idname= "UV_OT_select_inverse";
1350         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1351         
1352         /* api callbacks */
1353         ot->exec= select_inverse_exec;
1354         ot->poll= ED_operator_uvedit;
1355 }
1356
1357 /* ******************** (de)select all operator **************** */
1358
1359 static int de_select_all_exec(bContext *C, wmOperator *op)
1360 {
1361         Scene *scene;
1362         ToolSettings *ts;
1363         Object *obedit;
1364         EditMesh *em;
1365         EditFace *efa;
1366         Image *ima;
1367         MTFace *tf;
1368         int sel;
1369         
1370         scene= CTX_data_scene(C);
1371         ts= CTX_data_tool_settings(C);
1372         obedit= CTX_data_edit_object(C);
1373         em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
1374         ima= CTX_data_edit_image(C);
1375         
1376         if(ts->uv_flag & UV_SYNC_SELECTION) {
1377                 EDBM_toggle_select_all(((Mesh*)obedit->data)->edit_btmesh);
1378         }
1379         else {
1380                 sel= 0;
1381
1382                 for(efa= em->faces.first; efa; efa= efa->next) {
1383                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1384
1385                         if(uvedit_face_visible(scene, ima, efa, tf)) {
1386                                 if(tf->flag & (TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4)) {
1387                                         sel= 1;
1388                                         break;
1389                                 }
1390                         }
1391                 }
1392         
1393                 for(efa= em->faces.first; efa; efa= efa->next) {
1394                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1395
1396                         if(uvedit_face_visible(scene, ima, efa, tf)) {
1397                                 if(efa->v4) {
1398                                         if(sel) tf->flag &= ~(TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4);
1399                                         else tf->flag |= (TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4);
1400                                 }
1401                                 else {
1402                                         if(sel) tf->flag &= ~(TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4);
1403                                         else tf->flag |= (TF_SEL1+TF_SEL2+TF_SEL3);
1404                                 }
1405                         }
1406                 }
1407         }
1408
1409         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
1410
1411         BKE_mesh_end_editmesh(obedit->data, em);
1412         return OPERATOR_FINISHED;
1413 }
1414
1415 void UV_OT_select_all_toggle(wmOperatorType *ot)
1416 {
1417         /* identifiers */
1418         ot->name= "Select or Deselect All";
1419         ot->idname= "UV_OT_select_all_toggle";
1420         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1421         
1422         /* api callbacks */
1423         ot->exec= de_select_all_exec;
1424         ot->poll= ED_operator_uvedit;
1425 }
1426
1427 /* ******************** mouse select operator **************** */
1428
1429 static int sticky_select(float *limit, int hitv[4], int v, float *hituv[4], float *uv, int sticky)
1430 {
1431         int i;
1432
1433         /* this function test if some vertex needs to selected
1434          * in addition to the existing ones due to sticky select */
1435         if(sticky == SI_STICKY_DISABLE)
1436                 return 0;
1437
1438         for(i=0; i<4; i++) {
1439                 if(hitv[i] == v) {
1440                         if(sticky == SI_STICKY_LOC) {
1441                                 if(fabs(hituv[i][0]-uv[0]) < limit[0] && fabs(hituv[i][1]-uv[1]) < limit[1])
1442                                         return 1;
1443                         }
1444                         else if(sticky == SI_STICKY_VERTEX)
1445                                 return 1;
1446                 }
1447         }
1448
1449         return 0;
1450 }
1451
1452 static int mouse_select(bContext *C, float co[2], int extend, int loop)
1453 {
1454         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1455         Scene *scene= CTX_data_scene(C);
1456         ToolSettings *ts= CTX_data_tool_settings(C);
1457         Object *obedit= CTX_data_edit_object(C);
1458         Image *ima= CTX_data_edit_image(C);
1459         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
1460         EditFace *efa;
1461         MTFace *tf;
1462         NearestHit hit;
1463         int a, i, select = 1, selectmode, sticky, sync, hitv[4], nvert;
1464         int flush = 0; /* 0 == dont flush, 1 == sel, -1 == desel;  only use when selection sync is enabled */
1465         float limit[2], *hituv[4], penalty[2];
1466         
1467         uvedit_pixel_to_float(sima, limit, 0.05f);
1468         uvedit_pixel_to_float(sima, penalty, 5.0f);
1469
1470         /* retrieve operation mode */
1471         if(ts->uv_flag & UV_SYNC_SELECTION) {
1472                 sync= 1;
1473
1474                 if(ts->selectmode & SCE_SELECT_FACE)
1475                         selectmode= UV_SELECT_FACE;
1476                 else if(ts->selectmode & SCE_SELECT_EDGE)
1477                         selectmode= UV_SELECT_EDGE;
1478                 else
1479                         selectmode= UV_SELECT_VERTEX;
1480
1481                 sticky= SI_STICKY_DISABLE;
1482         }
1483         else {
1484                 sync= 0;
1485                 selectmode= ts->uv_selectmode;
1486                 sticky= sima->sticky;
1487         }
1488
1489         /* find nearest element */
1490         if(loop) {
1491                 /* find edge */
1492                 find_nearest_uv_edge(scene, ima, em, co, &hit);
1493                 if(hit.efa == NULL) {
1494                         BKE_mesh_end_editmesh(obedit->data, em);
1495                         return OPERATOR_CANCELLED;
1496                 }
1497         }
1498         else if(selectmode == UV_SELECT_VERTEX) {
1499                 /* find vertex */
1500                 find_nearest_uv_vert(scene, ima, em, co, penalty, &hit);
1501                 if(hit.efa == NULL) {
1502                         BKE_mesh_end_editmesh(obedit->data, em);
1503                         return OPERATOR_CANCELLED;
1504                 }
1505
1506                 /* mark 1 vertex as being hit */
1507                 for(i=0; i<4; i++)
1508                         hitv[i]= 0xFFFFFFFF;
1509
1510                 hitv[hit.uv]= hit.vert;
1511                 hituv[hit.uv]= hit.tf->uv[hit.uv];
1512         }
1513         else if(selectmode == UV_SELECT_EDGE) {
1514                 /* find edge */
1515                 find_nearest_uv_edge(scene, ima, em, co, &hit);
1516                 if(hit.efa == NULL) {
1517                         BKE_mesh_end_editmesh(obedit->data, em);
1518                         return OPERATOR_CANCELLED;
1519                 }
1520
1521                 /* mark 2 edge vertices as being hit */
1522                 for(i=0; i<4; i++)
1523                         hitv[i]= 0xFFFFFFFF;
1524
1525                 nvert= (hit.efa->v4)? 4: 3;
1526
1527                 hitv[hit.edge]= hit.vert;
1528                 hitv[(hit.edge+1)%nvert]= hit.vert2;
1529                 hituv[hit.edge]= hit.tf->uv[hit.edge];
1530                 hituv[(hit.edge+1)%nvert]= hit.tf->uv[(hit.edge+1)%nvert];
1531         }
1532         else if(selectmode == UV_SELECT_FACE) {
1533                 /* find face */
1534                 find_nearest_uv_face(scene, ima, em, co, &hit);
1535                 if(hit.efa == NULL) {
1536                         BKE_mesh_end_editmesh(obedit->data, em);
1537                         return OPERATOR_CANCELLED;
1538                 }
1539                 
1540                 /* make active */
1541                 EM_set_actFace(em, hit.efa);
1542
1543                 /* mark all face vertices as being hit */
1544                 for(i=0; i<4; i++)
1545                         hituv[i]= hit.tf->uv[i];
1546
1547                 hitv[0]= hit.efa->v1->tmp.l;
1548                 hitv[1]= hit.efa->v2->tmp.l;
1549                 hitv[2]= hit.efa->v3->tmp.l;
1550                 
1551                 if(hit.efa->v4) hitv[3]= hit.efa->v4->tmp.l;
1552                 else hitv[3]= 0xFFFFFFFF;
1553         }
1554         else if(selectmode == UV_SELECT_ISLAND) {
1555                 find_nearest_uv_vert(scene, ima, em, co, NULL, &hit);
1556
1557                 if(hit.efa==NULL) {
1558                         BKE_mesh_end_editmesh(obedit->data, em);
1559                         return OPERATOR_CANCELLED;
1560                 }
1561         }
1562         else {
1563                 BKE_mesh_end_editmesh(obedit->data, em);
1564                 return OPERATOR_CANCELLED;
1565         }
1566
1567         /* do selection */
1568         if(loop) {
1569                 flush= select_edgeloop(scene, ima, em, &hit, limit, extend);
1570         }
1571         else if(selectmode == UV_SELECT_ISLAND) {
1572                 select_linked(scene, ima, em, limit, &hit, extend);
1573         }
1574         else if(extend) {
1575                 if(selectmode == UV_SELECT_VERTEX) {
1576                         /* (de)select uv vertex */
1577                         if(uvedit_uv_selected(scene, hit.efa, hit.tf, hit.uv)) {
1578                                 uvedit_uv_deselect(scene, hit.efa, hit.tf, hit.uv);
1579                                 select= 0;
1580                         }
1581                         else {
1582                                 uvedit_uv_select(scene, hit.efa, hit.tf, hit.uv);
1583                                 select= 1;
1584                         }
1585                         flush = 1;
1586                 }
1587                 else if(selectmode == UV_SELECT_EDGE) {
1588                         /* (de)select edge */
1589                         if(uvedit_edge_selected(scene, hit.efa, hit.tf, hit.edge)) {
1590                                 uvedit_edge_deselect(scene, hit.efa, hit.tf, hit.edge);
1591                                 select= 0;
1592                         }
1593                         else {
1594                                 uvedit_edge_select(scene, hit.efa, hit.tf, hit.edge);
1595                                 select= 1;
1596                         }
1597                         flush = 1;
1598                 }
1599                 else if(selectmode == UV_SELECT_FACE) {
1600                         /* (de)select face */
1601                         if(uvedit_face_selected(scene, hit.efa, hit.tf)) {
1602                                 uvedit_face_deselect(scene, hit.efa, hit.tf);
1603                                 select= 0;
1604                         }
1605                         else {
1606                                 uvedit_face_select(scene, hit.efa, hit.tf);
1607                                 select= 1;
1608                         }
1609                         flush = -1;
1610                 }
1611
1612                 /* (de)select sticky uv nodes */
1613                 if(sticky != SI_STICKY_DISABLE) {
1614                         EditVert *ev;
1615                         
1616                         for(a=0, ev=em->verts.first; ev; ev = ev->next, a++)
1617                                 ev->tmp.l = a;
1618                         
1619                         /* deselect */
1620                         if(select==0) {
1621                                 for(efa= em->faces.first; efa; efa= efa->next) {
1622                                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1623
1624                                         if(uvedit_face_visible(scene, ima, efa, tf)) {
1625                                                 if(sticky_select(limit, hitv, efa->v1->tmp.l, hituv, tf->uv[0], sticky))
1626                                                         uvedit_uv_deselect(scene, efa, tf, 0);
1627                                                 if(sticky_select(limit, hitv, efa->v2->tmp.l, hituv, tf->uv[1], sticky))
1628                                                         uvedit_uv_deselect(scene, efa, tf, 1);
1629                                                 if(sticky_select(limit, hitv, efa->v3->tmp.l, hituv, tf->uv[2], sticky))
1630                                                         uvedit_uv_deselect(scene, efa, tf, 2);
1631                                                 if(efa->v4)
1632                                                         if(sticky_select(limit, hitv, efa->v4->tmp.l, hituv, tf->uv[3], sticky))
1633                                                                 uvedit_uv_deselect(scene, efa, tf, 3);
1634                                         }
1635                                 }
1636                                 flush = -1;
1637                         }
1638                         /* select */
1639                         else {
1640                                 for(efa= em->faces.first; efa; efa= efa->next) {
1641                                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1642
1643                                         if(uvedit_face_visible(scene, ima, efa, tf)) {
1644                                                 if(sticky_select(limit, hitv, efa->v1->tmp.l, hituv, tf->uv[0], sticky))
1645                                                         uvedit_uv_select(scene, efa, tf, 0);
1646                                                 if(sticky_select(limit, hitv, efa->v2->tmp.l, hituv, tf->uv[1], sticky))
1647                                                         uvedit_uv_select(scene, efa, tf, 1);
1648                                                 if(sticky_select(limit, hitv, efa->v3->tmp.l, hituv, tf->uv[2], sticky))
1649                                                         uvedit_uv_select(scene, efa, tf, 2);
1650                                                 if(efa->v4)
1651                                                         if(sticky_select(limit, hitv, efa->v4->tmp.l, hituv, tf->uv[3], sticky))
1652                                                                 uvedit_uv_select(scene, efa, tf, 3);
1653                                         }
1654                                 }
1655                                 
1656                                 flush = 1;
1657                         }                       
1658                 }
1659         }
1660         else {
1661                 /* deselect all */
1662                 for(efa= em->faces.first; efa; efa= efa->next) {
1663                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1664                         uvedit_face_deselect(scene, efa, tf);
1665                 }
1666
1667                 if(selectmode == UV_SELECT_VERTEX) {
1668                         /* select vertex */
1669                         uvedit_uv_select(scene, hit.efa, hit.tf, hit.uv);
1670                         flush= 1;
1671                 }
1672                 else if(selectmode == UV_SELECT_EDGE) {
1673                         /* select edge */
1674                         uvedit_edge_select(scene, hit.efa, hit.tf, hit.edge);
1675                         flush= 1;
1676                 }
1677                 else if(selectmode == UV_SELECT_FACE) {
1678                         /* select face */
1679                         uvedit_face_select(scene, hit.efa, hit.tf);
1680                 }
1681
1682                 /* select sticky uvs */
1683                 if(sticky != SI_STICKY_DISABLE) {
1684                         for(efa= em->faces.first; efa; efa= efa->next) {
1685                                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1686                                 if(uvedit_face_visible(scene, ima, efa, tf)) {
1687                                         if(sticky == SI_STICKY_DISABLE) continue;
1688
1689                                         if(sticky_select(limit, hitv, efa->v1->tmp.l, hituv, tf->uv[0], sticky))
1690                                                 uvedit_uv_select(scene, efa, tf, 0);
1691                                         if(sticky_select(limit, hitv, efa->v2->tmp.l, hituv, tf->uv[1], sticky))
1692                                                 uvedit_uv_select(scene, efa, tf, 1);
1693                                         if(sticky_select(limit, hitv, efa->v3->tmp.l, hituv, tf->uv[2], sticky))
1694                                                 uvedit_uv_select(scene, efa, tf, 2);
1695                                         if(efa->v4)
1696                                                 if(sticky_select(limit, hitv, efa->v4->tmp.l, hituv, tf->uv[3], sticky))
1697                                                         uvedit_uv_select(scene, efa, tf, 3);
1698
1699                                         flush= 1;
1700                                 }
1701                         }
1702                 }
1703         }
1704         
1705         if(sync) {
1706                 /* flush for mesh selection */
1707                 if(ts->selectmode != SCE_SELECT_FACE) {
1708                         if(flush==1)            EM_select_flush(em);
1709                         else if(flush==-1)      EM_deselect_flush(em);
1710                 }
1711         }
1712         
1713         DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
1714         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
1715         
1716         BKE_mesh_end_editmesh(obedit->data, em);
1717         return OPERATOR_PASS_THROUGH|OPERATOR_FINISHED;
1718 }
1719
1720 static int select_exec(bContext *C, wmOperator *op)
1721 {
1722         float co[2];
1723         int extend, loop;
1724
1725         RNA_float_get_array(op->ptr, "location", co);
1726         extend= RNA_boolean_get(op->ptr, "extend");
1727         loop= 0;
1728
1729         return mouse_select(C, co, extend, loop);
1730 }
1731
1732 static int select_invoke(bContext *C, wmOperator *op, wmEvent *event)
1733 {
1734         ARegion *ar= CTX_wm_region(C);
1735         float co[2];
1736         int x, y;
1737
1738         x= event->x - ar->winrct.xmin;
1739         y= event->y - ar->winrct.ymin;
1740
1741         UI_view2d_region_to_view(&ar->v2d, x, y, &co[0], &co[1]);
1742         RNA_float_set_array(op->ptr, "location", co);
1743
1744         return select_exec(C, op);
1745 }
1746
1747 void UV_OT_select(wmOperatorType *ot)
1748 {
1749         /* identifiers */
1750         ot->name= "Select";
1751         ot->idname= "UV_OT_select";
1752         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1753         
1754         /* api callbacks */
1755         ot->exec= select_exec;
1756         ot->invoke= select_invoke;
1757         ot->poll= ED_operator_uvedit;
1758
1759         /* properties */
1760         RNA_def_boolean(ot->srna, "extend", 0,
1761                 "Extend", "Extend selection rather than clearing the existing selection.");
1762         RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX,
1763                 "Location", "Mouse location in normalized coordinates, 0.0 to 1.0 is within the image bounds.", -100.0f, 100.0f);
1764 }
1765
1766 /* ******************** loop select operator **************** */
1767
1768 static int select_loop_exec(bContext *C, wmOperator *op)
1769 {
1770         float co[2];
1771         int extend, loop;
1772
1773         RNA_float_get_array(op->ptr, "location", co);
1774         extend= RNA_boolean_get(op->ptr, "extend");
1775         loop= 1;
1776
1777         return mouse_select(C, co, extend, loop);
1778 }
1779
1780 static int select_loop_invoke(bContext *C, wmOperator *op, wmEvent *event)
1781 {
1782         ARegion *ar= CTX_wm_region(C);
1783         float co[2];
1784         int x, y;
1785
1786         x= event->x - ar->winrct.xmin;
1787         y= event->y - ar->winrct.ymin;
1788
1789         UI_view2d_region_to_view(&ar->v2d, x, y, &co[0], &co[1]);
1790         RNA_float_set_array(op->ptr, "location", co);
1791
1792         return select_loop_exec(C, op);
1793 }
1794
1795 void UV_OT_select_loop(wmOperatorType *ot)
1796 {
1797         /* identifiers */
1798         ot->name= "Loop Select";
1799         ot->idname= "UV_OT_select_loop";
1800         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1801         
1802         /* api callbacks */
1803         ot->exec= select_loop_exec;
1804         ot->invoke= select_loop_invoke;
1805         ot->poll= ED_operator_uvedit;
1806
1807         /* properties */
1808         RNA_def_boolean(ot->srna, "extend", 0,
1809                 "Extend", "Extend selection rather than clearing the existing selection.");
1810         RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX,
1811                 "Location", "Mouse location in normalized coordinates, 0.0 to 1.0 is within the image bounds.", -100.0f, 100.0f);
1812 }
1813
1814 /* ******************** linked select operator **************** */
1815
1816 static int select_linked_exec(bContext *C, wmOperator *op)
1817 {
1818         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
1819         Scene *scene= CTX_data_scene(C);
1820         ToolSettings *ts= CTX_data_tool_settings(C);
1821         Object *obedit= CTX_data_edit_object(C);
1822         Image *ima= CTX_data_edit_image(C);
1823         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
1824         float limit[2];
1825         int extend;
1826
1827         if(ts->uv_flag & UV_SYNC_SELECTION) {
1828                 BKE_report(op->reports, RPT_ERROR, "Can't select linked when sync selection is enabled.");
1829                 BKE_mesh_end_editmesh(obedit->data, em);
1830                 return OPERATOR_CANCELLED;
1831         }
1832
1833         extend= RNA_boolean_get(op->ptr, "extend");
1834         uvedit_pixel_to_float(sima, limit, 0.05f);
1835         select_linked(scene, ima, em, limit, NULL, extend);
1836
1837         DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
1838         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
1839
1840         BKE_mesh_end_editmesh(obedit->data, em);
1841         return OPERATOR_FINISHED;
1842 }
1843
1844 void UV_OT_select_linked(wmOperatorType *ot)
1845 {
1846         /* identifiers */
1847         ot->name= "Select Linked";
1848         ot->idname= "UV_OT_select_linked";
1849         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1850         
1851         /* api callbacks */
1852         ot->exec= select_linked_exec;
1853         ot->poll= ED_operator_uvedit;
1854
1855         /* properties */
1856         RNA_def_boolean(ot->srna, "extend", 0,
1857                 "Extend", "Extend selection rather than clearing the existing selection.");
1858 }
1859
1860 /* ******************** unlink selection operator **************** */
1861
1862 static int unlink_selection_exec(bContext *C, wmOperator *op)
1863 {
1864         Scene *scene= CTX_data_scene(C);
1865         ToolSettings *ts= CTX_data_tool_settings(C);
1866         Object *obedit= CTX_data_edit_object(C);
1867         Image *ima= CTX_data_edit_image(C);
1868         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
1869         EditFace *efa;
1870         MTFace *tf;
1871
1872         if(ts->uv_flag & UV_SYNC_SELECTION) {
1873                 BKE_report(op->reports, RPT_ERROR, "Can't unlink selection when sync selection is enabled.");
1874                 BKE_mesh_end_editmesh(obedit->data, em);
1875                 return OPERATOR_CANCELLED;
1876         }
1877         
1878         for(efa= em->faces.first; efa; efa= efa->next) {
1879                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1880
1881                 if(uvedit_face_visible(scene, ima, efa, tf)) {
1882                         if(efa->v4) {
1883                                 if(~tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4))
1884                                         tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1885                         }
1886                         else {
1887                                 if(~tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
1888                                         tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3);
1889                         }
1890                 }
1891         }
1892         
1893         DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
1894         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
1895
1896         BKE_mesh_end_editmesh(obedit->data, em);
1897         return OPERATOR_FINISHED;
1898 }
1899
1900 void UV_OT_unlink_selection(wmOperatorType *ot)
1901 {
1902         /* identifiers */
1903         ot->name= "Unlink Selection";
1904         ot->idname= "UV_OT_unlink_selection";
1905         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1906         
1907         /* api callbacks */
1908         ot->exec= unlink_selection_exec;
1909         ot->poll= ED_operator_uvedit;
1910 }
1911
1912 /* ******************** border select operator **************** */
1913
1914 /* This function sets the selection on tagged faces, need because settings the
1915  * selection a face is done in a number of places but it also needs to respect
1916  * the sticky modes for the UV verts, so dealing with the sticky modes is best
1917  * done in a seperate function.
1918  * 
1919  * De-selects faces that have been tagged on efa->tmp.l.  */
1920
1921 static void uv_faces_do_sticky(bContext *C, SpaceImage *sima, Scene *scene, Object *obedit, short select)
1922 {
1923         /* Selecting UV Faces with some modes requires us to change 
1924          * the selection in other faces (depending on the sticky mode).
1925          * 
1926          * This only needs to be done when the Mesh is not used for
1927          * selection (so for sticky modes, vertex or location based). */
1928         
1929         ToolSettings *ts= CTX_data_tool_settings(C);
1930         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
1931         EditFace *efa;
1932         MTFace *tf;
1933         int nverts, i;
1934         
1935         if((ts->uv_flag & UV_SYNC_SELECTION)==0 && sima->sticky == SI_STICKY_VERTEX) {
1936                 /* Tag all verts as untouched, then touch the ones that have a face center
1937                  * in the loop and select all MTFace UV's that use a touched vert. */
1938                 EditVert *eve;
1939                 
1940                 for(eve= em->verts.first; eve; eve= eve->next)
1941                         eve->tmp.l = 0;
1942                 
1943                 for(efa= em->faces.first; efa; efa= efa->next) {
1944                         if(efa->tmp.l) {
1945                                 if(efa->v4)
1946                                         efa->v1->tmp.l= efa->v2->tmp.l= efa->v3->tmp.l= efa->v4->tmp.l=1;
1947                                 else
1948                                         efa->v1->tmp.l= efa->v2->tmp.l= efa->v3->tmp.l= 1;
1949                         }
1950                 }
1951
1952                 /* now select tagged verts */
1953                 for(efa= em->faces.first; efa; efa= efa->next) {
1954                         tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);               
1955                         nverts= efa->v4? 4: 3;
1956                         for(i=0; i<nverts; i++) {
1957                                 if((*(&efa->v1 + i))->tmp.l) {
1958                                         if(select)
1959                                                 uvedit_uv_select(scene, efa, tf, i);
1960                                         else
1961                                                 uvedit_uv_deselect(scene, efa, tf, i);
1962                                 }
1963                         }
1964                 }
1965         }
1966         else if((ts->uv_flag & UV_SYNC_SELECTION)==0 && sima->sticky == SI_STICKY_LOC) {
1967                 EditFace *efa_vlist;
1968                 MTFace *tf_vlist;
1969                 UvMapVert *start_vlist=NULL, *vlist_iter;
1970                 struct UvVertMap *vmap;
1971                 float limit[2];
1972                 int efa_index;
1973                 //EditVert *eve; /* removed vert counting for now */ 
1974                 //int a;
1975                 
1976                 uvedit_pixel_to_float(sima, limit, 0.05);
1977                 
1978                 EM_init_index_arrays(em, 0, 0, 1);
1979                 vmap= EM_make_uv_vert_map(em, 0, 0, limit);
1980                 
1981                 /* verts are numbered above in make_uv_vert_map_EM, make sure this stays true! */
1982                 /*for(a=0, eve= em->verts.first; eve; a++, eve= eve->next)
1983                         eve->tmp.l = a; */
1984                 
1985                 if(vmap == NULL) {
1986                         BKE_mesh_end_editmesh(obedit->data, em);
1987                         return;
1988                 }
1989                 
1990                 for(efa_index=0, efa= em->faces.first; efa; efa_index++, efa= efa->next) {
1991                         if(efa->tmp.l) {
1992                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1993                                 nverts= efa->v4? 4: 3;
1994
1995                                 for(i=0; i<nverts; i++) {
1996                                         if(select)
1997                                                 uvedit_uv_select(scene, efa, tf, i);
1998                                         else
1999                                                 uvedit_uv_deselect(scene, efa, tf, i);
2000                                         
2001                                         vlist_iter= EM_get_uv_map_vert(vmap, (*(&efa->v1 + i))->tmp.l);
2002                                         
2003                                         while (vlist_iter) {
2004                                                 if(vlist_iter->separate)
2005                                                         start_vlist = vlist_iter;
2006                                                 
2007                                                 if(efa_index == vlist_iter->f)
2008                                                         break;
2009
2010                                                 vlist_iter = vlist_iter->next;
2011                                         }
2012                                 
2013                                         vlist_iter = start_vlist;
2014                                         while (vlist_iter) {
2015                                                 
2016                                                 if(vlist_iter != start_vlist && vlist_iter->separate)
2017                                                         break;
2018                                                 
2019                                                 if(efa_index != vlist_iter->f) {
2020                                                         efa_vlist = EM_get_face_for_index(vlist_iter->f);
2021                                                         tf_vlist = CustomData_em_get(&em->fdata, efa_vlist->data, CD_MTFACE);
2022                                                         
2023                                                         if(select)
2024                                                                 uvedit_uv_select(scene, efa_vlist, tf_vlist, vlist_iter->tfindex);
2025                                                         else
2026                                                                 uvedit_uv_deselect(scene, efa_vlist, tf_vlist, vlist_iter->tfindex);
2027                                                 }
2028                                                 vlist_iter = vlist_iter->next;
2029                                         }
2030                                 }
2031                         }
2032                 }
2033                 EM_free_index_arrays();
2034                 EM_free_uv_vert_map(vmap);
2035                 
2036         }
2037         else { /* SI_STICKY_DISABLE or ts->uv_flag & UV_SYNC_SELECTION */
2038                 for(efa= em->faces.first; efa; efa= efa->next) {
2039                         if(efa->tmp.l) {
2040                                 tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2041                                 if(select)
2042                                         uvedit_face_select(scene, efa, tf);
2043                                 else
2044                                         uvedit_face_deselect(scene, efa, tf);
2045                         }
2046                 }
2047         }
2048         BKE_mesh_end_editmesh(obedit->data, em);
2049 }
2050
2051 static int border_select_exec(bContext *C, wmOperator *op)
2052 {
2053         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
2054         Scene *scene= CTX_data_scene(C);
2055         ToolSettings *ts= CTX_data_tool_settings(C);
2056         Object *obedit= CTX_data_edit_object(C);
2057         Image *ima= CTX_data_edit_image(C);
2058         ARegion *ar= CTX_wm_region(C);
2059         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
2060         EditFace *efa;
2061         MTFace *tface;
2062         rcti rect;
2063         rctf rectf;
2064         int change, pinned, select, faces;
2065
2066         /* get rectangle from operator */
2067         rect.xmin= RNA_int_get(op->ptr, "xmin");
2068         rect.ymin= RNA_int_get(op->ptr, "ymin");
2069         rect.xmax= RNA_int_get(op->ptr, "xmax");
2070         rect.ymax= RNA_int_get(op->ptr, "ymax");
2071                 
2072         UI_view2d_region_to_view(&ar->v2d, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin);
2073         UI_view2d_region_to_view(&ar->v2d, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax);
2074
2075         /* figure out what to select/deselect */
2076         select= (RNA_int_get(op->ptr, "event_type") == LEFTMOUSE); // XXX hardcoded
2077         pinned= RNA_boolean_get(op->ptr, "pinned");
2078         
2079         if(ts->uv_flag & UV_SYNC_SELECTION)
2080                 faces= (ts->selectmode == SCE_SELECT_FACE);
2081         else
2082                 faces= (ts->uv_selectmode == UV_SELECT_FACE);
2083
2084         /* do actual selection */
2085         if(faces && !pinned) {
2086                 /* handle face selection mode */
2087                 float cent[2];
2088
2089                 change= 0;
2090
2091                 for(efa= em->faces.first; efa; efa= efa->next) {
2092                         /* assume not touched */
2093                         efa->tmp.l = 0;
2094                         tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2095                         if(uvedit_face_visible(scene, ima, efa, tface)) {
2096                                 uv_center(tface->uv, cent, efa->v4 != NULL);
2097                                 if(BLI_in_rctf(&rectf, cent[0], cent[1])) {
2098                                         efa->tmp.l = change = 1;
2099                                 }
2100                         }
2101                 }
2102
2103                 /* (de)selects all tagged faces and deals with sticky modes */
2104                 if(change)
2105                         uv_faces_do_sticky(C, sima, scene, obedit, select);
2106         }
2107         else {
2108                 /* other selection modes */
2109                 change= 1;
2110
2111                 for(efa= em->faces.first; efa; efa= efa->next) {
2112                         tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2113                         if(uvedit_face_visible(scene, ima, efa, tface)) {
2114                                 if(!pinned || (ts->uv_flag & UV_SYNC_SELECTION) ) {
2115                                         /* UV_SYNC_SELECTION - can't do pinned selection */
2116                                         if(BLI_in_rctf(&rectf, tface->uv[0][0], tface->uv[0][1])) {
2117                                                 if(select)      uvedit_uv_select(scene, efa, tface, 0);
2118                                                 else            uvedit_uv_deselect(scene, efa, tface, 0);
2119                                         }
2120                                         if(BLI_in_rctf(&rectf, tface->uv[1][0], tface->uv[1][1])) {
2121                                                 if(select)      uvedit_uv_select(scene, efa, tface, 1);
2122                                                 else            uvedit_uv_deselect(scene, efa, tface, 1);
2123                                         }
2124                                         if(BLI_in_rctf(&rectf, tface->uv[2][0], tface->uv[2][1])) {
2125                                                 if(select)      uvedit_uv_select(scene, efa, tface, 2);
2126                                                 else            uvedit_uv_deselect(scene, efa, tface, 2);
2127                                         }
2128                                         if(efa->v4 && BLI_in_rctf(&rectf, tface->uv[3][0], tface->uv[3][1])) {
2129                                                 if(select)      uvedit_uv_select(scene, efa, tface, 3);
2130                                                 else            uvedit_uv_deselect(scene, efa, tface, 3);
2131                                         }
2132                                 }
2133                                 else if(pinned) {
2134                                         if((tface->unwrap & TF_PIN1) && 
2135                                                 BLI_in_rctf(&rectf, tface->uv[0][0], tface->uv[0][1])) {
2136                                                 
2137                                                 if(select)      uvedit_uv_select(scene, efa, tface, 0);
2138                                                 else            uvedit_uv_deselect(scene, efa, tface, 0);
2139                                         }
2140                                         if((tface->unwrap & TF_PIN2) && 
2141                                                 BLI_in_rctf(&rectf, tface->uv[1][0], tface->uv[1][1])) {
2142                                                 
2143                                                 if(select)      uvedit_uv_select(scene, efa, tface, 1);
2144                                                 else            uvedit_uv_deselect(scene, efa, tface, 1);
2145                                         }
2146                                         if((tface->unwrap & TF_PIN3) && 
2147                                                 BLI_in_rctf(&rectf, tface->uv[2][0], tface->uv[2][1])) {
2148                                                 
2149                                                 if(select)      uvedit_uv_select(scene, efa, tface, 2);
2150                                                 else            uvedit_uv_deselect(scene, efa, tface, 2);
2151                                         }
2152                                         if((efa->v4) && (tface->unwrap & TF_PIN4) && BLI_in_rctf(&rectf, tface->uv[3][0], tface->uv[3][1])) {
2153                                                 if(select)      uvedit_uv_select(scene, efa, tface, 3);
2154                                                 else            uvedit_uv_deselect(scene, efa, tface, 3);
2155                                         }
2156                                 }
2157                         }
2158                 }
2159         }
2160
2161         if(change) {
2162                 /* make sure newly selected vert selection is updated*/
2163                 if(ts->uv_flag & UV_SYNC_SELECTION) {
2164                         if(ts->selectmode != SCE_SELECT_FACE) {
2165                                 if(select)      EM_select_flush(em);
2166                                 else            EM_deselect_flush(em);
2167                         }
2168                 }
2169
2170                 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
2171                 
2172                 BKE_mesh_end_editmesh(obedit->data, em);
2173                 return OPERATOR_FINISHED;
2174         }
2175
2176         BKE_mesh_end_editmesh(obedit->data, em);
2177         return OPERATOR_CANCELLED;
2178
2179
2180 void UV_OT_select_border(wmOperatorType *ot)
2181 {
2182         /* identifiers */
2183         ot->name= "Border Select";
2184         ot->idname= "UV_OT_select_border";
2185         
2186         /* api callbacks */
2187         ot->invoke= WM_border_select_invoke;
2188         ot->exec= border_select_exec;
2189         ot->modal= WM_border_select_modal;
2190         ot->poll= ED_operator_uvedit;
2191         
2192         /* flags */
2193         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2194         
2195         /* properties */
2196         RNA_def_boolean(ot->srna, "pinned", 0, "Pinned", "Border select pinned UVs only.");
2197
2198         RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
2199         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
2200         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
2201         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
2202         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
2203 }
2204
2205 /* ******************** circle select operator **************** */
2206
2207 static void select_uv_inside_ellipse(SpaceImage *sima, Scene *scene, int select, EditFace *efa, MTFace *tface, int index, float *offset, float *ell, int select_index)
2208 {
2209         /* normalized ellipse: ell[0] = scaleX, ell[1] = scaleY */
2210         float x, y, r2, *uv;
2211         
2212         uv= tface->uv[index];
2213
2214         x= (uv[0] - offset[0])*ell[0];
2215         y= (uv[1] - offset[1])*ell[1];
2216
2217         r2 = x*x + y*y;
2218         if(r2 < 1.0) {
2219                 if(select)      uvedit_uv_select(scene, efa, tface, select_index);
2220                 else uvedit_uv_deselect(scene, efa, tface, select_index);
2221         }
2222 }
2223
2224 int circle_select_exec(bContext *C, wmOperator *op)
2225 {
2226         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
2227         Scene *scene= CTX_data_scene(C);
2228         Object *obedit= CTX_data_edit_object(C);
2229         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
2230         ARegion *ar= CTX_wm_region(C);
2231         EditFace *efa;
2232         MTFace *tface;
2233         int x, y, radius, width, height, select;
2234         float zoomx, zoomy, offset[2], ellipse[2];
2235
2236         /* get operator properties */
2237         select= (RNA_int_get(op->ptr, "event_type") == LEFTMOUSE); // XXX hardcoded
2238         x= RNA_int_get(op->ptr, "x");
2239         y= RNA_int_get(op->ptr, "y");
2240         radius= RNA_int_get(op->ptr, "radius");
2241
2242         /* compute ellipse size and location, not a circle since we deal
2243          * with non square image. ellipse is normalized, r = 1.0. */
2244         ED_space_image_size(sima, &width, &height);
2245         ED_space_image_zoom(sima, ar, &zoomx, &zoomy);
2246
2247         ellipse[0]= width*zoomx/radius;
2248         ellipse[1]= height*zoomy/radius;
2249
2250         UI_view2d_region_to_view(&ar->v2d, x, y, &offset[0], &offset[1]);
2251         
2252         /* do selection */
2253         for(efa= em->faces.first; efa; efa= efa->next) {
2254                 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2255                 select_uv_inside_ellipse(sima, scene, select, efa, tface, 0, offset, ellipse, 0);
2256                 select_uv_inside_ellipse(sima, scene, select, efa, tface, 1, offset, ellipse, 1);
2257                 select_uv_inside_ellipse(sima, scene, select, efa, tface, 2, offset, ellipse, 2);
2258                 if(efa->v4)
2259                         select_uv_inside_ellipse(sima, scene, select, efa, tface, 3, offset, ellipse, 3);
2260         }
2261
2262         if(select) EM_select_flush(em);
2263         else EM_deselect_flush(em);
2264
2265         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
2266
2267         BKE_mesh_end_editmesh(obedit->data, em);
2268         return OPERATOR_FINISHED;
2269 }
2270
2271 void UV_OT_circle_select(wmOperatorType *ot)
2272 {
2273         /* identifiers */
2274         ot->name= "Circle Select";
2275         ot->idname= "UV_OT_circle_select";
2276         
2277         /* api callbacks */
2278         ot->invoke= WM_gesture_circle_invoke;
2279         ot->modal= WM_gesture_circle_modal;
2280         ot->exec= circle_select_exec;
2281         ot->poll= ED_operator_uvedit;
2282         
2283         /* flags */
2284         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2285         
2286         /* properties */
2287         RNA_def_int(ot->srna, "x", 0, INT_MIN, INT_MAX, "X", "", INT_MIN, INT_MAX);
2288         RNA_def_int(ot->srna, "y", 0, INT_MIN, INT_MAX, "Y", "", INT_MIN, INT_MAX);
2289         RNA_def_int(ot->srna, "radius", 0, INT_MIN, INT_MAX, "Radius", "", INT_MIN, INT_MAX);
2290         RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
2291 }
2292
2293 /* ******************** snap cursor operator **************** */
2294
2295 static void snap_uv_to_pixel(float *uvco, float w, float h)
2296 {
2297         uvco[0] = ((float)((int)((uvco[0]*w) + 0.5f)))/w;
2298         uvco[1] = ((float)((int)((uvco[1]*h) + 0.5f)))/h;
2299 }
2300
2301 static void snap_cursor_to_pixels(SpaceImage *sima, View2D *v2d)
2302 {
2303         int width= 0, height= 0;
2304
2305         ED_space_image_size(sima, &width, &height);
2306         snap_uv_to_pixel(v2d->cursor, width, height);
2307 }
2308
2309 static int snap_cursor_to_selection(Scene *scene, Image *ima, Object *obedit, View2D *v2d)
2310 {
2311         return uvedit_center(scene, ima, obedit, v2d->cursor, 0);
2312 }
2313
2314 static int snap_cursor_exec(bContext *C, wmOperator *op)
2315 {
2316         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
2317         Scene *scene= CTX_data_scene(C);
2318         Object *obedit= CTX_data_edit_object(C);
2319         Image *ima= CTX_data_edit_image(C);
2320         ARegion *ar= CTX_wm_region(C);
2321         int change= 0;
2322
2323         switch(RNA_boolean_get(op->ptr, "target")) {
2324                 case 0:
2325                         snap_cursor_to_pixels(sima, &ar->v2d);
2326                         change= 1;
2327                         break;
2328                 case 1:
2329                         change= snap_cursor_to_selection(scene, ima, obedit, &ar->v2d);
2330                         break;
2331         }
2332
2333         if(!change)
2334                 return OPERATOR_CANCELLED;
2335         
2336         ED_region_tag_redraw(ar);
2337
2338         return OPERATOR_FINISHED;
2339 }
2340
2341 void UV_OT_snap_cursor(wmOperatorType *ot)
2342 {
2343         static EnumPropertyItem target_items[] = {
2344                 {0, "PIXELS", 0, "Pixels", ""},
2345                 {1, "SELECTION", 0, "Selection", ""},
2346                 {0, NULL, 0, NULL, NULL}};
2347
2348         /* identifiers */
2349         ot->name= "Snap Cursor";
2350         ot->idname= "UV_OT_snap_cursor";
2351         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2352         
2353         /* api callbacks */
2354         ot->exec= snap_cursor_exec;
2355         ot->poll= ED_operator_uvedit;
2356
2357         /* properties */
2358         RNA_def_enum(ot->srna, "target", target_items, 0, "Target", "Target to snap the selected UV's to.");
2359 }
2360
2361 /* ******************** snap selection operator **************** */
2362
2363 static int snap_uvs_to_cursor(Scene *scene, Image *ima, Object *obedit, View2D *v2d)
2364 {
2365         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
2366         EditFace *efa;
2367         MTFace *tface;
2368         short change= 0;
2369
2370         for(efa= em->faces.first; efa; efa= efa->next) {
2371                 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2372                 if(uvedit_face_visible(scene, ima, efa, tface)) {
2373                         if(uvedit_uv_selected(scene, efa, tface, 0))            VECCOPY2D(tface->uv[0], v2d->cursor);
2374                         if(uvedit_uv_selected(scene, efa, tface, 1))            VECCOPY2D(tface->uv[1], v2d->cursor);
2375                         if(uvedit_uv_selected(scene, efa, tface, 2))            VECCOPY2D(tface->uv[2], v2d->cursor);
2376                         if(efa->v4)
2377                                 if(uvedit_uv_selected(scene, efa, tface, 3))    VECCOPY2D(tface->uv[3], v2d->cursor);
2378
2379                         change= 1;
2380                 }
2381         }
2382
2383         BKE_mesh_end_editmesh(obedit->data, em);
2384         return change;
2385 }
2386
2387 static int snap_uvs_to_adjacent_unselected(Scene *scene, Image *ima, Object *obedit)
2388 {
2389         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
2390         EditFace *efa;
2391         EditVert *eve;
2392         MTFace *tface;
2393         short change = 0;
2394         int count = 0;
2395         float *coords;
2396         short *usercount, users;
2397         
2398         /* set all verts to -1 : an unused index*/
2399         for(eve= em->verts.first; eve; eve= eve->next)
2400                 eve->tmp.l=-1;
2401         
2402         /* index every vert that has a selected UV using it, but only once so as to
2403          * get unique indicies and to count how much to malloc */
2404         for(efa= em->faces.first; efa; efa= efa->next) {
2405                 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2406
2407                 if(uvedit_face_visible(scene, ima, efa, tface)) {
2408                         if(uvedit_uv_selected(scene, efa, tface, 0) && efa->v1->tmp.l==-1)              efa->v1->tmp.l= count++;
2409                         if(uvedit_uv_selected(scene, efa, tface, 1) && efa->v2->tmp.l==-1)              efa->v2->tmp.l= count++;
2410                         if(uvedit_uv_selected(scene, efa, tface, 2) && efa->v3->tmp.l==-1)              efa->v3->tmp.l= count++;
2411                         if(efa->v4)
2412                                 if(uvedit_uv_selected(scene, efa, tface, 3) && efa->v4->tmp.l==-1)      efa->v4->tmp.l= count++;
2413
2414                         change = 1;
2415                         
2416                         /* optional speedup */
2417                         efa->tmp.p = tface;
2418                 }
2419                 else
2420                         efa->tmp.p = NULL;
2421         }
2422         
2423         coords = MEM_callocN(sizeof(float)*count*2, "snap to adjacent coords");
2424         usercount = MEM_callocN(sizeof(short)*count, "snap to adjacent counts");
2425         
2426         /* add all UV coords from visible, unselected UV coords as well as counting them to average later */
2427         for(efa= em->faces.first; efa; efa= efa->next) {
2428                 if((tface=(MTFace *)efa->tmp.p)) {
2429                         /* is this an unselected UV we can snap to? */
2430                         if(efa->v1->tmp.l >= 0 && (!uvedit_uv_selected(scene, efa, tface, 0))) {
2431                                 coords[efa->v1->tmp.l*2] +=             tface->uv[0][0];
2432                                 coords[(efa->v1->tmp.l*2)+1] += tface->uv[0][1];
2433                                 usercount[efa->v1->tmp.l]++;
2434                                 change = 1;
2435                         }
2436                         if(efa->v2->tmp.l >= 0 && (!uvedit_uv_selected(scene, efa, tface, 1))) {
2437                                 coords[efa->v2->tmp.l*2] +=             tface->uv[1][0];
2438                                 coords[(efa->v2->tmp.l*2)+1] += tface->uv[1][1];
2439                                 usercount[efa->v2->tmp.l]++;
2440                                 change = 1;
2441                         }
2442                         if(efa->v3->tmp.l >= 0 && (!uvedit_uv_selected(scene, efa, tface, 2))) {
2443                                 coords[efa->v3->tmp.l*2] +=             tface->uv[2][0];
2444                                 coords[(efa->v3->tmp.l*2)+1] += tface->uv[2][1];
2445                                 usercount[efa->v3->tmp.l]++;
2446                                 change = 1;
2447                         }
2448                         
2449                         if(efa->v4) {
2450                                 if(efa->v4->tmp.l >= 0 && (!uvedit_uv_selected(scene, efa, tface, 3))) {
2451                                         coords[efa->v4->tmp.l*2] +=             tface->uv[3][0];
2452                                         coords[(efa->v4->tmp.l*2)+1] += tface->uv[3][1];
2453                                         usercount[efa->v4->tmp.l]++;
2454                                         change = 1;
2455                                 }
2456                         }
2457                 }
2458         }
2459         
2460         /* no other verts selected, bail out */
2461         if(!change) {
2462                 MEM_freeN(coords);
2463                 MEM_freeN(usercount);
2464                 BKE_mesh_end_editmesh(obedit->data, em);
2465                 return change;
2466         }
2467         
2468         /* copy the averaged unselected UVs back to the selected UVs */
2469         for(efa= em->faces.first; efa; efa= efa->next) {
2470                 if((tface=(MTFace *)efa->tmp.p)) {
2471                         
2472                         if(     uvedit_uv_selected(scene, efa, tface, 0) &&
2473                                         efa->v1->tmp.l >= 0 &&
2474                                         (users = usercount[efa->v1->tmp.l])
2475                         ) {
2476                                 tface->uv[0][0] = coords[efa->v1->tmp.l*2]              / users;
2477                                 tface->uv[0][1] = coords[(efa->v1->tmp.l*2)+1]  / users;
2478                         }
2479
2480                         if(     uvedit_uv_selected(scene, efa, tface, 1) &&
2481                                         efa->v2->tmp.l >= 0 &&
2482                                         (users = usercount[efa->v2->tmp.l])
2483                         ) {
2484                                 tface->uv[1][0] = coords[efa->v2->tmp.l*2]              / users;
2485                                 tface->uv[1][1] = coords[(efa->v2->tmp.l*2)+1]  / users;
2486                         }
2487                         
2488                         if(     uvedit_uv_selected(scene, efa, tface, 2) &&
2489                                         efa->v3->tmp.l >= 0 &&
2490                                         (users = usercount[efa->v3->tmp.l])
2491                         ) {
2492                                 tface->uv[2][0] = coords[efa->v3->tmp.l*2]              / users;
2493                                 tface->uv[2][1] = coords[(efa->v3->tmp.l*2)+1]  / users;
2494                         }
2495                         
2496                         if(efa->v4) {
2497                                 if(     uvedit_uv_selected(scene, efa, tface, 3) &&
2498                                                 efa->v4->tmp.l >= 0 &&
2499                                                 (users = usercount[efa->v4->tmp.l])
2500                                 ) {
2501                                         tface->uv[3][0] = coords[efa->v4->tmp.l*2]              / users;
2502                                         tface->uv[3][1] = coords[(efa->v4->tmp.l*2)+1]  / users;
2503                                 }
2504                         }
2505                 }
2506         }
2507         
2508         MEM_freeN(coords);
2509         MEM_freeN(usercount);
2510
2511         BKE_mesh_end_editmesh(obedit->data, em);
2512         return change;
2513 }
2514
2515 static int snap_uvs_to_pixels(SpaceImage *sima, Scene *scene, Object *obedit)
2516 {
2517         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
2518         Image *ima= sima->image;
2519         EditFace *efa;
2520         MTFace *tface;
2521         int width= 0, height= 0;
2522         float w, h;
2523         short change = 0;
2524
2525         ED_space_image_size(sima, &width, &height);
2526         w = (float)width;
2527         h = (float)height;
2528         
2529         for(efa= em->faces.first; efa; efa= efa->next) {
2530                 tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2531                 if(uvedit_face_visible(scene, ima, efa, tface)) {
2532                         if(uvedit_uv_selected(scene, efa, tface, 0)) snap_uv_to_pixel(tface->uv[0], w, h);
2533                         if(uvedit_uv_selected(scene, efa, tface, 1)) snap_uv_to_pixel(tface->uv[1], w, h);
2534                         if(uvedit_uv_selected(scene, efa, tface, 2)) snap_uv_to_pixel(tface->uv[2], w, h);
2535                         if(efa->v4)
2536                                 if(uvedit_uv_selected(scene, efa, tface, 3)) snap_uv_to_pixel(tface->uv[3], w, h);
2537
2538                         change = 1;
2539                 }
2540         }
2541
2542         BKE_mesh_end_editmesh(obedit->data, em);
2543         return change;
2544 }
2545
2546 static int snap_selection_exec(bContext *C, wmOperator *op)
2547 {
2548         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
2549         Scene *scene= CTX_data_scene(C);
2550         Object *obedit= CTX_data_edit_object(C);
2551         Image *ima= CTX_data_edit_image(C);
2552         ARegion *ar= CTX_wm_region(C);
2553         int change= 0;
2554
2555         switch(RNA_boolean_get(op->ptr, "target")) {
2556                 case 0:
2557                         change= snap_uvs_to_pixels(sima, scene, obedit);
2558                         break;
2559                 case 1:
2560                         change= snap_uvs_to_cursor(scene, ima, obedit, &ar->v2d);
2561                         break;
2562                 case 2:
2563                         change= snap_uvs_to_adjacent_unselected(scene, ima, obedit);
2564                         break;
2565         }
2566
2567         if(!change)
2568                 return OPERATOR_CANCELLED;
2569         
2570         DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
2571         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, obedit);
2572
2573         return OPERATOR_FINISHED;
2574 }
2575
2576 void UV_OT_snap_selection(wmOperatorType *ot)
2577 {
2578         static EnumPropertyItem target_items[] = {
2579                 {0, "PIXELS", 0, "Pixels", ""},
2580                 {1, "CURSOR", 0, "Cursor", ""},
2581                 {2, "ADJACENT_UNSELECTED", 0, "Adjacent Unselected", ""},
2582                 {0, NULL, 0, NULL, NULL}};
2583
2584         /* identifiers */
2585         ot->name= "Snap Selection";
2586         ot->idname= "UV_OT_snap_selection";
2587         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2588         
2589         /* api callbacks */
2590         ot->exec= snap_selection_exec;
2591         ot->poll= ED_operator_uvedit;
2592
2593         /* properties */
2594         RNA_def_enum(ot->srna, "target", target_items, 0, "Target", "Target to snap the selected UV's to.");
2595 }
2596
2597 /* ******************** pin operator **************** */
2598
2599 static int pin_exec(bContext *C, wmOperator *op)
2600 {
2601         Scene *scene= CTX_data_scene(C);
2602         Object *obedit= CTX_data_edit_object(C);
2603         Image *ima= CTX_data_edit_image(C);
2604         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
2605         EditFace *efa;
2606         MTFace *tface;
2607         int clear= RNA_boolean_get(op->ptr, "clear");
2608         
2609         for(efa= em->faces.first; efa; efa= efa->next) {
2610                 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2611
2612                 if(uvedit_face_visible(scene, ima, efa, tface)) {
2613                         if(!clear) {
2614                                 if(uvedit_uv_selected(scene, efa, tface, 0)) tface->unwrap |= TF_PIN1;
2615                                 if(uvedit_uv_selected(scene, efa, tface, 1)) tface->unwrap |= TF_PIN2;
2616                                 if(uvedit_uv_selected(scene, efa, tface, 2)) tface->unwrap |= TF_PIN3;
2617                                 if(efa->v4)
2618                                         if(uvedit_uv_selected(scene, efa, tface, 3)) tface->unwrap |= TF_PIN4;
2619                         }
2620                         else {
2621                                 if(uvedit_uv_selected(scene, efa, tface, 0)) tface->unwrap &= ~TF_PIN1;
2622                                 if(uvedit_uv_selected(scene, efa, tface, 1)) tface->unwrap &= ~TF_PIN2;
2623                                 if(uvedit_uv_selected(scene, efa, tface, 2)) tface->unwrap &= ~TF_PIN3;
2624                                 if(efa->v4)
2625                                         if(uvedit_uv_selected(scene, efa, tface, 3)) tface->unwrap &= ~TF_PIN4;
2626                         }
2627                 }
2628         }
2629         
2630         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_DATA, obedit);
2631
2632         BKE_mesh_end_editmesh(obedit->data, em);
2633         return OPERATOR_FINISHED;
2634 }
2635
2636 void UV_OT_pin(wmOperatorType *ot)
2637 {
2638         /* identifiers */
2639         ot->name= "Pin";
2640         ot->idname= "UV_OT_pin";
2641         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2642         
2643         /* api callbacks */
2644         ot->exec= pin_exec;
2645         ot->poll= ED_operator_uvedit;
2646
2647         /* properties */
2648         RNA_def_boolean(ot->srna, "clear", 0, "Clear", "Clear pinning for the selection instead of setting it.");
2649 }
2650
2651 /******************* select pinned operator ***************/
2652
2653 static int select_pinned_exec(bContext *C, wmOperator *op)
2654 {
2655         Scene *scene= CTX_data_scene(C);
2656         Object *obedit= CTX_data_edit_object(C);
2657         Image *ima= CTX_data_edit_image(C);
2658         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
2659         EditFace *efa;
2660         MTFace *tface;
2661         
2662         for(efa= em->faces.first; efa; efa= efa->next) {
2663                 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2664
2665                 if(uvedit_face_visible(scene, ima, efa, tface)) {
2666                         if(tface->unwrap & TF_PIN1) uvedit_uv_select(scene, efa, tface, 0);
2667                         if(tface->unwrap & TF_PIN2) uvedit_uv_select(scene, efa, tface, 1);
2668                         if(tface->unwrap & TF_PIN3) uvedit_uv_select(scene, efa, tface, 2);
2669                         if(efa->v4) {
2670                                 if(tface->unwrap & TF_PIN4) uvedit_uv_select(scene, efa, tface, 3);
2671                         }
2672                 }
2673         }
2674         
2675         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
2676
2677         BKE_mesh_end_editmesh(obedit->data, em);
2678         return OPERATOR_FINISHED;
2679 }
2680
2681 void UV_OT_select_pinned(wmOperatorType *ot)
2682 {
2683         /* identifiers */
2684         ot->name= "Selected Pinned";
2685         ot->idname= "UV_OT_select_pinned";
2686         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2687         
2688         /* api callbacks */
2689         ot->exec= select_pinned_exec;
2690         ot->poll= ED_operator_uvedit;
2691 }
2692
2693 /********************** hide operator *********************/
2694
2695 static int hide_exec(bContext *C, wmOperator *op)
2696 {
2697         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
2698         ToolSettings *ts= CTX_data_tool_settings(C);
2699         Object *obedit= CTX_data_edit_object(C);
2700         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
2701         EditFace *efa;
2702         MTFace *tf;
2703         int swap= RNA_boolean_get(op->ptr, "unselected");
2704
2705         if(ts->uv_flag & UV_SYNC_SELECTION) {
2706                 EM_hide_mesh(em, swap);
2707                 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
2708
2709
2710                 BKE_mesh_end_editmesh(obedit->data, em);
2711                 return OPERATOR_FINISHED;
2712         }
2713         
2714         if(swap) {
2715                 for(efa= em->faces.first; efa; efa= efa->next) {
2716                         if(efa->f & SELECT) {
2717                                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2718                                 if(sima->flag & SI_SELACTFACE) {
2719                                         /* Pretend face mode */
2720                                         if((    (efa->v4==NULL && 
2721                                                         (       tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) ==                        (TF_SEL1|TF_SEL2|TF_SEL3) )                      ||
2722                                                         (       tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)) ==        (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)       ) == 0) {
2723                                                 
2724                                                 if(em->selectmode == SCE_SELECT_FACE) {
2725                                                         efa->f &= ~SELECT;
2726                                                         /* must re-select after */
2727                                                         efa->e1->f &= ~SELECT;
2728                                                         efa->e2->f &= ~SELECT;
2729                                                         efa->e3->f &= ~SELECT;
2730                                                         if(efa->e4) efa->e4->f &= ~SELECT;
2731                                                 }
2732                                                 else
2733                                                         EM_select_face(efa, 0);
2734                                         }
2735                                         tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2736                                 }
2737                                 else if(em->selectmode == SCE_SELECT_FACE) {
2738                                         if((tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3))==0) {
2739                                                 if(!efa->v4)
2740                                                         EM_select_face(efa, 0);
2741                                                 else if(!(tf->flag & TF_SEL4))
2742                                                         EM_select_face(efa, 0);
2743                                                 tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2744                                         }
2745                                 }
2746                                 else {
2747                                         /* EM_deselect_flush will deselect the face */
2748                                         if((tf->flag & TF_SEL1)==0)                             efa->v1->f &= ~SELECT;
2749                                         if((tf->flag & TF_SEL2)==0)                             efa->v2->f &= ~SELECT;
2750                                         if((tf->flag & TF_SEL3)==0)                             efa->v3->f &= ~SELECT;
2751                                         if((efa->v4) && (tf->flag & TF_SEL4)==0)        efa->v4->f &= ~SELECT;                  
2752
2753                                         tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2754                                 }
2755                         }
2756                 }
2757         }
2758         else {
2759                 for(efa= em->faces.first; efa; efa= efa->next) {
2760                         if(efa->f & SELECT) {
2761                                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2762
2763                                 if(sima->flag & SI_SELACTFACE) {
2764                                         if(     (efa->v4==NULL && 
2765                                                         (       tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) ==                        (TF_SEL1|TF_SEL2|TF_SEL3) )                      ||
2766                                                         (       tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)) ==        (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)       ) {
2767                                                 
2768                                                 if(em->selectmode == SCE_SELECT_FACE) {
2769                                                         efa->f &= ~SELECT;
2770                                                         /* must re-select after */
2771                                                         efa->e1->f &= ~SELECT;
2772                                                         efa->e2->f &= ~SELECT;
2773                                                         efa->e3->f &= ~SELECT;
2774                                                         if(efa->e4) efa->e4->f &= ~SELECT;
2775                                                 }
2776                                                 else
2777                                                         EM_select_face(efa, 0);
2778                                         }
2779
2780                                         tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2781                                 }
2782                                 else if(em->selectmode == SCE_SELECT_FACE) {
2783                                         if(tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
2784                                                 EM_select_face(efa, 0);
2785                                         else if(efa->v4 && tf->flag & TF_SEL4)
2786                                                 EM_select_face(efa, 0);
2787
2788                                         tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2789                                 }
2790                                 else {
2791                                         /* EM_deselect_flush will deselect the face */
2792                                         if(tf->flag & TF_SEL1)                          efa->v1->f &= ~SELECT;
2793                                         if(tf->flag & TF_SEL2)                          efa->v2->f &= ~SELECT;
2794                                         if(tf->flag & TF_SEL3)                          efa->v3->f &= ~SELECT;
2795                                         if((efa->v4) && tf->flag & TF_SEL4)     efa->v4->f &= ~SELECT;
2796
2797                                         tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
2798                                 }
2799                         }
2800                 }
2801         }
2802         
2803         /*deselects too many but ok for now*/
2804         if(em->selectmode & (SCE_SELECT_EDGE|SCE_SELECT_VERTEX))
2805                 EM_deselect_flush(em);
2806         
2807         if(em->selectmode==SCE_SELECT_FACE) {
2808                 /* de-selected all edges from faces that were de-selected.
2809                  * now make sure all faces that are selected also have selected edges */
2810                 for(efa= em->faces.first; efa; efa= efa->next)
2811                         if(efa->f & SELECT)
2812                                 EM_select_face(efa, 1);
2813         }
2814         
2815         EM_validate_selections(em);
2816         WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
2817
2818         BKE_mesh_end_editmesh(obedit->data, em);
2819         return OPERATOR_FINISHED;
2820 }
2821
2822 void UV_OT_hide(wmOperatorType *ot)
2823 {
2824         /* identifiers */
2825         ot->name= "Hide Selected";
2826         ot->idname= "UV_OT_hide";
2827         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2828         
2829         /* api callbacks */
2830         ot->exec= hide_exec;
2831         ot->poll= ED_operator_uvedit;
2832
2833         /* props */
2834         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected.");
2835 }
2836
2837 /****************** reveal operator ******************/
2838
2839 static int reveal_exec(bContext *C, wmOperator *op)
2840 {
2841         SpaceImage *sima= (SpaceImage*)CTX_wm_space_data(C);
2842         ToolSettings *ts= CTX_data_tool_settings(C);
2843         Object *obedit= CTX_data_edit_object(C);
2844         EditMesh *em= BKE_mesh_get_editmesh((Mesh*)obedit->data);
2845         EditFace *efa;
2846         MTFace *tf;
2847         
2848         /* call the mesh function if we are in mesh sync sel */
2849         if(ts->uv_flag & UV_SYNC_SELECTION) {
2850                 EM_reveal_mesh(em);
2851                 WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
2852
2853                 BKE_mesh_end_editmesh(obedit->data, em);
2854                 return OPERATOR_FINISHED;
2855         }
2856         
2857         if(sima->flag & SI_SELACTFACE) {
2858                 if(em->selectmode == SCE_SELECT_FACE) {
2859                         for(efa= em->faces.first; efa; efa= efa->next) {
2860                                 if(!(efa->h) && !(efa->f & SELECT)) {
2861                                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2862                                         EM_select_face(efa, 1);
2863                                         tf->flag |= TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4;
2864                                 }
2865                         }
2866                 }
2867                 else {
2868                         /* enable adjacent faces to have disconnected UV selections if sticky is disabled */
2869                         if(sima->sticky == SI_STICKY_DISABLE) {
2870                                 for(efa= em->faces.first; efa; efa= efa->next) {
2871                                         if(!(efa->h) && !(efa->f & SELECT)) {
2872                                                 /* All verts must be unselected for the face to be selected in the UV view */
2873                                                 if((efa->v1->f&SELECT)==0 && (efa->v2->f&SELECT)==0 && (efa->v3->f&SELECT)==0 && (efa->v4==0 || (efa->v4->f&SELECT)==0)) {
2874                                                         tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2875
2876                                                         tf->flag |= TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4;
2877                                                         /* Cant use EM_select_face here because it unselects the verts
2878                                                          * and we cant tell if the face was totally unselected or not */
2879                                                         /*EM_select_face(efa, 1);
2880                                                          * 
2881                                                          * See Loop with EM_select_face() below... */
2882                                                         efa->f |= SELECT;
2883                                                 }
2884                                         }
2885                                 }
2886                         }
2887                         else {
2888                                 for(efa= em->faces.first; efa; efa= efa->next) {
2889                                         if(!(efa->h) && !(efa->f & SELECT)) {
2890                                                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2891
2892                                                 if((efa->v1->f & SELECT)==0)                            {tf->flag |= TF_SEL1;}
2893                                                 if((efa->v2->f & SELECT)==0)                            {tf->flag |= TF_SEL2;}
2894                                                 if((efa->v3->f & SELECT)==0)                            {tf->flag |= TF_SEL3;}
2895                                                 if((efa->v4 && (efa->v4->f & SELECT)==0))       {tf->flag |= TF_SEL4;}
2896
2897                                                 efa->f |= SELECT;
2898                                         }
2899                                 }
2900                         }
2901                         
2902                         /* Select all edges and verts now */
2903                         for(efa= em->faces.first; efa; efa= efa->next)
2904                                 /* we only selected the face flags, and didnt changes edges or verts, fix this now */
2905                                 if(!(efa->h) && (efa->f & SELECT))
2906                                         EM_select_face(efa, 1);
2907
2908                         EM_select_flush(em);
2909                 }
2910         }
2911         else if(em->selectmode == SCE_SELECT_FACE) {
2912                 for(efa= em->faces.first; efa; efa= efa->next) {
2913                         if(!(efa->h) && !(efa->f & SELECT)) {
2914                                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2915                                 efa->f |= SELECT;
2916                                 tf->flag |= TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4;
2917                         }
2918                 }
2919                 
2920                 /* Select all edges and verts now */
2921                 for(efa= em->faces.first; efa; efa= efa->next)
2922                         /* we only selected the face flags, and didnt changes edges or verts, fix this now */
2923                         if(!(efa->h) && (efa->f & SELECT))
2924                                 EM_select_face(efa, 1);
2925         }
2926         else {
2927                 for(efa= em->faces.first; efa; efa= efa->next) {
2928                         if(!(efa->h) && !(efa->f & SELECT)) {
2929                                 tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
2930
2931                                 if((efa->v1->f & SELECT)==0)                            {tf->flag |= TF_SEL1;}
2932                                 if((efa->v2->f & SELECT)==0)                            {tf->flag |= TF_SEL2;}
2933                                 if((efa->v3->f & SELECT)==0)                            {tf->flag |= TF_SEL3;}
2934                                 if((efa->v4 && (efa->v4->f & SELECT)==0))       {tf->flag |= TF_SEL4;}
2935
2936                                 efa->f |= SELECT;
2937                         }
2938                 }