Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "ResizableRectangle.h"
- enum Interaction {
- None = 0, // 0000 0000
- Moving = -1, // 1111 1111
- ResizingTop = 1, // 0000 0001
- ResizingBottom = 2, // 0000 0010
- ResizingLeft = 4, // 0000 0100
- ResizingRight = 8, // 0000 1000
- };
- ResizableRectangle::ResizableRectangle() {
- mRect.setPosition(sf::Vector2f(500, 264));
- mRect.setSize({ 252, 220 });
- mRect.setFillColor({ 125, 125, 125, 125 });
- mRect.setOutlineColor(sf::Color(185, 185, 185, 185));
- mRect.setOutlineColor(sf::Color(66, 255, 141));
- mRect.setOutlineThickness(3);
- }
- ResizableRectangle::~ResizableRectangle() {
- }
- void ResizableRectangle::Draw(sf::RenderWindow &window) {
- window.draw(mRect);
- }
- sf::Vector2f ResizableRectangle::GetPosition() {
- return mRect.getPosition();
- }
- sf::Vector2f ResizableRectangle::GetSize() {
- return mRect.getSize();
- }
- void ResizableRectangle::SetPosition(sf::Vector2f position) {
- mRect.setPosition(position);
- }
- void ResizableRectangle::SetSize(sf::Vector2f size) {
- mRect.setSize(size);
- }
- void ResizableRectangle::SetFillColor(sf::Color color) {
- mRect.setFillColor(color);
- }
- void ResizableRectangle::SetOutlineColor(sf::Color color) {
- mRect.setOutlineColor(color);
- }
- bool ResizableRectangle::IsActive() const {
- return mInteraction != None;
- }
- void ResizableRectangle::Handle(const sf::Event &event) {
- switch (event.type) {
- case sf::Event::MouseButtonPressed:
- if (event.mouseButton.button == sf::Mouse::Left) {
- mDragPos = Game::GetWindow().mapPixelToCoords({ event.mouseButton.x, event.mouseButton.y }) - mRect.getPosition();
- const sf::Vector2f size(mRect.getSize());
- // Check if outside the rectangle...
- if (mDragPos.x < 0 || mDragPos.y < 0 || mDragPos.x > size.x || mDragPos.y > size.y)
- break; // do nothing
- const bool leftBorder = mDragPos.x < mResizeBorder;
- const bool rightBorder = mDragPos.x > size.x - mResizeBorder;
- const bool topBorder = mDragPos.y < mResizeBorder;
- const bool bottomBorder = mDragPos.y > size.y - mResizeBorder;
- if (!(leftBorder || rightBorder || topBorder || bottomBorder)) {
- // We're somewhere in the center
- mInteraction = Moving;
- }
- else {
- mInteraction = None;
- if (leftBorder)
- mInteraction |= ResizingLeft;
- if (rightBorder)
- mInteraction |= ResizingRight;
- if (topBorder)
- mInteraction |= ResizingTop;
- if (bottomBorder)
- mInteraction |= ResizingBottom;
- }
- }
- break;
- case sf::Event::MouseButtonReleased:
- if (event.mouseButton.button == sf::Mouse::Left) {
- mInteraction = None;
- if (mSnap != 0) {
- sf::Vector2f size = mRect.getSize();
- size.x = std::floor(size.x / mSnap) * mSnap;
- size.y = std::floor(size.y / mSnap) * mSnap;
- mRect.setSize(size);
- }
- }
- break;
- case sf::Event::MouseMoved:
- if (mInteraction == None)
- break;
- const sf::Vector2f mouse(Game::GetWindow().mapPixelToCoords({ event.mouseMove.x, event.mouseMove.y }) - mRect.getPosition());
- sf::Vector2f delta(mouse - mDragPos);
- if (mInteraction == Moving) {
- std::cout << "moving\n";
- mRect.move(delta);
- onChange();
- }
- else {
- sf::Vector2f size = mRect.getSize();
- // Resizing right, change size and adjust drag position
- if (mInteraction & ResizingRight) {
- size.x += delta.x;
- mDragPos.x += delta.x;
- std::cout << "right" << std::endl;
- }
- // Resizing left, change size and move the rectangle
- if (mInteraction & ResizingLeft) {
- size.x -= delta.x;
- mRect.move(sf::Vector2f(delta.x, 0));
- std::cout << "left" << std::endl;
- }
- // Resizing bottom, change size and adjust drag position
- if (mInteraction & ResizingBottom) {
- size.y += delta.y;
- mDragPos.y += delta.y;
- std::cout << "down" << std::endl;
- }
- // Resizing top, change size and move the rectangle
- if (mInteraction & ResizingTop) {
- size.y -= delta.y;
- mRect.move(sf::Vector2f(0, delta.y));
- std::cout << "up" << std::endl;
- }
- // Ensure minimum size
- if (size.x < mMinimumSize)
- size.x = mMinimumSize;
- if (size.y < mMinimumSize)
- size.y = mMinimumSize;
- if (size.x > mMaximumSize)
- size.x = mMaximumSize;
- if (size.y > mMaximumSize)
- size.y = mMaximumSize;
- mRect.setSize(size);
- std::cout << "size: x = " << size.x << ", y = " << size.y << "\n";
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement