Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Efficient ways to create buttons  (Read 2414 times)

0 Members and 1 Guest are viewing this topic.

Alex.UA

  • Newbie
  • *
  • Posts: 1
    • View Profile
Efficient ways to create buttons
« on: June 24, 2024, 03:09:08 pm »
Hi, i`m new to sfml. Lately i was thinking about most efficient ways to create buttons, especially how to track clicks. I thought about two ways of making it. First: every button checks every tick is it hovered. If true then check for right mouse click event. Second is to check if my cursor is in zone of any button(or object).If true do something on click. So I`d like to know which of the ways better, or is there other efficient way before i move on

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Efficient ways to create buttons
« Reply #1 on: June 24, 2024, 06:57:00 pm »
Howdy! :)

I personally prefer making a button class.

Like so.





bool released;

class Button
{
        Sprite sprite;
        bool clickedleft=false;
        bool clickedright=false;
       
        bool active=false;
       
       
       
       
        void update(RenderWindow &window)
        {
               
                if(active)
                {
               
                        if(Mouse::isButtonPressed(Mouse::Left))
                        {
                                if(released)
                                {
                                        clickedleft=true;
                                }
                                released=false;
                               
                        }
                        if(Mouse::isButtonPressed(Mouse::Right))
                        {
                                if(released)
                                {
                                        clickedright=true;
                                }
                                released=false;
                               
                        }
                       
                       
                       
                       
                       
                        window.draw(sprite);
                }
               
                       
        }
};






int main()
{
       
        Texture texture;
        texture.loadFromFile("button.png");
       
       
        Button btn[10];
        btn[0].sprite.setTexture(texture);
        btn[0].sprite.setPosition(300,300);
        btn[0].active=true;
       
        RenderWindow window(VideoMode(960,540),"");
       
        while(window.isOpen())
        {
                Event e;
                while(window.pollEvent(e))
                {
                        if(e.type==Event::Closed)
                                window.close();
                }
               
               
                if(!Mouse::isButtonPressed(Mouse::Left)&&!Mouse::isButtonPressed(Mouse::Right))
                        released=true;
                       
               
               
                if(btn[0].clickedleft)
                {
                        btn[0].active=false;
                        cout<<"clickedLeft";
                }
               
               
               
               
               
               
               
               
                window.clear();
               
                for(int a=0;a<sizeof(btn);a++)
                {
                        if(btn.active)
                                btn.update(window);
                }
               
                window.display();
        }
       
       
       
}
 



This limits the checking for a click to one time and makes the button invisible when clicked.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Efficient ways to create buttons
« Reply #2 on: June 24, 2024, 09:02:43 pm »
Another way is to use events only as you can then only do something when you would expect to.

For example, check events to see if a mouse button press happened and then check if the mouse was inside the button. Similar work for mouse button release if you want it to activate on release.

If you also want the ability to hover, then - again with events - check to see if mouse was moved and then check if the mouse is now inside the button. If so, it's now hovering; if not, it's now not.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

hdsiria

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Efficient ways to create buttons
« Reply #3 on: June 26, 2024, 04:44:50 pm »
I think the first way is good, each button should judge the event for itself.

 

anything