knxdisplay/main/widgets/Widget.cpp
2026-01-24 10:42:15 +01:00

97 lines
2.5 KiB
C++

#include "Widget.hpp"
Widget::Widget(const WidgetConfig& config)
: config_(config)
, obj_(nullptr)
{
}
Widget::~Widget() {
destroy();
}
void Widget::destroy() {
if (obj_ != nullptr) {
lv_obj_delete(obj_);
obj_ = nullptr;
}
}
void Widget::applyStyle() {
if (obj_ == nullptr) return;
applyCommonStyle();
}
void Widget::onKnxValue(float /*value*/) {
// Default: do nothing
}
void Widget::onKnxSwitch(bool /*value*/) {
// Default: do nothing
}
void Widget::onKnxText(const char* /*text*/) {
// Default: do nothing
}
void Widget::applyCommonStyle() {
if (obj_ == nullptr) return;
// Text color
lv_obj_set_style_text_color(obj_, lv_color_make(
config_.textColor.r, config_.textColor.g, config_.textColor.b), 0);
// Font
lv_obj_set_style_text_font(obj_, getFontBySize(config_.fontSize), 0);
// Background (for buttons and labels with bg)
if (config_.bgOpacity > 0) {
lv_obj_set_style_bg_color(obj_, lv_color_make(
config_.bgColor.r, config_.bgColor.g, config_.bgColor.b), 0);
lv_obj_set_style_bg_opa(obj_, config_.bgOpacity, 0);
}
// Border radius
if (config_.borderRadius > 0) {
lv_obj_set_style_radius(obj_, config_.borderRadius, 0);
}
// Shadow
applyShadowStyle();
}
void Widget::applyShadowStyle() {
if (obj_ == nullptr || !config_.shadow.enabled) return;
lv_obj_set_style_shadow_color(obj_, lv_color_make(
config_.shadow.color.r, config_.shadow.color.g, config_.shadow.color.b), 0);
lv_obj_set_style_shadow_opa(obj_, 180, 0);
lv_obj_set_style_shadow_width(obj_, config_.shadow.blur, 0);
lv_obj_set_style_shadow_spread(obj_, config_.shadow.spread, 0);
lv_obj_set_style_shadow_offset_x(obj_, config_.shadow.offsetX, 0);
lv_obj_set_style_shadow_offset_y(obj_, config_.shadow.offsetY, 0);
}
const lv_font_t* Widget::getFontBySize(uint8_t sizeIndex) {
// Font sizes: 0=14, 1=18, 2=22, 3=28, 4=36, 5=48
switch (sizeIndex) {
case 0: return &lv_font_montserrat_14;
#if LV_FONT_MONTSERRAT_18
case 1: return &lv_font_montserrat_18;
#endif
#if LV_FONT_MONTSERRAT_22
case 2: return &lv_font_montserrat_22;
#endif
#if LV_FONT_MONTSERRAT_28
case 3: return &lv_font_montserrat_28;
#endif
#if LV_FONT_MONTSERRAT_36
case 4: return &lv_font_montserrat_36;
#endif
#if LV_FONT_MONTSERRAT_48
case 5: return &lv_font_montserrat_48;
#endif
default: return &lv_font_montserrat_14;
}
}