Add lv_obj_set_drag_dir and lv_obj_get_drag_dir

This commit is contained in:
Themba Dube
2019-04-04 21:51:13 -04:00
parent d85c138137
commit 789e7a3a12
3 changed files with 67 additions and 5 deletions

View File

@@ -1030,6 +1030,8 @@ static void indev_drag(lv_indev_proc_t * state)
if(lv_obj_get_drag(drag_obj) == false) return;
lv_drag_direction_t allowed_dirs = lv_obj_get_drag_dir(drag_obj);
/*Count the movement by drag*/
state->types.pointer.drag_sum.x += state->types.pointer.vect.x;
state->types.pointer.drag_sum.y += state->types.pointer.vect.y;
@@ -1037,8 +1039,8 @@ static void indev_drag(lv_indev_proc_t * state)
/*Enough move?*/
if(state->types.pointer.drag_limit_out == 0) {
/*If a move is greater then LV_DRAG_LIMIT then begin the drag*/
if(LV_MATH_ABS(state->types.pointer.drag_sum.x) >= indev_act->driver.drag_limit ||
LV_MATH_ABS(state->types.pointer.drag_sum.y) >= indev_act->driver.drag_limit) {
if(((allowed_dirs & LV_DRAG_DIR_HOR) && LV_MATH_ABS(state->types.pointer.drag_sum.x) >= indev_act->driver.drag_limit) ||
((allowed_dirs & LV_DRAG_DIR_VER) && LV_MATH_ABS(state->types.pointer.drag_sum.y) >= indev_act->driver.drag_limit)) {
state->types.pointer.drag_limit_out = 1;
}
}
@@ -1059,8 +1061,11 @@ static void indev_drag(lv_indev_proc_t * state)
lv_coord_t prev_par_w = lv_obj_get_width(lv_obj_get_parent(drag_obj));
lv_coord_t prev_par_h = lv_obj_get_height(lv_obj_get_parent(drag_obj));
lv_obj_set_pos(drag_obj, act_x + state->types.pointer.vect.x,
act_y + state->types.pointer.vect.y);
if(allowed_dirs & LV_DRAG_DIR_HOR)
lv_obj_set_x(drag_obj, act_x + state->types.pointer.vect.x);
if(allowed_dirs & LV_DRAG_DIR_VER)
lv_obj_set_y(drag_obj, act_y + state->types.pointer.vect.y);
/*Set the drag in progress flag if the object is really moved*/
if(drag_obj->coords.x1 != prev_x || drag_obj->coords.y1 != prev_y) {
@@ -1115,6 +1120,8 @@ static void indev_drag_throw(lv_indev_proc_t * proc)
return;
}
lv_drag_direction_t allowed_dirs = lv_obj_get_drag_dir(drag_obj);
/*Reduce the vectors*/
proc->types.pointer.drag_throw_vect.x =
proc->types.pointer.drag_throw_vect.x * (100 - indev_act->driver.drag_throw) / 100;
@@ -1127,7 +1134,12 @@ static void indev_drag_throw(lv_indev_proc_t * proc)
lv_obj_get_coords(drag_obj, &coords_ori);
lv_coord_t act_x = lv_obj_get_x(drag_obj) + proc->types.pointer.drag_throw_vect.x;
lv_coord_t act_y = lv_obj_get_y(drag_obj) + proc->types.pointer.drag_throw_vect.y;
lv_obj_set_pos(drag_obj, act_x, act_y);
if(allowed_dirs & LV_DRAG_DIR_HOR)
lv_obj_set_x(drag_obj, act_x );
if(allowed_dirs & LV_DRAG_DIR_VER)
lv_obj_set_y(drag_obj, act_y);
lv_area_t coord_new;
lv_obj_get_coords(drag_obj, &coord_new);

View File

@@ -253,6 +253,7 @@ lv_obj_t * lv_obj_create(lv_obj_t * parent, const lv_obj_t * copy)
/*Set attributes*/
new_obj->click = 1;
new_obj->drag = 0;
new_obj->drag_dir = 0;
new_obj->drag_throw = 0;
new_obj->drag_parent = 0;
new_obj->hidden = 0;
@@ -296,6 +297,7 @@ lv_obj_t * lv_obj_create(lv_obj_t * parent, const lv_obj_t * copy)
/*Copy attributes*/
new_obj->click = copy->click;
new_obj->drag = copy->drag;
new_obj->drag_dir = copy->drag_dir;
new_obj->drag_throw = copy->drag_throw;
new_obj->drag_parent = copy->drag_parent;
new_obj->hidden = copy->hidden;
@@ -1094,6 +1096,22 @@ void lv_obj_set_drag(lv_obj_t * obj, bool en)
{
if(en == true) lv_obj_set_click(obj, true); /*Drag is useless without enabled clicking*/
obj->drag = (en == true ? 1 : 0);
if(en && obj->drag_dir == 0) {
obj->drag_dir = LV_DRAG_DIR_HOR | LV_DRAG_DIR_VER;
}
}
/**
* Set the directions an object can be dragged in
* @param obj pointer to an object
* @param en true: make the object dragable
*/
void lv_obj_set_drag_dir(lv_obj_t * obj, lv_drag_direction_t drag_dir)
{
obj->drag_dir = drag_dir;
if(obj->drag_dir != 0)
lv_obj_set_drag(obj, true); /*Drag direction requires drag*/
}
/**
@@ -1692,6 +1710,16 @@ bool lv_obj_get_drag(const lv_obj_t * obj)
return obj->drag == 0 ? false : true;
}
/**
* Get the directions an object can be dragged
* @param obj pointer to an object
* @return directions an object can be dragged
*/
lv_drag_direction_t lv_obj_get_drag_dir(const lv_obj_t * obj)
{
return obj->drag_dir;
}
/**
* Get the drag throw enable attribute of an object
* @param obj pointer to an object

View File

@@ -169,6 +169,13 @@ typedef struct
} lv_reailgn_t;
#endif
enum {
LV_DRAG_DIR_HOR = 0x1,
LV_DRAG_DIR_VER = 0x2
};
typedef uint8_t lv_drag_direction_t;
typedef struct _lv_obj_t
{
struct _lv_obj_t * par; /*Pointer to the parent object*/
@@ -189,6 +196,7 @@ typedef struct _lv_obj_t
/*Attributes and states*/
uint8_t click : 1; /*1: Can be pressed by an input device*/
uint8_t drag : 1; /*1: Enable the dragging*/
lv_drag_direction_t drag_dir; /* Which directions the object can be dragged in */
uint8_t drag_throw : 1; /*1: Enable throwing with drag*/
uint8_t drag_parent : 1; /*1: Parent will be dragged instead*/
uint8_t hidden : 1; /*1: Object is hidden*/
@@ -446,6 +454,13 @@ void lv_obj_set_top(lv_obj_t * obj, bool en);
*/
void lv_obj_set_drag(lv_obj_t * obj, bool en);
/**
* Set the directions an object can be dragged in
* @param obj pointer to an object
* @param en true: make the object dragable
*/
void lv_obj_set_drag_dir(lv_obj_t * obj, lv_drag_direction_t drag_dir);
/**
* Enable the throwing of an object after is is dragged
* @param obj pointer to an object
@@ -736,6 +751,13 @@ bool lv_obj_get_top(const lv_obj_t * obj);
*/
bool lv_obj_get_drag(const lv_obj_t * obj);
/**
* Get the directions an object can be dragged
* @param obj pointer to an object
* @return directions an object can be dragged
*/
lv_drag_direction_t lv_obj_get_drag_dir(const lv_obj_t * obj);
/**
* Get the drag throw enable attribute of an object
* @param obj pointer to an object