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

477 lines
15 KiB
C
Raw Normal View History

2018-11-25 12:02:16 +01:00
/**
* @file lv_tileview.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_tileview.h"
#if LV_USE_TILEVIEW != 0
2018-11-25 12:02:16 +01:00
#include <stdbool.h>
#include "lv_cont.h"
2019-11-29 11:32:22 +08:00
#include "../lv_misc/lv_math.h"
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
#include "../lv_themes/lv_theme.h"
2018-11-25 12:02:16 +01:00
/*********************
* DEFINES
*********************/
#define LV_OBJX_NAME "lv_tileview"
#if LV_USE_ANIMATION
2020-02-26 19:48:27 +01:00
#ifndef LV_TILEVIEW_DEF_ANIM_TIME
#define LV_TILEVIEW_DEF_ANIM_TIME 300 /*Animation time loading a tile [ms] (0: no animation) */
#endif
2018-11-25 12:02:16 +01:00
#else
2020-02-26 19:48:27 +01:00
#undef LV_TILEVIEW_DEF_ANIM_TIME
#define LV_TILEVIEW_DEF_ANIM_TIME 0 /*No animations*/
2018-11-25 12:02:16 +01:00
#endif
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static lv_res_t lv_tileview_signal(lv_obj_t * tileview, lv_signal_t sign, void * param);
static lv_res_t lv_tileview_scrl_signal(lv_obj_t * scrl, lv_signal_t sign, void * param);
static void drag_end_handler(lv_obj_t * tileview);
static bool set_valid_drag_dirs(lv_obj_t * tileview);
/**********************
* STATIC VARIABLES
**********************/
static lv_signal_cb_t ancestor_signal;
static lv_signal_cb_t ancestor_scrl_signal;
static lv_design_cb_t ancestor_design;
2018-11-25 12:02:16 +01:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Create a tileview object
* @param par pointer to an object, it will be the parent of the new tileview
* @param copy pointer to a tileview object, if not NULL then the new object will be copied from it
* @return pointer to the created tileview
*/
lv_obj_t * lv_tileview_create(lv_obj_t * par, const lv_obj_t * copy)
{
LV_LOG_TRACE("tileview create started");
/*Create the ancestor of tileview*/
lv_obj_t * new_tileview = lv_page_create(par, copy);
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(new_tileview);
2018-11-25 12:02:16 +01:00
if(new_tileview == NULL) return NULL;
/*Allocate the tileview type specific extended data*/
lv_tileview_ext_t * ext = lv_obj_allocate_ext_attr(new_tileview, sizeof(lv_tileview_ext_t));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(ext);
if(ext == NULL) {
lv_obj_del(new_tileview);
return NULL;
}
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_tileview);
2019-06-06 06:05:40 +02:00
if(ancestor_scrl_signal == NULL) ancestor_scrl_signal = lv_obj_get_signal_cb(lv_page_get_scrl(new_tileview));
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(new_tileview);
2018-11-25 12:02:16 +01:00
/*Initialize the allocated 'ext' */
#if LV_USE_ANIMATION
ext->anim_time = LV_TILEVIEW_DEF_ANIM_TIME;
#endif
2019-06-27 07:16:15 +02:00
ext->act_id.x = 0;
ext->act_id.y = 0;
ext->valid_pos = NULL;
2019-06-20 14:28:25 +02:00
ext->valid_pos_cnt = 0;
2018-11-25 12:02:16 +01:00
/*The signal and design functions are not copied so set them here*/
lv_obj_set_signal_cb(new_tileview, lv_tileview_signal);
lv_obj_set_signal_cb(lv_page_get_scrl(new_tileview), lv_tileview_scrl_signal);
2018-11-25 12:02:16 +01:00
/*Init the new tileview*/
if(copy == NULL) {
2019-06-25 15:14:47 +02:00
/* Set a size which fits into the parent.
* Don't use `par` directly because if the tileview is created on a page it is moved to the
* scrollable so the parent has changed */
lv_coord_t w;
lv_coord_t h;
if(par) {
w = lv_obj_get_width_fit(lv_obj_get_parent(new_tileview));
h = lv_obj_get_height_fit(lv_obj_get_parent(new_tileview));
2020-02-26 19:48:27 +01:00
}
else {
w = lv_disp_get_hor_res(NULL);
h = lv_disp_get_ver_res(NULL);
}
lv_obj_set_size(new_tileview, w, h);
2020-02-09 23:08:25 +01:00
lv_obj_set_drag_throw(lv_page_get_scrl(new_tileview), true);
lv_obj_set_drag_dir(lv_page_get_scrl(new_tileview), LV_DRAG_DIR_ONE);
2020-02-05 10:45:10 +01:00
lv_page_set_scrl_fit(new_tileview, LV_FIT_TIGHT);
2020-02-14 22:04:00 +01:00
lv_obj_reset_style_list(new_tileview, LV_PAGE_PART_SCRL);
lv_theme_apply(new_tileview, LV_THEME_TILEVIEW);
2018-11-25 12:02:16 +01:00
}
/*Copy an existing tileview*/
else {
lv_tileview_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
2019-04-04 07:15:40 +02:00
ext->act_id.x = copy_ext->act_id.x;
ext->act_id.y = copy_ext->act_id.y;
2019-06-27 07:16:15 +02:00
ext->valid_pos = copy_ext->valid_pos;
ext->valid_pos_cnt = copy_ext->valid_pos_cnt;
#if LV_USE_ANIMATION
2019-06-06 06:05:40 +02:00
ext->anim_time = copy_ext->anim_time;
#endif
2018-11-25 12:02:16 +01:00
/*Refresh the style with new signal function*/
2020-03-10 10:41:39 +01:00
lv_obj_refresh_style(new_tileview, LV_STYLE_PROP_ALL);
2018-11-25 12:02:16 +01:00
}
LV_LOG_INFO("tileview created");
return new_tileview;
}
/*======================
* Add/remove functions
*=====================*/
/**
* Register an object on the tileview. The register object will able to slide the tileview
2019-03-15 05:45:27 +01:00
* @param tileview pointer to a Tileview object
2018-11-25 12:02:16 +01:00
* @param element pointer to an object
*/
2019-03-15 05:45:27 +01:00
void lv_tileview_add_element(lv_obj_t * tileview, lv_obj_t * element)
2018-11-25 12:02:16 +01:00
{
LV_ASSERT_OBJ(tileview, LV_OBJX_NAME);
LV_ASSERT_NULL(tileview);
2019-09-17 14:38:55 +02:00
lv_page_glue_obj(element, true);
2018-11-25 12:02:16 +01:00
}
/*=====================
* Setter functions
*====================*/
/**
* Set the valid position's indices. The scrolling will be possible only to these positions.
* @param tileview pointer to a Tileview object
* @param valid_pos array width the indices. E.g. `lv_point_t p[] = {{0,0}, {1,0}, {1,1}`.
* Only the pointer is saved so can't be a local variable.
2019-06-20 14:28:25 +02:00
* @param valid_pos_cnt numner of elements in `valid_pos` array
2018-11-25 12:02:16 +01:00
*/
void lv_tileview_set_valid_positions(lv_obj_t * tileview, const lv_point_t valid_pos[], uint16_t valid_pos_cnt)
2018-11-25 12:02:16 +01:00
{
LV_ASSERT_OBJ(tileview, LV_OBJX_NAME);
LV_ASSERT_NULL(valid_pos);
2018-11-25 12:02:16 +01:00
lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
2019-04-04 07:15:40 +02:00
ext->valid_pos = valid_pos;
2019-06-27 07:16:15 +02:00
ext->valid_pos_cnt = valid_pos_cnt;
2019-03-15 05:45:27 +01:00
set_valid_drag_dirs(tileview);
2019-03-15 05:45:27 +01:00
/*If valid pos. is selected do nothing*/
uint16_t i;
2019-06-20 14:28:25 +02:00
for(i = 0; i < valid_pos_cnt; i++) {
if(valid_pos[i].x == ext->act_id.x && valid_pos[i].y == ext->act_id.y) {
2019-03-15 05:45:27 +01:00
return;
}
}
/*Set a valid position if now an invalid is selected*/
2019-06-20 14:28:25 +02:00
if(valid_pos_cnt > 0) {
lv_tileview_set_tile_act(tileview, valid_pos[0].x, valid_pos[0].y, LV_ANIM_OFF);
2019-03-15 05:45:27 +01:00
}
2018-11-25 12:02:16 +01:00
}
/**
* Set the tile to be shown
* @param tileview pointer to a tileview object
* @param x column id (0, 1, 2...)
* @param y line id (0, 1, 2...)
2019-06-11 13:51:14 +02:00
* @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
2018-11-25 12:02:16 +01:00
*/
2019-06-11 13:51:14 +02:00
void lv_tileview_set_tile_act(lv_obj_t * tileview, lv_coord_t x, lv_coord_t y, lv_anim_enable_t anim)
2018-11-25 12:02:16 +01:00
{
LV_ASSERT_OBJ(tileview, LV_OBJX_NAME);
#if LV_USE_ANIMATION == 0
2019-06-11 13:51:14 +02:00
anim = LV_ANIM_OFF;
2018-11-25 12:02:16 +01:00
#endif
lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
2019-06-20 14:28:25 +02:00
uint32_t tile_id;
2018-11-25 12:02:16 +01:00
bool valid = false;
2019-06-20 14:28:25 +02:00
for(tile_id = 0; tile_id < ext->valid_pos_cnt; tile_id++) {
if(ext->valid_pos[tile_id].x == x && ext->valid_pos[tile_id].y == y) {
2018-11-25 12:02:16 +01:00
valid = true;
break;
2018-11-25 12:02:16 +01:00
}
}
2019-04-04 07:15:40 +02:00
if(valid == false) return; /*Don't load not valid tiles*/
2018-11-25 12:02:16 +01:00
ext->act_id.x = x;
ext->act_id.y = y;
lv_coord_t x_coord = -x * lv_obj_get_width(tileview);
lv_coord_t y_coord = -y * lv_obj_get_height(tileview);
2019-04-04 07:15:40 +02:00
lv_obj_t * scrl = lv_page_get_scrl(tileview);
2019-06-11 13:51:14 +02:00
if(anim) {
#if LV_USE_ANIMATION
2018-11-25 12:02:16 +01:00
lv_coord_t x_act = lv_obj_get_x(scrl);
lv_coord_t y_act = lv_obj_get_y(scrl);
2020-02-19 06:18:24 +01:00
2018-11-25 12:02:16 +01:00
lv_anim_t a;
2020-02-19 06:18:24 +01:00
lv_anim_init(&a);
lv_anim_set_var(&a, scrl);
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_x);
lv_anim_set_time(&a, ext->anim_time);
lv_anim_set_path_cb(&a, lv_anim_path_linear);
2018-11-25 12:02:16 +01:00
if(x_coord != x_act) {
2020-02-19 06:18:24 +01:00
lv_anim_set_values(&a, x_act, x_coord);
lv_anim_start(&a);
2018-11-25 12:02:16 +01:00
}
if(y_coord != y_act) {
2020-02-26 19:48:27 +01:00
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
2020-02-19 06:18:24 +01:00
lv_anim_set_values(&a, y_act, y_coord);
lv_anim_start(&a);
2018-11-25 12:02:16 +01:00
}
#endif
2020-02-26 19:48:27 +01:00
}
else {
lv_obj_set_pos(scrl, x_coord, y_coord);
2018-11-25 12:02:16 +01:00
}
2020-02-25 15:32:35 +01:00
lv_res_t res;
2019-06-20 14:28:25 +02:00
res = lv_event_send(tileview, LV_EVENT_VALUE_CHANGED, &tile_id);
2019-04-04 07:15:40 +02:00
if(res != LV_RES_OK) return; /*Prevent the tile loading*/
set_valid_drag_dirs(tileview);
2018-11-25 12:02:16 +01:00
}
/*=====================
* Getter functions
*====================*/
/*
* New object specific "get" functions come here
*/
2019-11-29 11:32:22 +08:00
/**
* Get the tile to be shown
* @param tileview pointer to a tileview object
* @param x column id (0, 1, 2...)
* @param y line id (0, 1, 2...)
*/
2020-02-26 19:48:27 +01:00
void lv_tileview_get_tile_act(lv_obj_t * tileview, lv_coord_t * x, lv_coord_t * y)
2019-11-29 11:32:22 +08:00
{
lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
*x = ext->act_id.x;
*y = ext->act_id.y;
}
2018-11-25 12:02:16 +01:00
/*=====================
* Other functions
*====================*/
/*
* New object specific "other" functions come here
*/
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Signal function of the tileview
* @param tileview pointer to a tileview 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_tileview_signal(lv_obj_t * tileview, lv_signal_t sign, void * param)
{
lv_res_t res;
/* Include the ancient signal function */
res = ancestor_signal(tileview, sign, param);
if(res != LV_RES_OK) return res;
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
2018-11-25 12:02:16 +01:00
if(sign == LV_SIGNAL_CLEANUP) {
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
}
return res;
}
/**
* Signal function of the tileview scrollable
* @param tileview pointer to the scrollable part of the tileview 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_tileview_scrl_signal(lv_obj_t * scrl, lv_signal_t sign, void * param)
{
lv_res_t res;
/* Include the ancient signal function */
res = ancestor_scrl_signal(scrl, sign, param);
if(res != LV_RES_OK) return res;
2019-12-06 13:43:38 +01:00
if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, "");
2018-11-25 12:02:16 +01:00
2019-04-11 19:59:55 +08:00
lv_obj_t * tileview = lv_obj_get_parent(scrl);
2019-09-17 14:38:55 +02:00
if(sign == LV_SIGNAL_DRAG_BEGIN) {
set_valid_drag_dirs(tileview);
}
2019-09-19 14:58:07 +02:00
else if(sign == LV_SIGNAL_DRAG_THROW_BEGIN) {
2019-09-17 14:38:55 +02:00
drag_end_handler(tileview);
2019-09-19 14:58:07 +02:00
2019-11-04 16:56:57 +01:00
res = lv_indev_finish_drag(lv_indev_get_act());
2019-09-19 14:58:07 +02:00
if(res != LV_RES_OK) return res;
2019-09-17 14:38:55 +02:00
}
2018-11-25 12:02:16 +01:00
/*Apply constraint on moving of the tileview*/
else if(sign == LV_SIGNAL_COORD_CHG) {
2018-11-25 12:02:16 +01:00
lv_indev_t * indev = lv_indev_get_act();
if(indev) {
lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
lv_coord_t x = lv_obj_get_x(scrl);
lv_coord_t y = lv_obj_get_y(scrl);
lv_coord_t h = lv_obj_get_height(tileview);
lv_coord_t w = lv_obj_get_width(tileview);
lv_coord_t top = lv_obj_get_style_pad_top(tileview, LV_TILEVIEW_PART_BG);
lv_coord_t left = lv_obj_get_style_pad_left(tileview, LV_TILEVIEW_PART_BG);
if(!ext->drag_top_en && y > -(ext->act_id.y * h) && indev->proc.types.pointer.vect.y > 0) {
lv_page_start_edge_flash(tileview, LV_PAGE_EDGE_TOP);
lv_obj_set_y(scrl, -ext->act_id.y * h + top);
2018-11-25 12:02:16 +01:00
}
if(!ext->drag_bottom_en && indev->proc.types.pointer.vect.y < 0 && y < -(ext->act_id.y * h)) {
lv_page_start_edge_flash(tileview, LV_PAGE_EDGE_BOTTOM);
lv_obj_set_y(scrl, -ext->act_id.y * h + top);
2018-11-25 12:02:16 +01:00
}
if(!ext->drag_left_en && x > -(ext->act_id.x * w) && indev->proc.types.pointer.vect.x > 0) {
lv_page_start_edge_flash(tileview, LV_PAGE_EDGE_LEFT);
lv_obj_set_x(scrl, -ext->act_id.x * w + left);
2018-11-25 12:02:16 +01:00
}
if(!ext->drag_right_en && indev->proc.types.pointer.vect.x < 0 && x < -(ext->act_id.x * w)) {
lv_page_start_edge_flash(tileview, LV_PAGE_EDGE_RIGHT);
lv_obj_set_x(scrl, -ext->act_id.x * w + top);
2018-11-25 12:02:16 +01:00
}
/*Apply the drag constraints*/
lv_drag_dir_t drag_dir = indev->proc.types.pointer.drag_dir;
2019-11-29 15:44:15 +01:00
if(drag_dir == LV_DRAG_DIR_HOR)
lv_obj_set_y(scrl, -ext->act_id.y * lv_obj_get_height(tileview) + top);
2019-11-29 11:32:22 +08:00
else if(drag_dir == LV_DRAG_DIR_VER)
lv_obj_set_x(scrl, -ext->act_id.x * lv_obj_get_width(tileview) + left);
2018-11-25 12:02:16 +01:00
}
}
return res;
}
/**
* Called when the user releases an element of the tileview after dragging it.
* @param tileview pointer to a tileview object
*/
static void drag_end_handler(lv_obj_t * tileview)
{
lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
2019-04-04 07:15:40 +02:00
lv_indev_t * indev = lv_indev_get_act();
2018-11-25 12:02:16 +01:00
lv_point_t point_act;
lv_indev_get_point(indev, &point_act);
lv_obj_t * scrl = lv_page_get_scrl(tileview);
lv_point_t p;
2019-04-04 07:15:40 +02:00
p.x = -(scrl->coords.x1 - lv_obj_get_width(tileview) / 2);
p.y = -(scrl->coords.y1 - lv_obj_get_height(tileview) / 2);
2018-11-25 12:02:16 +01:00
lv_drag_dir_t drag_dir = indev->proc.types.pointer.drag_dir;
2018-11-25 12:02:16 +01:00
/*From the drag vector (drag throw) predict the end position*/
if(drag_dir & LV_DRAG_DIR_HOR) {
2018-11-25 12:02:16 +01:00
lv_point_t vect;
lv_indev_get_vect(indev, &vect);
lv_coord_t predict = 0;
while(vect.x != 0) {
predict += vect.x;
vect.x = vect.x * (100 - LV_INDEV_DEF_DRAG_THROW) / 100;
2018-11-25 12:02:16 +01:00
}
p.x -= predict;
2019-09-19 14:58:07 +02:00
2020-02-26 19:48:27 +01:00
}
else if(drag_dir & LV_DRAG_DIR_VER) {
2018-11-25 12:02:16 +01:00
lv_point_t vect;
lv_indev_get_vect(indev, &vect);
lv_coord_t predict = 0;
while(vect.y != 0) {
predict += vect.y;
vect.y = vect.y * (100 - LV_INDEV_DEF_DRAG_THROW) / 100;
2018-11-25 12:02:16 +01:00
}
p.y -= predict;
}
/*Get the index of the tile*/
p.x = p.x / lv_obj_get_width(tileview);
p.y = p.y / lv_obj_get_height(tileview);
/*Max +- move*/
lv_coord_t x_move = p.x - ext->act_id.x;
lv_coord_t y_move = p.y - ext->act_id.y;
if(x_move < -1) x_move = -1;
2019-04-04 07:15:40 +02:00
if(x_move > 1) x_move = 1;
2018-11-25 12:02:16 +01:00
if(y_move < -1) y_move = -1;
if(y_move > 1) y_move = 1;
/*Set the new tile*/
2019-04-04 07:15:40 +02:00
lv_tileview_set_tile_act(tileview, ext->act_id.x + x_move, ext->act_id.y + y_move, true);
2018-11-25 12:02:16 +01:00
}
static bool set_valid_drag_dirs(lv_obj_t * tileview)
{
lv_tileview_ext_t * ext = lv_obj_get_ext_attr(tileview);
if(ext->valid_pos == NULL) return false;
ext->drag_bottom_en = 0;
2019-04-04 07:15:40 +02:00
ext->drag_top_en = 0;
ext->drag_left_en = 0;
ext->drag_right_en = 0;
2018-11-25 12:02:16 +01:00
uint16_t i;
2019-06-20 14:28:25 +02:00
for(i = 0; i < ext->valid_pos_cnt; i++) {
2019-06-06 06:05:40 +02:00
if(ext->valid_pos[i].x == ext->act_id.x && ext->valid_pos[i].y == ext->act_id.y - 1) ext->drag_top_en = 1;
if(ext->valid_pos[i].x == ext->act_id.x && ext->valid_pos[i].y == ext->act_id.y + 1) ext->drag_bottom_en = 1;
if(ext->valid_pos[i].x == ext->act_id.x - 1 && ext->valid_pos[i].y == ext->act_id.y) ext->drag_left_en = 1;
if(ext->valid_pos[i].x == ext->act_id.x + 1 && ext->valid_pos[i].y == ext->act_id.y) ext->drag_right_en = 1;
2018-11-25 12:02:16 +01:00
}
return true;
}
#endif