Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. #ifndef _pButton_
  2. #define _pButton_
  3. #include "pGlobals.hpp"
  4. #include "sfRoundedRectangleShape.hpp"
  5. #include <SFML\Graphics.hpp>
  6. #include <iostream>
  7. using namespace std;
  8. namespace Panda
  9. {
  10.     class Button : public sf::Drawable
  11.     {
  12.         public:
  13.             Button(const sf::Text &text, const pColor &mcolor, const float &mpos_x, const float &mpos_y, const float &msize_x, const float &msize_y, const bool &mrounded, const float &mrounded_radius, const float &mrounded_points, void(*mbutton_fallback)())
  14.             {
  15.                 button_text.setOrigin(button_text.getGlobalBounds().width / 2,button_text.getGlobalBounds().height);//center origin
  16.                 button_shape.setOrigin(button_shape.getLocalBounds().width / 2, button_shape.getLocalBounds().height / 2); // "  "
  17.  
  18.                 button_text = text;
  19.                 button_color = mcolor;
  20.  
  21.                 button_pos.x = mpos_x;
  22.                 button_pos.y = mpos_y;
  23.                 cout << "mpos" << mpos_x << mpos_y << endl;
  24.                 button_size.x = msize_x;
  25.                 button_size.y = msize_y;
  26.  
  27.                 button_isRounded = mrounded;
  28.                 roundedPoints = mrounded_radius;
  29.                 roundedRadius = mrounded_radius;
  30.  
  31.                 button_fallback = mbutton_fallback;
  32.  
  33.                 if (button_isRounded)
  34.                 {
  35.                     button_shape.setCornerPointCount(mrounded_points);
  36.                     button_shape.setCornersRadius(mrounded_radius);
  37.                 }
  38.                 button_shape.setFillColor(sf::Color(mcolor.red, mcolor.green, mcolor.blue, mcolor.alpha));
  39.                 button_shape.setPosition(mpos_x, mpos_y);
  40.                 button_shape.setSize(msize_x, msize_y);
  41.  
  42.                 button_text = text;
  43.                 button_text.setPosition(mpos_x, mpos_y);
  44.                 setPosition(mpos_x, mpos_y);
  45.  
  46.                 while ((button_text.getGlobalBounds().width) > (button_shape.getGlobalBounds().width - (button_shape.getGlobalBounds().width / 12)))
  47.                 {
  48.                     button_text.setCharacterSize(button_text.getCharacterSize() - 1);
  49.                 }
  50.             }
  51.  
  52.             void setText(const sf::Text &text)
  53.             {
  54.                 button_text = text;
  55.             }
  56.             sf::Text getText()
  57.             {
  58.                 return button_text;
  59.             }
  60.  
  61.             void setPosition(sf::Vector2f pos)
  62.             {
  63.                 button_pos = pos;
  64.             }
  65.             void setPosition(float x, float y)
  66.             {
  67.                 button_pos.x = x;
  68.                 button_pos.y = y;
  69.             }
  70.             sf::Vector2f getPosition()
  71.             {
  72.                 return button_pos;
  73.             }
  74.  
  75.             void setRotation(float rot)
  76.             {
  77.                 rotation = rot;
  78.             }
  79.             float getRotation()
  80.             {
  81.                 return rotation;
  82.             }
  83.  
  84.             void setScale(float scale_x, float scale_y)
  85.             {
  86.                 button_scale.x = scale_x;
  87.                 button_scale.y = scale_y;
  88.             }
  89.             void setScale(sf::Vector2f scale)
  90.             {
  91.                 button_scale = scale;
  92.             }
  93.             sf::Vector2f getScale()
  94.             {
  95.                 return button_scale;
  96.             }
  97.  
  98.             void handleEvent(sf::Event event)
  99.             {
  100.                 if (event.type == sf::Event::MouseButtonPressed)
  101.                 {
  102.                     if (event.mouseButton.button == sf::Mouse::Left)
  103.                     {
  104.                         if (button_shape.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y))
  105.                         {
  106.                             (*button_fallback)();
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.             void update()
  112.             {
  113.  
  114.  
  115.                 button_text.setOrigin(button_text.getGlobalBounds().width / 2,button_text.getGlobalBounds().height/2);//center origin
  116.                 button_shape.setOrigin(button_shape.getLocalBounds().width / 2, button_shape.getLocalBounds().height/2); // "  "
  117.                
  118.  
  119.                 button_shape.setPosition(getPosition());
  120.                 button_shape.setScale(getScale());
  121.                 button_shape.setRotation(getRotation());
  122.  
  123.                 button_text.setPosition(button_shape.getPosition());
  124.                 button_text.setRotation(getRotation());
  125.                 button_text.setScale(getScale());
  126.                
  127.                 cout << "************" << endl;
  128.                 cout << "POS[" << button_shape.getPosition().x << ";" << button_shape.getPosition().y << "];" << endl;
  129.                 cout << "SIZE[" << button_shape.getSize().x << ";" << button_shape.getSize().y << "];" << endl;
  130.                 cout << "************" << endl;
  131.                
  132.             }
  133.         private:
  134.             /*
  135.             bool isIntersecting(float x, float y)
  136.             {
  137.                 float x_begin, x_end, y_begin, y_end;
  138.                 x_begin = button_shape.getPosition().x - (-button_shape.getSize().x/2);
  139.                 x_end = button_shape.getPosition().x + (button_shape.getSize().x/2);
  140.  
  141.                 y_begin = button_shape.getPosition().y - (-button_shape.getSize().y/2);
  142.                 y_end = button_shape.getPosition().y + (button_shape.getSize().y/2);
  143.    
  144.                
  145.                 if ((x > x_begin) && (x < x_end))
  146.                 {
  147.                     if ((y > y_begin) && (y < y_end))
  148.                     {
  149.                         return true;
  150.                     }
  151.                     else
  152.                     {
  153.                         return false;
  154.                     }
  155.                 }
  156.                 else
  157.                 {
  158.                     return false;
  159.                 }
  160.                
  161.             }
  162.             */
  163.             virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
  164.             {
  165.                 target.draw(button_shape,states);
  166.                 target.draw(button_text, states);
  167.             }
  168.             void(*button_fallback)();
  169.  
  170.  
  171.             sf::Text button_text;
  172.  
  173.             pColor button_color;
  174.             sf::Vector2f button_pos;
  175.             sf::Vector2f button_size;
  176.             sf::Vector2f button_scale = sf::Vector2f(1,1);
  177.  
  178.             bool button_isRounded;
  179.             float roundedRadius;
  180.             float roundedPoints;
  181.  
  182.             float rotation;
  183.  
  184.             bool isActivated = false;
  185.  
  186.             sf::RoundedRectangleShape button_shape;
  187.  
  188.     };
  189. }
  190. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement