diff --git a/CHANGELOG.md b/CHANGELOG.md index c654be0e4..7a110f1b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Available in the `dev` branch - Allow setting different font for the selected text in `lv_roller` - Add `theme->apply_cb` to replace `theme->apply_xcb` to make it compatible with the MicroPython binding - Add `lv_theme_set_base()` to allow easy extension of built-in (or any) themes. +- Add `lv_obj_align_x()` and `lv_obj_align_y()` functions ## v7.1.0 (planned on 07.07.2020) *Available in the `master` branch* diff --git a/src/lv_core/lv_obj.c b/src/lv_core/lv_obj.c index dd7df33ca..55cef5424 100644 --- a/src/lv_core/lv_obj.c +++ b/src/lv_core/lv_obj.c @@ -85,6 +85,7 @@ static void refresh_children_position(lv_obj_t * obj, lv_coord_t x_diff, lv_coor static void report_style_mod_core(void * style_p, lv_obj_t * obj); static void refresh_children_style(lv_obj_t * obj); static void base_dir_refr_children(lv_obj_t * obj); +static void obj_align_core(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, bool x_set, bool y_set, lv_coord_t x_ofs, lv_coord_t y_ofs); #if LV_USE_ANIMATION static lv_style_trans_t * trans_create(lv_obj_t * obj, lv_style_property_t prop, uint8_t part, lv_state_t prev_state, lv_state_t new_state); @@ -883,7 +884,6 @@ void lv_obj_set_height_margin(lv_obj_t * obj, lv_coord_t h) lv_obj_set_height(obj, h - mtop - mbottom); } - /** * Align an object to an other object. * @param obj pointer to an object to align @@ -900,19 +900,7 @@ void lv_obj_align(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, lv_co LV_ASSERT_OBJ(base, LV_OBJX_NAME); - lv_point_t new_pos; - _lv_area_align(&base->coords, &obj->coords, align, &new_pos); - - /*Bring together the coordination system of base and obj*/ - lv_obj_t * par = lv_obj_get_parent(obj); - lv_coord_t par_abs_x = par->coords.x1; - lv_coord_t par_abs_y = par->coords.y1; - new_pos.x += x_ofs; - new_pos.y += y_ofs; - new_pos.x -= par_abs_x; - new_pos.y -= par_abs_y; - - lv_obj_set_pos(obj, new_pos.x, new_pos.y); + obj_align_core(obj, base, align, true, true, x_ofs, y_ofs); #if LV_USE_OBJ_REALIGN /*Save the last align parameters to use them in `lv_obj_realign`*/ @@ -924,6 +912,42 @@ void lv_obj_align(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, lv_co #endif } +/** + * Align an object to an other object horizontally. + * @param obj pointer to an object to align + * @param base pointer to an object (if NULL the parent is used). 'obj' will be aligned to it. + * @param align type of alignment (see 'lv_align_t' enum) + * @param x_ofs x coordinate offset after alignment + */ +void lv_obj_align_x(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, lv_coord_t x_ofs) +{ + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); + + if(base == NULL) base = lv_obj_get_parent(obj); + + LV_ASSERT_OBJ(base, LV_OBJX_NAME); + + obj_align_core(obj, base, align, true, false, x_ofs, 0); +} + +/** + * Align an object to an other object vertically. + * @param obj pointer to an object to align + * @param base pointer to an object (if NULL the parent is used). 'obj' will be aligned to it. + * @param align type of alignment (see 'lv_align_t' enum) + * @param y_ofs y coordinate offset after alignment + */ +void lv_obj_align_y(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, lv_coord_t y_ofs) +{ + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); + + if(base == NULL) base = lv_obj_get_parent(obj); + + LV_ASSERT_OBJ(base, LV_OBJX_NAME); + + obj_align_core(obj, base, align, true, false, 0, y_ofs); +} + /** * Align an object's middle point to an other object. * @param obj pointer to an object to align @@ -3871,6 +3895,27 @@ static void base_dir_refr_children(lv_obj_t * obj) } } +static void obj_align_core(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, bool x_set, bool y_set, lv_coord_t x_ofs, lv_coord_t y_ofs) +{ + lv_point_t new_pos; + _lv_area_align(&base->coords, &obj->coords, align, &new_pos); + + /*Bring together the coordination system of base and obj*/ + lv_obj_t * par = lv_obj_get_parent(obj); + lv_coord_t par_abs_x = par->coords.x1; + lv_coord_t par_abs_y = par->coords.y1; + new_pos.x += x_ofs; + new_pos.y += y_ofs; + new_pos.x -= par_abs_x; + new_pos.y -= par_abs_y; + + if(x_set && y_set) lv_obj_set_pos(obj, new_pos.x, new_pos.y); + else if(x_set) lv_obj_set_x(obj, new_pos.x); + else if(y_set) lv_obj_set_y(obj, new_pos.y); +} + + + #if LV_USE_ANIMATION /** @@ -4147,3 +4192,4 @@ static bool obj_valid_child(const lv_obj_t * parent, const lv_obj_t * obj_to_fin return false; } + diff --git a/src/lv_core/lv_obj.h b/src/lv_core/lv_obj.h index a4419b683..c470e7af5 100644 --- a/src/lv_core/lv_obj.h +++ b/src/lv_core/lv_obj.h @@ -468,6 +468,24 @@ void lv_obj_set_height_margin(lv_obj_t * obj, lv_coord_t h); */ void lv_obj_align(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, lv_coord_t x_ofs, lv_coord_t y_ofs); +/** + * Align an object to an other object horizontally. + * @param obj pointer to an object to align + * @param base pointer to an object (if NULL the parent is used). 'obj' will be aligned to it. + * @param align type of alignment (see 'lv_align_t' enum) + * @param x_ofs x coordinate offset after alignment + */ +void lv_obj_align_x(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, lv_coord_t x_ofs); + +/** + * Align an object to an other object vertically. + * @param obj pointer to an object to align + * @param base pointer to an object (if NULL the parent is used). 'obj' will be aligned to it. + * @param align type of alignment (see 'lv_align_t' enum) + * @param y_ofs y coordinate offset after alignment + */ +void lv_obj_align_y(lv_obj_t * obj, const lv_obj_t * base, lv_align_t align, lv_coord_t y_ofs); + /** * Align an object to an other object. * @param obj pointer to an object to align diff --git a/src/lv_widgets/lv_chart.h b/src/lv_widgets/lv_chart.h index 06bbe84a2..c7f68284c 100644 --- a/src/lv_widgets/lv_chart.h +++ b/src/lv_widgets/lv_chart.h @@ -42,6 +42,7 @@ enum { LV_CHART_TYPE_NONE = 0x00, /**< Don't draw the series*/ LV_CHART_TYPE_LINE = 0x01, /**< Connect the points with lines*/ LV_CHART_TYPE_COLUMN = 0x02, /**< Draw columns*/ + LV_CHART_TYPE_SCATTER = 0x03, /**< X/Y chart, points and/or lines*/ }; typedef uint8_t lv_chart_type_t;