image: Combine clamp_view() and center_view ()
We need to clamp or center on a per axis basis. If the window is wider but the image is taller, we need to center horizontally but clamp vertically. We can only do that if by combining the two functions.
This commit is contained in:
+23
-40
@@ -69,17 +69,6 @@ get_scale(struct image *image)
|
|||||||
return image->matrix.xx;
|
return image->matrix.xx;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
center_view(struct image *image)
|
|
||||||
{
|
|
||||||
struct rectangle allocation;
|
|
||||||
double scale = get_scale(image);
|
|
||||||
|
|
||||||
widget_get_allocation(image->widget, &allocation);
|
|
||||||
image->matrix.x0 = (allocation.width - image->width * scale) / 2;
|
|
||||||
image->matrix.y0 = (allocation.height - image->height * scale) / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
clamp_view(struct image *image)
|
clamp_view(struct image *image)
|
||||||
{
|
{
|
||||||
@@ -91,14 +80,25 @@ clamp_view(struct image *image)
|
|||||||
sh = image->height * scale;
|
sh = image->height * scale;
|
||||||
widget_get_allocation(image->widget, &allocation);
|
widget_get_allocation(image->widget, &allocation);
|
||||||
|
|
||||||
if (image->matrix.x0 > 0.0)
|
if (sw < allocation.width) {
|
||||||
image->matrix.x0 = 0.0;
|
image->matrix.x0 =
|
||||||
if (image->matrix.y0 > 0.0)
|
(allocation.width - image->width * scale) / 2;
|
||||||
image->matrix.y0 = 0.0;
|
} else {
|
||||||
if (sw + image->matrix.x0 < allocation.width)
|
if (image->matrix.x0 > 0.0)
|
||||||
image->matrix.x0 = allocation.width - sw;
|
image->matrix.x0 = 0.0;
|
||||||
if (sh + image->matrix.y0 < allocation.height)
|
if (sw + image->matrix.x0 < allocation.width)
|
||||||
image->matrix.y0 = allocation.height - sh;
|
image->matrix.x0 = allocation.width - sw;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sh < allocation.width) {
|
||||||
|
image->matrix.y0 =
|
||||||
|
(allocation.height - image->height * scale) / 2;
|
||||||
|
} else {
|
||||||
|
if (image->matrix.y0 > 0.0)
|
||||||
|
image->matrix.y0 = 0.0;
|
||||||
|
if (sh + image->matrix.y0 < allocation.height)
|
||||||
|
image->matrix.y0 = allocation.height - sh;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -141,7 +141,7 @@ redraw_handler(struct widget *widget, void *data)
|
|||||||
image->height = height;
|
image->height = height;
|
||||||
cairo_matrix_init_scale(&image->matrix, scale, scale);
|
cairo_matrix_init_scale(&image->matrix, scale, scale);
|
||||||
|
|
||||||
center_view(image);
|
clamp_view(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix = image->matrix;
|
matrix = image->matrix;
|
||||||
@@ -166,7 +166,7 @@ resize_handler(struct widget *widget,
|
|||||||
{
|
{
|
||||||
struct image *image = data;
|
struct image *image = data;
|
||||||
|
|
||||||
center_view(image);
|
clamp_view(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -196,18 +196,6 @@ enter_handler(struct widget *widget,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
|
||||||
image_is_smaller(struct image *image)
|
|
||||||
{
|
|
||||||
double scale;
|
|
||||||
struct rectangle allocation;
|
|
||||||
|
|
||||||
scale = get_scale(image);
|
|
||||||
widget_get_allocation(image->widget, &allocation);
|
|
||||||
|
|
||||||
return scale * image->width < allocation.width;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
move_viewport(struct image *image, double dx, double dy)
|
move_viewport(struct image *image, double dx, double dy)
|
||||||
{
|
{
|
||||||
@@ -217,11 +205,7 @@ move_viewport(struct image *image, double dx, double dy)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
cairo_matrix_translate(&image->matrix, -dx/scale, -dy/scale);
|
cairo_matrix_translate(&image->matrix, -dx/scale, -dy/scale);
|
||||||
|
clamp_view(image);
|
||||||
if (image_is_smaller(image))
|
|
||||||
center_view(image);
|
|
||||||
else
|
|
||||||
clamp_view(image);
|
|
||||||
|
|
||||||
window_schedule_redraw(image->window);
|
window_schedule_redraw(image->window);
|
||||||
}
|
}
|
||||||
@@ -288,8 +272,7 @@ zoom(struct image *image, double scale)
|
|||||||
cairo_matrix_translate(&scale_matrix, -x, -y);
|
cairo_matrix_translate(&scale_matrix, -x, -y);
|
||||||
|
|
||||||
cairo_matrix_multiply(&image->matrix, &image->matrix, &scale_matrix);
|
cairo_matrix_multiply(&image->matrix, &image->matrix, &scale_matrix);
|
||||||
if (image_is_smaller(image))
|
clamp_view(image);
|
||||||
center_view(image);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|||||||
Reference in New Issue
Block a user