Files
lvgl/examples/grad/lv_example_grad_2.c
2025-02-12 22:30:53 -03:00

123 lines
3.5 KiB
C

#include "../lv_examples.h"
#if LV_BUILD_EXAMPLES && LV_USE_LABEL
#if LV_USE_DRAW_SW_COMPLEX_GRADIENTS
static void position_bullet(lv_event_t * e, lv_point_t * p)
{
lv_indev_t * indev = lv_event_get_param(e);
lv_indev_get_point(indev, p);
lv_obj_t * bullet = lv_event_get_target(e);
lv_obj_t * parent = lv_obj_get_parent(bullet);
p->x -= lv_obj_get_x(parent);
p->y -= lv_obj_get_y(parent);
int32_t w = lv_obj_get_width(parent);
int32_t h = lv_obj_get_height(parent);
lv_obj_set_pos(bullet, LV_CLAMP(5, p->x, w - 20), LV_CLAMP(5, p->y, h - 20));
}
static void start_event_cb(lv_event_t * e)
{
lv_style_t * style = lv_event_get_user_data(e);
lv_style_value_t v;
lv_style_get_prop(style, LV_STYLE_BG_GRAD, &v);
lv_grad_dsc_t * dsc = (lv_grad_dsc_t *)v.ptr;
lv_point_t p;
position_bullet(e, &p);
dsc->params.linear.start.x = p.x;
dsc->params.linear.start.y = p.y;
lv_obj_t * bullet = lv_event_get_target(e);
lv_obj_t * parent = lv_obj_get_parent(bullet);
lv_obj_invalidate(parent);
}
static void end_event_cb(lv_event_t * e)
{
lv_style_t * style = lv_event_get_user_data(e);
lv_style_value_t v;
lv_style_get_prop(style, LV_STYLE_BG_GRAD, &v);
lv_grad_dsc_t * dsc = (lv_grad_dsc_t *)v.ptr;
lv_point_t p;
position_bullet(e, &p);
dsc->params.linear.end.x = p.x;
dsc->params.linear.end.y = p.y;
lv_obj_t * bullet = lv_event_get_target(e);
lv_obj_t * parent = lv_obj_get_parent(bullet);
lv_obj_invalidate(parent);
}
/**
* Play with the linear gradient.
* Adjust the 2 point in between the a linear gradient can be drawn (can be skew as well)
*/
void lv_example_grad_2(void)
{
static const lv_color_t grad_colors[2] = {
LV_COLOR_MAKE(0xff, 0x00, 0x00),
LV_COLOR_MAKE(0x00, 0xff, 0x00),
};
static const lv_opa_t grad_opa[2] = {
LV_OPA_100,
LV_OPA_0,
};
static lv_style_t style;
lv_style_init(&style);
/*First define a color gradient. In this example we use a purple to black color map.*/
static lv_grad_dsc_t grad;
lv_grad_init_stops(&grad, grad_colors, grad_opa, NULL, sizeof(grad_colors) / sizeof(lv_color_t));
lv_grad_linear_init(&grad, 100, 100, 200, 150, LV_GRAD_EXTEND_PAD);
/*Set gradient as background*/
lv_style_set_bg_grad(&style, &grad);
lv_style_set_border_width(&style, 2);
lv_style_set_pad_all(&style, 0);
lv_style_set_radius(&style, 12);
/*Create an object with the new style*/
lv_obj_t * obj = lv_obj_create(lv_screen_active());
lv_obj_add_style(obj, &style, 0);
lv_obj_set_size(obj, lv_pct(80), lv_pct(80));
lv_obj_center(obj);
lv_obj_t * start = lv_button_create(obj);
lv_obj_set_size(start, 15, 15);
lv_obj_set_style_bg_color(start, lv_color_hex(0x0000ff), 0);
lv_obj_add_event_cb(start, start_event_cb, LV_EVENT_PRESSING, &style);
lv_obj_set_ext_click_area(start, 5);
lv_obj_set_pos(start, 100, 100);
lv_obj_t * end = lv_button_create(obj);
lv_obj_set_size(end, 15, 15);
lv_obj_set_style_bg_color(end, lv_color_hex(0x00ffff), 0);
lv_obj_add_event_cb(end, end_event_cb, LV_EVENT_PRESSING, &style);
lv_obj_set_ext_click_area(end, 5);
lv_obj_set_pos(end, 200, 150);
}
#else
void lv_example_grad_2(void)
{
lv_obj_t * label = lv_label_create(lv_screen_active());
lv_label_set_text(label, "LV_USE_DRAW_SW_COMPLEX_GRADIENTS needs to be enabled");
lv_obj_center(label);
}
#endif /*LV_USE_DRAW_SW_COMPLEX_GRADIENTS*/
#endif