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

Author Topic: Having trouble making clickable menu buttons. Failing hard.  (Read 8733 times)

0 Members and 1 Guest are viewing this topic.

stellarpie

  • Newbie
  • *
  • Posts: 9
    • View Profile
Having trouble making clickable menu buttons. Failing hard.
« on: March 01, 2014, 02:08:40 am »
[see next post]
« Last Edit: March 01, 2014, 02:15:03 am by stellarpie »

stellarpie

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #1 on: March 01, 2014, 02:14:12 am »
       
Code: [Select]
sf::Vector2f rect1coord(35,200);
sf::Vector2f rect1size(120,50);
sf::Vector2f rect1coord2(155, 250);

sf::Vector2f rect2coord(35,280);
sf::Vector2f rect2size(120,50);

sf::Vector2f rect3coord(35,360);
sf::Vector2f rect3size(120,50);

sf::FloatRect rect1(rect1coord, rect1coord2);
sf::FloatRect rect2(rect2coord,(rect2coord + rect2size));
sf::FloatRect rect3(rect3coord,(rect3coord + rect3size));

Okay, so above I made a bunch of vector2fs and plugging it into Float Rects, as you can see above, and Also using for RectangleShape below. (I have 3 of them for 3 different buttons.)
Code: [Select]
        sf::RectangleShape rectangle1(rect1size);
rectangle1.setFillColor(sf::Color(0,0,0));
rectangle1.setOutlineThickness(10.0f);
rectangle1.setOutlineColor(sf::Color(255,105,180));
rectangle1.move(rect1coord);
Here's the output that I want for clicking one of the buttons.
Code: [Select]
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Left)
{
if(rect1.contains(Mousepos.x,Mousepos.y))
{
cout << "You Picked Easy Mode\n\n" << endl;
}
if(rect2.contains(Mousepos.x,Mousepos.y))
{
cout << "You Picked Medium Mode\n\n" << endl;
}
if(rect3.contains(Mousepos.x,Mousepos.y))
{
cout << "You Picked Hard Mode\n\n" << endl;
}
Basically, If I put (0,0,100,100) into rect1, (using no vector2f) It obviously makes a clickable 100 by 100 pixel area. I can click it and it will cout "You picked easy mode".

but If I put like anything higher than 0 in the first xand y coordinate, or if I use the vector2f's its not working. I can't figure it out :/


[Sorry I don't know what the heck happened, it said I already posted when I didnt.)
« Last Edit: March 01, 2014, 04:17:22 am by stellarpie »

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #2 on: March 01, 2014, 02:59:12 am »
You'll want to use code tags:
[code=cpp]

Also, where do you set Mousepos? My guess is you're forgetting to get the mouse position relative to the window. Like this:
boundingBox.contains(sf::Vector2f(sf::Mouse::getPosition(window)))
 

It also wouldn't be a bad idea to wrap the functionality of a button into a class, so your code is simpler to read and it's easier to make more than one button.
« Last Edit: March 01, 2014, 03:00:56 am by dabbertorres »

stellarpie

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #3 on: March 01, 2014, 04:19:13 am »
You'll want to use code tags:
[code=cpp]

Also, where do you set Mousepos? My guess is you're forgetting to get the mouse position relative to the window. Like this:
boundingBox.contains(sf::Vector2f(sf::Mouse::getPosition(window)))
 

It also wouldn't be a bad idea to wrap the functionality of a button into a class, so your code is simpler to read and it's easier to make more than one button.
Yup. I did have a line that got my mouse's position and stored it into a vector2f, but I accidently deleted it :(.
I might try making this into a class, that will give me something good to work on, as I'm still not that great with classes, and they're important in c++ lol :P

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #4 on: March 01, 2014, 04:53:25 pm »
Like this:
boundingBox.contains(sf::Vector2f(sf::Mouse::getPosition(window)))
Don't just cast the vector, it only works in the most simple case with the default view. In general, use the sf::RenderWindow::mapPixelToCoords() method.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #5 on: March 01, 2014, 07:28:09 pm »
BTW, instead of initializing yourself rect1, you could use rectangle1.getGlobalBounds(). Same for rect2 and rect3.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #6 on: March 01, 2014, 09:11:37 pm »
The sizes of rect2 and rect3 shouldn't be added to their positions.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #7 on: March 01, 2014, 09:15:22 pm »
Indeed, the second argument of sf::FloatRect should be its size. :o

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #8 on: March 01, 2014, 11:19:26 pm »
It confused me at first. I was always used to specifying the top-left coordinates and the bottom-right ones. It looks like that has happened here instead of specifying the sizes directly.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #9 on: March 01, 2014, 11:35:17 pm »
Like this:
boundingBox.contains(sf::Vector2f(sf::Mouse::getPosition(window)))
Don't just cast the vector, it only works in the most simple case with the default view. In general, use the sf::RenderWindow::mapPixelToCoords() method.
Oh. I checked the code for the functions on github, I can see why, thanks.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #10 on: March 01, 2014, 11:42:06 pm »
You might consider using:
rectangle1.setPosition(rect1coord);
rather than:
rectangle1.move(rect1coord);
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

stellarpie

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #11 on: March 02, 2014, 11:37:08 pm »
Okay, So I've made considerable changes to le code. I made a class called Button to, well you know, make buttons. Here is the code currently.

Code: [Select]
#include <iostream>
#include <string>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>


using namespace std;

class Button{
public:
Button(sf::Vector2f size,sf::Vector2f position, sf::Color color)
{
buttonSize = size;
rectShape.setSize(buttonSize);
rectShape.setFillColor(color);
rectShape.setPosition(position);
}
sf::Vector2f getButtonSize()
{
return buttonSize;
}
sf::RectangleShape getRectShape()
{
return rectShape;
}
private:
sf::Vector2f buttonSize;
sf::RectangleShape rectShape;
sf::FloatRect rectBoundingBox;

};

int main()

{
sf::Vector2f defaultButtonSize(120,50);

Button button1(defaultButtonSize,sf::Vector2f(35,200), sf::Color::Red);
Button button2(defaultButtonSize,sf::Vector2f(35,280), sf::Color::Blue);
Button button3(defaultButtonSize,sf::Vector2f(35,360), sf::Color::Cyan);

sf::ContextSettings settings;
settings.antialiasingLevel = 8;

sf::RenderWindow window(sf::VideoMode(800,600), "My window.");
sf::Music music;
if(!music.openFromFile("sound.wav"))
std::cout << "failed to open sound.wav" << std::endl;
music.play();

sf::Font font;
if(!font.loadFromFile("sansation.ttf"))
std::cout << "failed to open sansation.tff" << std::endl;

sf::Text text("Welcome to click the boxes!", font, 50);
sf::Text text2("Would you like easy, medium,\n or hard mode?\n\n\n\t\tEasy\n\n\n\t\tMedium\n\n\n\t\tHard", font, 25);
text2.move(0,100);

while (window.isOpen())
{
sf::Vector2i Mousepos;

sf::Event event;
while(window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Left)
{

}
}
}
window.clear();

window.draw(button1.getRectShape());
window.draw(button2.getRectShape());
window.draw(button3.getRectShape());
window.draw(text);
window.draw(text2);

window.display();
}
}

