Passing an array to a custom drawable

So I've made a custom drawable which I was hoping to use in layouts taking some parameters using <Param> one of which is a two dimensional array of points, I was hoping it would work the same way as how you can pass an array of coordinates to the polygon shape in a layout but it doesn't seem to, is there a way to do it? Otherwise I'll have to use a strong and parse it in the custom drawable initialise method but I assume that will add a fair bit of unnecessary overhead?
  • Former Member
    Former Member
    aledthomas79,

    I'm trying to figure out what you are trying to do based on your description. Could you provide some simplified code showing how you set up your custom drawables?

    Thanks,
    - Coleman
  • Ok, I'm not at my computer but on a phone so forgive my syntax

    The dc.fillpolygon(pts) function takes an array of coordinates (a 2-dimensional array).
    my custom drawable class starts off with a set of points to draw a polygon, and applies some calculation to them before calling fillpolygon(pts) with the resulting points to plot. It will be reused in a few places with different starting values, and also will need different starting points on different screen shapes/sizes, hence my wanting to specify the points in a layout.xml.


    we can already specify a <shape type=polygon> in a drawables-list in a layout, and pass an array of points for it through the XML layout file.
    e.g. <shape type=polygon points="[[1,1],[5,5],[1,5]]" >

    How can my custom drawable take an array of points to draw from a layout file in the same way?
    I tried
    <drawable class="mycustomdrawable">
    <param name="points">[[1,1],[5,5],[1,5]]</param>
    </drawable>

    But it doesn't seem to work like that, is there a way to do this?
    hope that makes some sense
  • This works fine for me. Here is my layout definition...

    <layout id="MainLayout">
    <drawable id="customDrawable" class="CustomDrawable">
    <param name="array">[ 1, 2, 3, 4 ]</param>
    <param name="matrix">[ [ 1, 2 ], [ 3, 4 ] ]</param>
    </drawable>
    </layout>


    This is my custom drawable class...

    using Toybox.WatchUi;
    using Toybox.System;

    class CustomDrawable extends WatchUi.Drawable
    {
    function initialize(params) {
    System.println(params[:array]);
    System.println(params[:matrix]);
    }
    }


    Here is the output I see on the console...

    [1, 2, 3, 4]
    [[1, 2], [3, 4]]
  • Ok thanks, probably I've missed something simple I'll have another look later, you're sure it's passing those as arrays not just as strings? I don't know how println formats arrays does it include the brackets and commas?
  • you're sure it's passing those as arrays not just as strings?

    Yes. I'm certain. I've just verified...

    using Toybox.WatchUi;
    using Toybox.System;

    class CustomDrawable extends WatchUi.Drawable
    {
    function initialize(params) {
    System.println(params[:array]);
    if (params[:array] instanceof Lang.Array) {
    System.println("It's an array silly.");
    }
    else {
    System.println("It is not an array! Argh!");
    }

    System.println(params[:matrix]);
    }
    }


    [1, 2, 3, 4]
    It's an array silly.
    [[1, 2], [3, 4]]


    The resource compiler takes whatever you put between the open and close param tags and inserts it literally into the code. The properties that I described in my first post would generate code something like this..

    var options = {
    :identity => "customDrawable",
    :array => [ 1, 2, 3, 4 ],
    :matrix => [ [ 1, 2 ], [ 3, 4 ] ]
    }
    var drawable = new CustomDrawable(options);

    // put drawable into the layout



    I don't know how println formats arrays does it include the brackets and commas?

    Yes. That is the output produced when printing the things that we just verified are arrays.

  • :D Ok that's great thanks man, I was using Params.get and I must have made a mistake somewhere, it's been a few years since I've written anything other than assembly and i still forget to put semicolons and quotes and simple things!
    the way you've done it there is much neater anyway so I'll re do it that way.
  • You get the same result if you use params.get(:array) or params[:array]. The thing that would be easy to mess up would be to use the name of the parameter as a string instead of a symbol...

    // this is what you want... the key is a symbol
    var value = params[:array];

    // that is very different from this, which will not work
    value = params["array"];