LVGL-page控件

page的子对象

1、控件创建时会发送信号给它的父对象:

lv_obj_t * lv_obj_create(lv_obj_t * parent, const lv_obj_t * copy)

{

……

    /*Send a signal to the parent to notify it about the new child*/
    if(parent != NULL) {
        parent->signal_cb(parent, LV_SIGNAL_CHILD_CHG, new_obj);

        /*Invalidate the area if not screen created*/
        lv_obj_invalidate(new_obj);
    }

……

}

2、如果父对象是page(A_obj)时,子对像的父会被更改为page的扩展属性里面对像(ext->scrl,B_obj)。page的移动就是移动B_obj及B_obj为父的所有子对象(X_obj)。A_obj的坐标不会被修改的。

static lv_res_t lv_page_signal(lv_obj_t * page, lv_signal_t sign, void * param)

{

……

        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*/

                /* Reposition the child to take padding into account
                 * It's required to keep new the object on the same coordinate if FIT is enabled.*/
                if((tmp->coords.x1 == page->coords.x1)  &&
                   (fit_left == LV_FIT_TIGHT || fit_left == LV_FIT_MAX) &&
                   base_dir != LV_BIDI_DIR_RTL) {
                    tmp->coords.x1 += scrl_left;
                    tmp->coords.x2 += scrl_left;
                }
                else if((tmp->coords.x2 == page->coords.x2) &&
                        (fit_right == LV_FIT_TIGHT || fit_right == LV_FIT_MAX)
                        && base_dir == LV_BIDI_DIR_RTL) {
                    tmp->coords.x1 -= scrl_right;
                    tmp->coords.x2 -= scrl_right;
                }
                if((tmp->coords.y1 == page->coords.y1) && (fit_top == LV_FIT_TIGHT || fit_top == LV_FIT_MAX)) {
                    tmp->coords.y1 += scrl_top;
                    tmp->coords.y2 += scrl_top;
                }

                lv_obj_set_parent(tmp, ext->scrl);

            }
            else {
                child = lv_obj_get_child(page, child);
            }
        }

……

}

page的滚动传输

page的滚动传输是限于两个page控件之间。当控件支持drag动作时,由于坐标改变会触发static lv_res_t lv_page_scrollable_signal(lv_obj_t * scrl, lv_signal_t sign, void * param)。

滚动开始

                        lv_obj_set_drag_parent(page, true);
                        lv_obj_set_drag_parent(scrl, true);
                        parent_ext->scroll_prop_obj = page;

滚动结束时反向操作。

也可以人为的结合事件模拟滚动传输效果。

