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

Author Topic: Sprite button  (Read 3243 times)

0 Members and 1 Guest are viewing this topic.

bucknasty189

  • Newbie
  • *
  • Posts: 23
    • View Profile
Sprite button
« on: April 12, 2011, 02:52:12 am »
I've had multiple problems using cpGUI in my engine so I've decided to just scrap it and do my own gui since I only really need buttons.

Code: [Select]

const sf::Input& theInput = m_Game->m_Window.GetInput();
bool mouseButtonReleased = theInput.IsMouseButtonDown(sf::Mouse::Right);

// Convert local window input coordinates to game coordinates
sf::Vector2f mousePos = m_Game->m_Window.ConvertCoords(m_Game->m_Window.GetInput().GetMouseX(), m_Game->m_Window.GetInput().GetMouseY());

if(mouseButtonReleased && m_Start->GetSubRect().Contains(mousePos.x, mousePos.y)){
mouseButtonReleased = false;
// m_Game->PushState(new OptionsState(m_Game));
cout << "BUTTON PUSHED!!!!!" << endl;
}


Can someone tell my why this doesn't work as a button? Maybe I don't understand subrect....
Knowledge Speaks,
Wisdom Listens
                       -JH

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Sprite button
« Reply #1 on: April 12, 2011, 03:09:18 am »
You should show us a minimal, compilable example of your problem that doesn't load images from files so people can help you more easily.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Sprite button
« Reply #2 on: April 12, 2011, 07:40:47 am »
There are tons of resources about collision detection between mouse and sprites, here on the forum and on the wiki.

Your problem is that the subrect of a sprite is in local coordinates (ie. it doesn't take the sprite's position/rotation/scale in account) while the mouse cursor is in global coordinates. You need to convert one to match the other (mouse is easier, since it's just a point).
Laurent Gomila - SFML developer

bucknasty189

  • Newbie
  • *
  • Posts: 23
    • View Profile
Sprite button
« Reply #3 on: April 13, 2011, 11:56:35 pm »
I'm finding out that the default subrect does not change with the moving of the sprite. Please look at this compilable code. The mouse is contained within the sprite at the top left corner of the screen where it would be before the position was set. I left the 'IN BUTTON' cout statement to show what I mean.

Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

int main()
{
sf::RenderWindow App(sf::VideoMode(1024,768,32),"Laraus", sf::Style::Close | sf::Style::Resize, sf::WindowSettings());

sf::Image button;
button.LoadFromFile("../Datas/paddle_left.png");
sf::Sprite Start(button);
Start.SetPosition(200,200);
sf::IntRect StartButton(Start.GetSubRect().Left, Start.GetSubRect().Top, Start.GetSubRect().Right, Start.GetSubRect().Bottom);
StartButton.Offset(200,200);

while(App.IsOpened())
{
float ElapsedTime = App.GetFrameTime();
sf::Event Event;
while(App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))App.Close();
}

// Convert local window input coordinates to game coordinates
sf::Vector2f mousePos = App.ConvertCoords(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());

// BUTTON CODE
///////////////////////////////////
if(App.GetInput().IsMouseButtonDown(sf::Mouse::Left) && StartButton.Contains(mousePos.x, mousePos.y)){
cout << "BUTTON PUSHED!!!!!" << endl;
}
else if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)){
cout << "Mouse DOWN" << endl;
}

if(App.GetInput().IsKeyDown(sf::Key::C)){
cout << "Mouse coord = " << mousePos.x << " , " << mousePos.y << endl;
cout << "Button Rect left bottom " << Start.GetSubRect().Left << " , " << Start.GetSubRect().Bottom << endl;
cout << "Button Rect right top " << Start.GetSubRect().Right << " , " << Start.GetSubRect().Top << endl;
}

if (Start.GetSubRect().Contains(mousePos.x, mousePos.y))
cout << "In BUTTON!!" << endl;

App.Clear();

App.Draw(Start);

App.Display();

} // while(App.IsOpened())
return 0;
}


As you can see, I got it to do what I wanted, but I feel like there has to be an easier way... Any suggestions?
Knowledge Speaks,
Wisdom Listens
                       -JH

bucknasty189

  • Newbie
  • *
  • Posts: 23
    • View Profile
Sprite button
« Reply #4 on: April 14, 2011, 12:02:02 am »
And even weirder, my sprite doesn't display when I add

Code: [Select]

Start.SetSubRect(StartButton);


after Offseting the rect  (after line 15)
Knowledge Speaks,
Wisdom Listens
                       -JH