Animation.h now also includes AnimationSlice to show only part of an animation.

This commit is contained in:
Fabian Schlenz 2019-06-06 06:40:30 +02:00
parent 5b7c7a7007
commit 4fe34afd08
2 changed files with 28 additions and 8 deletions

View File

@ -44,8 +44,10 @@ class Animation {
CRGB* colors;
CRGB* fgColor;
CRGB* bgColor;
uint16_t xOffset = 0;
uint16_t yOffset = 0;
int8_t xOffset = 0;
int8_t yOffset = 0;
uint8_t startFrame;
uint8_t endFrame;
virtual CRGB* getColor(uint8_t color_index);
void drawFrame(uint8_t frame_index);
@ -54,16 +56,25 @@ class Animation {
public:
Animation(AnimationData* d) : Animation(d, new CRGB(0x000000), 0, 0) {};
Animation(AnimationData* d, CRGB* bg_color): Animation(d, bg_color, 0, 0) {};
Animation(AnimationData* d, CRGB* bg_color, uint16_t xOffset, uint16_t yOffset);
Animation(AnimationData* d, CRGB* bg_color, int8_t xOffset, int8_t yOffset);
virtual ~Animation();
void draw();
void drawFrame();
virtual bool advance();
};
class Image : public Animation {
protected:
virtual bool advance();
class AnimationSlice : public Animation {
public:
AnimationSlice(AnimationData* d, uint8_t start, uint8_t end) : AnimationSlice(d, start, end, new CRGB(0x000000), 0, 0) {};
AnimationSlice(AnimationData* d, uint8_t start, uint8_t end, CRGB* color) : AnimationSlice(d, start, end, color, 0, 0) {};
AnimationSlice(AnimationData* d, uint8_t start, uint8_t end, CRGB* color, int8_t x, int8_t y);
};
class Image : public AnimationSlice {
public:
Image(AnimationData* d) : AnimationSlice(d, 0, 0, new CRGB(0x000000), 0, 0) {};
Image(AnimationData* d, CRGB* color) : AnimationSlice(d, 0, 0, color, 0, 0) {};
Image(AnimationData* d, CRGB* color, int8_t x, int8_t y) : AnimationSlice(d, 0, 0, color, x, y) {};
};
class Sprite : public Image {

View File

@ -33,12 +33,13 @@
#include "Animation.h"
#include "functions.h"
Animation::Animation(AnimationData *d, CRGB *bg_color, uint16_t x, uint16_t y) {
Animation::Animation(AnimationData *d, CRGB *bg_color, int8_t x, int8_t y) {
this->data = d;
this->bgColor = bg_color;
this->xOffset = x;
this->yOffset = y;
this->endFrame = d->frame_count-1;
this->colors = new CRGB[this->data->color_count];
uint8_t *color_data = new uint8_t[this->data->color_count * 3];
memcpy_P(color_data, this->data->colors, this->data->color_count * 3);
@ -88,7 +89,8 @@ bool Animation::advance() {
if (this->currentFrameSince == 0) {
this->currentFrameSince = millis();
} else if (this->currentFrameSince + this->getFrameDelay(currentFrame) < millis() || this->currentFrameSince > millis()) {
currentFrame = (currentFrame + 1) % this->data->frame_count;
currentFrame++;
if (currentFrame > endFrame) currentFrame = startFrame;
this->currentFrameSince = millis();
return true;
}
@ -108,3 +110,10 @@ CRGB* Animation::getColor(uint8_t index) {
if (index==1) return this->bgColor;
else return &this->colors[index - 2];
}
AnimationSlice::AnimationSlice(AnimationData* d, uint8_t start, uint8_t end, CRGB* color, int8_t x, int8_t y) : Animation(d, color, x, y) {
if (start <= end && end < d->frame_count) {
this->startFrame = start;
this->endFrame = end;
}
}