Skip to main content
gamedev.net
🔒 Locked

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

Started by BaneTrapper 2 replies 1k views
Original Post
BaneTrapper
BaneTrapper

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.

SiCrane
SiCrane
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.
BaneTrapper
BaneTrapper

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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.