68 lines
2.1 KiB
C++
68 lines
2.1 KiB
C++
#include "PowerLinkWidget.hpp"
|
|
#include "../WidgetManager.hpp"
|
|
|
|
PowerLinkWidget::PowerLinkWidget(const WidgetConfig& config)
|
|
: Widget(config)
|
|
{
|
|
points_[0].x = 0;
|
|
points_[0].y = 0;
|
|
points_[1].x = 1;
|
|
points_[1].y = 1;
|
|
}
|
|
|
|
void PowerLinkWidget::buildLinePoints() {
|
|
if (obj_ == nullptr) return;
|
|
if (config_.x < 0 || config_.y < 0) return;
|
|
|
|
const ScreenConfig* screen = WidgetManager::instance().currentScreen();
|
|
if (screen == nullptr) return;
|
|
|
|
const WidgetConfig* fromNode = screen->findWidget(static_cast<uint8_t>(config_.x));
|
|
const WidgetConfig* toNode = screen->findWidget(static_cast<uint8_t>(config_.y));
|
|
if (fromNode == nullptr || toNode == nullptr) return;
|
|
if (fromNode->parentId != config_.parentId || toNode->parentId != config_.parentId) return;
|
|
|
|
int16_t x1 = fromNode->x + fromNode->width / 2;
|
|
int16_t y1 = fromNode->y + fromNode->height / 2;
|
|
int16_t x2 = toNode->x + toNode->width / 2;
|
|
int16_t y2 = toNode->y + toNode->height / 2;
|
|
|
|
int16_t minX = x1 < x2 ? x1 : x2;
|
|
int16_t minY = y1 < y2 ? y1 : y2;
|
|
int16_t maxX = x1 > x2 ? x1 : x2;
|
|
int16_t maxY = y1 > y2 ? y1 : y2;
|
|
|
|
points_[0].x = x1 - minX;
|
|
points_[0].y = y1 - minY;
|
|
points_[1].x = x2 - minX;
|
|
points_[1].y = y2 - minY;
|
|
|
|
lv_obj_set_pos(obj_, minX, minY);
|
|
lv_obj_set_size(obj_, (maxX - minX) + 1, (maxY - minY) + 1);
|
|
lv_line_set_points(obj_, points_, 2);
|
|
}
|
|
|
|
lv_obj_t* PowerLinkWidget::create(lv_obj_t* parent) {
|
|
obj_ = lv_line_create(parent);
|
|
if (obj_ == nullptr) {
|
|
return nullptr;
|
|
}
|
|
|
|
lv_obj_clear_flag(obj_, LV_OBJ_FLAG_CLICKABLE);
|
|
buildLinePoints();
|
|
return obj_;
|
|
}
|
|
|
|
void PowerLinkWidget::applyStyle() {
|
|
if (obj_ == nullptr) return;
|
|
|
|
lv_obj_set_style_line_color(obj_, lv_color_make(
|
|
config_.bgColor.r, config_.bgColor.g, config_.bgColor.b), 0);
|
|
lv_obj_set_style_line_width(obj_, config_.width > 0 ? config_.width : 2, 0);
|
|
lv_obj_set_style_line_opa(obj_, config_.bgOpacity, 0);
|
|
|
|
int32_t dash = config_.iconGap > 0 ? config_.iconGap : 6;
|
|
lv_obj_set_style_line_dash_width(obj_, dash, 0);
|
|
lv_obj_set_style_line_dash_gap(obj_, dash + 4, 0);
|
|
}
|