renderbug/src/animations/SolidAnimation.cpp

73 lines
2.0 KiB
C++
Raw Normal View History

#include "SolidAnimation.h"
#include "../Static.h"
2019-05-10 05:17:29 +00:00
SolidAnimation::SolidAnimation() : Figment("Solid", Task::Stopped) {
}
2019-05-10 05:17:29 +00:00
void SolidAnimation::randomize() {
m_isRandom = true;
m_blobs.forEach([](Blob& blob) {
blob.setPos(random(140));
blob.setBrightness(random(255));
if (random(255) % 2) {
blob.setVelocity(-1);
2019-05-10 05:17:29 +00:00
}
});
}
void SolidAnimation::handleEvent(const InputEvent& evt) {
if (evt.intent == InputEvent::SetColor) {
CRGB nextColor = evt.asRGB();
m_red.set(nextColor.red);
m_green.set(nextColor.green);
m_blue.set(nextColor.blue);
m_changePct.set(0, 255);
m_prevColor = m_curColor;
m_curColor = nextColor;
m_horizontal = !m_horizontal;
} else if (evt.intent == InputEvent::Beat) {
m_isRandom = false;
2019-05-10 05:17:29 +00:00
}
}
2019-05-10 05:17:29 +00:00
void SolidAnimation::loop() {
if (!m_isRandom) {
randomize();
2019-05-10 05:17:29 +00:00
}
m_red.update(15);
m_green.update(15);
m_blue.update(15);
EVERY_N_MILLIS(16) {
m_changePct.update(12);
}
EVERY_N_MILLIS(6) {
CRGB rgb{m_red, m_green, m_blue};
CHSV hsv = rgb2hsv_approximate(rgb);
m_blobs.forEach([=](Blob& blob) {
blob.setHue(hsv.hue);
blob.setSaturation(hsv.saturation);
});
m_blobs.update();
}
}
#include <Perfcounter.h>
2019-05-10 05:17:29 +00:00
void SolidAnimation::render(Display* dpy) const {
PerfCounter _("solidRender");
CRGB color(m_red.value(), m_green.value(), m_blue.value());
uint8_t frame = ease8InOutApprox(m_changePct);
if (frame == 255) {
Surface(dpy, {0, 0}, {255, 255}) = color;
} else {
uint8_t cutoff = (frame / 2);
uint8_t rotation = m_horizontal ? 0 : 128;
Surface(dpy, {0, 0}, {128 - cutoff, 255}, rotation) = m_prevColor;
Surface(dpy, {128 - cutoff, 0}, {128 + cutoff, 255}, rotation) = color;
Surface(dpy, {128 + cutoff, 0}, {255, 255}, rotation) = m_prevColor;
2019-05-10 05:17:29 +00:00
}
m_blobs.render(dpy);
}
STATIC_ALLOC(SolidAnimation);
STATIC_TASK(SolidAnimation);