My current problem is in my Button class. I need to give rectBoundingBox it's size and position (which needs to be identical to the sf::RectangleShape rectShape)

I see that I can do rectBoundingBox.left, top, width and height. But I'm not sure what left coordinate and top coordinate means... and I also get an error. :P
« Last Edit: March 02, 2014, 11:38:56 pm by stellarpie »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #12 on: March 03, 2014, 12:14:00 am »
I need to give rectBoundingBox it's size and position (which needs to be identical to the sf::RectangleShape rectShape)

I see that I can do rectBoundingBox.left, top, width and height. But I'm not sure what left coordinate and top coordinate means... and I also get an error. :P
Left and top would be the position of the top-left of the box; width and height would be the size of the box.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #13 on: March 03, 2014, 12:14:41 am »
What is your error ? ...
Left and top should be the x and y coordinates of the top left corner of your bounding box. If the origin of your rectangle shape is its top left corner then it's easy... Anyway, my first answer on this thread.

stellarpie

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Having trouble making clickable menu buttons. Failing hard.
« Reply #14 on: March 03, 2014, 12:28:29 am »
Error   1   error C2064: term does not evaluate to a function taking 1 arguments
   3   IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type   

Code: [Select]
class Button{
public:
Button(sf::Vector2f size,sf::Vector2f position, sf::Color color)
{
buttonSize = size;
rectShape.setSize(buttonSize);
rectShape.setFillColor(color);
rectShape.setPosition(position);
rectBoundingBox.left(20);
rectBoundingBox.top(20);


}