Image view selected didn't account for region size

Now fit the selection with a 1.4x margin (matching 3d view).

Also move border fitting into own function.
This commit is contained in:
Campbell Barton
2016-04-28 13:25:26 +10:00
parent 6e94ec623d
commit 42e6477129

View File

@@ -149,6 +149,34 @@ static void sima_zoom_set_factor(SpaceImage *sima, ARegion *ar, float zoomfac, c
sima_zoom_set(sima, ar, sima->zoom * zoomfac, location); sima_zoom_set(sima, ar, sima->zoom * zoomfac, location);
} }
/**
* Fits the view to the bounds exactly, caller should add margin if needed.
*/
static void sima_zoom_set_from_bounds(SpaceImage *sima, ARegion *ar, const rctf *bounds)
{
int image_size[2];
float aspx, aspy;
ED_space_image_get_size(sima, &image_size[0], &image_size[1]);
ED_space_image_get_aspect(sima, &aspx, &aspy);
image_size[0] = image_size[0] * aspx;
image_size[1] = image_size[1] * aspy;
/* adjust offset and zoom */
sima->xof = roundf((BLI_rctf_cent_x(bounds) - 0.5f) * image_size[0]);
sima->yof = roundf((BLI_rctf_cent_y(bounds) - 0.5f) * image_size[1]);
float size_xy[2], size;
size_xy[0] = BLI_rcti_size_x(&ar->winrct) / (BLI_rctf_size_x(bounds) * image_size[0]);
size_xy[1] = BLI_rcti_size_y(&ar->winrct) / (BLI_rctf_size_y(bounds) * image_size[1]);
size = min_ff(size_xy[0], size_xy[1]);
CLAMP_MAX(size, 100.0f);
sima_zoom_set(sima, ar, size, NULL);
}
#if 0 // currently unused #if 0 // currently unused
static int image_poll(bContext *C) static int image_poll(bContext *C)
{ {
@@ -763,8 +791,6 @@ static int image_view_selected_exec(bContext *C, wmOperator *UNUSED(op))
Scene *scene; Scene *scene;
Object *obedit; Object *obedit;
Image *ima; Image *ima;
float size, min[2], max[2], d[2], aspx, aspy;
int width, height;
/* retrieve state */ /* retrieve state */
sima = CTX_wm_space_image(C); sima = CTX_wm_space_image(C);
@@ -773,33 +799,28 @@ static int image_view_selected_exec(bContext *C, wmOperator *UNUSED(op))
obedit = CTX_data_edit_object(C); obedit = CTX_data_edit_object(C);
ima = ED_space_image(sima); ima = ED_space_image(sima);
ED_space_image_get_size(sima, &width, &height);
ED_space_image_get_aspect(sima, &aspx, &aspy);
width = width * aspx;
height = height * aspy;
/* get bounds */ /* get bounds */
float min[2], max[2];
if (ED_space_image_show_uvedit(sima, obedit)) { if (ED_space_image_show_uvedit(sima, obedit)) {
if (!ED_uvedit_minmax(scene, ima, obedit, min, max)) if (!ED_uvedit_minmax(scene, ima, obedit, min, max)) {
return OPERATOR_CANCELLED; return OPERATOR_CANCELLED;
}
} }
else if (ED_space_image_check_show_maskedit(scene, sima)) { else if (ED_space_image_check_show_maskedit(scene, sima)) {
if (!ED_mask_selected_minmax(C, min, max)) { if (!ED_mask_selected_minmax(C, min, max)) {
return OPERATOR_CANCELLED; return OPERATOR_CANCELLED;
} }
} }
rctf bounds = {
.xmin = min[0], .ymin = min[1],
.xmax = max[0], .ymax = max[1],
};
/* adjust offset and zoom */ /* add some margin */
sima->xof = (int)(((min[0] + max[0]) * 0.5f - 0.5f) * width); BLI_rctf_scale(&bounds, 1.4f);
sima->yof = (int)(((min[1] + max[1]) * 0.5f - 0.5f) * height);
d[0] = max[0] - min[0]; sima_zoom_set_from_bounds(sima, ar, &bounds);
d[1] = max[1] - min[1];
size = 0.5f * MAX2(d[0], d[1]) * MAX2(width, height) / 256.0f;
if (size <= 0.01f) size = 0.01f;
sima_zoom_set(sima, ar, 0.7f / size, NULL);
ED_region_tag_redraw(ar); ED_region_tag_redraw(ar);