68 lines
2.1 KiB
C++
68 lines
2.1 KiB
C++
#include "SolidAnimation.h"
|
|
#include "../Static.h"
|
|
|
|
SolidAnimation::SolidAnimation() : Figment("Solid") {
|
|
}
|
|
|
|
void SolidAnimation::randomize() {
|
|
m_isRandom = true;
|
|
m_blobs.forEach([](Blob& blob) {
|
|
blob.setPos(random(140));
|
|
blob.setBrightness(random(255));
|
|
if (random(254) % 2) {
|
|
blob.setVelocity(-1);
|
|
}
|
|
});
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
void SolidAnimation::loop() {
|
|
if (!m_isRandom) {
|
|
randomize();
|
|
}
|
|
EVERY_N_MILLIS(20) {
|
|
m_changePct.update(1);
|
|
m_red.update(1);
|
|
m_green.update(1);
|
|
m_blue.update(1);
|
|
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();
|
|
}
|
|
m_noiseOffset += 1;
|
|
}
|
|
|
|
void SolidAnimation::render(Display* dpy) const {
|
|
Surface sfc = Surface(dpy, {0, 0}, {255, 255});
|
|
uint8_t noiseY = sin8(m_noiseOffset % 255);
|
|
uint8_t noiseX = cos8(m_noiseOffset % 255);
|
|
CHSV color(rgb2hsv_approximate(CRGB(m_red, m_green, m_blue)));
|
|
|
|
sfc.paintShader([=](CRGB& pixel, const VirtualCoordinates& coords, const PhysicalCoordinates, const VirtualCoordinates& surfaceCoords) {
|
|
uint8_t brightness = inoise8(noiseX + surfaceCoords.x, noiseY + surfaceCoords.y);
|
|
uint8_t saturation = inoise8(noiseY + surfaceCoords.y, noiseX + surfaceCoords.x);
|
|
nblend(pixel, CHSV(color.h, std::max((uint8_t)128, saturation), scale8(color.v, brightness)), 128);
|
|
});
|
|
m_blobs.render(dpy);
|
|
}
|
|
STATIC_ALLOC(SolidAnimation);
|
|
STATIC_TASK(SolidAnimation);
|