Adjust translation with mouse to be pixel-perfect, and zoom occurs around the screen-center now.

This commit is contained in:
2025-05-08 12:30:26 -05:00
parent 39c9e99c25
commit f5bcb4d3f0
3 changed files with 37 additions and 11 deletions

View File

@@ -35,6 +35,22 @@ enum class Fractal {
MandelbrotSet, JuliaSet
};
template <typename T>
struct Lerped {
T current;
T goal;
float rate = 1.f;
void Update(float elapsed);
};
template <>
inline void Lerped<float>::Update(float elapsed) { current = Math::Lerp(current, goal, elapsed*rate); }
template <>
inline void Lerped<Vector2>::Update(float elapsed) { current = Vector2::Lerp(current, goal, elapsed*rate); }
/// This class represents the FractalApp program state.
class FractalInspectorApp : public ReWindow::OpenGLWindow {
protected:

View File

@@ -3,9 +3,6 @@
/// (c) 2025 redacted.cc
/// This work is explicitly dedicated to the Public Domain.
#include <FractalApp.hpp>
int main(int argc, char** argv) {

View File

@@ -413,7 +413,7 @@ void FractalInspectorApp::Update(float elapsed) {
}
// TODO: Vary the rate appropriately.
float rate = elapsed * 0.125f;
float rate = elapsed * 0.25f;
if (IsKeyDown(Keys::LeftShift))
rate *= 0.125f;
@@ -421,10 +421,18 @@ void FractalInspectorApp::Update(float elapsed) {
if (IsKeyDown(Keys::LeftControl))
rate *= 0.125f;
if (IsKeyDown(Keys::Minus))
if (IsKeyDown(Keys::Minus)) {
u_scale += rate;
if (IsKeyDown(Keys::Equals))
// Stupid hack to make zoom originate at the center of the screen.
u_translation += Vector2(0.5f, 0.5f)*rate;
}
if (IsKeyDown(Keys::Equals)) {
u_scale -= rate;
// Stupid hack to make zoom originate at the center of the screen.
u_translation -= Vector2(0.5f, 0.5f)*rate;
}
if (IsKeyDown(Keys::DownArrow))
u_translation.y += rate;
@@ -447,10 +455,10 @@ void FractalInspectorApp::Update(float elapsed) {
// TODO: Consider moving this logic deeper into the shader to make it easier to work with.
float readable_scale = Math::Round(ReadableScale(), 3);
float readable_scale = Math::Round(ReadableScale(), 4);
std::string fps_text = std::format("Pos: {},{} Zoom: {}x FPS: {}",
Math::Round(u_translation.x, 2), Math::Round(u_translation.y, 2), readable_scale, fps);
Math::Round(u_translation.x, 4), Math::Round(u_translation.y, 4), readable_scale, fps);
fps_label->SetContent(fps_text);
}
@@ -580,11 +588,16 @@ void FractalInspectorApp::OnMouseMove(const ReWindow::MouseMoveEvent &e) {
// TODO: e.Delta is always 0, investigate in JUI.
Vector2 delta = nmp - last;
float bias_slowdown_as_we_zoom_in = 1.125f;
float base_slowdown = 0.005;
float base_slowdown = 768.f;
base_slowdown = GetHeight(); // TODO: Compute the actual aspect ratio.
// Invert vertical.
Vector2 pan_direction(delta.x, -delta.y);
float pan_rate = u_scale / base_slowdown;
if (panning)
u_translation += delta*(1.f / ReadableScale()*bias_slowdown_as_we_zoom_in)*base_slowdown;
u_translation += pan_direction*pan_rate;
last = nmp;
}