static lv_res_t lv_page_scrollable_signal(lv_obj_t * scrl, lv_signal_t sign, void * param)
{

    if(sign == LV_SIGNAL_GET_STYLE) {
        return ancestor_signal(scrl, sign, param);
    }

    lv_res_t res;

    /* Include the ancient signal function */
    res = ancestor_signal(scrl, sign, param);

    if(res != LV_RES_OK) return res;
    if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, "");

    lv_obj_t * page               = lv_obj_get_parent(scrl);
    lv_page_ext_t * page_ext      = lv_obj_get_ext_attr(page);

    if(sign == LV_SIGNAL_COORD_CHG) {
        lv_obj_t * page_parent = lv_obj_get_parent(page);

        /*Handle scroll propagation*/
        lv_indev_t * indev = lv_indev_get_act();
        if(page_ext->scroll_prop  && indev) {
            lv_point_t * drag_sum = &indev->proc.types.pointer.drag_sum;
            lv_page_ext_t * parent_ext = lv_obj_get_ext_attr(lv_obj_get_parent(page_parent));
            if(parent_ext->scroll_prop_obj == NULL) {
                /*If the dragging just started or scroll is already propagated to this object
                 *  enable the scroll propagation if the conditions are met*/
                if((lv_indev_is_dragging(indev) == false || page_ext->scroll_prop_obj) && (drag_sum->y || drag_sum->x)) {
                    /*Propagate vertically?*/
                    if((drag_sum->y > 0 && lv_page_on_edge(page, LV_PAGE_EDGE_TOP)) ||
                       (drag_sum->y < 0 && lv_page_on_edge(page, LV_PAGE_EDGE_BOTTOM))) {
                        lv_obj_set_drag_parent(page, true);
                        lv_obj_set_drag_parent(scrl, true);
                        parent_ext->scroll_prop_obj = page;
                    }
                    /*Propagate horizontally?*/
                    if((drag_sum->x > 0 && lv_page_on_edge(page, LV_PAGE_EDGE_LEFT)) ||
                       (drag_sum->x < 0 && lv_page_on_edge(page, LV_PAGE_EDGE_RIGHT))) {
                        lv_obj_set_drag_parent(page, true);
                        lv_obj_set_drag_parent(scrl, true);
                        parent_ext->scroll_prop_obj = page;
                    }
                }
            }
        }

        scrl_reposition(page);

        lv_page_ext_t * ext      = lv_obj_get_ext_attr(page);

        /*The scrollbars are important only if they are visible now or the scrollable's size has changed*/
        if((ext->scrlbar.hor_draw || ext->scrlbar.ver_draw) ||
           (lv_obj_get_width(scrl) != lv_area_get_width(param) || lv_obj_get_height(scrl) != lv_area_get_height(param))) {
            scrlbar_refresh(page);
        }
    }
    else if(sign == LV_SIGNAL_STYLE_CHG) {
        scrl_reposition(page);
        scrlbar_refresh(page);
    }
    else if(sign == LV_SIGNAL_DRAG_BEGIN) {
        if(page_ext->scrlbar.mode == LV_SCROLLBAR_MODE_DRAG) {
            scrlbar_refresh(page);
        }
    }
    else if(sign == LV_SIGNAL_DRAG_END) {
        /*Scroll propagation is finished on drag end*/
        if(page_ext->scroll_prop_obj) {
            lv_obj_t * scroller_page = page_ext->scroll_prop_obj;
            lv_page_ext_t * scroller_page_ext      = lv_obj_get_ext_attr(scroller_page);
            page_ext->scroll_prop_obj = NULL;
            lv_obj_set_drag_parent(scroller_page, false);
            lv_obj_set_drag_parent(lv_page_get_scrollable(scroller_page), false);

            /*Hide scrollbars if required*/
            if(scroller_page_ext->scrlbar.mode == LV_SCROLLBAR_MODE_DRAG) {
                lv_area_t sb_area_tmp;
                if(scroller_page_ext->scrlbar.hor_draw) {
                    lv_area_copy(&sb_area_tmp, &scroller_page_ext->scrlbar.hor_area);
                    sb_area_tmp.x1 += scroller_page->coords.x1;
                    sb_area_tmp.y1 += scroller_page->coords.y1;
                    sb_area_tmp.x2 += scroller_page->coords.x1;
                    sb_area_tmp.y2 += scroller_page->coords.y1;
                    lv_obj_invalidate_area(scroller_page, &sb_area_tmp);
                    scroller_page_ext->scrlbar.hor_draw = 0;
                }
                if(scroller_page_ext->scrlbar.ver_draw) {
                    lv_area_copy(&sb_area_tmp, &scroller_page_ext->scrlbar.ver_area);
                    sb_area_tmp.x1 += scroller_page->coords.x1;
                    sb_area_tmp.y1 += scroller_page->coords.y1;
                    sb_area_tmp.x2 += scroller_page->coords.x1;
                    sb_area_tmp.y2 += scroller_page->coords.y1;
                    lv_obj_invalidate_area(scroller_page, &sb_area_tmp);
                    scroller_page_ext->scrlbar.ver_draw = 0;
                }
            }

            /*The scrolling can be chained so stop all of them*/
            lv_page_ext_t * scroller_ext = lv_obj_get_ext_attr(scroller_page);
            while(scroller_ext->scroll_prop_obj) {
                scroller_page = scroller_ext->scroll_prop_obj;
                scroller_ext->scroll_prop_obj = NULL;
                lv_obj_set_drag_parent(scroller_page, false);
                lv_obj_set_drag_parent(lv_page_get_scrollable(scroller_page), false);

                /*Hide scrollbars if required*/
                if(scroller_page_ext->scrlbar.mode == LV_SCROLLBAR_MODE_DRAG) {
                    scroller_page_ext->scrlbar.hor_draw = 0;
                    scroller_page_ext->scrlbar.ver_draw = 0;
                    lv_obj_invalidate(scroller_page);
                }
                scroller_ext = lv_obj_get_ext_attr(scroller_page);
            }
        }

        /*Hide scrollbars if required*/
        if(page_ext->scrlbar.mode == LV_SCROLLBAR_MODE_DRAG) {
            lv_area_t sb_area_tmp;
            if(page_ext->scrlbar.hor_draw) {
                lv_area_copy(&sb_area_tmp, &page_ext->scrlbar.hor_area);
                sb_area_tmp.x1 += page->coords.x1;
                sb_area_tmp.y1 += page->coords.y1;
                sb_area_tmp.x2 += page->coords.x1;
                sb_area_tmp.y2 += page->coords.y1;
                lv_obj_invalidate_area(page, &sb_area_tmp);
                page_ext->scrlbar.hor_draw = 0;
            }
            if(page_ext->scrlbar.ver_draw) {
                lv_area_copy(&sb_area_tmp, &page_ext->scrlbar.ver_area);
                sb_area_tmp.x1 += page->coords.x1;
                sb_area_tmp.y1 += page->coords.y1;
                sb_area_tmp.x2 += page->coords.x1;
                sb_area_tmp.y2 += page->coords.y1;
                lv_obj_invalidate_area(page, &sb_area_tmp);
                page_ext->scrlbar.ver_draw = 0;
            }
        }
    }
    else if(sign == LV_SIGNAL_CLEANUP) {
        page_ext->scrl = NULL;

    }
    return res;
}

