1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include <SFML/Graphics.hpp>
#include "fenetreDeSaisie.h"
#include <iostream>
#include <string>
FenetreDeSaisie::FenetreDeSaisie(int largeur,int hauteur,int posX,int posY,int caracMax,std::string lettre)
{
sf::Font Arial;
if (!Arial.loadFromFile("arial.ttf"))
{
std::cout << "Erreur chargement Arial.ttf" <<std:: endl;
}
_Arial = Arial;
sf::RectangleShape line;
sf::Text boutonEntrer;
sf::RectangleShape Rectangle;
sf::RectangleShape RectangleEntrer;
_hauteurTableau = hauteur;
_largeurTableau = largeur;
_posY = posY;
_posX = posX;
_Rect = Rectangle ;
_RectEntrer = RectangleEntrer;
_boutonEntrer = boutonEntrer;
_line = line;
_clicSurFenetre = false;
_caracMax = caracMax;
lettre = _letreEntrer;
}
void FenetreDeSaisie::CrerLaFenetre(FenetreDeSaisie, sf::RenderWindow& win)
{
_Rect.setSize(sf::Vector2f(_largeurTableau,_hauteurTableau));
_Rect.setPosition(_posX,_posY);
_Rect.setOutlineThickness(1);
_Rect.setOutlineColor(sf::Color::Blue);
int largeur = 40 ;
int posX = _posX + _largeurTableau;
_RectEntrer.setSize(sf::Vector2f(largeur,_hauteurTableau ));
_RectEntrer.setPosition(posX,_posY);
_RectEntrer.setFillColor(sf::Color(100,100,100));
_RectEntrer.setOutlineThickness(1);
_RectEntrer.setOutlineColor(sf::Color::Blue);
int posY = _posY + ((_hauteurTableau /2)-10) ;
_boutonEntrer.setPosition(posX,posY);
_boutonEntrer.setFont(_Arial);
_boutonEntrer.setString("Entrer");
_boutonEntrer.setCharacterSize(largeur / 3 );
int hauteurligne = (_hauteurTableau-10);
int posLigne = (_posX + 2);
_line.setSize(sf::Vector2f(1,hauteurligne));
_line.setPosition(posLigne,posY);
_line.setFillColor(sf::Color::Black);
_textEntrer.setFont(_Arial);
_textEntrer.setCharacterSize(largeur/2);
_textEntrer.setPosition(_posX,_posY);
_textEntrer.setColor(sf::Color::Green);
_textEntrer.setString(_mot);
win.draw(_Rect);
win.draw(_RectEntrer);
win.draw(_boutonEntrer);
win.draw(_textEntrer);
if(_clicSurFenetre == true )
{
win.draw(_line);
}
}
sf::Event FenetreDeSaisie::lettreEntrer(sf::RenderWindow& win, sf::Event event)
{
_event = event;
std::string mot;
int posX = _posX + _largeurTableau;
int largeur = 40;
sf::Vector2i positionSouris = sf::Mouse::getPosition(win);
sf::FloatRect monBouton(_posX, _posY, _largeurTableau, _hauteurTableau);
sf::FloatRect monEntrer(posX,_posY, largeur ,_hauteurTableau);
if(_event.type == sf::Event::MouseButtonPressed)
{
if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && monBouton.contains(positionSouris.x ,positionSouris.y))
{
_clicSurFenetre = true;
}
else if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && !monBouton.contains(positionSouris.x ,positionSouris.y) && !monEntrer.contains(positionSouris.x ,positionSouris.y) )
_clicSurFenetre = false;
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && monEntrer.contains(positionSouris.x ,positionSouris.y))
{
_letreEntrer = _mot;
_mot = mot;
}
}
if (_clicSurFenetre == true)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace)&& _mot.size()>0)
{
_mot.erase(_mot.size()-1,1);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
{
_letreEntrer = _mot;
_mot = mot;
}
if(_event.type == sf::Event::TextEntered)
{
if ((_event.text.unicode > 30 && (_event.text.unicode < 127 || _event.text.unicode > 159)))
{
if(_mot.size() != _caracMax)
{
_mot += static_cast<char>(_event.text.unicode);
}
}
}
}
return(event);
}
std::string FenetreDeSaisie::obtenirMot()
{
return(_letreEntrer);
} |
Partager