Merge pull request #1003 from littlevgl/obj_drag_dir
Enhance drag logic
This commit is contained in:
@@ -1038,6 +1038,8 @@ static void indev_drag(lv_indev_proc_t * state)
|
||||
|
||||
if(lv_obj_get_drag(drag_obj) == false) return;
|
||||
|
||||
lv_drag_dir_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;
|
||||
@@ -1045,8 +1047,10 @@ 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;
|
||||
}
|
||||
}
|
||||
@@ -1056,9 +1060,6 @@ static void indev_drag(lv_indev_proc_t * state)
|
||||
/*Set new position if the vector is not zero*/
|
||||
if(state->types.pointer.vect.x != 0 || state->types.pointer.vect.y != 0) {
|
||||
|
||||
/*Get the coordinates of the object and modify them*/
|
||||
lv_coord_t act_x = lv_obj_get_x(drag_obj);
|
||||
lv_coord_t act_y = lv_obj_get_y(drag_obj);
|
||||
uint16_t inv_buf_size = lv_disp_get_inv_buf_size(
|
||||
indev_act->driver.disp); /*Get the number of currently invalidated areas*/
|
||||
|
||||
@@ -1067,20 +1068,28 @@ 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);
|
||||
/*Get the coordinates of the object and modify them*/
|
||||
lv_coord_t act_x = lv_obj_get_x(drag_obj);
|
||||
lv_coord_t act_y = lv_obj_get_y(drag_obj);
|
||||
|
||||
/*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) {
|
||||
if(state->types.pointer.drag_in_prog !=
|
||||
0) { /*Send the drag begin signal on first move*/
|
||||
drag_obj->signal_cb(drag_obj, LV_SIGNAL_DRAG_BEGIN, indev_act);
|
||||
if(state->reset_query != 0) return;
|
||||
}
|
||||
state->types.pointer.drag_in_prog = 1;
|
||||
if(allowed_dirs == LV_DRAG_DIR_ALL)
|
||||
lv_obj_set_pos(drag_obj, act_x + state->types.pointer.vect.x, act_y + state->types.pointer.vect.y);
|
||||
else if(allowed_dirs & LV_DRAG_DIR_HOR)
|
||||
lv_obj_set_x(drag_obj, act_x + state->types.pointer.vect.x);
|
||||
else 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*/
|
||||
/*Send the drag begin signal on first move*/
|
||||
if(state->types.pointer.drag_in_prog == 0) {
|
||||
drag_obj->signal_cb(drag_obj, LV_SIGNAL_DRAG_BEGIN, indev_act);
|
||||
if(state->reset_query != 0) return;
|
||||
}
|
||||
|
||||
state->types.pointer.drag_in_prog = 1;
|
||||
|
||||
/*If the object didn't moved then clear the invalidated areas*/
|
||||
else {
|
||||
if(drag_obj->coords.x1 == prev_x && drag_obj->coords.y1 == prev_y) {
|
||||
/*In a special case if the object is moved on a page and
|
||||
* the scrollable has fit == true and the object is dragged of the page then
|
||||
* while its coordinate is not changing only the parent's size is reduced */
|
||||
@@ -1092,6 +1101,7 @@ static void indev_drag(lv_indev_proc_t * state)
|
||||
new_inv_buf_size - inv_buf_size);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1123,6 +1133,8 @@ static void indev_drag_throw(lv_indev_proc_t * proc)
|
||||
return;
|
||||
}
|
||||
|
||||
lv_drag_dir_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;
|
||||
@@ -1135,7 +1147,13 @@ 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_ALL)
|
||||
lv_obj_set_pos(drag_obj, act_x, act_y);
|
||||
else if(allowed_dirs & LV_DRAG_DIR_HOR)
|
||||
lv_obj_set_x(drag_obj, act_x);
|
||||
else 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);
|
||||
|
||||
@@ -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 = LV_DRAG_DIR_ALL;
|
||||
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;
|
||||
@@ -1096,6 +1098,19 @@ void lv_obj_set_drag(lv_obj_t * obj, bool en)
|
||||
obj->drag = (en == true ? 1 : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the directions an object can be dragged in
|
||||
* @param obj pointer to an object
|
||||
* @param drag_dir bitwise OR of allowed directions an object can be dragged in
|
||||
*/
|
||||
void lv_obj_set_drag_dir(lv_obj_t * obj, lv_drag_dir_t drag_dir)
|
||||
{
|
||||
obj->drag_dir = drag_dir;
|
||||
|
||||
if(obj->drag_dir != 0)
|
||||
lv_obj_set_drag(obj, true); /*Drag direction requires drag*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the throwing of an object after is is dragged
|
||||
* @param obj pointer to an object
|
||||
@@ -1692,6 +1707,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 bitwise OR of allowed directions an object can be dragged in
|
||||
*/
|
||||
lv_drag_dir_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
|
||||
|
||||
@@ -169,6 +169,14 @@ typedef struct
|
||||
} lv_reailgn_t;
|
||||
#endif
|
||||
|
||||
enum {
|
||||
LV_DRAG_DIR_HOR = 0x1,
|
||||
LV_DRAG_DIR_VER = 0x2,
|
||||
LV_DRAG_DIR_ALL = 0x3, /* Should be the bitwise OR of the above */
|
||||
};
|
||||
|
||||
typedef uint8_t lv_drag_dir_t;
|
||||
|
||||
typedef struct _lv_obj_t
|
||||
{
|
||||
struct _lv_obj_t * par; /*Pointer to the parent object*/
|
||||
@@ -189,6 +197,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_dir_t drag_dir : 2; /* 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 +455,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 drag_dir bitwise OR of allowed drag directions
|
||||
*/
|
||||
void lv_obj_set_drag_dir(lv_obj_t * obj, lv_drag_dir_t drag_dir);
|
||||
|
||||
/**
|
||||
* Enable the throwing of an object after is is dragged
|
||||
* @param obj pointer to an object
|
||||
@@ -736,6 +752,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 bitwise OR of allowed directions an object can be dragged in
|
||||
*/
|
||||
lv_drag_dir_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
|
||||
|
||||
Reference in New Issue
Block a user