svn merge ^/trunk/blender -r46979:47003
[blender.git] / source / blender / editors / space_view3d / view3d_select.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_select.c
28  *  \ingroup spview3d
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <math.h>
35 #include <float.h>
36 #include <assert.h>
37
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_meta_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_tracking_types.h"
47
48 #include "MEM_guardedalloc.h"
49
50 #include "BLI_math.h"
51 #include "BLI_lasso.h"
52 #include "BLI_rect.h"
53 #include "BLI_rand.h"
54 #include "BLI_linklist.h"
55 #include "BLI_listbase.h"
56 #include "BLI_string.h"
57 #include "BLI_utildefines.h"
58
59 /* vertex box select */
60 #include "IMB_imbuf_types.h"
61 #include "IMB_imbuf.h"
62 #include "BKE_global.h"
63
64 #include "BKE_context.h"
65 #include "BKE_paint.h"
66 #include "BKE_armature.h"
67 #include "BKE_tessmesh.h"
68 #include "BKE_movieclip.h"
69 #include "BKE_object.h"
70 #include "BKE_tracking.h"
71
72
73 #include "BIF_gl.h"
74 #include "BIF_glutil.h"
75
76 #include "WM_api.h"
77 #include "WM_types.h"
78
79 #include "RNA_access.h"
80 #include "RNA_define.h"
81 #include "RNA_enum_types.h"
82
83 #include "ED_armature.h"
84 #include "ED_curve.h"
85 #include "ED_particle.h"
86 #include "ED_mesh.h"
87 #include "ED_object.h"
88 #include "ED_screen.h"
89 #include "ED_mball.h"
90
91 #include "UI_interface.h"
92 #include "UI_resources.h"
93
94 #include "view3d_intern.h"  // own include
95
96 // TODO: should return whether there is valid context to continue
97 void view3d_set_viewcontext(bContext *C, ViewContext *vc)
98 {
99         memset(vc, 0, sizeof(ViewContext));
100         vc->ar = CTX_wm_region(C);
101         vc->scene = CTX_data_scene(C);
102         vc->v3d = CTX_wm_view3d(C);
103         vc->rv3d = CTX_wm_region_view3d(C);
104         vc->obact = CTX_data_active_object(C);
105         vc->obedit = CTX_data_edit_object(C);
106 }
107
108 int view3d_get_view_aligned_coordinate(ViewContext *vc, float fp[3], const int mval[2], const short do_fallback)
109 {
110         float dvec[3];
111         int mval_cpy[2];
112
113         mval_cpy[0] = mval[0];
114         mval_cpy[1] = mval[1];
115
116         project_int_noclip(vc->ar, fp, mval_cpy);
117
118         initgrabz(vc->rv3d, fp[0], fp[1], fp[2]);
119
120         if (mval_cpy[0] != IS_CLIPPED) {
121                 float mval_f[2];
122                 VECSUB2D(mval_f, mval_cpy, mval);
123                 ED_view3d_win_to_delta(vc->ar, mval_f, dvec);
124                 sub_v3_v3(fp, dvec);
125
126                 return TRUE;
127         }
128         else {
129                 /* fallback to the view center */
130                 if (do_fallback) {
131                         negate_v3_v3(fp, vc->rv3d->ofs);
132                         return view3d_get_view_aligned_coordinate(vc, fp, mval, FALSE);
133                 }
134                 else {
135                         return FALSE;
136                 }
137         }
138 }
139
140 /*
141  * ob == NULL if you want global matrices
142  * */
143 void view3d_get_transformation(const ARegion *ar, RegionView3D *rv3d, Object *ob, bglMats *mats)
144 {
145         float cpy[4][4];
146         int i, j;
147
148         if (ob) {
149                 mult_m4_m4m4(cpy, rv3d->viewmat, ob->obmat);
150         }
151         else {
152                 copy_m4_m4(cpy, rv3d->viewmat);
153         }
154
155         for (i = 0; i < 4; ++i) {
156                 for (j = 0; j < 4; ++j) {
157                         mats->projection[i * 4 + j] = rv3d->winmat[i][j];
158                         mats->modelview[i * 4 + j] = cpy[i][j];
159                 }
160         }
161
162         mats->viewport[0] = ar->winrct.xmin;
163         mats->viewport[1] = ar->winrct.ymin;
164         mats->viewport[2] = ar->winx;
165         mats->viewport[3] = ar->winy;   
166 }
167
168 /* ********************** view3d_select: selection manipulations ********************* */
169
170 /* local prototypes */
171
172 static void edbm_backbuf_check_and_select_verts(BMEditMesh *em, int select)
173 {
174         BMVert *eve;
175         BMIter iter;
176         int index = bm_wireoffs;
177
178         eve = BM_iter_new(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
179         for (; eve; eve = BM_iter_step(&iter), index++) {
180                 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
181                         if (EDBM_backbuf_check(index)) {
182                                 BM_vert_select_set(em->bm, eve, select);
183                         }
184                 }
185         }
186 }
187
188 static void edbm_backbuf_check_and_select_edges(BMEditMesh *em, int select)
189 {
190         BMEdge *eed;
191         BMIter iter;
192         int index = bm_solidoffs;
193
194         eed = BM_iter_new(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
195         for (; eed; eed = BM_iter_step(&iter), index++) {
196                 if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
197                         if (EDBM_backbuf_check(index)) {
198                                 BM_edge_select_set(em->bm, eed, select);
199                         }
200                 }
201         }
202 }
203
204 static void edbm_backbuf_check_and_select_faces(BMEditMesh *em, int select)
205 {
206         BMFace *efa;
207         BMIter iter;
208         int index = 1;
209
210         efa = BM_iter_new(&iter, em->bm, BM_FACES_OF_MESH, NULL);
211         for (; efa; efa = BM_iter_step(&iter), index++) {
212                 if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
213                         if (EDBM_backbuf_check(index)) {
214                                 BM_face_select_set(em->bm, efa, select);
215                         }
216                 }
217         }
218 }
219
220
221 /* object mode, EM_ prefix is confusing here, rename? */
222 static void edbm_backbuf_check_and_select_verts_obmode(Mesh *me, int select)
223 {
224         MVert *mv = me->mvert;
225         int a;
226
227         if (mv) {
228                 for (a = 1; a <= me->totvert; a++, mv++) {
229                         if (EDBM_backbuf_check(a)) {
230                                 if (!(mv->flag & ME_HIDE)) {
231                                         mv->flag = select ? (mv->flag | SELECT) : (mv->flag & ~SELECT);
232                                 }
233                         }
234                 }
235         }
236 }
237 /* object mode, EM_ prefix is confusing here, rename? */
238
239 static void edbm_backbuf_check_and_select_tfaces(Mesh *me, int select)
240 {
241         MPoly *mpoly = me->mpoly;
242         int a;
243
244         if (mpoly) {
245                 for (a = 1; a <= me->totpoly; a++, mpoly++) {
246                         if (EDBM_backbuf_check(a)) {
247                                 mpoly->flag = select ? (mpoly->flag | ME_FACE_SEL) : (mpoly->flag & ~ME_FACE_SEL);
248                         }
249                 }
250         }
251 }
252
253 /* *********************** GESTURE AND LASSO ******************* */
254
255 typedef struct LassoSelectUserData {
256         ViewContext *vc;
257         rcti *rect;
258         int (*mcords)[2], moves, select, pass, done;
259 } LassoSelectUserData;
260
261 static int view3d_selectable_data(bContext *C)
262 {
263         Object *ob = CTX_data_active_object(C);
264
265         if (!ED_operator_region_view3d_active(C))
266                 return 0;
267
268         if (ob) {
269                 if (ob->mode & OB_MODE_EDIT) {
270                         if (ob->type == OB_FONT) {
271                                 return 0;
272                         }
273                 }
274                 else {
275                         if (ob->mode & OB_MODE_SCULPT) {
276                                 return 0;
277                         }
278                         if ((ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT)) &&
279                             !paint_facesel_test(ob) && !paint_vertsel_test(ob))
280                         {
281                                 return 0;
282                         }
283                 }
284         }
285
286         return 1;
287 }
288
289
290 /* helper also for borderselect */
291 static int edge_fully_inside_rect(rcti *rect, short x1, short y1, short x2, short y2)
292 {
293         return BLI_in_rcti(rect, x1, y1) && BLI_in_rcti(rect, x2, y2);
294 }
295
296 static int edge_inside_rect(rcti *rect, short x1, short y1, short x2, short y2)
297 {
298         int d1, d2, d3, d4;
299         
300         /* check points in rect */
301         if (edge_fully_inside_rect(rect, x1, y1, x2, y2)) return 1;
302         
303         /* check points completely out rect */
304         if (x1 < rect->xmin && x2 < rect->xmin) return 0;
305         if (x1 > rect->xmax && x2 > rect->xmax) return 0;
306         if (y1 < rect->ymin && y2 < rect->ymin) return 0;
307         if (y1 > rect->ymax && y2 > rect->ymax) return 0;
308         
309         /* simple check lines intersecting. */
310         d1 = (y1 - y2) * (x1 - rect->xmin) + (x2 - x1) * (y1 - rect->ymin);
311         d2 = (y1 - y2) * (x1 - rect->xmin) + (x2 - x1) * (y1 - rect->ymax);
312         d3 = (y1 - y2) * (x1 - rect->xmax) + (x2 - x1) * (y1 - rect->ymax);
313         d4 = (y1 - y2) * (x1 - rect->xmax) + (x2 - x1) * (y1 - rect->ymin);
314         
315         if (d1 < 0 && d2 < 0 && d3 < 0 && d4 < 0) return 0;
316         if (d1 > 0 && d2 > 0 && d3 > 0 && d4 > 0) return 0;
317         
318         return 1;
319 }
320
321
322 #define MOVES_GESTURE 50
323 #define MOVES_LASSO 500
324
325
326 /* warning; lasso select with backbuffer-check draws in backbuf with persp(PERSP_WIN) 
327  * and returns with persp(PERSP_VIEW). After lasso select backbuf is not OK
328  */
329 static void do_lasso_select_pose(ViewContext *vc, Object *ob, int mcords[][2], short moves, short select)
330 {
331         bPoseChannel *pchan;
332         float vec[3];
333         int sco1[2], sco2[2];
334         bArmature *arm = ob->data;
335         
336         if (ob->type != OB_ARMATURE || ob->pose == NULL) return;
337
338         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
339                 if (PBONE_VISIBLE(arm, pchan->bone) && (pchan->bone->flag & BONE_UNSELECTABLE) == 0) {
340                         mul_v3_m4v3(vec, ob->obmat, pchan->pose_head);
341                         project_int(vc->ar, vec, sco1);
342                         mul_v3_m4v3(vec, ob->obmat, pchan->pose_tail);
343                         project_int(vc->ar, vec, sco2);
344                         
345                         if (BLI_lasso_is_edge_inside(mcords, moves, sco1[0], sco1[1], sco2[0], sco2[1], IS_CLIPPED)) {
346                                 if (select) pchan->bone->flag |= BONE_SELECTED;
347                                 else pchan->bone->flag &= ~BONE_SELECTED;
348                         }
349                 }
350         }
351 }
352
353 static void object_deselect_all_visible(Scene *scene, View3D *v3d)
354 {
355         Base *base;
356
357         for (base = scene->base.first; base; base = base->next) {
358                 if (BASE_SELECTABLE(v3d, base)) {
359                         ED_base_object_select(base, BA_DESELECT);
360                 }
361         }
362 }
363
364 static void do_lasso_select_objects(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
365 {
366         Base *base;
367         
368         if (extend == 0 && select)
369                 object_deselect_all_visible(vc->scene, vc->v3d);
370
371         for (base = vc->scene->base.first; base; base = base->next) {
372                 if (BASE_SELECTABLE(vc->v3d, base)) { /* use this to avoid un-needed lasso lookups */
373                         project_short(vc->ar, base->object->obmat[3], &base->sx);
374                         if (BLI_lasso_is_point_inside(mcords, moves, base->sx, base->sy, IS_CLIPPED)) {
375                                 
376                                 if (select) ED_base_object_select(base, BA_SELECT);
377                                 else ED_base_object_select(base, BA_DESELECT);
378                                 base->object->flag = base->flag;
379                         }
380                         if (base->object->mode & OB_MODE_POSE) {
381                                 do_lasso_select_pose(vc, base->object, mcords, moves, select);
382                         }
383                 }
384         }
385 }
386
387 static void do_lasso_select_mesh__doSelectVert(void *userData, BMVert *eve, int x, int y, int UNUSED(index))
388 {
389         LassoSelectUserData *data = userData;
390
391         if (BLI_in_rcti(data->rect, x, y) &&
392             BLI_lasso_is_point_inside(data->mcords, data->moves, x, y, IS_CLIPPED))
393         {
394                 BM_vert_select_set(data->vc->em->bm, eve, data->select);
395         }
396 }
397 static void do_lasso_select_mesh__doSelectEdge(void *userData, BMEdge *eed, int x0, int y0, int x1, int y1, int index)
398 {
399         LassoSelectUserData *data = userData;
400
401         if (EDBM_backbuf_check(bm_solidoffs + index)) {
402                 if (data->pass == 0) {
403                         if (edge_fully_inside_rect(data->rect, x0, y0, x1, y1)  &&
404                             BLI_lasso_is_point_inside(data->mcords, data->moves, x0, y0, IS_CLIPPED) &&
405                             BLI_lasso_is_point_inside(data->mcords, data->moves, x1, y1, IS_CLIPPED))
406                         {
407                                 BM_edge_select_set(data->vc->em->bm, eed, data->select);
408                                 data->done = TRUE;
409                         }
410                 }
411                 else {
412                         if (BLI_lasso_is_edge_inside(data->mcords, data->moves, x0, y0, x1, y1, IS_CLIPPED)) {
413                                 BM_edge_select_set(data->vc->em->bm, eed, data->select);
414                         }
415                 }
416         }
417 }
418 static void do_lasso_select_mesh__doSelectFace(void *userData, BMFace *efa, int x, int y, int UNUSED(index))
419 {
420         LassoSelectUserData *data = userData;
421
422         if (BLI_in_rcti(data->rect, x, y) &&
423             BLI_lasso_is_point_inside(data->mcords, data->moves, x, y, IS_CLIPPED))
424         {
425                 BM_face_select_set(data->vc->em->bm, efa, data->select);
426         }
427 }
428
429 static void do_lasso_select_mesh(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
430 {
431         LassoSelectUserData data;
432         ToolSettings *ts = vc->scene->toolsettings;
433         rcti rect;
434         int bbsel;
435         
436         BLI_lasso_boundbox(&rect, mcords, moves);
437         
438         /* set editmesh */
439         vc->em = BMEdit_FromObject(vc->obedit);
440
441         data.vc = vc;
442         data.rect = &rect;
443         data.mcords = mcords;
444         data.moves = moves;
445         data.select = select;
446         data.done = FALSE;
447         data.pass = 0;
448
449         if (extend == 0 && select)
450                 EDBM_flag_disable_all(vc->em, BM_ELEM_SELECT);
451
452         /* for non zbuf projections, don't change the GL state */
453         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
454
455         glLoadMatrixf(vc->rv3d->viewmat);
456         bbsel = EDBM_backbuf_border_mask_init(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
457         
458         if (ts->selectmode & SCE_SELECT_VERTEX) {
459                 if (bbsel) {
460                         edbm_backbuf_check_and_select_verts(vc->em, select);
461                 }
462                 else {
463                         mesh_foreachScreenVert(vc, do_lasso_select_mesh__doSelectVert, &data, V3D_CLIP_TEST_RV3D_CLIPPING);
464                 }
465         }
466         if (ts->selectmode & SCE_SELECT_EDGE) {
467                 /* Does both bbsel and non-bbsel versions (need screen cos for both) */
468                 data.pass = 0;
469                 mesh_foreachScreenEdge(vc, do_lasso_select_mesh__doSelectEdge, &data, V3D_CLIP_TEST_OFF);
470
471                 if (data.done == 0) {
472                         data.pass = 1;
473                         mesh_foreachScreenEdge(vc, do_lasso_select_mesh__doSelectEdge, &data, V3D_CLIP_TEST_OFF);
474                 }
475         }
476         
477         if (ts->selectmode & SCE_SELECT_FACE) {
478                 if (bbsel) {
479                         edbm_backbuf_check_and_select_faces(vc->em, select);
480                 }
481                 else {
482                         mesh_foreachScreenFace(vc, do_lasso_select_mesh__doSelectFace, &data);
483                 }
484         }
485         
486         EDBM_backbuf_free();
487         EDBM_selectmode_flush(vc->em);
488 }
489
490 static void do_lasso_select_curve__doSelect(void *userData, Nurb *UNUSED(nu), BPoint *bp, BezTriple *bezt, int beztindex, int x, int y)
491 {
492         LassoSelectUserData *data = userData;
493         Object *obedit = data->vc->obedit;
494         Curve *cu = (Curve *)obedit->data;
495
496         if (BLI_lasso_is_point_inside(data->mcords, data->moves, x, y, IS_CLIPPED)) {
497                 if (bp) {
498                         bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
499                         if (bp == cu->lastsel && !(bp->f1 & 1)) cu->lastsel = NULL;
500                 }
501                 else {
502                         if (cu->drawflag & CU_HIDE_HANDLES) {
503                                 /* can only be beztindex==0 here since handles are hidden */
504                                 bezt->f1 = bezt->f2 = bezt->f3 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
505                         }
506                         else {
507                                 if (beztindex == 0) {
508                                         bezt->f1 = data->select ? (bezt->f1 | SELECT) : (bezt->f1 & ~SELECT);
509                                 }
510                                 else if (beztindex == 1) {
511                                         bezt->f2 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
512                                 }
513                                 else {
514                                         bezt->f3 = data->select ? (bezt->f3 | SELECT) : (bezt->f3 & ~SELECT);
515                                 }
516                         }
517
518                         if (bezt == cu->lastsel && !(bezt->f2 & 1)) cu->lastsel = NULL;
519                 }
520         }
521 }
522
523 static void do_lasso_select_curve(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
524 {
525         LassoSelectUserData data;
526
527         /* set vc->editnurb */
528         data.vc = vc;
529         data.mcords = mcords;
530         data.moves = moves;
531         data.select = select;
532
533         if (extend == 0 && select)
534                 CU_deselect_all(vc->obedit);
535
536         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
537         nurbs_foreachScreenVert(vc, do_lasso_select_curve__doSelect, &data);
538 }
539
540 static void do_lasso_select_lattice__doSelect(void *userData, BPoint *bp, int x, int y)
541 {
542         LassoSelectUserData *data = userData;
543
544         if (BLI_lasso_is_point_inside(data->mcords, data->moves, x, y, IS_CLIPPED)) {
545                 bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
546         }
547 }
548 static void do_lasso_select_lattice(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
549 {
550         LassoSelectUserData data;
551
552         /* set editdata in vc */
553         data.mcords = mcords;
554         data.moves = moves;
555         data.select = select;
556
557         if (extend == 0 && select)
558                 ED_setflagsLatt(vc->obedit, 0);
559
560         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
561         lattice_foreachScreenVert(vc, do_lasso_select_lattice__doSelect, &data);
562 }
563
564 static void do_lasso_select_armature(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
565 {
566         bArmature *arm = vc->obedit->data;
567         EditBone *ebone;
568         float vec[3];
569         short sco1[2], sco2[2], didpoint;
570         int change = FALSE;
571
572         if (extend == 0 && select)
573                 ED_armature_deselect_all_visible(vc->obedit);
574
575         /* set editdata in vc */
576         
577         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
578                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_UNSELECTABLE) == 0) {
579                         mul_v3_m4v3(vec, vc->obedit->obmat, ebone->head);
580                         project_short(vc->ar, vec, sco1);
581                         mul_v3_m4v3(vec, vc->obedit->obmat, ebone->tail);
582                         project_short(vc->ar, vec, sco2);
583                         
584                         didpoint = 0;
585                         if (BLI_lasso_is_point_inside(mcords, moves, sco1[0], sco1[1], IS_CLIPPED)) {
586                                 if (select) ebone->flag |= BONE_ROOTSEL;
587                                 else ebone->flag &= ~BONE_ROOTSEL;
588                                 didpoint = 1;
589                                 change = TRUE;
590                         }
591                         if (BLI_lasso_is_point_inside(mcords, moves, sco2[0], sco2[1], IS_CLIPPED)) {
592                                 if (select) ebone->flag |= BONE_TIPSEL;
593                                 else ebone->flag &= ~BONE_TIPSEL;
594                                 didpoint = 1;
595                                 change = TRUE;
596                         }
597                         /* if one of points selected, we skip the bone itself */
598                         if (didpoint == 0 &&
599                             BLI_lasso_is_edge_inside(mcords, moves, sco1[0], sco1[1], sco2[0], sco2[1], IS_CLIPPED))
600                         {
601                                 if (select) ebone->flag |= BONE_TIPSEL | BONE_ROOTSEL | BONE_SELECTED;
602                                 else ebone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
603                                 change = TRUE;
604                         }
605                 }
606         }
607         
608         if (change) {
609                 ED_armature_sync_selection(arm->edbo);
610                 ED_armature_validate_active(arm);
611                 WM_main_add_notifier(NC_OBJECT | ND_BONE_SELECT, vc->obedit);
612         }
613 }
614
615
616
617
618 static void do_lasso_select_meta(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
619 {
620         MetaBall *mb = (MetaBall *)vc->obedit->data;
621         MetaElem *ml;
622         float vec[3];
623         short sco[2];
624
625         if (extend == 0 && select) {
626                 for (ml = mb->editelems->first; ml; ml = ml->next) {
627                         ml->flag &= ~SELECT;
628                 }
629         }
630
631         for (ml = mb->editelems->first; ml; ml = ml->next) {
632                 
633                 mul_v3_m4v3(vec, vc->obedit->obmat, &ml->x);
634                 project_short(vc->ar, vec, sco);
635
636                 if (BLI_lasso_is_point_inside(mcords, moves, sco[0], sco[1], IS_CLIPPED)) {
637                         if (select) ml->flag |= SELECT;
638                         else ml->flag &= ~SELECT;
639                 }
640         }
641 }
642
643 int do_paintvert_box_select(ViewContext *vc, rcti *rect, int select, int extend)
644 {
645         Mesh *me;
646         MVert *mvert;
647         struct ImBuf *ibuf;
648         unsigned int *rt;
649         int a, index;
650         char *selar;
651         int sx = rect->xmax - rect->xmin + 1;
652         int sy = rect->ymax - rect->ymin + 1;
653
654         me = vc->obact->data;
655
656         if (me == NULL || me->totvert == 0 || sx * sy <= 0)
657                 return OPERATOR_CANCELLED;
658
659         selar = MEM_callocN(me->totvert + 1, "selar");
660
661         if (extend == 0 && select)
662                 paintvert_deselect_all_visible(vc->obact, SEL_DESELECT, FALSE);
663
664         view3d_validate_backbuf(vc);
665
666         ibuf = IMB_allocImBuf(sx, sy, 32, IB_rect);
667         rt = ibuf->rect;
668         glReadPixels(rect->xmin + vc->ar->winrct.xmin,  rect->ymin + vc->ar->winrct.ymin, sx, sy, GL_RGBA, GL_UNSIGNED_BYTE,  ibuf->rect);
669         if (ENDIAN_ORDER == B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
670
671         a = sx * sy;
672         while (a--) {
673                 if (*rt) {
674                         index = WM_framebuffer_to_index(*rt);
675                         if (index <= me->totvert) selar[index] = 1;
676                 }
677                 rt++;
678         }
679
680         mvert = me->mvert;
681         for (a = 1; a <= me->totvert; a++, mvert++) {
682                 if (selar[a]) {
683                         if ((mvert->flag & ME_HIDE) == 0) {
684                                 if (select) mvert->flag |=  SELECT;
685                                 else        mvert->flag &= ~SELECT;
686                         }
687                 }
688         }
689
690         IMB_freeImBuf(ibuf);
691         MEM_freeN(selar);
692
693 #ifdef __APPLE__        
694         glReadBuffer(GL_BACK);
695 #endif
696
697         paintvert_flush_flags(vc->obact);
698
699         return OPERATOR_FINISHED;
700 }
701
702 static void do_lasso_select_paintvert(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
703 {
704         Object *ob = vc->obact;
705         Mesh *me = ob ? ob->data : NULL;
706         rcti rect;
707
708         if (me == NULL || me->totvert == 0)
709                 return;
710
711         if (extend == 0 && select)
712                 paintvert_deselect_all_visible(ob, SEL_DESELECT, FALSE);  /* flush selection at the end */
713         bm_vertoffs = me->totvert + 1; /* max index array */
714
715         BLI_lasso_boundbox(&rect, mcords, moves);
716         EDBM_backbuf_border_mask_init(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
717
718         edbm_backbuf_check_and_select_verts_obmode(me, select);
719
720         EDBM_backbuf_free();
721
722         paintvert_flush_flags(ob);
723 }
724 static void do_lasso_select_paintface(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
725 {
726         Object *ob = vc->obact;
727         Mesh *me = ob ? ob->data : NULL;
728         rcti rect;
729
730         if (me == NULL || me->totpoly == 0)
731                 return;
732
733         if (extend == 0 && select)
734                 paintface_deselect_all_visible(ob, SEL_DESELECT, FALSE);  /* flush selection at the end */
735
736         bm_vertoffs = me->totpoly + 1; /* max index array */
737
738         BLI_lasso_boundbox(&rect, mcords, moves);
739         EDBM_backbuf_border_mask_init(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
740         
741         edbm_backbuf_check_and_select_tfaces(me, select);
742
743         EDBM_backbuf_free();
744
745         paintface_flush_flags(ob);
746 }
747
748 #if 0
749 static void do_lasso_select_node(int mcords[][2], short moves, short select)
750 {
751         SpaceNode *snode = sa->spacedata.first;
752         
753         bNode *node;
754         rcti rect;
755         short node_cent[2];
756         float node_centf[2];
757         
758         BLI_lasso_boundbox(&rect, mcords, moves);
759         
760         /* store selection in temp test flag */
761         for (node = snode->edittree->nodes.first; node; node = node->next) {
762                 
763                 node_centf[0] = (node->totr.xmin + node->totr.xmax) / 2;
764                 node_centf[1] = (node->totr.ymin + node->totr.ymax) / 2;
765                 
766                 ipoco_to_areaco_noclip(G.v2d, node_centf, node_cent);
767                 if (BLI_in_rcti(&rect, node_cent[0], node_cent[1]) && BLI_lasso_is_point_inside(mcords, moves, node_cent[0], node_cent[1])) {
768                         if (select) {
769                                 node->flag |= SELECT;
770                         }
771                         else {
772                                 node->flag &= ~SELECT;
773                         }
774                 }
775         }
776         BIF_undo_push("Lasso select nodes");
777 }
778 #endif
779
780 static void view3d_lasso_select(bContext *C, ViewContext *vc, int mcords[][2], short moves, short extend, short select)
781 {
782         Object *ob = CTX_data_active_object(C);
783
784         if (vc->obedit == NULL) { /* Object Mode */
785                 if (paint_facesel_test(ob))
786                         do_lasso_select_paintface(vc, mcords, moves, extend, select);
787                 else if (paint_vertsel_test(ob))
788                         do_lasso_select_paintvert(vc, mcords, moves, extend, select);
789                 else if (ob && ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT))
790                         ;
791                 else if (ob && ob->mode & OB_MODE_PARTICLE_EDIT)
792                         PE_lasso_select(C, mcords, moves, extend, select);
793                 else {
794                         do_lasso_select_objects(vc, mcords, moves, extend, select);
795                         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, vc->scene);
796                 }
797         }
798         else { /* Edit Mode */
799                 switch (vc->obedit->type) {
800                         case OB_MESH:
801                                 do_lasso_select_mesh(vc, mcords, moves, extend, select);
802                                 break;
803                         case OB_CURVE:
804                         case OB_SURF:
805                                 do_lasso_select_curve(vc, mcords, moves, extend, select);
806                                 break;
807                         case OB_LATTICE:
808                                 do_lasso_select_lattice(vc, mcords, moves, extend, select);
809                                 break;
810                         case OB_ARMATURE:
811                                 do_lasso_select_armature(vc, mcords, moves, extend, select);
812                                 break;
813                         case OB_MBALL:
814                                 do_lasso_select_meta(vc, mcords, moves, extend, select);
815                                 break;
816                         default:
817                                 assert(!"lasso select on incorrect object type");
818                 }
819
820                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc->obedit->data);
821         }
822 }
823
824
825 /* lasso operator gives properties, but since old code works
826  * with short array we convert */
827 static int view3d_lasso_select_exec(bContext *C, wmOperator *op)
828 {
829         ViewContext vc;
830         int mcords_tot;
831         int (*mcords)[2] = WM_gesture_lasso_path_to_array(C, op, &mcords_tot);
832         
833         if (mcords) {
834                 short extend, select;
835                 view3d_operator_needs_opengl(C);
836                 
837                 /* setup view context for argument to callbacks */
838                 view3d_set_viewcontext(C, &vc);
839                 
840                 extend = RNA_boolean_get(op->ptr, "extend");
841                 select = !RNA_boolean_get(op->ptr, "deselect");
842                 view3d_lasso_select(C, &vc, mcords, mcords_tot, extend, select);
843                 
844                 MEM_freeN(mcords);
845
846                 return OPERATOR_FINISHED;
847         }
848         return OPERATOR_PASS_THROUGH;
849 }
850
851 void VIEW3D_OT_select_lasso(wmOperatorType *ot)
852 {
853         ot->name = "Lasso Select";
854         ot->description = "Select items using lasso selection";
855         ot->idname = "VIEW3D_OT_select_lasso";
856         
857         ot->invoke = WM_gesture_lasso_invoke;
858         ot->modal = WM_gesture_lasso_modal;
859         ot->exec = view3d_lasso_select_exec;
860         ot->poll = view3d_selectable_data;
861         ot->cancel = WM_gesture_lasso_cancel;
862         
863         /* flags */
864         ot->flag = OPTYPE_UNDO;
865         
866         RNA_def_collection_runtime(ot->srna, "path", &RNA_OperatorMousePath, "Path", "");
867         RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Deselect rather than select items");
868         RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection instead of deselecting everything first");
869 }
870
871
872 /* ************************************************* */
873
874 #if 0
875 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
876 static unsigned int samplerect(unsigned int *buf, int size, unsigned int dontdo)
877 {
878         Base *base;
879         unsigned int *bufmin, *bufmax;
880         int a, b, rc, tel, len, dirvec[4][2], maxob;
881         unsigned int retval = 0;
882         
883         base = LASTBASE;
884         if (base == 0) return 0;
885         maxob = base->selcol;
886
887         len = (size - 1) / 2;
888         rc = 0;
889
890         dirvec[0][0] = 1;
891         dirvec[0][1] = 0;
892         dirvec[1][0] = 0;
893         dirvec[1][1] = -size;
894         dirvec[2][0] = -1;
895         dirvec[2][1] = 0;
896         dirvec[3][0] = 0;
897         dirvec[3][1] = size;
898
899         bufmin = buf;
900         bufmax = buf + size * size;
901         buf += len * size + len;
902
903         for (tel = 1; tel <= size; tel++) {
904
905                 for (a = 0; a < 2; a++) {
906                         for (b = 0; b < tel; b++) {
907
908                                 if (*buf && *buf <= maxob && *buf != dontdo) return *buf;
909                                 if (*buf == dontdo) retval = dontdo;  /* if only color dontdo is available, still return dontdo */
910                                 
911                                 buf += (dirvec[rc][0] + dirvec[rc][1]);
912
913                                 if (buf < bufmin || buf >= bufmax) return retval;
914                         }
915                         rc++;
916                         rc &= 3;
917                 }
918         }
919         return retval;
920 }
921 #endif
922
923 /* ************************** mouse select ************************* */
924
925
926 /* The max number of menu items in an object select menu */
927 typedef struct SelMenuItemF {
928         char idname[MAX_ID_NAME - 2];
929         int icon;
930 } SelMenuItemF;
931
932 #define SEL_MENU_SIZE   22
933 static SelMenuItemF object_mouse_select_menu_data[SEL_MENU_SIZE];
934
935 /* special (crappy) operator only for menu select */
936 static EnumPropertyItem *object_select_menu_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
937 {
938         EnumPropertyItem *item = NULL, item_tmp = {0};
939         int totitem = 0;
940         int i = 0;
941
942         /* don't need context but avoid docgen using this */
943         if (C == NULL || object_mouse_select_menu_data[i].idname[0] == '\0') {
944                 return DummyRNA_NULL_items;
945         }
946
947         for (; i < SEL_MENU_SIZE && object_mouse_select_menu_data[i].idname[0] != '\0'; i++) {
948                 item_tmp.name = object_mouse_select_menu_data[i].idname;
949                 item_tmp.identifier = object_mouse_select_menu_data[i].idname;
950                 item_tmp.value = i;
951                 item_tmp.icon = object_mouse_select_menu_data[i].icon;
952                 RNA_enum_item_add(&item, &totitem, &item_tmp);
953         }
954
955         RNA_enum_item_end(&item, &totitem);
956         *free = 1;
957
958         return item;
959 }
960
961 static int object_select_menu_exec(bContext *C, wmOperator *op)
962 {
963         int name_index = RNA_enum_get(op->ptr, "name");
964         short extend = RNA_boolean_get(op->ptr, "extend");
965         short changed = 0;
966         const char *name = object_mouse_select_menu_data[name_index].idname;
967
968         if (!extend) {
969                 CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
970                 {
971                         if (base->flag & SELECT) {
972                                 ED_base_object_select(base, BA_DESELECT);
973                                 changed = 1;
974                         }
975                 }
976                 CTX_DATA_END;
977         }
978
979         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
980         {
981                 /* this is a bit dodjy, there should only be ONE object with this name, but library objects can mess this up */
982                 if (strcmp(name, base->object->id.name + 2) == 0) {
983                         ED_base_object_activate(C, base);
984                         ED_base_object_select(base, BA_SELECT);
985                         changed = 1;
986                 }
987         }
988         CTX_DATA_END;
989
990         /* weak but ensures we activate menu again before using the enum */
991         memset(object_mouse_select_menu_data, 0, sizeof(object_mouse_select_menu_data));
992
993         /* undo? */
994         if (changed) {
995                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
996                 return OPERATOR_FINISHED;
997         }
998         else {
999                 return OPERATOR_CANCELLED;
1000         }
1001 }
1002
1003 void VIEW3D_OT_select_menu(wmOperatorType *ot)
1004 {
1005         PropertyRNA *prop;
1006
1007         /* identifiers */
1008         ot->name = "Select Menu";
1009         ot->description = "Menu object selection";
1010         ot->idname = "VIEW3D_OT_select_menu";
1011
1012         /* api callbacks */
1013         ot->invoke = WM_menu_invoke;
1014         ot->exec = object_select_menu_exec;
1015
1016         /* flags */
1017         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1018
1019         /* keyingset to use (dynamic enum) */
1020         prop = RNA_def_enum(ot->srna, "name", DummyRNA_NULL_items, 0, "Object Name", "");
1021         RNA_def_enum_funcs(prop, object_select_menu_enum_itemf);
1022         RNA_def_property_flag(prop, PROP_HIDDEN);
1023         ot->prop = prop;
1024
1025         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everything first");
1026 }
1027
1028 static void deselectall_except(Scene *scene, Base *b)   /* deselect all except b */
1029 {
1030         Base *base;
1031         
1032         for (base = FIRSTBASE; base; base = base->next) {
1033                 if (base->flag & SELECT) {
1034                         if (b != base) {
1035                                 ED_base_object_select(base, BA_DESELECT);
1036                         }
1037                 }
1038         }
1039 }
1040
1041 static Base *object_mouse_select_menu(bContext *C, ViewContext *vc, unsigned int *buffer, int hits, const int mval[2], short extend)
1042 {
1043         short baseCount = 0;
1044         short ok;
1045         LinkNode *linklist = NULL;
1046         
1047         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
1048         {
1049                 ok = FALSE;
1050
1051                 /* two selection methods, the CTRL select uses max dist of 15 */
1052                 if (buffer) {
1053                         int a;
1054                         for (a = 0; a < hits; a++) {
1055                                 /* index was converted */
1056                                 if (base->selcol == buffer[(4 * a) + 3])
1057                                         ok = TRUE;
1058                         }
1059                 }
1060                 else {
1061                         int temp, dist = 15;
1062
1063                         project_short(vc->ar, base->object->obmat[3], &base->sx);
1064                         
1065                         temp = abs(base->sx - mval[0]) + abs(base->sy - mval[1]);
1066                         if (temp < dist)
1067                                 ok = TRUE;
1068                 }
1069
1070                 if (ok) {
1071                         baseCount++;
1072                         BLI_linklist_prepend(&linklist, base);
1073
1074                         if (baseCount == SEL_MENU_SIZE)
1075                                 break;
1076                 }
1077         }
1078         CTX_DATA_END;
1079
1080         if (baseCount == 0) {
1081                 return NULL;
1082         }
1083         if (baseCount == 1) {
1084                 Base *base = (Base *)linklist->link;
1085                 BLI_linklist_free(linklist, NULL);
1086                 return base;
1087         }
1088         else {
1089                 /* UI, full in static array values that we later use in an enum function */
1090                 LinkNode *node;
1091                 int i;
1092
1093                 memset(object_mouse_select_menu_data, 0, sizeof(object_mouse_select_menu_data));
1094
1095                 for (node = linklist, i = 0; node; node = node->next, i++) {
1096                         Base *base = node->link;
1097                         Object *ob = base->object;
1098                         char *name = ob->id.name + 2;
1099
1100                         BLI_strncpy(object_mouse_select_menu_data[i].idname, name, MAX_ID_NAME - 2);
1101                         object_mouse_select_menu_data[i].icon = uiIconFromID(&ob->id);
1102                 }
1103
1104                 {
1105                         PointerRNA ptr;
1106
1107                         WM_operator_properties_create(&ptr, "VIEW3D_OT_select_menu");
1108                         RNA_boolean_set(&ptr, "extend", extend);
1109                         WM_operator_name_call(C, "VIEW3D_OT_select_menu", WM_OP_INVOKE_DEFAULT, &ptr);
1110                         WM_operator_properties_free(&ptr);
1111                 }
1112
1113                 BLI_linklist_free(linklist, NULL);
1114                 return NULL;
1115         }
1116 }
1117
1118 /* we want a select buffer with bones, if there are... */
1119 /* so check three selection levels and compare */
1120 static short mixed_bones_object_selectbuffer(ViewContext *vc, unsigned int *buffer, const int mval[2])
1121 {
1122         rcti rect;
1123         int offs;
1124         short a, hits15, hits9 = 0, hits5 = 0;
1125         short has_bones15 = 0, has_bones9 = 0, has_bones5 = 0;
1126         
1127         BLI_init_rcti(&rect, mval[0] - 14, mval[0] + 14, mval[1] - 14, mval[1] + 14);
1128         hits15 = view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect);
1129         if (hits15 > 0) {
1130                 for (a = 0; a < hits15; a++) if (buffer[4 * a + 3] & 0xFFFF0000) has_bones15 = 1;
1131
1132                 offs = 4 * hits15;
1133                 BLI_init_rcti(&rect, mval[0] - 9, mval[0] + 9, mval[1] - 9, mval[1] + 9);
1134                 hits9 = view3d_opengl_select(vc, buffer + offs, MAXPICKBUF - offs, &rect);
1135                 if (hits9 > 0) {
1136                         for (a = 0; a < hits9; a++) if (buffer[offs + 4 * a + 3] & 0xFFFF0000) has_bones9 = 1;
1137
1138                         offs += 4 * hits9;
1139                         BLI_init_rcti(&rect, mval[0] - 5, mval[0] + 5, mval[1] - 5, mval[1] + 5);
1140                         hits5 = view3d_opengl_select(vc, buffer + offs, MAXPICKBUF - offs, &rect);
1141                         if (hits5 > 0) {
1142                                 for (a = 0; a < hits5; a++) if (buffer[offs + 4 * a + 3] & 0xFFFF0000) has_bones5 = 1;
1143                         }
1144                 }
1145                 
1146                 if (has_bones5) {
1147                         offs = 4 * hits15 + 4 * hits9;
1148                         memcpy(buffer, buffer + offs, 4 * offs);
1149                         return hits5;
1150                 }
1151                 if (has_bones9) {
1152                         offs = 4 * hits15;
1153                         memcpy(buffer, buffer + offs, 4 * offs);
1154                         return hits9;
1155                 }
1156                 if (has_bones15) {
1157                         return hits15;
1158                 }
1159                 
1160                 if (hits5 > 0) {
1161                         offs = 4 * hits15 + 4 * hits9;
1162                         memcpy(buffer, buffer + offs, 4 * offs);
1163                         return hits5;
1164                 }
1165                 if (hits9 > 0) {
1166                         offs = 4 * hits15;
1167                         memcpy(buffer, buffer + offs, 4 * offs);
1168                         return hits9;
1169                 }
1170                 return hits15;
1171         }
1172         
1173         return 0;
1174 }
1175
1176 /* returns basact */
1177 static Base *mouse_select_eval_buffer(ViewContext *vc, unsigned int *buffer, int hits, const int mval[2], Base *startbase, int has_bones)
1178 {
1179         Scene *scene = vc->scene;
1180         View3D *v3d = vc->v3d;
1181         Base *base, *basact = NULL;
1182         static int lastmval[2] = {-100, -100};
1183         int a, do_nearest = FALSE;
1184         
1185         /* define if we use solid nearest select or not */
1186         if (v3d->drawtype > OB_WIRE) {
1187                 do_nearest = TRUE;
1188                 if (ABS(mval[0] - lastmval[0]) < 3 && ABS(mval[1] - lastmval[1]) < 3) {
1189                         if (!has_bones) /* hrms, if theres bones we always do nearest */
1190                                 do_nearest = FALSE;
1191                 }
1192         }
1193         lastmval[0] = mval[0]; lastmval[1] = mval[1];
1194         
1195         if (do_nearest) {
1196                 unsigned int min = 0xFFFFFFFF;
1197                 int selcol = 0, notcol = 0;
1198                 
1199                 
1200                 if (has_bones) {
1201                         /* we skip non-bone hits */
1202                         for (a = 0; a < hits; a++) {
1203                                 if (min > buffer[4 * a + 1] && (buffer[4 * a + 3] & 0xFFFF0000) ) {
1204                                         min = buffer[4 * a + 1];
1205                                         selcol = buffer[4 * a + 3] & 0xFFFF;
1206                                 }
1207                         }
1208                 }
1209                 else {
1210                         /* only exclude active object when it is selected... */
1211                         if (BASACT && (BASACT->flag & SELECT) && hits > 1) notcol = BASACT->selcol;
1212                         
1213                         for (a = 0; a < hits; a++) {
1214                                 if (min > buffer[4 * a + 1] && notcol != (buffer[4 * a + 3] & 0xFFFF)) {
1215                                         min = buffer[4 * a + 1];
1216                                         selcol = buffer[4 * a + 3] & 0xFFFF;
1217                                 }
1218                         }
1219                 }
1220                 
1221                 base = FIRSTBASE;
1222                 while (base) {
1223                         if (BASE_SELECTABLE(v3d, base)) {
1224                                 if (base->selcol == selcol) break;
1225                         }
1226                         base = base->next;
1227                 }
1228                 if (base) basact = base;
1229         }
1230         else {
1231                 
1232                 base = startbase;
1233                 while (base) {
1234                         /* skip objects with select restriction, to prevent prematurely ending this loop
1235                          * with an un-selectable choice */
1236                         if (base->object->restrictflag & OB_RESTRICT_SELECT) {
1237                                 base = base->next;
1238                                 if (base == NULL) base = FIRSTBASE;
1239                                 if (base == startbase) break;
1240                         }
1241                         
1242                         if (BASE_SELECTABLE(v3d, base)) {
1243                                 for (a = 0; a < hits; a++) {
1244                                         if (has_bones) {
1245                                                 /* skip non-bone objects */
1246                                                 if ((buffer[4 * a + 3] & 0xFFFF0000)) {
1247                                                         if (base->selcol == (buffer[(4 * a) + 3] & 0xFFFF))
1248                                                                 basact = base;
1249                                                 }
1250                                         }
1251                                         else {
1252                                                 if (base->selcol == (buffer[(4 * a) + 3] & 0xFFFF))
1253                                                         basact = base;
1254                                         }
1255                                 }
1256                         }
1257                         
1258                         if (basact) break;
1259                         
1260                         base = base->next;
1261                         if (base == NULL) base = FIRSTBASE;
1262                         if (base == startbase) break;
1263                 }
1264         }
1265         
1266         return basact;
1267 }
1268
1269 /* mval comes from event->mval, only use within region handlers */
1270 Base *ED_view3d_give_base_under_cursor(bContext *C, const int mval[2])
1271 {
1272         ViewContext vc;
1273         Base *basact = NULL;
1274         unsigned int buffer[4 * MAXPICKBUF];
1275         int hits;
1276         
1277         /* setup view context for argument to callbacks */
1278         view3d_operator_needs_opengl(C);
1279         view3d_set_viewcontext(C, &vc);
1280         
1281         hits = mixed_bones_object_selectbuffer(&vc, buffer, mval);
1282         
1283         if (hits > 0) {
1284                 int a, has_bones = 0;
1285                 
1286                 for (a = 0; a < hits; a++) if (buffer[4 * a + 3] & 0xFFFF0000) has_bones = 1;
1287                 
1288                 basact = mouse_select_eval_buffer(&vc, buffer, hits, mval, vc.scene->base.first, has_bones);
1289         }
1290         
1291         return basact;
1292 }
1293
1294 static void deselect_all_tracks(MovieTracking *tracking)
1295 {
1296         MovieTrackingObject *object;
1297
1298         object = tracking->objects.first;
1299         while (object) {
1300                 ListBase *tracksbase = BKE_tracking_object_tracks(tracking, object);
1301                 MovieTrackingTrack *track = tracksbase->first;
1302
1303                 while (track) {
1304                         BKE_tracking_deselect_track(track, TRACK_AREA_ALL);
1305
1306                         track = track->next;
1307                 }
1308
1309                 object = object->next;
1310         }
1311 }
1312
1313 /* mval is region coords */
1314 static int mouse_select(bContext *C, const int mval[2], short extend, short deselect, short toggle, short obcenter, short enumerate)
1315 {
1316         ViewContext vc;
1317         ARegion *ar = CTX_wm_region(C);
1318         View3D *v3d = CTX_wm_view3d(C);
1319         Scene *scene = CTX_data_scene(C);
1320         Base *base, *startbase = NULL, *basact = NULL, *oldbasact = NULL;
1321         int temp, a, dist = 100;
1322         int retval = 0;
1323         short hits;
1324         
1325         /* setup view context for argument to callbacks */
1326         view3d_set_viewcontext(C, &vc);
1327         
1328         /* always start list from basact in wire mode */
1329         startbase =  FIRSTBASE;
1330         if (BASACT && BASACT->next) startbase = BASACT->next;
1331         
1332         /* This block uses the control key to make the object selected by its center point rather than its contents */
1333         /* in editmode do not activate */
1334         if (obcenter) {
1335                 
1336                 /* note; shift+alt goes to group-flush-selecting */
1337                 if (enumerate) {
1338                         basact = object_mouse_select_menu(C, &vc, NULL, 0, mval, extend);
1339                 }
1340                 else {
1341                         base = startbase;
1342                         while (base) {
1343                                 if (BASE_SELECTABLE(v3d, base)) {
1344                                         project_short(ar, base->object->obmat[3], &base->sx);
1345                                         
1346                                         temp = abs(base->sx - mval[0]) + abs(base->sy - mval[1]);
1347                                         if (base == BASACT) temp += 10;
1348                                         if (temp < dist) {
1349                                                 
1350                                                 dist = temp;
1351                                                 basact = base;
1352                                         }
1353                                 }
1354                                 base = base->next;
1355                                 
1356                                 if (base == NULL) base = FIRSTBASE;
1357                                 if (base == startbase) break;
1358                         }
1359                 }
1360         }
1361         else {
1362                 unsigned int buffer[4 * MAXPICKBUF];
1363
1364                 /* if objects have posemode set, the bones are in the same selection buffer */
1365                 
1366                 hits = mixed_bones_object_selectbuffer(&vc, buffer, mval);
1367                 
1368                 if (hits > 0) {
1369                         int has_bones = 0;
1370                         
1371                         /* note: bundles are handling in the same way as bones */
1372                         for (a = 0; a < hits; a++) if (buffer[4 * a + 3] & 0xFFFF0000) has_bones = 1;
1373
1374                         /* note; shift+alt goes to group-flush-selecting */
1375                         if (has_bones == 0 && enumerate) {
1376                                 basact = object_mouse_select_menu(C, &vc, buffer, hits, mval, extend);
1377                         }
1378                         else {
1379                                 basact = mouse_select_eval_buffer(&vc, buffer, hits, mval, startbase, has_bones);
1380                         }
1381                         
1382                         if (has_bones && basact) {
1383                                 if (basact->object->type == OB_CAMERA) {
1384                                         if (BASACT == basact) {
1385                                                 int i, hitresult;
1386                                                 int changed = 0;
1387
1388                                                 for (i = 0; i < hits; i++) {
1389                                                         hitresult = buffer[3 + (i * 4)];
1390
1391                                                         /* if there's bundles in buffer select bundles first,
1392                                                          * so non-camera elements should be ignored in buffer */
1393                                                         if (basact->selcol != (hitresult & 0xFFFF)) {
1394                                                                 continue;
1395                                                         }
1396
1397                                                         /* index of bundle is 1<<16-based. if there's no "bone" index
1398                                                          * in hight word, this buffer value belongs to camera,. not to bundle */
1399                                                         if (buffer[4 * i + 3] & 0xFFFF0000) {
1400                                                                 MovieClip *clip = BKE_object_movieclip_get(scene, basact->object, 0);
1401                                                                 MovieTracking *tracking = &clip->tracking;
1402                                                                 ListBase *tracksbase;
1403                                                                 MovieTrackingTrack *track;
1404
1405                                                                 track = BKE_tracking_indexed_track(&clip->tracking, hitresult >> 16, &tracksbase);
1406
1407                                                                 if (TRACK_SELECTED(track) && extend) {
1408                                                                         changed = 0;
1409                                                                         BKE_tracking_deselect_track(track, TRACK_AREA_ALL);
1410                                                                 }
1411                                                                 else {
1412                                                                         int oldsel = TRACK_SELECTED(track) ? 1 : 0;
1413                                                                         if (!extend)
1414                                                                                 deselect_all_tracks(tracking);
1415
1416                                                                         BKE_tracking_select_track(tracksbase, track, TRACK_AREA_ALL, extend);
1417
1418                                                                         if (oldsel != (TRACK_SELECTED(track) ? 1 : 0))
1419                                                                                 changed = 1;
1420                                                                 }
1421
1422                                                                 basact->flag |= SELECT;
1423                                                                 basact->object->flag = basact->flag;
1424
1425                                                                 retval = 1;
1426
1427                                                                 WM_event_add_notifier(C, NC_MOVIECLIP | ND_SELECT, track);
1428                                                                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1429
1430                                                                 break;
1431                                                         }
1432                                                 }
1433
1434                                                 if (!changed) {
1435                                                         /* fallback to regular object selection if no new bundles were selected,
1436                                                          * allows to select object parented to reconstruction object */
1437                                                         basact = mouse_select_eval_buffer(&vc, buffer, hits, mval, startbase, 0);
1438                                                 }
1439                                         }
1440                                 }
1441                                 else if (ED_do_pose_selectbuffer(scene, basact, buffer, hits, extend, deselect, toggle) ) {   /* then bone is found */
1442                                 
1443                                         /* we make the armature selected: 
1444                                          * not-selected active object in posemode won't work well for tools */
1445                                         basact->flag |= SELECT;
1446                                         basact->object->flag = basact->flag;
1447                                         
1448                                         retval = 1;
1449                                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, basact->object);
1450                                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_ACTIVE, basact->object);
1451                                         
1452                                         /* in weightpaint, we use selected bone to select vertexgroup, so no switch to new active object */
1453                                         if (BASACT && (BASACT->object->mode & OB_MODE_WEIGHT_PAINT)) {
1454                                                 /* prevent activating */
1455                                                 basact = NULL;
1456                                         }
1457
1458                                 }
1459                                 /* prevent bone selecting to pass on to object selecting */
1460                                 if (basact == BASACT)
1461                                         basact = NULL;
1462                         }
1463                 }
1464         }
1465         
1466         /* so, do we have something selected? */
1467         if (basact) {
1468                 retval = 1;
1469                 
1470                 if (vc.obedit) {
1471                         /* only do select */
1472                         deselectall_except(scene, basact);
1473                         ED_base_object_select(basact, BA_SELECT);
1474                 }
1475                 /* also prevent making it active on mouse selection */
1476                 else if (BASE_SELECTABLE(v3d, basact)) {
1477
1478                         oldbasact = BASACT;
1479                         
1480                         if (extend) {
1481                                 ED_base_object_select(basact, BA_SELECT);
1482                         }
1483                         else if(deselect) {
1484                                 ED_base_object_select(basact, BA_DESELECT);
1485                         }
1486                         else if(toggle) {
1487                                 if (basact->flag & SELECT) {
1488                                         if (basact == oldbasact)
1489                                                 ED_base_object_select(basact, BA_DESELECT);
1490                                 }
1491                                 else ED_base_object_select(basact, BA_SELECT);
1492                         }
1493                         else {
1494                                 deselectall_except(scene, basact);
1495                                 ED_base_object_select(basact, BA_SELECT);
1496                         }
1497
1498                         if (oldbasact != basact) {
1499                                 ED_base_object_activate(C, basact); /* adds notifier */
1500                         }
1501                 }
1502
1503                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1504         }
1505
1506         return retval;
1507 }
1508
1509 /* ********************  border and circle ************************************** */
1510
1511 typedef struct BoxSelectUserData {
1512         ViewContext *vc;
1513         rcti *rect;
1514         int select, pass, done;
1515 } BoxSelectUserData;
1516
1517 int edge_inside_circle(short centx, short centy, short rad, short x1, short y1, short x2, short y2)
1518 {
1519         int radsq = rad * rad;
1520         float v1[2], v2[2], v3[2];
1521         
1522         /* check points in circle itself */
1523         if ( (x1 - centx) * (x1 - centx) + (y1 - centy) * (y1 - centy) <= radsq) return 1;
1524         if ( (x2 - centx) * (x2 - centx) + (y2 - centy) * (y2 - centy) <= radsq) return 1;
1525         
1526         /* pointdistline */
1527         v3[0] = centx;
1528         v3[1] = centy;
1529         v1[0] = x1;
1530         v1[1] = y1;
1531         v2[0] = x2;
1532         v2[1] = y2;
1533         
1534         if (dist_to_line_segment_v2(v3, v1, v2) < (float)rad) return 1;
1535         
1536         return 0;
1537 }
1538
1539 static void do_nurbs_box_select__doSelect(void *userData, Nurb *UNUSED(nu), BPoint *bp, BezTriple *bezt, int beztindex, int x, int y)
1540 {
1541         BoxSelectUserData *data = userData;
1542         Object *obedit = data->vc->obedit;
1543         Curve *cu = (Curve *)obedit->data;
1544
1545         if (BLI_in_rcti(data->rect, x, y)) {
1546                 if (bp) {
1547                         bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
1548                         if (bp == cu->lastsel && !(bp->f1 & 1)) cu->lastsel = NULL;
1549                 }
1550                 else {
1551                         if (cu->drawflag & CU_HIDE_HANDLES) {
1552                                 /* can only be beztindex==0 here since handles are hidden */
1553                                 bezt->f1 = bezt->f2 = bezt->f3 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
1554                         }
1555                         else {
1556                                 if (beztindex == 0) {
1557                                         bezt->f1 = data->select ? (bezt->f1 | SELECT) : (bezt->f1 & ~SELECT);
1558                                 }
1559                                 else if (beztindex == 1) {
1560                                         bezt->f2 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
1561                                 }
1562                                 else {
1563                                         bezt->f3 = data->select ? (bezt->f3 | SELECT) : (bezt->f3 & ~SELECT);
1564                                 }
1565                         }
1566
1567                         if (bezt == cu->lastsel && !(bezt->f2 & 1)) cu->lastsel = NULL;
1568                 }
1569         }
1570 }
1571 static int do_nurbs_box_select(ViewContext *vc, rcti *rect, int select, int extend)
1572 {
1573         BoxSelectUserData data;
1574         
1575         data.vc = vc;
1576         data.rect = rect;
1577         data.select = select;
1578
1579         if (extend == 0 && select)
1580                 CU_deselect_all(vc->obedit);
1581
1582         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
1583         nurbs_foreachScreenVert(vc, do_nurbs_box_select__doSelect, &data);
1584
1585         return OPERATOR_FINISHED;
1586 }
1587
1588 static void do_lattice_box_select__doSelect(void *userData, BPoint *bp, int x, int y)
1589 {
1590         BoxSelectUserData *data = userData;
1591
1592         if (BLI_in_rcti(data->rect, x, y)) {
1593                 bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
1594         }
1595 }
1596 static int do_lattice_box_select(ViewContext *vc, rcti *rect, int select, int extend)
1597 {
1598         BoxSelectUserData data;
1599
1600         data.vc = vc;
1601         data.rect = rect;
1602         data.select = select;
1603
1604         if (extend == 0 && select)
1605                 ED_setflagsLatt(vc->obedit, 0);
1606
1607         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
1608         lattice_foreachScreenVert(vc, do_lattice_box_select__doSelect, &data);
1609         
1610         return OPERATOR_FINISHED;
1611 }
1612
1613 static void do_mesh_box_select__doSelectVert(void *userData, BMVert *eve, int x, int y, int UNUSED(index))
1614 {
1615         BoxSelectUserData *data = userData;
1616
1617         if (BLI_in_rcti(data->rect, x, y)) {
1618                 BM_vert_select_set(data->vc->em->bm, eve, data->select);
1619         }
1620 }
1621 static void do_mesh_box_select__doSelectEdge(void *userData, BMEdge *eed, int x0, int y0, int x1, int y1, int index)
1622 {
1623         BoxSelectUserData *data = userData;
1624
1625         if (EDBM_backbuf_check(bm_solidoffs + index)) {
1626                 if (data->pass == 0) {
1627                         if (edge_fully_inside_rect(data->rect, x0, y0, x1, y1)) {
1628                                 BM_edge_select_set(data->vc->em->bm, eed, data->select);
1629                                 data->done = TRUE;
1630                         }
1631                 }
1632                 else {
1633                         if (edge_inside_rect(data->rect, x0, y0, x1, y1)) {
1634                                 BM_edge_select_set(data->vc->em->bm, eed, data->select);
1635                         }
1636                 }
1637         }
1638 }
1639 static void do_mesh_box_select__doSelectFace(void *userData, BMFace *efa, int x, int y, int UNUSED(index))
1640 {
1641         BoxSelectUserData *data = userData;
1642
1643         if (BLI_in_rcti(data->rect, x, y)) {
1644                 BM_face_select_set(data->vc->em->bm, efa, data->select);
1645         }
1646 }
1647 static int do_mesh_box_select(ViewContext *vc, rcti *rect, int select, int extend)
1648 {
1649         BoxSelectUserData data;
1650         ToolSettings *ts = vc->scene->toolsettings;
1651         int bbsel;
1652         
1653         data.vc = vc;
1654         data.rect = rect;
1655         data.select = select;
1656         data.pass = 0;
1657         data.done = FALSE;
1658
1659         if (extend == 0 && select)
1660                 EDBM_flag_disable_all(vc->em, BM_ELEM_SELECT);
1661
1662         /* for non zbuf projections, don't change the GL state */
1663         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
1664
1665         glLoadMatrixf(vc->rv3d->viewmat);
1666         bbsel = EDBM_backbuf_border_init(vc, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
1667
1668         if (ts->selectmode & SCE_SELECT_VERTEX) {
1669                 if (bbsel) {
1670                         edbm_backbuf_check_and_select_verts(vc->em, select);
1671                 }
1672                 else {
1673                         mesh_foreachScreenVert(vc, do_mesh_box_select__doSelectVert, &data, V3D_CLIP_TEST_RV3D_CLIPPING);
1674                 }
1675         }
1676         if (ts->selectmode & SCE_SELECT_EDGE) {
1677                 /* Does both bbsel and non-bbsel versions (need screen cos for both) */
1678
1679                 data.pass = 0;
1680                 mesh_foreachScreenEdge(vc, do_mesh_box_select__doSelectEdge, &data, V3D_CLIP_TEST_OFF);
1681
1682                 if (data.done == 0) {
1683                         data.pass = 1;
1684                         mesh_foreachScreenEdge(vc, do_mesh_box_select__doSelectEdge, &data, V3D_CLIP_TEST_OFF);
1685                 }
1686         }
1687         
1688         if (ts->selectmode & SCE_SELECT_FACE) {
1689                 if (bbsel) {
1690                         edbm_backbuf_check_and_select_faces(vc->em, select);
1691                 }
1692                 else {
1693                         mesh_foreachScreenFace(vc, do_mesh_box_select__doSelectFace, &data);
1694                 }
1695         }
1696         
1697         EDBM_backbuf_free();
1698                 
1699         EDBM_selectmode_flush(vc->em);
1700         
1701         return OPERATOR_FINISHED;
1702 }
1703
1704 static int do_meta_box_select(ViewContext *vc, rcti *rect, int select, int extend)
1705 {
1706         MetaBall *mb = (MetaBall *)vc->obedit->data;
1707         MetaElem *ml;
1708         int a;
1709
1710         unsigned int buffer[4 * MAXPICKBUF];
1711         short hits;
1712
1713         hits = view3d_opengl_select(vc, buffer, MAXPICKBUF, rect);
1714
1715         if (extend == 0 && select) {
1716                 for (ml = mb->editelems->first; ml; ml = ml->next) {
1717                         ml->flag &= ~SELECT;
1718                 }
1719         }
1720         
1721         for (ml = mb->editelems->first; ml; ml = ml->next) {
1722                 for (a = 0; a < hits; a++) {
1723                         if (ml->selcol1 == buffer[(4 * a) + 3]) {
1724                                 ml->flag |= MB_SCALE_RAD;
1725                                 if (select) ml->flag |= SELECT;
1726                                 else ml->flag &= ~SELECT;
1727                                 break;
1728                         }
1729                         if (ml->selcol2 == buffer[(4 * a) + 3]) {
1730                                 ml->flag &= ~MB_SCALE_RAD;
1731                                 if (select) ml->flag |= SELECT;
1732                                 else ml->flag &= ~SELECT;
1733                                 break;
1734                         }
1735                 }
1736         }
1737
1738         return OPERATOR_FINISHED;
1739 }
1740
1741 static int do_armature_box_select(ViewContext *vc, rcti *rect, short select, short extend)
1742 {
1743         bArmature *arm = vc->obedit->data;
1744         EditBone *ebone;
1745         int a;
1746
1747         unsigned int buffer[4 * MAXPICKBUF];
1748         short hits;
1749
1750         hits = view3d_opengl_select(vc, buffer, MAXPICKBUF, rect);
1751         
1752         /* clear flag we use to detect point was affected */
1753         for (ebone = arm->edbo->first; ebone; ebone = ebone->next)
1754                 ebone->flag &= ~BONE_DONE;
1755         
1756         if (extend == 0 && select)
1757                 ED_armature_deselect_all_visible(vc->obedit);
1758
1759         /* first we only check points inside the border */
1760         for (a = 0; a < hits; a++) {
1761                 int index = buffer[(4 * a) + 3];
1762                 if (index != -1) {
1763                         ebone = BLI_findlink(arm->edbo, index & ~(BONESEL_ANY));
1764                         if ((ebone->flag & BONE_UNSELECTABLE) == 0) {
1765                                 if (index & BONESEL_TIP) {
1766                                         ebone->flag |= BONE_DONE;
1767                                         if (select) ebone->flag |= BONE_TIPSEL;
1768                                         else ebone->flag &= ~BONE_TIPSEL;
1769                                 }
1770                                 
1771                                 if (index & BONESEL_ROOT) {
1772                                         ebone->flag |= BONE_DONE;
1773                                         if (select) ebone->flag |= BONE_ROOTSEL;
1774                                         else ebone->flag &= ~BONE_ROOTSEL;
1775                                 }
1776                         }
1777                 }
1778         }
1779         
1780         /* now we have to flush tag from parents... */
1781         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
1782                 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
1783                         if (ebone->parent->flag & BONE_DONE)
1784                                 ebone->flag |= BONE_DONE;
1785                 }
1786         }
1787         
1788         /* only select/deselect entire bones when no points where in the rect */
1789         for (a = 0; a < hits; a++) {
1790                 int index = buffer[(4 * a) + 3];
1791                 if (index != -1) {
1792                         ebone = BLI_findlink(arm->edbo, index & ~(BONESEL_ANY));
1793                         if (index & BONESEL_BONE) {
1794                                 if ((ebone->flag & BONE_UNSELECTABLE) == 0) {
1795                                         if (!(ebone->flag & BONE_DONE)) {
1796                                                 if (select)
1797                                                         ebone->flag |= (BONE_ROOTSEL | BONE_TIPSEL | BONE_SELECTED);
1798                                                 else
1799                                                         ebone->flag &= ~(BONE_ROOTSEL | BONE_TIPSEL | BONE_SELECTED);
1800                                         }
1801                                 }
1802                         }
1803                 }
1804         }
1805         
1806         ED_armature_sync_selection(arm->edbo);
1807         
1808         return OPERATOR_CANCELLED;
1809 }
1810
1811 static int do_object_pose_box_select(bContext *C, ViewContext *vc, rcti *rect, int select, int extend)
1812 {
1813         Bone *bone;
1814         Object *ob = vc->obact;
1815         unsigned int *vbuffer = NULL; /* selection buffer       */
1816         unsigned int *col;          /* color in buffer  */
1817         int bone_only;
1818         int bone_selected = 0;
1819         int totobj = MAXPICKBUF; // XXX solve later
1820         short hits;
1821         
1822         if ((ob) && (ob->mode & OB_MODE_POSE))
1823                 bone_only = 1;
1824         else
1825                 bone_only = 0;
1826         
1827         if (extend == 0 && select) {
1828                 if (bone_only) {
1829                         CTX_DATA_BEGIN (C, bPoseChannel *, pchan, visible_pose_bones)
1830                         {
1831                                 if ((pchan->bone->flag & BONE_UNSELECTABLE) == 0) {
1832                                         pchan->bone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1833                                 }
1834                         }
1835                         CTX_DATA_END;
1836                 }
1837                 else {
1838                         object_deselect_all_visible(vc->scene, vc->v3d);
1839                 }
1840         }
1841
1842         /* selection buffer now has bones potentially too, so we add MAXPICKBUF */
1843         vbuffer = MEM_mallocN(4 * (totobj + MAXPICKBUF) * sizeof(unsigned int), "selection buffer");
1844         hits = view3d_opengl_select(vc, vbuffer, 4 * (totobj + MAXPICKBUF), rect);
1845         /*
1846          * LOGIC NOTES (theeth):
1847          * The buffer and ListBase have the same relative order, which makes the selection
1848          * very simple. Loop through both data sets at the same time, if the color
1849          * is the same as the object, we have a hit and can move to the next color
1850          * and object pair, if not, just move to the next object,
1851          * keeping the same color until we have a hit.
1852          * 
1853          * The buffer order is defined by OGL standard, hopefully no stupid GFX card
1854          * does it incorrectly.
1855          */
1856
1857         if (hits > 0) { /* no need to loop if there's no hit */
1858                 Base *base;
1859                 col = vbuffer + 3;
1860                 
1861                 for (base = vc->scene->base.first; base && hits; base = base->next) {
1862                         if (BASE_SELECTABLE(vc->v3d, base)) {
1863                                 while (base->selcol == (*col & 0xFFFF)) {   /* we got an object */
1864                                         
1865                                         if (*col & 0xFFFF0000) {                    /* we got a bone */
1866                                                 bone = get_indexed_bone(base->object, *col & ~(BONESEL_ANY));
1867                                                 if (bone) {
1868                                                         if (select) {
1869                                                                 if ((bone->flag & BONE_UNSELECTABLE) == 0) {
1870                                                                         bone->flag |= BONE_SELECTED;
1871                                                                         bone_selected = 1;
1872 // XXX                                                                  select_actionchannel_by_name(base->object->action, bone->name, 1);
1873                                                                 }
1874                                                         }
1875                                                         else {
1876                                                                 bArmature *arm = base->object->data;
1877                                                                 bone->flag &= ~BONE_SELECTED;
1878 // XXX                                                                  select_actionchannel_by_name(base->object->action, bone->name, 0);
1879                                                                 if (arm->act_bone == bone)
1880                                                                         arm->act_bone = NULL;
1881                                                                 
1882                                                         }
1883                                                 }
1884                                         }
1885                                         else if (!bone_only) {
1886                                                 if (select)
1887                                                         ED_base_object_select(base, BA_SELECT);
1888                                                 else
1889                                                         ED_base_object_select(base, BA_DESELECT);
1890                                         }
1891
1892                                         col += 4; /* next color */
1893                                         hits--;
1894                                         if (hits == 0) break;
1895                                 }
1896                         }
1897                         
1898                         if (bone_selected) {
1899                                 WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, base->object);
1900                         }
1901                 }
1902
1903                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, vc->scene);
1904
1905         }
1906         MEM_freeN(vbuffer);
1907
1908         return hits > 0 ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1909 }
1910
1911 static int view3d_borderselect_exec(bContext *C, wmOperator *op)
1912 {
1913         ViewContext vc;
1914         rcti rect;
1915         short extend;
1916         short select;
1917
1918         int ret = OPERATOR_CANCELLED;
1919
1920         view3d_operator_needs_opengl(C);
1921
1922         /* setup view context for argument to callbacks */
1923         view3d_set_viewcontext(C, &vc);
1924         
1925         select = (RNA_int_get(op->ptr, "gesture_mode") == GESTURE_MODAL_SELECT);
1926         rect.xmin = RNA_int_get(op->ptr, "xmin");
1927         rect.ymin = RNA_int_get(op->ptr, "ymin");
1928         rect.xmax = RNA_int_get(op->ptr, "xmax");
1929         rect.ymax = RNA_int_get(op->ptr, "ymax");
1930         extend = RNA_boolean_get(op->ptr, "extend");
1931
1932         if (vc.obedit) {
1933                 switch (vc.obedit->type) {
1934                         case OB_MESH:
1935                                 vc.em = BMEdit_FromObject(vc.obedit);
1936                                 ret = do_mesh_box_select(&vc, &rect, select, extend);
1937 //                      if (EM_texFaceCheck())
1938                                 if (ret & OPERATOR_FINISHED) {
1939                                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
1940                                 }
1941                                 break;
1942                         case OB_CURVE:
1943                         case OB_SURF:
1944                                 ret = do_nurbs_box_select(&vc, &rect, select, extend);
1945                                 if (ret & OPERATOR_FINISHED) {
1946                                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
1947                                 }
1948                                 break;
1949                         case OB_MBALL:
1950                                 ret = do_meta_box_select(&vc, &rect, select, extend);
1951                                 if (ret & OPERATOR_FINISHED) {
1952                                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
1953                                 }
1954                                 break;
1955                         case OB_ARMATURE:
1956                                 ret = do_armature_box_select(&vc, &rect, select, extend);
1957                                 if (ret & OPERATOR_FINISHED) {
1958                                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, vc.obedit);
1959                                 }
1960                                 break;
1961                         case OB_LATTICE:
1962                                 ret = do_lattice_box_select(&vc, &rect, select, extend);
1963                                 if (ret & OPERATOR_FINISHED) {
1964                                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
1965                                 }
1966                                 break;
1967                         default:
1968                                 assert(!"border select on incorrect object type");
1969                 }
1970         }
1971         else {  /* no editmode, unified for bones and objects */
1972                 if (vc.obact && vc.obact->mode & OB_MODE_SCULPT) {
1973                         /* pass */
1974                 }
1975                 else if (vc.obact && paint_facesel_test(vc.obact)) {
1976                         ret = do_paintface_box_select(&vc, &rect, select, extend);
1977                 }
1978                 else if (vc.obact && paint_vertsel_test(vc.obact)) {
1979                         ret = do_paintvert_box_select(&vc, &rect, select, extend);
1980                 }
1981                 else if (vc.obact && vc.obact->mode & OB_MODE_PARTICLE_EDIT) {
1982                         ret = PE_border_select(C, &rect, select, extend);
1983                 }
1984                 else { /* object mode with none active */
1985                         ret = do_object_pose_box_select(C, &vc, &rect, select, extend);
1986                 }
1987         }
1988
1989         return ret;
1990
1991
1992
1993 /* *****************Selection Operators******************* */
1994
1995 /* ****** Border Select ****** */
1996 void VIEW3D_OT_select_border(wmOperatorType *ot)
1997 {
1998         /* identifiers */
1999         ot->name = "Border Select";
2000         ot->description = "Select items using border selection";
2001         ot->idname = "VIEW3D_OT_select_border";
2002         
2003         /* api callbacks */
2004         ot->invoke = WM_border_select_invoke;
2005         ot->exec = view3d_borderselect_exec;
2006         ot->modal = WM_border_select_modal;
2007         ot->poll = view3d_selectable_data;
2008         ot->cancel = WM_border_select_cancel;
2009         
2010         /* flags */
2011         ot->flag = OPTYPE_UNDO;
2012         
2013         /* rna */
2014         WM_operator_properties_gesture_border(ot, TRUE);
2015 }
2016
2017 /* much like facesel_face_pick()*/
2018 /* returns 0 if not found, otherwise 1 */
2019 static int vertsel_vert_pick(struct bContext *C, Mesh *me, const int mval[2], unsigned int *index, int size)
2020 {
2021         ViewContext vc;
2022         view3d_set_viewcontext(C, &vc);
2023
2024         if (!me || me->totvert == 0)
2025                 return 0;
2026
2027         if (size > 0) {
2028                 /* sample rect to increase changes of selecting, so that when clicking
2029                  * on an face in the backbuf, we can still select a vert */
2030
2031                 int dist;
2032                 *index = view3d_sample_backbuf_rect(&vc, mval, size, 1, me->totvert + 1, &dist, 0, NULL, NULL);
2033         }
2034         else {
2035                 /* sample only on the exact position */
2036                 *index = view3d_sample_backbuf(&vc, mval[0], mval[1]);
2037         }
2038
2039         if ((*index) <= 0 || (*index) > (unsigned int)me->totvert)
2040                 return 0;
2041
2042         (*index)--;
2043         
2044         return 1;
2045 }
2046
2047 /* mouse selection in weight paint */
2048 /* gets called via generic mouse select operator */
2049 static int mouse_weight_paint_vertex_select(bContext *C, const int mval[2], short extend, short deselect, short toggle, Object *obact)
2050 {
2051         Mesh *me = obact->data; /* already checked for NULL */
2052         unsigned int index = 0;
2053         MVert *mv;
2054
2055         if (vertsel_vert_pick(C, me, mval, &index, 50)) {
2056                 mv = me->mvert + index;
2057                 if (extend) {
2058                         mv->flag |= SELECT;
2059                 }
2060                 else if (deselect) {
2061                         mv->flag &= ~SELECT;
2062                 }
2063                 else if (toggle) {
2064                         mv->flag ^= SELECT;
2065                 }
2066                 else {
2067                         paintvert_deselect_all_visible(obact, SEL_DESELECT, FALSE);
2068                         mv->flag |= SELECT;
2069                 }
2070                 paintvert_flush_flags(obact);
2071                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obact->data);
2072                 return 1;
2073         }
2074         return 0;
2075 }
2076
2077 /* ****** Mouse Select ****** */
2078
2079
2080 static int view3d_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
2081 {
2082         Object *obedit = CTX_data_edit_object(C);
2083         Object *obact = CTX_data_active_object(C);
2084         short extend = RNA_boolean_get(op->ptr, "extend");
2085         short deselect = RNA_boolean_get(op->ptr, "deselect");
2086         short toggle = RNA_boolean_get(op->ptr, "toggle");
2087         short center = RNA_boolean_get(op->ptr, "center");
2088         short enumerate = RNA_boolean_get(op->ptr, "enumerate");
2089         short object = RNA_boolean_get(op->ptr, "object");
2090         int retval = 0;
2091
2092         view3d_operator_needs_opengl(C);
2093
2094         if (object) {
2095                 obedit = NULL;
2096                 obact = NULL;
2097
2098                 /* ack, this is incorrect but to do this correctly we would need an
2099                  * alternative editmode/objectmode keymap, this copies the functionality
2100                  * from 2.4x where Ctrl+Select in editmode does object select only */
2101                 center = FALSE;
2102         }
2103
2104         if (obedit && object == FALSE) {
2105                 if (obedit->type == OB_MESH)
2106                         retval = mouse_mesh(C, event->mval, extend, deselect, toggle);
2107                 else if (obedit->type == OB_ARMATURE)
2108                         retval = mouse_armature(C, event->mval, extend, deselect, toggle);
2109                 else if (obedit->type == OB_LATTICE)
2110                         retval = mouse_lattice(C, event->mval, extend, deselect, toggle);
2111                 else if (ELEM(obedit->type, OB_CURVE, OB_SURF))
2112                         retval = mouse_nurb(C, event->mval, extend, deselect, toggle);
2113                 else if (obedit->type == OB_MBALL)
2114                         retval = mouse_mball(C, event->mval, extend, deselect, toggle);
2115                         
2116         }
2117         else if (obact && obact->mode & OB_MODE_SCULPT)
2118                 return OPERATOR_CANCELLED;
2119         else if (obact && obact->mode & OB_MODE_PARTICLE_EDIT)
2120                 return PE_mouse_particles(C, event->mval, extend, deselect, toggle);
2121         else if (obact && paint_facesel_test(obact))
2122                 retval = paintface_mouse_select(C, obact, event->mval, extend, deselect, toggle);
2123         else if (paint_vertsel_test(obact))
2124                 retval = mouse_weight_paint_vertex_select(C, event->mval, extend, deselect, toggle, obact);
2125         else
2126                 retval = mouse_select(C, event->mval, extend, deselect, toggle, center, enumerate);
2127
2128         /* passthrough allows tweaks
2129          * FINISHED to signal one operator worked
2130          * */
2131         if (retval)
2132                 return OPERATOR_PASS_THROUGH | OPERATOR_FINISHED;
2133         else
2134                 return OPERATOR_PASS_THROUGH;  /* nothing selected, just passthrough */
2135 }
2136
2137 void VIEW3D_OT_select(wmOperatorType *ot)
2138 {
2139         /* identifiers */
2140         ot->name = "Activate/Select";
2141         ot->description = "Activate/select item(s)";
2142         ot->idname = "VIEW3D_OT_select";
2143         
2144         /* api callbacks */
2145         ot->invoke = view3d_select_invoke;
2146         ot->poll = ED_operator_view3d_active;
2147         
2148         /* flags */
2149         ot->flag = OPTYPE_UNDO;
2150         
2151         /* properties */
2152         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everything first");
2153         RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Remove from selection");
2154         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle Selection", "Toggles selection");
2155         RNA_def_boolean(ot->srna, "center", 0, "Center", "Use the object center when selecting, in editmode used to extend object selection");
2156         RNA_def_boolean(ot->srna, "enumerate", 0, "Enumerate", "List objects under the mouse (object mode only)");
2157         RNA_def_boolean(ot->srna, "object", 0, "Object", "Use object selection (editmode only)");
2158 }
2159
2160
2161 /* -------------------- circle select --------------------------------------------- */
2162
2163 typedef struct CircleSelectUserData {
2164         ViewContext *vc;
2165         short select;
2166         int mval[2];
2167         float radius;
2168 } CircleSelectUserData;
2169
2170 static void mesh_circle_doSelectVert(void *userData, BMVert *eve, int x, int y, int UNUSED(index))
2171 {
2172         CircleSelectUserData *data = userData;
2173         int mx = x - data->mval[0], my = y - data->mval[1];
2174         float r = sqrt(mx * mx + my * my);
2175
2176         if (r <= data->radius) {
2177                 BM_vert_select_set(data->vc->em->bm, eve, data->select);
2178         }
2179 }
2180 static void mesh_circle_doSelectEdge(void *userData, BMEdge *eed, int x0, int y0, int x1, int y1, int UNUSED(index))
2181 {
2182         CircleSelectUserData *data = userData;
2183
2184         if (edge_inside_circle(data->mval[0], data->mval[1], (short) data->radius, x0, y0, x1, y1)) {
2185                 BM_edge_select_set(data->vc->em->bm, eed, data->select);
2186         }
2187 }
2188 static void mesh_circle_doSelectFace(void *userData, BMFace *efa, int x, int y, int UNUSED(index))
2189 {
2190         CircleSelectUserData *data = userData;
2191         int mx = x - data->mval[0], my = y - data->mval[1];
2192         float r = sqrt(mx * mx + my * my);
2193         
2194         if (r <= data->radius) {
2195                 BM_face_select_set(data->vc->em->bm, efa, data->select);
2196         }
2197 }
2198
2199 static void mesh_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2200 {
2201         ToolSettings *ts = vc->scene->toolsettings;
2202         int bbsel;
2203         CircleSelectUserData data;
2204         
2205         bbsel = EDBM_backbuf_circle_init(vc, mval[0], mval[1], (short)(rad + 1.0f));
2206         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
2207
2208         vc->em = BMEdit_FromObject(vc->obedit);
2209
2210         data.vc = vc;
2211         data.select = select;
2212         data.mval[0] = mval[0];
2213         data.mval[1] = mval[1];
2214         data.radius = rad;
2215
2216         if (ts->selectmode & SCE_SELECT_VERTEX) {
2217                 if (bbsel) {
2218                         edbm_backbuf_check_and_select_verts(vc->em, select == LEFTMOUSE);
2219                 }
2220                 else {
2221                         mesh_foreachScreenVert(vc, mesh_circle_doSelectVert, &data, V3D_CLIP_TEST_RV3D_CLIPPING);
2222                 }
2223         }
2224
2225         if (ts->selectmode & SCE_SELECT_EDGE) {
2226                 if (bbsel) {
2227                         edbm_backbuf_check_and_select_edges(vc->em, select == LEFTMOUSE);
2228                 }
2229                 else {
2230                         mesh_foreachScreenEdge(vc, mesh_circle_doSelectEdge, &data, V3D_CLIP_TEST_OFF);
2231                 }
2232         }
2233         
2234         if (ts->selectmode & SCE_SELECT_FACE) {
2235                 if (bbsel) {
2236                         edbm_backbuf_check_and_select_faces(vc->em, select == LEFTMOUSE);
2237                 }
2238                 else {
2239                         mesh_foreachScreenFace(vc, mesh_circle_doSelectFace, &data);
2240                 }
2241         }
2242
2243         EDBM_backbuf_free();
2244         EDBM_selectmode_flush(vc->em);
2245 }
2246
2247 static void paint_facesel_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2248 {
2249         Object *ob = vc->obact;
2250         Mesh *me = ob ? ob->data : NULL;
2251         /* int bbsel; */ /* UNUSED */
2252
2253         if (me) {
2254                 bm_vertoffs = me->totpoly + 1; /* max index array */
2255
2256                 /* bbsel= */ /* UNUSED */ EDBM_backbuf_circle_init(vc, mval[0], mval[1], (short)(rad + 1.0f));
2257                 edbm_backbuf_check_and_select_tfaces(me, select == LEFTMOUSE);
2258                 EDBM_backbuf_free();
2259         }
2260 }
2261
2262
2263 static void paint_vertsel_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2264 {
2265         Object *ob = vc->obact;
2266         Mesh *me = ob ? ob->data : NULL;
2267         /* int bbsel; */ /* UNUSED */
2268         /* CircleSelectUserData data = {NULL}; */ /* UNUSED */
2269         if (me) {
2270                 bm_vertoffs = me->totvert + 1; /* max index array */
2271
2272                 /* bbsel= */ /* UNUSED */ EDBM_backbuf_circle_init(vc, mval[0], mval[1], (short)(rad + 1.0f));
2273                 edbm_backbuf_check_and_select_verts_obmode(me, select == LEFTMOUSE);
2274                 EDBM_backbuf_free();
2275
2276                 paintvert_flush_flags(ob);
2277         }
2278 }
2279
2280
2281 static void nurbscurve_circle_doSelect(void *userData, Nurb *UNUSED(nu), BPoint *bp, BezTriple *bezt, int beztindex, int x, int y)
2282 {
2283         CircleSelectUserData *data = userData;
2284         int mx = x - data->mval[0], my = y - data->mval[1];
2285         float r = sqrt(mx * mx + my * my);
2286         Object *obedit = data->vc->obedit;
2287         Curve *cu = (Curve *)obedit->data;
2288
2289         if (r <= data->radius) {
2290                 if (bp) {
2291                         bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
2292
2293                         if (bp == cu->lastsel && !(bp->f1 & 1)) cu->lastsel = NULL;
2294                 }
2295                 else {
2296                         if (cu->drawflag & CU_HIDE_HANDLES) {
2297                                 /* can only be beztindex==0 here since handles are hidden */
2298                                 bezt->f1 = bezt->f2 = bezt->f3 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
2299                         }
2300                         else {
2301                                 if (beztindex == 0) {
2302                                         bezt->f1 = data->select ? (bezt->f1 | SELECT) : (bezt->f1 & ~SELECT);
2303                                 }
2304                                 else if (beztindex == 1) {
2305                                         bezt->f2 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
2306                                 }
2307                                 else {
2308                                         bezt->f3 = data->select ? (bezt->f3 | SELECT) : (bezt->f3 & ~SELECT);
2309                                 }
2310                         }
2311
2312                         if (bezt == cu->lastsel && !(bezt->f2 & 1)) cu->lastsel = NULL;
2313                 }
2314         }
2315 }
2316 static void nurbscurve_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2317 {
2318         CircleSelectUserData data;
2319
2320         /* set vc-> edit data */
2321         
2322         data.select = select;
2323         data.mval[0] = mval[0];
2324         data.mval[1] = mval[1];
2325         data.radius = rad;
2326         data.vc = vc;
2327
2328         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
2329         nurbs_foreachScreenVert(vc, nurbscurve_circle_doSelect, &data);
2330 }
2331
2332
2333 static void latticecurve_circle_doSelect(void *userData, BPoint *bp, int x, int y)
2334 {
2335         CircleSelectUserData *data = userData;
2336         int mx = x - data->mval[0], my = y - data->mval[1];
2337         float r = sqrt(mx * mx + my * my);
2338
2339         if (r <= data->radius) {
2340                 bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
2341         }
2342 }
2343 static void lattice_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2344 {
2345         CircleSelectUserData data;
2346
2347         /* set vc-> edit data */
2348         
2349         data.select = select;
2350         data.mval[0] = mval[0];
2351         data.mval[1] = mval[1];
2352         data.radius = rad;
2353
2354         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
2355         lattice_foreachScreenVert(vc, latticecurve_circle_doSelect, &data);
2356 }
2357
2358
2359 // NOTE: pose-bone case is copied from editbone case...
2360 static short pchan_circle_doSelectJoint(void *userData, bPoseChannel *pchan, int x, int y)
2361 {
2362         CircleSelectUserData *data = userData;
2363         int mx = x - data->mval[0], my = y - data->mval[1];
2364         float r = sqrt(mx * mx + my * my);
2365         
2366         if (r <= data->radius) {
2367                 if (data->select)
2368                         pchan->bone->flag |= BONE_SELECTED;
2369                 else
2370                         pchan->bone->flag &= ~BONE_SELECTED;
2371                 return 1;
2372         }
2373         return 0;
2374 }
2375 static void pose_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2376 {
2377         CircleSelectUserData data;
2378         bArmature *arm = vc->obact->data;
2379         bPose *pose = vc->obact->pose;
2380         bPoseChannel *pchan;
2381         int change = FALSE;
2382         
2383         /* set vc->edit data */
2384         data.select = select;
2385         data.mval[0] = mval[0];
2386         data.mval[1] = mval[1];
2387         data.radius = rad;
2388
2389         ED_view3d_init_mats_rv3d(vc->obact, vc->rv3d); /* for foreach's screen/vert projection */
2390         
2391         /* check each PoseChannel... */
2392         // TODO: could be optimized at some point
2393         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
2394                 short sco1[2], sco2[2], didpoint = 0;
2395                 float vec[3];
2396                 
2397                 /* skip invisible bones */
2398                 if (PBONE_VISIBLE(arm, pchan->bone) == 0)
2399                         continue;
2400                 
2401                 /* project head location to screenspace */
2402                 mul_v3_m4v3(vec, vc->obact->obmat, pchan->pose_head);
2403                 project_short(vc->ar, vec, sco1);
2404                 
2405                 /* project tail location to screenspace */
2406                 mul_v3_m4v3(vec, vc->obact->obmat, pchan->pose_tail);
2407                 project_short(vc->ar, vec, sco2);
2408                 
2409                 /* check if the head and/or tail is in the circle 
2410                  *      - the call to check also does the selection already
2411                  */
2412                 if (pchan_circle_doSelectJoint(&data, pchan, sco1[0], sco1[1]))
2413                         didpoint = 1;
2414                 if (pchan_circle_doSelectJoint(&data, pchan, sco2[0], sco2[1]))
2415                         didpoint = 1;
2416                 
2417                 change |= didpoint;
2418         }
2419
2420         if (change) {
2421                 WM_main_add_notifier(NC_OBJECT | ND_BONE_SELECT, vc->obact);
2422         }
2423 }
2424
2425 static short armature_circle_doSelectJoint(void *userData, EditBone *ebone, int x, int y, short head)
2426 {
2427         CircleSelectUserData *data = userData;
2428         int mx = x - data->mval[0], my = y - data->mval[1];
2429         float r = sqrt(mx * mx + my * my);
2430         
2431         if (r <= data->radius) {
2432                 if (head) {
2433                         if (data->select)
2434                                 ebone->flag |= BONE_ROOTSEL;
2435                         else 
2436                                 ebone->flag &= ~BONE_ROOTSEL;
2437                 }
2438                 else {
2439                         if (data->select)
2440                                 ebone->flag |= BONE_TIPSEL;
2441                         else 
2442                                 ebone->flag &= ~BONE_TIPSEL;
2443                 }
2444                 return 1;
2445         }
2446         return 0;
2447 }
2448 static void armature_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2449 {
2450         CircleSelectUserData data;
2451         bArmature *arm = vc->obedit->data;
2452         EditBone *ebone;
2453         int change = FALSE;
2454         
2455         /* set vc->edit data */
2456         data.select = select;
2457         data.mval[0] = mval[0];
2458         data.mval[1] = mval[1];
2459         data.radius = rad;
2460
2461         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
2462         
2463         /* check each EditBone... */
2464         // TODO: could be optimized at some point
2465         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
2466                 short sco1[2], sco2[2], didpoint = 0;
2467                 float vec[3];
2468                 
2469                 /* project head location to screenspace */
2470                 mul_v3_m4v3(vec, vc->obedit->obmat, ebone->head);
2471                 project_short(vc->ar, vec, sco1);
2472                 
2473                 /* project tail location to screenspace */
2474                 mul_v3_m4v3(vec, vc->obedit->obmat, ebone->tail);
2475                 project_short(vc->ar, vec, sco2);
2476                 
2477                 /* check if the head and/or tail is in the circle 
2478                  *      - the call to check also does the selection already
2479                  */
2480                 if (armature_circle_doSelectJoint(&data, ebone, sco1[0], sco1[1], 1))
2481                         didpoint = 1;
2482                 if (armature_circle_doSelectJoint(&data, ebone, sco2[0], sco2[1], 0))
2483                         didpoint = 1;
2484                         
2485                 /* only if the endpoints didn't get selected, deal with the middle of the bone too */
2486                 // XXX should we just do this always?
2487                 if ( (didpoint == 0) && edge_inside_circle(mval[0], mval[1], rad, sco1[0], sco1[1], sco2[0], sco2[1]) ) {
2488                         if (select) 
2489                                 ebone->flag |= BONE_TIPSEL | BONE_ROOTSEL | BONE_SELECTED;
2490                         else 
2491                                 ebone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
2492                         change = TRUE;
2493                 }
2494                 
2495                 change |= didpoint;
2496         }
2497
2498         if (change) {
2499                 ED_armature_sync_selection(arm->edbo);
2500                 ED_armature_validate_active(arm);
2501                 WM_main_add_notifier(NC_OBJECT | ND_BONE_SELECT, vc->obedit);
2502         }
2503 }
2504
2505 /** Callbacks for circle selection in Editmode */
2506
2507 static void obedit_circle_select(ViewContext *vc, short select, const int mval[2], float rad)
2508 {
2509         switch (vc->obedit->type) {
2510                 case OB_MESH:
2511                         mesh_circle_select(vc, select, mval, rad);
2512                         break;
2513                 case OB_CURVE:
2514                 case OB_SURF:
2515                         nurbscurve_circle_select(vc, select, mval, rad);
2516                         break;
2517                 case OB_LATTICE:
2518                         lattice_circle_select(vc, select, mval, rad);
2519                         break;
2520                 case OB_ARMATURE:
2521                         armature_circle_select(vc, select, mval, rad);
2522                         break;
2523                 default:
2524                         return;
2525         }
2526 }
2527
2528 /* not a real operator, only for circle test */
2529 static int view3d_circle_select_exec(bContext *C, wmOperator *op)
2530 {
2531         ScrArea *sa = CTX_wm_area(C);
2532         ARegion *ar = CTX_wm_region(C);
2533         Scene *scene = CTX_data_scene(C);
2534         Object *obact = CTX_data_active_object(C);
2535         View3D *v3d = sa->spacedata.first;
2536         int x = RNA_int_get(op->ptr, "x");
2537         int y = RNA_int_get(op->ptr, "y");
2538         int radius = RNA_int_get(op->ptr, "radius");
2539         int gesture_mode = RNA_int_get(op->ptr, "gesture_mode");
2540         int select;
2541         
2542         select = (gesture_mode == GESTURE_MODAL_SELECT);
2543
2544         if (CTX_data_edit_object(C) || paint_facesel_test(obact) || paint_vertsel_test(obact) ||
2545             (obact && (obact->mode & (OB_MODE_PARTICLE_EDIT | OB_MODE_POSE))) )
2546         {
2547                 ViewContext vc;
2548                 int mval[2];
2549                 
2550                 view3d_operator_needs_opengl(C);
2551                 
2552                 view3d_set_viewcontext(C, &vc);
2553                 mval[0] = x;
2554                 mval[1] = y;
2555
2556                 if (CTX_data_edit_object(C)) {
2557                         obedit_circle_select(&vc, select, mval, (float)radius);
2558                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obact->data);
2559                 }
2560                 else if (paint_facesel_test(obact)) {
2561                         paint_facesel_circle_select(&vc, select, mval, (float)radius);
2562                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obact->data);
2563                 }
2564                 else if (paint_vertsel_test(obact)) {
2565                         paint_vertsel_circle_select(&vc, select, mval, (float)radius);
2566                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obact->data);
2567                 }
2568                 else if (obact->mode & OB_MODE_POSE)
2569                         pose_circle_select(&vc, select, mval, (float)radius);
2570                 else
2571                         return PE_circle_select(C, select, mval, (float)radius);
2572         }
2573         else if (obact && obact->mode & OB_MODE_SCULPT) {
2574                 return OPERATOR_CANCELLED;
2575         }
2576         else {
2577                 Base *base;
2578                 select = select ? BA_SELECT : BA_DESELECT;
2579                 for (base = FIRSTBASE; base; base = base->next) {
2580                         if (BASE_SELECTABLE(v3d, base)) {
2581                                 project_short(ar, base->object->obmat[3], &base->sx);
2582                                 if (base->sx != IS_CLIPPED) {
2583                                         int dx = base->sx - x;
2584                                         int dy = base->sy - y;
2585                                         if (dx * dx + dy * dy < radius * radius)
2586                                                 ED_base_object_select(base, select);
2587                                 }
2588                         }
2589                 }
2590                 
2591                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
2592         }
2593         
2594         return OPERATOR_FINISHED;
2595 }
2596
2597 void VIEW3D_OT_select_circle(wmOperatorType *ot)
2598 {
2599         ot->name = "Circle Select";
2600         ot->description = "Select items using circle selection";
2601         ot->idname = "VIEW3D_OT_select_circle";
2602         
2603         ot->invoke = WM_gesture_circle_invoke;
2604         ot->modal = WM_gesture_circle_modal;
2605         ot->exec = view3d_circle_select_exec;
2606         ot->poll = view3d_selectable_data;
2607         ot->cancel = WM_gesture_circle_cancel;
2608         
2609         /* flags */
2610         ot->flag = OPTYPE_UNDO;
2611         
2612         RNA_def_int(ot->srna, "x", 0, INT_MIN, INT_MAX, "X", "", INT_MIN, INT_MAX);
2613         RNA_def_int(ot->srna, "y", 0, INT_MIN, INT_MAX, "Y", "", INT_MIN, INT_MAX);
2614         RNA_def_int(ot->srna, "radius", 0, INT_MIN, INT_MAX, "Radius", "", INT_MIN, INT_MAX);
2615         RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
2616 }