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

Author Topic: Button effect  (Read 3375 times)

0 Members and 1 Guest are viewing this topic.

Poraft

  • Newbie
  • *
  • Posts: 24
    • View Profile
Button effect
« on: July 06, 2011, 09:13:09 pm »
Hello,

I'm trying to make a sort of button effect thingy. With 3 states; no-effect, hover-over and while pressing down. But I don't know how to get the third effect working properly.

Here is what I've got so far:
Code: [Select]

const sf::Input& Input = screen.GetInput();

if(Input.GetMouseX() > 300 && Input.GetMouseX() <  550 && // 300 550
Input.GetMouseY() > 300 && Input.GetMouseY() < 330 ) // 300 // 330
{

//hover over effect :D
buttonSprite.SetSubRect(sf::IntRect(0, 30, 250, 30));

if(Input.IsMouseButtonDown(sf::Mouse::Left))
{
//while clickingdown subrect effect
buttonSprite.SetSubRect(sf::IntRect(0, 0, 250, 30));
                       

                        // now there must be away that first clicking down on the button and then up to exit.
                        // but I cant figure it out, help plox:D
screen.Close();
}
}
else
{
//pak een stukje van de knop geen effecten
buttonSprite.SetSubRect(sf::IntRect(0, 0, 250, 30));
}


Maybe instead of the 2nd if-loop a while-loop, but then I couldn't end the while loop, because it didn't check for input anymore..

Help plz:D

Poraft

  • Newbie
  • *
  • Posts: 24
    • View Profile
Button effect
« Reply #1 on: July 07, 2011, 02:56:29 pm »
Anyone? & Sorry for the fact that I bumped too early;]

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Button effect
« Reply #2 on: July 07, 2011, 05:40:53 pm »
I would do something like :

initialize the sprite with :
Code: [Select]
sprite set subrect(rect corresponding to 'no-effect');

in the event processing loop :
Code: [Select]
while (anEvent) {
  if (mouse pressed) {
    if (mouse over area) {
      sprite set subrect(rect corresponding to 'pressing down');
    } else {
      nothing
    }
  } else if (mouse release) {
    if (mouse over area) {
      sprite set subrect(rect corresponding to 'no-effect');
      do action();
    } else {
      nothing
    }
  } else if (mouse moved) {
    if (mouse over area) {
      if (mouse down) {
        sprite set subrect(rect corresponding to 'pressing down');
      } else {
        sprite set subrect(rect corresponding to 'no-effect');
      }
    } else {
      sprite set subrect(rect corresponding to 'no-effect');
    }
  }


then simply drawing the sprite should be enough.

You can also use sf::Input but you'll need some boolean to keep track of pressed/released sate.
SFML / OS X developer

Poraft

  • Newbie
  • *
  • Posts: 24
    • View Profile
Button effect
« Reply #3 on: July 07, 2011, 06:14:38 pm »
Mmh, okay thanks i'll give that a try.

What's better first checking for a click and then for the position or visa versa?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Button effect
« Reply #4 on: July 07, 2011, 06:25:19 pm »
sorry, there was a mistake in my pseudo code, "&& mouse down" was not necessary because we already know that (we received a mouse pressed event).
SFML / OS X developer

Cuban-Pete

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Button effect
« Reply #5 on: July 07, 2011, 06:40:04 pm »
I do it this way:

check for a click with mouse and that it is released.

start a function that gets mouse position and button position.

the function checks if the mouse is in the button position and if so, it updates the button struct bool for highlight or the pressed down effect.

struct button{
 string text;
 float x;
 float y;
 float width;
 float height;
 bool active;
 bool pressed;
};

during the "drawing loop" a different function gets the button struct and displays the button with the effects.

something like that. :) I hope it helps a little.

Poraft

  • Newbie
  • *
  • Posts: 24
    • View Profile
Button effect
« Reply #6 on: July 07, 2011, 11:10:56 pm »
Oke thanks, making another bool did the trick :-]

 

anything