Git Product home page Git Product logo

Comments (6)

leidto avatar leidto commented on June 19, 2024 2

pct_to_px method is my friend, thank you both for your help!

from lvgl.

liamHowatt avatar liamHowatt commented on June 19, 2024 1

Ah you're right, it's always correct for pixel values.

@leidto you can do this

print('btn x value:', lv.pct_to_px(
    btn.get_x_aligned(),            # value
    btn.get_parent().get_width()    # base
))
btn x value: 42

42 is 10% of 420, the screen width.

from lvgl.

liamHowatt avatar liamHowatt commented on June 19, 2024

get_x_aligned grabs the style property for x position of the object while get_x does a bit more to give you the real pixel value. The reason get_x_aligned reports 536870922 is because percent values are represented as special large numbers.

I was able to get 192 with

btn.update_layout()
print('btn x value:', btn.get_x())

The update_layout is needed to calculate the real pixel location.

The docstring for get_x_aligned (lv_obj_get_x_aligned) is:

/**
 * Get the actually set x coordinate of object, i.e. the offset form the set alignment
 * @param obj       pointer to an object
 * @return          the set x coordinate
 */
int32_t lv_obj_get_x_aligned(const lv_obj_t * obj);

And the docstring for get_x (lv_obj_get_x) is:

/**
 * Get the x coordinate of object.
 * @param obj       pointer to an object
 * @return          distance of `obj` from the left side of its parent plus the parent's left padding
 * @note            The position of the object is recalculated only on the next redraw. To force coordinate recalculation
 *                  call `lv_obj_update_layout(obj)`.
 * @note            Zero return value means the object is on the left padding of the parent, and not on the left edge.
 * @note            Scrolling of the parent doesn't change the returned value.
 * @note            The returned value is always the distance from the parent even if `obj` is positioned by a layout.
 */
int32_t lv_obj_get_x(const lv_obj_t * obj);

from lvgl.

leidto avatar leidto commented on June 19, 2024

Interesting. The get_x_aligned method doesn't make much sense in this form. I'm looking for a method to get the aligned coordinates. In this case the 192 is incorrect, because is relative to the top left but I work with a centered button in this case. I'd like to get the aligned coordinates.

from lvgl.

liamHowatt avatar liamHowatt commented on June 19, 2024

Fair enough! In fact, get_x_aligned doesn't do what that docstring says.

@kisvegabor should this be fixed? If so, should it call get_x internally and then calculate the offset based on the currently set align?

from lvgl.

kisvegabor avatar kisvegabor commented on June 19, 2024

The docstring says

Get the actually set x coordinate of object, i.e. the offset form the set alignment

So if you set the x offset in pixels, it will return it. If you set %, it would return that. In fact, update_layout is not required as get_x_aligned reads the data from the styles directly.

int32_t lv_obj_get_x_aligned(const lv_obj_t * obj)
{
    return lv_obj_get_style_x(obj, LV_PART_MAIN);
}

from lvgl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.