5#ifndef MODES_DRAW_OVERLAY_HPP
6#define MODES_DRAW_OVERLAY_HPP
8namespace lampda::modes::draw {
28 uint32_t get_color()
const {
return (red << 16) | (green << 8) | blue; }
49template<
int UIElementSize = 16, u
int8_t freezeSize = 5, u
int8_t nbBlackLines = 2>
class Manager
52 Manager() : activeUiElements(0) {}
59 assert(activeUiElements < UIElementSize);
62 ctx.skipFirstLedsForFrames(0);
64 if (activeUiElements == 0)
68 for (uint8_t i = 0; i < ctx.lamp.maxWidth * nbBlackLines; ++i)
70 ctx.lamp.setPixelColor(i, 0);
74 size_t lastActiveElementIndex = 0;
75 const auto timeNow = ctx.lamp.now;
76 const size_t activeUiElementsToDisplay = activeUiElements;
78 for (uint8_t i = 0; i < activeUiElementsToDisplay; ++i)
80 const auto elementTimeout = elements[i].timeout_ms;
82 if (elementTimeout > 0 and timeNow > elementTimeout)
85 if (activeUiElements > 0)
90 drawElement(elements[i], ctx);
93 if (i != lastActiveElementIndex)
96 elements[lastActiveElementIndex] = elements[i];
98 ++lastActiveElementIndex;
103 ctx.skipFirstLedsForFrames(ctx.lamp.maxWidth * nbBlackLines, freezeSize);
107 void clear() { activeUiElements = 0; }
113 for (uint8_t i = 0; i < activeUiElements; ++i)
116 if (elements[i].type == type)
137 const uint16_t coordinateX = 0,
138 const uint16_t coordinateY = 0,
139 const uint8_t progress = 0,
140 const uint32_t activityDelay_ms = 50)
142 if (activeUiElements >= UIElementSize - 1)
151 element.
color = palette;
153 if (activityDelay_ms > 0)
154 element.
timeout_ms = ctx.lamp.now + activityDelay_ms;
168 template<
bool shouldUpdateTimeout = true>
174 elements[index].progress = progress;
177 const uint32_t newTimeout = ctx.lamp.now + freezeSize * ctx.lamp.frameDurationMs;
178 if (shouldUpdateTimeout and elements[index].timeout_ms <= newTimeout)
181 elements[index].timeout_ms = newTimeout;
197 uint16_t desiredIndex,
203 elements[index].color = palette;
206 const uint32_t newTimeout = ctx.lamp.now + freezeSize * ctx.lamp.frameDurationMs;
207 if (shouldUpdateTimeout and elements[index].timeout_ms <= newTimeout)
210 elements[index].timeout_ms = newTimeout;
217 bool update_type_timeout(
const auto& ctx,
const ElementType type, uint16_t desiredIndex,
const uint32_t timeout_ms)
223 elements[index].timeout_ms = ctx.lamp.now + timeout_ms;
237 template<
bool shouldUpdateTimeout = true>
bool update_type(
const auto& ctx,
239 uint16_t desiredIndex,
240 const uint8_t progress,
246 elements[index].progress = progress;
247 elements[index].color = palette;
250 const uint32_t newTimeout = ctx.lamp.now + freezeSize * ctx.lamp.frameDurationMs;
251 if (shouldUpdateTimeout and elements[index].timeout_ms <= newTimeout)
254 elements[index].timeout_ms = newTimeout;
266 for (uint8_t i = 0; i < activeUiElements; ++i)
269 if (elements[i].type == type)
272 if (desiredIndex <= 0)
298 case ElementType::NONE:
309 const uint8_t progress,
311 const uint16_t startCoordinateY)
const
313 const uint16_t rampScale = (progress / 255.0) * ctx.lamp.maxWidth;
314 for (uint16_t i = 0; i < rampScale; ++i)
316 const auto color = modes::colors::from_palette<false>(lmpd_map<uint8_t>(i, 0, rampScale, 0, 255), palette);
317 ctx.lamp.setPixelColorXY(i, startCoordinateY, color);
323 const uint8_t progress,
325 const uint16_t coordinateX,
326 const uint16_t coordinateY)
const
328 const auto color = modes::colors::from_palette<false>(progress, palette);
329 ctx.lamp.setPixelColorXY(coordinateX, coordinateY, color);
334 std::array<__private::UIElement, UIElementSize> elements;
336 size_t activeUiElements = 0;
Definition: overlay.hpp:50
void display_dot(auto &ctx, const uint8_t progress, const colors::PaletteTy &palette, const uint16_t coordinateX, const uint16_t coordinateY) const
Display a single colored pixel.
Definition: overlay.hpp:322
bool add_ui_element(const auto &ctx, const ElementType type, const colors::PaletteTy &palette, const uint16_t coordinateX=0, const uint16_t coordinateY=0, const uint8_t progress=0, const uint32_t activityDelay_ms=50)
Add a new UI element to the overlay.
Definition: overlay.hpp:134
void display_ramp(auto &ctx, const uint8_t progress, const colors::PaletteTy &palette, const uint16_t startCoordinateY) const
Display a colored ramp.
Definition: overlay.hpp:308
uint8_t get_element_count(const ElementType type) const
Return the count of elements of a target type.
Definition: overlay.hpp:110
bool get_N_of_type(const ElementType type, uint16_t desiredIndex, size_t &elementId) const
Find and return the Nth element of a given type.
Definition: overlay.hpp:263
void clear()
Clear all UI elements.
Definition: overlay.hpp:107
bool update_type_color(const auto &ctx, const ElementType type, uint16_t desiredIndex, const colors::PaletteTy &palette)
Update the target UI element color palette.
Definition: overlay.hpp:195
bool update_type(const auto &ctx, const ElementType type, uint16_t desiredIndex, const uint8_t progress, const colors::PaletteTy &palette)
Update the target UI element.
Definition: overlay.hpp:237
bool update_type_progress(const auto &ctx, const ElementType type, uint16_t desiredIndex, const uint8_t progress)
Update the target UI element progress.
Definition: overlay.hpp:169
void display_update(auto &ctx)
Display and update the overlay.
Definition: overlay.hpp:57
std::array< uint32_t, 16 > PaletteTy
Palette types.
Definition: palettes.hpp:18
ElementType
< Define an UI element type
Definition: overlay.hpp:14
@ DOT
Display a pixel dot.
< define a display color
Definition: overlay.hpp:24
Define an UI element.
Definition: overlay.hpp:35
uint16_t coordinateY
start Y coordinates
Definition: overlay.hpp:40
ElementType type
type of UI elements
Definition: overlay.hpp:36
colors::PaletteTy color
color palette to display
Definition: overlay.hpp:42
uint16_t coordinateX
start X coordinates
Definition: overlay.hpp:39
int32_t timeout_ms
If set to a positive number, this element will delete itself at the set time.
Definition: overlay.hpp:44
uint8_t progress
0-255 animation progress
Definition: overlay.hpp:37