3ff6a7146c647078f33525f1e5959cfd84d356ee
[blender.git] / source / blender / editors / space_view3d / view3d_select.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup spview3d
22  */
23
24
25 #include <string.h>
26 #include <stdio.h>
27 #include <math.h>
28 #include <float.h>
29 #include <assert.h>
30
31 #include "DNA_action_types.h"
32 #include "DNA_armature_types.h"
33 #include "DNA_curve_types.h"
34 #include "DNA_meta_types.h"
35 #include "DNA_mesh_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_tracking_types.h"
40 #include "DNA_gpencil_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_array.h"
45 #include "BLI_math.h"
46 #include "BLI_lasso_2d.h"
47 #include "BLI_rect.h"
48 #include "BLI_linklist.h"
49 #include "BLI_listbase.h"
50 #include "BLI_string.h"
51 #include "BLI_utildefines.h"
52
53 #ifdef __BIG_ENDIAN__
54 #  include "BLI_endian_switch.h"
55 #endif
56
57 /* vertex box select */
58 #include "IMB_imbuf_types.h"
59 #include "IMB_imbuf.h"
60 #include "BKE_global.h"
61
62 #include "BKE_armature.h"
63 #include "BKE_context.h"
64 #include "BKE_curve.h"
65 #include "BKE_layer.h"
66 #include "BKE_mball.h"
67 #include "BKE_mesh.h"
68 #include "BKE_object.h"
69 #include "BKE_paint.h"
70 #include "BKE_editmesh.h"
71 #include "BKE_scene.h"
72 #include "BKE_tracking.h"
73 #include "BKE_workspace.h"
74
75 #include "DEG_depsgraph.h"
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 #include "RNA_access.h"
81 #include "RNA_define.h"
82 #include "RNA_enum_types.h"
83
84 #include "ED_armature.h"
85 #include "ED_curve.h"
86 #include "ED_lattice.h"
87 #include "ED_particle.h"
88 #include "ED_mesh.h"
89 #include "ED_object.h"
90 #include "ED_screen.h"
91 #include "ED_select_utils.h"
92 #include "ED_sculpt.h"
93 #include "ED_mball.h"
94 #include "ED_gpencil.h"
95
96 #include "UI_interface.h"
97
98 #include "GPU_draw.h"
99 #include "GPU_glew.h"
100 #include "GPU_matrix.h"
101
102 #include "view3d_intern.h"  /* own include */
103
104 // #include "PIL_time_utildefines.h"
105
106
107 /* -------------------------------------------------------------------- */
108 /** \name Public Utilities
109  * \{ */
110
111 float ED_view3d_select_dist_px(void)
112 {
113         return 75.0f * U.pixelsize;
114 }
115
116 /* TODO: should return whether there is valid context to continue */
117 void ED_view3d_viewcontext_init(bContext *C, ViewContext *vc)
118 {
119         memset(vc, 0, sizeof(ViewContext));
120         vc->C = C;
121         vc->ar = CTX_wm_region(C);
122         vc->bmain = CTX_data_main(C);
123         vc->depsgraph = CTX_data_depsgraph(C);
124         vc->scene = CTX_data_scene(C);
125         vc->view_layer = CTX_data_view_layer(C);
126         vc->v3d = CTX_wm_view3d(C);
127         vc->win = CTX_wm_window(C);
128         vc->rv3d = CTX_wm_region_view3d(C);
129         vc->obact = CTX_data_active_object(C);
130         vc->obedit = CTX_data_edit_object(C);
131 }
132
133 void ED_view3d_viewcontext_init_object(ViewContext *vc, Object *obact)
134 {
135         vc->obact = obact;
136         if (vc->obedit) {
137                 BLI_assert(BKE_object_is_in_editmode(obact));
138                 vc->obedit = obact;
139                 /* previous selections are now invalid. */
140                 vc->v3d->flag |= V3D_INVALID_BACKBUF;
141
142                 if (vc->em) {
143                         vc->em = BKE_editmesh_from_object(vc->obedit);
144                 }
145         }
146 }
147
148 /** \} */
149
150 /* -------------------------------------------------------------------- */
151 /** \name Internal Object Utilities
152  * \{ */
153
154 static bool object_deselect_all_visible(ViewLayer *view_layer, View3D *v3d)
155 {
156         bool changed = false;
157         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
158                 if (base->flag & BASE_SELECTED) {
159                         if (BASE_SELECTABLE(v3d, base)) {
160                                 ED_object_base_select(base, BA_DESELECT);
161                                 changed = true;
162                         }
163                 }
164         }
165         return changed;
166 }
167
168 /* deselect all except b */
169 static bool object_deselect_all_except(ViewLayer *view_layer, Base *b)
170 {
171         bool changed = false;
172         for (Base *base = view_layer->object_bases.first; base; base = base->next) {
173                 if (base->flag & BASE_SELECTED) {
174                         if (b != base) {
175                                 ED_object_base_select(base, BA_DESELECT);
176                                 changed = true;
177                         }
178                 }
179         }
180         return changed;
181 }
182
183 /** \} */
184
185 /* -------------------------------------------------------------------- */
186 /** \name Internal Edit-Mesh Utilities
187  * \{ */
188
189 static bool edbm_backbuf_check_and_select_verts(BMEditMesh *em, const eSelectOp sel_op)
190 {
191         BMVert *eve;
192         BMIter iter;
193         uint index = bm_wireoffs;
194         bool changed = false;
195
196         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
197                 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
198                         const bool is_select = BM_elem_flag_test(eve, BM_ELEM_SELECT);
199                         const bool is_inside = EDBM_backbuf_check(index);
200                         const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside);
201                         if (sel_op_result != -1) {
202                                 BM_vert_select_set(em->bm, eve, sel_op_result);
203                                 changed = true;
204                         }
205                 }
206                 index++;
207         }
208         return changed;
209 }
210
211 static bool edbm_backbuf_check_and_select_edges(BMEditMesh *em, const eSelectOp sel_op)
212 {
213         BMEdge *eed;
214         BMIter iter;
215         uint index = bm_solidoffs;
216         bool changed = false;
217
218         BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
219                 if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
220                         const bool is_select = BM_elem_flag_test(eed, BM_ELEM_SELECT);
221                         const bool is_inside = EDBM_backbuf_check(index);
222                         const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside);
223                         if (sel_op_result != -1) {
224                                 BM_edge_select_set(em->bm, eed, sel_op_result);
225                                 changed = true;
226                         }
227                 }
228                 index++;
229         }
230         return changed;
231 }
232
233 static bool edbm_backbuf_check_and_select_faces(BMEditMesh *em, const eSelectOp sel_op)
234 {
235         BMFace *efa;
236         BMIter iter;
237         uint index = 1;
238         bool changed = false;
239
240         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
241                 if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
242                         const bool is_select = BM_elem_flag_test(efa, BM_ELEM_SELECT);
243                         const bool is_inside = EDBM_backbuf_check(index);
244                         const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside);
245                         if (sel_op_result != -1) {
246                                 BM_face_select_set(em->bm, efa, sel_op_result);
247                                 changed = true;
248                         }
249                 }
250                 index++;
251         }
252         return changed;
253 }
254
255 /* object mode, edbm_ prefix is confusing here, rename? */
256 static bool edbm_backbuf_check_and_select_verts_obmode(Mesh *me, const eSelectOp sel_op)
257 {
258         MVert *mv = me->mvert;
259         uint index;
260         bool changed = false;
261
262         if (mv) {
263                 for (index = 1; index <= me->totvert; index++, mv++) {
264                         if (!(mv->flag & ME_HIDE)) {
265                                 const bool is_select = mv->flag & SELECT;
266                                 const bool is_inside = EDBM_backbuf_check(index);
267                                 const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside);
268                                 if (sel_op_result != -1) {
269                                         SET_FLAG_FROM_TEST(mv->flag, sel_op_result, SELECT);
270                                         changed = true;
271                                 }
272                         }
273                 }
274         }
275         return changed;
276 }
277
278 /* object mode, edbm_ prefix is confusing here, rename? */
279 static bool edbm_backbuf_check_and_select_tfaces(Mesh *me, const eSelectOp sel_op)
280 {
281         MPoly *mpoly = me->mpoly;
282         uint index;
283         bool changed = false;
284
285         if (mpoly) {
286                 for (index = 1; index <= me->totpoly; index++, mpoly++) {
287                         if (!(mpoly->flag & ME_HIDE)) {
288                                 const bool is_select = mpoly->flag & ME_FACE_SEL;
289                                 const bool is_inside = EDBM_backbuf_check(index);
290                                 const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside);
291                                 if (sel_op_result != -1) {
292                                         SET_FLAG_FROM_TEST(mpoly->flag, sel_op_result, ME_FACE_SEL);
293                                         changed = true;
294                                 }
295                         }
296                 }
297         }
298         return changed;
299 }
300
301 /** \} */
302
303 /* -------------------------------------------------------------------- */
304 /** \name Lasso Select
305  * \{ */
306
307 typedef struct LassoSelectUserData {
308         ViewContext *vc;
309         const rcti *rect;
310         const rctf *rect_fl;
311         rctf       _rect_fl;
312         const int (*mcords)[2];
313         int moves;
314         eSelectOp sel_op;
315
316         /* runtime */
317         int pass;
318         bool is_done;
319         bool is_changed;
320 } LassoSelectUserData;
321
322 static void view3d_userdata_lassoselect_init(
323         LassoSelectUserData *r_data,
324         ViewContext *vc, const rcti *rect, const int (*mcords)[2],
325         const int moves, const eSelectOp sel_op)
326 {
327         r_data->vc = vc;
328
329         r_data->rect = rect;
330         r_data->rect_fl = &r_data->_rect_fl;
331         BLI_rctf_rcti_copy(&r_data->_rect_fl, rect);
332
333         r_data->mcords = mcords;
334         r_data->moves = moves;
335         r_data->sel_op = sel_op;
336
337         /* runtime */
338         r_data->pass = 0;
339         r_data->is_done = false;
340         r_data->is_changed = false;
341 }
342
343 static bool view3d_selectable_data(bContext *C)
344 {
345         Object *ob = CTX_data_active_object(C);
346
347         if (!ED_operator_region_view3d_active(C)) {
348                 return 0;
349         }
350
351         if (ob) {
352                 if (ob->mode & OB_MODE_EDIT) {
353                         if (ob->type == OB_FONT) {
354                                 return 0;
355                         }
356                 }
357                 else {
358                         if ((ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT)) &&
359                             !BKE_paint_select_elem_test(ob))
360                         {
361                                 return 0;
362                         }
363                 }
364         }
365
366         return 1;
367 }
368
369
370 /* helper also for box_select */
371 static bool edge_fully_inside_rect(const rctf *rect, const float v1[2], const float v2[2])
372 {
373         return BLI_rctf_isect_pt_v(rect, v1) && BLI_rctf_isect_pt_v(rect, v2);
374 }
375
376 static bool edge_inside_rect(const rctf *rect, const float v1[2], const float v2[2])
377 {
378         int d1, d2, d3, d4;
379
380         /* check points in rect */
381         if (edge_fully_inside_rect(rect, v1, v2)) {
382                 return 1;
383         }
384
385         /* check points completely out rect */
386         if (v1[0] < rect->xmin && v2[0] < rect->xmin) {
387                 return 0;
388         }
389         if (v1[0] > rect->xmax && v2[0] > rect->xmax) {
390                 return 0;
391         }
392         if (v1[1] < rect->ymin && v2[1] < rect->ymin) {
393                 return 0;
394         }
395         if (v1[1] > rect->ymax && v2[1] > rect->ymax) {
396                 return 0;
397         }
398
399         /* simple check lines intersecting. */
400         d1 = (v1[1] - v2[1]) * (v1[0] - rect->xmin) + (v2[0] - v1[0]) * (v1[1] - rect->ymin);
401         d2 = (v1[1] - v2[1]) * (v1[0] - rect->xmin) + (v2[0] - v1[0]) * (v1[1] - rect->ymax);
402         d3 = (v1[1] - v2[1]) * (v1[0] - rect->xmax) + (v2[0] - v1[0]) * (v1[1] - rect->ymax);
403         d4 = (v1[1] - v2[1]) * (v1[0] - rect->xmax) + (v2[0] - v1[0]) * (v1[1] - rect->ymin);
404
405         if (d1 < 0 && d2 < 0 && d3 < 0 && d4 < 0) {
406                 return 0;
407         }
408         if (d1 > 0 && d2 > 0 && d3 > 0 && d4 > 0) {
409                 return 0;
410         }
411
412         return 1;
413 }
414
415 static void do_lasso_select_pose__do_tag(
416         void *userData, struct bPoseChannel *pchan, const float screen_co_a[2], const float screen_co_b[2])
417 {
418         LassoSelectUserData *data = userData;
419         bArmature *arm = data->vc->obact->data;
420
421         if (PBONE_SELECTABLE(arm, pchan->bone)) {
422                 bool is_point_done = false;
423                 int points_proj_tot = 0;
424
425                 /* project head location to screenspace */
426                 if (screen_co_a[0] != IS_CLIPPED) {
427                         points_proj_tot++;
428                         if (BLI_rcti_isect_pt(data->rect, UNPACK2(screen_co_a)) &&
429                             BLI_lasso_is_point_inside(data->mcords, data->moves, UNPACK2(screen_co_a), INT_MAX))
430                         {
431                                 is_point_done = true;
432                         }
433                 }
434
435                 /* project tail location to screenspace */
436                 if (screen_co_b[0] != IS_CLIPPED) {
437                         points_proj_tot++;
438                         if (BLI_rcti_isect_pt(data->rect, UNPACK2(screen_co_b)) &&
439                             BLI_lasso_is_point_inside(data->mcords, data->moves, UNPACK2(screen_co_b), INT_MAX))
440                         {
441                                 is_point_done = true;
442                         }
443                 }
444
445                 /* if one of points selected, we skip the bone itself */
446                 if ((is_point_done == true) ||
447                     ((is_point_done == false) && (points_proj_tot == 2) &&
448                      BLI_lasso_is_edge_inside(data->mcords, data->moves, UNPACK2(screen_co_a), UNPACK2(screen_co_b), INT_MAX)))
449                 {
450                         pchan->bone->flag |= BONE_DONE;
451                 }
452                 data->is_changed |= is_point_done;
453         }
454 }
455 static void do_lasso_tag_pose(ViewContext *vc, Object *ob, const int mcords[][2], short moves)
456 {
457         ViewContext vc_tmp;
458         LassoSelectUserData data;
459         rcti rect;
460
461         if ((ob->type != OB_ARMATURE) || (ob->pose == NULL)) {
462                 return;
463         }
464
465         vc_tmp = *vc;
466         vc_tmp.obact = ob;
467
468         BLI_lasso_boundbox(&rect, mcords, moves);
469
470         view3d_userdata_lassoselect_init(&data, vc, &rect, mcords, moves, 0);
471
472         ED_view3d_init_mats_rv3d(vc_tmp.obact, vc->rv3d);
473
474         pose_foreachScreenBone(&vc_tmp, do_lasso_select_pose__do_tag, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
475 }
476
477 static bool do_lasso_select_objects(
478         ViewContext *vc, const int mcords[][2], const short moves,
479         const eSelectOp sel_op)
480 {
481         View3D *v3d = vc->v3d;
482         Base *base;
483
484         bool changed = false;
485         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
486                 changed |= object_deselect_all_visible(vc->view_layer, vc->v3d);
487         }
488
489         for (base = vc->view_layer->object_bases.first; base; base = base->next) {
490                 if (BASE_SELECTABLE(v3d, base)) { /* use this to avoid un-needed lasso lookups */
491                         const bool is_select = base->flag & BASE_SELECTED;
492                         const bool is_inside = (
493                                 (ED_view3d_project_base(vc->ar, base) == V3D_PROJ_RET_OK) &&
494                                 BLI_lasso_is_point_inside(mcords, moves, base->sx, base->sy, IS_CLIPPED));
495                         const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside);
496                         if (sel_op_result != -1) {
497                                 ED_object_base_select(base, sel_op_result ? BA_SELECT : BA_DESELECT);
498                                 changed = true;
499                         }
500                 }
501         }
502
503         if (changed) {
504                 DEG_id_tag_update(&vc->scene->id, ID_RECALC_SELECT);
505                 WM_main_add_notifier(NC_SCENE | ND_OB_SELECT, vc->scene);
506         }
507         return changed;
508 }
509
510
511 /**
512  * Use for lasso & box select.
513  */
514 static Base **do_pose_tag_select_op_prepare(ViewContext *vc, uint *r_bases_len)
515 {
516         Base **bases = NULL;
517         BLI_array_declare(bases);
518         FOREACH_BASE_IN_MODE_BEGIN (vc->view_layer, vc->v3d, OB_ARMATURE, OB_MODE_POSE, base_iter) {
519                 Object *ob_iter = base_iter->object;
520                 bArmature *arm = ob_iter->data;
521                 for (bPoseChannel *pchan = ob_iter->pose->chanbase.first; pchan; pchan = pchan->next) {
522                         Bone *bone = pchan->bone;
523                         bone->flag &= ~BONE_DONE;
524                 }
525                 arm->id.tag |= LIB_TAG_DOIT;
526                 ob_iter->id.tag &= ~LIB_TAG_DOIT;
527                 BLI_array_append(bases, base_iter);
528         }
529         FOREACH_BASE_IN_MODE_END;
530         *r_bases_len = BLI_array_len(bases);
531         return bases;
532 }
533
534 static bool do_pose_tag_select_op_exec(Base **bases, const uint bases_len, const eSelectOp sel_op)
535 {
536         bool changed_multi = false;
537
538         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
539                 for (int i = 0; i < bases_len; i++) {
540                         Base *base_iter = bases[i];
541                         Object *ob_iter = base_iter->object;
542                         if (ED_pose_deselect_all(ob_iter, SEL_DESELECT, false)) {
543                                 ED_pose_bone_select_tag_update(ob_iter);
544                                 changed_multi = true;
545                         }
546                 }
547         }
548
549         for (int i = 0; i < bases_len; i++) {
550                 Base *base_iter = bases[i];
551                 Object *ob_iter = base_iter->object;
552                 bArmature *arm = ob_iter->data;
553
554                 /* Don't handle twice. */
555                 if (arm->id.tag & LIB_TAG_DOIT) {
556                         arm->id.tag &= ~LIB_TAG_DOIT;
557                 }
558                 else {
559                         continue;
560                 }
561
562                 bool changed = true;
563                 for (bPoseChannel *pchan = ob_iter->pose->chanbase.first; pchan; pchan = pchan->next) {
564                         Bone *bone = pchan->bone;
565                         if ((bone->flag & BONE_UNSELECTABLE) == 0) {
566                                 const bool is_select = bone->flag & BONE_SELECTED;
567                                 const bool is_inside = bone->flag & BONE_DONE;
568                                 const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside);
569                                 if (sel_op_result != -1) {
570                                         SET_FLAG_FROM_TEST(bone->flag, sel_op_result, BONE_SELECTED);
571                                         if (sel_op_result == 0) {
572                                                 if (arm->act_bone == bone) {
573                                                         arm->act_bone = NULL;
574                                                 }
575                                         }
576                                         changed = true;
577                                 }
578                         }
579                 }
580                 if (changed) {
581                         ED_pose_bone_select_tag_update(ob_iter);
582                         changed_multi = true;
583                 }
584         }
585         return changed_multi;
586 }
587
588 static bool do_lasso_select_pose(
589         ViewContext *vc, const int mcords[][2], const short moves,
590         const eSelectOp sel_op)
591 {
592         uint bases_len;
593         Base **bases = do_pose_tag_select_op_prepare(vc, &bases_len);
594
595         for (int i = 0; i < bases_len; i++) {
596                 Base *base_iter = bases[i];
597                 Object *ob_iter = base_iter->object;
598                 do_lasso_tag_pose(vc, ob_iter, mcords, moves);
599         }
600
601         const bool changed_multi = do_pose_tag_select_op_exec(bases, bases_len, sel_op);
602         if (changed_multi) {
603                 DEG_id_tag_update(&vc->scene->id, ID_RECALC_SELECT);
604                 WM_main_add_notifier(NC_SCENE | ND_OB_SELECT, vc->scene);
605         }
606
607         MEM_freeN(bases);
608         return changed_multi;
609 }
610
611 static void do_lasso_select_mesh__doSelectVert(void *userData, BMVert *eve, const float screen_co[2], int UNUSED(index))
612 {
613         LassoSelectUserData *data = userData;
614         const bool is_select = BM_elem_flag_test(eve, BM_ELEM_SELECT);
615         const bool is_inside = (
616                 BLI_rctf_isect_pt_v(data->rect_fl, screen_co) &&
617                 BLI_lasso_is_point_inside(data->mcords, data->moves, screen_co[0], screen_co[1], IS_CLIPPED));
618         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
619         if (sel_op_result != -1) {
620                 BM_vert_select_set(data->vc->em->bm, eve, sel_op_result);
621                 data->is_changed = true;
622         }
623 }
624 static void do_lasso_select_mesh__doSelectEdge_pass0(
625         void *userData, BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2], int index)
626 {
627         LassoSelectUserData *data = userData;
628         const bool is_select = BM_elem_flag_test(eed, BM_ELEM_SELECT);
629         const bool is_inside = (
630                 EDBM_backbuf_check(bm_solidoffs + index) &&
631                 edge_fully_inside_rect(data->rect_fl, screen_co_a, screen_co_b)  &&
632                 BLI_lasso_is_point_inside(data->mcords, data->moves, UNPACK2(screen_co_a), IS_CLIPPED) &&
633                 BLI_lasso_is_point_inside(data->mcords, data->moves, UNPACK2(screen_co_b), IS_CLIPPED));
634         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
635         if (sel_op_result != -1) {
636                 BM_edge_select_set(data->vc->em->bm, eed, sel_op_result);
637                 data->is_done = true;
638                 data->is_changed = true;
639         }
640 }
641 static void do_lasso_select_mesh__doSelectEdge_pass1(
642         void *userData, BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2], int index)
643 {
644         LassoSelectUserData *data = userData;
645         const bool is_select = BM_elem_flag_test(eed, BM_ELEM_SELECT);
646         const bool is_inside = (
647                 EDBM_backbuf_check(bm_solidoffs + index) &&
648                 BLI_lasso_is_edge_inside(
649                         data->mcords, data->moves, UNPACK2(screen_co_a), UNPACK2(screen_co_b), IS_CLIPPED));
650         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
651         if (sel_op_result != -1) {
652                 BM_edge_select_set(data->vc->em->bm, eed, sel_op_result);
653                 data->is_changed = true;
654         }
655 }
656
657 static void do_lasso_select_mesh__doSelectFace(void *userData, BMFace *efa, const float screen_co[2], int UNUSED(index))
658 {
659         LassoSelectUserData *data = userData;
660         const bool is_select = BM_elem_flag_test(efa, BM_ELEM_SELECT);
661         const bool is_inside = (
662                 BLI_rctf_isect_pt_v(data->rect_fl, screen_co) &&
663                 BLI_lasso_is_point_inside(
664                         data->mcords, data->moves, screen_co[0], screen_co[1], IS_CLIPPED));
665         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
666         if (sel_op_result != -1) {
667                 BM_face_select_set(data->vc->em->bm, efa, sel_op_result);
668                 data->is_changed = true;
669         }
670 }
671
672 static bool do_lasso_select_mesh(
673         ViewContext *vc,
674         const int mcords[][2], short moves, const eSelectOp sel_op)
675 {
676         LassoSelectUserData data;
677         ToolSettings *ts = vc->scene->toolsettings;
678         rcti rect;
679         int bbsel;
680
681         /* set editmesh */
682         vc->em = BKE_editmesh_from_object(vc->obedit);
683
684         BLI_lasso_boundbox(&rect, mcords, moves);
685
686         view3d_userdata_lassoselect_init(&data, vc, &rect, mcords, moves, sel_op);
687
688         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
689                 if (vc->em->bm->totvertsel) {
690                         EDBM_flag_disable_all(vc->em, BM_ELEM_SELECT);
691                         data.is_changed = true;
692                 }
693         }
694
695         /* for non zbuf projections, don't change the GL state */
696         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
697
698         GPU_matrix_set(vc->rv3d->viewmat);
699         bbsel = EDBM_backbuf_border_mask_init(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
700
701         if (ts->selectmode & SCE_SELECT_VERTEX) {
702                 if (bbsel) {
703                         data.is_changed = edbm_backbuf_check_and_select_verts(vc->em, sel_op);
704                 }
705                 else {
706                         mesh_foreachScreenVert(vc, do_lasso_select_mesh__doSelectVert, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
707                 }
708         }
709         if (ts->selectmode & SCE_SELECT_EDGE) {
710                 /* Does both bbsel and non-bbsel versions (need screen cos for both) */
711                 mesh_foreachScreenEdge(vc, do_lasso_select_mesh__doSelectEdge_pass0, &data, V3D_PROJ_TEST_CLIP_NEAR);
712                 if (data.is_done == false) {
713                         mesh_foreachScreenEdge(vc, do_lasso_select_mesh__doSelectEdge_pass1, &data, V3D_PROJ_TEST_CLIP_NEAR);
714                 }
715         }
716
717         if (ts->selectmode & SCE_SELECT_FACE) {
718                 if (bbsel) {
719                         data.is_changed = edbm_backbuf_check_and_select_faces(vc->em, sel_op);
720                 }
721                 else {
722                         mesh_foreachScreenFace(vc, do_lasso_select_mesh__doSelectFace, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
723                 }
724         }
725
726         EDBM_backbuf_free();
727
728         if (data.is_changed) {
729                 EDBM_selectmode_flush(vc->em);
730         }
731         return data.is_changed;
732 }
733
734 static void do_lasso_select_curve__doSelect(
735         void *userData, Nurb *UNUSED(nu), BPoint *bp, BezTriple *bezt, int beztindex, const float screen_co[2])
736 {
737         LassoSelectUserData *data = userData;
738
739         const bool is_inside = BLI_lasso_is_point_inside(data->mcords, data->moves, screen_co[0], screen_co[1], IS_CLIPPED);
740         if (bp) {
741                 const bool is_select = bp->f1 & SELECT;
742                 const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
743                 if (sel_op_result != -1) {
744                         SET_FLAG_FROM_TEST(bp->f1, sel_op_result, SELECT);
745                         data->is_changed = true;
746                 }
747         }
748         else {
749                 if ((data->vc->v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_CU_HANDLES) == 0) {
750                         /* can only be (beztindex == 0) here since handles are hidden */
751                         const bool is_select = bezt->f2 & SELECT;
752                         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
753                         if (sel_op_result != -1) {
754                                 SET_FLAG_FROM_TEST(bezt->f2, sel_op_result, SELECT);
755                         }
756                         bezt->f1 = bezt->f3 = bezt->f2;
757                         data->is_changed = true;
758                 }
759                 else {
760                         char *flag_p = (&bezt->f1) + beztindex;
761                         const bool is_select = *flag_p & SELECT;
762                         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
763                         if (sel_op_result != -1) {
764                                 SET_FLAG_FROM_TEST(*flag_p, sel_op_result, SELECT);
765                                 data->is_changed = true;
766                         }
767                 }
768         }
769 }
770
771 static bool do_lasso_select_curve(ViewContext *vc, const int mcords[][2], short moves, const eSelectOp sel_op)
772 {
773         LassoSelectUserData data;
774         rcti rect;
775
776         BLI_lasso_boundbox(&rect, mcords, moves);
777
778         view3d_userdata_lassoselect_init(&data, vc, &rect, mcords, moves, sel_op);
779
780         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
781                 Curve *curve = (Curve *) vc->obedit->data;
782                 data.is_changed |= ED_curve_deselect_all(curve->editnurb);
783         }
784
785         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
786         nurbs_foreachScreenVert(vc, do_lasso_select_curve__doSelect, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
787         if (data.is_changed) {
788                 BKE_curve_nurb_vert_active_validate(vc->obedit->data);
789         }
790         return data.is_changed;
791 }
792
793 static void do_lasso_select_lattice__doSelect(void *userData, BPoint *bp, const float screen_co[2])
794 {
795         LassoSelectUserData *data = userData;
796         const bool is_select = bp->f1 & SELECT;
797         const bool is_inside = (
798                 BLI_rctf_isect_pt_v(data->rect_fl, screen_co) &&
799                 BLI_lasso_is_point_inside(data->mcords, data->moves, screen_co[0], screen_co[1], IS_CLIPPED));
800         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
801         if (sel_op_result != -1) {
802                 SET_FLAG_FROM_TEST(bp->f1, sel_op_result, SELECT);
803                 data->is_changed = true;
804         }
805 }
806 static bool do_lasso_select_lattice(ViewContext *vc, const int mcords[][2], short moves, const eSelectOp sel_op)
807 {
808         LassoSelectUserData data;
809         rcti rect;
810
811         BLI_lasso_boundbox(&rect, mcords, moves);
812
813         view3d_userdata_lassoselect_init(&data, vc, &rect, mcords, moves, sel_op);
814
815         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
816                 data.is_changed |= ED_lattice_flags_set(vc->obedit, 0);
817         }
818
819         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
820         lattice_foreachScreenVert(vc, do_lasso_select_lattice__doSelect, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
821         return data.is_changed;
822 }
823
824 static void do_lasso_select_armature__doSelectBone(
825         void *userData, EditBone *ebone, const float screen_co_a[2], const float screen_co_b[2])
826 {
827         LassoSelectUserData *data = userData;
828         bArmature *arm = data->vc->obedit->data;
829         if (EBONE_VISIBLE(arm, ebone)) {
830                 int is_ignore_flag = 0;
831                 int is_inside_flag = 0;
832
833                 if (screen_co_a[0] != IS_CLIPPED) {
834                         if (BLI_rcti_isect_pt(data->rect, UNPACK2(screen_co_a)) &&
835                             BLI_lasso_is_point_inside(data->mcords, data->moves, UNPACK2(screen_co_a), INT_MAX))
836                         {
837                                 is_inside_flag |= BONESEL_ROOT;
838                         }
839                 }
840                 else {
841                         is_ignore_flag |= BONESEL_ROOT;
842                 }
843
844                 if (screen_co_b[0] != IS_CLIPPED) {
845                         if (BLI_rcti_isect_pt(data->rect, UNPACK2(screen_co_b)) &&
846                             BLI_lasso_is_point_inside(data->mcords, data->moves, UNPACK2(screen_co_b), INT_MAX))
847                         {
848                                 is_inside_flag |= BONESEL_TIP;
849                         }
850                 }
851                 else {
852                         is_ignore_flag |= BONESEL_TIP;
853                 }
854
855                 if (is_inside_flag == (BONE_ROOTSEL | BONE_TIPSEL) ||
856                     BLI_lasso_is_edge_inside(
857                             data->mcords, data->moves, UNPACK2(screen_co_a), UNPACK2(screen_co_b), INT_MAX))
858                 {
859                         is_inside_flag |= BONESEL_BONE;
860                 }
861
862
863                 ebone->temp.i = is_inside_flag | (is_ignore_flag >> 16);
864         }
865 }
866
867 static bool do_lasso_select_armature(ViewContext *vc, const int mcords[][2], short moves, const eSelectOp sel_op)
868 {
869         LassoSelectUserData data;
870         rcti rect;
871
872         BLI_lasso_boundbox(&rect, mcords, moves);
873
874         view3d_userdata_lassoselect_init(&data, vc, &rect, mcords, moves, sel_op);
875
876         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
877                 data.is_changed |= ED_armature_edit_deselect_all_visible(vc->obedit);
878         }
879
880         bArmature *arm = vc->obedit->data;
881
882         ED_armature_ebone_listbase_temp_clear(arm->edbo);
883
884         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
885
886         armature_foreachScreenBone(vc, do_lasso_select_armature__doSelectBone, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
887
888         data.is_changed |= ED_armature_edit_select_op_from_tagged(vc->obedit->data, sel_op);
889
890         if (data.is_changed) {
891                 WM_main_add_notifier(NC_OBJECT | ND_BONE_SELECT, vc->obedit);
892         }
893         return data.is_changed;
894 }
895
896 static void do_lasso_select_mball__doSelectElem(void *userData, struct MetaElem *ml, const float screen_co[2])
897 {
898         LassoSelectUserData *data = userData;
899         const bool is_select = ml->flag & SELECT;
900         const bool is_inside = (
901                 BLI_rctf_isect_pt_v(data->rect_fl, screen_co) &&
902                 BLI_lasso_is_point_inside(data->mcords, data->moves, screen_co[0], screen_co[1], INT_MAX));
903         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
904         if (sel_op_result != -1) {
905                 SET_FLAG_FROM_TEST(ml->flag, sel_op_result, SELECT);
906                 data->is_changed = true;
907         }
908 }
909 static bool do_lasso_select_meta(ViewContext *vc, const int mcords[][2], short moves, const eSelectOp sel_op)
910 {
911         LassoSelectUserData data;
912         rcti rect;
913
914         MetaBall *mb = (MetaBall *)vc->obedit->data;
915
916         BLI_lasso_boundbox(&rect, mcords, moves);
917
918         view3d_userdata_lassoselect_init(&data, vc, &rect, mcords, moves, sel_op);
919
920         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
921                 data.is_changed |= BKE_mball_deselect_all(mb);
922         }
923
924         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
925
926         mball_foreachScreenElem(vc, do_lasso_select_mball__doSelectElem, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
927
928         return data.is_changed;
929 }
930
931 static void do_lasso_select_meshobject__doSelectVert(void *userData, MVert *mv, const float screen_co[2], int UNUSED(index))
932 {
933         LassoSelectUserData *data = userData;
934         const bool is_select = mv->flag & SELECT;
935         const bool is_inside = (
936                 BLI_rctf_isect_pt_v(data->rect_fl, screen_co) &&
937                 BLI_lasso_is_point_inside(data->mcords, data->moves, screen_co[0], screen_co[1], IS_CLIPPED));
938         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
939         if (sel_op_result != -1) {
940                 SET_FLAG_FROM_TEST(mv->flag, sel_op_result, SELECT);
941                 data->is_changed = true;
942         }
943 }
944 static bool do_lasso_select_paintvert(ViewContext *vc, const int mcords[][2], short moves, const eSelectOp sel_op)
945 {
946         const bool use_zbuf = !XRAY_ENABLED(vc->v3d);
947         Object *ob = vc->obact;
948         Mesh *me = ob->data;
949         rcti rect;
950
951         if (me == NULL || me->totvert == 0) {
952                 return false;
953         }
954
955         bool changed = false;
956         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
957                 /* flush selection at the end */
958                 changed |= paintvert_deselect_all_visible(ob, SEL_DESELECT, false);
959         }
960
961         BLI_lasso_boundbox(&rect, mcords, moves);
962
963         if (use_zbuf) {
964                 bm_vertoffs = me->totvert + 1; /* max index array */
965
966                 EDBM_backbuf_border_mask_init(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
967
968                 changed |= edbm_backbuf_check_and_select_verts_obmode(me, sel_op);
969
970                 EDBM_backbuf_free();
971         }
972         else {
973                 LassoSelectUserData data;
974
975                 view3d_userdata_lassoselect_init(&data, vc, &rect, mcords, moves, sel_op);
976
977                 ED_view3d_init_mats_rv3d(vc->obact, vc->rv3d);
978
979                 meshobject_foreachScreenVert(vc, do_lasso_select_meshobject__doSelectVert, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
980
981                 changed |= data.is_changed;
982         }
983
984         if (changed) {
985                 if (SEL_OP_CAN_DESELECT(sel_op)) {
986                         BKE_mesh_mselect_validate(me);
987                 }
988                 paintvert_flush_flags(ob);
989                 paintvert_tag_select_update(vc->C, ob);
990         }
991         return changed;
992 }
993 static bool do_lasso_select_paintface(ViewContext *vc, const int mcords[][2], short moves, const eSelectOp sel_op)
994 {
995         Object *ob = vc->obact;
996         Mesh *me = ob->data;
997         rcti rect;
998
999         if (me == NULL || me->totpoly == 0) {
1000                 return false;
1001         }
1002
1003         bm_vertoffs = me->totpoly + 1; /* max index array */
1004
1005         BLI_lasso_boundbox(&rect, mcords, moves);
1006         EDBM_backbuf_border_mask_init(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
1007
1008         bool changed = false;
1009         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
1010                 /* flush selection at the end */
1011                 changed |= paintface_deselect_all_visible(vc->C, ob, SEL_DESELECT, false);
1012         }
1013         changed |= edbm_backbuf_check_and_select_tfaces(me, sel_op);
1014
1015         EDBM_backbuf_free();
1016
1017         if (changed) {
1018                 paintface_flush_flags(vc->C, ob, SELECT);
1019         }
1020         return changed;
1021 }
1022
1023 #if 0
1024 static void do_lasso_select_node(int mcords[][2], short moves, const eSelectOp sel_op)
1025 {
1026         SpaceNode *snode = sa->spacedata.first;
1027
1028         bNode *node;
1029         rcti rect;
1030         int node_cent[2];
1031         float node_centf[2];
1032         bool changed = false;
1033
1034         BLI_lasso_boundbox(&rect, mcords, moves);
1035
1036         /* store selection in temp test flag */
1037         for (node = snode->edittree->nodes.first; node; node = node->next) {
1038                 node_centf[0] = BLI_RCT_CENTER_X(&node->totr);
1039                 node_centf[1] = BLI_RCT_CENTER_Y(&node->totr);
1040
1041                 ipoco_to_areaco_noclip(G.v2d, node_centf, node_cent);
1042                 const bool is_select = node->flag & SELECT;
1043                 const bool is_inside = (
1044                         BLI_rcti_isect_pt_v(&rect, node_cent) &&
1045                         BLI_lasso_is_point_inside(mcords, moves, node_cent[0], node_cent[1]));
1046                 const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside);
1047                 if (sel_op_result != -1) {
1048                         SET_FLAG_FROM_TEST(node->flag, sel_op_result, SELECT);
1049                         changed = true;
1050                 }
1051         }
1052         if (changed) {
1053                 BIF_undo_push("Lasso select nodes");
1054         }
1055 }
1056 #endif
1057
1058 static bool view3d_lasso_select(
1059         bContext *C, ViewContext *vc,
1060         const int mcords[][2], short moves,
1061         const eSelectOp sel_op)
1062 {
1063         Object *ob = CTX_data_active_object(C);
1064         bool changed_multi = false;
1065
1066         if (vc->obedit == NULL) { /* Object Mode */
1067                 if (BKE_paint_select_face_test(ob)) {
1068                         changed_multi = do_lasso_select_paintface(vc, mcords, moves, sel_op);
1069                 }
1070                 else if (BKE_paint_select_vert_test(ob)) {
1071                         changed_multi = do_lasso_select_paintvert(vc, mcords, moves, sel_op);
1072                 }
1073                 else if (ob && (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT))) {
1074                         /* pass */
1075                 }
1076                 else if (ob && (ob->mode & OB_MODE_PARTICLE_EDIT)) {
1077                         changed_multi = PE_lasso_select(C, mcords, moves, sel_op);
1078                 }
1079                 else if (ob && (ob->mode & OB_MODE_POSE)) {
1080                         changed_multi = do_lasso_select_pose(vc, mcords, moves, sel_op);
1081                 }
1082                 else {
1083                         changed_multi = do_lasso_select_objects(vc, mcords, moves, sel_op);
1084                 }
1085         }
1086         else { /* Edit Mode */
1087
1088                 FOREACH_OBJECT_IN_MODE_BEGIN (vc->view_layer, vc->v3d, ob->type, ob->mode, ob_iter) {
1089                         ED_view3d_viewcontext_init_object(vc, ob_iter);
1090                         bool changed = false;
1091
1092                         switch (vc->obedit->type) {
1093                                 case OB_MESH:
1094                                         changed = do_lasso_select_mesh(vc, mcords, moves, sel_op);
1095                                         break;
1096                                 case OB_CURVE:
1097                                 case OB_SURF:
1098                                         changed = do_lasso_select_curve(vc, mcords, moves, sel_op);
1099                                         break;
1100                                 case OB_LATTICE:
1101                                         changed = do_lasso_select_lattice(vc, mcords, moves, sel_op);
1102                                         break;
1103                                 case OB_ARMATURE:
1104                                         changed = do_lasso_select_armature(vc, mcords, moves, sel_op);
1105                                         break;
1106                                 case OB_MBALL:
1107                                         changed = do_lasso_select_meta(vc, mcords, moves, sel_op);
1108                                         break;
1109                                 default:
1110                                         assert(!"lasso select on incorrect object type");
1111                                         break;
1112                         }
1113
1114                         if (changed) {
1115                                 DEG_id_tag_update(vc->obedit->data, ID_RECALC_SELECT);
1116                                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc->obedit->data);
1117                                 changed_multi = true;
1118                         }
1119                 }
1120                 FOREACH_OBJECT_IN_MODE_END;
1121         }
1122         return changed_multi;
1123 }
1124
1125
1126 /* lasso operator gives properties, but since old code works
1127  * with short array we convert */
1128 static int view3d_lasso_select_exec(bContext *C, wmOperator *op)
1129 {
1130         ViewContext vc;
1131         int mcords_tot;
1132         const int (*mcords)[2] = WM_gesture_lasso_path_to_array(C, op, &mcords_tot);
1133
1134         if (mcords) {
1135                 view3d_operator_needs_opengl(C);
1136
1137                 /* setup view context for argument to callbacks */
1138                 ED_view3d_viewcontext_init(C, &vc);
1139
1140                 eSelectOp sel_op = RNA_enum_get(op->ptr, "mode");
1141                 bool changed_multi = view3d_lasso_select(C, &vc, mcords, mcords_tot, sel_op);
1142
1143                 MEM_freeN((void *)mcords);
1144
1145                 if (changed_multi) {
1146                         return OPERATOR_FINISHED;
1147                 }
1148                 else {
1149                         return OPERATOR_CANCELLED;
1150                 }
1151         }
1152         return OPERATOR_PASS_THROUGH;
1153 }
1154
1155 void VIEW3D_OT_select_lasso(wmOperatorType *ot)
1156 {
1157         ot->name = "Lasso Select";
1158         ot->description = "Select items using lasso selection";
1159         ot->idname = "VIEW3D_OT_select_lasso";
1160
1161         ot->invoke = WM_gesture_lasso_invoke;
1162         ot->modal = WM_gesture_lasso_modal;
1163         ot->exec = view3d_lasso_select_exec;
1164         ot->poll = view3d_selectable_data;
1165         ot->cancel = WM_gesture_lasso_cancel;
1166
1167         /* flags */
1168         ot->flag = OPTYPE_UNDO;
1169
1170         /* properties */
1171         WM_operator_properties_gesture_lasso(ot);
1172         WM_operator_properties_select_operation(ot);
1173 }
1174
1175 /** \} */
1176
1177 /* -------------------------------------------------------------------- */
1178 /** \name Cursor Picking
1179  * \{ */
1180
1181 /* The max number of menu items in an object select menu */
1182 typedef struct SelMenuItemF {
1183         char idname[MAX_ID_NAME - 2];
1184         int icon;
1185 } SelMenuItemF;
1186
1187 #define SEL_MENU_SIZE   22
1188 static SelMenuItemF object_mouse_select_menu_data[SEL_MENU_SIZE];
1189
1190 /* special (crappy) operator only for menu select */
1191 static const EnumPropertyItem *object_select_menu_enum_itemf(
1192         bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
1193 {
1194         EnumPropertyItem *item = NULL, item_tmp = {0};
1195         int totitem = 0;
1196         int i = 0;
1197
1198         /* don't need context but avoid docgen using this */
1199         if (C == NULL || object_mouse_select_menu_data[i].idname[0] == '\0') {
1200                 return DummyRNA_NULL_items;
1201         }
1202
1203         for (; i < SEL_MENU_SIZE && object_mouse_select_menu_data[i].idname[0] != '\0'; i++) {
1204                 item_tmp.name = object_mouse_select_menu_data[i].idname;
1205                 item_tmp.identifier = object_mouse_select_menu_data[i].idname;
1206                 item_tmp.value = i;
1207                 item_tmp.icon = object_mouse_select_menu_data[i].icon;
1208                 RNA_enum_item_add(&item, &totitem, &item_tmp);
1209         }
1210
1211         RNA_enum_item_end(&item, &totitem);
1212         *r_free = true;
1213
1214         return item;
1215 }
1216
1217 static int object_select_menu_exec(bContext *C, wmOperator *op)
1218 {
1219         const int name_index = RNA_enum_get(op->ptr, "name");
1220         const bool toggle = RNA_boolean_get(op->ptr, "toggle");
1221         bool changed = false;
1222         const char *name = object_mouse_select_menu_data[name_index].idname;
1223
1224         if (!toggle) {
1225                 CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
1226                 {
1227                         if ((base->flag & BASE_SELECTED) != 0) {
1228                                 ED_object_base_select(base, BA_DESELECT);
1229                                 changed = true;
1230                         }
1231                 }
1232                 CTX_DATA_END;
1233         }
1234
1235         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
1236         {
1237                 /* This is a bit dodgy, there should only be ONE object with this name,
1238                  * but library objects can mess this up. */
1239                 if (STREQ(name, base->object->id.name + 2)) {
1240                         ED_object_base_activate(C, base);
1241                         ED_object_base_select(base, BA_SELECT);
1242                         changed = true;
1243                 }
1244         }
1245         CTX_DATA_END;
1246
1247         /* weak but ensures we activate menu again before using the enum */
1248         memset(object_mouse_select_menu_data, 0, sizeof(object_mouse_select_menu_data));
1249
1250         /* undo? */
1251         if (changed) {
1252                 Scene *scene = CTX_data_scene(C);
1253                 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1254                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1255                 return OPERATOR_FINISHED;
1256         }
1257         else {
1258                 return OPERATOR_CANCELLED;
1259         }
1260 }
1261
1262 void VIEW3D_OT_select_menu(wmOperatorType *ot)
1263 {
1264         PropertyRNA *prop;
1265
1266         /* identifiers */
1267         ot->name = "Select Menu";
1268         ot->description = "Menu object selection";
1269         ot->idname = "VIEW3D_OT_select_menu";
1270
1271         /* api callbacks */
1272         ot->invoke = WM_menu_invoke;
1273         ot->exec = object_select_menu_exec;
1274
1275         /* flags */
1276         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1277
1278         /* keyingset to use (dynamic enum) */
1279         prop = RNA_def_enum(ot->srna, "name", DummyRNA_NULL_items, 0, "Object Name", "");
1280         RNA_def_enum_funcs(prop, object_select_menu_enum_itemf);
1281         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_ENUM_NO_TRANSLATE);
1282         ot->prop = prop;
1283
1284         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "Toggle selection instead of deselecting everything first");
1285 }
1286
1287 static Base *object_mouse_select_menu(
1288         bContext *C, ViewContext *vc, uint *buffer, int hits,
1289         const int mval[2], bool toggle)
1290 {
1291         short baseCount = 0;
1292         bool ok;
1293         LinkNode *linklist = NULL;
1294
1295         /* handle base->object->select_id */
1296         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
1297         {
1298                 ok = false;
1299
1300                 /* two selection methods, the CTRL select uses max dist of 15 */
1301                 if (buffer) {
1302                         for (int a = 0; a < hits; a++) {
1303                                 /* index was converted */
1304                                 if (base->object->select_id == (buffer[(4 * a) + 3] & ~0xFFFF0000)) {
1305                                         ok = true;
1306                                         break;
1307                                 }
1308                         }
1309                 }
1310                 else {
1311                         const int dist = 15 * U.pixelsize;
1312                         if (ED_view3d_project_base(vc->ar, base) == V3D_PROJ_RET_OK) {
1313                                 const int delta_px[2] = {base->sx - mval[0], base->sy - mval[1]};
1314                                 if (len_manhattan_v2_int(delta_px) < dist) {
1315                                         ok = true;
1316                                 }
1317                         }
1318                 }
1319
1320                 if (ok) {
1321                         baseCount++;
1322                         BLI_linklist_prepend(&linklist, base);
1323
1324                         if (baseCount == SEL_MENU_SIZE) {
1325                                 break;
1326                         }
1327                 }
1328         }
1329         CTX_DATA_END;
1330
1331         if (baseCount == 0) {
1332                 return NULL;
1333         }
1334         if (baseCount == 1) {
1335                 Base *base = (Base *)linklist->link;
1336                 BLI_linklist_free(linklist, NULL);
1337                 return base;
1338         }
1339         else {
1340                 /* UI, full in static array values that we later use in an enum function */
1341                 LinkNode *node;
1342                 int i;
1343
1344                 memset(object_mouse_select_menu_data, 0, sizeof(object_mouse_select_menu_data));
1345
1346                 for (node = linklist, i = 0; node; node = node->next, i++) {
1347                         Base *base = node->link;
1348                         Object *ob = base->object;
1349                         const char *name = ob->id.name + 2;
1350
1351                         BLI_strncpy(object_mouse_select_menu_data[i].idname, name, MAX_ID_NAME - 2);
1352                         object_mouse_select_menu_data[i].icon = UI_icon_from_id(&ob->id);
1353                 }
1354
1355                 {
1356                         wmOperatorType *ot = WM_operatortype_find("VIEW3D_OT_select_menu", false);
1357                         PointerRNA ptr;
1358
1359                         WM_operator_properties_create_ptr(&ptr, ot);
1360                         RNA_boolean_set(&ptr, "toggle", toggle);
1361                         WM_operator_name_call_ptr(C, ot, WM_OP_INVOKE_DEFAULT, &ptr);
1362                         WM_operator_properties_free(&ptr);
1363                 }
1364
1365                 BLI_linklist_free(linklist, NULL);
1366                 return NULL;
1367         }
1368 }
1369
1370 static bool selectbuffer_has_bones(const uint *buffer, const uint hits)
1371 {
1372         uint i;
1373         for (i = 0; i < hits; i++) {
1374                 if (buffer[(4 * i) + 3] & 0xFFFF0000) {
1375                         return true;
1376                 }
1377         }
1378         return false;
1379 }
1380
1381 /* utility function for mixed_bones_object_selectbuffer */
1382 static int selectbuffer_ret_hits_15(uint *UNUSED(buffer), const int hits15)
1383 {
1384         return hits15;
1385 }
1386
1387 static int selectbuffer_ret_hits_9(uint *buffer, const int hits15, const int hits9)
1388 {
1389         const int offs = 4 * hits15;
1390         memcpy(buffer, buffer + offs, 4 * hits9 * sizeof(uint));
1391         return hits9;
1392 }
1393
1394 static int selectbuffer_ret_hits_5(uint *buffer, const int hits15, const int hits9, const int hits5)
1395 {
1396         const int offs = 4 * hits15 + 4 * hits9;
1397         memcpy(buffer, buffer + offs, 4 * hits5  * sizeof(uint));
1398         return hits5;
1399 }
1400
1401 /**
1402  * Populate a select buffer with objects and bones, if there are any.
1403  * Checks three selection levels and compare.
1404  */
1405 static int mixed_bones_object_selectbuffer(
1406         ViewContext *vc, uint *buffer, const int mval[2], eV3DSelectObjectFilter select_filter,
1407         bool do_nearest)
1408 {
1409         rcti rect;
1410         int hits15, hits9 = 0, hits5 = 0;
1411         bool has_bones15 = false, has_bones9 = false, has_bones5 = false;
1412
1413         const int select_mode = (do_nearest ? VIEW3D_SELECT_PICK_NEAREST : VIEW3D_SELECT_PICK_ALL);
1414         int hits = 0;
1415
1416         /* we _must_ end cache before return, use 'goto finally' */
1417         view3d_opengl_select_cache_begin();
1418
1419         BLI_rcti_init_pt_radius(&rect, mval, 14);
1420         hits15 = view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect, select_mode, select_filter);
1421         if (hits15 == 1) {
1422                 hits = selectbuffer_ret_hits_15(buffer, hits15);
1423                 goto finally;
1424         }
1425         else if (hits15 > 0) {
1426                 int offs;
1427                 has_bones15 = selectbuffer_has_bones(buffer, hits15);
1428
1429                 offs = 4 * hits15;
1430                 BLI_rcti_init_pt_radius(&rect, mval, 9);
1431                 hits9 = view3d_opengl_select(vc, buffer + offs, MAXPICKBUF - offs, &rect, select_mode, select_filter);
1432                 if (hits9 == 1) {
1433                         hits = selectbuffer_ret_hits_9(buffer, hits15, hits9);
1434                         goto finally;
1435                 }
1436                 else if (hits9 > 0) {
1437                         has_bones9 = selectbuffer_has_bones(buffer + offs, hits9);
1438
1439                         offs += 4 * hits9;
1440                         BLI_rcti_init_pt_radius(&rect, mval, 5);
1441                         hits5 = view3d_opengl_select(vc, buffer + offs, MAXPICKBUF - offs, &rect, select_mode, select_filter);
1442                         if (hits5 == 1) {
1443                                 hits = selectbuffer_ret_hits_5(buffer, hits15, hits9, hits5);
1444                                 goto finally;
1445                         }
1446                         else if (hits5 > 0) {
1447                                 has_bones5 = selectbuffer_has_bones(buffer + offs, hits5);
1448                         }
1449                 }
1450
1451                 if      (has_bones5)  { hits = selectbuffer_ret_hits_5(buffer,  hits15, hits9, hits5); goto finally; }
1452                 else if (has_bones9)  { hits = selectbuffer_ret_hits_9(buffer,  hits15, hits9); goto finally; }
1453                 else if (has_bones15) { hits = selectbuffer_ret_hits_15(buffer, hits15); goto finally; }
1454
1455                 if      (hits5 > 0) { hits = selectbuffer_ret_hits_5(buffer,  hits15, hits9, hits5); goto finally; }
1456                 else if (hits9 > 0) { hits = selectbuffer_ret_hits_9(buffer,  hits15, hits9); goto finally; }
1457                 else                { hits = selectbuffer_ret_hits_15(buffer, hits15); goto finally; }
1458         }
1459
1460 finally:
1461         view3d_opengl_select_cache_end();
1462         return hits;
1463 }
1464
1465 static int mixed_bones_object_selectbuffer_extended(
1466         ViewContext *vc, uint *buffer, const int mval[2], eV3DSelectObjectFilter select_filter,
1467         bool use_cycle, bool enumerate, bool *r_do_nearest)
1468 {
1469         static int last_mval[2] = {-100, -100};
1470         bool do_nearest = false;
1471         View3D *v3d = vc->v3d;
1472
1473         /* define if we use solid nearest select or not */
1474         if (use_cycle) {
1475                 if (!XRAY_ACTIVE(v3d)) {
1476                         do_nearest = true;
1477                         if (len_manhattan_v2v2_int(mval, last_mval) <= WM_EVENT_CURSOR_MOTION_THRESHOLD) {
1478                                 do_nearest = false;
1479                         }
1480                 }
1481                 copy_v2_v2_int(last_mval, mval);
1482         }
1483         else {
1484                 if (!XRAY_ACTIVE(v3d)) {
1485                         do_nearest = true;
1486                 }
1487         }
1488
1489         if (r_do_nearest) {
1490                 *r_do_nearest = do_nearest;
1491         }
1492
1493         do_nearest = do_nearest && !enumerate;
1494
1495         int hits = mixed_bones_object_selectbuffer(vc, buffer, mval, select_filter, do_nearest);
1496
1497         if (vc->scene->toolsettings->object_flag & SCE_OBJECT_MODE_LOCK) {
1498                 const bool is_pose_mode = (
1499                         (vc->obact && vc->obact->mode & OB_MODE_POSE) ||
1500                         (select_filter == VIEW3D_SELECT_FILTER_WPAINT_POSE_MODE_LOCK));
1501                 struct {
1502                         uint data[4];
1503                 } *buffer4 = (void *)buffer;
1504                 uint j = 0;
1505                 for (uint i = 0; i < hits; i++) {
1506                         if (((buffer4[i].data[3] & 0xFFFF0000) != 0) == is_pose_mode) {
1507                                 if (i != j) {
1508                                         buffer4[j] = buffer4[i];
1509                                 }
1510                                 j++;
1511                         }
1512                 }
1513                 hits = j;
1514         }
1515
1516         return hits;
1517 }
1518
1519 /* returns basact */
1520 static Base *mouse_select_eval_buffer(
1521         ViewContext *vc, const uint *buffer, int hits,
1522         Base *startbase, bool has_bones, bool do_nearest)
1523 {
1524         ViewLayer *view_layer = vc->view_layer;
1525         View3D *v3d = vc->v3d;
1526         Base *base, *basact = NULL;
1527         int a;
1528
1529         if (do_nearest) {
1530                 uint min = 0xFFFFFFFF;
1531                 int selcol = 0, notcol = 0;
1532
1533
1534                 if (has_bones) {
1535                         /* we skip non-bone hits */
1536                         for (a = 0; a < hits; a++) {
1537                                 if (min > buffer[4 * a + 1] && (buffer[4 * a + 3] & 0xFFFF0000) ) {
1538                                         min = buffer[4 * a + 1];
1539                                         selcol = buffer[4 * a + 3] & 0xFFFF;
1540                                 }
1541                         }
1542                 }
1543                 else {
1544                         /* only exclude active object when it is selected... */
1545                         if (BASACT(view_layer) && (BASACT(view_layer)->flag & BASE_SELECTED) && hits > 1) {
1546                                 notcol = BASACT(view_layer)->object->select_id;
1547                         }
1548
1549                         for (a = 0; a < hits; a++) {
1550                                 if (min > buffer[4 * a + 1] && notcol != (buffer[4 * a + 3] & 0xFFFF)) {
1551                                         min = buffer[4 * a + 1];
1552                                         selcol = buffer[4 * a + 3] & 0xFFFF;
1553                                 }
1554                         }
1555                 }
1556
1557                 base = FIRSTBASE(view_layer);
1558                 while (base) {
1559                         if (BASE_SELECTABLE(v3d, base)) {
1560                                 if (base->object->select_id == selcol) {
1561                                         break;
1562                                 }
1563                         }
1564                         base = base->next;
1565                 }
1566                 if (base) {
1567                         basact = base;
1568                 }
1569         }
1570         else {
1571
1572                 base = startbase;
1573                 while (base) {
1574                         /* skip objects with select restriction, to prevent prematurely ending this loop
1575                          * with an un-selectable choice */
1576                         if ((base->flag & BASE_SELECTABLE) == 0) {
1577                                 base = base->next;
1578                                 if (base == NULL) {
1579                                         base = FIRSTBASE(view_layer);
1580                                 }
1581                                 if (base == startbase) {
1582                                         break;
1583                                 }
1584                         }
1585
1586                         if (BASE_SELECTABLE(v3d, base)) {
1587                                 for (a = 0; a < hits; a++) {
1588                                         if (has_bones) {
1589                                                 /* skip non-bone objects */
1590                                                 if ((buffer[4 * a + 3] & 0xFFFF0000)) {
1591                                                         if (base->object->select_id == (buffer[(4 * a) + 3] & 0xFFFF)) {
1592                                                                 basact = base;
1593                                                         }
1594                                                 }
1595                                         }
1596                                         else {
1597                                                 if (base->object->select_id == (buffer[(4 * a) + 3] & 0xFFFF)) {
1598                                                         basact = base;
1599                                                 }
1600                                         }
1601                                 }
1602                         }
1603
1604                         if (basact) {
1605                                 break;
1606                         }
1607
1608                         base = base->next;
1609                         if (base == NULL) {
1610                                 base = FIRSTBASE(view_layer);
1611                         }
1612                         if (base == startbase) {
1613                                 break;
1614                         }
1615                 }
1616         }
1617
1618         return basact;
1619 }
1620
1621 /* mval comes from event->mval, only use within region handlers */
1622 Base *ED_view3d_give_base_under_cursor(bContext *C, const int mval[2])
1623 {
1624         ViewContext vc;
1625         Base *basact = NULL;
1626         uint buffer[MAXPICKBUF];
1627
1628         /* setup view context for argument to callbacks */
1629         view3d_operator_needs_opengl(C);
1630
1631         ED_view3d_viewcontext_init(C, &vc);
1632
1633         const bool do_nearest = !XRAY_ACTIVE(vc.v3d);
1634         const int hits = mixed_bones_object_selectbuffer(
1635                 &vc, buffer, mval, VIEW3D_SELECT_FILTER_NOP, do_nearest);
1636
1637         if (hits > 0) {
1638                 const bool has_bones = selectbuffer_has_bones(buffer, hits);
1639                 basact = mouse_select_eval_buffer(&vc, buffer, hits, vc.view_layer->object_bases.first, has_bones, do_nearest);
1640         }
1641
1642         return basact;
1643 }
1644
1645 Object *ED_view3d_give_object_under_cursor(bContext *C, const int mval[2])
1646 {
1647         Base *base = ED_view3d_give_base_under_cursor(C, mval);
1648         if (base) {
1649                 return base->object;
1650         }
1651         return NULL;
1652 }
1653
1654 bool ED_view3d_is_object_under_cursor(bContext *C, const int mval[2])
1655 {
1656         return ED_view3d_give_object_under_cursor(C, mval) != NULL;
1657 }
1658
1659 static void deselect_all_tracks(MovieTracking *tracking)
1660 {
1661         MovieTrackingObject *object;
1662
1663         object = tracking->objects.first;
1664         while (object) {
1665                 ListBase *tracksbase = BKE_tracking_object_get_tracks(tracking, object);
1666                 MovieTrackingTrack *track = tracksbase->first;
1667
1668                 while (track) {
1669                         BKE_tracking_track_deselect(track, TRACK_AREA_ALL);
1670
1671                         track = track->next;
1672                 }
1673
1674                 object = object->next;
1675         }
1676 }
1677
1678 /* mval is region coords */
1679 static bool ed_object_select_pick(
1680         bContext *C, const int mval[2],
1681         bool extend, bool deselect, bool toggle, bool obcenter, bool enumerate, bool object)
1682 {
1683         ViewContext vc;
1684         /* setup view context for argument to callbacks */
1685         ED_view3d_viewcontext_init(C, &vc);
1686
1687         ARegion *ar = CTX_wm_region(C);
1688         Scene *scene = CTX_data_scene(C);
1689         ViewLayer *view_layer = CTX_data_view_layer(C);
1690         View3D *v3d = CTX_wm_view3d(C);
1691         /* Don't set when the context has no active object (hidden), see: T60807. */
1692         const Base *oldbasact = vc.obact ? BASACT(view_layer) : NULL;
1693         Base *base, *startbase = NULL, *basact = NULL;
1694         const eObjectMode object_mode = oldbasact ? oldbasact->object->mode : OB_MODE_OBJECT;
1695         bool is_obedit;
1696         float dist = ED_view3d_select_dist_px() * 1.3333f;
1697         bool retval = false;
1698         int hits;
1699         const float mval_fl[2] = {(float)mval[0], (float)mval[1]};
1700
1701
1702         is_obedit = (vc.obedit != NULL);
1703         if (object) {
1704                 /* signal for view3d_opengl_select to skip editmode objects */
1705                 vc.obedit = NULL;
1706         }
1707
1708         /* In pose mode we don't want to mess with object selection. */
1709         const bool is_pose_mode = (vc.obact && vc.obact->mode & OB_MODE_POSE);
1710
1711         /* always start list from basact in wire mode */
1712         startbase =  FIRSTBASE(view_layer);
1713         if (oldbasact && oldbasact->next) {
1714                 startbase = oldbasact->next;
1715         }
1716
1717         /* This block uses the control key to make the object selected
1718          * by its center point rather than its contents */
1719
1720         /* in editmode do not activate */
1721         if (obcenter) {
1722
1723                 /* note; shift+alt goes to group-flush-selecting */
1724                 if (enumerate) {
1725                         basact = object_mouse_select_menu(C, &vc, NULL, 0, mval, toggle);
1726                 }
1727                 else {
1728                         base = startbase;
1729                         while (base) {
1730                                 if (BASE_SELECTABLE(v3d, base)) {
1731                                         float screen_co[2];
1732                                         if (ED_view3d_project_float_global(
1733                                                     ar, base->object->obmat[3], screen_co,
1734                                                     V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_WIN | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK)
1735                                         {
1736                                                 float dist_temp = len_manhattan_v2v2(mval_fl, screen_co);
1737                                                 if (base == oldbasact) {
1738                                                         dist_temp += 10.0f;
1739                                                 }
1740                                                 if (dist_temp < dist) {
1741                                                         dist = dist_temp;
1742                                                         basact = base;
1743                                                 }
1744                                         }
1745                                 }
1746                                 base = base->next;
1747
1748                                 if (base == NULL) {
1749                                         base = FIRSTBASE(view_layer);
1750                                 }
1751                                 if (base == startbase) {
1752                                         break;
1753                                 }
1754                         }
1755                 }
1756                 if (scene->toolsettings->object_flag & SCE_OBJECT_MODE_LOCK) {
1757                         if (is_obedit == false) {
1758                                 if (basact && !BKE_object_is_mode_compat(basact->object, object_mode)) {
1759                                         if (object_mode == OB_MODE_OBJECT) {
1760                                                 struct Main *bmain = CTX_data_main(C);
1761                                                 ED_object_mode_generic_exit(bmain, vc.depsgraph, scene, basact->object);
1762                                         }
1763                                         if (!BKE_object_is_mode_compat(basact->object, object_mode)) {
1764                                                 basact = NULL;
1765                                         }
1766                                 }
1767                         }
1768                 }
1769         }
1770         else {
1771                 uint buffer[MAXPICKBUF];
1772                 bool do_nearest;
1773
1774                 // TIMEIT_START(select_time);
1775
1776                 /* if objects have posemode set, the bones are in the same selection buffer */
1777                 const eV3DSelectObjectFilter select_filter = (
1778                         (object == false) ? ED_view3d_select_filter_from_mode(scene, vc.obact) : VIEW3D_SELECT_FILTER_NOP);
1779                 hits = mixed_bones_object_selectbuffer_extended(
1780                         &vc, buffer, mval, select_filter,
1781                         true, enumerate,
1782                         &do_nearest);
1783
1784                 // TIMEIT_END(select_time);
1785
1786                 if (hits > 0) {
1787                         /* note: bundles are handling in the same way as bones */
1788                         const bool has_bones = selectbuffer_has_bones(buffer, hits);
1789
1790                         /* note; shift+alt goes to group-flush-selecting */
1791                         if (enumerate) {
1792                                 basact = object_mouse_select_menu(C, &vc, buffer, hits, mval, toggle);
1793                         }
1794                         else {
1795                                 basact = mouse_select_eval_buffer(&vc, buffer, hits, startbase, has_bones, do_nearest);
1796                         }
1797
1798                         if (has_bones && basact) {
1799                                 if (basact->object->type == OB_CAMERA) {
1800                                         if (oldbasact == basact) {
1801                                                 int i, hitresult;
1802                                                 bool changed = false;
1803
1804                                                 for (i = 0; i < hits; i++) {
1805                                                         hitresult = buffer[3 + (i * 4)];
1806
1807                                                         /* if there's bundles in buffer select bundles first,
1808                                                          * so non-camera elements should be ignored in buffer */
1809                                                         if (basact->object->select_id != (hitresult & 0xFFFF)) {
1810                                                                 continue;
1811                                                         }
1812
1813                                                         /* index of bundle is 1<<16-based. if there's no "bone" index
1814                                                          * in height word, this buffer value belongs to camera. not to bundle
1815                                                          */
1816                                                         if (buffer[4 * i + 3] & 0xFFFF0000) {
1817                                                                 MovieClip *clip = BKE_object_movieclip_get(scene, basact->object, false);
1818                                                                 MovieTracking *tracking = &clip->tracking;
1819                                                                 ListBase *tracksbase;
1820                                                                 MovieTrackingTrack *track;
1821
1822                                                                 track = BKE_tracking_track_get_indexed(&clip->tracking, hitresult >> 16, &tracksbase);
1823
1824                                                                 if (TRACK_SELECTED(track) && extend) {
1825                                                                         changed = false;
1826                                                                         BKE_tracking_track_deselect(track, TRACK_AREA_ALL);
1827                                                                 }
1828                                                                 else {
1829                                                                         int oldsel = TRACK_SELECTED(track) ? 1 : 0;
1830                                                                         if (!extend) {
1831                                                                                 deselect_all_tracks(tracking);
1832                                                                         }
1833
1834                                                                         BKE_tracking_track_select(tracksbase, track, TRACK_AREA_ALL, extend);
1835
1836                                                                         if (oldsel != (TRACK_SELECTED(track) ? 1 : 0)) {
1837                                                                                 changed = true;
1838                                                                         }
1839                                                                 }
1840
1841                                                                 basact->flag |= BASE_SELECTED;
1842                                                                 BKE_scene_object_base_flag_sync_from_base(basact);
1843
1844                                                                 retval = true;
1845
1846                                                                 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1847                                                                 DEG_id_tag_update(&clip->id, ID_RECALC_SELECT);
1848                                                                 WM_event_add_notifier(C, NC_MOVIECLIP | ND_SELECT, track);
1849                                                                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1850
1851                                                                 break;
1852                                                         }
1853                                                 }
1854
1855                                                 if (!changed) {
1856                                                         /* fallback to regular object selection if no new bundles were selected,
1857                                                          * allows to select object parented to reconstruction object */
1858                                                         basact = mouse_select_eval_buffer(&vc, buffer, hits, startbase, 0, do_nearest);
1859                                                 }
1860                                         }
1861                                 }
1862                                 else if (ED_armature_pose_select_pick_with_buffer(
1863                                                  view_layer, v3d, basact, buffer, hits, extend, deselect, toggle, do_nearest))
1864                                 {
1865                                         /* then bone is found */
1866
1867                                         /* we make the armature selected:
1868                                          * not-selected active object in posemode won't work well for tools */
1869                                         basact->flag |= BASE_SELECTED;
1870                                         BKE_scene_object_base_flag_sync_from_base(basact);
1871
1872                                         retval = true;
1873                                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, basact->object);
1874                                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_ACTIVE, basact->object);
1875
1876                                         /* in weightpaint, we use selected bone to select vertexgroup,
1877                                          * so no switch to new active object */
1878                                         if (oldbasact && (oldbasact->object->mode & OB_MODE_WEIGHT_PAINT)) {
1879                                                 /* prevent activating */
1880                                                 basact = NULL;
1881                                         }
1882
1883                                 }
1884                                 /* prevent bone selecting to pass on to object selecting */
1885                                 if (basact == oldbasact) {
1886                                         basact = NULL;
1887                                 }
1888                         }
1889
1890                         if (scene->toolsettings->object_flag & SCE_OBJECT_MODE_LOCK) {
1891                                 if (is_obedit == false) {
1892                                         if (basact && !BKE_object_is_mode_compat(basact->object, object_mode)) {
1893                                                 if (object_mode == OB_MODE_OBJECT) {
1894                                                         struct Main *bmain = CTX_data_main(C);
1895                                                         ED_object_mode_generic_exit(bmain, vc.depsgraph, scene, basact->object);
1896                                                 }
1897                                                 if (!BKE_object_is_mode_compat(basact->object, object_mode)) {
1898                                                         basact = NULL;
1899                                                 }
1900                                         }
1901                                 }
1902                         }
1903                 }
1904         }
1905
1906         if (scene->toolsettings->object_flag & SCE_OBJECT_MODE_LOCK) {
1907                 /* Disallow switching modes,
1908                  * special exception for edit-mode - vertex-parent operator. */
1909                 if (is_obedit == false) {
1910                         if (oldbasact && basact) {
1911                                 if ((oldbasact->object->mode != basact->object->mode) &&
1912                                     (oldbasact->object->mode & basact->object->mode) == 0)
1913                                 {
1914                                         basact = NULL;
1915                                 }
1916                         }
1917                 }
1918         }
1919
1920         /* Ensure code above doesn't change the active base. */
1921         BLI_assert(oldbasact == (vc.obact ? BASACT(view_layer) : NULL));
1922
1923         /* so, do we have something selected? */
1924         if (basact) {
1925                 retval = true;
1926
1927                 if (vc.obedit) {
1928                         /* only do select */
1929                         object_deselect_all_except(view_layer, basact);
1930                         ED_object_base_select(basact, BA_SELECT);
1931                 }
1932                 /* also prevent making it active on mouse selection */
1933                 else if (BASE_SELECTABLE(v3d, basact)) {
1934                         if (extend) {
1935                                 ED_object_base_select(basact, BA_SELECT);
1936                         }
1937                         else if (deselect) {
1938                                 ED_object_base_select(basact, BA_DESELECT);
1939                         }
1940                         else if (toggle) {
1941                                 if (basact->flag & BASE_SELECTED) {
1942                                         if (basact == oldbasact) {
1943                                                 ED_object_base_select(basact, BA_DESELECT);
1944                                         }
1945                                 }
1946                                 else {
1947                                         ED_object_base_select(basact, BA_SELECT);
1948                                 }
1949                         }
1950                         else {
1951                                 /* When enabled, this puts other objects out of multi pose-mode. */
1952                                 if (is_pose_mode == false) {
1953                                         object_deselect_all_except(view_layer, basact);
1954                                         ED_object_base_select(basact, BA_SELECT);
1955                                 }
1956                         }
1957
1958                         if ((oldbasact != basact) && (is_obedit == false)) {
1959                                 ED_object_base_activate(C, basact); /* adds notifier */
1960                         }
1961
1962                         /* Set special modes for grease pencil
1963                            The grease pencil modes are not real modes, but a hack to make the interface
1964                            consistent, so need some tricks to keep UI synchronized */
1965                         // XXX: This stuff needs reviewing (Aligorith)
1966                         if (false &&
1967                             (((oldbasact) && oldbasact->object->type == OB_GPENCIL) ||
1968                              (basact->object->type == OB_GPENCIL)))
1969                         {
1970                                 /* set cursor */
1971                                 if (ELEM(basact->object->mode,
1972                                          OB_MODE_PAINT_GPENCIL,
1973                                          OB_MODE_SCULPT_GPENCIL,
1974                                          OB_MODE_WEIGHT_GPENCIL))
1975                                 {
1976                                         ED_gpencil_toggle_brush_cursor(C, true, NULL);
1977                                 }
1978                                 else {
1979                                         /* TODO: maybe is better use restore */
1980                                         ED_gpencil_toggle_brush_cursor(C, false, NULL);
1981                                 }
1982                         }
1983                 }
1984
1985                 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
1986                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1987         }
1988
1989         return retval;
1990 }
1991
1992 /* mouse selection in weight paint */
1993 /* gets called via generic mouse select operator */
1994 static bool ed_wpaint_vertex_select_pick(
1995         bContext *C, const int mval[2],
1996         bool extend, bool deselect, bool toggle, Object *obact)
1997 {
1998         View3D *v3d = CTX_wm_view3d(C);
1999         const bool use_zbuf = !XRAY_ENABLED(v3d);
2000
2001         Mesh *me = obact->data; /* already checked for NULL */
2002         uint index = 0;
2003         MVert *mv;
2004
2005         if (ED_mesh_pick_vert(C, obact, mval, ED_MESH_PICK_DEFAULT_VERT_DIST, use_zbuf, &index)) {
2006                 mv = &me->mvert[index];
2007                 if (extend) {
2008                         mv->flag |= SELECT;
2009                 }
2010                 else if (deselect) {
2011                         mv->flag &= ~SELECT;
2012                 }
2013                 else if (toggle) {
2014                         mv->flag ^= SELECT;
2015                 }
2016                 else {
2017                         paintvert_deselect_all_visible(obact, SEL_DESELECT, false);
2018                         mv->flag |= SELECT;
2019                 }
2020
2021                 /* update mselect */
2022                 if (mv->flag & SELECT) {
2023                         BKE_mesh_mselect_active_set(me, index, ME_VSEL);
2024                 }
2025                 else {
2026                         BKE_mesh_mselect_validate(me);
2027                 }
2028
2029                 paintvert_flush_flags(obact);
2030                 paintvert_tag_select_update(C, obact);
2031                 return true;
2032         }
2033         return false;
2034 }
2035
2036 static int view3d_select_exec(bContext *C, wmOperator *op)
2037 {
2038         Scene *scene = CTX_data_scene(C);
2039         Object *obedit = CTX_data_edit_object(C);
2040         Object *obact = CTX_data_active_object(C);
2041         bool extend = RNA_boolean_get(op->ptr, "extend");
2042         bool deselect = RNA_boolean_get(op->ptr, "deselect");
2043         bool deselect_all = RNA_boolean_get(op->ptr, "deselect_all");
2044         bool toggle = RNA_boolean_get(op->ptr, "toggle");
2045         bool center = RNA_boolean_get(op->ptr, "center");
2046         bool enumerate = RNA_boolean_get(op->ptr, "enumerate");
2047         /* only force object select for editmode to support vertex parenting,
2048          * or paint-select to allow pose bone select with vert/face select */
2049         bool object = (RNA_boolean_get(op->ptr, "object") &&
2050                        (obedit ||
2051                         BKE_paint_select_elem_test(obact) ||
2052                         /* so its possible to select bones in weightpaint mode (LMB select) */
2053                         (obact && (obact->mode & OB_MODE_WEIGHT_PAINT) && BKE_object_pose_armature_get(obact))));
2054
2055         bool retval = false;
2056         int location[2];
2057
2058         RNA_int_get_array(op->ptr, "location", location);
2059
2060         view3d_operator_needs_opengl(C);
2061
2062         if (object) {
2063                 obedit = NULL;
2064                 obact = NULL;
2065
2066                 /* ack, this is incorrect but to do this correctly we would need an
2067                  * alternative editmode/objectmode keymap, this copies the functionality
2068                  * from 2.4x where Ctrl+Select in editmode does object select only */
2069                 center = false;
2070         }
2071
2072         if (obedit && object == false) {
2073                 if (obedit->type == OB_MESH) {
2074                         retval = EDBM_select_pick(C, location, extend, deselect, toggle);
2075                         if (!retval && deselect_all) {
2076                                 retval = EDBM_mesh_deselect_all_multi(C);
2077                         }
2078                 }
2079                 else if (obedit->type == OB_ARMATURE) {
2080                         retval = ED_armature_edit_select_pick(C, location, extend, deselect, toggle);
2081                         if (!retval && deselect_all) {
2082                                 retval = ED_armature_edit_deselect_all_visible_multi(C);
2083                         }
2084                 }
2085                 else if (obedit->type == OB_LATTICE) {
2086                         retval = ED_lattice_select_pick(C, location, extend, deselect, toggle);
2087                         if (!retval && deselect_all) {
2088                                 retval = ED_lattice_deselect_all_multi(C);
2089                         }
2090                 }
2091                 else if (ELEM(obedit->type, OB_CURVE, OB_SURF)) {
2092                         retval = ED_curve_editnurb_select_pick(C, location, extend, deselect, toggle);
2093                         if (!retval && deselect_all) {
2094                                 retval = ED_curve_deselect_all_multi(C);
2095                         }
2096                 }
2097                 else if (obedit->type == OB_MBALL) {
2098                         retval = ED_mball_select_pick(C, location, extend, deselect, toggle);
2099                         if (!retval && deselect_all) {
2100                                 retval = ED_mball_deselect_all_multi(C);
2101                         }
2102                 }
2103                 else if (obedit->type == OB_FONT) {
2104                         retval = ED_curve_editfont_select_pick(C, location, extend, deselect, toggle);
2105                         if (!retval && deselect_all) {
2106                                 /* pass */
2107                         }
2108                 }
2109
2110         }
2111         else if (obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
2112                 retval = PE_mouse_particles(C, location, extend, deselect, toggle);
2113                 if (!retval && deselect_all) {
2114                         retval = PE_deselect_all_visible(C);
2115                 }
2116         }
2117         else if (obact && BKE_paint_select_face_test(obact)) {
2118                 retval = paintface_mouse_select(C, obact, location, extend, deselect, toggle);
2119                 if (!retval && deselect_all) {
2120                         retval = paintface_deselect_all_visible(C, CTX_data_active_object(C), SEL_DESELECT, false);
2121                 }
2122         }
2123         else if (BKE_paint_select_vert_test(obact)) {
2124                 retval = ed_wpaint_vertex_select_pick(C, location, extend, deselect, toggle, obact);
2125                 if (!retval && deselect_all) {
2126                         retval = paintvert_deselect_all_visible(obact, SEL_DESELECT, false);
2127                 }
2128         }
2129         else {
2130                 retval = ed_object_select_pick(C, location, extend, deselect, toggle, center, enumerate, object);
2131                 if (!retval && deselect_all) {
2132                         if (ED_pose_object_from_context(C)) {
2133                                 retval = ED_pose_deselect_all_multi(C, SEL_DESELECT, false);
2134                         }
2135                         else {
2136                                 retval = ED_object_base_deselect_all(CTX_data_view_layer(C), CTX_wm_view3d(C), SEL_DESELECT);
2137                                 DEG_id_tag_update(&scene->id, ID_RECALC_SELECT);
2138                         }
2139                 }
2140         }
2141
2142         /* passthrough allows tweaks
2143          * FINISHED to signal one operator worked
2144          * */
2145         if (retval) {
2146                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2147                 return OPERATOR_PASS_THROUGH | OPERATOR_FINISHED;
2148         }
2149         else {
2150                 return OPERATOR_PASS_THROUGH;  /* nothing selected, just passthrough */
2151         }
2152 }
2153
2154 static int view3d_select_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2155 {
2156         RNA_int_set_array(op->ptr, "location", event->mval);
2157
2158         return view3d_select_exec(C, op);
2159 }
2160
2161 void VIEW3D_OT_select(wmOperatorType *ot)
2162 {
2163         PropertyRNA *prop;
2164
2165         /* identifiers */
2166         ot->name = "Select";
2167         ot->description = "Select and activate item(s)";
2168         ot->idname = "VIEW3D_OT_select";
2169
2170         /* api callbacks */
2171         ot->invoke = view3d_select_invoke;
2172         ot->exec = view3d_select_exec;
2173         ot->poll = ED_operator_view3d_active;
2174
2175         /* flags */
2176         ot->flag = OPTYPE_UNDO;
2177
2178         /* properties */
2179         WM_operator_properties_mouse_select(ot);
2180         prop = RNA_def_boolean(ot->srna, "deselect_all", 0, "Deselect", "Deselect all when nothing under the cursor");
2181         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2182
2183         prop = RNA_def_boolean(ot->srna, "center", 0, "Center", "Use the object center when selecting, in editmode used to extend object selection");
2184         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2185         prop = RNA_def_boolean(ot->srna, "enumerate", 0, "Enumerate", "List objects under the mouse (object mode only)");
2186         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2187         prop = RNA_def_boolean(ot->srna, "object", 0, "Object", "Use object selection (editmode only)");
2188         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2189
2190         prop = RNA_def_int_vector(ot->srna, "location", 2, NULL, INT_MIN, INT_MAX, "Location", "Mouse location", INT_MIN, INT_MAX);
2191         RNA_def_property_flag(prop, PROP_HIDDEN);
2192 }
2193
2194 /** \} */
2195
2196 /* -------------------------------------------------------------------- */
2197 /** \name Box Select
2198  * \{ */
2199
2200 typedef struct BoxSelectUserData {
2201         ViewContext *vc;
2202         const rcti *rect;
2203         const rctf *rect_fl;
2204         rctf       _rect_fl;
2205         eSelectOp sel_op;
2206
2207         /* runtime */
2208         bool is_done;
2209         bool is_changed;
2210 } BoxSelectUserData;
2211
2212 static void view3d_userdata_boxselect_init(
2213         BoxSelectUserData *r_data,
2214         ViewContext *vc, const rcti *rect, const eSelectOp sel_op)
2215 {
2216         r_data->vc = vc;
2217
2218         r_data->rect = rect;
2219         r_data->rect_fl = &r_data->_rect_fl;
2220         BLI_rctf_rcti_copy(&r_data->_rect_fl, rect);
2221
2222         r_data->sel_op = sel_op;
2223
2224         /* runtime */
2225         r_data->is_done = false;
2226         r_data->is_changed = false;
2227 }
2228
2229 bool edge_inside_circle(const float cent[2], float radius, const float screen_co_a[2], const float screen_co_b[2])
2230 {
2231         const float radius_squared = radius * radius;
2232         return (dist_squared_to_line_segment_v2(cent, screen_co_a, screen_co_b) < radius_squared);
2233 }
2234
2235 static void do_paintvert_box_select__doSelectVert(void *userData, MVert *mv, const float screen_co[2], int UNUSED(index))
2236 {
2237         BoxSelectUserData *data = userData;
2238         const bool is_select = mv->flag & SELECT;
2239         const bool is_inside = BLI_rctf_isect_pt_v(data->rect_fl, screen_co);
2240         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
2241         if (sel_op_result != -1) {
2242                 SET_FLAG_FROM_TEST(mv->flag, sel_op_result, SELECT);
2243                 data->is_changed = true;
2244         }
2245 }
2246 static bool do_paintvert_box_select(
2247         ViewContext *vc, const rcti *rect, const eSelectOp sel_op)
2248 {
2249         const bool use_zbuf = !XRAY_ENABLED(vc->v3d);
2250         Mesh *me;
2251         MVert *mvert;
2252         unsigned int *rt;
2253         int a, index;
2254         char *selar;
2255
2256         me = vc->obact->data;
2257         if ((me == NULL) || (me->totvert == 0) || BLI_rcti_is_empty(rect)) {
2258                 return OPERATOR_CANCELLED;
2259         }
2260
2261         bool changed = false;
2262         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
2263                 changed |= paintvert_deselect_all_visible(vc->obact, SEL_DESELECT, false);
2264         }
2265
2266         if (use_zbuf) {
2267                 selar = MEM_callocN(me->totvert + 1, "selar");
2268
2269                 uint buf_len;
2270                 uint *buf = ED_view3d_select_id_read_rect(vc, rect, &buf_len);
2271
2272                 rt = buf;
2273
2274                 a = buf_len;
2275                 while (a--) {
2276                         if (*rt) {
2277                                 index = *rt;
2278                                 if (index <= me->totvert) {
2279                                         selar[index] = 1;
2280                                 }
2281                         }
2282                         rt++;
2283                 }
2284
2285                 mvert = me->mvert;
2286                 for (a = 1; a <= me->totvert; a++, mvert++) {
2287                         if ((mvert->flag & ME_HIDE) == 0) {
2288                                 const bool is_select = mvert->flag & SELECT;
2289                                 const bool is_inside = (selar[a] != 0);
2290                                 const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside);
2291                                 if (sel_op_result != -1) {
2292                                         SET_FLAG_FROM_TEST(mvert->flag, sel_op_result, SELECT);
2293                                         changed = true;
2294                                 }
2295                         }
2296                 }
2297
2298                 MEM_freeN(buf);
2299                 MEM_freeN(selar);
2300
2301 #ifdef __APPLE__
2302                 glReadBuffer(GL_BACK);
2303 #endif
2304         }
2305         else {
2306                 BoxSelectUserData data;
2307
2308                 view3d_userdata_boxselect_init(&data, vc, rect, sel_op);
2309
2310                 ED_view3d_init_mats_rv3d(vc->obact, vc->rv3d);
2311
2312                 meshobject_foreachScreenVert(vc, do_paintvert_box_select__doSelectVert, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
2313                 changed |= data.is_changed;
2314         }
2315
2316         if (changed) {
2317                 if (SEL_OP_CAN_DESELECT(sel_op)) {
2318                         BKE_mesh_mselect_validate(me);
2319                 }
2320                 paintvert_flush_flags(vc->obact);
2321                 paintvert_tag_select_update(vc->C, vc->obact);
2322         }
2323         return changed;
2324 }
2325
2326 static void do_nurbs_box_select__doSelect(
2327         void *userData, Nurb *UNUSED(nu), BPoint *bp, BezTriple *bezt, int beztindex, const float screen_co[2])
2328 {
2329         BoxSelectUserData *data = userData;
2330
2331         const bool is_inside = BLI_rctf_isect_pt_v(data->rect_fl, screen_co);
2332         if (bp) {
2333                 const bool is_select = bp->f1 & SELECT;
2334                 const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
2335                 if (sel_op_result != -1) {
2336                         SET_FLAG_FROM_TEST(bp->f1, sel_op_result, SELECT);
2337                         data->is_changed = true;
2338                 }
2339         }
2340         else {
2341                 if ((data->vc->v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_CU_HANDLES) == 0) {
2342                         /* can only be (beztindex == 0) here since handles are hidden */
2343                         const bool is_select = bezt->f2 & SELECT;
2344                         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
2345                         if (sel_op_result != -1) {
2346                                 SET_FLAG_FROM_TEST(bezt->f2, sel_op_result, SELECT);
2347                                 data->is_changed = true;
2348                         }
2349                         bezt->f1 = bezt->f3 = bezt->f2;
2350                 }
2351                 else {
2352                         char *flag_p = (&bezt->f1) + beztindex;
2353                         const bool is_select = *flag_p & SELECT;
2354                         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
2355                         if (sel_op_result != -1) {
2356                                 SET_FLAG_FROM_TEST(*flag_p, sel_op_result, SELECT);
2357                                 data->is_changed = true;
2358                         }
2359                 }
2360         }
2361 }
2362 static bool do_nurbs_box_select(ViewContext *vc, rcti *rect, const eSelectOp sel_op)
2363 {
2364         BoxSelectUserData data;
2365
2366         view3d_userdata_boxselect_init(&data, vc, rect, sel_op);
2367
2368         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
2369                 Curve *curve = (Curve *) vc->obedit->data;
2370                 data.is_changed |= ED_curve_deselect_all(curve->editnurb);
2371         }
2372
2373         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
2374         nurbs_foreachScreenVert(vc, do_nurbs_box_select__doSelect, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
2375         BKE_curve_nurb_vert_active_validate(vc->obedit->data);
2376
2377         return data.is_changed;
2378 }
2379
2380 static void do_lattice_box_select__doSelect(void *userData, BPoint *bp, const float screen_co[2])
2381 {
2382         BoxSelectUserData *data = userData;
2383         const bool is_select = bp->f1 & SELECT;
2384         const bool is_inside = BLI_rctf_isect_pt_v(data->rect_fl, screen_co);
2385         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
2386         if (sel_op_result != -1) {
2387                 SET_FLAG_FROM_TEST(bp->f1, sel_op_result, SELECT);
2388                 data->is_changed = true;
2389         }
2390 }
2391 static bool do_lattice_box_select(ViewContext *vc, rcti *rect, const eSelectOp sel_op)
2392 {
2393         BoxSelectUserData data;
2394
2395         view3d_userdata_boxselect_init(&data, vc, rect, sel_op);
2396
2397         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
2398                 data.is_changed |= ED_lattice_flags_set(vc->obedit, 0);
2399         }
2400
2401         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
2402         lattice_foreachScreenVert(vc, do_lattice_box_select__doSelect, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
2403
2404         return data.is_changed;
2405 }
2406
2407 static void do_mesh_box_select__doSelectVert(void *userData, BMVert *eve, const float screen_co[2], int UNUSED(index))
2408 {
2409         BoxSelectUserData *data = userData;
2410         const bool is_select = BM_elem_flag_test(eve, BM_ELEM_SELECT);
2411         const bool is_inside = BLI_rctf_isect_pt_v(data->rect_fl, screen_co);
2412         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
2413         if (sel_op_result != -1) {
2414                 BM_vert_select_set(data->vc->em->bm, eve, sel_op_result);
2415                 data->is_changed = true;
2416         }
2417 }
2418 static void do_mesh_box_select__doSelectEdge_pass0(
2419         void *userData, BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2], int index)
2420 {
2421         BoxSelectUserData *data = userData;
2422         const bool is_select = BM_elem_flag_test(eed, BM_ELEM_SELECT);
2423         const bool is_inside = (
2424                 EDBM_backbuf_check(bm_solidoffs + index) &&
2425                 edge_fully_inside_rect(data->rect_fl, screen_co_a, screen_co_b));
2426         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
2427         if (sel_op_result != -1) {
2428                 BM_edge_select_set(data->vc->em->bm, eed, sel_op_result);
2429                 data->is_done = true;
2430                 data->is_changed = true;
2431         }
2432 }
2433 static void do_mesh_box_select__doSelectEdge_pass1(
2434         void *userData, BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2], int index)
2435 {
2436         BoxSelectUserData *data = userData;
2437         const bool is_select = BM_elem_flag_test(eed, BM_ELEM_SELECT);
2438         const bool is_inside = (
2439                 EDBM_backbuf_check(bm_solidoffs + index) &&
2440                 edge_inside_rect(data->rect_fl, screen_co_a, screen_co_b));
2441         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
2442         if (sel_op_result != -1) {
2443                 BM_edge_select_set(data->vc->em->bm, eed, sel_op_result);
2444                 data->is_changed = true;
2445         }
2446 }
2447 static void do_mesh_box_select__doSelectFace(void *userData, BMFace *efa, const float screen_co[2], int UNUSED(index))
2448 {
2449         BoxSelectUserData *data = userData;
2450         const bool is_select = BM_elem_flag_test(efa, BM_ELEM_SELECT);
2451         const bool is_inside = BLI_rctf_isect_pt_v(data->rect_fl, screen_co);
2452         const int sel_op_result = ED_select_op_action_deselected(data->sel_op, is_select, is_inside);
2453         if (sel_op_result != -1) {
2454                 BM_face_select_set(data->vc->em->bm, efa, sel_op_result);
2455                 data->is_changed = true;
2456         }
2457 }
2458 static bool do_mesh_box_select(
2459         ViewContext *vc, rcti *rect, const eSelectOp sel_op)
2460 {
2461         BoxSelectUserData data;
2462         ToolSettings *ts = vc->scene->toolsettings;
2463         int bbsel;
2464
2465         view3d_userdata_boxselect_init(&data, vc, rect, sel_op);
2466
2467         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
2468                 if (vc->em->bm->totvertsel) {
2469                         EDBM_flag_disable_all(vc->em, BM_ELEM_SELECT);
2470                         data.is_changed = true;
2471                 }
2472         }
2473
2474         /* for non zbuf projections, don't change the GL state */
2475         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
2476
2477         GPU_matrix_set(vc->rv3d->viewmat);
2478         bbsel = EDBM_backbuf_border_init(vc, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
2479
2480         if (ts->selectmode & SCE_SELECT_VERTEX) {
2481                 if (bbsel) {
2482                         data.is_changed = edbm_backbuf_check_and_select_verts(vc->em, sel_op);
2483                 }
2484                 else {
2485                         mesh_foreachScreenVert(vc, do_mesh_box_select__doSelectVert, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
2486                 }
2487         }
2488         if (ts->selectmode & SCE_SELECT_EDGE) {
2489                 /* Does both bbsel and non-bbsel versions (need screen cos for both) */
2490                 mesh_foreachScreenEdge(vc, do_mesh_box_select__doSelectEdge_pass0, &data, V3D_PROJ_TEST_CLIP_NEAR);
2491                 if (data.is_done == false) {
2492                         mesh_foreachScreenEdge(vc, do_mesh_box_select__doSelectEdge_pass1, &data, V3D_PROJ_TEST_CLIP_NEAR);
2493                 }
2494         }
2495
2496         if (ts->selectmode & SCE_SELECT_FACE) {
2497                 if (bbsel) {
2498                         data.is_changed = edbm_backbuf_check_and_select_faces(vc->em, sel_op);
2499                 }
2500                 else {
2501                         mesh_foreachScreenFace(vc, do_mesh_box_select__doSelectFace, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
2502                 }
2503         }
2504
2505         EDBM_backbuf_free();
2506
2507         if (data.is_changed) {
2508                 EDBM_selectmode_flush(vc->em);
2509         }
2510         return data.is_changed;
2511 }
2512
2513 static bool do_meta_box_select(
2514         ViewContext *vc,
2515         const rcti *rect, const eSelectOp sel_op)
2516 {
2517         Object *ob = vc->obedit;
2518         MetaBall *mb = (MetaBall *)ob->data;
2519         MetaElem *ml;
2520         int a;
2521         bool changed = false;
2522
2523         uint buffer[MAXPICKBUF];
2524         int hits;
2525
2526         hits = view3d_opengl_select(
2527                 vc, buffer, MAXPICKBUF, rect,
2528                 VIEW3D_SELECT_ALL, VIEW3D_SELECT_FILTER_NOP);
2529
2530         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
2531                 changed |= BKE_mball_deselect_all(mb);
2532         }
2533
2534         int metaelem_id = 0;
2535         for (ml = mb->editelems->first; ml; ml = ml->next, metaelem_id += 0x10000) {
2536                 bool is_inside_radius = false;
2537                 bool is_inside_stiff = false;
2538
2539                 for (a = 0; a < hits; a++) {
2540                         int hitresult = buffer[(4 * a) + 3];
2541
2542                         if (hitresult == -1) {
2543                                 continue;
2544                         }
2545                         else if (hitresult & MBALL_NOSEL) {
2546                                 continue;
2547                         }
2548
2549                         const uint hit_object = hitresult & 0xFFFF;
2550                         if (vc->obedit->select_id != hit_object) {
2551                                 continue;
2552                         }
2553
2554                         if (metaelem_id != (hitresult & 0xFFFF0000 & ~(MBALLSEL_ANY))) {
2555                                 continue;
2556                         }
2557
2558                         if (hitresult & MBALLSEL_RADIUS) {
2559                                 is_inside_radius = true;
2560                                 break;
2561                         }
2562
2563                         if (hitresult & MBALLSEL_STIFF) {
2564                                 is_inside_stiff = true;
2565                                 break;
2566                         }
2567                 }
2568                 const int flag_prev = ml->flag;
2569                 if (is_inside_radius) {
2570                         ml->flag |= MB_SCALE_RAD;
2571                 }
2572                 if (is_inside_stiff) {
2573                         ml->flag &= ~MB_SCALE_RAD;
2574                 }
2575
2576                 const bool is_select = (ml->flag & SELECT);
2577                 const bool is_inside = is_inside_radius || is_inside_stiff;
2578
2579                 const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside);
2580                 if (sel_op_result != -1) {
2581                         SET_FLAG_FROM_TEST(ml->flag, sel_op_result, SELECT);
2582                 }
2583                 changed |= (flag_prev != ml->flag);
2584         }
2585
2586         return changed;
2587 }
2588
2589 static bool do_armature_box_select(
2590         ViewContext *vc,
2591         const rcti *rect, const eSelectOp sel_op)
2592 {
2593         bool changed = false;
2594         int a;
2595
2596         uint buffer[MAXPICKBUF];
2597         int hits;
2598
2599         hits = view3d_opengl_select(
2600                 vc, buffer, MAXPICKBUF, rect,
2601                 VIEW3D_SELECT_ALL, VIEW3D_SELECT_FILTER_NOP);
2602
2603         uint bases_len = 0;
2604         Base **bases = BKE_view_layer_array_from_bases_in_edit_mode_unique_data(vc->view_layer, vc->v3d, &bases_len);
2605
2606         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
2607                 changed |= ED_armature_edit_deselect_all_visible_multi_ex(bases, bases_len);
2608         }
2609
2610         for (uint base_index = 0; base_index < bases_len; base_index++) {
2611                 Object *obedit = bases[base_index]->object;
2612                 obedit->id.tag &= ~LIB_TAG_DOIT;
2613
2614                 bArmature *arm = obedit->data;
2615                 ED_armature_ebone_listbase_temp_clear(arm->edbo);
2616         }
2617
2618         /* first we only check points inside the border */
2619         for (a = 0; a < hits; a++) {
2620                 int select_id = buffer[(4 * a) + 3];
2621                 if (select_id != -1) {
2622                         if ((select_id & 0xFFFF0000) == 0) {
2623                                 continue;
2624                         }
2625
2626                         EditBone *ebone;
2627                         Base *base_edit = ED_armature_base_and_ebone_from_select_buffer(bases, bases_len, select_id, &ebone);
2628                         ebone->temp.i |= select_id & BONESEL_ANY;
2629                         base_edit->object->id.tag |= LIB_TAG_DOIT;
2630                 }
2631         }
2632
2633         for (uint base_index = 0; base_index < bases_len; base_index++) {
2634                 Object *obedit = bases[base_index]->object;
2635                 if (obedit->id.tag & LIB_TAG_DOIT) {
2636                         obedit->id.tag &= ~LIB_TAG_DOIT;
2637                         changed |= ED_armature_edit_select_op_from_tagged(obedit->data, sel_op);
2638                 }
2639         }
2640
2641         MEM_freeN(bases);
2642
2643         return changed;
2644 }
2645
2646 /**
2647  * Compare result of 'GPU_select': 'uint[4]',
2648  * needed for when we need to align with object draw-order.
2649  */
2650 static int opengl_bone_select_buffer_cmp(const void *sel_a_p, const void *sel_b_p)
2651 {
2652         /* 4th element is select id */
2653         uint sel_a = ((uint *)sel_a_p)[3];
2654         uint sel_b = ((uint *)sel_b_p)[3];
2655
2656 #ifdef __BIG_ENDIAN__
2657         BLI_endian_switch_uint32(&sel_a);
2658         BLI_endian_switch_uint32(&sel_b);
2659 #endif
2660
2661         if (sel_a < sel_b) {
2662                 return -1;
2663         }
2664         else if (sel_a > sel_b) {
2665                 return 1;
2666         }
2667         else {
2668                 return 0;
2669         }
2670 }
2671
2672 static bool do_object_box_select(bContext *C, ViewContext *vc, rcti *rect, const eSelectOp sel_op)
2673 {
2674         View3D *v3d = vc->v3d;
2675         int totobj = MAXPICKBUF; /* XXX solve later */
2676
2677         /* selection buffer now has bones potentially too, so we add MAXPICKBUF */
2678         uint *vbuffer = MEM_mallocN(4 * (totobj + MAXPICKELEMS) * sizeof(uint[4]), "selection buffer");
2679         const eV3DSelectObjectFilter select_filter = ED_view3d_select_filter_from_mode(vc->scene, vc->obact);
2680         const int hits = view3d_opengl_select(
2681                 vc, vbuffer, 4 * (totobj + MAXPICKELEMS), rect,
2682                 VIEW3D_SELECT_ALL, select_filter);
2683
2684         for (Base *base = vc->view_layer->object_bases.first; base; base = base->next) {
2685                 base->object->id.tag &= ~LIB_TAG_DOIT;
2686         }
2687
2688         Base **bases = NULL;
2689         BLI_array_declare(bases);
2690
2691         bool changed = false;
2692         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
2693                 changed |= object_deselect_all_visible(vc->view_layer, vc->v3d);
2694         }
2695
2696         if ((hits == -1) && !SEL_OP_USE_OUTSIDE(sel_op)) {
2697                 goto finally;
2698         }
2699
2700         for (Base *base = vc->view_layer->object_bases.first; base; base = base->next) {
2701                 if (BASE_SELECTABLE(v3d, base)) {
2702                         if ((base->object->select_id & 0x0000FFFF) != 0) {
2703                                 BLI_array_append(bases, base);
2704                         }
2705                 }
2706         }
2707
2708         /* The draw order doesn't always match the order we populate the engine, see: T51695. */
2709         qsort(vbuffer, hits, sizeof(uint[4]), opengl_bone_select_buffer_cmp);
2710
2711         for (const uint *col = vbuffer + 3, *col_end = col + (hits * 4); col < col_end; col += 4) {
2712                 Bone *bone;
2713                 Base *base = ED_armature_base_and_bone_from_select_buffer(bases, BLI_array_len(bases), *col, &bone);
2714                 if (base != NULL) {
2715                         base->object->id.tag |= LIB_TAG_DOIT;
2716                 }
2717         }
2718
2719         for (Base *base = vc->view_layer->object_bases.first; base && hits; base = base->next) {
2720                 if (BASE_SELECTABLE(v3d, base)) {
2721                         const bool is_select = base->flag & BASE_SELECTED;
2722                         const bool is_inside = base->object->id.tag & LIB_TAG_DOIT;
2723                         const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside);
2724                         if (sel_op_result != -1) {
2725                                 ED_object_base_select(base, sel_op_result ? BA_SELECT : BA_DESELECT);
2726                                 changed = true;
2727                         }
2728                 }
2729         }
2730
2731 finally:
2732         if (bases != NULL) {
2733                 MEM_freeN(bases);
2734         }
2735
2736         MEM_freeN(vbuffer);
2737
2738         if (changed) {
2739                 DEG_id_tag_update(&vc->scene->id, ID_RECALC_SELECT);
2740                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, vc->scene);
2741         }
2742         return changed;
2743 }
2744
2745 static bool do_pose_box_select(bContext *C, ViewContext *vc, rcti *rect, const eSelectOp sel_op)
2746 {
2747         uint bases_len;
2748         Base **bases = do_pose_tag_select_op_prepare(vc, &bases_len);
2749
2750         int totobj = MAXPICKBUF; /* XXX solve later */
2751
2752         /* selection buffer now has bones potentially too, so we add MAXPICKBUF */
2753         uint *vbuffer = MEM_mallocN((totobj + MAXPICKELEMS) * sizeof(uint[4]), "selection buffer");
2754         const eV3DSelectObjectFilter select_filter = ED_view3d_select_filter_from_mode(vc->scene, vc->obact);
2755         const int hits = view3d_opengl_select(
2756                 vc, vbuffer, 4 * (totobj + MAXPICKELEMS), rect,
2757                 VIEW3D_SELECT_ALL, select_filter);
2758         /*
2759          * LOGIC NOTES (theeth):
2760          * The buffer and ListBase have the same relative order, which makes the selection
2761          * very simple. Loop through both data sets at the same time, if the color
2762          * is the same as the object, we have a hit and can move to the next color
2763          * and object pair, if not, just move to the next object,
2764          * keeping the same color until we have a hit.
2765          */
2766
2767         if (hits > 0) {
2768                 /* no need to loop if there's no hit */
2769
2770                 /* The draw order doesn't always match the order we populate the engine, see: T51695. */
2771                 qsort(vbuffer, hits, sizeof(uint[4]), opengl_bone_select_buffer_cmp);
2772
2773                 for (const uint *col = vbuffer + 3, *col_end = col + (hits * 4); col < col_end; col += 4) {
2774                         Bone *bone;
2775                         Base *base = ED_armature_base_and_bone_from_select_buffer(bases, bases_len, *col, &bone);
2776
2777                         if (base == NULL) {
2778                                 continue;
2779                         }
2780
2781                         /* Loop over contiguous bone hits for 'base'. */
2782                         for (; col != col_end; col += 4) {
2783                                 /* should never fail */
2784                                 if (bone != NULL) {
2785                                         base->object->id.tag |= LIB_TAG_DOIT;
2786                                         bone->flag |= BONE_DONE;
2787                                 }
2788
2789                                 /* Select the next bone if we're not switching bases. */
2790                                 if (col + 4 != col_end) {
2791                                         if ((base->object->select_id & 0x0000FFFF) != (col[4] & 0x0000FFFF)) {
2792                                                 break;
2793                                         }
2794                                         if (base->object->pose != NULL) {
2795                                                 const uint hit_bone = (col[4] & ~BONESEL_ANY) >> 16;
2796                                                 bPoseChannel *pchan = BLI_findlink(&base->object->pose->chanbase, hit_bone);
2797                                                 bone = pchan ? pchan->bone : NULL;
2798                                         }
2799                                         else {
2800                                                 bone = NULL;
2801                                         }
2802                                 }
2803                         }
2804                 }
2805         }
2806
2807         const bool changed_multi = do_pose_tag_select_op_exec(bases, bases_len, sel_op);
2808         if (changed_multi) {
2809                 DEG_id_tag_update(&vc->scene->id, ID_RECALC_SELECT);
2810                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, vc->scene);
2811         }
2812
2813         if (bases != NULL) {
2814                 MEM_freeN(bases);
2815         }
2816         MEM_freeN(vbuffer);
2817
2818         return changed_multi;
2819 }
2820
2821 static int view3d_box_select_exec(bContext *C, wmOperator *op)
2822 {
2823         ViewContext vc;
2824         rcti rect;
2825         bool changed_multi = false;
2826
2827         view3d_operator_needs_opengl(C);
2828
2829         /* setup view context for argument to callbacks */
2830         ED_view3d_viewcontext_init(C, &vc);
2831
2832         eSelectOp sel_op = RNA_enum_get(op->ptr, "mode");
2833         WM_operator_properties_border_to_rcti(op, &rect);
2834
2835         if (vc.obedit) {
2836
2837                 FOREACH_OBJECT_IN_MODE_BEGIN (vc.view_layer, vc.v3d, vc.obedit->type, vc.obedit->mode, ob_iter) {
2838                         ED_view3d_viewcontext_init_object(&vc, ob_iter);
2839                         bool changed = false;
2840
2841                         switch (vc.obedit->type) {
2842                                 case OB_MESH:
2843                                         vc.em = BKE_editmesh_from_object(vc.obedit);
2844                                         changed = do_mesh_box_select(&vc, &rect, sel_op);
2845                                         if (changed) {
2846                                                 DEG_id_tag_update(vc.obedit->data, ID_RECALC_SELECT);
2847                                                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
2848                                         }
2849                                         break;
2850                                 case OB_CURVE:
2851                                 case OB_SURF:
2852                                         changed =  do_nurbs_box_select(&vc, &rect, sel_op);
2853                                         if (changed) {
2854                                                 DEG_id_tag_update(vc.obedit->data, ID_RECALC_SELECT);
2855                                                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
2856                                         }
2857                                         break;
2858                                 case OB_MBALL:
2859                                         changed = do_meta_box_select(&vc, &rect, sel_op);
2860                                         if (changed) {
2861                                                 DEG_id_tag_update(vc.obedit->data, ID_RECALC_SELECT);
2862                                                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
2863                                         }
2864                                         break;
2865                                 case OB_ARMATURE:
2866                                         changed = do_armature_box_select(&vc, &rect, sel_op);
2867                                         if (changed) {
2868                                                 DEG_id_tag_update(&vc.obedit->id, ID_RECALC_SELECT);
2869                                                 WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, vc.obedit);
2870                                         }
2871                                         break;
2872                                 case OB_LATTICE:
2873                                         changed = do_lattice_box_select(&vc, &rect, sel_op);
2874                                         if (changed) {
2875                                                 DEG_id_tag_update(vc.obedit->data, ID_RECALC_SELECT);
2876                                                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
2877                                         }
2878                                         break;
2879                                 default:
2880                                         assert(!"box select on incorrect object type");
2881                                         break;
2882                         }
2883                         changed_multi |= changed;
2884                 }
2885                 FOREACH_OBJECT_IN_MODE_END;
2886         }
2887         else {  /* no editmode, unified for bones and objects */
2888                 if (vc.obact && vc.obact->mode & OB_MODE_SCULPT) {
2889                         /* XXX, this is not selection, could be it's own operator. */
2890                         changed_multi = ED_sculpt_mask_box_select(C, &vc, &rect, sel_op == SEL_OP_ADD ? true : false);
2891                 }
2892                 else if (vc.obact && BKE_paint_select_face_test(vc.obact)) {
2893                         changed_multi = do_paintface_box_select(&vc, &rect, sel_op);
2894                 }
2895                 else if (vc.obact && BKE_paint_select_vert_test(vc.obact)) {
2896                         changed_multi = do_paintvert_box_select(&vc, &rect, sel_op);
2897                 }
2898                 else if (vc.obact && vc.obact->mode & OB_MODE_PARTICLE_EDIT) {
2899                         changed_multi = PE_box_select(C, &rect, sel_op);
2900                 }
2901                 else if (vc.obact && vc.obact->mode & OB_MODE_POSE) {
2902                         changed_multi = do_pose_box_select(C, &vc, &rect, sel_op);
2903                 }
2904                 else { /* object mode with none active */
2905                         changed_multi = do_object_box_select(C, &vc, &rect, sel_op);
2906                 }
2907         }
2908
2909         if (changed_multi) {
2910                 return OPERATOR_FINISHED;
2911         }
2912         else {
2913                 return OPERATOR_CANCELLED;
2914         }
2915 }
2916
2917 void VIEW3D_OT_select_box(wmOperatorType *ot)
2918 {
2919         /* identifiers */
2920         ot->name = "Box Select";
2921         ot->description = "Select items using box selection";
2922         ot->idname = "VIEW3D_OT_select_box";
2923
2924         /* api callbacks */
2925         ot->invoke = WM_gesture_box_invoke;
2926         ot->exec = view3d_box_select_exec;
2927         ot->modal = WM_gesture_box_modal;
2928         ot->poll = view3d_selectable_data;
2929         ot->cancel = WM_gesture_box_cancel;
2930
2931         /* flags */
2932         ot->flag = OPTYPE_UNDO;
2933
2934         /* rna */
2935         WM_operator_properties_gesture_box(ot);
2936         WM_operator_properties_select_operation(ot);
2937 }
2938
2939 /** \} */
2940
2941 /* -------------------------------------------------------------------- */
2942 /** \name Circle Select
2943  * \{ */
2944
2945 typedef struct CircleSelectUserData {
2946         ViewContext *vc;
2947         bool select;
2948         int   mval[2];
2949         float mval_fl[2];
2950         float radius;
2951         float radius_squared;
2952
2953         /* runtime */
2954         bool is_changed;
2955 } CircleSelectUserData;
2956
2957 static void view3d_userdata_circleselect_init(
2958         CircleSelectUserData *r_data,
2959         ViewContext *vc, const bool select, const int mval[2], const float rad)
2960 {
2961         r_data->vc = vc;
2962         r_data->select = select;
2963         copy_v2_v2_int(r_data->mval, mval);
2964         r_data->mval_fl[0] = mval[0];
2965         r_data->mval_fl[1] = mval[1];
2966
2967         r_data->radius = rad;
2968         r_data->radius_squared = rad * rad;
2969
2970         /* runtime */
2971         r_data->is_changed = false;
2972 }
2973
2974 static void mesh_circle_doSelectVert(void *userData, BMVert *eve, const float screen_co[2], int UNUSED(index))
2975 {
2976         CircleSelectUserData *data = userData;
2977
2978         if (len_squared_v2v2(data->mval_fl, screen_co) <= data->radius_squared) {
2979                 BM_vert_select_set(data->vc->em->bm, eve, data->select);
2980                 data->is_changed = true;
2981         }
2982 }
2983 static void mesh_circle_doSelectEdge(
2984         void *userData, BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2], int UNUSED(index))
2985 {
2986         CircleSelectUserData *data = userData;
2987
2988         if (edge_inside_circle(data->mval_fl, data->radius, screen_co_a, screen_co_b)) {
2989                 BM_edge_select_set(data->vc->em->bm, eed, data->select);
2990                 data->is_changed = true;
2991         }
2992 }
2993 static void mesh_circle_doSelectFace(void *userData, BMFace *efa, const float screen_co[2], int UNUSED(index))
2994 {
2995         CircleSelectUserData *data = userData;
2996
2997         if (len_squared_v2v2(data->mval_fl, screen_co) <= data->radius_squared) {
2998                 BM_face_select_set(data->vc->em->bm, efa, data->select);
2999                 data->is_changed = true;
3000         }
3001 }
3002
3003 static bool mesh_circle_select(ViewContext *vc, eSelectOp sel_op, const int mval[2], float rad)
3004 {
3005         ToolSettings *ts = vc->scene->toolsettings;
3006         int bbsel;
3007         CircleSelectUserData data;
3008         vc->em = BKE_editmesh_from_object(vc->obedit);
3009
3010         bool changed = false;
3011         if (SEL_OP_USE_PRE_DESELECT(sel_op)) {
3012                 if (vc->em->bm->totvertsel) {
3013                         EDBM_flag_disable_all(vc->em, BM_ELEM_SELECT);
3014                         changed = true;