Skip to content

Commit b70fe27

Browse files
committed
Refactor input handling
1 parent fbc18f2 commit b70fe27

File tree

1 file changed

+52
-116
lines changed

1 file changed

+52
-116
lines changed

Editor/EditorApp.cpp

Lines changed: 52 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -58,93 +58,42 @@ namespace RTEGUI {
5858
m_Input->GetMouseButtons(mouseButtons.data(), mouseStates.data());
5959
m_Input->GetMousePosition(&mousePosX, &mousePosY);
6060

61-
// If click released
62-
if (mouseButtons.at(0) == GUIInput::Released) {
63-
// Move the control after a grab
64-
if (m_SelectionInfo.GrabbedControl && m_SelectionInfo.TriggerGrab && m_SelectionInfo.Control) {
65-
// TODO: Check if not moved to another parent
66-
int destX = ProcessSnapCoord(mousePosX + m_SelectionInfo.GrabX);
67-
int destY = ProcessSnapCoord(mousePosY + m_SelectionInfo.GrabY);
68-
m_SelectionInfo.Control->Move(destX, destY);
69-
70-
m_UnsavedChanges = true;
71-
}
72-
73-
// Resize/Move control after a grab
74-
if (m_SelectionInfo.GrabbedHandle && m_SelectionInfo.TriggerGrab && m_SelectionInfo.Control) {
75-
int xPos;
76-
int yPos;
77-
int width;
78-
int height;
79-
CalculateHandleResize(mousePosX, mousePosY, &xPos, &yPos, &width, &height);
61+
EditorSelection &currentSelection = m_EditorManager->GetCurrentSelection();
8062

81-
m_SelectionInfo.Control->Move(xPos, yPos);
82-
m_SelectionInfo.Control->Resize(width, height);
63+
// Trigger the grab only if we grabbed the control/handle and moved it far enough from the starting spot, this prevents accidental small movements when grabbing/releasing.
64+
currentSelection.CheckMovementAndSetTriggerGrab(mousePosX, mousePosY);
8365

84-
m_UnsavedChanges = true;
66+
if (mouseButtons.at(0) == GUIInput::Released) {
67+
if (currentSelection.GetControl()) {
68+
// Move the control after a grab
69+
if (currentSelection.ControlGrabbedAndTriggered()) { m_UnsavedChanges = currentSelection.MoveSelection(mousePosX, mousePosY); }
70+
// Resize/Move control after a grab
71+
if (currentSelection.HandleGrabbedAndTriggered()) { m_UnsavedChanges = currentSelection.ResizeSelection(mousePosX, mousePosY); }
72+
// Update properties
73+
if (m_UnsavedChanges && !m_EditorManager->ControlUnderMouse(m_EditorManager->GetPropertyPage(), mousePosX, mousePosY)) { m_EditorManager->UpdateControlProperties(currentSelection.GetControl()); }
8574
}
86-
87-
// Update properties
88-
if (!ControlUnderMouse(m_PropertyPage.get(), mousePosX, mousePosY) && m_SelectionInfo.Control) { UpdateControlProperties(m_SelectionInfo.Control); }
89-
90-
m_SelectionInfo.GrabbedControl = false;
91-
m_SelectionInfo.GrabbedHandle = false;
92-
m_SelectionInfo.TriggerGrab = false;
93-
}
94-
95-
96-
// Check for grabbing handles
97-
if (!m_SelectionInfo.GrabbedControl && m_SelectionInfo.Control && mouseButtons.at(0) == GUIInput::Pushed) {
98-
int HandleIndex = HandleUnderMouse(m_SelectionInfo.Control, mousePosX, mousePosY);
99-
if (HandleIndex != -1) {
100-
m_SelectionInfo.GrabbedControl = false;
101-
m_SelectionInfo.GrabbedHandle = true;
102-
m_SelectionInfo.HandleIndex = HandleIndex;
103-
104-
m_SelectionInfo.GrabX = mousePosX;
105-
m_SelectionInfo.GrabY = mousePosY;
106-
m_SelectionInfo.ClickX = mousePosX;
107-
m_SelectionInfo.ClickY = mousePosY;
75+
currentSelection.ReleaseAnyGrabs();
76+
} else if (mouseButtons.at(0) == GUIInput::Pushed) {
77+
// Check for grabbing handles
78+
if (currentSelection.GetControl() && !currentSelection.IsGrabbingControl()) {
79+
int handleIndex = m_EditorManager->HandleUnderMouse(currentSelection.GetControl(), mousePosX, mousePosY);
80+
if (handleIndex != -1) { currentSelection.GrabHandle(handleIndex, mousePosX, mousePosY); }
10881
}
109-
}
110-
111-
// If we've grabbed a control or handle, and we've moved far enough from the starting spot, trigger the grab
112-
// This prevents quickly selecting a control and slightly moving a couple pixels before releasing
113-
if ((m_SelectionInfo.GrabbedControl || m_SelectionInfo.GrabbedHandle) && !m_SelectionInfo.TriggerGrab) {
114-
int moveDist = 4;
115-
if (std::fabs(m_SelectionInfo.ClickX - mousePosX) >= moveDist || std::fabs(m_SelectionInfo.ClickY - mousePosY) >= moveDist) { m_SelectionInfo.TriggerGrab = true; }
116-
}
117-
118-
// Check if mouse clicked on a control
119-
if (!m_SelectionInfo.GrabbedControl && !m_SelectionInfo.GrabbedHandle && mouseButtons.at(0) == GUIInput::Pushed) {
120-
GUIControl *control = ControlUnderMouse(m_RootControl, mousePosX, mousePosY);
121-
if (control && control != m_RootControl) {
122-
int xPos;
123-
int yPos;
124-
int width;
125-
int height;
126-
control->GetControlRect(&xPos, &yPos, &width, &height);
127-
128-
m_SelectionInfo.GrabbedControl = true;
129-
m_SelectionInfo.GrabbedHandle = false;
130-
m_SelectionInfo.Control = control;
131-
132-
m_SelectionInfo.GrabX = xPos - mousePosX;
133-
m_SelectionInfo.GrabY = yPos - mousePosY;
134-
m_SelectionInfo.ClickX = mousePosX;
135-
m_SelectionInfo.ClickY = mousePosY;
136-
137-
UpdateControlProperties(m_SelectionInfo.Control, false);
138-
139-
SelectActiveControlInList();
140-
} else if (control == m_RootControl) {
141-
// Unselect control
142-
m_SelectionInfo.GrabbedControl = false;
143-
m_SelectionInfo.GrabbedHandle = false;
144-
m_SelectionInfo.Control = nullptr;
145-
146-
m_PropertyPage->ClearValues();
147-
SelectActiveControlInList();
82+
// Check if mouse clicked on a control
83+
if (!currentSelection.IsGrabbingControl() && !currentSelection.IsGrabbingHandle()) {
84+
GUIControl *clickedControl = m_EditorManager->ControlUnderMouse(m_EditorManager->GetRootControl(), mousePosX, mousePosY);
85+
if (clickedControl && clickedControl != m_EditorManager->GetRootControl()) {
86+
currentSelection.GrabControl(clickedControl, mousePosX, mousePosY);
87+
88+
m_EditorManager->UpdateControlProperties(currentSelection.GetControl());
89+
m_EditorManager->SelectActiveControlInList(currentSelection.GetControl());
90+
91+
// Remove focus from the currently focused editor manager element between selection changes so the currently selected property page line doesn't persist between selection changes
92+
m_EditorManager->RemoveFocus();
93+
} else if (clickedControl == m_EditorManager->GetRootControl()) {
94+
// Unselect control if the workspace was clicked
95+
m_EditorManager->ClearCurrentSelection();
96+
}
14897
}
14998
}
15099
}
@@ -160,41 +109,11 @@ namespace RTEGUI {
160109

161110
bool modCtrl = m_KeyStates.at(KEY_LCONTROL) == pressed || m_KeyStates.at(KEY_RCONTROL) == pressed;
162111
bool modShift = m_KeyStates.at(KEY_LSHIFT) == pressed || m_KeyStates.at(KEY_RSHIFT) == pressed;
163-
bool modAlt = m_KeyStates.at(KEY_ALT) == pressed;
164-
165-
if (!m_PropertyPage->HasTextFocus() && m_SelectionInfo.Control) {
166-
if (m_KeyStates.at(KEY_DEL) == pressed) {
167-
m_ControlManager->RemoveControl(m_SelectionInfo.Control->GetName(), true);
168-
m_SelectionInfo.Control = nullptr;
169-
m_SelectionInfo.GrabbedControl = false;
170-
m_SelectionInfo.GrabbedHandle = false;
171-
m_PropertyPage->ClearValues();
172-
} else {
173-
const GUIPanel *selectedElement = dynamic_cast<GUIPanel *>(m_SelectionInfo.Control);
174112

175-
int nudgeSize = modShift ? 1 : m_GridSize;
176-
177-
if (m_KeyStates.at(KEY_UP) == pressed && m_PrevKeyStates.at(KEY_UP) != pressed) {
178-
m_SelectionInfo.Control->Move(selectedElement->GetXPos(), selectedElement->GetYPos() - nudgeSize);
179-
UpdateControlProperties(m_SelectionInfo.Control);
180-
} else if (m_KeyStates.at(KEY_DOWN) == pressed && m_PrevKeyStates.at(KEY_DOWN) != pressed) {
181-
m_SelectionInfo.Control->Move(selectedElement->GetXPos(), selectedElement->GetYPos() + nudgeSize);
182-
UpdateControlProperties(m_SelectionInfo.Control);
183-
} else if (m_KeyStates.at(KEY_LEFT) == pressed && m_PrevKeyStates.at(KEY_LEFT) != pressed) {
184-
m_SelectionInfo.Control->Move(selectedElement->GetXPos() - nudgeSize, selectedElement->GetYPos());
185-
UpdateControlProperties(m_SelectionInfo.Control);
186-
} else if (m_KeyStates.at(KEY_RIGHT) == pressed && m_PrevKeyStates.at(KEY_RIGHT) != pressed) {
187-
m_SelectionInfo.Control->Move(selectedElement->GetXPos() + nudgeSize, selectedElement->GetYPos());
188-
UpdateControlProperties(m_SelectionInfo.Control);
189-
}
190-
}
191-
}
113+
if (m_KeyStates.at(KEY_ALT) && m_KeyStates.at(KEY_F4)) { OnQuitButton(); }
192114

193115
// Escape key - Undo any grab
194-
if (m_KeyStates.at(KEY_ESC) == pressed) {
195-
m_SelectionInfo.ClearSelection();
196-
m_PropertyPage->ClearValues();
197-
}
116+
if (m_KeyStates.at(KEY_ESC) == pressed) { m_EditorManager->ClearCurrentSelection(); }
198117

199118
if (modCtrl) {
200119
if (m_KeyStates.at(KEY_S) == pressed) {
@@ -204,8 +123,25 @@ namespace RTEGUI {
204123
}
205124
}
206125

207-
if (modAlt && m_KeyStates.at(KEY_F4)) { OnQuitButton(); }
126+
const EditorSelection &currentSelection = m_EditorManager->GetCurrentSelection();
208127

128+
if (currentSelection.GetControl() && !m_EditorManager->GetPropertyPage()->HasTextFocus()) {
129+
if (m_KeyStates.at(KEY_DEL) == pressed) {
130+
m_EditorManager->RemoveControl(currentSelection.GetControl()->GetName());
131+
} else {
132+
bool selectionNudged = false;
133+
if (m_KeyStates.at(KEY_UP) == pressed && m_PrevKeyStates.at(KEY_UP) != pressed) {
134+
selectionNudged = currentSelection.NudgeSelection(EditorSelection::NudgeDirection::NudgeUp, modShift);
135+
} else if (m_KeyStates.at(KEY_DOWN) == pressed && m_PrevKeyStates.at(KEY_DOWN) != pressed) {
136+
selectionNudged = currentSelection.NudgeSelection(EditorSelection::NudgeDirection::NudgeDown, modShift);
137+
} else if (m_KeyStates.at(KEY_LEFT) == pressed && m_PrevKeyStates.at(KEY_LEFT) != pressed) {
138+
selectionNudged = currentSelection.NudgeSelection(EditorSelection::NudgeDirection::NudgeLeft, modShift);
139+
} else if (m_KeyStates.at(KEY_RIGHT) == pressed && m_PrevKeyStates.at(KEY_RIGHT) != pressed) {
140+
selectionNudged = currentSelection.NudgeSelection(EditorSelection::NudgeDirection::NudgeRight, modShift);
141+
}
142+
if (selectionNudged) { m_UnsavedChanges = m_EditorManager->UpdateControlProperties(currentSelection.GetControl()); }
143+
}
144+
}
209145
m_PrevKeyStates = m_KeyStates;
210146
}
211147

0 commit comments

Comments
 (0)