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

136 lines
4.2 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 focal_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.radial.focal.x = p.x;
dsc->params.radial.focal.y = p.y;
dsc->params.radial.focal_extent.x = p.x + 10;
dsc->params.radial.focal_extent.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.radial.end.x = p.x;
dsc->params.radial.end.y = p.y;
dsc->params.radial.end_extent.x = p.x + 100;
dsc->params.radial.end_extent.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 radial gradient
* Adjust the end circle and focal point position.
* The radius of the end circle and an focal point are hardcoded in the example.
*/
void lv_example_grad_3(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));
/*Init a radial gradient where the center is at 100;100
*and the edge of the circle is at 200;100.
*Try LV_GRAD_EXTEND_REFLECT and LV_GRAD_EXTEND_REPEAT too. */
lv_grad_radial_init(&grad, 100, 100, 200, 100, LV_GRAD_EXTEND_PAD);
/*The gradient will be calculated between the focal point's circle and the
*edge of the circle. If the center of the focal point and the
*center of the main circle is the same, the gradient will spread
*evenly in all directions. The focal point should be inside the
*main circle.*/
lv_grad_radial_set_focal(&grad, 50, 50, 10);
/*Set the widget containing the gradient*/
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 * focal = lv_button_create(obj);
lv_obj_set_size(focal, 15, 15);
lv_obj_set_style_bg_color(focal, lv_color_hex(0x0000ff), 0);
lv_obj_add_event_cb(focal, focal_event_cb, LV_EVENT_PRESSING, &style);
lv_obj_set_ext_click_area(focal, 5);
lv_obj_set_pos(focal, 50, 50);
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, 100, 100);
}
#else
void lv_example_grad_3(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