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