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