diff options
Diffstat (limited to 'source/blender/editors/uvedit/uvedit_select.c')
-rw-r--r-- | source/blender/editors/uvedit/uvedit_select.c | 144 |
1 files changed, 122 insertions, 22 deletions
diff --git a/source/blender/editors/uvedit/uvedit_select.c b/source/blender/editors/uvedit/uvedit_select.c index 6699f247288..4c354ab5940 100644 --- a/source/blender/editors/uvedit/uvedit_select.c +++ b/source/blender/editors/uvedit/uvedit_select.c @@ -145,6 +145,59 @@ BMLoop *ED_uvedit_active_edge_loop_get(BMesh *bm) * \{ */ /** + * Intentionally don't return #UV_SELECT_ISLAND as it's not an element type. + * In this case return #UV_SELECT_VERTEX as a fallback. + */ +char ED_uvedit_select_mode_get(const Scene *scene) +{ + const ToolSettings *ts = scene->toolsettings; + char uv_selectmode = UV_SELECT_VERTEX; + + if (ts->uv_flag & UV_SYNC_SELECTION) { + if (ts->selectmode & SCE_SELECT_VERTEX) { + uv_selectmode = UV_SELECT_VERTEX; + } + else if (ts->selectmode & SCE_SELECT_EDGE) { + uv_selectmode = UV_SELECT_EDGE; + } + else if (ts->selectmode & SCE_SELECT_FACE) { + uv_selectmode = UV_SELECT_FACE; + } + } + else { + if (ts->uv_selectmode & UV_SELECT_VERTEX) { + uv_selectmode = UV_SELECT_VERTEX; + } + else if (ts->uv_selectmode & UV_SELECT_EDGE) { + uv_selectmode = UV_SELECT_EDGE; + } + else if (ts->uv_selectmode & UV_SELECT_FACE) { + uv_selectmode = UV_SELECT_FACE; + } + } + return uv_selectmode; +} + +void ED_uvedit_select_sync_flush(const ToolSettings *ts, BMEditMesh *em, const bool select) +{ + /* bmesh API handles flushing but not on de-select */ + if (ts->uv_flag & UV_SYNC_SELECTION) { + if (ts->selectmode != SCE_SELECT_FACE) { + if (select == false) { + EDBM_deselect_flush(em); + } + else { + EDBM_select_flush(em); + } + } + + if (select == false) { + BM_select_history_validate(em->bm); + } + } +} + +/** * Apply a penalty to elements that are already selected * so elements that aren't already selected are prioritized. * @@ -857,6 +910,72 @@ bool ED_uvedit_nearest_uv_multi(const Scene *scene, /** \} */ /* -------------------------------------------------------------------- */ +/** \name Find Nearest to Element + * + * These functions are quite specialized, useful when sync select is enabled + * and we want to pick an active UV vertex/edge from the active element which may + * have multiple UV's split out. + * \{ */ + +BMLoop *uv_find_nearest_loop_from_vert(struct Scene *scene, + struct Object *obedit, + struct BMVert *v, + const float co[2]) +{ + BMEditMesh *em = BKE_editmesh_from_object(obedit); + const uint cd_loop_uv_offset = CustomData_get_offset(&em->bm->ldata, CD_MLOOPUV); + + BMIter liter; + BMLoop *l; + BMLoop *l_found = NULL; + float dist_best_sq = FLT_MAX; + + BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) { + if (!uvedit_face_visible_test(scene, l->f)) { + continue; + } + + const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset); + const float dist_test_sq = len_squared_v2v2(co, luv->uv); + if (dist_test_sq < dist_best_sq) { + dist_best_sq = dist_test_sq; + l_found = l; + } + } + return l_found; +} + +BMLoop *uv_find_nearest_loop_from_edge(struct Scene *scene, + struct Object *obedit, + struct BMEdge *e, + const float co[2]) +{ + BMEditMesh *em = BKE_editmesh_from_object(obedit); + const uint cd_loop_uv_offset = CustomData_get_offset(&em->bm->ldata, CD_MLOOPUV); + + BMIter eiter; + BMLoop *l; + BMLoop *l_found = NULL; + float dist_best_sq = FLT_MAX; + + BM_ITER_ELEM (l, &eiter, e, BM_LOOPS_OF_EDGE) { + if (!uvedit_face_visible_test(scene, l->f)) { + continue; + } + const MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset); + const MLoopUV *luv_next = BM_ELEM_CD_GET_VOID_P(l->next, cd_loop_uv_offset); + const float dist_test_sq = dist_squared_to_line_segment_v2(co, luv->uv, luv_next->uv); + if (dist_test_sq < dist_best_sq) { + dist_best_sq = dist_test_sq; + l_found = l; + } + } + return l_found; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ /** \name Edge Loop Select * \{ */ @@ -2383,25 +2502,6 @@ void UV_OT_select_split(wmOperatorType *ot) ot->poll = ED_operator_uvedit; /* requires space image */ } -static void uv_select_sync_flush(const ToolSettings *ts, BMEditMesh *em, const short select) -{ - /* bmesh API handles flushing but not on de-select */ - if (ts->uv_flag & UV_SYNC_SELECTION) { - if (ts->selectmode != SCE_SELECT_FACE) { - if (select == false) { - EDBM_deselect_flush(em); - } - else { - EDBM_select_flush(em); - } - } - - if (select == false) { - BM_select_history_validate(em->bm); - } - } -} - static void uv_select_tag_update_for_object(Depsgraph *depsgraph, const ToolSettings *ts, Object *obedit) @@ -2806,7 +2906,7 @@ static int uv_box_select_exec(bContext *C, wmOperator *op) if (changed || use_pre_deselect) { changed_multi = true; - uv_select_sync_flush(ts, em, select); + ED_uvedit_select_sync_flush(ts, em, select); uv_select_tag_update_for_object(depsgraph, ts, obedit); } } @@ -3026,7 +3126,7 @@ static int uv_circle_select_exec(bContext *C, wmOperator *op) if (changed || use_pre_deselect) { changed_multi = true; - uv_select_sync_flush(ts, em, select); + ED_uvedit_select_sync_flush(ts, em, select); uv_select_tag_update_for_object(depsgraph, ts, obedit); } } @@ -3221,7 +3321,7 @@ static bool do_lasso_select_mesh_uv(bContext *C, if (changed || use_pre_deselect) { changed_multi = true; - uv_select_sync_flush(ts, em, select); + ED_uvedit_select_sync_flush(ts, em, select); uv_select_tag_update_for_object(depsgraph, ts, obedit); } } |