Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _pButton_
- #define _pButton_
- #include "pGlobals.hpp"
- #include "sfRoundedRectangleShape.hpp"
- #include <SFML\Graphics.hpp>
- #include <iostream>
- using namespace std;
- namespace Panda
- {
- class Button : public sf::Drawable
- {
- public:
- 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)())
- {
- button_text.setOrigin(button_text.getGlobalBounds().width / 2,button_text.getGlobalBounds().height);//center origin
- button_shape.setOrigin(button_shape.getLocalBounds().width / 2, button_shape.getLocalBounds().height / 2); // " "
- button_text = text;
- button_color = mcolor;
- button_pos.x = mpos_x;
- button_pos.y = mpos_y;
- cout << "mpos" << mpos_x << mpos_y << endl;
- button_size.x = msize_x;
- button_size.y = msize_y;
- button_isRounded = mrounded;
- roundedPoints = mrounded_radius;
- roundedRadius = mrounded_radius;
- button_fallback = mbutton_fallback;
- if (button_isRounded)
- {
- button_shape.setCornerPointCount(mrounded_points);
- button_shape.setCornersRadius(mrounded_radius);
- }
- button_shape.setFillColor(sf::Color(mcolor.red, mcolor.green, mcolor.blue, mcolor.alpha));
- button_shape.setPosition(mpos_x, mpos_y);
- button_shape.setSize(msize_x, msize_y);
- button_text = text;
- button_text.setPosition(mpos_x, mpos_y);
- setPosition(mpos_x, mpos_y);
- while ((button_text.getGlobalBounds().width) > (button_shape.getGlobalBounds().width - (button_shape.getGlobalBounds().width / 12)))
- {
- button_text.setCharacterSize(button_text.getCharacterSize() - 1);
- }
- }
- void setText(const sf::Text &text)
- {
- button_text = text;
- }
- sf::Text getText()
- {
- return button_text;
- }
- void setPosition(sf::Vector2f pos)
- {
- button_pos = pos;
- }
- void setPosition(float x, float y)
- {
- button_pos.x = x;
- button_pos.y = y;
- }
- sf::Vector2f getPosition()
- {
- return button_pos;
- }
- void setRotation(float rot)
- {
- rotation = rot;
- }
- float getRotation()
- {
- return rotation;
- }
- void setScale(float scale_x, float scale_y)
- {
- button_scale.x = scale_x;
- button_scale.y = scale_y;
- }
- void setScale(sf::Vector2f scale)
- {
- button_scale = scale;
- }
- sf::Vector2f getScale()
- {
- return button_scale;
- }
- void handleEvent(sf::Event event)
- {
- if (event.type == sf::Event::MouseButtonPressed)
- {
- if (event.mouseButton.button == sf::Mouse::Left)
- {
- if (button_shape.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y))
- {
- (*button_fallback)();
- }
- }
- }
- }
- void update()
- {
- button_text.setOrigin(button_text.getGlobalBounds().width / 2,button_text.getGlobalBounds().height/2);//center origin
- button_shape.setOrigin(button_shape.getLocalBounds().width / 2, button_shape.getLocalBounds().height/2); // " "
- button_shape.setPosition(getPosition());
- button_shape.setScale(getScale());
- button_shape.setRotation(getRotation());
- button_text.setPosition(button_shape.getPosition());
- button_text.setRotation(getRotation());
- button_text.setScale(getScale());
- cout << "************" << endl;
- cout << "POS[" << button_shape.getPosition().x << ";" << button_shape.getPosition().y << "];" << endl;
- cout << "SIZE[" << button_shape.getSize().x << ";" << button_shape.getSize().y << "];" << endl;
- cout << "************" << endl;
- }
- private:
- /*
- bool isIntersecting(float x, float y)
- {
- float x_begin, x_end, y_begin, y_end;
- x_begin = button_shape.getPosition().x - (-button_shape.getSize().x/2);
- x_end = button_shape.getPosition().x + (button_shape.getSize().x/2);
- y_begin = button_shape.getPosition().y - (-button_shape.getSize().y/2);
- y_end = button_shape.getPosition().y + (button_shape.getSize().y/2);
- if ((x > x_begin) && (x < x_end))
- {
- if ((y > y_begin) && (y < y_end))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- */
- virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
- {
- target.draw(button_shape,states);
- target.draw(button_text, states);
- }
- void(*button_fallback)();
- sf::Text button_text;
- pColor button_color;
- sf::Vector2f button_pos;
- sf::Vector2f button_size;
- sf::Vector2f button_scale = sf::Vector2f(1,1);
- bool button_isRounded;
- float roundedRadius;
- float roundedPoints;
- float rotation;
- bool isActivated = false;
- sf::RoundedRectangleShape button_shape;
- };
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement