-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine.cpp
More file actions
50 lines (44 loc) · 1.28 KB
/
Line.cpp
File metadata and controls
50 lines (44 loc) · 1.28 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
#include "Line.hpp"
Line::Line(sf::Vector2f position, sf::RectangleShape rect) : rect(rect)
{
this->rect.setPosition(position);
}
sf::RectangleShape &Line::getRectangle()
{
return this->rect;
}
void Line::update(sf::RenderWindow &window, sf::Vector2i m_prev)
{
auto mousePos = sf::Mouse::getPosition(window);
if (this->isSelected == true)
{
this->rect.setFillColor(sf::Color(35, 208, 217));
if (mousePos != m_prev)
{
if (rect.getGlobalBounds().width > rect.getGlobalBounds().height)
{
//line is horizontal
this->rect.move(0, sf::Vector2f(mousePos - m_prev).y * IMAGE_SCALE);
}
else
{
this->rect.move(sf::Vector2f(mousePos - m_prev).x * IMAGE_SCALE, 0);
}
}
}
else
{
this->rect.setFillColor(sf::Color::White);
}
}
void Line::drawTo(sf::RenderWindow &window)
{
window.draw(rect);
}
sf::Vector2i Line::getMidpoint()
{
auto size = sf::Vector2f(this->rect.getGlobalBounds().width, this->rect.getGlobalBounds().height);
auto pos = this->rect.getPosition();
auto midpoint = sf::Vector2i(pos.x + (size.x / 2), pos.y + (size.y / 2));
return midpoint;
}