Several improvements
- Make all random functions const - Remove make_black() call from setup() - Add optional size_changed() override
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
#include "ScreensaverPlugin.hpp"
|
#include "ScreensaverPlugin.hpp"
|
||||||
|
#include <cairo/cairo.h>
|
||||||
#include <libscnumerics.hpp>
|
#include <libscnumerics.hpp>
|
||||||
|
|
||||||
void ScreensaverPlugin::setup(cairo_t* context, const cairo_rectangle_t& rect) {
|
void ScreensaverPlugin::setup(cairo_t* context, const cairo_rectangle_t& rect) {
|
||||||
_c = context;
|
_c = context;
|
||||||
_r = rect;
|
_r = rect;
|
||||||
make_black();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int ScreensaverPlugin::fps() const {
|
int ScreensaverPlugin::fps() const {
|
||||||
@@ -17,11 +17,11 @@ void ScreensaverPlugin::make_black() {
|
|||||||
cairo_fill(_c);
|
cairo_fill(_c);
|
||||||
}
|
}
|
||||||
|
|
||||||
cairo_rectangle_t ScreensaverPlugin::random_rect() {
|
cairo_rectangle_t ScreensaverPlugin::random_rect() const {
|
||||||
return random_rect_in_rect(_r);
|
return random_rect_in_rect(_r);
|
||||||
}
|
}
|
||||||
|
|
||||||
cairo_rectangle_t ScreensaverPlugin::random_rect_in_rect(const cairo_rectangle_t& rect) {
|
cairo_rectangle_t ScreensaverPlugin::random_rect_in_rect(const cairo_rectangle_t& rect) const {
|
||||||
cairo_rectangle_t r;
|
cairo_rectangle_t r;
|
||||||
std::uniform_real_distribution<double> dx {rect.x, rect.width};
|
std::uniform_real_distribution<double> dx {rect.x, rect.width};
|
||||||
std::uniform_real_distribution<double> dy {rect.y, rect.height};
|
std::uniform_real_distribution<double> dy {rect.y, rect.height};
|
||||||
@@ -47,22 +47,26 @@ cairo_rectangle_t ScreensaverPlugin::random_rect_in_rect(const cairo_rectangle_t
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
double ScreensaverPlugin::random_x() {
|
bool ScreensaverPlugin::random_bool() const {
|
||||||
|
return sc::random::boolean();
|
||||||
|
}
|
||||||
|
|
||||||
|
double ScreensaverPlugin::random_x() const {
|
||||||
std::uniform_real_distribution<double> dist {0, _r.width};
|
std::uniform_real_distribution<double> dist {0, _r.width};
|
||||||
return dist(sc::random::instance().engine());
|
return dist(sc::random::instance().engine());
|
||||||
}
|
}
|
||||||
|
|
||||||
double ScreensaverPlugin::random_y() {
|
double ScreensaverPlugin::random_y() const {
|
||||||
std::uniform_real_distribution<double> dist {0, _r.height};
|
std::uniform_real_distribution<double> dist {0, _r.height};
|
||||||
return dist(sc::random::instance().engine());
|
return dist(sc::random::instance().engine());
|
||||||
}
|
}
|
||||||
|
|
||||||
double ScreensaverPlugin::random01() {
|
double ScreensaverPlugin::random01() const {
|
||||||
std::uniform_real_distribution<double> dist {};
|
std::uniform_real_distribution<double> dist {};
|
||||||
return dist(sc::random::instance().engine());
|
return dist(sc::random::instance().engine());
|
||||||
}
|
}
|
||||||
|
|
||||||
double ScreensaverPlugin::random_between(double v1, double v2) {
|
double ScreensaverPlugin::random_between(double v1, double v2) const {
|
||||||
std::uniform_real_distribution<double> dist {v1, v2};
|
std::uniform_real_distribution<double> dist {v1, v2};
|
||||||
return dist(sc::random::instance().engine());
|
return dist(sc::random::instance().engine());
|
||||||
}
|
}
|
||||||
@@ -100,3 +104,7 @@ void ScreensaverPlugin::config(nlohmann::json&& data) {
|
|||||||
void ScreensaverPlugin::configure() {
|
void ScreensaverPlugin::configure() {
|
||||||
// empty default implementation
|
// empty default implementation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScreensaverPlugin::size_changed() {
|
||||||
|
// empty default implementation
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ class ScreensaverPlugin {
|
|||||||
|
|
||||||
virtual void setup(cairo_t* context, const cairo_rectangle_t& rect);
|
virtual void setup(cairo_t* context, const cairo_rectangle_t& rect);
|
||||||
virtual void configure();
|
virtual void configure();
|
||||||
|
virtual void size_changed();
|
||||||
virtual int fps() const;
|
virtual int fps() const;
|
||||||
|
|
||||||
virtual std::string version() const = 0;
|
virtual std::string version() const = 0;
|
||||||
@@ -27,12 +28,13 @@ class ScreensaverPlugin {
|
|||||||
nlohmann::json _j {};
|
nlohmann::json _j {};
|
||||||
|
|
||||||
void make_black();
|
void make_black();
|
||||||
cairo_rectangle_t random_rect();
|
cairo_rectangle_t random_rect() const;
|
||||||
cairo_rectangle_t random_rect_in_rect(const cairo_rectangle_t& rect);
|
cairo_rectangle_t random_rect_in_rect(const cairo_rectangle_t& rect) const;
|
||||||
double random_x();
|
bool random_bool() const;
|
||||||
double random_y();
|
double random_x() const;
|
||||||
double random01();
|
double random_y() const;
|
||||||
double random_between(double v1, double v2);
|
double random01() const;
|
||||||
|
double random_between(double v1, double v2) const;
|
||||||
void rounded_rect(const cairo_rectangle_t& rect, double radius);
|
void rounded_rect(const cairo_rectangle_t& rect, double radius);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user