/** * Focus on an object. It ensures that the object will be visible on the page. * @param page pointer to a page object * @param obj pointer to an object to focus (must be on the page) * @param anim_time scroll animation time in milliseconds (0: no animation) */ void lv_page_focus(lv_obj_t * page, lv_obj_t * obj, uint16_t anim_time) { lv_page_ext_t * ext = lv_obj_get_ext_attr(page); lv_style_t * style = lv_page_get_style(page, LV_PAGE_STYLE_BG); lv_style_t * style_scrl = lv_page_get_style(page, LV_PAGE_STYLE_SCRL); lv_coord_t obj_y = obj->coords.y1 - ext->scrl->coords.y1; lv_coord_t obj_h = lv_obj_get_height(obj); lv_coord_t scrlable_y = lv_obj_get_y(ext->scrl); lv_coord_t page_h = lv_obj_get_height(page); lv_coord_t top_err = -(scrlable_y + obj_y); lv_coord_t bot_err = scrlable_y + obj_y + obj_h - page_h; /*If obj is higher then the page focus where the "error" is smaller*/ /*Out of the page on the top*/ if((obj_h <= page_h && top_err > 0) || (obj_h > page_h && top_err < bot_err)) { /*Calculate a new position and let some space above*/ scrlable_y = -(obj_y - style_scrl->body.padding.ver - style->body.padding.ver); scrlable_y += style_scrl->body.padding.ver; } /*Out of the page on the bottom*/ else if((obj_h <= page_h && bot_err > 0) || (obj_h > page_h && top_err >= bot_err)) { /*Calculate a new position and let some space below*/ scrlable_y = -obj_y; scrlable_y += page_h - obj_h; scrlable_y -= style_scrl->body.padding.ver; } else { /*Already in focus*/ return; } if(anim_time == 0) { lv_obj_set_y(ext->scrl, scrlable_y); } else { #if USE_LV_ANIMATION lv_anim_t a; a.act_time = 0; a.start = lv_obj_get_y(ext->scrl); a.end = scrlable_y; a.time = anim_time; a.end_cb = NULL; a.playback = 0; a.repeat = 0; a.var = ext->scrl; a.path = lv_anim_path_linear; a.fp = (lv_anim_fp_t) lv_obj_set_y; lv_anim_create(&a); #endif } }
/** * Handle the drawing related tasks of the rollers * @param roller 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 * @param return true/false, depends on 'mode' */ static bool lv_roller_design(lv_obj_t * roller, const lv_area_t * mask, lv_design_mode_t mode) { /*Return false if the object is not covers the mask_p area*/ if(mode == LV_DESIGN_COVER_CHK) { return false; } /*Draw the object*/ else if(mode == LV_DESIGN_DRAW_MAIN) { draw_bg(roller, mask); lv_style_t *style = lv_roller_get_style(roller, LV_ROLLER_STYLE_BG); const lv_font_t * font = style->text.font; lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller); lv_coord_t font_h = lv_font_get_height_scale(font); lv_area_t rect_area; rect_area.y1 = roller->coords.y1 + lv_obj_get_height(roller) / 2 - font_h / 2 - style->text.line_space / 2; rect_area.y2 = rect_area.y1 + font_h + style->text.line_space; rect_area.x1 = roller->coords.x1; rect_area.x2 = roller->coords.x2; lv_draw_rect(&rect_area, mask, ext->ddlist.sel_style); } /*Post draw when the children are drawn*/ else if(mode == LV_DESIGN_DRAW_POST) { lv_style_t *style = lv_roller_get_style(roller, LV_ROLLER_STYLE_BG); lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller); const lv_font_t * font = style->text.font; lv_coord_t font_h = lv_font_get_height_scale(font); /*Redraw the text on the selected area with a different color*/ lv_area_t rect_area; rect_area.y1 = roller->coords.y1 + lv_obj_get_height(roller) / 2 - font_h / 2 - style->text.line_space / 2; rect_area.y2 = rect_area.y1 + font_h + style->text.line_space; rect_area.x1 = roller->coords.x1; rect_area.x2 = roller->coords.x2; lv_area_t mask_sel; bool area_ok; area_ok = lv_area_union(&mask_sel, mask, &rect_area); if(area_ok) { lv_style_t *sel_style = lv_roller_get_style(roller, LV_ROLLER_STYLE_SEL); lv_style_t new_style; lv_style_copy(&new_style, style); new_style.text.color = sel_style->text.color; new_style.text.opa = sel_style->text.opa; lv_draw_label(&ext->ddlist.label->coords, &mask_sel, &new_style, lv_label_get_text(ext->ddlist.label), LV_TXT_FLAG_CENTER, NULL); } } return true; }
static void list_create(lv_obj_t *parent) { lv_page_set_style(parent, LV_PAGE_STYLE_BG, &lv_style_transp_fit); lv_page_set_style(parent, LV_PAGE_STYLE_SCRL, &lv_style_transp_fit); lv_page_set_scrl_fit(parent, false, false); lv_page_set_scrl_height(parent, lv_obj_get_height(parent)); lv_page_set_sb_mode(parent, LV_SB_MODE_OFF); /*Create styles for the buttons*/ static lv_style_t style_btn_rel; static lv_style_t style_btn_pr; lv_style_copy(&style_btn_rel, &lv_style_btn_rel); style_btn_rel.body.main_color = LV_COLOR_HEX3(0x333); style_btn_rel.body.grad_color = LV_COLOR_BLACK; style_btn_rel.body.border.color = LV_COLOR_SILVER; style_btn_rel.body.border.width = 1; style_btn_rel.body.border.opa = LV_OPA_50; style_btn_rel.body.radius = 0; lv_style_copy(&style_btn_pr, &style_btn_rel); style_btn_pr.body.main_color = LV_COLOR_MAKE(0x55, 0x96, 0xd8); style_btn_pr.body.grad_color = LV_COLOR_MAKE(0x37, 0x62, 0x90); style_btn_pr.text.color = LV_COLOR_MAKE(0xbb, 0xd5, 0xf1); lv_obj_t *list = lv_list_create(parent, NULL); lv_obj_set_height(list, 2 * lv_obj_get_height(parent) / 3); lv_list_set_style(list, LV_LIST_STYLE_BG, &lv_style_transp_tight); lv_list_set_style(list, LV_LIST_STYLE_SCRL, &lv_style_transp_tight); lv_list_set_style(list, LV_LIST_STYLE_BTN_REL, &style_btn_rel); lv_list_set_style(list, LV_LIST_STYLE_BTN_PR, &style_btn_pr); lv_obj_align(list, NULL, LV_ALIGN_IN_TOP_MID, 0, LV_DPI / 4); lv_list_add(list, SYMBOL_FILE, "New", list_btn_action); lv_list_add(list, SYMBOL_DIRECTORY, "Open", list_btn_action); lv_list_add(list, SYMBOL_TRASH, "Delete", list_btn_action); lv_list_add(list, SYMBOL_EDIT, "Edit", list_btn_action); lv_list_add(list, SYMBOL_SAVE, "Save", list_btn_action); lv_list_add(list, SYMBOL_WIFI, "WiFi", list_btn_action); lv_list_add(list, SYMBOL_GPS, "GPS", list_btn_action); lv_obj_t *mbox= lv_mbox_create(parent, NULL); lv_mbox_set_text(mbox, "Click a button to copy its text to the Text area "); lv_obj_set_width(mbox, LV_HOR_RES - LV_DPI); static const char * mbox_btns[] = {"Got it", ""}; lv_mbox_add_btns(mbox, mbox_btns, NULL); /*The default action is close*/ lv_obj_align(mbox, parent, LV_ALIGN_IN_TOP_MID, 0, LV_DPI / 2); }
/** * Refresh the position of the roller. It uses the id stored in: ext->ddlist.selected_option_id * @param roller pointer to a roller object * @param anim_en true: refresh with animation; false: without animation */ static void refr_position(lv_obj_t *roller, bool anim_en) { lv_obj_t *roller_scrl = lv_page_get_scrl(roller); lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller); lv_style_t * style_label = lv_obj_get_style(ext->ddlist.label); const lv_font_t * font = style_label->text.font; lv_coord_t font_h = lv_font_get_height_scale(font); lv_coord_t h = lv_obj_get_height(roller); int32_t id = ext->ddlist.sel_opt_id; lv_coord_t line_y1 = id * (font_h + style_label->text.line_space) + ext->ddlist.label->coords.y1 - roller_scrl->coords.y1; lv_coord_t new_y = - line_y1 + (h - font_h) / 2; if(ext->ddlist.anim_time == 0 || anim_en == false) { lv_obj_set_y(roller_scrl, new_y); } else { #if USE_LV_ANIMATION lv_anim_t a; a.var = roller_scrl; a.start = lv_obj_get_y(roller_scrl); a.end = new_y; a.fp = (lv_anim_fp_t)lv_obj_set_y; a.path = lv_anim_path_linear; a.end_cb = NULL; a.act_time = 0; a.time = ext->ddlist.anim_time; a.playback = 0; a.playback_pause = 0; a.repeat = 0; a.repeat_pause = 0; lv_anim_create(&a); #endif } }
/** * Refresh the size of drop down list according to its status (open or closed) * @param ddlist pointer to a drop down list object * @param anim_en Change the size (open/close) with or without animation (true/false) */ static void lv_ddlist_refr_size(lv_obj_t * ddlist, bool anim_en) { #if USE_LV_ANIMATION == 0 anim_en = false; #endif lv_ddlist_ext_t * ext = lv_obj_get_ext_attr(ddlist); lv_style_t * style = lv_obj_get_style(ddlist); lv_coord_t new_height; if(ext->opened) { /*Open the list*/ if(ext->fix_height == 0) new_height = lv_obj_get_height(lv_page_get_scrl(ddlist)) + 2 * style->body.padding.ver; else new_height = ext->fix_height; } else { /*Close the list*/ const lv_font_t * font = style->text.font; lv_style_t * label_style = lv_obj_get_style(ext->label); lv_coord_t font_h = lv_font_get_height(font); new_height = font_h + 2 * label_style->text.line_space; } if(anim_en == 0) { lv_obj_set_height(ddlist, new_height); lv_ddlist_pos_current_option(ddlist); } else { #if USE_LV_ANIMATION lv_anim_t a; a.var = ddlist; a.start = lv_obj_get_height(ddlist); a.end = new_height; a.fp = (lv_anim_fp_t)lv_obj_set_height; a.path = lv_anim_path_linear; a.end_cb = (lv_anim_cb_t)lv_ddlist_pos_current_option; a.act_time = 0; a.time = ext->anim_time; a.playback = 0; a.playback_pause = 0; a.repeat = 0; a.repeat_pause = 0; lv_anim_create(&a); #endif } }
/** * Draw a rectangle which has gradient on its top and bottom * @param roller pointer to a roller object * @param mask pointer to the current mask (from the design function) */ static void draw_bg(lv_obj_t *roller, const lv_area_t *mask) { lv_style_t *style = lv_roller_get_style(roller, LV_ROLLER_STYLE_BG); lv_area_t half_mask; lv_area_t half_roller; lv_coord_t h = lv_obj_get_height(roller); bool union_ok; lv_area_copy(&half_roller, &roller->coords); half_roller.x1 -= roller->ext_size; /*Add ext size too (e.g. because of shadow draw) */ half_roller.x2 += roller->ext_size; half_roller.y1 -= roller->ext_size; half_roller.y2 = roller->coords.y1 + h / 2; union_ok = lv_area_union(&half_mask, &half_roller, mask); half_roller.x1 += roller->ext_size; /*Revert ext. size adding*/ half_roller.x2 -= roller->ext_size; half_roller.y1 += roller->ext_size; half_roller.y2 += style->body.radius; if(union_ok) { lv_draw_rect(&half_roller, &half_mask, style); } half_roller.x1 -= roller->ext_size; /*Add ext size too (e.g. because of shadow draw) */ half_roller.x2 += roller->ext_size; half_roller.y2 = roller->coords.y2 + roller->ext_size; half_roller.y1 = roller->coords.y1 + h / 2; if((h & 0x1) == 0) half_roller.y1++; /*With even height the pixels in the middle would be drawn twice*/ union_ok = lv_area_union(&half_mask, &half_roller, mask); half_roller.x1 += roller->ext_size; /*Revert ext. size adding*/ half_roller.x2 -= roller->ext_size; half_roller.y2 -= roller->ext_size; half_roller.y1 -= style->body.radius; if(union_ok){ lv_color_t main_tmp = style->body.main_color; lv_color_t grad_tmp = style->body.grad_color; style->body.main_color = grad_tmp; style->body.grad_color = main_tmp; lv_draw_rect(&half_roller, &half_mask, style); style->body.main_color = main_tmp; style->body.grad_color = grad_tmp; } }
/** * Draw the needles of a gauge * @param gauge pointer to gauge object * @param mask mask of drawing */ static void lv_gauge_draw_needle(lv_obj_t * gauge, const lv_area_t * mask) { lv_style_t style_needle; lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge); lv_style_t * style = lv_gauge_get_style(gauge); lv_coord_t r = lv_obj_get_width(gauge) / 2 - style->body.padding.hor; lv_coord_t x_ofs = lv_obj_get_width(gauge) / 2 + gauge->coords.x1; lv_coord_t y_ofs = lv_obj_get_height(gauge) / 2 + gauge->coords.y1; uint16_t angle = lv_lmeter_get_scale_angle(gauge); int16_t angle_ofs = 90 + (360 - angle) / 2; int16_t min = lv_gauge_get_min_value(gauge); int16_t max = lv_gauge_get_max_value(gauge); lv_point_t p_mid; lv_point_t p_end; uint8_t i; lv_style_copy(&style_needle, style); p_mid.x = x_ofs; p_mid.y = y_ofs; for(i = 0; i < ext->needle_count; i++) { /*Calculate the end point of a needle*/ int16_t needle_angle = (ext->values[i] - min) * angle / (max - min) + angle_ofs; p_end.y = (lv_trigo_sin(needle_angle) * r) / LV_TRIGO_SIN_MAX + y_ofs; p_end.x = (lv_trigo_sin(needle_angle + 90) * r) / LV_TRIGO_SIN_MAX + x_ofs; /*Draw the needle with the corresponding color*/ if(ext->needle_colors == NULL) style_needle.line.color = LV_GAUGE_DEF_NEEDLE_COLOR; else style_needle.line.color = ext->needle_colors[i]; lv_draw_line(&p_mid, &p_end, mask, &style_needle); } /*Draw the needle middle area*/ lv_style_t style_neddle_mid; lv_style_copy(&style_neddle_mid, &lv_style_plain); style_neddle_mid.body.main_color = style->body.border.color; style_neddle_mid.body.grad_color = style->body.border.color; style_neddle_mid.body.radius = LV_RADIUS_CIRCLE; lv_area_t nm_cord; nm_cord.x1 = x_ofs - style->body.padding.ver; nm_cord.y1 = y_ofs - style->body.padding.ver; nm_cord.x2 = x_ofs + style->body.padding.ver; nm_cord.y2 = y_ofs + style->body.padding.ver; lv_draw_rect(&nm_cord, mask, &style_neddle_mid); }
/** * Set the position of list when it is closed to show the selected item * @param ddlist pointer to a drop down list */ static void lv_ddlist_pos_current_option(lv_obj_t * ddlist) { lv_ddlist_ext_t * ext = lv_obj_get_ext_attr(ddlist); lv_style_t * style = lv_obj_get_style(ddlist); const lv_font_t * font = style->text.font; lv_coord_t font_h = lv_font_get_height(font); lv_style_t * label_style = lv_obj_get_style(ext->label); lv_obj_t * scrl = lv_page_get_scrl(ddlist); lv_coord_t h = lv_obj_get_height(ddlist); lv_coord_t line_y1 = ext->sel_opt_id * (font_h + label_style->text.line_space) + ext->label->coords.y1 - scrl->coords.y1; lv_obj_set_y(scrl, - line_y1 + (h - font_h) / 2); }
static void write_create(lv_obj_t *parent) { lv_page_set_style(parent, LV_PAGE_STYLE_BG, &lv_style_transp_fit); lv_page_set_style(parent, LV_PAGE_STYLE_SCRL, &lv_style_transp_fit); lv_page_set_sb_mode(parent, LV_SB_MODE_OFF); static lv_style_t style_ta; lv_style_copy(&style_ta, &lv_style_pretty); style_ta.body.opa = LV_OPA_30; style_ta.body.radius = 0; style_ta.text.color = LV_COLOR_HEX3(0x222); ta = lv_ta_create(parent, NULL); lv_obj_set_size(ta, lv_page_get_scrl_width(parent), lv_obj_get_height(parent) / 2); lv_ta_set_style(ta, LV_TA_STYLE_BG, &style_ta); lv_ta_set_text(ta, ""); lv_page_set_rel_action(ta, keyboard_open_close); lv_style_copy(&style_kb, &lv_style_plain); style_kb.body.opa = LV_OPA_70; style_kb.body.main_color = LV_COLOR_HEX3(0x333); style_kb.body.grad_color = LV_COLOR_HEX3(0x333); style_kb.body.padding.hor = 0; style_kb.body.padding.ver = 0; style_kb.body.padding.inner = 0; lv_style_copy(&style_kb_rel, &lv_style_plain); style_kb_rel.body.empty = 1; style_kb_rel.body.radius = 0; style_kb_rel.body.border.width = 1; style_kb_rel.body.border.color = LV_COLOR_SILVER; style_kb_rel.body.border.opa = LV_OPA_50; style_kb_rel.body.main_color = LV_COLOR_HEX3(0x333); /*Recommended if LV_VDB_SIZE == 0 and bpp > 1 fonts are used*/ style_kb_rel.body.grad_color = LV_COLOR_HEX3(0x333); style_kb_rel.text.color = LV_COLOR_WHITE; lv_style_copy(&style_kb_pr, &lv_style_plain); style_kb_pr.body.radius = 0; style_kb_pr.body.opa = LV_OPA_50; style_kb_pr.body.main_color = LV_COLOR_WHITE; style_kb_pr.body.grad_color = LV_COLOR_WHITE; style_kb_pr.body.border.width = 1; style_kb_pr.body.border.color = LV_COLOR_SILVER; keyboard_open_close(ta); }
/** * Signal function of the roller * @param roller pointer to a roller 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_roller_signal(lv_obj_t * roller, lv_signal_t sign, void * param) { lv_res_t res = LV_RES_OK; /*Don't let the drop down list to handle the control signals. It works differently*/ if(sign != LV_SIGNAL_CONTROLL && sign != LV_SIGNAL_FOCUS && sign != LV_SIGNAL_DEFOCUS) { /* Include the ancient signal function */ res = ancestor_signal(roller, sign, param); if(res != LV_RES_OK) return res; } lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller); if(sign == LV_SIGNAL_STYLE_CHG) { lv_obj_set_height(lv_page_get_scrl(roller), lv_obj_get_height(ext->ddlist.label) + lv_obj_get_height(roller)); lv_obj_align(ext->ddlist.label, NULL, LV_ALIGN_CENTER, 0, 0); lv_ddlist_set_selected(roller, ext->ddlist.sel_opt_id); refr_position(roller, false); } else if(sign == LV_SIGNAL_CORD_CHG) { if(lv_obj_get_width(roller) != lv_area_get_width(param) || lv_obj_get_height(roller) != lv_area_get_height(param)) { lv_ddlist_set_fix_height(roller, lv_obj_get_height(roller)); lv_obj_set_height(lv_page_get_scrl(roller), lv_obj_get_height(ext->ddlist.label) + lv_obj_get_height(roller)); lv_obj_align(ext->ddlist.label, NULL, LV_ALIGN_CENTER, 0, 0); lv_ddlist_set_selected(roller, ext->ddlist.sel_opt_id); refr_position(roller, false); } } else if(sign == LV_SIGNAL_CONTROLL) { char c = *((char*)param); if(c == LV_GROUP_KEY_RIGHT || c == LV_GROUP_KEY_DOWN) { if(ext->ddlist.sel_opt_id +1 < ext->ddlist.option_cnt) { lv_roller_set_selected(roller, ext->ddlist.sel_opt_id + 1, true); if(ext->ddlist.action != NULL) { ext->ddlist.action(roller); } } } else if(c == LV_GROUP_KEY_LEFT || c == LV_GROUP_KEY_UP) { if(ext->ddlist.sel_opt_id > 0) { lv_roller_set_selected(roller, ext->ddlist.sel_opt_id - 1, true); if(ext->ddlist.action != NULL) { ext->ddlist.action(roller); } } } } return res; }
/** * Draw the scale on a gauge * @param gauge pointer to gauge object * @param mask mask of drawing */ static void lv_gauge_draw_scale(lv_obj_t * gauge, const lv_area_t * mask) { char scale_txt[16]; lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge); lv_style_t * style = lv_obj_get_style(gauge); lv_coord_t r = lv_obj_get_width(gauge) / 2 - (3 * style->body.padding.hor) - style->body.padding.inner; lv_coord_t x_ofs = lv_obj_get_width(gauge) / 2 + gauge->coords.x1; lv_coord_t y_ofs = lv_obj_get_height(gauge) / 2 + gauge->coords.y1; int16_t scale_angle = lv_lmeter_get_scale_angle(gauge); uint16_t label_num = ext->label_count; int16_t angle_ofs = 90 + (360 - scale_angle) / 2; int16_t min = lv_gauge_get_min_value(gauge); int16_t max = lv_gauge_get_max_value(gauge); uint8_t i; for(i = 0; i < label_num; i++) { /*Calculate the position a scale label*/ int16_t angle = (i * scale_angle) / (label_num - 1) + angle_ofs; lv_coord_t y = (int32_t)((int32_t)lv_trigo_sin(angle) * r) / LV_TRIGO_SIN_MAX; y += y_ofs; lv_coord_t x = (int32_t)((int32_t)lv_trigo_sin(angle + 90) * r) / LV_TRIGO_SIN_MAX; x += x_ofs; int16_t scale_act = (int32_t)((int32_t)(max - min) * i) / (label_num - 1); scale_act += min; lv_math_num_to_str(scale_act, scale_txt); lv_area_t label_cord; lv_point_t label_size; lv_txt_get_size(&label_size, scale_txt, style->text.font, style->text.letter_space, style->text.line_space, LV_COORD_MAX, LV_TXT_FLAG_NONE); /*Draw the label*/ label_cord.x1 = x - label_size.x / 2; label_cord.y1 = y - label_size.y / 2; label_cord.x2 = label_cord.x1 + label_size.x; label_cord.y2 = label_cord.y1 + label_size.y; lv_draw_label(&label_cord, mask, style, scale_txt, LV_TXT_FLAG_NONE, NULL); } }
static lv_res_t keyboard_open_close(lv_obj_t * text_area) { lv_obj_t * parent = lv_obj_get_parent(lv_obj_get_parent(ta)); /*Test area is on the scrollabe part of the page but we need the page itself*/ if(kb) { return keyboard_hide_action(kb); } else { kb = lv_kb_create(parent, NULL); lv_obj_set_size(kb, lv_page_get_scrl_width(parent), lv_obj_get_height(parent) / 2); lv_obj_align(kb, ta, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); lv_kb_set_ta(kb, ta); lv_kb_set_style(kb, LV_KB_STYLE_BG, &style_kb); lv_kb_set_style(kb, LV_KB_STYLE_BTN_REL, &style_kb_rel); lv_kb_set_style(kb, LV_KB_STYLE_BTN_PR, &style_kb_pr); lv_kb_set_hide_action(kb, keyboard_hide_action); lv_kb_set_ok_action(kb, keyboard_hide_action); #if USE_LV_ANIMATION lv_obj_animate(kb, LV_ANIM_FLOAT_BOTTOM | LV_ANIM_IN, 300, 0, NULL); #endif return LV_RES_OK; } }
static void chart_create(lv_obj_t *parent) { lv_page_set_style(parent, LV_PAGE_STYLE_BG, &lv_style_transp_fit); lv_page_set_style(parent, LV_PAGE_STYLE_SCRL, &lv_style_transp_fit); lv_page_set_scrl_fit(parent, false, false); lv_page_set_scrl_height(parent, lv_obj_get_height(parent)); lv_page_set_sb_mode(parent, LV_SB_MODE_OFF); static lv_style_t style_chart; lv_style_copy(&style_chart, &lv_style_pretty); style_chart.body.opa = LV_OPA_60; style_chart.body.radius = 0; style_chart.line.color = LV_COLOR_GRAY; chart = lv_chart_create(parent, NULL); lv_obj_set_size(chart, 2 * lv_obj_get_width(parent) / 3, lv_obj_get_height(parent) / 2); lv_obj_align(chart, NULL, LV_ALIGN_IN_TOP_MID, 0, LV_DPI / 4); lv_chart_set_type(chart, LV_CHART_TYPE_COLUMN); lv_chart_set_style(chart, &style_chart); lv_chart_set_series_opa(chart, LV_OPA_70); lv_chart_series_t *ser1; ser1 = lv_chart_add_series(chart, LV_COLOR_RED); lv_chart_set_next(chart, ser1, 40); lv_chart_set_next(chart, ser1, 30); lv_chart_set_next(chart, ser1, 47); lv_chart_set_next(chart, ser1, 59); lv_chart_set_next(chart, ser1, 59); lv_chart_set_next(chart, ser1, 31); lv_chart_set_next(chart, ser1, 55); lv_chart_set_next(chart, ser1, 70); lv_chart_set_next(chart, ser1, 82); /*Create a bar, an indicator and a knob style*/ static lv_style_t style_bar; static lv_style_t style_indic; static lv_style_t style_knob; lv_style_copy(&style_bar, &lv_style_pretty); style_bar.body.main_color = LV_COLOR_BLACK; style_bar.body.grad_color = LV_COLOR_GRAY; style_bar.body.radius = LV_RADIUS_CIRCLE; style_bar.body.border.color = LV_COLOR_WHITE; style_bar.body.opa = LV_OPA_60; style_bar.body.padding.hor = 0; style_bar.body.padding.ver = LV_DPI / 10; lv_style_copy(&style_indic, &lv_style_pretty); style_indic.body.grad_color = LV_COLOR_MARRON; style_indic.body.main_color = LV_COLOR_RED; style_indic.body.radius = LV_RADIUS_CIRCLE; style_indic.body.shadow.width = LV_DPI / 10; style_indic.body.shadow.color = LV_COLOR_RED; style_indic.body.padding.hor = LV_DPI / 30; style_indic.body.padding.ver = LV_DPI / 30; lv_style_copy(&style_knob, &lv_style_pretty); style_knob.body.radius = LV_RADIUS_CIRCLE; style_knob.body.opa = LV_OPA_70; /*Create a second slider*/ lv_obj_t *slider = lv_slider_create(parent, NULL); lv_slider_set_style(slider, LV_SLIDER_STYLE_BG, &style_bar); lv_slider_set_style(slider, LV_SLIDER_STYLE_INDIC, &style_indic); lv_slider_set_style(slider, LV_SLIDER_STYLE_KNOB, &style_knob); lv_obj_set_size(slider, lv_obj_get_width(chart), LV_DPI / 3); lv_obj_align(slider, chart, LV_ALIGN_OUT_BOTTOM_MID, 0, (LV_VER_RES - chart->coords.y2 - lv_obj_get_height(slider)) / 2); /*Align to below the chart*/ lv_slider_set_action(slider, slider_action); lv_slider_set_range(slider, 10, 1000); lv_slider_set_value(slider, 700); slider_action(slider); /*Simulate a user value set the refresh the chart*/ }
/** * Refresh the label with its text stored in its extended data * @param label pointer to a label object */ static void lv_label_refr_text(lv_obj_t * label) { lv_label_ext_t * ext = lv_obj_get_ext_attr(label); if(ext->text == NULL) return; lv_coord_t max_w = lv_obj_get_width(label); lv_style_t * style = lv_obj_get_style(label); const lv_font_t * font = style->text.font; /*If the width will be expanded set the max length to very big */ if(ext->long_mode == LV_LABEL_LONG_EXPAND || ext->long_mode == LV_LABEL_LONG_SCROLL) { max_w = LV_COORD_MAX; } /*Calc. the height and longest line*/ lv_point_t size; lv_txt_flag_t flag = LV_TXT_FLAG_NONE; if(ext->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR; if(ext->expand != 0) flag |= LV_TXT_FLAG_EXPAND; if(ext->no_break != 0) flag |= LV_TXT_FLAG_NO_BREAK; lv_txt_get_size(&size, ext->text, font, style->text.letter_space, style->text.line_space, max_w, flag); /*Set the full size in expand mode*/ if(ext->long_mode == LV_LABEL_LONG_EXPAND || ext->long_mode == LV_LABEL_LONG_SCROLL) { lv_obj_set_size(label, size.x, size.y); /*Start scrolling if the label is greater then its parent*/ if(ext->long_mode == LV_LABEL_LONG_SCROLL) { #if USE_LV_ANIMATION lv_obj_t * parent = lv_obj_get_parent(label); /*Delete the potential previous scroller animations*/ lv_anim_del(label, (lv_anim_fp_t) lv_obj_set_x); lv_anim_del(label, (lv_anim_fp_t) lv_obj_set_y); lv_anim_t anim; anim.var = label; anim.repeat = 1; anim.playback = 1; anim.start = lv_font_get_width(font, ' '); anim.act_time = 0; anim.end_cb = NULL; anim.path = lv_anim_path_linear; anim.playback_pause = (((lv_font_get_width(style->text.font, ' ') + style->text.letter_space) * 1000) / ext->anim_speed) * ANIM_WAIT_CHAR_COUNT; anim.repeat_pause = anim.playback_pause; if(lv_obj_get_width(label) > lv_obj_get_width(parent)) { anim.end = lv_obj_get_width(parent) - lv_obj_get_width(label) - lv_font_get_width(font, ' '); anim.fp = (lv_anim_fp_t) lv_obj_set_x; anim.time = lv_anim_speed_to_time(ext->anim_speed, anim.start, anim.end); lv_anim_create(&anim); } else if(lv_obj_get_height(label) > lv_obj_get_height(parent)) { anim.end = lv_obj_get_height(parent) - lv_obj_get_height(label) - lv_font_get_height(font); anim.fp = (lv_anim_fp_t)lv_obj_set_y; anim.time = lv_anim_speed_to_time(ext->anim_speed, anim.start, anim.end); lv_anim_create(&anim); } #endif } } /*In roll mode keep the size but start offset animations*/ else if(ext->long_mode == LV_LABEL_LONG_ROLL) { #if USE_LV_ANIMATION lv_anim_t anim; anim.var = label; anim.repeat = 1; anim.playback = 1; anim.start = lv_font_get_width(font, ' '); anim.act_time = 0; anim.end_cb = NULL; anim.path = lv_anim_path_linear; anim.playback_pause = (((lv_font_get_width(style->text.font, ' ') + style->text.letter_space) * 1000) / ext->anim_speed) * ANIM_WAIT_CHAR_COUNT;; anim.repeat_pause = anim.playback_pause; bool hor_anim = false; if(size.x > lv_obj_get_width(label)) { anim.end = lv_obj_get_width(label) - size.x - lv_font_get_width(font, ' '); anim.fp = (lv_anim_fp_t) lv_label_set_offset_x; anim.time = lv_anim_speed_to_time(ext->anim_speed, anim.start, anim.end); lv_anim_create(&anim); hor_anim = true; } else { /*Delete the offset animation if not required*/ lv_anim_del(label, (lv_anim_fp_t) lv_label_set_offset_x); ext->offset.x = 0; } if(size.y > lv_obj_get_height(label) && hor_anim == false) { anim.end = lv_obj_get_height(label) - size.y - (lv_font_get_height(font)); anim.fp = (lv_anim_fp_t)lv_label_set_offset_y; anim.time = lv_anim_speed_to_time(ext->anim_speed, anim.start, anim.end); lv_anim_create(&anim); } else { /*Delete the offset animation if not required*/ lv_anim_del(label, (lv_anim_fp_t) lv_label_set_offset_y); ext->offset.y = 0; } #endif } else if(ext->long_mode == LV_LABEL_LONG_DOT) { if(size.y <= lv_obj_get_height(label)) { /*No dots are required, the text is short enough*/ ext->dot_end = LV_LABEL_DOT_END_INV; } else if(lv_txt_get_length(ext->text) <= LV_LABEL_DOT_NUM) { /*Don't turn to dots all the characters*/ ext->dot_end = LV_LABEL_DOT_END_INV; } else { lv_point_t p; p.x = lv_obj_get_width(label) - (lv_font_get_width(style->text.font, '.') + style->text.letter_space) * LV_LABEL_DOT_NUM; /*Shrink with dots*/ p.y = lv_obj_get_height(label); p.y -= p.y % (lv_font_get_height(style->text.font) + style->text.line_space); /*Round down to the last line*/ p.y -= style->text.line_space; /*Trim the last line space*/ uint32_t letter_id = lv_label_get_letter_on(label, &p); #if LV_TXT_UTF8 == 0 /*Save letters under the dots and replace them with dots*/ uint8_t i; for(i = 0; i < LV_LABEL_DOT_NUM; i++) { ext->dot_tmp[i] = ext->text[letter_id + i]; ext->text[letter_id + i] = '.'; } ext->dot_tmp[LV_LABEL_DOT_NUM] = ext->text[letter_id + LV_LABEL_DOT_NUM]; ext->text[letter_id + LV_LABEL_DOT_NUM] = '\0'; ext->dot_end = letter_id + LV_LABEL_DOT_NUM; #else /*Save letters under the dots and replace them with dots*/ uint32_t i; uint32_t byte_id = txt_utf8_get_byte_id(ext->text, letter_id); uint32_t byte_id_ori = byte_id; uint8_t len = 0; for(i = 0; i <= LV_LABEL_DOT_NUM; i++) { len += lv_txt_utf8_size(ext->text[byte_id]); lv_txt_utf8_next(ext->text, &byte_id); } memcpy(ext->dot_tmp, &ext->text[byte_id_ori], len); ext->dot_tmp[len] = '\0'; /*Close with a zero*/ for(i = 0; i < LV_LABEL_DOT_NUM; i++) { ext->text[byte_id_ori + i] = '.'; } ext->text[byte_id_ori + LV_LABEL_DOT_NUM] = '\0'; ext->dot_end = letter_id + LV_LABEL_DOT_NUM; #endif } } /*In break mode only the height can change*/ else if (ext->long_mode == LV_LABEL_LONG_BREAK) { lv_obj_set_height(label, size.y); } lv_obj_invalidate(label); }
/** * Refresh the position and size of the scroll bars. * @param page pointer to a page object */ static void lv_page_sb_refresh(lv_obj_t * page) { lv_page_ext_t * ext = lv_obj_get_ext_attr(page); lv_style_t * style = lv_obj_get_style(page); lv_obj_t * scrl = ext->scrl; lv_coord_t size_tmp; lv_coord_t scrl_w = lv_obj_get_width(scrl); lv_coord_t scrl_h = lv_obj_get_height(scrl); lv_coord_t hpad = style->body.padding.hor; lv_coord_t vpad = style->body.padding.ver; lv_coord_t obj_w = lv_obj_get_width(page); lv_coord_t obj_h = lv_obj_get_height(page); /*Always let 'scrollbar width' padding above, under, left and right to the scrollbars * else: * - horizontal and vertical scrollbars can overlap on the corners * - if the page has radius the scrollbar can be out of the radius */ lv_coord_t sb_hor_pad = LV_MATH_MAX(ext->sb.style->body.padding.inner, style->body.padding.hor); lv_coord_t sb_ver_pad = LV_MATH_MAX(ext->sb.style->body.padding.inner, style->body.padding.ver); if(ext->sb.mode == LV_SB_MODE_OFF) return; if(ext->sb.mode == LV_SB_MODE_ON) { ext->sb.hor_draw = 1; ext->sb.ver_draw = 1; } /*Invalidate the current (old) scrollbar areas*/ lv_area_t sb_area_tmp; if(ext->sb.hor_draw != 0) { lv_area_copy(&sb_area_tmp, &ext->sb.hor_area); sb_area_tmp.x1 += page->coords.x1; sb_area_tmp.y1 += page->coords.y1; sb_area_tmp.x2 += page->coords.x2; sb_area_tmp.y2 += page->coords.y2; lv_inv_area(&sb_area_tmp); } if(ext->sb.ver_draw != 0) { lv_area_copy(&sb_area_tmp, &ext->sb.ver_area); sb_area_tmp.x1 += page->coords.x1; sb_area_tmp.y1 += page->coords.y1; sb_area_tmp.x2 += page->coords.x2; sb_area_tmp.y2 += page->coords.y2; lv_inv_area(&sb_area_tmp); } if(ext->sb.mode == LV_SB_MODE_DRAG && lv_indev_is_dragging(lv_indev_get_act()) == false) { ext->sb.hor_draw = 0; ext->sb.ver_draw = 0; return; } /*Horizontal scrollbar*/ if(scrl_w <= obj_w - 2 * hpad) { /*Full sized scroll bar*/ lv_area_set_width(&ext->sb.hor_area, obj_w - 2 * sb_hor_pad); lv_area_set_pos(&ext->sb.hor_area, sb_hor_pad, obj_h - ext->sb.style->body.padding.inner - ext->sb.style->body.padding.ver); if(ext->sb.mode == LV_SB_MODE_AUTO || ext->sb.mode == LV_SB_MODE_DRAG) ext->sb.hor_draw = 0; } else { size_tmp = (obj_w * (obj_w - (2 * sb_hor_pad))) / (scrl_w + 2 * hpad); if(size_tmp < LV_PAGE_SB_MIN_SIZE) size_tmp = LV_PAGE_SB_MIN_SIZE; lv_area_set_width(&ext->sb.hor_area, size_tmp); lv_area_set_pos(&ext->sb.hor_area, sb_hor_pad + (-(lv_obj_get_x(scrl) - hpad) * (obj_w - size_tmp - 2 * sb_hor_pad)) / (scrl_w + 2 * hpad - obj_w ), obj_h - ext->sb.style->body.padding.inner - ext->sb.style->body.padding.ver); if(ext->sb.mode == LV_SB_MODE_AUTO || ext->sb.mode == LV_SB_MODE_DRAG) ext->sb.hor_draw = 1; } /*Vertical scrollbar*/ if(scrl_h <= obj_h - 2 * vpad) { /*Full sized scroll bar*/ lv_area_set_height(&ext->sb.ver_area, obj_h - 2 * sb_ver_pad); lv_area_set_pos(&ext->sb.ver_area, obj_w - ext->sb.style->body.padding.inner - ext->sb.style->body.padding.hor, sb_ver_pad); if(ext->sb.mode == LV_SB_MODE_AUTO || ext->sb.mode == LV_SB_MODE_DRAG) ext->sb.ver_draw = 0; } else { size_tmp = (obj_h * (obj_h - (2 * sb_ver_pad))) / (scrl_h + 2 * vpad); if(size_tmp < LV_PAGE_SB_MIN_SIZE) size_tmp = LV_PAGE_SB_MIN_SIZE; lv_area_set_height(&ext->sb.ver_area, size_tmp); lv_area_set_pos(&ext->sb.ver_area, obj_w - ext->sb.style->body.padding.inner - ext->sb.style->body.padding.hor, sb_ver_pad + (-(lv_obj_get_y(scrl) - vpad) * (obj_h - size_tmp - 2 * sb_ver_pad)) / (scrl_h + 2 * vpad - obj_h )); if(ext->sb.mode == LV_SB_MODE_AUTO || ext->sb.mode == LV_SB_MODE_DRAG) ext->sb.ver_draw = 1; } /*Invalidate the new scrollbar areas*/ if(ext->sb.hor_draw != 0) { lv_area_copy(&sb_area_tmp, &ext->sb.hor_area); sb_area_tmp.x1 += page->coords.x1; sb_area_tmp.y1 += page->coords.y1; sb_area_tmp.x2 += page->coords.x2; sb_area_tmp.y2 += page->coords.y2; lv_inv_area(&sb_area_tmp); } if(ext->sb.ver_draw != 0) { lv_area_copy(&sb_area_tmp, &ext->sb.ver_area); sb_area_tmp.x1 += page->coords.x1; sb_area_tmp.y1 += page->coords.y1; sb_area_tmp.x2 += page->coords.x2; sb_area_tmp.y2 += page->coords.y2; lv_inv_area(&sb_area_tmp); } }
/** * Signal function of the page * @param page pointer to a page 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_page_signal(lv_obj_t * page, lv_signal_t sign, void * param) { lv_res_t res; /* Include the ancient signal function */ res = ancestor_signal(page, sign, param); if(res != LV_RES_OK) return res; lv_page_ext_t * ext = lv_obj_get_ext_attr(page); lv_style_t * style = lv_obj_get_style(page); lv_obj_t * child; if(sign == LV_SIGNAL_CHILD_CHG) { /*Automatically move children to the scrollable object*/ child = lv_obj_get_child(page, NULL); while(child != NULL) { if(lv_obj_is_protected(child, LV_PROTECT_PARENT) == false) { lv_obj_t * tmp = child; child = lv_obj_get_child(page, child); /*Get the next child before move this*/ lv_obj_set_parent(tmp, ext->scrl); } else { child = lv_obj_get_child(page, child); } } } else if(sign == LV_SIGNAL_STYLE_CHG) { /*If no hor_fit enabled set the scrollable's width to the page's width*/ if(lv_cont_get_hor_fit(ext->scrl) == false) { lv_obj_set_width(ext->scrl, lv_obj_get_width(page) - 2 * style->body.padding.hor); } else { ext->scrl->signal_func(ext->scrl, LV_SIGNAL_CORD_CHG, &ext->scrl->coords); } /*The scrollbars are important only if they are visible now*/ if(ext->sb.hor_draw || ext->sb.ver_draw) lv_page_sb_refresh(page); /*Refresh the ext. size because the scrollbars might be positioned out of the page*/ lv_obj_refresh_ext_size(page); } else if(sign == LV_SIGNAL_CORD_CHG) { /*Refresh the scrollbar and notify the scrl if the size is changed*/ if(ext->scrl != NULL && (lv_obj_get_width(page) != lv_area_get_width(param) || lv_obj_get_height(page) != lv_area_get_height(param))) { /*If no hor_fit enabled set the scrollable's width to the page's width*/ if(lv_cont_get_hor_fit(ext->scrl) == false) { lv_obj_set_width(ext->scrl, lv_obj_get_width(page) - 2 * style->body.padding.hor); } ext->scrl->signal_func(ext->scrl, LV_SIGNAL_CORD_CHG, &ext->scrl->coords); /*The scrollbars are important only if they are visible now*/ if(ext->sb.hor_draw || ext->sb.ver_draw) lv_page_sb_refresh(page); } } else if(sign == LV_SIGNAL_PRESSED) { if(ext->pr_action != NULL) { ext->pr_action(page); } } else if(sign == LV_SIGNAL_RELEASED) { if(lv_indev_is_dragging(lv_indev_get_act()) == false) { if(ext->rel_action != NULL) { ext->rel_action(page); } } } else if(sign == LV_SIGNAL_REFR_EXT_SIZE) { /*Ensure ext. size for the scrollbars if they are out of the page*/ if(page->ext_size < (-ext->sb.style->body.padding.hor)) page->ext_size = -ext->sb.style->body.padding.hor; if(page->ext_size < (-ext->sb.style->body.padding.ver)) page->ext_size = -ext->sb.style->body.padding.ver; } else if(sign == LV_SIGNAL_GET_TYPE) { lv_obj_type_t * buf = param; uint8_t i; for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/ if(buf->type[i] == NULL) break; } buf->type[i] = "lv_page"; } return res; }