1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-21 06:53:01 +08:00
lvgl/src/lv_objx/lv_cpicker.c

999 lines
30 KiB
C
Raw Normal View History

/**
* @file lv_cpicker.c
*
* From @AloyseTech and @paulpv.
*/
/*********************
* INCLUDES
*********************/
#include "lv_cpicker.h"
#if LV_USE_CPICKER != 0
2019-09-30 07:00:34 -07:00
#include "../lv_core/lv_debug.h"
#include "../lv_draw/lv_draw_arc.h"
#include "../lv_themes/lv_theme.h"
#include "../lv_core/lv_indev.h"
#include "../lv_core/lv_refr.h"
#include "../lv_misc/lv_math.h"
/*********************
* DEFINES
*********************/
2019-10-01 21:16:30 +02:00
#define LV_OBJX_NAME "lv_cpicker"
#ifndef LV_CPICKER_DEF_TYPE
#define LV_CPICKER_DEF_TYPE LV_CPICKER_TYPE_DISC
#endif
#ifndef LV_CPICKER_DEF_HUE
#define LV_CPICKER_DEF_HUE 0
#endif
#ifndef LV_CPICKER_DEF_SATURATION
#define LV_CPICKER_DEF_SATURATION 100
#endif
#ifndef LV_CPICKER_DEF_VALUE
#define LV_CPICKER_DEF_VALUE 100
#endif
2019-09-30 07:00:34 -07:00
#ifndef LV_CPICKER_DEF_HSV
#define LV_CPICKER_DEF_HSV ((lv_color_hsv_t){LV_CPICKER_DEF_HUE, LV_CPICKER_DEF_SATURATION, LV_CPICKER_DEF_VALUE})
#endif
#ifndef LV_CPICKER_DEF_QF /*quantization factor*/
2019-09-25 23:16:12 -07:00
#define LV_CPICKER_DEF_QF 3
#endif
2019-10-01 21:16:30 +02:00
#define TRI_OFFSET 2
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2019-11-04 16:56:57 +01:00
static lv_design_res_t lv_cpicker_design(lv_obj_t * cpicker, const lv_area_t * clip_area, lv_design_mode_t mode);
2019-09-26 10:50:34 -07:00
static lv_res_t lv_cpicker_signal(lv_obj_t * cpicker, lv_signal_t sign, void * param);
2020-02-11 06:41:03 +01:00
static lv_style_list_t * lv_cpicker_get_style(lv_obj_t * cpicker, uint8_t part);
static bool lv_cpicker_hit(lv_obj_t * cpicker, const lv_point_t * p);
2020-02-11 06:41:03 +01:00
static void draw_rect_grad(lv_obj_t * cpicker, const lv_area_t * mask);
static void draw_disc_grad(lv_obj_t * cpicker, const lv_area_t * mask);
static void draw_indic(lv_obj_t * cpicker, const lv_area_t * mask);
static void invalidate_indic(lv_obj_t * cpicker);
2019-10-03 23:35:57 -07:00
static lv_area_t get_indic_area(lv_obj_t * cpicker);
2019-10-01 21:16:30 +02:00
static void next_color_mode(lv_obj_t * cpicker);
static lv_res_t double_click_reset(lv_obj_t * cpicker);
static void refr_indic_pos(lv_obj_t * cpicker);
static lv_color_t angle_to_mode_color(lv_obj_t * cpicker, uint16_t angle);
static uint16_t get_angle(lv_obj_t * cpicker);
/**********************
* STATIC VARIABLES
**********************/
static lv_signal_cb_t ancestor_signal;
static lv_design_cb_t ancestor_design;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create a color_picker object
* @param par pointer to an object, it will be the parent of the new color_picker
* @param copy pointer to a color_picker object, if not NULL then the new object will be copied from it
* @return pointer to the created color_picker
*/
lv_obj_t * lv_cpicker_create(lv_obj_t * par, const lv_obj_t * copy)
{
LV_LOG_TRACE("color_picker create started");
2020-02-11 06:41:03 +01:00
lv_obj_t * cpicker = lv_obj_create(par, copy);
LV_ASSERT_MEM(cpicker);
if(cpicker == NULL) return NULL;
2020-02-11 06:41:03 +01:00
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(cpicker);
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(cpicker);
/*Allocate the extended data*/
2020-02-11 06:41:03 +01:00
lv_cpicker_ext_t * ext = lv_obj_allocate_ext_attr(cpicker, sizeof(lv_cpicker_ext_t));
2019-09-30 07:00:34 -07:00
LV_ASSERT_MEM(ext);
if(ext == NULL) {
2020-02-11 06:41:03 +01:00
lv_obj_del(cpicker);
return NULL;
}
/*Initialize the allocated 'ext' */
2019-11-06 05:32:42 +01:00
ext->type = LV_CPICKER_DEF_TYPE;
2019-09-30 07:00:34 -07:00
ext->hsv = LV_CPICKER_DEF_HSV;
2020-02-13 06:56:14 +01:00
ext->indic.colored = 1;
ext->color_mode = LV_CPICKER_COLOR_MODE_HUE;
ext->color_mode_fixed = 0;
2019-10-01 05:20:20 +02:00
ext->last_click_time = 0;
2019-10-02 16:47:08 +02:00
ext->last_change_time = 0;
2020-02-11 06:41:03 +01:00
lv_style_list_init(&ext->indic.style_list);
/*The signal and design functions are not copied so set them here*/
2020-02-11 06:41:03 +01:00
lv_obj_set_signal_cb(cpicker, lv_cpicker_signal);
lv_obj_set_design_cb(cpicker, lv_cpicker_design);
/*If no copy do the basic initialization*/
if(copy == NULL) {
2020-02-11 06:41:03 +01:00
lv_obj_set_size(cpicker, LV_DPI * 2, LV_DPI * 2);
lv_obj_set_protect(cpicker, LV_PROTECT_PRESS_LOST);
2020-02-13 06:56:14 +01:00
lv_obj_set_adv_hittest(cpicker, true);
2020-02-11 06:41:03 +01:00
lv_theme_apply(cpicker, LV_THEME_CPICKER);
}
/*Copy 'copy'*/
else {
lv_cpicker_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
2019-10-01 21:16:30 +02:00
ext->type = copy_ext->type;
ext->color_mode = copy_ext->color_mode;
ext->color_mode_fixed = copy_ext->color_mode_fixed;
ext->hsv = copy_ext->hsv;
ext->indic.colored = copy_ext->indic.colored;
2020-02-11 06:41:03 +01:00
lv_style_list_copy(&ext->indic.style_list, &copy_ext->indic.style_list);
/*Refresh the style with new signal function*/
2020-02-11 06:41:03 +01:00
lv_obj_refresh_style(cpicker);
}
2020-02-11 06:41:03 +01:00
refr_indic_pos(cpicker);
LV_LOG_INFO("color_picker created");
2020-02-11 06:41:03 +01:00
return cpicker;
}
/*=====================
* Setter functions
*====================*/
2019-09-24 21:06:51 -07:00
/**
* Set a new type for a cpicker
* @param cpicker pointer to a cpicker object
* @param type new type of the cpicker (from 'lv_cpicker_type_t' enum)
*/
void lv_cpicker_set_type(lv_obj_t * cpicker, lv_cpicker_type_t type)
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
2019-09-24 21:06:51 -07:00
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
if(ext->type == type) return;
ext->type = type;
2019-10-01 21:16:30 +02:00
lv_obj_refresh_ext_draw_pad(cpicker);
refr_indic_pos(cpicker);
2019-09-24 21:06:51 -07:00
lv_obj_invalidate(cpicker);
}
/**
* Set the current hue of a colorpicker.
* @param cpicker pointer to colorpicker object
* @param hue current selected hue [0..360]
* @return true if changed, otherwise false
*/
bool lv_cpicker_set_hue(lv_obj_t * cpicker, uint16_t hue)
{
lv_color_hsv_t hsv = lv_cpicker_get_hsv(cpicker);
hsv.h = hue;
return lv_cpicker_set_hsv(cpicker, hsv);
}
/**
* Set the current saturation of a colorpicker.
* @param cpicker pointer to colorpicker object
* @param saturation current selected saturation [0..100]
* @return true if changed, otherwise false
*/
bool lv_cpicker_set_saturation(lv_obj_t * cpicker, uint8_t saturation)
{
lv_color_hsv_t hsv = lv_cpicker_get_hsv(cpicker);
hsv.s = saturation;
return lv_cpicker_set_hsv(cpicker, hsv);
}
/**
* Set the current value of a colorpicker.
* @param cpicker pointer to colorpicker object
* @param val current selected value [0..100]
* @return true if changed, otherwise false
*/
bool lv_cpicker_set_value(lv_obj_t * cpicker, uint8_t val)
{
lv_color_hsv_t hsv = lv_cpicker_get_hsv(cpicker);
hsv.v = val;
return lv_cpicker_set_hsv(cpicker, hsv);
}
/**
2019-09-30 07:00:34 -07:00
* Set the current hsv of a colorpicker.
* @param cpicker pointer to colorpicker object
2019-09-30 07:00:34 -07:00
* @param color current selected hsv
* @return true if changed, otherwise false
*/
bool lv_cpicker_set_hsv(lv_obj_t * cpicker, lv_color_hsv_t hsv)
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
if (hsv.h > 360) hsv.h %= 360;
if (hsv.s > 100) hsv.s = 100;
if (hsv.v > 100) hsv.v = 100;
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
if (ext->hsv.h == hsv.h && ext->hsv.s == hsv.s && ext->hsv.v == hsv.v) return false;
2019-09-30 07:00:34 -07:00
ext->hsv = hsv;
2019-10-01 21:16:30 +02:00
refr_indic_pos(cpicker);
2020-02-13 06:56:14 +01:00
if (ext->type == LV_CPICKER_TYPE_DISC) {
lv_obj_invalidate(cpicker);
}
return true;
}
2019-09-30 07:00:34 -07:00
/**
* Set the current color of a colorpicker.
* @param cpicker pointer to colorpicker object
* @param color current selected color
* @return true if changed, otherwise false
2019-09-30 07:00:34 -07:00
*/
bool lv_cpicker_set_color(lv_obj_t * cpicker, lv_color_t color)
2019-09-30 07:00:34 -07:00
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
2019-10-30 06:00:13 +01:00
lv_color32_t c32;
c32.full = lv_color_to32(color);
return lv_cpicker_set_hsv(cpicker,
2019-11-15 09:17:42 +01:00
lv_color_rgb_to_hsv(c32.ch.red, c32.ch.green, c32.ch.blue));
2019-09-30 07:00:34 -07:00
}
/**
* Set the current color mode.
* @param cpicker pointer to colorpicker object
* @param mode color mode (hue/sat/val)
*/
void lv_cpicker_set_color_mode(lv_obj_t * cpicker, lv_cpicker_color_mode_t mode)
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
ext->color_mode = mode;
2019-10-01 21:16:30 +02:00
refr_indic_pos(cpicker);
lv_obj_invalidate(cpicker);
}
/**
* Set if the color mode is changed on long press on center
* @param cpicker pointer to colorpicker object
* @param fixed color mode cannot be changed on long press
*/
void lv_cpicker_set_color_mode_fixed(lv_obj_t * cpicker, bool fixed)
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
ext->color_mode_fixed = fixed;
}
2019-10-01 21:16:30 +02:00
/**
* Make the indicator to be colored to the current color
* @param cpicker pointer to colorpicker object
* @param en true: color the indicator; false: not color the indicator
*/
void lv_cpicker_set_indic_colored(lv_obj_t * cpicker, bool en)
{
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
ext->indic.colored = en ? 1 : 0;
invalidate_indic(cpicker);
}
/*=====================
* Getter functions
*====================*/
/**
* Get the current color mode.
* @param cpicker pointer to colorpicker object
* @return color mode (hue/sat/val)
*/
lv_cpicker_color_mode_t lv_cpicker_get_color_mode(lv_obj_t * cpicker)
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
return ext->color_mode;
}
/**
* Get if the color mode is changed on long press on center
* @param cpicker pointer to colorpicker object
* @return mode cannot be changed on long press
*/
bool lv_cpicker_get_color_mode_fixed(lv_obj_t * cpicker)
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
return ext->color_mode_fixed;
}
/**
2019-09-30 07:00:34 -07:00
* Get the current selected hue of a colorpicker.
* @param cpicker pointer to colorpicker object
* @return hue current selected hue
*/
uint16_t lv_cpicker_get_hue(lv_obj_t * cpicker)
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
2019-09-30 07:00:34 -07:00
return ext->hsv.h;
}
/**
2019-09-30 07:00:34 -07:00
* Get the current selected saturation of a colorpicker.
* @param cpicker pointer to colorpicker object
* @return current selected saturation
*/
uint8_t lv_cpicker_get_saturation(lv_obj_t * cpicker)
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
2019-09-30 07:00:34 -07:00
return ext->hsv.s;
}
/**
2019-09-30 07:00:34 -07:00
* Get the current selected hue of a colorpicker.
* @param cpicker pointer to colorpicker object
* @return current selected value
*/
uint8_t lv_cpicker_get_value(lv_obj_t * cpicker)
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
2019-09-30 07:00:34 -07:00
return ext->hsv.v;
}
/**
* Get the current selected hsv of a colorpicker.
* @param cpicker pointer to colorpicker object
* @return current selected hsv
*/
lv_color_hsv_t lv_cpicker_get_hsv(lv_obj_t * cpicker)
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
2019-09-30 07:00:34 -07:00
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
return ext->hsv;
}
/**
* Get the current selected color of a colorpicker.
* @param cpicker pointer to colorpicker object
* @return color current selected color
*/
lv_color_t lv_cpicker_get_color(lv_obj_t * cpicker)
{
2019-10-01 21:16:30 +02:00
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
2019-09-30 07:00:34 -07:00
return lv_color_hsv_to_rgb(ext->hsv.h, ext->hsv.s, ext->hsv.v);
}
2019-10-01 21:16:30 +02:00
/**
* Whether the indicator is colored to the current color or not
* @param cpicker pointer to colorpicker object
* @return true: color the indicator; false: not color the indicator
*/
bool lv_cpicker_get_indic_colored(lv_obj_t * cpicker)
{
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
return ext->indic.colored ? true : false;
}
/*=====================
* Other functions
*====================*/
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Handle the drawing related tasks of the color_picker
* @param cpicker pointer to an object
* @param mask the object will be drawn only in this area
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
* (return 'true' if yes)
* LV_DESIGN_DRAW: draw the object (always return 'true')
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
2019-11-04 16:56:57 +01:00
* @return return an element of `lv_design_res_t`
*/
2019-11-04 16:56:57 +01:00
static lv_design_res_t lv_cpicker_design(lv_obj_t * cpicker, const lv_area_t * clip_area, lv_design_mode_t mode)
{
/*Return false if the object is not covers the mask_p area*/
2019-10-01 05:20:20 +02:00
if(mode == LV_DESIGN_COVER_CHK) {
2019-11-04 16:56:57 +01:00
return LV_DESIGN_RES_NOT_COVER;
}
/*Draw the object*/
2019-10-01 05:20:20 +02:00
else if(mode == LV_DESIGN_DRAW_MAIN) {
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
2019-10-01 05:20:20 +02:00
if(ext->type == LV_CPICKER_TYPE_DISC) {
2020-02-11 06:41:03 +01:00
draw_disc_grad(cpicker, clip_area);
2019-10-01 05:20:20 +02:00
} else if(ext->type == LV_CPICKER_TYPE_RECT) {
2020-02-11 06:41:03 +01:00
draw_rect_grad(cpicker, clip_area);
}
2019-10-01 21:16:30 +02:00
2020-02-11 06:41:03 +01:00
draw_indic(cpicker, clip_area);
}
/*Post draw when the children are drawn*/
2019-10-01 05:20:20 +02:00
else if(mode == LV_DESIGN_DRAW_POST) {
}
2019-11-04 16:56:57 +01:00
return LV_DESIGN_RES_OK;
}
2020-02-11 06:41:03 +01:00
static void draw_disc_grad(lv_obj_t * cpicker, const lv_area_t * mask)
{
2019-10-02 16:59:11 +02:00
lv_coord_t w = lv_obj_get_width(cpicker);
lv_coord_t h = lv_obj_get_height(cpicker);
lv_coord_t cx = cpicker->coords.x1 + w / 2;
lv_coord_t cy = cpicker->coords.y1 + h / 2;
lv_coord_t r = w / 2;
2019-10-01 21:16:30 +02:00
2020-02-11 06:41:03 +01:00
lv_draw_line_dsc_t line_dsc;
lv_draw_line_dsc_init(&line_dsc);
lv_obj_init_draw_line_dsc(cpicker, LV_CPICKER_PART_MAIN, &line_dsc);
line_dsc.width = (r * 628 / (360 / LV_CPICKER_DEF_QF)) / 100;
line_dsc.width += 2;
2019-11-15 09:17:42 +01:00
uint16_t i;
2020-02-11 06:41:03 +01:00
lv_coord_t cir_w = lv_obj_get_style_scale_width(cpicker, LV_CPICKER_PART_MAIN);
2019-09-25 23:38:27 -07:00
2019-11-15 09:20:49 +01:00
for(i = 0; i <= 360; i+= LV_CPICKER_DEF_QF) {
2020-02-11 06:41:03 +01:00
line_dsc.color = angle_to_mode_color(cpicker, i);
2019-10-01 21:16:30 +02:00
2019-10-02 16:59:11 +02:00
lv_point_t p[2];
p[0].x = cx + (r * lv_trigo_sin(i) >> LV_TRIGO_SHIFT);
p[0].y = cy + (r * lv_trigo_sin(i+ 90) >> LV_TRIGO_SHIFT);
p[1].x = cx + ((r-cir_w) * lv_trigo_sin(i) >> LV_TRIGO_SHIFT);
p[1].y = cy + ((r-cir_w) * lv_trigo_sin(i+ 90) >> LV_TRIGO_SHIFT);
2019-10-01 21:16:30 +02:00
2020-02-11 06:41:03 +01:00
lv_draw_line(&p[0], &p[1], mask, &line_dsc);
2019-10-02 16:59:11 +02:00
}
2019-10-01 21:16:30 +02:00
2020-02-13 06:56:14 +01:00
/*Mask out the inner area*/
lv_draw_rect_dsc_t bg_dsc;
lv_draw_rect_dsc_init(&bg_dsc);
lv_obj_init_draw_rect_dsc(cpicker, LV_CPICKER_PART_MAIN, &bg_dsc);
bg_dsc.radius = LV_RADIUS_CIRCLE;
2020-02-11 06:41:03 +01:00
2020-02-13 06:56:14 +01:00
lv_area_t area_mid;
lv_area_copy(&area_mid, &cpicker->coords);
area_mid.x1 += cir_w;
area_mid.y1 += cir_w;
area_mid.x2 -= cir_w;
area_mid.y2 -= cir_w;
lv_draw_rect(&area_mid, mask, &bg_dsc);
lv_style_int_t inner = lv_obj_get_style_pad_inner(cpicker, LV_CPICKER_PART_MAIN);
lv_color_t color = lv_cpicker_get_color(cpicker);
bg_dsc.bg_color = color;
area_mid.x1 += inner;
area_mid.y1 += inner;
area_mid.x2 -= inner;
area_mid.y2 -= inner;
lv_draw_rect(&area_mid, mask, &bg_dsc);
2019-09-25 23:38:27 -07:00
}
2020-02-11 06:41:03 +01:00
static void draw_rect_grad(lv_obj_t * cpicker, const lv_area_t * mask)
2019-09-25 23:38:27 -07:00
{
2020-02-11 06:41:03 +01:00
lv_draw_rect_dsc_t bg_dsc;
lv_draw_rect_dsc_init(&bg_dsc);
lv_obj_init_draw_rect_dsc(cpicker, LV_CPICKER_PART_MAIN, &bg_dsc);
2019-09-26 12:32:05 -07:00
2019-10-01 21:16:30 +02:00
lv_area_t grad_area;
lv_obj_get_coords(cpicker, &grad_area);
2019-09-26 12:32:05 -07:00
2020-02-11 06:41:03 +01:00
if(bg_dsc.radius) {
2019-10-01 21:16:30 +02:00
lv_coord_t h = lv_obj_get_height(cpicker);
2020-02-11 06:41:03 +01:00
lv_coord_t r = bg_dsc.radius;
2019-10-01 21:16:30 +02:00
if(r > h / 2) r = h / 2;
/*Make the gradient area smaller with a half circle on both ends*/
grad_area.x1 += r;
grad_area.x2 -= r;
2019-09-26 12:32:05 -07:00
2019-10-01 21:16:30 +02:00
/*Draw the left rounded end*/
lv_area_t rounded_edge_area;
lv_obj_get_coords(cpicker, &rounded_edge_area);
rounded_edge_area.x2 = rounded_edge_area.x1 + 2 * r;
2019-09-26 12:32:05 -07:00
2020-02-11 06:41:03 +01:00
bg_dsc.bg_color = angle_to_mode_color(cpicker, 0);
2019-09-25 23:38:27 -07:00
2020-02-11 06:41:03 +01:00
lv_draw_rect(&rounded_edge_area, mask, &bg_dsc);
2019-10-01 21:16:30 +02:00
/*Draw the right rounded end*/
lv_obj_get_coords(cpicker, &rounded_edge_area);
rounded_edge_area.x1 = rounded_edge_area.x2 - 2 * r;
2020-02-11 06:41:03 +01:00
bg_dsc.bg_color = angle_to_mode_color(cpicker, 359);
2019-10-01 21:16:30 +02:00
2020-02-11 06:41:03 +01:00
lv_draw_rect(&rounded_edge_area, mask, &bg_dsc);
2019-09-26 12:32:05 -07:00
}
2019-10-01 21:16:30 +02:00
lv_coord_t grad_w = lv_area_get_width(&grad_area);
uint16_t i_step = LV_MATH_MAX(LV_CPICKER_DEF_QF, 360 / grad_w);
2020-02-11 06:41:03 +01:00
bg_dsc.radius = 0;
bg_dsc.border_width = 0;
bg_dsc.shadow_width = 0;
2019-10-01 21:16:30 +02:00
2019-11-15 09:17:42 +01:00
uint16_t i;
for(i = 0; i < 360; i += i_step) {
2020-02-11 06:41:03 +01:00
bg_dsc.bg_color = angle_to_mode_color(cpicker, i);
2019-10-01 21:16:30 +02:00
/*the following attribute might need changing between index to add border, shadow, radius etc*/
lv_area_t rect_area;
/*scale angle (hue/sat/val) to linear coordinate*/
lv_coord_t xi = (i * grad_w) / 360;
rect_area.x1 = LV_MATH_MIN(grad_area.x1 + xi, grad_area.x1 + grad_w - i_step);
rect_area.y1 = grad_area.y1;
rect_area.x2 = rect_area.x1 + i_step;
rect_area.y2 = grad_area.y2;
2020-02-11 06:41:03 +01:00
lv_draw_rect(&rect_area, mask, &bg_dsc);
2019-09-26 12:32:05 -07:00
}
2019-09-25 23:38:27 -07:00
}
2019-10-03 03:11:38 -07:00
2020-02-11 06:41:03 +01:00
static void draw_indic(lv_obj_t * cpicker, const lv_area_t * mask)
{
2019-10-01 21:16:30 +02:00
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
2019-09-26 11:24:47 -07:00
2020-02-11 06:41:03 +01:00
lv_draw_rect_dsc_t cir_dsc;
2020-02-12 08:54:03 +01:00
lv_draw_rect_dsc_init(&cir_dsc);
2020-02-11 06:41:03 +01:00
lv_obj_init_draw_rect_dsc(cpicker, LV_CPICKER_PART_INDIC, &cir_dsc);
cir_dsc.radius = LV_RADIUS_CIRCLE;
2019-09-26 11:24:47 -07:00
2019-10-01 21:16:30 +02:00
if(ext->indic.colored) {
2020-02-11 06:41:03 +01:00
cir_dsc.bg_color = lv_cpicker_get_color(cpicker);
2019-09-26 11:24:47 -07:00
}
2019-10-03 23:35:57 -07:00
lv_area_t indic_area = get_indic_area(cpicker);
2020-02-11 06:41:03 +01:00
lv_draw_rect(&indic_area, mask, &cir_dsc);
}
static void invalidate_indic(lv_obj_t * cpicker)
{
2019-10-03 23:35:57 -07:00
lv_area_t indic_area = get_indic_area(cpicker);
lv_obj_invalidate_area(cpicker, &indic_area);
}
2019-10-03 23:35:57 -07:00
static lv_area_t get_indic_area(lv_obj_t * cpicker)
{
2019-10-03 23:35:57 -07:00
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
2020-02-11 06:41:03 +01:00
/*Get indicator's radius*/
uint16_t r = 0;
2020-02-11 06:41:03 +01:00
if(ext->type == LV_CPICKER_TYPE_DISC) {
2020-02-13 06:56:14 +01:00
r = lv_obj_get_style_scale_width(cpicker, LV_CPICKER_PART_MAIN) / 2;
2020-02-11 06:41:03 +01:00
}
else if(ext->type == LV_CPICKER_TYPE_RECT) {
lv_coord_t h = lv_obj_get_height(cpicker);
r = h / 2;
}
2020-02-13 06:56:14 +01:00
lv_style_int_t left = lv_obj_get_style_pad_left(cpicker, LV_CPICKER_PART_INDIC);
lv_style_int_t right = lv_obj_get_style_pad_right(cpicker, LV_CPICKER_PART_INDIC);
lv_style_int_t top = lv_obj_get_style_pad_top(cpicker, LV_CPICKER_PART_INDIC);
lv_style_int_t bottom = lv_obj_get_style_pad_bottom(cpicker, LV_CPICKER_PART_INDIC);
2020-02-11 06:41:03 +01:00
lv_area_t indic_area;
2020-02-11 06:41:03 +01:00
indic_area.x1 = cpicker->coords.x1 + ext->indic.pos.x - r - left;
indic_area.y1 = cpicker->coords.y1 + ext->indic.pos.y - r - right;
indic_area.x2 = cpicker->coords.x1 + ext->indic.pos.x + r + top;
indic_area.y2 = cpicker->coords.y1 + ext->indic.pos.y + r + bottom;
return indic_area;
2019-10-01 21:16:30 +02:00
}
2019-10-01 21:16:30 +02:00
/**
* Signal function of the color_picker
* @param cpicker pointer to a color_picker object
* @param sign a signal type from lv_signal_t enum
* @param param pointer to a signal specific variable
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
*/
static lv_res_t lv_cpicker_signal(lv_obj_t * cpicker, lv_signal_t sign, void * param)
{
/* Include the ancient signal function */
2020-02-11 06:41:03 +01:00
lv_res_t res;
if(sign == LV_SIGNAL_GET_STYLE) {
lv_get_style_info_t * info = param;
info->result = lv_cpicker_get_style(cpicker, info->part);
if(info->result != NULL) return LV_RES_OK;
else return ancestor_signal(cpicker, sign, param);
}
res = ancestor_signal(cpicker, sign, param);
2019-10-01 21:16:30 +02:00
if(res != LV_RES_OK) return res;
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
2019-10-01 21:16:30 +02:00
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
2019-10-01 21:16:30 +02:00
if(sign == LV_SIGNAL_CLEANUP) {
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
} else if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) {
2020-02-13 06:56:14 +01:00
lv_style_int_t left = lv_obj_get_style_pad_left(cpicker, LV_CPICKER_PART_INDIC);
lv_style_int_t right = lv_obj_get_style_pad_right(cpicker, LV_CPICKER_PART_INDIC);
lv_style_int_t top = lv_obj_get_style_pad_top(cpicker, LV_CPICKER_PART_INDIC);
lv_style_int_t bottom = lv_obj_get_style_pad_bottom(cpicker, LV_CPICKER_PART_INDIC);
2019-10-01 21:16:30 +02:00
2020-02-13 06:56:14 +01:00
lv_coord_t indic_pad = LV_MATH_MAX4(left, right, top, bottom) + 2;
2019-10-01 21:16:30 +02:00
2020-02-13 06:56:14 +01:00
if(ext->type == LV_CPICKER_TYPE_DISC) {
cpicker->ext_draw_pad = LV_MATH_MAX(cpicker->ext_draw_pad, indic_pad);
} else {
indic_pad += lv_obj_get_height(cpicker) / 2;
cpicker->ext_draw_pad = LV_MATH_MAX(cpicker->ext_draw_pad, indic_pad);
}
2020-02-11 06:41:03 +01:00
} else if(sign == LV_SIGNAL_COORD_CHG) {
2019-10-01 21:16:30 +02:00
/*Refresh extended draw area to make knob visible*/
if(lv_obj_get_width(cpicker) != lv_area_get_width(param) ||
2019-10-03 03:11:38 -07:00
lv_obj_get_height(cpicker) != lv_area_get_height(param)) {
2019-10-01 21:16:30 +02:00
lv_obj_refresh_ext_draw_pad(cpicker);
refr_indic_pos(cpicker);
}
} else if(sign == LV_SIGNAL_STYLE_CHG) {
/*Refresh extended draw area to make knob visible*/
lv_obj_refresh_ext_draw_pad(cpicker);
refr_indic_pos(cpicker);
}
else if(sign == LV_SIGNAL_CONTROL) {
uint32_t c = *((uint32_t *)param); /*uint32_t because can be UTF-8*/
if(c == LV_KEY_RIGHT || c == LV_KEY_UP) {
lv_color_hsv_t hsv_cur;
hsv_cur = ext->hsv;
switch(ext->color_mode) {
case LV_CPICKER_COLOR_MODE_HUE:
hsv_cur.h = (ext->hsv.h + 1) % 360;
break;
case LV_CPICKER_COLOR_MODE_SATURATION:
hsv_cur.s = (ext->hsv.s + 1) % 100;
break;
case LV_CPICKER_COLOR_MODE_VALUE:
hsv_cur.v = (ext->hsv.v + 1) % 100;
break;
}
2019-10-01 21:16:30 +02:00
if (lv_cpicker_set_hsv(cpicker, hsv_cur)) {
res = lv_event_send(cpicker, LV_EVENT_VALUE_CHANGED, NULL);
if(res != LV_RES_OK) return res;
}
2019-10-01 21:16:30 +02:00
}
else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) {
lv_color_hsv_t hsv_cur;
hsv_cur = ext->hsv;
switch(ext->color_mode) {
case LV_CPICKER_COLOR_MODE_HUE:
hsv_cur.h = ext->hsv.h > 0?(ext->hsv.h - 1) : 360;
break;
case LV_CPICKER_COLOR_MODE_SATURATION:
hsv_cur.s = ext->hsv.s > 0?(ext->hsv.s - 1) : 100;
break;
case LV_CPICKER_COLOR_MODE_VALUE:
hsv_cur.v = ext->hsv.v > 0?(ext->hsv.v - 1) : 100;
break;
}
if (lv_cpicker_set_hsv(cpicker, hsv_cur)) {
res = lv_event_send(cpicker, LV_EVENT_VALUE_CHANGED, NULL);
if(res != LV_RES_OK) return res;
}
2019-09-26 11:24:47 -07:00
}
}
2019-10-01 21:16:30 +02:00
else if(sign == LV_SIGNAL_PRESSED) {
ext->last_change_time = lv_tick_get();
2019-10-01 22:00:23 +02:00
lv_indev_get_point(lv_indev_get_act(), &ext->last_press_point);
res = double_click_reset(cpicker);
2019-10-01 21:16:30 +02:00
if(res != LV_RES_OK) return res;
2019-10-03 03:11:38 -07:00
} else if(sign == LV_SIGNAL_PRESSING) {
2019-10-01 21:16:30 +02:00
lv_indev_t * indev = lv_indev_get_act();
2019-10-01 22:00:23 +02:00
if(indev == NULL) return res;
2019-10-01 21:16:30 +02:00
lv_point_t p;
lv_indev_get_point(indev, &p);
2019-10-01 22:00:23 +02:00
if((LV_MATH_ABS(p.x - ext->last_press_point.x) > indev->driver.drag_limit / 2) ||
2019-10-02 16:59:11 +02:00
(LV_MATH_ABS(p.y - ext->last_press_point.y) > indev->driver.drag_limit / 2)) {
2019-10-01 22:00:23 +02:00
ext->last_change_time = lv_tick_get();
ext->last_press_point.x = p.x;
ext->last_press_point.y = p.y;
}
2019-10-01 21:16:30 +02:00
p.x -= cpicker->coords.x1;
p.y -= cpicker->coords.y1;
2019-10-01 21:16:30 +02:00
/*Ignore pressing in the inner area*/
uint16_t w = lv_obj_get_width(cpicker);
2019-10-01 21:16:30 +02:00
int16_t angle = 0;
2019-10-01 21:16:30 +02:00
if(ext->type == LV_CPICKER_TYPE_RECT) {
2019-10-01 22:00:23 +02:00
/*If pressed long enough without change go to next color mode*/
uint32_t diff = lv_tick_elaps(ext->last_change_time);
2019-12-02 12:20:01 +01:00
if(diff > (uint32_t)indev->driver.long_press_time * 2 && !ext->color_mode_fixed) {
2019-10-01 22:00:23 +02:00
next_color_mode(cpicker);
lv_indev_wait_release(lv_indev_get_act());
return res;
}
2019-10-01 21:16:30 +02:00
angle = (p.x * 360) / w;
if(angle < 0) angle = 0;
if(angle >= 360) angle = 359;
2019-10-01 21:16:30 +02:00
} else if(ext->type == LV_CPICKER_TYPE_DISC) {
2020-02-11 06:41:03 +01:00
lv_style_int_t scale_w = lv_obj_get_style_scale_width(cpicker, LV_CPICKER_PART_MAIN);
2019-10-01 22:00:23 +02:00
lv_coord_t r_in = w / 2;
p.x -= r_in;
p.y -= r_in;
bool on_ring = true;
2020-02-11 06:41:03 +01:00
r_in -= scale_w;
2019-10-01 22:08:14 +02:00
if(r_in > LV_DPI / 2) {
2020-02-13 06:56:14 +01:00
lv_style_int_t inner = lv_obj_get_style_pad_inner(cpicker, LV_CPICKER_PART_MAIN);
r_in -= inner;
2019-10-01 22:08:14 +02:00
2019-10-02 08:28:24 +02:00
if(r_in < LV_DPI / 2) r_in = LV_DPI / 2;
2019-10-01 22:08:14 +02:00
}
2019-10-01 22:00:23 +02:00
if(p.x * p.x + p.y * p.y < r_in * r_in) {
on_ring = false;
}
/*If the inner area is being pressed, go to the next color mode on long press*/
uint32_t diff = lv_tick_elaps(ext->last_change_time);
2020-02-13 06:56:14 +01:00
if(!on_ring && diff > indev->driver.long_press_time && !ext->color_mode_fixed) {
next_color_mode(cpicker);
lv_indev_wait_release(lv_indev_get_act());
2019-10-01 22:00:23 +02:00
return res;
}
2019-10-01 21:16:30 +02:00
/*Set the angle only if pressed on the ring*/
if(!on_ring) return res;
2019-10-01 21:16:30 +02:00
angle = lv_atan2(p.x, p.y) % 360;
}
lv_color_hsv_t hsv_cur;
hsv_cur = ext->hsv;
2019-10-01 21:16:30 +02:00
switch(ext->color_mode) {
case LV_CPICKER_COLOR_MODE_HUE:
hsv_cur.h = angle;
break;
case LV_CPICKER_COLOR_MODE_SATURATION:
hsv_cur.s = (angle * 100) / 360;
break;
case LV_CPICKER_COLOR_MODE_VALUE:
hsv_cur.v = (angle * 100) / 360;
break;
}
2019-09-30 07:00:34 -07:00
if (lv_cpicker_set_hsv(cpicker, hsv_cur)) {
res = lv_event_send(cpicker, LV_EVENT_VALUE_CHANGED, NULL);
if(res != LV_RES_OK) return res;
}
} else if(sign == LV_SIGNAL_HIT_TEST) {
lv_hit_test_info_t *info = param;
info->result = lv_cpicker_hit(cpicker, info->point);
}
return res;
}
2020-02-11 06:41:03 +01:00
/**
* Get the style_list descriptor of a part of the object
* @param cpicker pointer the object
* @param part the part of the cpicker. (LV_PAGE_CPICKER_...)
* @return pointer to the style_list descriptor of the specified part
*/
static lv_style_list_t * lv_cpicker_get_style(lv_obj_t * cpicker, uint8_t part)
{
LV_ASSERT_OBJ(cpicker, LV_OBJX_NAME);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
lv_style_list_t * style_dsc_p;
switch(part) {
case LV_CPICKER_PART_MAIN :
style_dsc_p = &cpicker->style_list;
break;
case LV_CPICKER_PART_INDIC:
style_dsc_p = &ext->indic.style_list;
break;
default:
style_dsc_p = NULL;
}
return style_dsc_p;
}
static bool lv_cpicker_hit(lv_obj_t * cpicker, const lv_point_t * p)
{
bool is_point_on_coords = lv_obj_is_point_on_coords(cpicker, p);
if(!is_point_on_coords)
return false;
lv_cpicker_ext_t * ext = (lv_cpicker_ext_t *)lv_obj_get_ext_attr(cpicker);
2020-02-13 06:56:14 +01:00
if(ext->type == LV_CPICKER_TYPE_RECT)
return true;
2020-02-11 06:41:03 +01:00
2020-02-13 06:56:14 +01:00
/*Valid clicks can be only in the circle*/
if(lv_area_is_point_on(&cpicker->coords, p, LV_RADIUS_CIRCLE)) return true;
else return false;
}
2019-10-03 03:11:38 -07:00
static void next_color_mode(lv_obj_t * cpicker)
{
2019-10-01 21:16:30 +02:00
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
ext->color_mode = (ext->color_mode + 1) % 3;
refr_indic_pos(cpicker);
lv_obj_invalidate(cpicker);
}
2019-10-01 21:16:30 +02:00
static void refr_indic_pos(lv_obj_t * cpicker)
{
2019-10-01 21:16:30 +02:00
invalidate_indic(cpicker);
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
lv_coord_t w = lv_obj_get_width(cpicker);
lv_coord_t h = lv_obj_get_height(cpicker);
2019-10-01 21:16:30 +02:00
if(ext->type == LV_CPICKER_TYPE_RECT) {
lv_coord_t ind_pos = 0;
switch(ext->color_mode) {
case LV_CPICKER_COLOR_MODE_HUE:
ind_pos += (ext->hsv.h * w) / 360;
break;
case LV_CPICKER_COLOR_MODE_SATURATION:
ind_pos += (ext->hsv.s * w) / 100;
break;
case LV_CPICKER_COLOR_MODE_VALUE:
ind_pos += (ext->hsv.v * w) / 100;
break;
}
2019-10-01 21:16:30 +02:00
ext->indic.pos.x = ind_pos;
ext->indic.pos.y = h / 2;
2019-10-03 03:11:38 -07:00
} else if(ext->type == LV_CPICKER_TYPE_DISC) {
2020-02-11 06:41:03 +01:00
lv_style_int_t scale_w = lv_obj_get_style_scale_width(cpicker, LV_CPICKER_PART_MAIN);
lv_coord_t r = (w - scale_w) / 2;
2019-10-01 21:16:30 +02:00
uint16_t angle = get_angle(cpicker);
ext->indic.pos.x = (((int32_t)r * lv_trigo_sin(angle)) >> LV_TRIGO_SHIFT);
ext->indic.pos.y = (((int32_t)r * lv_trigo_sin(angle + 90)) >> LV_TRIGO_SHIFT);
ext->indic.pos.x = ext->indic.pos.x + w / 2;
ext->indic.pos.y = ext->indic.pos.y + h / 2;
}
2019-10-01 21:16:30 +02:00
invalidate_indic(cpicker);
}
2019-10-01 21:16:30 +02:00
static lv_res_t double_click_reset(lv_obj_t * cpicker)
{
2019-10-01 21:16:30 +02:00
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
lv_indev_t * indev = lv_indev_get_act();
/*Double clicked? Use long press time as double click time out*/
if(lv_tick_elaps(ext->last_click_time) < indev->driver.long_press_time) {
lv_color_hsv_t hsv_cur;
hsv_cur = ext->hsv;
switch(ext->color_mode) {
case LV_CPICKER_COLOR_MODE_HUE:
hsv_cur.h = LV_CPICKER_DEF_HUE;
break;
case LV_CPICKER_COLOR_MODE_SATURATION:
hsv_cur.s = LV_CPICKER_DEF_SATURATION;
break;
case LV_CPICKER_COLOR_MODE_VALUE:
hsv_cur.v = LV_CPICKER_DEF_VALUE;
break;
}
lv_indev_wait_release(indev);
if (lv_cpicker_set_hsv(cpicker, hsv_cur)) {
lv_res_t res = lv_event_send(cpicker, LV_EVENT_VALUE_CHANGED, NULL);
if(res != LV_RES_OK) return res;
}
2019-10-01 21:16:30 +02:00
}
ext->last_click_time = lv_tick_get();
2019-10-01 21:16:30 +02:00
return LV_RES_OK;
}
2019-10-01 21:16:30 +02:00
static lv_color_t angle_to_mode_color(lv_obj_t * cpicker, uint16_t angle)
{
2019-10-01 21:16:30 +02:00
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
lv_color_t color;
switch(ext->color_mode)
{
2019-10-01 21:16:30 +02:00
default:
case LV_CPICKER_COLOR_MODE_HUE:
color = lv_color_hsv_to_rgb(angle % 360, ext->hsv.s, ext->hsv.v);
break;
2019-10-01 21:16:30 +02:00
case LV_CPICKER_COLOR_MODE_SATURATION:
color = lv_color_hsv_to_rgb(ext->hsv.h, ((angle % 360) * 100) / 360, ext->hsv.v);
break;
2019-10-01 21:16:30 +02:00
case LV_CPICKER_COLOR_MODE_VALUE:
color = lv_color_hsv_to_rgb(ext->hsv.h, ext->hsv.s, ((angle % 360) * 100) / 360);
break;
}
2019-10-01 21:16:30 +02:00
return color;
}
2019-10-01 21:16:30 +02:00
static uint16_t get_angle(lv_obj_t * cpicker)
{
2019-10-01 21:16:30 +02:00
lv_cpicker_ext_t * ext = lv_obj_get_ext_attr(cpicker);
uint16_t angle;
switch(ext->color_mode)
{
2019-10-01 21:16:30 +02:00
default:
case LV_CPICKER_COLOR_MODE_HUE:
angle = ext->hsv.h;
break;
2019-10-01 21:16:30 +02:00
case LV_CPICKER_COLOR_MODE_SATURATION:
angle = (ext->hsv.s * 360) / 100;
break;
2019-10-01 21:16:30 +02:00
case LV_CPICKER_COLOR_MODE_VALUE:
angle = (ext->hsv.v * 360) / 100 ;
break;
}
2019-10-01 21:16:30 +02:00
return angle;
}
#endif /* LV_USE_CPICKER != 0 */