summaryrefslogtreecommitdiff
path: root/Userland/Demos
diff options
context:
space:
mode:
authorsin-ack <sin-ack@users.noreply.github.com>2021-08-08 14:01:23 +0000
committerGunnar Beutner <gunnar@beutner.name>2021-08-10 09:48:12 +0200
commitd28802ad226a5c750f843d0d011f98bb9247f3de (patch)
tree8f17003d43a018c598c7c8484347dd65ae84337c /Userland/Demos
parentfaec8bbe45d1b3ebed0e1fb73bab5a2f635e9ec1 (diff)
downloadserenity-d28802ad226a5c750f843d0d011f98bb9247f3de.zip
Mandelbrot: Add panning
Adds the ability to use the middle-mouse click to pan the current view.
Diffstat (limited to 'Userland/Demos')
-rw-r--r--Userland/Demos/Mandelbrot/Mandelbrot.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/Userland/Demos/Mandelbrot/Mandelbrot.cpp b/Userland/Demos/Mandelbrot/Mandelbrot.cpp
index b402f32b40..ce1229cda1 100644
--- a/Userland/Demos/Mandelbrot/Mandelbrot.cpp
+++ b/Userland/Demos/Mandelbrot/Mandelbrot.cpp
@@ -51,6 +51,19 @@ public:
calculate();
}
+ void pan_by(Gfx::IntPoint const& delta)
+ {
+ auto relative_width_pixel = (m_x_end - m_x_start) / m_bitmap->width();
+ auto relative_height_pixel = (m_y_end - m_y_start) / m_bitmap->height();
+
+ set_view(
+ m_x_start - delta.x() * relative_width_pixel,
+ m_x_end - delta.x() * relative_width_pixel,
+ m_y_start - delta.y() * relative_height_pixel,
+ m_y_end - delta.y() * relative_height_pixel);
+ calculate();
+ }
+
double mandelbrot(double px, double py, i32 max_iterations)
{
// Based on https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set
@@ -154,6 +167,9 @@ private:
Gfx::IntPoint m_selection_start;
Gfx::IntPoint m_selection_end;
+ bool m_panning { false };
+ Gfx::IntPoint m_last_pan_position;
+
MandelbrotSet m_set;
};
@@ -179,6 +195,12 @@ void Mandelbrot::mousedown_event(GUI::MouseEvent& event)
m_dragging = true;
update();
}
+ } else if (event.button() == GUI::MouseButton::Middle) {
+ if (!m_panning) {
+ m_last_pan_position = event.position();
+ m_panning = true;
+ update();
+ }
}
return GUI::Widget::mousedown_event(event);
@@ -199,6 +221,13 @@ void Mandelbrot::mousemove_event(GUI::MouseEvent& event)
update();
}
+ if (m_panning) {
+ m_set.pan_by(event.position() - m_last_pan_position);
+ m_last_pan_position = event.position();
+
+ update();
+ }
+
return GUI::Widget::mousemove_event(event);
}
@@ -210,6 +239,9 @@ void Mandelbrot::mouseup_event(GUI::MouseEvent& event)
m_set.zoom(selection);
m_dragging = false;
update();
+ } else if (event.button() == GUI::MouseButton::Middle) {
+ m_panning = false;
+ update();
} else if (event.button() == GUI::MouseButton::Right) {
m_set.reset();
update();