hunting/middleware/GuiEngine/lvgl_common/LvglWidget.cpp

244 lines
6.4 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, softwareZhoufuwuqimima123
*
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "LvglWidget.h"
#include "ILog.h"
#include "images_handle.h"
i_lv_event_code_t transitionEventCode_MidApp(lv_event_code_t code)
{
switch (code)
{
case LV_EVENT_CLICKED:
return i_lv_event_code_t::I_LV_EVENT_CLICKED;
default:
return i_lv_event_code_t::I_LV_EVENT_ALL;
}
}
lv_event_code_t transitionEventCode_AppMid(i_lv_event_code_t code)
{
switch (code)
{
case i_lv_event_code_t::I_LV_EVENT_CLICKED:
return LV_EVENT_CLICKED;
default:
return LV_EVENT_ALL;
}
}
lv_align_t transitionAlignCode_AppMid(i_lv_align_t align)
{
switch (align)
{
case i_lv_align_t::I_LV_ALIGN_DEFAULT:
return LV_ALIGN_DEFAULT;
case i_lv_align_t::I_LV_ALIGN_TOP_LEFT:
return LV_ALIGN_TOP_LEFT;
case i_lv_align_t::I_LV_ALIGN_TOP_MID:
return LV_ALIGN_TOP_MID;
case i_lv_align_t::I_LV_ALIGN_TOP_RIGHT:
return LV_ALIGN_TOP_RIGHT;
case i_lv_align_t::I_LV_ALIGN_BOTTOM_LEFT:
return LV_ALIGN_BOTTOM_LEFT;
case i_lv_align_t::I_LV_ALIGN_BOTTOM_MID:
return LV_ALIGN_BOTTOM_MID;
case i_lv_align_t::I_LV_ALIGN_BOTTOM_RIGHT:
return LV_ALIGN_BOTTOM_RIGHT;
case i_lv_align_t::I_LV_ALIGN_LEFT_MID:
return LV_ALIGN_LEFT_MID;
case i_lv_align_t::I_LV_ALIGN_RIGHT_MID:
return LV_ALIGN_RIGHT_MID;
case i_lv_align_t::I_LV_ALIGN_CENTER:
return LV_ALIGN_CENTER;
case i_lv_align_t::I_LV_ALIGN_OUT_TOP_LEFT:
return LV_ALIGN_OUT_TOP_LEFT;
case i_lv_align_t::I_LV_ALIGN_OUT_TOP_MID:
return LV_ALIGN_OUT_TOP_MID;
case i_lv_align_t::I_LV_ALIGN_OUT_TOP_RIGHT:
return LV_ALIGN_OUT_TOP_RIGHT;
case i_lv_align_t::I_LV_ALIGN_OUT_BOTTOM_LEFT:
return LV_ALIGN_OUT_BOTTOM_LEFT;
case i_lv_align_t::I_LV_ALIGN_OUT_BOTTOM_MID:
return LV_ALIGN_OUT_BOTTOM_MID;
case i_lv_align_t::I_LV_ALIGN_OUT_BOTTOM_RIGHT:
return LV_ALIGN_OUT_BOTTOM_RIGHT;
case i_lv_align_t::I_LV_ALIGN_OUT_LEFT_TOP:
return LV_ALIGN_OUT_LEFT_TOP;
case i_lv_align_t::I_LV_ALIGN_OUT_LEFT_MID:
return LV_ALIGN_OUT_LEFT_MID;
case i_lv_align_t::I_LV_ALIGN_OUT_LEFT_BOTTOM:
return LV_ALIGN_OUT_LEFT_BOTTOM;
case i_lv_align_t::I_LV_ALIGN_OUT_RIGHT_TOP:
return LV_ALIGN_OUT_RIGHT_TOP;
case i_lv_align_t::I_LV_ALIGN_OUT_RIGHT_MID:
return LV_ALIGN_OUT_RIGHT_MID;
case i_lv_align_t::I_LV_ALIGN_OUT_RIGHT_BOTTOM:
return LV_ALIGN_OUT_RIGHT_BOTTOM;
default:
return LV_ALIGN_CENTER;
}
}
void LvglWidget::AlignTo(std::shared_ptr<Widget> base, i_lv_align_t align, int16_t posX, int16_t posY)
{
if (mWidget)
{
if(base == nullptr)
{
lv_obj_align_to(mWidget, nullptr, transitionAlignCode_AppMid(align), posX, posY);
}
else
{
std::shared_ptr<LvglWidget> _base = std::dynamic_pointer_cast<LvglWidget>(base);
lv_obj_align_to(mWidget, _base->mWidget, transitionAlignCode_AppMid(align), posX, posY);
}
}
}
void LvglWidget::SetPos(int16_t posX, int16_t posY)
{
if (mWidget)
{
lv_obj_set_pos(mWidget, posX, posY);
}
}
/**
* @brief aligns the object to the center of its parent object.
*
* Note that if the size of the parent element changes, 'obj' needs to be manually aligned again
*/
void LvglWidget::Center(void)
{
if (mWidget)
{
lv_obj_center(mWidget);
}
}
/**
* @brief deletes the LvglWidget object
*
* Deletes the current LvglWidget object and frees the associated resources.
*/
void LvglWidget::Delete(void)
{
if (mWidget)
{
lv_obj_del(mWidget);
mWidget = nullptr;
}
}
void LvglWidget::SetOpa(const uint8_t opaValue)
{
if (mWidget)
{
lv_obj_set_style_bg_opa(mWidget, opaValue, 0);
}
}
void LvglWidget::SetSize(const int16_t width, const int16_t height)
{
if (mWidget)
{
lv_obj_set_size(mWidget, width, height);
}
}
void LvglWidget::SetColor(uint32_t color)
{
if (mWidget)
{
lv_obj_set_style_bg_color(mWidget, lv_color_hex(color), 0);
}
}
static void publicEventCb(lv_event_t * e)
{
LvglWidget * kb = (LvglWidget *)lv_event_get_user_data(e);
lv_event_code_t code = lv_event_get_code(e);
kb->mEventCb(transitionEventCode_MidApp(code), nullptr); // 这里的 nullptr 需要改, 不是这样的,而是根据具体的需求
}
void LvglWidget::SetEventCb(i_lv_event_cb_t eventCb)
{
if (mWidget)
{
mEventCb = eventCb;
lv_obj_add_event_cb(mWidget, publicEventCb, LV_EVENT_ALL, this);
}
}
void LvglWidget::EventSend(i_lv_event_code_t e, void * userData)
{
if (mWidget)
{
lv_event_send(mWidget, transitionEventCode_AppMid(e), nullptr);
}
}
void ImageWidget::ImageSetSrc(const void *src)
{
if (mWidget)
{
// LogInfo("================================= image src = %s\n", (char *)src);
lv_img_set_src(mWidget, src);
}
}
/**
* @brief sets the label text
*
* @param text Indicates the text content
*/
void LabelWidget::SetText(const char *text)
{
if (mWidget)
{
lv_label_set_text(mWidget, text);
}
}
/**
* @brief Sets the color of the button control
*
* @param color Color value
*/
void ButtonWidget::SetColor(uint32_t color)
{
if (mWidget)
{
// 打桩
}
}