Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "ResizableRectangle.h"
  3.  
  4. enum Interaction {
  5.     None = 0, // 0000 0000
  6.     Moving = -1, // 1111 1111
  7.     ResizingTop = 1, // 0000 0001
  8.     ResizingBottom = 2, // 0000 0010
  9.     ResizingLeft = 4, // 0000 0100
  10.     ResizingRight = 8, // 0000 1000
  11. };
  12.  
  13. ResizableRectangle::ResizableRectangle() {
  14.         mRect.setPosition(sf::Vector2f(500, 264));
  15.         mRect.setSize({ 252, 220 });
  16.         mRect.setFillColor({ 125, 125, 125, 125 });
  17.         mRect.setOutlineColor(sf::Color(185, 185, 185, 185));
  18.         mRect.setOutlineColor(sf::Color(66, 255, 141));
  19.         mRect.setOutlineThickness(3);
  20. }
  21.  
  22. ResizableRectangle::~ResizableRectangle() {
  23.  
  24. }
  25.  
  26. void ResizableRectangle::Draw(sf::RenderWindow &window) {
  27.         window.draw(mRect);
  28. }
  29.  
  30. sf::Vector2f ResizableRectangle::GetPosition() {
  31.         return mRect.getPosition();
  32. }
  33.  
  34. sf::Vector2f ResizableRectangle::GetSize() {
  35.     return mRect.getSize();
  36. }
  37.  
  38. void ResizableRectangle::SetPosition(sf::Vector2f position) {
  39.         mRect.setPosition(position);
  40. }
  41. void ResizableRectangle::SetSize(sf::Vector2f size) {
  42.         mRect.setSize(size);
  43. }
  44. void ResizableRectangle::SetFillColor(sf::Color color) {
  45.     mRect.setFillColor(color);
  46. }
  47. void ResizableRectangle::SetOutlineColor(sf::Color color) {
  48.     mRect.setOutlineColor(color);
  49. }
  50. bool ResizableRectangle::IsActive() const {
  51.     return mInteraction != None;
  52. }
  53. void ResizableRectangle::Handle(const sf::Event &event) {
  54.     switch (event.type) {
  55.     case sf::Event::MouseButtonPressed:
  56.         if (event.mouseButton.button == sf::Mouse::Left) {
  57.             mDragPos = Game::GetWindow().mapPixelToCoords({ event.mouseButton.x, event.mouseButton.y }) - mRect.getPosition();
  58.             const sf::Vector2f size(mRect.getSize());
  59.  
  60.             // Check if outside the rectangle...
  61.             if (mDragPos.x < 0 || mDragPos.y < 0 || mDragPos.x > size.x || mDragPos.y > size.y)
  62.                 break; // do nothing
  63.  
  64.             const bool leftBorder = mDragPos.x < mResizeBorder;
  65.             const bool rightBorder = mDragPos.x > size.x - mResizeBorder;
  66.             const bool topBorder = mDragPos.y < mResizeBorder;
  67.             const bool bottomBorder = mDragPos.y > size.y - mResizeBorder;
  68.  
  69.             if (!(leftBorder || rightBorder || topBorder || bottomBorder)) {
  70.                 // We're somewhere in the center
  71.                 mInteraction = Moving;
  72.             }
  73.             else {
  74.                 mInteraction = None;
  75.                 if (leftBorder)
  76.                     mInteraction |= ResizingLeft;
  77.                 if (rightBorder)
  78.                     mInteraction |= ResizingRight;
  79.                 if (topBorder)
  80.                     mInteraction |= ResizingTop;
  81.                 if (bottomBorder)
  82.                     mInteraction |= ResizingBottom;
  83.                
  84.             }
  85.         }
  86.         break;
  87.     case sf::Event::MouseButtonReleased:
  88.         if (event.mouseButton.button == sf::Mouse::Left) {
  89.             mInteraction = None;
  90.  
  91.             if (mSnap != 0) {
  92.                 sf::Vector2f size = mRect.getSize();
  93.                 size.x = std::floor(size.x / mSnap) * mSnap;
  94.                 size.y = std::floor(size.y / mSnap) * mSnap;
  95.                 mRect.setSize(size);
  96.             }
  97.         }
  98.         break;
  99.     case sf::Event::MouseMoved:
  100.         if (mInteraction == None)
  101.             break;
  102.  
  103.         const sf::Vector2f mouse(Game::GetWindow().mapPixelToCoords({ event.mouseMove.x, event.mouseMove.y }) - mRect.getPosition());
  104.         sf::Vector2f delta(mouse - mDragPos);
  105.  
  106.         if (mInteraction == Moving) {
  107.             std::cout << "moving\n";
  108.             mRect.move(delta);
  109.             onChange();
  110.         }
  111.  
  112.         else {
  113.             sf::Vector2f size = mRect.getSize();
  114.            
  115.             // Resizing right, change size and adjust drag position
  116.             if (mInteraction & ResizingRight) {
  117.                 size.x += delta.x;
  118.                 mDragPos.x += delta.x;
  119.                 std::cout << "right" << std::endl;
  120.             }
  121.  
  122.             // Resizing left, change size and move the rectangle
  123.             if (mInteraction & ResizingLeft) {
  124.                 size.x -= delta.x;
  125.                 mRect.move(sf::Vector2f(delta.x, 0));
  126.                 std::cout << "left" << std::endl;
  127.             }
  128.  
  129.             // Resizing bottom, change size and adjust drag position
  130.             if (mInteraction & ResizingBottom) {
  131.                 size.y += delta.y;
  132.                 mDragPos.y += delta.y;
  133.                 std::cout << "down" << std::endl;
  134.             }
  135.  
  136.             // Resizing top, change size and move the rectangle
  137.             if (mInteraction & ResizingTop) {
  138.                 size.y -= delta.y;
  139.                 mRect.move(sf::Vector2f(0, delta.y));
  140.                 std::cout << "up" << std::endl;
  141.             }
  142.  
  143.             // Ensure minimum size
  144.             if (size.x < mMinimumSize)
  145.                 size.x = mMinimumSize;
  146.             if (size.y < mMinimumSize)
  147.                 size.y = mMinimumSize;
  148.  
  149.             if (size.x > mMaximumSize)
  150.                 size.x = mMaximumSize;
  151.             if (size.y > mMaximumSize)
  152.                 size.y = mMaximumSize;
  153.  
  154.             mRect.setSize(size);
  155.             std::cout << "size: x = " << size.x << ", y = " << size.y << "\n";
  156.         }
  157.         break;
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement