Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace game {
- class Button
- {
- public:
- Button(int _x, int _y, int _width, int _height, std::string text)
- {
- texture.loadFromFile("res/button.png");
- state = NORMAL;
- this->x = _x;
- this->y = _y;
- this->width = _width;
- this->height = _height;
- sf::Font font;
- font.loadFromFile("res/SF Intermosaic B.ttf");
- label.setFont(font);
- label.setString(text);
- label.setCharacterSize(16);
- label.setColor(sf::Color(20, 20, 20));
- recalculateVertices();
- }
- void update(const sf::Vector2i mousePos)
- {
- if(mousePos.x > this->x && mousePos.y > this->y && mousePos.x < this->x + this->width && mousePos.y < this->y + this->height)
- {
- if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
- state = ACTIVE;
- }
- else
- {
- state = HOVER;
- }
- }
- else
- {
- state = NORMAL;
- }
- }
- void draw(sf::RenderWindow& window) const
- {
- sf::RenderStates states;
- states.texture = &texture;
- window.draw(vertices[state], states);
- // window.draw(label);
- }
- private:
- sf::VertexArray vertices[3];
- sf::Texture texture;
- int x, y, width, height;
- sf::Text label;
- enum { NORMAL, HOVER, ACTIVE } state;
- void recalculateVertices()
- {
- int left = 6;
- int top = 6;
- int right = 34;
- int bottom = 14;
- int texwidth = 40;
- int texheight = 20;
- int xPos[4] = { x, x + left, x + width - (texwidth - right), x + width };
- int yPos[4] = { y, y + left, y + height - (texheight - bottom), y + height };
- int xTCoords[4] = { 0, left, right, texwidth };
- int yTCoords[4] = { 0, top, bottom, texheight };
- label.setPosition(x + 10, y + 10);
- for(int l = 0; l < 3; l++) {
- vertices[l] = sf::VertexArray(sf::Quads, 36);
- int i = 0;
- for(int j = 0; j < 3; j++) {
- for(int k = 0; k < 3; k++) {
- vertices[l][i+0].position = sf::Vector2f(xPos[j+0], yPos[k+0]);
- vertices[l][i+1].position = sf::Vector2f(xPos[j+1], yPos[k+0]);
- vertices[l][i+2].position = sf::Vector2f(xPos[j+1], yPos[k+1]);
- vertices[l][i+3].position = sf::Vector2f(xPos[j+0], yPos[k+1]);
- vertices[l][i+0].texCoords = sf::Vector2f(xTCoords[j+0], yTCoords[k+0] + texheight * l);
- vertices[l][i+1].texCoords = sf::Vector2f(xTCoords[j+1], yTCoords[k+0] + texheight * l);
- vertices[l][i+2].texCoords = sf::Vector2f(xTCoords[j+1], yTCoords[k+1] + texheight * l);
- vertices[l][i+3].texCoords = sf::Vector2f(xTCoords[j+0], yTCoords[k+1] + texheight * l);
- i += 4;
- }
- }
- }
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement