This repository was archived by the owner on Jan 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangle.cpp
More file actions
62 lines (58 loc) · 1.85 KB
/
Rectangle.cpp
File metadata and controls
62 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "Rectangle.h"
using namespace Tools;
Rectangle::Rectangle()
{
}
Rectangle::~Rectangle()
{
}
void Rectangle::process(Canvas &canvasMain, Canvas &canvasTemp,
const MouseState &mouse, const PaintParameters ¶m)
{
canvasTemp.clear();
if (!m_leftClicked && mouse.isLeftPressed())
{
if (mouse.isOnCanvas())
{
m_rectStart = mouse.getPos();
m_rectStart -= canvasMain.getPos();
m_startOnCanvas = true;
}
else
{
m_startOnCanvas = false;
}
}
else if (m_leftClicked && mouse.isLeftPressed() && m_startOnCanvas)
{
m_rectEnd = mouse.getPos();
m_rectEnd -= canvasMain.getPos();
drawRectangle(*canvasTemp.ptr(), m_rectStart, m_rectEnd, param);
}
else if (m_leftClicked && !mouse.isLeftPressed() && m_startOnCanvas)
{
drawRectangle(*canvasMain.ptr(), m_rectStart, m_rectEnd, param);
}
m_leftClicked = mouse.isLeftPressed();
}
void Rectangle::drawRectangle(sf::RenderTarget &target,
const sf::Vector2i &start, const sf::Vector2i &end,
const PaintParameters ¶m)
{
int h = target.getSize().y;
sf::Vector2f size{ static_cast<float>(end.x - start.x),
static_cast<float>(end.y - start.y) };
sf::RectangleShape rectangle;
auto getCorrectWidth = [](const int width, const sf::Vector2f &size)
{
int minWidth = std::min(fabsf(size.x), fabsf(size.y)) / 2.f;
return width > minWidth ? minWidth + 1 : width;
};
rectangle.setOutlineThickness(-getCorrectWidth(param.width, size));
rectangle.setOutlineColor(param.color);
rectangle.setFillColor(sf::Color(0, 0, 0, 0));
rectangle.setOrigin(size / 2.f);
rectangle.setPosition(start.x + size.x / 2.f, h - start.y - size.y / 2.f);
rectangle.setSize(size);
target.draw(rectangle);
}