Beispiel #1
0
bool mouse_lattice(bContext *C, const int mval[2], bool extend, bool deselect, bool toggle)
{
	ViewContext vc;
	BPoint *bp = NULL;

	view3d_set_viewcontext(C, &vc);
	bp = findnearestLattvert(&vc, mval, TRUE);

	if (bp) {
		if (extend) {
			bp->f1 |= SELECT;
		}
		else if (deselect) {
			bp->f1 &= ~SELECT;
		}
		else if (toggle) {
			bp->f1 ^= SELECT;  /* swap */
		}
		else {
			ED_setflagsLatt(vc.obedit, 0);
			bp->f1 |= SELECT;
		}

		WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);

		return true;
	}

	return false;
}
Beispiel #2
0
void em_setup_viewcontext(bContext *C, ViewContext *vc)
{
	view3d_set_viewcontext(C, vc);
	
	if(vc->obedit) {
		Mesh *me= vc->obedit->data;
		vc->em= me->edit_mesh;
	}
}
Beispiel #3
0
static void clip_planes_from_rect(bContext *C,
                                  float clip_planes[4][4],
                                  const rcti *rect)
{
	ViewContext vc;
	BoundBox bb;
	bglMats mats = {{0}};
	
	view3d_operator_needs_opengl(C);
	view3d_set_viewcontext(C, &vc);
	view3d_get_transformation(vc.ar, vc.rv3d, vc.obact, &mats);
	ED_view3d_calc_clipping(&bb, clip_planes, &mats, rect);
	mul_m4_fl(clip_planes, -1.0f);
}
static int edcu_shortest_path_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
	Object *obedit = CTX_data_edit_object(C);
	Curve *cu = obedit->data;
	Nurb *nu_src = BKE_curve_nurb_active_get(cu);
	int vert_src = cu->actvert;

	ViewContext vc;
	Nurb *nu_dst;
	BezTriple *bezt_dst;
	BPoint *bp_dst;
	int vert_dst;
	void *vert_dst_p;

	if (vert_src == CU_ACT_NONE) {
		return OPERATOR_PASS_THROUGH;
	}

	view3d_operator_needs_opengl(C);
	view3d_set_viewcontext(C, &vc);

	if (!ED_curve_pick_vert(&vc, 1, event->mval, &nu_dst, &bezt_dst, &bp_dst, NULL)) {
		return OPERATOR_PASS_THROUGH;
	}

	if (nu_src != nu_dst) {
		BKE_report(op->reports, RPT_ERROR, "Control point belongs to another spline");
		return OPERATOR_CANCELLED;
	}

	vert_dst_p = bezt_dst ? (void *)bezt_dst : (void *)bp_dst;
	vert_dst = BKE_curve_nurb_vert_index_get(nu_dst, vert_dst_p);
	if (vert_src == vert_dst) {
		return OPERATOR_CANCELLED;
	}

	if ((obedit->type == OB_SURF) && (nu_src->pntsv > 1)) {
		curve_select_shortest_path_surf(nu_src, vert_src, vert_dst);
	}
	else {
		curve_select_shortest_path_curve(nu_src, vert_src, vert_dst);
	}

	BKE_curve_nurb_vert_active_set(cu, nu_dst, vert_dst_p);

	WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
	return OPERATOR_FINISHED;
}
Beispiel #5
0
/* x and y are mouse coords (area space) */
void *get_nearest_bone(bContext *C, short findunsel, int x, int y)
{
	ViewContext vc;
	rcti rect;
	unsigned int buffer[MAXPICKBUF];
	short hits;
	
	view3d_set_viewcontext(C, &vc);
	
	// rect.xmin = ... mouseco!
	rect.xmin = rect.xmax = x;
	rect.ymin = rect.ymax = y;
	
	hits = view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect, true);

	if (hits > 0)
		return get_bone_from_selectbuffer(vc.scene, vc.scene->basact, buffer, hits, findunsel, true);
	
	return NULL;
}
static int select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
	Object *obedit = CTX_data_edit_object(C);
	ViewContext vc;
	Nurb *nu;
	BezTriple *bezt;
	BPoint *bp;
	int a;
	const bool select = !RNA_boolean_get(op->ptr, "deselect");

	view3d_operator_needs_opengl(C);
	view3d_set_viewcontext(C, &vc);

	if (!ED_curve_pick_vert(&vc, 1, event->mval, &nu, &bezt, &bp, NULL)) {
		return OPERATOR_CANCELLED;
	}

	if (bezt) {
		a = nu->pntsu;
		bezt = nu->bezt;
		while (a--) {
			select_beztriple(bezt, select, SELECT, VISIBLE);
			bezt++;
		}
	}
	else if (bp) {
		a = nu->pntsu * nu->pntsv;
		bp = nu->bp;
		while (a--) {
			select_bpoint(bp, select, SELECT, VISIBLE);
			bp++;
		}
	}

	WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
	if (!select) {
		BKE_curve_nurb_vert_active_validate(obedit->data);
	}

	return OPERATOR_FINISHED;
}
Beispiel #7
0
bool ED_lattice_select_pick(bContext *C, const int mval[2], bool extend, bool deselect, bool toggle)
{
	ViewContext vc;
	BPoint *bp = NULL;
	Lattice *lt;

	view3d_set_viewcontext(C, &vc);
	lt = ((Lattice *)vc.obedit->data)->editlatt->latt;
	bp = findnearestLattvert(&vc, mval, true);

	if (bp) {
		if (extend) {
			bp->f1 |= SELECT;
		}
		else if (deselect) {
			bp->f1 &= ~SELECT;
		}
		else if (toggle) {
			bp->f1 ^= SELECT;  /* swap */
		}
		else {
			ED_lattice_flags_set(vc.obedit, 0);
			bp->f1 |= SELECT;
		}

		if (bp->f1 & SELECT) {
			lt->actbp = bp - lt->def;
		}
		else {
			lt->actbp = LT_ACTBP_NONE;
		}

		WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);

		return true;
	}

	return false;
}
Beispiel #8
0
int mouse_lattice(bContext *C, const int mval[2], int extend)
{
	ViewContext vc;
	BPoint *bp= NULL;

	view3d_set_viewcontext(C, &vc);
	bp= findnearestLattvert(&vc, mval, 1);

	if(bp) {
		if(extend==0) {
			ED_setflagsLatt(vc.obedit, 0);
			bp->f1 |= SELECT;
		}
		else
			bp->f1 ^= SELECT; /* swap */

		WM_event_add_notifier(C, NC_GEOM|ND_SELECT, vc.obedit->data);

		return 1;
	}

	return 0;
}
Beispiel #9
0
/* Select MetaElement with mouse click (user can select radius circle or
 * stiffness circle) */
