@@ -18,13 +18,20 @@ DraggableBox::DraggableBox(int x, int y, int width, int height, UIStyle& style)
1818 : AbstractUI(x, y, width, height, style)
1919{
2020 m_dragged = false ;
21+ m_mouseStart = sf::Vector2f (0 , 0 );
22+ m_startPos = sf::Vector2f (0 , 0 );
23+ m_snap = false ;
24+ m_snapGrid = sf::Vector2f (1 , 1 );
2125}
2226
2327DraggableBox::DraggableBox (const DraggableBox & _db)
2428 : AbstractUI(_db)
2529{
2630 m_dragged = _db.m_dragged ;
27- m_mouseOffset = _db.m_mouseOffset ;
31+ m_mouseStart = _db.m_mouseStart ;
32+ m_startPos = _db.m_startPos ;
33+ m_snap = _db.m_snap ;
34+ m_snapGrid = _db.m_snapGrid ;
2835}
2936
3037DraggableBox::DraggableBox (DraggableBox && _db)
@@ -47,46 +54,64 @@ void DraggableBox::swap(DraggableBox & _other)
4754{
4855 AbstractUI::swap (_other);
4956 std::swap (m_dragged, _other.m_dragged );
50- std::swap (m_mouseOffset, _other.m_mouseOffset );
57+ // std::swap(m_mouseOffset, _other.m_mouseOffset);
58+ std::swap (m_mouseStart, _other.m_mouseStart );
59+ std::swap (m_startPos, _other.m_startPos );
60+ std::swap (m_snap, _other.m_snap );
61+ std::swap (m_snapGrid, _other.m_snapGrid );
5162}
5263
5364void DraggableBox::_updateState ()
5465{
5566 if (m_dragged)
5667 {
68+ sf::Vector2f mouseNewPos;
5769 if (nullptr != m_viewParent)
5870 {
59- m_position = m_viewParent->mapScreenPointToView ((sf::Vector2f) Input::GetMousePosition ()) + m_mouseOffset;
71+ mouseNewPos = m_viewParent->mapScreenPointToView (Input::GetMousePosition ()); // + m_mouseOffset;
6072 }
6173 else
6274 {
63- m_position = (sf::Vector2f) Input::GetMousePosition () + m_mouseOffset;
75+ mouseNewPos = Input::GetMousePosition (); // + m_mouseOffset;
6476 }
6577 if (Input::GetMouseButtonUp (sf::Mouse::Left))
6678 {
6779 m_dragged = false ;
6880 m_state = UIState::UI_NORMAL ;
6981 }
82+
83+ if (m_snap)
84+ {
85+ m_position = sf::Vector2f (
86+ floorf ((m_startPos.x + (mouseNewPos.x - m_mouseStart.x )) / m_snapGrid.x ) * m_snapGrid.x ,
87+ floorf ((m_startPos.y + (mouseNewPos.y - m_mouseStart.y )) / m_snapGrid.y ) * m_snapGrid.y );
88+ }
89+ else
90+ {
91+ m_position = m_startPos + (mouseNewPos - m_mouseStart);
92+ }
7093 }
7194 else
7295 {
7396 if (click () && UIManager::GetFirstHoveredUI (Input::GetMousePosition ()) == this )
7497 {
7598 if (nullptr != m_viewParent)
7699 {
77- if (m_viewParent->hovered ((sf::Vector2f) Input::GetMousePosition ()))
100+ if (m_viewParent->hovered (Input::GetMousePosition ()))
78101 {
79- m_mouseOffset = m_position - m_viewParent->mapScreenPointToView ((sf::Vector2f)Input::GetMousePosition ());
80- m_dragged = true ;
81- m_state = UIState::UI_HOVERED ;
102+ m_mouseStart = m_viewParent->mapScreenPointToView (Input::GetMousePosition ());
103+ // m_mouseOffset = m_position - m_viewParent->mapScreenPointToView(Input::GetMousePosition());
82104 }
83105 }
84106 else
85107 {
86- m_mouseOffset = m_position - (sf::Vector2f)Input::GetMousePosition ();
87- m_dragged = true ;
88- m_state = UIState::UI_HOVERED ;
108+ m_mouseStart = Input::GetMousePosition ();
109+ // m_mouseOffset = m_position - Input::GetMousePosition();
89110 }
111+
112+ m_startPos = m_position;
113+ m_dragged = true ;
114+ m_state = UIState::UI_HOVERED ;
90115 }
91116 }
92117
@@ -95,34 +120,3 @@ void DraggableBox::_updateState()
95120 m_state = m_focused ? UIState::UI_FOCUSED : UIState::UI_NORMAL ;
96121 }
97122}
98-
99- // void DraggableBox::_updateTransform()
100- // {
101- // }
102-
103- // void DraggableBox::update()
104- // {
105- // if (m_dragged)
106- // {
107- // m_rect->setPosition((sf::Vector2f)Input::GetMousePosition() + m_mouseOffset);
108- //
109- // if (Input::GetMouseButtonUp(sf::Mouse::Left))
110- // {
111- // m_dragged = false;
112- // m_state = UIState::UI_NORMAL;
113- // }
114- // }
115- // else
116- // {
117- // if (click())
118- // {
119- // m_dragged = true;
120- // m_mouseOffset = m_rect->getPosition() - (sf::Vector2f)Input::GetMousePosition();
121- // m_state = UIState::UI_HOVERED;
122- // }
123- // }
124- //
125- //
126- // m_rect->setFillColor((*m_style)[m_state].bgCol);
127- // m_rect->setOutlineColor((*m_style)[m_state].outCol);
128- // }
0 commit comments