diff --git a/CMakeLists.txt b/CMakeLists.txt index 4da994a..f87a0c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,7 @@ set(headers src/visibleinterface.h src/widgets/centeredlabel.h src/widgets/graph.h + src/widgets/iconwidget.h src/widgets/label.h src/widgets/progressbar.h src/widgets/reverseprogressbar.h @@ -88,6 +89,7 @@ set(sources src/icons/checked_grey.cpp src/icons/unchecked_grey.cpp src/widgets/centeredlabel.cpp + src/widgets/iconwidget.cpp src/widgets/label.cpp src/widgets/progressbar.cpp src/widgets/reverseprogressbar.cpp diff --git a/src/widgets/iconwidget.cpp b/src/widgets/iconwidget.cpp new file mode 100644 index 0000000..1c7020f --- /dev/null +++ b/src/widgets/iconwidget.cpp @@ -0,0 +1,68 @@ +#include "iconwidget.h" + +namespace espgui { +void IconWidget::start(TftInterface &tft) +{ + m_lastIcon = nullptr; +} + +void IconWidget::start(TftInterface &tft, const int32_t iconWidth, const int32_t iconHeight, const uint16_t *icon) +{ + if (m_iconWidth != iconWidth || m_iconHeight != iconHeight) + { + ESP_LOGE(TAG, "Icon size mismatch: %dx%d vs %ldx%ld", m_iconWidth, m_iconHeight, iconWidth, iconHeight); + return; + } + + m_lastIcon = icon; + + if (icon) + tft.pushImage(m_x, m_y, m_iconWidth, m_iconHeight, icon); +} + +void IconWidget::redraw(TftInterface &tft, const int32_t iconWidth, const int32_t iconHeight, const uint16_t *icon, uint16_t bgcolor, bool forceRedraw) +{ + if (m_iconWidth != iconWidth || m_iconHeight != iconHeight) + { + ESP_LOGE(TAG, "Icon size mismatch: %dx%d vs %ldx%ld", m_iconWidth, m_iconHeight, iconWidth, iconHeight); + return; + } + + if (forceRedraw || m_lastIcon != icon) + { + if (icon) + tft.pushImage(m_x, m_y, m_iconWidth, m_iconHeight, icon); + else if (m_lastIcon) + tft.fillRect(m_x, m_y, m_iconWidth, m_iconHeight, bgcolor); + + m_lastIcon = icon; + } +} + +void IconWidget::redraw(TftInterface &tft, const int32_t iconWidth, const int32_t iconHeight, const uint16_t *icon, bool forceRedraw) +{ + redraw(tft, iconWidth, iconHeight, icon, m_bgcolor, forceRedraw); +} + +void IconWidget::clear(TftInterface &tft, uint16_t bgcolor) +{ + if (m_lastIcon) + tft.fillRect(m_x, m_y, m_iconWidth, m_iconHeight, bgcolor); + + m_lastIcon = nullptr; +} + +void IconWidget::clear(TftInterface &tft) +{ + clear(tft, m_bgcolor); +} + +IconWidget::IconWidget(int x, int y, int iconWidth, int iconHeight, uint16_t bgcolor): + m_x{x}, + m_y{y}, + m_iconWidth{iconWidth}, + m_iconHeight{iconHeight}, + m_bgcolor{bgcolor}, + m_lastIcon{nullptr} +{} +} // namespace espgui diff --git a/src/widgets/iconwidget.h b/src/widgets/iconwidget.h new file mode 100644 index 0000000..fdd10ee --- /dev/null +++ b/src/widgets/iconwidget.h @@ -0,0 +1,45 @@ +#pragma once + +// esp-idf includes +#include + +// local includes +#include "icon.h" +#include "tftcolors.h" +#include "tftinterface.h" + +namespace espgui { + +class IconWidget +{ + static constexpr const char * const TAG = "IconWidget"; +public: + IconWidget(int x, int y, int iconWidth, int iconHeight, uint16_t bgcolor = espgui::TFT_BLACK); + + void start(TftInterface &tft); + + void start(TftInterface &tft, int32_t iconWidth, int32_t iconHeight, const uint16_t *icon); + + void redraw(TftInterface &tft, int32_t iconWidth, int32_t iconHeight, const uint16_t *icon, uint16_t bgcolor, bool forceRedraw = false); + + void redraw(TftInterface &tft, int32_t iconWidth, int32_t iconHeight, const uint16_t *icon, bool forceRedraw = false); + + void clear(espgui::TftInterface &tft, uint16_t bgcolor); + + void clear(espgui::TftInterface &tft); + + int x() const { return m_x; } + + int y() const { return m_y; } + +private: + const int m_x; + const int m_y; + const int m_iconWidth; + const int m_iconHeight; + + uint16_t m_bgcolor; + + const uint16_t *m_lastIcon; +}; +}