From 90e19e3211a91aae5b0e987d37823a2508bb064b Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Wed, 6 Oct 2021 17:06:12 +0200 Subject: [PATCH] Using configure method to build knots --- modules/Whirling/Whirling.cpp | 62 +++++++++++++---------------------- 1 file changed, 22 insertions(+), 40 deletions(-) diff --git a/modules/Whirling/Whirling.cpp b/modules/Whirling/Whirling.cpp index 248ee0e..4800c31 100644 --- a/modules/Whirling/Whirling.cpp +++ b/modules/Whirling/Whirling.cpp @@ -21,38 +21,10 @@ struct Lissajous { double line_width; cairo_rectangle_t frame; - Lissajous(double f0, - double f1, - double f2, - double f3, - double p0, - double p1, - double p2, - double p3, - int segments, - double shift); - double calc_x(double phi) const; double calc_y(double phi) const; }; -Lissajous::Lissajous(double f0, - double f1, - double f2, - double f3, - double p0, - double p1, - double p2, - double p3, - int segments, - double shift) -{ - f[0] = f0; f[1] = f1; f[2] = f2; f[3] = f3; - p[0] = p0; p[1] = p1; p[2] = p2; p[3] = p3; - steps = segments; - delta = shift; -} - double Lissajous::calc_x(double phi) const { return sin(f[0] * phi + p[0]) * cos(f[1] * phi + p[1]); } @@ -63,12 +35,13 @@ double Lissajous::calc_y(double phi) const { class Whirling : public ScreensaverPlugin { public: - Whirling(); + Whirling() = default; ~Whirling() = default; int fps() const override; void update() override; void render() override; + void configure() override; private: std::vector knots; @@ -80,17 +53,6 @@ ScreensaverPlugin* create_instance() { return new Whirling; } -Whirling::Whirling() { - knots.emplace_back(9, 7, 5, 7, - 0, 0, 0, 0, - 1000, - 2 * M_PI / 360.0); - Lissajous& knot = knots.back(); - knot.color = {1.0, 1.0, 1.0}; // white - knot.alpha = 1.0; // opaque - knot.line_width = 1.0; -} - int Whirling::fps() const { return 30; } @@ -131,3 +93,23 @@ void Whirling::render_knot(const Lissajous& knot) { cairo_set_line_width(_c, knot.line_width); cairo_stroke(_c); } + +void Whirling::configure() { + for (const auto& obj : _j["knots"]) { + Lissajous knot; + for (int i = 0; i < 4; ++i) { + knot.f[i] = obj["f"][i]; + } + for (int i = 0; i < 4; ++i) { + knot.p[i] = obj["p"][i]; + } + knot.steps = obj["steps"]; + knot.delta = obj["delta"]; + knot.color.r = obj["r"]; + knot.color.g = obj["g"]; + knot.color.b = obj["b"]; + knot.alpha = obj["a"]; + knot.line_width = obj["line_width"]; + knots.push_back(knot); + } +}