1
Graphics / Re: Having trouble making clickable menu buttons. Failing hard.
« on: March 03, 2014, 02:01:58 am »left and top are not functionsdon't be mad bbz :c
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
left and top are not functionsdon't be mad bbz :c
Try assignments.
term does not evaluate to a function taking 1 argumentsWhich term? Which function is being called?IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function typeWhich expression? Where in the code does this happen?
//Inside of the Button Class's constructor function
buttonSize = size;
rectShape.setSize(buttonSize);
rectShape.setFillColor(color);
rectShape.setPosition(position);
rectBoundingBox.left(20); //<----------------------------
rectBoundingBox.top(20); //<------------------------------------
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);
}
#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();
}
}
You'll want to use code tags:Yup. I did have a line that got my mouse's position and stored it into a vector2f, but I accidently deleted it .[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.
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));
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.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".