page的重定位

page有个特点会根据子对象大小而扩展。当page移动时,ext->scrl坐标超出page的空间时会被重定位。保证不会因为子对象移动而改变大小。

static void scrl_reposition(lv_obj_t * page)
{
    /*Limit the position of the scrollable object to be always visible
     * (Do not let its edge inner then its parent respective edge)*/
    lv_obj_t * scrl = lv_page_get_scrollable(page);
    lv_coord_t new_x = lv_obj_get_x(scrl);
    lv_coord_t new_y = lv_obj_get_y(scrl);
    bool refr_x      = false;
    bool refr_y      = false;
    lv_area_t page_coords;
    lv_area_t scrl_coords;
    lv_obj_get_coords(scrl, &scrl_coords);
    lv_obj_get_coords(page, &page_coords);

    lv_style_int_t left = lv_obj_get_style_pad_left(page, LV_PAGE_PART_BG);
    lv_style_int_t right = lv_obj_get_style_pad_right(page, LV_PAGE_PART_BG);
    lv_style_int_t top = lv_obj_get_style_pad_top(page, LV_PAGE_PART_BG);
    lv_style_int_t bottom = lv_obj_get_style_pad_bottom(page, LV_PAGE_PART_BG);

    /*scrollable width smaller then page width? -> align to left*/
    if(lv_area_get_width(&scrl_coords) + left + right <= lv_area_get_width(&page_coords)) {
        if(scrl_coords.x1 != page_coords.x1 + left) {
            new_x  = left;
            refr_x = true;
        }
    }
    else {
        /*The edges of the scrollable can not be in the page (minus hpad) */
        if(scrl_coords.x2 < page_coords.x2 - right) {
            new_x = lv_area_get_width(&page_coords) - lv_area_get_width(&scrl_coords) - right; /* Right align */
            refr_x = true;
            lv_page_start_edge_flash(page, LV_PAGE_EDGE_RIGHT);
        }
        else if(scrl_coords.x1 > page_coords.x1 + left) {
            new_x  = left; /*Left align*/
            refr_x = true;
            lv_page_start_edge_flash(page, LV_PAGE_EDGE_LEFT);
        }
    }

    /*scrollable height smaller then page height? -> align to top*/
    if(lv_area_get_height(&scrl_coords) + top + bottom <= lv_area_get_height(&page_coords)) {
        if(scrl_coords.y1 != page_coords.y1 + top) {
            new_y  = top;
            refr_y = true;
        }
    }
    else {
        /*The edges of the scrollable can not be in the page (minus vpad) */
        if(scrl_coords.y2 < page_coords.y2 - bottom) {
            new_y = lv_area_get_height(&page_coords) - lv_area_get_height(&scrl_coords) - bottom; /* Bottom align */
            refr_y = true;
            lv_page_start_edge_flash(page, LV_PAGE_EDGE_BOTTOM);
        }
        else if(scrl_coords.y1 > page_coords.y1 + top) {
            new_y  = top; /*Top align*/
            refr_y = true;
            lv_page_start_edge_flash(page, LV_PAGE_EDGE_TOP);
        }
    }

    if(refr_x || refr_y) {
        lv_obj_set_pos(scrl, new_x, new_y);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值