/
section.cpp
580 lines (507 loc) · 14.6 KB
/
section.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
Section.cpp - Class for controlling multiple Pixels.
Requires Pixel and Colors classes.
*/
#include "../animation/blinkanimation.h"
#include "../animation/cycleanimation.h"
#include "../animation/fireanimation.h"
#include "../animation/lightninganimation.h"
#include "../animation/mandelbrotanimation.h"
#include "../animation/plasmaanimation.h"
#include "../animation/radialanimation.h"
#include "../animation/randomanimation.h"
#include "../animation/solidanimation.h"
#include "../animation/sparkleanimation.h"
#include "../animation/waveanimation.h"
#include "../canvas/canvas.h"
#include "../utility.h"
#include "colors.h"
#include "pixel.h"
#include "section.h"
namespace PixelMaestro {
/**
* Constructor. Initializes an empty Section.
* @param Maestro The Section's parent Maestro.
*/
Section::Section() : Section(0, 0) { }
/**
* Constructor. Initializes the Pixel array.
* @param x Number of rows in the Section.
* @param y Number of columns in the Section.
* @param parent Parent Section (if this is an Layer).
*/
Section::Section(uint16_t x, uint16_t y, Section* parent) {
set_dimensions(x, y);
parent_section_ = parent;
}
/**
Returns the current animation.
@return Current animation.
*/
Animation* Section::get_animation() const {
return animation_;
}
/**
* Returns the Section's brightness level.
* @return Brightness level.
*/
uint8_t Section::get_brightness() const {
return brightness_;
}
/**
* Returns the Section's Canvas.
* @return Section's Canvas.
*/
Canvas* Section::get_canvas() const {
return canvas_;
}
/**
Returns the size of the Pixel grid.
@return Size of the Pixel grid.
*/
Point& Section::get_dimensions() const {
return const_cast<Point&>(dimensions_);
}
/**
* Returns the Section's parent Maestro.
* @return Parent Maestro.
*/
Maestro& Section::get_maestro() const {
return *maestro_;
}
/**
* Returns the Layer (if one exists).
*
* @return Active Layer.
*/
Section::Layer* Section::get_layer() const {
return layer_;
}
/**
* Returns whether mirroring is enabled across each axis.
* @return Mirror configuration.
*/
Section::Mirror* Section::get_mirror() const {
return mirror_;
}
/**
* Returns the Section's offset.
* @return Offset.
*/
Point* Section::get_offset() {
return &offset_;
}
/**
* Returns this Section's parent (if this is an Layer).
* @return Parent Section.
*/
Section* Section::get_parent_section() const {
return parent_section_;
}
/**
* Returns the Pixel at the specified index.
*
* @param x Pixel x coordinate.
* @param y Pixel y coordinate.
* @return Pixel.
*/
Pixel& Section::get_pixel(uint16_t x, uint16_t y) const {
return pixels_[dimensions_.get_inline_index(x, y)];
}
/**
Returns the final color of the specified Pixel.
@param x Pixel x-coordinate.
@param y Pixel y-coordinate.
@param base_color The color to use as a base when blending in Layers.
@return RGB value of the Pixel's final color.
*/
Colors::RGB Section::get_pixel_color(uint16_t x, uint16_t y, Colors::RGB* base_color) {
// Adjust coordinates based on offset
uint16_t offset_x = (x + offset_.x) % dimensions_.x;
uint16_t offset_y = (y + offset_.y) % dimensions_.y;
// If mirroring is enabled, mirror across the axes.
if (mirror_ != nullptr) {
if (mirror_->x) {
if (offset_y > mirror_->mid_y) {
offset_y = mirror_->mid_y - (offset_y - mirror_->mid_y);
}
}
if (mirror_->y) {
if (offset_x > mirror_->mid_x) {
offset_x = mirror_->mid_x - (offset_x - mirror_->mid_x);
}
}
}
Colors::RGB final_color = pixels_[dimensions_.get_inline_index(offset_x, offset_y)].get_color();
/*
* If this Section *is* a Layer, combine this Pixel's color with its parent Pixel's color.
* This is done recursively, with each Layer building on top of its parent Section.
*/
if (parent_section_ != nullptr) {
final_color = Colors::mix_colors(
base_color,
&final_color,
parent_section_->get_layer()->mix_mode,
parent_section_->get_layer()->alpha);
}
// If this Section *has* a Layer, merge in the Layer's color output.
if (layer_ != nullptr) {
final_color = layer_->section->get_pixel_color(x, y, &final_color);
}
// Return color with brightness calculated
return final_color * static_cast<float>(brightness_ / static_cast<float>(255));
}
/**
* Returns the Section's scrolling behavior.
* @return Scrolling behavior.
*/
Section::Scroll* Section::get_scroll() const {
return scroll_;
}
/**
* Deletes the current Animation.
*
* @param clear_pixels If true, Pixels are cleared.
*/
void Section::remove_animation(bool clear_pixels) {
delete animation_;
animation_ = nullptr;
// Reset Pixel colors
if (clear_pixels) {
for (uint32_t pixel = 0; pixel < dimensions_.size(); pixel++) {
pixels_[pixel].clear();
}
}
}
/**
* Deletes the current Canvas.
*/
void Section::remove_canvas() {
delete canvas_;
canvas_ = nullptr;
}
/**
* Deletes the current Layer.
*/
void Section::remove_layer() {
delete layer_;
layer_ = nullptr;
}
/**
* Deletes the Section's scrolling behavior.
*/
void Section::remove_scroll() {
delete scroll_;
scroll_ = nullptr;
}
/**
* Sets a new Animation.
*
* @param type Animation type.
* @param palette The color palette to use.
* @param preserve_settings If true, the generic configurations in the old Animation (Palette, cycle index, orientation, fade, reverse, speed, and pause) are copied to the new Animation.
* @return New Animation.
*/
Animation& Section::set_animation(AnimationType animation_type, bool preserve_settings) {
Animation* new_animation = nullptr;
switch(animation_type) {
case AnimationType::Blink:
new_animation = new BlinkAnimation(*this);
break;
case AnimationType::Cycle:
new_animation = new CycleAnimation(*this);
break;
case AnimationType::Fire:
new_animation = new FireAnimation(*this);
break;
case AnimationType::Lightning:
new_animation = new LightningAnimation(*this);
break;
case AnimationType::Mandelbrot:
new_animation = new MandelbrotAnimation(*this);
break;
case AnimationType::Plasma:
new_animation = new PlasmaAnimation(*this);
break;
case AnimationType::Radial:
new_animation = new RadialAnimation(*this);
break;
case AnimationType::Random:
new_animation = new RandomAnimation(*this);
break;
case AnimationType::Solid:
new_animation = new SolidAnimation(*this);
break;
case AnimationType::Sparkle:
new_animation = new SparkleAnimation(*this);
break;
case AnimationType::Wave:
new_animation = new WaveAnimation(*this);
break;
}
/*
* Check for an existing Animation.
* If one exists and preserve_settings is true, copy the old Animation's settings to the new Animation.
*/
if (this->animation_ != nullptr) {
if (preserve_settings) {
new_animation->set_palette(*this->animation_->get_palette());
new_animation->set_cycle_index(this->animation_->get_cycle_index());
new_animation->set_fade(this->animation_->get_fade());
new_animation->set_orientation(this->animation_->get_orientation());
new_animation->set_reverse(this->animation_->get_reverse());
if (this->animation_->get_timer() != nullptr) {
new_animation->set_timer(this->animation_->get_timer()->get_interval(), this->animation_->get_timer()->get_delay());
}
}
remove_animation(false);
}
this->animation_ = new_animation;
return *animation_;
}
/**
* Sets the overall brightness of the Section.
* 255 is full brightness, and 0 is completely dark.
* @param brightness Brightness level (0 - 255).
*/
void Section::set_brightness(uint8_t brightness) {
this->brightness_ = brightness;
}
/**
* Sets a new Canvas of the specified type.
* This will overwrite the existing Canvas.
* @param num_frames The number of frames in the Canvas.
* @return The new Canvas.
*/
Canvas& Section::set_canvas(uint16_t num_frames) {
remove_canvas();
canvas_ = new Canvas(*this, num_frames);
return *canvas_;
}
/**
* Sets the size of the Section.
* @param x Number of Pixels along the x-coordinate.
* @param y Number of Pixels along the y-coordinate.
*/
void Section::set_dimensions(uint16_t x, uint16_t y) {
dimensions_.x = x;
dimensions_.y = y;
// Resize the Pixel grid
delete [] pixels_;
pixels_ = new Pixel[dimensions_.size()];
// Resize the Animation
if (animation_ != nullptr) {
animation_->rebuild_map();
animation_->map();
}
// Resize the Canvas
if (canvas_ != nullptr) {
canvas_->initialize();
}
// Resize the Layer
if (layer_ != nullptr) {
layer_->section->set_dimensions(dimensions_.x, dimensions_.y);
}
// If mirroring is enabled, recalculate the midway points
if (mirror_ != nullptr) {
mirror_->set(mirror_->x, mirror_->y, this->dimensions_);
}
}
/**
* Sets a new Layer.
* If an Layer already exists, this updates the existing Layer.
* @param mix_mode The method for blending the Layer.
* @param alpha The Layer's transparency (0 - 255.
* @return New Layer.
*/
Section::Layer& Section::set_layer(Colors::MixMode mix_mode, uint8_t alpha) {
if (layer_ == nullptr) {
layer_ = new Layer(*this, mix_mode, alpha);
}
else {
layer_->mix_mode = mix_mode;
layer_->alpha = alpha;
}
return *layer_;
}
/**
* Sets the Section's parent Maestro.
* @param maestro Parent Maestro.
*/
void Section::set_maestro(Maestro& maestro) {
this->maestro_ = &maestro;
}
/**
* Mirrors the Section across the axes.
* If both x and y are false, mirroring is disabled.
* @param x Mirror the x axis.
* @param y Mirror the y axis.
*/
Section::Mirror* Section::set_mirror(bool x, bool y) {
if (x == false && y == false) {
delete mirror_;
mirror_ = nullptr;
}
else {
if (mirror_ == nullptr) {
mirror_ = new Mirror();
}
mirror_->set(x, y, this->dimensions_);
}
return mirror_;
}
/**
* Sets the Section's offset, which shifts the Section's output along the Pixel grid.
* @param x Offset along the x axis.
* @param y Offset along the y axis.
* @return Offset.
*/
Point& Section::set_offset(uint16_t x, uint16_t y) {
offset_.set(x, y);
return offset_;
}
/**
Sets the specified Pixel to a new color.
@param x The Pixel's column.
@param y The Pixel's row.
@param color New color.
@param step_count Number of intermediate steps until the new color is reached.
*/
void Section::set_one(uint16_t x, uint16_t y, const Colors::RGB& color, uint8_t step_count) {
// Only continue if the Pixel is within the bounds of the array.
if (dimensions_.in_bounds(x, y)) {
pixels_[dimensions_.get_inline_index(x, y)].set_next_color(color, step_count);
}
}
/**
* Sets the direction and rate that the Section will scroll.
* The values determine the amount of time until the Section completes a single scroll along the respective axis.
* Using a negative value reverses the scrolling direction.
* Setting either axis to 0 disables scrolling on that axis.
*
* @param x Scrolling interval along the x axis.
* @param y Scrolling interval along the y axis.
* @param reverse_x If true, reverses the scrolling direction along the x axis.
* @param reverse_y If true, reverses the scrolling direction along the y axis.
*/
Section::Scroll& Section::set_scroll(uint16_t x, uint16_t y, bool reverse_x, bool reverse_y) {
if (scroll_ == nullptr) {
scroll_ = new Scroll();
}
scroll_->set(maestro_->get_timer().get_interval(), &dimensions_, x, y, reverse_x, reverse_y);
return *scroll_;
}
/**
* Sets the last update time of each component's timer.
* @param new_time New last refresh time.
*/
void Section::sync(const uint32_t &new_time) {
if (layer_ != nullptr) {
layer_->section->sync(new_time);
}
if (animation_ != nullptr) {
animation_->get_timer()->set_last_time(new_time);
}
if (canvas_ != nullptr && canvas_->get_frame_timer() != nullptr) {
canvas_->get_frame_timer()->set_last_time(new_time);
}
if (scroll_ != nullptr) {
if (scroll_->timer_x != nullptr) scroll_->timer_x->set_last_time(new_time);
if (scroll_->timer_y != nullptr) scroll_->timer_y->set_last_time(new_time);
}
}
/**
Main update routine.
@param current_time Program runtime.
*/
void Section::update(const uint32_t& current_time) {
/*
* First, update the Animation and Canvas. These set each Pixel's color output directly.
* Next, update any post-processing effects. For now this is just Scroll.
* Finally, update the Layer.
*/
if (animation_ != nullptr) {
animation_->update(current_time);
}
if (canvas_ != nullptr) {
canvas_->update(current_time);
}
for (uint32_t pixel = 0; pixel < dimensions_.size(); pixel++) {
pixels_[pixel].update();
}
if (scroll_ != nullptr) {
update_scroll(current_time);
}
if (layer_ != nullptr) {
layer_->section->update(current_time);
}
}
/**
* Scrolls the Section.
* @param current_time The program's current runtime.
*/
void Section::update_scroll(const uint32_t ¤t_time) {
// Scroll x axis
if (scroll_->timer_x != nullptr || scroll_->step_x != 0) {
uint16_t x_step = 0;
// If a timer is used (scrolling < 1 pixel per update), determine whether it's time to scroll.
if (scroll_->timer_x != nullptr) {
if (scroll_->timer_x->update(current_time)) {
x_step = 1;
}
}
// If a timer is not used (scrolling > 1 pixel per update), apply the scroll amount directly to the offset.
else if (scroll_->step_x > 0) {
x_step = scroll_->step_x;
}
// Check to see if we need to reset the offset
if (!scroll_->reverse_x && offset_.x >= dimensions_.x) {
offset_.x = 0;
}
else if (scroll_->reverse_x && offset_.x == 0) {
offset_.x = dimensions_.x;
}
// Finally, apply the movement
if (!scroll_->reverse_x) {
offset_.x += x_step;
}
else {
offset_.x -= x_step;
}
}
// Repeat for y axis
if (scroll_->timer_y != nullptr || scroll_->step_y != 0) {
uint16_t y_step = 0;
if (scroll_->timer_y != nullptr) {
if (scroll_->timer_y->update(current_time)) {
y_step = 1;
}
}
else if (scroll_->step_y > 0) {
y_step = scroll_->step_y;
}
// Check to see if we need to reset the offset
if (!scroll_->reverse_y && offset_.y >= dimensions_.y) {
offset_.y = 0;
}
else if (scroll_->reverse_y && offset_.y == 0) {
offset_.y = dimensions_.y;
}
// Finally, apply the movement
if (!scroll_->reverse_y) {
offset_.y += y_step;
}
else {
offset_.y -= y_step;
}
}
}
Section::~Section() {
remove_animation(false);
remove_canvas();
remove_layer();
remove_scroll();
delete mirror_;
delete [] pixels_;
}
}