Added AnimationEffect::Blinker to quickly create a blinking sprite effect from an image file.

This commit is contained in:
Fabian Schlenz 2020-04-29 20:17:34 +02:00
parent 402d7f5d75
commit 1707084299
3 changed files with 28 additions and 5 deletions

View File

@ -11,12 +11,14 @@ private:
const char* name; const char* name;
uint16_t xOffset; uint16_t xOffset;
uint16_t yOffset; uint16_t yOffset;
unsigned long _last_blink_at;
uint16_t _blink_freq;
public: public:
AnimationEffect(const char* name) : AnimationEffect(name, 0x000000, 0, 0) {} AnimationEffect(const char* name, uint32_t bg_color=0x000000, int x=0, int y=0);
AnimationEffect(const char* name, uint32_t bg_color) : AnimationEffect(name, bg_color, 0, 0) {} static AnimationEffect* Blinker(const char* name, uint16_t interval, uint32_t color, uint32_t background_color=0x000000);
AnimationEffect(const char* name, uint32_t bg_color, int x, int y);
~AnimationEffect(); ~AnimationEffect();
AnimationEffect* setFgColor(uint32_t c); AnimationEffect* setFgColor(uint32_t c);
AnimationEffect* setBlinkFrequency(uint16_t);
void loop(uint16_t ms); void loop(uint16_t ms);
String get_name() override; String get_name() override;
}; };

View File

@ -9,6 +9,8 @@ AnimationEffect::AnimationEffect(const char* name, uint32_t bg, int x, int y) {
this->animation = new Animation(name, window); this->animation = new Animation(name, window);
this->animation->setBgColor(bg); this->animation->setBgColor(bg);
this->animation->setOffsets(this->xOffset, this->yOffset); this->animation->setOffsets(this->xOffset, this->yOffset);
_last_blink_at = millis();
} }
AnimationEffect* AnimationEffect::setFgColor(uint32_t c) { AnimationEffect* AnimationEffect::setFgColor(uint32_t c) {
@ -16,11 +18,23 @@ AnimationEffect* AnimationEffect::setFgColor(uint32_t c) {
return this; return this;
} }
AnimationEffect* AnimationEffect::setBlinkFrequency(uint16_t ms) {
_blink_freq = ms;
return this;
}
AnimationEffect::~AnimationEffect() { AnimationEffect::~AnimationEffect() {
delete this->animation; delete this->animation;
} }
void AnimationEffect::loop(uint16_t ms) { void AnimationEffect::loop(uint16_t ms) {
if (_blink_freq > 0) {
unsigned long mil = millis();
if (mil < _last_blink_at || _last_blink_at + _blink_freq <= mil) {
this->animation->invert();
_last_blink_at = mil;
}
}
this->animation->drawFrame(); this->animation->drawFrame();
this->animation->advance(); this->animation->advance();
} }
@ -30,3 +44,10 @@ String AnimationEffect::get_name() {
s += this->name; s += this->name;
return s; return s;
} }
AnimationEffect* AnimationEffect::Blinker(const char* name, uint16_t interval, uint32_t color, uint32_t background_color) {
AnimationEffect* anim = new AnimationEffect(name, background_color);
anim->setFgColor(color);
anim->setBlinkFrequency(interval);
return anim;
}

View File

@ -31,11 +31,11 @@ ClockEffect effect_clock;
TimerEffect effect_timer; TimerEffect effect_timer;
// We're using 0 instead of false to get a better visual difference between true and false. // We're using 0 instead of false to get a better visual difference between true and false.
const EffectEntry effects[] = { EffectEntry effects[] = {
/* 0 */ {"sinematrix3", true, [](){ return new Sinematrix3Effect(); }}, /* 0 */ {"sinematrix3", true, [](){ return new Sinematrix3Effect(); }},
/* 1 */ {"big_clock", true, [](){ return new BigClockEffect(); }}, /* 1 */ {"big_clock", true, [](){ return new BigClockEffect(); }},
/* 2 */ {"clock", 0, [](){ return new ClockEffect(); }}, /* 2 */ {"clock", 0, [](){ return new ClockEffect(); }},
/* 3 */ {"bell", 0, [](){ return new BellEffect(); }}, /* 3 */ {"bell", 0, [](){ return AnimationEffect::Blinker("/bell.pia", 300, 0xFFFF00); }},
/* 4 */ {"off", 0, [](){ return new StaticEffect(0x000000); }}, /* 4 */ {"off", 0, [](){ return new StaticEffect(0x000000); }},
/* 5 */ {"single_dynamic", true, [](){ return new SingleDynamicEffect(); }}, /* 5 */ {"single_dynamic", true, [](){ return new SingleDynamicEffect(); }},
/* 6 */ {"multi_dynamic", true, [](){ return new MultiDynamicEffect(); }}, /* 6 */ {"multi_dynamic", true, [](){ return new MultiDynamicEffect(); }},