Jump to content
  • Advertisement
Sign in to follow this  
BaneTrapper

A function of class that resets bool of class #more info inside

This topic is 1951 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.

I am bad at explaining things so i will give my best.

 

Required:

To have a class looking like this

class Button

{

public:

    Button();

    bool isPressed();

    bool isReleased();

    void Pressed();//

    void Released();

private:

    bool isKBP;//is Key Button Pressed

    bool isKBR;// is key button pressed

};

 

And at end of each loop i want to call a function

int main()

{

while()

{
//Logic

//...
//Display
//...
 
//End of loop
ResetButtons();
}

}

ResetButtons(); function would have to go true each object of "Button" class and set "isKBP = false"

Any sugestions or ideas how i would acomplish this?

 

What i would prefear is not to thnink about "Button" class object, there are gonna be many of those all around the code.

Share this post


Link to post
Share on other sites
Advertisement
One option is when you create your buttons, stick a (smart) pointer to each button into a container. When you want to reset each button, iterate over the container and set each button state in that loop.

Share this post


Link to post
Share on other sites
Guest Hiwas

The only thing I can think of right now (pre-coffee wake up smile.png) for having the two different bools is that you are attempting to make sure that the mouse cursor is still over the button when released?  I.e. the button is only pressed if both pressed was true when released became true?  I tend to move such logic out of the UI elements and hook it to a concept in the input processing such that other items like double click can be dealt with also.  I.e. something like the following:

 

class UIManager
{
public:
  ...
 
  OnPressed( int x, int y )
  {
     mPressedItem = GetItemAt( x, y );  // Get an ID for the item.
     SetPressed( mOverItem ); // If over anything at all, set it pressed.
  }
  OnRelease( int x, int y )
  {
    if( mPressedItem == GetItemAt( x, y ) ) // Only valid if released while over the same item.
      .. Clear item, send the "released" event.
  }
};

 

In this way, you simplify all your UI widget code and put all the potentially complex logic in a single location.  You can also extend this to do hover, tooltips, double click etc very simply without having to add special code to each of your widget types.  And of course, no need to clean up each widget every loop. smile.png

Edited by Hiwas

Share this post


Link to post
Share on other sites

The only thing I can think of right now (pre-coffee wake up smile.png) for having the two different bools is that you are attempting to make sure that the mouse cursor is still over the button when released?  I.e. the button is only pressed if both pressed was true when released became true?  I tend to move such logic out of the UI elements and hook it to a concept in the input processing such that other items like double click can be dealt with also.  I.e. something like the following:

 

 

class UIManager
{
public:
  ...
 
  OnPressed( int x, int y )
  {
     mPressedItem = GetItemAt( x, y );  // Get an ID for the item.
     SetPressed( mOverItem ); // If over anything at all, set it pressed.
  }
  OnRelease( int x, int y )
  {
    if( mPressedItem == GetItemAt( x, y ) ) // Only valid if released while over the same item.
      .. Clear item, send the "released" event.
  }
};

 

In this way, you simplify all your UI widget code and put all the potentially complex logic in a single location.  You can also extend this to do hover, tooltips, double click etc very simply without having to add special code to each of your widget types.  And of course, no need to clean up each widget every loop. smile.png

Interesting concept, but it differs massively from my implementation atm.

 

 

One option is when you create your buttons, stick a (smart) pointer to each button into a container. When you want to reset each button, iterate over the container and set each button state in that loop.

This is what i am gonna do.

Overthinking got me nowhere, thanks for help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!