int mouse_mball(bContext *C, const int mval[2], int extend, int deselect, int toggle)
{
	static MetaElem *startelem = NULL;
	Object *obedit = CTX_data_edit_object(C);
	ViewContext vc;
	MetaBall *mb = (MetaBall *)obedit->data;
	MetaElem *ml, *ml_act = NULL;
	int a, hits;
	unsigned int buffer[4 * MAXPICKBUF];
	rcti rect;

	view3d_set_viewcontext(C, &vc);

	rect.xmin = mval[0] - 12;
	rect.xmax = mval[0] + 12;
	rect.ymin = mval[1] - 12;
	rect.ymax = mval[1] + 12;

	hits = view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect);

	/* does startelem exist? */
	ml = mb->editelems->first;
	while (ml) {
		if (ml == startelem) break;
		ml = ml->next;
	}

	if (ml == NULL) startelem = mb->editelems->first;
	
	if (hits > 0) {
		ml = startelem;
		while (ml) {
			for (a = 0; a < hits; a++) {
				/* index converted for gl stuff */
				if (ml->selcol1 == buffer[4 * a + 3]) {
					ml->flag |= MB_SCALE_RAD;
					ml_act = ml;
				}
				if (ml->selcol2 == buffer[4 * a + 3]) {
					ml->flag &= ~MB_SCALE_RAD;
					ml_act = ml;
				}
			}
			if (ml_act) break;
			ml = ml->next;
			if (ml == NULL) ml = mb->editelems->first;
			if (ml == startelem) break;
		}
		
		/* When some metaelem was found, then it is necessary to select or
		 * deselect it. */
		if (ml_act) {
			if (extend) {
				ml_act->flag |= SELECT;
			}
			else if (deselect) {
				ml_act->flag &= ~SELECT;
			}
			else if (toggle) {
				if (ml_act->flag & SELECT)
					ml_act->flag &= ~SELECT;
				else
					ml_act->flag |= SELECT;
			}
			else {
				/* Deselect all existing metaelems */
				BKE_mball_deselect_all(mb);

				/* Select only metaelem clicked on */
				ml_act->flag |= SELECT;
			}
			
			mb->lastelem = ml_act;
			
			WM_event_add_notifier(C, NC_GEOM | ND_SELECT, mb);

			return 1;
		}
	}

	return 0;
}
Beispiel #10
0
/* context: editmode armature in view3d */
bool mouse_armature(bContext *C, const int mval[2], bool extend, bool deselect, bool toggle)
{
	Object *obedit = CTX_data_edit_object(C);
	bArmature *arm = obedit->data;
	ViewContext vc;
	EditBone *nearBone = NULL;
	int selmask;

	view3d_set_viewcontext(C, &vc);
	
	BIF_sk_selectStroke(C, mval, extend);
	
	nearBone = get_nearest_editbonepoint(&vc, mval, arm->edbo, 1, &selmask);
	if (nearBone) {

		if (!extend && !deselect && !toggle)
			ED_armature_deselect_all(obedit, 0);
		
		/* by definition the non-root connected bones have no root point drawn,
		 * so a root selection needs to be delivered to the parent tip */
		
		if (selmask & BONE_SELECTED) {
			if (nearBone->parent && (nearBone->flag & BONE_CONNECTED)) {
				/* click in a chain */
				if (extend) {
					/* select this bone */
					nearBone->flag |= BONE_TIPSEL;
					nearBone->parent->flag |= BONE_TIPSEL;
				}
				else if (deselect) {
					/* deselect this bone */
					nearBone->flag &= ~(BONE_TIPSEL | BONE_SELECTED);
					/* only deselect parent tip if it is not selected */
					if (!(nearBone->parent->flag & BONE_SELECTED))
						nearBone->parent->flag &= ~BONE_TIPSEL;
				}
				else if (toggle) {
					/* hold shift inverts this bone's selection */
					if (nearBone->flag & BONE_SELECTED) {
						/* deselect this bone */
						nearBone->flag &= ~(BONE_TIPSEL | BONE_SELECTED);
						/* only deselect parent tip if it is not selected */
						if (!(nearBone->parent->flag & BONE_SELECTED))
							nearBone->parent->flag &= ~BONE_TIPSEL;
					}
					else {
						/* select this bone */
						nearBone->flag |= BONE_TIPSEL;
						nearBone->parent->flag |= BONE_TIPSEL;
					}
				}
				else {
					/* select this bone */
					nearBone->flag |= BONE_TIPSEL;
					nearBone->parent->flag |= BONE_TIPSEL;
				}
			}
			else {
				if (extend) {
					nearBone->flag |= (BONE_TIPSEL | BONE_ROOTSEL);
				}
				else if (deselect) {
					nearBone->flag &= ~(BONE_TIPSEL | BONE_ROOTSEL);
				}
				else if (toggle) {
					/* hold shift inverts this bone's selection */
					if (nearBone->flag & BONE_SELECTED)
						nearBone->flag &= ~(BONE_TIPSEL | BONE_ROOTSEL);
					else
						nearBone->flag |= (BONE_TIPSEL | BONE_ROOTSEL);
				}
				else
					nearBone->flag |= (BONE_TIPSEL | BONE_ROOTSEL);
			}
		}
		else {
			if (extend)
				nearBone->flag |= selmask;
			else if (deselect)
				nearBone->flag &= ~selmask;
			else if (toggle && (nearBone->flag & selmask))
				nearBone->flag &= ~selmask;
			else
				nearBone->flag |= selmask;
		}
		
		ED_armature_sync_selection(arm->edbo);
		
		if (nearBone) {
			/* then now check for active status */
			if (ebone_select_flag(nearBone)) {
				arm->act_edbone = nearBone;
			}
		}
		
		WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, vc.obedit);
		return true;
	}

	return false;
}
Beispiel #11
0
/* used for both 3d view and image window */
void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool texpaint_proj, bool use_palette)
{
	Scene *scene = CTX_data_scene(C);
	Paint *paint = BKE_paint_get_active_from_context(C);
	Palette *palette = BKE_paint_palette(paint);
	PaletteColor *color;
	Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
	unsigned int col;
	const unsigned char *cp;

	CLAMP(x, 0, ar->winx);
	CLAMP(y, 0, ar->winy);
	
	if (use_palette) {
		if (!palette) {
			palette = BKE_palette_add(CTX_data_main(C), "Palette");
			BKE_paint_palette_set(paint, palette);
		}

		color = BKE_palette_color_add(palette);
	}


	if (CTX_wm_view3d(C) && texpaint_proj) {
		/* first try getting a colour directly from the mesh faces if possible */
		Object *ob = OBACT;
		bool sample_success = false;

		if (ob) {
			DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);

			ViewContext vc;
			const int mval[2] = {x, y};
			unsigned int faceindex;
			unsigned int totface = dm->getNumTessFaces(dm);
			MTFace *dm_mtface = dm->getTessFaceDataArray(dm, CD_MTFACE);

			DM_update_materials(dm, ob);

			if (dm_mtface) {
				view3d_set_viewcontext(C, &vc);

				view3d_operator_needs_opengl(C);

				if (imapaint_pick_face(&vc, mval, &faceindex, totface)) {
					Image *image = imapaint_face_image(dm, faceindex);

					ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
					if (ibuf && ibuf->rect) {
						float uv[2];
						float u, v;
						imapaint_pick_uv(scene, ob, faceindex, mval, uv);
						sample_success = true;

						u = fmodf(uv[0], 1.0f);
						v = fmodf(uv[1], 1.0f);

						if (u < 0.0f) u += 1.0f;
						if (v < 0.0f) v += 1.0f;

						u = u * ibuf->x - 0.5f;
						v = v * ibuf->y - 0.5f;

						if (ibuf->rect_float) {
							float rgba_f[4];
							bilinear_interpolation_color_wrap(ibuf, NULL, rgba_f, u, v);
							straight_to_premul_v4(rgba_f);
							if (use_palette) {
								linearrgb_to_srgb_v3_v3(color->rgb, rgba_f);
							}
							else {
								linearrgb_to_srgb_v3_v3(rgba_f, rgba_f);
								BKE_brush_color_set(scene, br, rgba_f);
							}
						}
						else {
							unsigned char rgba[4];
							bilinear_interpolation_color_wrap(ibuf, rgba, NULL, u, v);
							if (use_palette) {
								rgb_uchar_to_float(color->rgb, rgba);
							}
							else {
								float rgba_f[3];
								rgb_uchar_to_float(rgba_f, rgba);
								BKE_brush_color_set(scene, br, rgba_f);
							}
						}
					}

					BKE_image_release_ibuf(image, ibuf, NULL);
				}
			}
			dm->release(dm);
		}

		if (!sample_success) {
			glReadBuffer(GL_FRONT);
			glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
			glReadBuffer(GL_BACK);
		}
		else
			return;
	}
	else {
		glReadBuffer(GL_FRONT);
		glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
		glReadBuffer(GL_BACK);
	}
	cp = (unsigned char *)&col;
	
	if (use_palette) {
		rgb_uchar_to_float(color->rgb, cp);
	}
	else {
		float rgba_f[3];
		rgb_uchar_to_float(rgba_f, cp);
		BKE_brush_color_set(scene, br, rgba_f);
	}
}