TGUI 0.7 is no longer supported, use a newer TGUI version instead.
This codes how to easily change the cursor image when hovering over a button.
#include <TGUI/TGUI.hpp>
int main()
{
sf::RenderWindow window{sf::VideoMode{300, 400}, "TGUI window"};
tgui::Gui gui{window};
// Make the real cursor invisible inside the application
window.setMouseCursorVisible(false);
// Load the texture that will be used as cursor
sf::Texture textureCursorNormal;
textureCursorNormal.loadFromFile("CursorNormal.png");
// Load the texture that will be used as cursor when the mouse is on top of one of the buttons
sf::Texture textureCursorHover;
textureCursorHover.loadFromFile("CursorHover.png");
// Show the normal cursor by default
sf::Sprite sprite(textureCursorNormal);
auto play = tgui::Button::create();
play->setPosition(tgui::bindWidth(gui)*0.15, tgui::bindHeight(gui)*0.125);
play->setSize(tgui::bindWidth(gui)*0.7, tgui::bindHeight(gui)*0.15);
play->setText("Play");
gui.add(play);
// When the mouse enters or leaves the button, the mouse cursor should change.
// We only have to do this for one button, since the others will be copies of this one.
play->connect("MouseEntered", [&](){ sprite.setTexture(textureCursorHover); });
play->connect("MouseLeft", [&](){ sprite.setTexture(textureCursorNormal); });
auto load = tgui::Button::copy(play);
load->setPosition(tgui::bindWidth(gui)*0.15, tgui::bindHeight(gui)*0.425);
load->setText("Load");
gui.add(load);
auto exit = tgui::Button::copy(play);
exit->setPosition(tgui::bindWidth(gui)*0.15, tgui::bindHeight(gui)*0.725);
exit->setText("Exit");
gui.add(exit);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// Reposition the sprite when the mouse moves
if (event.type == sf::Event::MouseMoved)
sprite.setPosition(window.mapPixelToCoords({event.mouseMove.x, event.mouseMove.y}));
else if (event.type == sf::Event::Closed)
window.close();
gui.handleEvent(event);
}
window.clear();
// Draw the buttons
gui.draw();
// Draw the cursor
window.draw(sprite);
window.display();
}
return EXIT_SUCCESS;
}