Access variable calculated in OnTap-Event for sizing in drawing shapes?

Hello,

I would like to increase the size of a shape (e.g. circle, rectangle) with a OnTap-Event.

I use class InputDelegate and function OnTap(). I defined a variable i and increase with i=i+1 within this function. Everything within MyInputDelegate.mc file

The drawings I understood have to be in the function OnUpdate() in the separate file XYview.mc

There I would like to use a kind of  dc.fillCircle(50, 50, i);

How can I got access to the i-Variable across different files?

A small example would be very helpful. Seems to be very easy from the task, but quite hard to realize.

Thanks!

  • The quickest way to do this is to not define "i" in a class, but make it a global.

    Or, you define "i" in your view, and pass the view to your delegate.  Then in the delegate, you have access to "myView.i"

    (there is a bit more to passing the view to the delegate)

  • Here's really simple app that changes the size when the use taps the screen on a touch device, or presses the upper right button on a non touch device.  (I use onSelect() instead of onTap() so it handles both).

    The view:

    import Toybox.Graphics;
    import Toybox.WatchUi;
    
    class growView extends WatchUi.View {
    	var width,height,curRad=0,maxRad;
    	
        function initialize() {
            View.initialize();
        }
    
        // Load your resources here
        function onLayout(dc as Dc) as Void {
    		width=dc.getWidth();
    		height=dc.getHeight();
    		maxRad=width/2-5;
        }
    
        // Called when this View is brought to the foreground. Restore
        // the state of this View and prepare it to be shown. This includes
        // loading resources into memory.
        function onShow() as Void {
        }
    
        // Update the view
        function onUpdate(dc as Dc) as Void {
    		dc.setColor(Graphics.COLOR_BLACK,Graphics.COLOR_BLACK);
    		dc.clear();
    		dc.setColor(Graphics.COLOR_WHITE,Graphics.COLOR_TRANSPARENT);
    		dc.setPenWidth(3);
    		dc.drawCircle(width/2,height/2,curRad);
        }
    
        // Called when this View is removed from the screen. Save the
        // state of this View here. This includes freeing resources from
        // memory.
        function onHide() as Void {
        }
    
    }
    

    The delegate:

    import Toybox.Lang;
    import Toybox.WatchUi;
    
    class growDelegate extends WatchUi.BehaviorDelegate {
    	var view;
    	
        function initialize(v) {
            BehaviorDelegate.initialize();
        	view=v;
        }
    
        function onSelect() as Boolean {
            view.curRad=(view.curRad+10)%view.maxRad;
            if(view.curRad<10) {view.curRad=0;}
            WatchUi.requestUpdate();
            return true;
        }
    }

    the AppBase:

    import Toybox.Application;
    import Toybox.Lang;
    import Toybox.WatchUi;
    
    class growApp extends Application.AppBase {
    
        function initialize() {
            AppBase.initialize();
        }
    
        // onStart() is called on application start up
        function onStart(state as Dictionary?) as Void {
        }
    
        // onStop() is called when your application is exiting
        function onStop(state as Dictionary?) as Void {
        }
    
        // Return the initial view of your application here
        function getInitialView() as Array<Views or InputDelegates>? {
        	var view=new growView();
            return [ view, new growDelegate(view) ] as Array<Views or InputDelegates>;
        }

    Notice what I do in getInitalView, where I pass the view to the delegate, and in initialize for the delegate to use the view.