fonction qui gère les click de souris, je sais pas trop si ça sera utile mais au moins c'est là

This commit is contained in:
Mylloon 2023-12-27 23:59:59 +01:00
parent a9293d1305
commit 15321d0032
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 35 additions and 4 deletions

View file

@ -11,5 +11,12 @@ struct Ecran {
const std::string name = "Projet"); // constructor
~Ecran(); // destructor
void afficher(const std::function<void()> f = {[]() {}}) const;
void afficher(
/* Function called at each frame */
const std::function<void()> drawEachFrame = {[]() {}},
/* Take 2 arguments: position X, position Y
* -> called each time user left-click */
const std::function<void(const int, const int)> doOnClick = {
[](const int, const int) {}}) const;
};

View file

@ -9,7 +9,12 @@ Ecran::Ecran(const uint w, const uint h, const std::string n) {
Ecran::~Ecran() {}
void Ecran::afficher(const std::function<void()> dessin) const {
void Ecran::afficher(
const std::function<void()> dessin,
const std::function<void(const int, const int)> onclick) const {
// Boolean flag to track whether the mouse button is pressed
bool mouseButtonPressed = false;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
@ -23,6 +28,21 @@ void Ecran::afficher(const std::function<void()> dessin) const {
// Appel d'une fonction dessin
dessin();
// Récupération des coordonnées du clic de souris
if (!mouseButtonPressed) {
if (event.type == sf::Event::MouseButtonPressed &&
event.mouseButton.button == sf::Mouse::Left) {
onclick(event.mouseButton.x, event.mouseButton.y);
mouseButtonPressed = true;
}
}
// Réinitialisation du flag lorsque le bouton est relâché
if (event.type == sf::Event::MouseButtonReleased &&
event.mouseButton.button == sf::Mouse::Left) {
mouseButtonPressed = false;
}
window.display();
}
}

View file

@ -2,7 +2,11 @@
#include "../includes/Ecran.hpp"
void draw_debug(PlateauDames &p) {
p.afficherPlateau(std::cout, true);
p.afficherPlateau(std::cout, false);
}
void click_debug(const int x, const int y) {
std::cout << "Clic souris @ (" << x << ", " << y << ")\n";
}
int main() {
@ -10,7 +14,7 @@ int main() {
Joueur j2;
PlateauDames p(j1, j2);
Ecran e;
// e.afficher(std::bind(&draw_debug, p));
// e.afficher(std::bind(&draw_debug, p), click_debug);
std::cout << "Good bye!" << std::endl;
return EXIT_SUCCESS;