LOCATION

how can i locate function the same way you locate a text?

example

i locate this

function drawRec(dc) {
    dc.setColor(aColor, Gfx.COLOR_TRANSPARENT);

    dc.fillRectangle(0, 100, 120, 4);
}

function onLayout(dc) {
    View.setLayout(Rez.Layouts.MainLayout(dc));

    var valueView = View.findDrawableById("value");
    valueView.locY = valueView.locY + 14;
}

function onUpdate(dc) {
    value.setText(lastPower);
}

or on complex template how do i send a rectangle back to the text

using Toybox.WatchUi;
using Toybox.Graphics;

class ${viewClassName} extends WatchUi.DataField {

    hidden var mValue;

    function initialize() {
        DataField.initialize();
        mValue = 0.0f;
    }

    // Set your layout here. Anytime the size of obscurity of
    // the draw context is changed this will be called.
    function onLayout(dc) {
        var obscurityFlags = DataField.getObscurityFlags();

        // Top left quadrant so we'll use the top left layout
        if (obscurityFlags == (OBSCURE_TOP | OBSCURE_LEFT)) {
            View.setLayout(Rez.Layouts.TopLeftLayout(dc));

        // Top right quadrant so we'll use the top right layout
        } else if (obscurityFlags == (OBSCURE_TOP | OBSCURE_RIGHT)) {
            View.setLayout(Rez.Layouts.TopRightLayout(dc));

        // Bottom left quadrant so we'll use the bottom left layout
        } else if (obscurityFlags == (OBSCURE_BOTTOM | OBSCURE_LEFT)) {
            View.setLayout(Rez.Layouts.BottomLeftLayout(dc));

        // Bottom right quadrant so we'll use the bottom right layout
        } else if (obscurityFlags == (OBSCURE_BOTTOM | OBSCURE_RIGHT)) {
            View.setLayout(Rez.Layouts.BottomRightLayout(dc));

        // Use the generic, centered layout
        } else {
            View.setLayout(Rez.Layouts.MainLayout(dc));
            var labelView = View.findDrawableById("label");
            labelView.locY = labelView.locY - 16;
            var valueView = View.findDrawableById("value");
            valueView.locY = valueView.locY + 7;
        }

        View.findDrawableById("label").setText(Rez.Strings.label);
        return true;
    }

    // The given info object contains all the current workout information.
    // Calculate a value and save it locally in this method.
    // Note that compute() and onUpdate() are asynchronous, and there is no
    // guarantee that compute() will be called before onUpdate().
    function compute(info) {
        // See Activity.Info in the documentation for available information.
        if(info has :currentHeartRate){
            if(info.currentHeartRate != null){
                mValue = info.currentHeartRate;
            } else {
                mValue = 0.0f;
            }
        }
    }

    // Display the value you computed here. This will be called
    // once a second when the data field is visible.
    function onUpdate(dc) {
        // Set the background color
        View.findDrawableById("Background").setColor(getBackgroundColor());

        // Set the foreground color and value
        var value = View.findDrawableById("value");
        if (getBackgroundColor() == Graphics.COLOR_BLACK) {
            value.setColor(Graphics.COLOR_WHITE);
        } else {
            value.setColor(Graphics.COLOR_BLACK);
        }
        value.setText(mValue.format("%.2f"));

        // Call parent's onUpdate(dc) to redraw the layout
        View.onUpdate(dc);

        dc.fillRectangle(0, 100, 120, 4);
    }

}

  • First off, that second code snippet seems to indicate that you're modifying the code templates that ship with the SDK. I'm not sure that you want to be doing this.

    As for your questions, I'm not really sure I understand what you're asking.

    In the first code snippet it looks like you are updating the value of a text field (the variable named valueView should probably be a member variable, and you should be updating it in onUpdate). This is fine, but you aren't actually drawing anything. Since you are using a layout, you just need to call View.onUpdate() at the end of your onUpdate function and the text will be drawn.

    It seems that the second code snippet is your attempt to draw a black rectangle over the top of whatever was drawn from the layout. The only issue that I see with that code is that you're not explicitly setting the draw colors just before drawing the rectangle. If the View.onUpdate() call changes the draw colors (it most certainly will), the rectangle will be in whatever color was most recently set.

  • thanks a lot travis

    this is the template layout order so i want to knok how can i put the view.onUpdate(dc); behind the values

    <layouts>

        <!-- A generic, centered layout. -->

        <layout id="MainLayout">

            <drawable class="Background" />

            <label id="label" x="center" y="center" color="Graphics.COLOR_BLACK" justification="Graphics.TEXT_JUSTIFY_CENTER" font="Graphics.FONT_SMALL" />

            <label id="value" x="center" y="center" color="Graphics.COLOR_BLACK" justification="Graphics.TEXT_JUSTIFY_CENTER" font="Graphics.FONT_NUMBER_MEDIUM" />

        </layout>

    </layouts>

  • this is the repository a also insert a drawable with the samples example but it´s also in front

    github.com/.../marathon

  • You've put the background drawing and the value drawing into the layout so the system will automatically display them all in the order specified in the xml. If you want to draw other things above the background but below the label and value, you either need to insert more drawables/labels into the layout, or you need to draw the layout elements yourself and do the dc.draw... calls after the background but before the label. 

  • thanks travis now i change the layout to background and it´s working