MatrixEffect can now have multiple columns in the same column. In RandomMatrix, they even run in all directions.
This commit is contained in:
		| @@ -9,16 +9,23 @@ | ||||
| class MatrixEffectColumn { | ||||
| protected: | ||||
|     Window* window; | ||||
|     int x, y; | ||||
|     int length = 1; | ||||
|     uint8_t x, y; | ||||
|     uint8_t length = 1; | ||||
|     uint8_t _direction = 2; | ||||
|     bool _random_direction = false; | ||||
|     virtual CRGB _getColor(uint8_t height); | ||||
|     virtual void restart(); | ||||
|     virtual void restart(bool completely_random); | ||||
| private: | ||||
|     uint16_t speed; | ||||
|     boolean running; | ||||
|     unsigned long last_move = 0; | ||||
| public: | ||||
|     MatrixEffectColumn(Window* win, int xPos); | ||||
| 	static const uint8_t DIR_NORTH = 0; | ||||
| 	static const uint8_t DIR_EAST = 1; | ||||
| 	static const uint8_t DIR_SOUTH = 2; | ||||
| 	static const uint8_t DIR_WEST = 3; | ||||
|  | ||||
|     MatrixEffectColumn(Window* win, uint8_t direction=0, bool random_direction=false); | ||||
|     virtual ~MatrixEffectColumn() {}; | ||||
|     void advance(); | ||||
|     void draw(); | ||||
| @@ -29,16 +36,16 @@ class RainbowMatrixEffectColumn : public MatrixEffectColumn { | ||||
| protected: | ||||
|     CRGB _getColor(uint8_t height) override; | ||||
| public: | ||||
|     RainbowMatrixEffectColumn(Window* win, int xPos) : MatrixEffectColumn(win, xPos) {}; | ||||
|     RainbowMatrixEffectColumn(Window* win, uint8_t dir, bool rnd=false) : MatrixEffectColumn(win, dir, rnd) {}; | ||||
| }; | ||||
|  | ||||
| class RandomMatrixEffectColumn : public MatrixEffectColumn { | ||||
| protected: | ||||
|     uint8_t _hue = 42; | ||||
|     CRGB _getColor(uint8_t height) override; | ||||
|     void restart() override; | ||||
|     void restart(bool completely_random) override; | ||||
| public: | ||||
|     RandomMatrixEffectColumn(Window* win, int xPos) : MatrixEffectColumn(win, xPos) {}; | ||||
|     RandomMatrixEffectColumn(Window* win, uint8_t dir, bool rnd=false) : MatrixEffectColumn(win, dir, rnd) {}; | ||||
| }; | ||||
|  | ||||
| class MatrixEffect : public Effect { | ||||
|   | ||||
| @@ -2,29 +2,80 @@ | ||||
| #include "my_color_palettes.h" | ||||
| #include "functions.h" | ||||
|  | ||||
| MatrixEffectColumn::MatrixEffectColumn(Window* win, int xPos) { | ||||
| MatrixEffectColumn::MatrixEffectColumn(Window* win, uint8_t dir, bool rand) { | ||||
|     window = win; | ||||
|     x = xPos; | ||||
|     restart(); | ||||
|     y = random8(0, window->height); | ||||
|     _direction = dir; | ||||
|     _random_direction = rand; | ||||
|     restart(true); | ||||
| } | ||||
|  | ||||
| void MatrixEffectColumn::restart() { | ||||
|     y=-1; | ||||
| void MatrixEffectColumn::restart(bool completely_random) { | ||||
| 	if (_random_direction) { | ||||
| 		_direction = random8(4); | ||||
| 	} | ||||
|  | ||||
| 	if (completely_random) { | ||||
| 		x = random8(window->width); | ||||
| 		y = random8(window->height); | ||||
| 	} else { | ||||
| 		switch(_direction) { | ||||
| 			case DIR_NORTH: | ||||
| 				x = random8(window->width); | ||||
| 				y = window->height - 1; | ||||
| 				break; | ||||
| 			case DIR_EAST: | ||||
| 				x = 0; | ||||
| 				y = random8(window->height); | ||||
| 				break; | ||||
| 			case DIR_SOUTH: | ||||
| 				x = random8(window->width); | ||||
| 				y = 0; | ||||
| 				break; | ||||
| 			case DIR_WEST: | ||||
| 				x = window->width - 1; | ||||
| 				y = random8(window->height); | ||||
| 				break; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|     length = random8(EFFECT_MATRIX_LENGTH_MIN, EFFECT_MATRIX_LENGTH_MAX); | ||||
|     running = true; | ||||
|     speed = random8(EFFECT_MATRIX_SPEED_MIN, EFFECT_MATRIX_SPEED_MAX); | ||||
| } | ||||
|  | ||||
| void MatrixEffectColumn::advance() { | ||||
|     y++; | ||||
|     if (y-length > window->height) running = false; | ||||
| 	switch(_direction) { | ||||
| 		case DIR_NORTH: | ||||
| 			y--; | ||||
| 			if (y > window->height && y + length > window->height) running=false; | ||||
| 			break; | ||||
| 		case DIR_EAST: | ||||
| 			x++; | ||||
| 			if (x - length > window->width) running=false; | ||||
| 			break; | ||||
| 		case DIR_SOUTH: | ||||
| 			y++; | ||||
| 			if (y - length > window->height) running=false; | ||||
| 			break; | ||||
| 		case DIR_WEST: | ||||
| 			x--; | ||||
| 			if (x > window->width && y + length > window->width) running=false; | ||||
| 			break; | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void MatrixEffectColumn::draw() { | ||||
| 	int8_t xdir = 0; | ||||
| 	int8_t ydir = 0; | ||||
| 	switch (_direction) { | ||||
| 		case DIR_NORTH: ydir = 1; break; | ||||
| 		case DIR_EAST:  xdir = 1; break; | ||||
| 		case DIR_SOUTH: ydir = -1; break; | ||||
| 		case DIR_WEST:  xdir = -1; break; | ||||
| 	} | ||||
|     for(int i=0; i<length; i++) { | ||||
|         CRGB color = _getColor(i); | ||||
|         window->setPixel(x, y-i, &color); | ||||
|         window->raisePixel(x+(xdir*i), y+(ydir*i), &color); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -32,7 +83,7 @@ void MatrixEffectColumn::loop() { | ||||
|     if (!running) { | ||||
|         if (random8() < 20) { | ||||
|             // Start the column again. | ||||
|             restart(); | ||||
|             restart(false); | ||||
|         } | ||||
|     } else { | ||||
|         if (millis() - last_move > speed) { | ||||
| @@ -75,8 +126,8 @@ CRGB RandomMatrixEffectColumn::_getColor(uint8_t i) { | ||||
|     return color; | ||||
| } | ||||
|  | ||||
| void RandomMatrixEffectColumn::restart() { | ||||
|     MatrixEffectColumn::restart(); | ||||
| void RandomMatrixEffectColumn::restart(bool completely_random) { | ||||
|     MatrixEffectColumn::restart(completely_random); | ||||
|     _hue = random8(); | ||||
| } | ||||
|  | ||||
| @@ -97,15 +148,15 @@ MatrixEffect::MatrixEffect() { | ||||
| } | ||||
|  | ||||
| void MatrixEffect::_init() { | ||||
| 	for (int i=0; i<window->width; i++) _columns[i] = new MatrixEffectColumn(window, i); | ||||
| 	for (int i=0; i<window->width; i++) _columns[i] = new MatrixEffectColumn(window, MatrixEffectColumn::DIR_SOUTH); | ||||
| } | ||||
|  | ||||
| void RandomMatrixEffect::_init() { | ||||
| 	for (int i=0; i<window->width; i++) _columns[i] = new RandomMatrixEffectColumn(window, i); | ||||
| 	for (int i=0; i<window->width; i++) _columns[i] = new RandomMatrixEffectColumn(window, random8(4), true); | ||||
| } | ||||
|  | ||||
| void RainbowMatrixEffect::_init() { | ||||
| 	for (int i=0; i<window->width; i++) _columns[i] = new RainbowMatrixEffectColumn(window, i); | ||||
| 	for (int i=0; i<window->width; i++) _columns[i] = new RainbowMatrixEffectColumn(window, MatrixEffectColumn::DIR_SOUTH); | ||||
| } | ||||
|  | ||||
| MatrixEffect::~MatrixEffect() { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user