-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathqpLayer.cpp
More file actions
203 lines (148 loc) · 4.92 KB
/
Copy pathqpLayer.cpp
File metadata and controls
203 lines (148 loc) · 4.92 KB
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
#include "qpLayer.h"
#include "layer_effects/qpContinuallyFadeBy.h"
qpLayer::qpLayer(CRGB *leds, int numLeds) {
this->leds = leds;
this->numLeds = numLeds;
this->setLayerBrush(OVERLAY);
}
void qpLayer::draw(CRGB *targetLeds, int numLeds) {
bool patternsRendered = false;
// Apply pre-render effects
while(qpLayerEffect *effect = this->preRenderEffects.fetch()) {
effect->apply(this->leds, this->numLeds);
}
// Render patterns
while(qpPattern *currentPattern = this->patterns.fetch()) {
currentPattern->render();
patternsRendered |= currentPattern->isActive();
// If this pattern isn't active and it's configured to auto delete when finished,
// then destroy and remove from the patterns linked list.
if(!currentPattern->isActive() && currentPattern->shouldRemoveWhenDeactivated()) {
// If this is considered the lastReferencedPattern, updated it.
if(currentPattern == this->lastReferencedPattern) {
this->lastReferencedPattern = nullptr;
}
this->patterns.remove(currentPattern);
}
}
// Apply post render effects
while(qpLayerEffect *effect = this->postRenderEffects.fetch()) {
effect->apply(this->leds, this->numLeds);
}
// Write values into main LED array via selected brush method
if(patternsRendered || this->bPersistWhenPatternsInactive)
(this->*applyLeds)(targetLeds, this->leds, numLeds);
}
// ~ Config
qpPattern &qpLayer::addPattern(qpPattern *pattern) {
pattern->assignTargetLeds(this->leds, this->numLeds);
pattern->initialize();
this->lastReferencedPattern = this->patterns.append(pattern);
return *pattern;
}
// ~ Effects
qpLayer &qpLayer::addAfterRenderEffect(qpLayerEffect *effect) {
this->postRenderEffects.append(effect);
return *this;
}
qpLayer &qpLayer::addPreRenderEffect(qpLayerEffect *effect) {
this->preRenderEffects.append(effect);
return *this;
}
// qpLayer &qpLayer::removePattern(qpPattern *pattern) {
// this->patterns.remove(pattern);
// return *this;
// }
// qpLayer &qpLayer::removePatternAtIndex(int index) {
// this->patterns.removeAtIndex(index);
// return *this;
// }
qpLayer &qpLayer::continuallyFadeLayerBy(int fadeAmount) {
this->addPreRenderEffect(new qpContinuallyFadeBy(constrain(fadeAmount, 0, 255)));
return *this;
}
qpLayer &qpLayer::hideWhenNoActivePatterns(bool trueOrFalse) {
this->bPersistWhenPatternsInactive = (! trueOrFalse);
return *this;
}
// ~ Brush config
//preset
qpLayer &qpLayer::setLayerBrush(QP_BRUSH_TYPE brushType) {
switch(brushType) {
case ADD:
this->applyLeds = &qpLayer::addToLeds;
break;
case SUBTRACT:
this->applyLeds = &qpLayer::subtractFromLeds;
break;
case OVERWRITE:
this->applyLeds = &qpLayer::overwriteWithLeds;
break;
case OVERLAY:
this->applyLeds = &qpLayer::overlayOnLeds;
break;
case COMBINE:
this->applyLeds = &qpLayer::combineWithLeds;
break;
case MASK:
this->applyLeds = &qpLayer::maskLeds;
break;
}
return *this;
}
//external brush assignment - TODO: would all brushes moved to external functions to implement
/*
qpLayer &qpLayer::setLayerBrush(void (*brushFunc)(CRGB *toLeds, CRGB *sourceLeds, int numLeds)) {
this->applyLeds = brushFunc;
return *this;
}
*/
// ~ Brushes
void qpLayer::addToLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
for(int i = 0; i < numLeds; i++)
targetLeds[i] += sourceLeds[i];
}
void qpLayer::subtractFromLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
for(int i = 0; i < numLeds; i++)
targetLeds[i] -= sourceLeds[i];
}
void qpLayer::overlayOnLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
for(int i = 0; i < numLeds; i++) {
if(sourceLeds[i] != CRGB(0, 0, 0))
targetLeds[i] = sourceLeds[i];
}
}
void qpLayer::overwriteWithLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
memcpy(targetLeds, sourceLeds, (sizeof(CRGB)*numLeds));
}
void qpLayer::combineWithLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
for(int i = 0; i < numLeds; i++)
targetLeds[i] |= this->leds[i];
// targetLeds[i] = blend(targetLeds[i], sourceLeds[i], 128); //TODO: implement true rgb blend
}
void qpLayer::maskLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
for(int i = 0; i < numLeds; i++) {
targetLeds[i] -= -sourceLeds[i];
}
}
// ~ Access
qpPattern &qpLayer::pattern(byte patternIndex) {
this->lastReferencedPattern = this->patterns.getItem(patternIndex);
return *this->lastReferencedPattern;
}
byte qpLayer::numberOfPatterns() {
return this->patterns.numElements;
}
void qpLayer::removePattern(byte patternIndex) {
qpPattern* pattern = this->patterns.getItem(patternIndex);
if (pattern == nullptr) {
return;
}
if(this->lastReferencedPattern == pattern) {
this->lastReferencedPattern = nullptr;
}
this->patterns.remove(patternIndex);
}
qpPattern &qpLayer::operator()(byte patternIndex) {
return this->pattern(patternIndex);
}