Texture stencil:

Robustness: Avoid translating the stencil outside the active area. Helps
to avoid losing the stencil somewhere in bitspace.

Usability: Take image repeat mapping and scaling into account when
fitting stencil aspect. Togglable by operator properties.
This commit is contained in:
Antony Riakiotakis 2013-04-14 09:43:12 +00:00
parent 4d0c576f4e
commit 1e839ec8bc

@ -468,6 +468,7 @@ typedef struct {
float init_rot;
float init_angle;
float lenorig;
float area_size[2];
StencilControlMode mode;
StencilConstraint constrain_mode;
Brush *br;
@ -480,6 +481,7 @@ static int stencil_control_invoke(bContext *C, wmOperator *op, const wmEvent *ev
Brush *br = BKE_paint_brush(paint);
float mdiff[2];
float mvalf[2] = {event->mval[0], event->mval[1]};
ARegion *ar = CTX_wm_region(C);
StencilControlData *scd = MEM_mallocN(sizeof(StencilControlData), "stencil_control");
@ -493,6 +495,8 @@ static int stencil_control_invoke(bContext *C, wmOperator *op, const wmEvent *ev
scd->init_angle = atan2(mdiff[1], mdiff[0]);
scd->mode = RNA_enum_get(op->ptr, "mode");
scd->event_type = event->type;
scd->area_size[0] = ar->winx;
scd->area_size[1] = ar->winy;
op->customdata = scd;
WM_event_add_modal_handler(C, op);
@ -515,6 +519,8 @@ static int stencil_control_cancel(bContext *UNUSED(C), wmOperator *op)
static void stencil_control_calculate(StencilControlData *scd, const int *mval)
{
#define PIXEL_MARGIN 5
float mdiff[2];
float mvalf[2] = {mval[0], mval[1]};
switch (scd->mode) {
@ -522,6 +528,14 @@ static void stencil_control_calculate(StencilControlData *scd, const int *mval)
sub_v2_v2v2(mdiff, mvalf, scd->init_mouse);
add_v2_v2v2(scd->br->stencil_pos, scd->init_spos,
mdiff);
CLAMP(scd->br->stencil_pos[0],
-scd->br->stencil_dimension[0] + PIXEL_MARGIN,
scd->area_size[0] + scd->br->stencil_dimension[0] - PIXEL_MARGIN);
CLAMP(scd->br->stencil_pos[1],
-scd->br->stencil_dimension[1] + PIXEL_MARGIN,
scd->area_size[1] + scd->br->stencil_dimension[1] - PIXEL_MARGIN);
break;
case STENCIL_SCALE:
{
@ -637,11 +651,13 @@ static void BRUSH_OT_stencil_control(wmOperatorType *ot)
}
static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *UNUSED(op))
static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *op)
{
Paint *paint = BKE_paint_get_active_from_context(C);
Brush *br = BKE_paint_brush(paint);
Tex *tex = (br)? br->mtex.tex : NULL;
bool use_scale = RNA_boolean_get(op->ptr, "use_scale");
bool use_repeat = RNA_boolean_get(op->ptr, "use_repeat");
if (tex && tex->type == TEX_IMAGE && tex->ima) {
float aspx, aspy;
@ -649,6 +665,16 @@ static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *UNUSED(op))
float orig_area, stencil_area, factor;
ED_image_get_uv_aspect(ima, NULL, &aspx, &aspy);
if (use_scale) {
aspx *= br->mtex.size[0];
aspy *= br->mtex.size[1];
}
if (use_repeat && tex->extend == TEX_REPEAT) {
aspx *= tex->xrepeat;
aspy *= tex->yrepeat;
}
orig_area = aspx * aspy;
stencil_area = br->stencil_dimension[0] * br->stencil_dimension[1];
@ -658,6 +684,8 @@ static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *UNUSED(op))
br->stencil_dimension[1] = factor * aspy;
}
WM_event_add_notifier(C, NC_WINDOW, NULL);
return OPERATOR_FINISHED;
}
@ -674,7 +702,10 @@ static void BRUSH_OT_stencil_fit_image_aspect(wmOperatorType *ot)
ot->poll = stencil_control_poll;
/* flags */
ot->flag = 0;
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_boolean(ot->srna, "use_repeat", 1, "Use Repeat", "Use repeat mapping values");
RNA_def_boolean(ot->srna, "use_scale", 1, "Use Scale", "Use texture scale values");
}