Advertisement
Darkwater

Untitled

May 7th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. namespace game {
  2.  
  3. class Button
  4. {
  5. public:
  6.     Button(int _x, int _y, int _width, int _height, std::string text)
  7.     {
  8.         texture.loadFromFile("res/button.png");
  9.  
  10.         state = NORMAL;
  11.  
  12.         this->x = _x;
  13.         this->y = _y;
  14.         this->width = _width;
  15.         this->height = _height;
  16.  
  17.         sf::Font font;
  18.         font.loadFromFile("res/SF Intermosaic B.ttf");
  19.         label.setFont(font);
  20.         label.setString(text);
  21.         label.setCharacterSize(16);
  22.         label.setColor(sf::Color(20, 20, 20));
  23.  
  24.         recalculateVertices();
  25.     }
  26.  
  27.     void update(const sf::Vector2i mousePos)
  28.     {
  29.         if(mousePos.x > this->x && mousePos.y > this->y && mousePos.x < this->x + this->width && mousePos.y < this->y + this->height)
  30.         {
  31.             if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
  32.                 state = ACTIVE;
  33.             }
  34.             else
  35.             {
  36.                 state = HOVER;
  37.             }
  38.         }
  39.         else
  40.         {
  41.             state = NORMAL;
  42.         }
  43.     }
  44.  
  45.     void draw(sf::RenderWindow& window) const
  46.     {
  47.         sf::RenderStates states;
  48.         states.texture = &texture;
  49.  
  50.         window.draw(vertices[state], states);
  51.         // window.draw(label);
  52.     }
  53.  
  54. private:
  55.     sf::VertexArray vertices[3];
  56.     sf::Texture texture;
  57.  
  58.     int x, y, width, height;
  59.     sf::Text label;
  60.  
  61.     enum { NORMAL, HOVER, ACTIVE } state;
  62.  
  63.     void recalculateVertices()
  64.     {
  65.         int left   = 6;
  66.         int top    = 6;
  67.         int right  = 34;
  68.         int bottom = 14;
  69.  
  70.         int texwidth  = 40;
  71.         int texheight = 20;
  72.  
  73.         int xPos[4] = { x, x + left, x + width - (texwidth - right),    x + width  };
  74.         int yPos[4] = { y, y + left, y + height - (texheight - bottom), y + height };
  75.         int xTCoords[4] = { 0, left, right, texwidth  };
  76.         int yTCoords[4] = { 0, top, bottom, texheight };
  77.  
  78.         label.setPosition(x + 10, y + 10);
  79.  
  80.  
  81.         for(int l = 0; l < 3; l++) {
  82.             vertices[l] = sf::VertexArray(sf::Quads, 36);
  83.  
  84.             int i = 0;
  85.             for(int j = 0; j < 3; j++) {
  86.                 for(int k = 0; k < 3; k++) {
  87.                     vertices[l][i+0].position = sf::Vector2f(xPos[j+0], yPos[k+0]);
  88.                     vertices[l][i+1].position = sf::Vector2f(xPos[j+1], yPos[k+0]);
  89.                     vertices[l][i+2].position = sf::Vector2f(xPos[j+1], yPos[k+1]);
  90.                     vertices[l][i+3].position = sf::Vector2f(xPos[j+0], yPos[k+1]);
  91.  
  92.                     vertices[l][i+0].texCoords = sf::Vector2f(xTCoords[j+0], yTCoords[k+0] + texheight * l);
  93.                     vertices[l][i+1].texCoords = sf::Vector2f(xTCoords[j+1], yTCoords[k+0] + texheight * l);
  94.                     vertices[l][i+2].texCoords = sf::Vector2f(xTCoords[j+1], yTCoords[k+1] + texheight * l);
  95.                     vertices[l][i+3].texCoords = sf::Vector2f(xTCoords[j+0], yTCoords[k+1] + texheight * l);
  96.  
  97.                     i += 4;
  98.                 }
  99.             }
  100.         }
  101.     }
  102. };
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement