Skip to content

Commit 373128b

Browse files
committed
Added snapping of components with Left Shift
1 parent c6036fd commit 373128b

3 files changed

Lines changed: 45 additions & 49 deletions

File tree

src/Components/ComponentRenderer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ComponentRenderer::ComponentRenderer(int x, int y, int width, int height, UIStyl
2020
assert(nullptr != m_pText);
2121
m_pText->setString("No component");
2222
m_outputPin.setParent(this);
23+
m_snapGrid = sf::Vector2f(50, 50);
2324
}
2425

2526
ComponentRenderer::ComponentRenderer(ComponentRenderer && _cr)
@@ -108,6 +109,8 @@ void ComponentRenderer::update()
108109
m_inputPins[i].setViewParent(m_viewParent);
109110
m_inputPins[i].update();
110111
}
112+
113+
m_snap = Input::GetKey(sf::Keyboard::LShift);
111114
}
112115

113116
void ComponentRenderer::_updateState()

src/Core/DraggableBox.cpp

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2327
DraggableBox::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

3037
DraggableBox::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

5364
void 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-
//}

src/Core/DraggableBox.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ class DraggableBox : public AbstractUI
2121
{
2222
protected:
2323
bool m_dragged;
24-
sf::Vector2f m_mouseOffset;
24+
sf::Vector2f m_mouseStart;
25+
sf::Vector2f m_startPos;
26+
27+
bool m_snap = true;
28+
sf::Vector2f m_snapGrid = sf::Vector2f(50, 50);
29+
2530
public:
2631
DraggableBox(int x, int y, int width, int height, UIStyle& style = UIStyle::Default);
2732
DraggableBox(const DraggableBox& _db);
@@ -31,14 +36,8 @@ class DraggableBox : public AbstractUI
3136
DraggableBox& operator=(DraggableBox&& _db);
3237
void swap(DraggableBox& _other);
3338

34-
35-
36-
//virtual void update();
37-
3839
protected:
3940
virtual void _updateState() override;
40-
//virtual void _updateTransform() override;
41-
//virtual void _updateStyle() override;
4241
};
4342

4443
#endif // _DRAGGABLEBOX_H

0 commit comments

Comments
 (0)