Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1206

Like
0Likes
Like

Posted 19 May 2013 - 05:14 AM

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.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9603

Like
2Likes
Like

Posted 19 May 2013 - 06:29 AM

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.

#3 AllEightUp   Moderators   -  Reputation: 4216

Like
2Likes
Like

Posted 19 May 2013 - 11:01 AM

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 AllEightUp, 19 May 2013 - 11:02 AM.


#4 BaneTrapper   Members   -  Reputation: 1206

Like
0Likes
Like

Posted 20 May 2013 - 10:07 AM

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!


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS