make a simple button and click for counter ++..



  • /*
    unlucky ... its not working as I expect,
    and I cant find any doc about this, any help?
    Thanks!!!
    */

    #include <mono.h>
    mono::ui::TextLabelView textLbl(mono::geo::Rect(0,20,176,20),"Hi, I'm Mono");
    mono::ui::ButtonView btn(mono::geo::Rect(20, 155, 136, 40),"Click me!");

    int counter;

    void btnClick() {
    counter++;
    textLbl.setText(mono::String::Format("count: %i", counter));
    }

    void setup() {
    btn.setClickCallback(btnClick);
    textLbl.show();
    btn.show();
    }

    void loop() {
    }



  • @tctc said in make a simple button and click for counter ++..:

    The issue is this line of code:

    btn.setClickCallback(btnClick);

    Unfortunately you can't provide C functions as callbacks to ButtonView. I will need to add this in a coming release. Until then you must define you callback as a method on a class. See this guide:

    http://developer.openmono.com/en/latest/tutorials/adding_a_button_to_the_sceen.html

    The guide does not use the Arduino IDE, but you are able to define your own class in Arduino, instantiate it and setup a callback to a method on that class:

    class MyClass
    {
    public:
        void myCallback()
        {
            printf("Yay, callback did execute!\t\n");
        }
    };
    
    MyClass myInst;
    
    void setup()
    {
        btn.setCallback<MyClass>(&myInst, &MyClass::MyCallback);
    }
    

    (This code is only for reference, I have not checked it for typos)



  • @stoffera
    thanks! its working after some try&error, but the btn.show() didn't draw anything on screen. But its still working (count +1 ) when i click on the blank area.. code below:

    #include <mono.h>
    mono::ui::TextLabelView textLbl(mono::geo::Rect(0,20,176,20),"Hi, I'm Mono");
    mono::ui::ButtonView btn(mono::geo::Rect(20, 175, 136, 40),"Click me");
    
    int loopItererations;
    int counter;
    
    class CallBackFunc{
      public:
      void btnClick() {
        counter++;
        textLbl.setText(mono::String::Format("count: %i", counter));
      }
    };
    
    CallBackFunc btnCallBack;
    
    void setup() {
      // put your setup code here, to run once:
      textLbl.setTextColor(mono::display::WhiteColor);   // 3
      textLbl.setAlignment(mono::ui::TextLabelView::ALIGN_CENTER);  
      textLbl.setTextColor(mono::display::AlizarinColor);
      textLbl.show();   // 4
      //
      btn.setClickCallback<CallBackFunc>(&btnCallBack, &CallBackFunc::btnClick);
      btn.show();
    }
    
    void loop() {
    }
    


  • @tctc Great it works. You cannot see the button because it has the same border coors as the background color.

    I admit it is a issue in our arduino environment that we will need to fix. But until then you should just change the default color schemes like this:

    void setup() {
      // change the default colors
      mono::ui::View::StandardBorderColor = mono::display::CloudsColor;
      mono::ui::View::StandardHighlightColor = mono::display::WhiteColor;
      mono::ui::View::StandardTextColor = mono::display::CloudsColor;
      mono::ui::View::StandardBackgroundColor = mono::display::BlackColor;
      
      // re-contruct the button to make use of new standard colors
      btn = mono::ui::ButtonView(mono::geo::Rect(20, 175, 136, 40),"Click me");
      
      // put your setup code here, to run once:
      textLbl.setTextColor(mono::display::WhiteColor);   // 3
      textLbl.setAlignment(mono::ui::TextLabelView::ALIGN_CENTER);  
      textLbl.setTextColor(mono::display::AlizarinColor);
      textLbl.show();   // 4
      //
      btn.setClickCallback<CallBackFunc>(&btnCallBack, &CallBackFunc::btnClick);
      btn.show();
    }
    


  • @stoffera thanks! solved!
    working no a volume control stuff, will share later on when finish! Thanks!


Log in to reply