Bonjour,
J'ai crée une petite GUI pour mon jeu (ici un bouton) et je rencontre un problème. En effet, j'ai un problème avec la position du sprite qui affiche le bouton. Si je lui attribue les coordonnées (500;300), il se positionne à (500;295). Donc ça crée un problème avec la gestion des événements (survole, clique ...).
La fonction qui reçoit la position du bouton et l'attribue au sprite du survole et du "non-survole" (par défaut) :
void Bouton::SetPosition(float px, float py)
{
x = px;
y = py;
bt.setPosition(x,y);
bt_hover.setPosition(x,y);
texte.setPosition(x + (width/2 - texte.getGlobalBounds().width/2), y + (height/2 - texte.getGlobalBounds().height/2));
}
J'ai vérifié que x et y correspondaient bien aux paramètres que j'ai envoyé (on sait jamais).
Et la fonction qui initialise les sprites :
void Bouton::LoadImage(sf::Texture &img, sf::Texture &imgHover)
{
bt.setTexture(img);
width = img.getSize().x;
height = img.getSize().y;
bt_hover.setTexture(imgHover);
if(bt_hover.getTexture() != NULL)
isImgHover = true;
else
isImgHover = false;
}
Je précise que la différence entre la position envoyée et celle des sprites n'est pas constante (5 dans mon exemple) mais elle dépend de l'ordonnée (plus y augmente, plus la différence augmente).
Voilà, je vois pas d'où ça vient ...
GUIButton.hpp
#ifndef HEADER_GUI_BUTTON
#define HEADER_GUI_BUTTON
#include <SFML/Graphics.hpp>
#include "GUI.hpp"
#include <sstream>
#include <iostream>
using namespace std;
class Bouton
{
public :
Bouton(string text = "", sf::Color color = sf::Color(255, 255, 255));
//~Bouton();
void Draw(sf::RenderWindow &App);
void LoadImage(string chemin, string cheminHover = "");
void LoadImage(sf::Texture &img, sf::Texture &imgHover = sf::Texture());
int SetEvent(const sf::Event &e, sf::RenderWindow &App); // gestion des evenements
void SetValue(string v);
string GetValue();
void SetText(string text);
void SetTextColor(sf::Color color);
void SetButtonSize(sf::Vector2i size);
void SetPosition(float x, float y);
void SetFont(const sf::Font &Font);
void SetGoTo(int v);
int GetGoTo();
void SetVisibility(bool value);
bool GetVisibility();
void SetTextSize(int size);
private :
sf::Texture img_bt, img_bt_hover;
sf::Sprite bt, bt_hover;
sf::Text texte;
string value;
int width, height;
bool visibility, isImgHover;
int goTo;
float x, y;
};
#endif
GUIButton.cpp
#include "GUIButton.hpp"
Bouton::Bouton(string text, sf::Color color) : value("none"), width(0), height(0), visibility(true), goTo(-1), x(0), y(0)
{
texte.setString(text);
texte.setColor(color);
width = texte.getGlobalBounds().width;
height = texte.getGlobalBounds().width;
}
void Bouton::Draw(sf::RenderWindow &App)
{
if(visibility)
{
if(value == "hover" && isImgHover)
App.draw(bt_hover);
else
App.draw(bt);
App.draw(texte);
}
}
void Bouton::LoadImage(string chemin, string cheminHover)
{
img_bt.loadFromFile(chemin);
bt.setTexture(img_bt);
width = img_bt.getSize().x;
height = img_bt.getSize().y;
if(!cheminHover.empty())
{
img_bt_hover.loadFromFile(cheminHover);
bt_hover.setTexture(img_bt_hover);
isImgHover = true;
}
else
isImgHover = false;
}
void Bouton::LoadImage(sf::Texture &img, sf::Texture &imgHover)
{
img_bt = img;
bt.setTexture(img_bt);
width = img_bt.getSize().x;
height = img_bt.getSize().y;
img_bt_hover = imgHover;
bt_hover.setTexture(img_bt_hover);
if(bt_hover.getTexture() != NULL)
isImgHover = true;
else
isImgHover = false;
}
int Bouton::SetEvent(const sf::Event &e, sf::RenderWindow &App)
{
if(visibility) // On utilise e lorsque le bouton est visible
{
// Renvoie clique si on clique sur le bouton ou si on appuie sur ENTRER lorsque le bouton est survolé
if((sf::Mouse::isButtonPressed(sf::Mouse::Left) && sf::Mouse::getPosition(App).x >= x && sf::Mouse::getPosition(App).x <= x+width
&& sf::Mouse::getPosition(App).y >= y && sf::Mouse::getPosition(App).y <= y+height) || ((e.type == sf::Event::KeyPressed) && (e.key.code == sf::Keyboard::Return) && value == "hover"))
return CLICKED;
// SOURIS
if(isImgHover) // Si il y a une image lors du passage de la souris
{
// Si la souris est sur le bouton
if((sf::Mouse::getPosition(App).x >= x && sf::Mouse::getPosition(App).x <= x+width)
&& (sf::Mouse::getPosition(App).y >= y && sf::Mouse::getPosition(App).y <= y+height))
{
SetValue("hover");
return SURVOL;
}
else
SetValue("none");
}
}
return -1; // Il n'y a pas d'évènement
}
void Bouton::SetValue(string v)
{
if(!v.empty() && value != v)
value = v;
}
string Bouton::GetValue()
{
return value;
}
void Bouton::SetText(string text)
{
texte.setString(text);
}
void Bouton::SetTextColor(sf::Color color)
{
texte.setColor(color);
}
void Bouton::SetButtonSize(sf::Vector2i size)
{
width = size.x;
height = size.y;
}
void Bouton::SetPosition(float px, float py)
{
x = px;
y = py;
bt.setPosition(x,y);
bt_hover.setPosition(x,y);
texte.setPosition(x + (width/2 - texte.getGlobalBounds().width/2), y + (height/2 - texte.getGlobalBounds().height/2));
}
void Bouton::SetFont(const sf::Font &Font)
{
texte.setFont(Font);
}
void Bouton::SetGoTo(int v)
{
goTo = v;
}
int Bouton::GetGoTo()
{
return goTo;
}
void Bouton::SetVisibility(bool value)
{
visibility = value;
}
bool Bouton::GetVisibility()
{
return visibility;
}
void Bouton::SetTextSize(int size)
{
texte.setCharacterSize(size);
}
Que représente l'attribut :int width, height;
Un coups c'est la taille du texte(cf Constructeur), plus loins la taille d'une texture(cf LoadImage).
Puis bt_hover et bt???
Puis la fonction :
void Bouton::SetButtonSize(sf::Vector2i size)
{
width = size.x;
height = size.y;
}
La maintenant c'est associé à plus nada.
J'ai trouvé la solution, cela vient de sf::View de la fonction main() :
sf::RenderWindow App(sf::VideoMode(1024, 768, 32), "titre", sf::Style::Close);
sf::View view(sf::FloatRect(0, 0, 1024, 768));
Mais vu que view fait la même taille que App, pourquoi il y a-t-il un décalage vertical ?
Tu veux que je fasse ça ?
sf::View view(sf::FloatRect(0, 0, 1024, 768));
view.setCenter(1024/2,768/2);
Ça ne change rien.
Pour le moment ce n'est pas grave vu que je peux simplement supprimer la vue mais lorsque il va y avoir plusieurs résolutions, je vais en avoir besoin.