How do i make a simple button?



  • Hi!

    I'm a fairly inexperienced C++ programmer but i do understand the basic concepts. All i want to do is create a simple program with a button in the middle of the screen, that somehow responds when you press it. I've taken inspiration from the Tic Tac Toe program, and there should be a box in the middle of the screen, that will draw a circle in itself when pressed. As of right now, the program compiles and my text shows, but no box is drawn on the screen.
    Could anybody please point out to me why my code doesn't work?

    Here is app_controller.h:
    https://gist.github.com/anonymous/4bdf0f8d65aafd9b4e9f630e06d0ad1d

    And here is the cpp:
    https://gist.github.com/anonymous/122d8bcc4cb1e54d5514d67c8df1827f



  • You never call show() on the field object. All views are invisible by default, you must call show() on them, when you are ready to display them.

    That said, here is my tutorial on how to do what you want to achieve: http://developer.openmono.com/en/latest/tutorials/adding_a_button_to_the_sceen.html

    Also, it is best practice to not call painter routines outside your views repaint method, since you risk seeing artifacts due to vsync problems.



  • It works! Thank you very much. If i were to avoid the painter call, should i make a new subclass of view consisting of a circle, and call its show function when the square is pressed?
    Despite browsing the openmono.com page quite a bit i haven't come across that tutorial. I will surely take a look at it.



  • @gulle_g said in How do i make a simple button?:

    should i make a new subclass of view consisting of a circle, and call its show function when the square is pressed?

    No, I think this would be too cumbersome. Simply use a flag that will determine if you should paint the circle in the repaint method. Add a bool named circleFlag to your TouchField class and:

    void TouchField::TouchBegin (TouchEvent & event)
    {
         this->circleFlag = true;
         this->scheduleRepaint();
    }
    
    void TouchField::repaint ()
    {
    	// Clear background.
    	painter.drawFillRect(viewRect,true);
    	// Show box around touch area.
    	painter.drawRect(viewRect);
    
           if (this->circleFlag)
           {
                  uint16_t radius = viewRect.Size().Width() / 2;
                  painter.drawCircle(viewRect.X()+radius,viewRect.Y()+radius,radius);
           }
    }
    

Log in to reply