Drawable.setLocation not working?

Former Member
Former Member
How do I set the location of a drawable?

I have a square:

<resources>
<drawable-list id="square" background="Gfx.COLOR_BLUE">
<shape type="rectangle" width="72" height="72" color="Gfx.COLOR_PURPLE" corner_radius="2" />
</drawable-list>
</resources>


I tried to display it one my watch face at location x=25, y=76.

Both of the following did not make it happen:

var square = new Rez.Rez.Drawables.square({"locX" => 25, "locY" => 76});
square.draw(dc);


var square = new Rez.Rez.Drawables.square();
square.setLocation(25, 76);
square.draw(dc);


Some help would be appreciated :)

N.B. I don't want to specify the location in the resource file, as I want to be able to draw the shape at different locations.
  • If you build from the command line with the {{-g}} compile flag, the compiler will generate some pseudo-assembly. You can see the initialize method doesn't read any properties...

    globals_Rez_Drawables_square_initialize:
    incsp 0
    Rez_17_24_start:
    Rez_17_24_stop:
    return
    globals_Rez_Drawables_square_initialize_end:


    The draw() method simply gets the position and size of the dc and passes that to drawComplex, which sets the foreground/background colors to transparent/blue, draws a rectangle, then sets the foreground/background colors to purple/transparent and draws another rectangle using the values hard-coded in your resource...

    # dc.setColor(Gfx.COLOR_PURPLE,Gfx.COLOR_TRANSPARENT);
    Rez_32_0:
    lgetv 1
    spush setColor
    getv
    frpush
    spush Toybox_Graphics
    getm
    spush COLOR_PURPLE
    getv
    spush Toybox_Graphics
    getm
    spush COLOR_TRANSPARENT
    getv
    invokem 3
    popv
    # dc.fillRoundedRectangle(((pX+0)+0)+0,((pY+0)+0)+0,72-(2*(0)),72-(2*(0)),2);
    Rez_33_0:
    lgetv 1
    spush fillRoundedRectangle
    getv
    frpush
    lgetv 2
    ipush 0
    addv
    ipush 0
    addv
    ipush 0
    addv
    lgetv 3
    ipush 0
    addv
    ipush 0
    addv
    ipush 0
    addv
    ipush 72
    ipush 2
    ipush 0
    mulv
    subv
    ipush 72
    ipush 2
    ipush 0
    mulv
    subv
    ipush 2
    invokem 6
    popv
    Rez_28_52_stop:
    return
    globals_Rez_Drawables_square_drawComplex_end:


    As far as I can see, there is nothing you can do to move a drawables list.
  • Former Member
    Former Member over 10 years ago
    Thanks for your reply.
    I haven't gotten around to dive into the assembly yet, but I'd expect the pX and pY parameter should be manipulated some way?

    If this kind off thing isn't possible, there's not much use for specifying shapes in resources. I'd like to use resources for drawing, but every time I try, I keep bumping into problems or limitations and end up coding the UI :S
    Is this your experience as well?
  • It seems that for this your best bet is to create a drawable class outside of the resource system. Unfortunately, specifying attributes of that class in the resource file becomes a huge hack.
  • Former Member
    Former Member over 10 years ago
    The best way to do this at the moment is to create a Square class which extends Drawable. You can then use the drawable attributes "locX" and "locY" to set the location when calling the constructor.

    As mentioned in this thread, the drawable-list and shape definitions were implemented before a Drawable object existed and that's why you aren't seeing the results you're expecting. This is on our list of things to fix.
  • Former Member
    Former Member over 10 years ago
    Yes, thanks. That's indeed how I solved it. I hope some improvements will be made to the layout system. Travis made some great suggestions in the thread you mentioned.
  • Yeah, I've bumped into problems with it a few times. It can be frustrating, but then I have to tell myself that I'm just doing this for fun...

    Travis