mouse_selection.cpp 2.1 KB
Newer Older
louiz’'s avatar
louiz’ committed
1
#include <gui/camera/mouse_selection.hpp>
2
3
#include <utils/math.hpp>

louiz’'s avatar
louiz’ committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

MouseSelection::MouseSelection():
  ongoing(false)
{
}

void MouseSelection::start(const sf::Vector2i& pos)
{
  this->start_pos = pos;
  this->ongoing = true;
}

void MouseSelection::end()
{
  // assert(this->ongoing == true);
  this->ongoing = false;
}

22
23
bool MouseSelection::is_rectangular_selection_ongoing(const sf::Vector2i& mouse_pos) const
{
24
  return this->ongoing && (vect_distance(this->start_pos, mouse_pos) > 5);
25
26
}

louiz’'s avatar
louiz’ committed
27
bool MouseSelection::contains(const sf::Vector2i& mouse_pos,
28
                              const sf::Vector2i& pos,
louiz’'s avatar
louiz’ committed
29
30
31
                              const uint width) const
{
  sf::Rect<uint> mouse_selection;
32
33

  if (this->is_rectangular_selection_ongoing(mouse_pos))
louiz’'s avatar
louiz’ committed
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
63
64
65
66
67
68
69
70
71
72
    {
      if ((this->start_pos.x < mouse_pos.x))
        mouse_selection.left = this->start_pos.x;
      else
        mouse_selection.left = mouse_pos.x;
      if ((this->start_pos.y < mouse_pos.y))
        mouse_selection.top = this->start_pos.y;
      else
        mouse_selection.top = mouse_pos.y;
      mouse_selection.top += width;
      mouse_selection.left += width/2;
      mouse_selection.width = ::abs(this->start_pos.x - mouse_pos.x);
      mouse_selection.height = ::abs(this->start_pos.y - mouse_pos.y);
    }
  else
    {
      mouse_selection.left = mouse_pos.x + width/2;
      mouse_selection.top = mouse_pos.y + width;
      mouse_selection.width = 1;
      mouse_selection.height = 1;
    }
  sf::Rect<uint> rectangle(pos.x, pos.y, width, width);
  return rectangle.intersects(mouse_selection);
  // if ((this->start_pos.x < mouse_pos.x))
  //   {
  //     if (((x - width/2) < this->start_pos.x) || ((x + width/2) > mouse_pos.x))
  //       return false;
  //   }
  // else if (((x + width/2) > this->start_pos.x) || ((x - width/2) < mouse_pos.x))
  //   return false;
  // if ((this->start_pos.y < mouse_pos.y))
  //   {
  //     if (((y - width/2) < this->start_pos.y) || ((y + width/2) > mouse_pos.y))
  //       return false;
  //   }
  // else if (((y + width/2) > this->start_pos.y) || ((y - width/2) < mouse_pos.y))
  //   return false;
  // return true;
}