mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:07:36 +00:00
LibGUI: Simplify AbstractZoomPanWidget code
No functional changes, just making things a bit easier to read.
This commit is contained in:
parent
b8a53e4a17
commit
5882d891d0
1 changed files with 29 additions and 26 deletions
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
|
* Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
|
||||||
|
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -16,10 +17,10 @@ void AbstractZoomPanWidget::set_scale(float new_scale)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m_scale = clamp(new_scale, m_min_scale, m_max_scale);
|
m_scale = clamp(new_scale, m_min_scale, m_max_scale);
|
||||||
Gfx::IntSize new_size;
|
m_content_rect.set_size({
|
||||||
new_size.set_width(m_original_rect.width() * m_scale);
|
m_original_rect.width() * m_scale,
|
||||||
new_size.set_height(m_original_rect.height() * m_scale);
|
m_original_rect.height() * m_scale,
|
||||||
m_content_rect.set_size(new_size);
|
});
|
||||||
|
|
||||||
if (on_scale_change)
|
if (on_scale_change)
|
||||||
on_scale_change(m_scale);
|
on_scale_change(m_scale);
|
||||||
|
@ -44,7 +45,7 @@ void AbstractZoomPanWidget::scale_centered(float new_scale, Gfx::IntPoint const&
|
||||||
|
|
||||||
Gfx::FloatPoint focus_point {
|
Gfx::FloatPoint focus_point {
|
||||||
center.x() - width() / 2.0f,
|
center.x() - width() / 2.0f,
|
||||||
center.y() - height() / 2.0f
|
center.y() - height() / 2.0f,
|
||||||
};
|
};
|
||||||
m_origin = (m_origin + focus_point) * (new_scale / m_scale) - focus_point;
|
m_origin = (m_origin + focus_point) * (new_scale / m_scale) - focus_point;
|
||||||
set_scale(new_scale);
|
set_scale(new_scale);
|
||||||
|
@ -77,35 +78,39 @@ void AbstractZoomPanWidget::pan_to(Gfx::IntPoint const& position)
|
||||||
|
|
||||||
Gfx::FloatPoint AbstractZoomPanWidget::frame_to_content_position(Gfx::IntPoint const& frame_position) const
|
Gfx::FloatPoint AbstractZoomPanWidget::frame_to_content_position(Gfx::IntPoint const& frame_position) const
|
||||||
{
|
{
|
||||||
Gfx::FloatPoint content_position;
|
return {
|
||||||
content_position.set_x(((float)frame_position.x() - (float)m_content_rect.x()) / m_scale);
|
(static_cast<float>(frame_position.x()) - m_content_rect.x()) / m_scale,
|
||||||
content_position.set_y(((float)frame_position.y() - (float)m_content_rect.y()) / m_scale);
|
(static_cast<float>(frame_position.y()) - m_content_rect.y()) / m_scale,
|
||||||
return content_position;
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Gfx::FloatRect AbstractZoomPanWidget::frame_to_content_rect(Gfx::IntRect const& frame_rect) const
|
Gfx::FloatRect AbstractZoomPanWidget::frame_to_content_rect(Gfx::IntRect const& frame_rect) const
|
||||||
{
|
{
|
||||||
Gfx::FloatRect content_rect;
|
Gfx::FloatRect content_rect;
|
||||||
content_rect.set_location(frame_to_content_position(frame_rect.location()));
|
content_rect.set_location(frame_to_content_position(frame_rect.location()));
|
||||||
content_rect.set_width((float)frame_rect.width() / m_scale);
|
content_rect.set_size({
|
||||||
content_rect.set_height((float)frame_rect.height() / m_scale);
|
frame_rect.width() / m_scale,
|
||||||
|
frame_rect.height() / m_scale,
|
||||||
|
});
|
||||||
return content_rect;
|
return content_rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
Gfx::FloatPoint AbstractZoomPanWidget::content_to_frame_position(Gfx::IntPoint const& content_position) const
|
Gfx::FloatPoint AbstractZoomPanWidget::content_to_frame_position(Gfx::IntPoint const& content_position) const
|
||||||
{
|
{
|
||||||
Gfx::FloatPoint frame_position;
|
return {
|
||||||
frame_position.set_x(m_content_rect.x() + ((float)content_position.x() * m_scale));
|
m_content_rect.x() + content_position.x() * m_scale,
|
||||||
frame_position.set_y(m_content_rect.y() + ((float)content_position.y() * m_scale));
|
m_content_rect.y() + content_position.y() * m_scale,
|
||||||
return frame_position;
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Gfx::FloatRect AbstractZoomPanWidget::content_to_frame_rect(Gfx::IntRect const& content_rect) const
|
Gfx::FloatRect AbstractZoomPanWidget::content_to_frame_rect(Gfx::IntRect const& content_rect) const
|
||||||
{
|
{
|
||||||
Gfx::FloatRect frame_rect;
|
Gfx::FloatRect frame_rect;
|
||||||
frame_rect.set_location(content_to_frame_position(content_rect.location()));
|
frame_rect.set_location(content_to_frame_position(content_rect.location()));
|
||||||
frame_rect.set_width((float)content_rect.width() * m_scale);
|
frame_rect.set_size({
|
||||||
frame_rect.set_height((float)content_rect.height() * m_scale);
|
content_rect.width() * m_scale,
|
||||||
|
content_rect.height() * m_scale,
|
||||||
|
});
|
||||||
return frame_rect;
|
return frame_rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,12 +157,10 @@ void AbstractZoomPanWidget::relayout()
|
||||||
if (m_original_rect.is_null())
|
if (m_original_rect.is_null())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Gfx::IntSize new_size = m_content_rect.size();
|
m_content_rect.set_location({
|
||||||
|
(width() / 2) - (m_content_rect.width() / 2) - m_origin.x(),
|
||||||
Gfx::IntPoint new_location;
|
(height() / 2) - (m_content_rect.height() / 2) - m_origin.y(),
|
||||||
new_location.set_x((width() / 2) - (new_size.width() / 2) - m_origin.x());
|
});
|
||||||
new_location.set_y((height() / 2) - (new_size.height() / 2) - m_origin.y());
|
|
||||||
m_content_rect.set_location(new_location);
|
|
||||||
|
|
||||||
handle_relayout(m_content_rect);
|
handle_relayout(m_content_rect);
|
||||||
}
|
}
|
||||||
|
@ -184,8 +187,8 @@ void AbstractZoomPanWidget::fit_content_to_rect(Gfx::IntRect const& viewport_rec
|
||||||
{
|
{
|
||||||
const float border_ratio = 0.95f;
|
const float border_ratio = 0.95f;
|
||||||
auto image_size = m_original_rect.size();
|
auto image_size = m_original_rect.size();
|
||||||
auto height_ratio = floorf(border_ratio * viewport_rect.height()) / (float)image_size.height();
|
auto height_ratio = floorf(border_ratio * viewport_rect.height()) / image_size.height();
|
||||||
auto width_ratio = floorf(border_ratio * viewport_rect.width()) / (float)image_size.width();
|
auto width_ratio = floorf(border_ratio * viewport_rect.width()) / image_size.width();
|
||||||
|
|
||||||
float new_scale = 1.0f;
|
float new_scale = 1.0f;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
@ -201,7 +204,7 @@ void AbstractZoomPanWidget::fit_content_to_rect(Gfx::IntRect const& viewport_rec
|
||||||
}
|
}
|
||||||
|
|
||||||
auto const& offset = rect().center() - viewport_rect.center();
|
auto const& offset = rect().center() - viewport_rect.center();
|
||||||
set_origin(Gfx::FloatPoint(offset.x(), offset.y()));
|
set_origin({ offset.x(), offset.y() });
|
||||||
set_scale(new_scale);
|
set_scale(new_scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue