freertos_f407/User/system/sgl/widgets/label/sgl_label.h

215 lines
6.0 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* source/widgets/sgl_label.h
*
* MIT License
*
* Copyright(c) 2023-present All contributors of SGL
* Document reference link: https://sgl-docs.readthedocs.io
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __SGL_LABEL_H__
#define __SGL_LABEL_H__
#include <sgl_core.h>
#include <sgl_draw.h>
#include <sgl_math.h>
#include <sgl_log.h>
#include <sgl_mm.h>
#include <sgl_cfgfix.h>
#include <string.h>
/**
* @brief sgl label object
* @obj: sgl general object
* @desc: draw task descriptor
*/
typedef struct sgl_label
{
sgl_obj_t obj;
const char *text;
char *text_buf; /* 动态文本缓冲,由 label 持有,销毁时自动释放 */
const sgl_font_t *font;
sgl_color_t color;
sgl_color_t bg_color;
uint8_t alpha;
uint8_t align : 6;
uint8_t bg_flag : 1;
uint8_t rota : 1;
union
{
struct
{
int8_t offset_x;
int8_t offset_y;
} offset;
int16_t rotation;
} transform;
} sgl_label_t;
/**
* @brief create a label object
* @param parent parent of the label
* @return pointer to the label object
*/
sgl_obj_t *sgl_label_create(sgl_obj_t *parent);
/**
* @brief set label text
* @param obj pointer to the label object
* @param text text to be set
* @return none
*/
static inline void sgl_label_set_text(sgl_obj_t *obj, const char *text)
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
label->text = text;
sgl_obj_set_dirty(obj);
}
/**
* @brief 设置动态文本:将 text 复制到 label 自有堆内存label 销毁时自动释放
* @note 使用前请先 sprintf 到临时 buf再调用此函数传入 buf
*/
static inline void sgl_label_set_text_dyn(sgl_obj_t *obj, const char *text)
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
size_t len = strlen(text) + 1;
if (label->text_buf != NULL)
{
sgl_free(label->text_buf);
}
label->text_buf = (char *)sgl_malloc(len);
if (label->text_buf != NULL)
{
memcpy(label->text_buf, text, len);
label->text = label->text_buf;
}
sgl_obj_set_dirty(obj);
}
/**
* @brief set label font
* @param obj pointer to the label object
* @param font pointer to the font
* @return none
*/
static inline void sgl_label_set_font(sgl_obj_t *obj, const sgl_font_t *font)
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
label->font = font;
sgl_obj_set_dirty(obj);
}
/**
* @brief set label text color
* @param obj pointer to the label object
* @param color color to be set
* @return none
*/
static inline void sgl_label_set_text_color(sgl_obj_t *obj, sgl_color_t color)
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
label->color = color;
sgl_obj_set_dirty(obj);
}
/**
* @brief set label background color
* @param obj pointer to the label object
* @param color color to be set
* @return none
*/
static inline void sgl_label_set_bg_color(sgl_obj_t *obj, sgl_color_t color)
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
label->bg_color = color;
label->bg_flag = 1;
sgl_obj_set_dirty(obj);
}
/**
* @brief set label radius
* @param obj pointer to the label object
* @param radius radius to be set
* @return none
*/
static inline void sgl_label_set_radius(sgl_obj_t *obj, uint8_t radius)
{
sgl_obj_set_radius(obj, radius);
sgl_obj_set_dirty(obj);
}
/**
* @brief set label text align
* @param obj pointer to the label object
* @param align align to be set
* @return none
*/
static inline void sgl_label_set_text_align(sgl_obj_t *obj, sgl_align_type_t align)
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
label->align = align;
sgl_obj_set_dirty(obj);
}
/**
* @brief set label alpha
* @param obj pointer to the label object
* @param alpha alpha to be set
* @return none
*/
static inline void sgl_label_set_alpha(sgl_obj_t *obj, uint8_t alpha)
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
label->alpha = alpha;
sgl_obj_set_dirty(obj);
}
/**
* @brief set label text offset
* @param obj pointer to the label object
* @param offset_x offset_x to be set
* @param offset_y offset_y to be set
* @return none
*/
static inline void sgl_label_set_text_offset(sgl_obj_t *obj, int8_t offset_x, int8_t offset_y)
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
label->transform.offset.offset_x = offset_x;
label->transform.offset.offset_y = offset_y;
sgl_obj_set_dirty(obj);
}
/**
* @brief set label text rotation
* @param obj pointer to the label object
* @param text_rotation text rotation angle (0-360 degree)
* @return none
*/
static inline void sgl_label_set_text_rotation(sgl_obj_t *obj, int16_t text_rotation)
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
label->transform.rotation = text_rotation % 360;
if (label->transform.rotation < 0)
label->transform.rotation += 360;
label->rota = label->transform.rotation ? 1 : 0;
sgl_obj_set_dirty(obj);
}
#endif // !__SGL_LABEL_H__