Skip to content

Commit 948a42b

Browse files
committed
Get selections in/from lists working
1 parent b70fe27 commit 948a42b

File tree

3 files changed

+173
-110
lines changed

3 files changed

+173
-110
lines changed

Editor/EditorApp.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,17 @@ namespace RTEGUI {
8686
currentSelection.GrabControl(clickedControl, mousePosX, mousePosY);
8787

8888
m_EditorManager->UpdateControlProperties(currentSelection.GetControl());
89-
m_EditorManager->SelectActiveControlInList(currentSelection.GetControl());
90-
89+
if (currentSelection.GetControl()->GetID() == "COLLECTIONBOX") {
90+
m_EditorManager->SelectActiveControlInParentList(currentSelection.GetControl());
91+
} else {
92+
m_EditorManager->SelectActiveControlInChildrenList(currentSelection.GetControl());
93+
}
9194
// 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
9295
m_EditorManager->RemoveFocus();
9396
} else if (clickedControl == m_EditorManager->GetRootControl()) {
9497
// Unselect control if the workspace was clicked
9598
m_EditorManager->ClearCurrentSelection();
99+
m_EditorManager->SelectActiveControlInParentList(m_EditorManager->GetRootControl());
96100
}
97101
}
98102
}
@@ -127,7 +131,7 @@ namespace RTEGUI {
127131

128132
if (currentSelection.GetControl() && !m_EditorManager->GetPropertyPage()->HasTextFocus()) {
129133
if (m_KeyStates.at(KEY_DEL) == pressed) {
130-
m_EditorManager->RemoveControl(currentSelection.GetControl()->GetName());
134+
m_EditorManager->RemoveControl(currentSelection.GetControl());
131135
} else {
132136
bool selectionNudged = false;
133137
if (m_KeyStates.at(KEY_UP) == pressed && m_PrevKeyStates.at(KEY_UP) != pressed) {
@@ -172,9 +176,9 @@ namespace RTEGUI {
172176
if (controlName == "PropertyPage") {
173177
m_UnsavedChanges = m_EditorManager->UpdatePropertyPage(editorEvent);
174178
} else if (controlName == "CollectionBoxList" && editorEvent.GetMsg() == GUIListBox::MouseDown) {
175-
m_EditorManager->UpdateCollectionBoxList();
179+
m_EditorManager->SelectActiveControlFromParentList();
176180
} else if (controlName == "ControlsInCollectionBoxList" && editorEvent.GetMsg() == GUIListBox::MouseDown) {
177-
//m_EditorManager->UpdateControlsInCollectionBoxList();
181+
m_EditorManager->SelectActiveControlFromChildrenList();
178182
} else if (controlName == "GridSizeTextBox" && editorEvent.GetMsg() == GUITextBox::Enter) {
179183
m_EditorManager->UpdateSnapGridSize(editorEvent);
180184
} else if (controlName == "SnapCheckBox") {
@@ -246,7 +250,7 @@ namespace RTEGUI {
246250
m_EditorManager->SetRootControl(newRootControl);
247251
m_UnsavedChanges = false;
248252

249-
m_EditorManager->PopulateCollectionBoxList();
253+
m_EditorManager->UpdateCollectionBoxList();
250254
}
251255
}
252256

Editor/EditorManager.cpp

Lines changed: 131 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace RTEGUI {
2525
m_EditorBase->SetDrawColor(makecol(32, 32, 32));
2626
m_EditorBase->SetDrawType(GUICollectionBox::Color);
2727

28-
GUILabel *frameTimeLabel = dynamic_cast<GUILabel *>(m_EditorControlManager->AddControl("FrameTimer", "LABEL", m_EditorBase.get(), 300, 10, 0, 20));
28+
GUILabel *frameTimeLabel = dynamic_cast<GUILabel *>(m_EditorControlManager->AddControl("FrameTimer", "LABEL", m_EditorBase.get(), 300, 10, 100, 20));
2929
frameTimeLabel->SetText("Frame Time: 0");
3030

3131
m_LeftColumn.reset(dynamic_cast<GUICollectionBox *>(m_EditorControlManager->AddControl("LeftColumn", "COLLECTIONBOX", nullptr, 0, 0, 290, screen->GetBitmap()->GetHeight())));
@@ -126,66 +126,17 @@ namespace RTEGUI {
126126
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127127

128128
void EditorManager::SetFrameTimeLabelText(int64_t frameTime) const {
129-
dynamic_cast<GUILabel *>(m_EditorControlManager->GetControl("FrameTimer"))->SetText("Frame Time: " + std::to_string(frameTime));
129+
dynamic_cast<GUILabel *>(m_EditorControlManager->GetControl("FrameTimer"))->SetText("Frame Time: " + std::to_string(frameTime) + "ms");
130130
}
131131

132132
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
133133

134-
void EditorManager::PopulateCollectionBoxList() const {
135-
m_CollectionBoxList->ClearList();
136-
m_CollectionBoxList->AddItem(m_RootControl->GetName());
137-
138-
// Lambda expression to recursively add lower-level CollectionBoxes belonging to the higher-level CollectionBoxes
139-
std::function<void(GUICollectionBox *, const std::string &)> recursiveAddItem = [&recursiveAddItem, this](GUICollectionBox *control, const std::string &indent) {
140-
m_CollectionBoxList->AddItem(indent + control->GetName());
141-
for (GUIControl *childControl : *control->GetChildren()) {
142-
if ((control = dynamic_cast<GUICollectionBox *>(childControl))) { recursiveAddItem(control, indent + "\t"); }
143-
}
144-
};
145-
146-
GUICollectionBox *collectionBox = nullptr;
147-
for (GUIControl *control : *m_WorkspaceManager->GetControlList()) {
148-
if ((collectionBox = dynamic_cast<GUICollectionBox *>(control)) && collectionBox->GetParent() == m_RootControl) { recursiveAddItem(collectionBox, "\t"); }
149-
}
150-
}
151-
152-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
153-
154-
void EditorManager::PopulateCollectionBoxChildrenList(GUICollectionBox *collectionBox) const {
155-
m_ControlsInCollectionBoxList->ClearList();
156-
157-
// Go through all the top-level (directly under root) controls and add only the CollectionBoxes to the list here
158-
for (GUIControl *control : *collectionBox->GetChildren()) {
159-
if (control->GetID() != "COLLECTIONBOX") { m_ControlsInCollectionBoxList->AddItem(control->GetName()); }
160-
// Check if this is selected in the editor, and if so, select it in the list too
161-
if (collectionBox == s_SelectionInfo.GetControl()) { m_ControlsInCollectionBoxList->SetSelectedIndex(m_CollectionBoxList->GetItemList()->size() - 1); }
162-
}
163-
}
164-
165-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
166-
167-
void EditorManager::SelectActiveControlInList(GUIControl *control) const {
168-
// Check if this is selected in the editor, and if so, select it in the list too
169-
if (control->GetID() == "COLLECTIONBOX") {
170-
for (const GUIListBox::Item *listEntry : *m_CollectionBoxList->GetItemList()) {
171-
if (listEntry->m_Name == control->GetName()) { m_CollectionBoxList->SetSelectedIndex(listEntry->m_ID); }
172-
}
173-
if (!control->GetChildren()->empty()) { PopulateCollectionBoxChildrenList(dynamic_cast<GUICollectionBox *>(control)); }
174-
} else {
175-
for (const GUIListBox::Item *listEntry : *m_ControlsInCollectionBoxList->GetItemList()) {
176-
if (listEntry->m_Name == control->GetName()) {
177-
for (const GUIListBox::Item *parentListEntry : *m_CollectionBoxList->GetItemList()) {
178-
if (parentListEntry->m_Name == listEntry->m_Name) { m_CollectionBoxList->SetSelectedIndex(parentListEntry->m_ID); }
179-
}
180-
m_ControlsInCollectionBoxList->SetSelectedIndex(listEntry->m_ID);
181-
}
182-
}
134+
bool EditorManager::AddNewControl(GUIEvent &editorEvent) {
135+
if (s_SelectionInfo.GetControl() && s_SelectionInfo.GetControl()->GetID() != "COLLECTIONBOX") {
136+
s_SelectionInfo.ClearSelection();
137+
m_PropertyPage->ClearValues();
183138
}
184-
}
185139

186-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
187-
188-
bool EditorManager::AddNewControl(GUIEvent &editorEvent) {
189140
std::string controlClass = editorEvent.GetControl()->GetName().substr(2, std::string::npos);
190141
GUIControl *parent = m_RootControl;
191142

@@ -199,18 +150,24 @@ namespace RTEGUI {
199150
m_WorkspaceManager->GetControl(name)->SetEnabled(false);
200151
}
201152

202-
PopulateCollectionBoxList();
203-
PopulateCollectionBoxChildrenList(dynamic_cast<GUICollectionBox *>(parent));
153+
UpdateCollectionBoxList();
154+
UpdateCollectionBoxChildrenList(dynamic_cast<GUICollectionBox *>(parent));
204155

205156
return true;
206157
}
207158

208159
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
209160

210-
void EditorManager::RemoveControl(const std::string &controlToRemove) const {
211-
m_WorkspaceManager->RemoveControl(controlToRemove, true);
212-
ClearCurrentSelection();
213-
PopulateCollectionBoxList();
161+
void EditorManager::RemoveControl(GUIControl *controlToRemove) const {
162+
m_WorkspaceManager->RemoveControl(controlToRemove->GetName(), true);
163+
if (controlToRemove->GetID() == "COLLECTIONBOX") {
164+
ClearCurrentSelection();
165+
UpdateCollectionBoxList();
166+
} else {
167+
s_SelectionInfo.ClearSelection();
168+
m_PropertyPage->ClearValues();
169+
UpdateCollectionBoxChildrenList(dynamic_cast<GUICollectionBox *>(controlToRemove->GetParent()));
170+
}
214171
}
215172

216173
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -236,6 +193,119 @@ namespace RTEGUI {
236193
return controlType;
237194
}
238195

196+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
197+
198+
void EditorManager::SelectActiveControlFromParentList() const {
199+
const GUIListPanel::Item *selectedItem = m_CollectionBoxList->GetSelected();
200+
201+
if (selectedItem) {
202+
// Try to find the box of that name, and select it
203+
GUIControl *control = m_WorkspaceManager->GetControl(selectedItem->m_Name.substr(selectedItem->m_Name.find_first_not_of('\t'), std::string::npos));
204+
if (control) {
205+
// If the selected item is the root control don't grab it but proceed to populate the children list from it
206+
if (selectedItem->m_Name == m_RootControl->GetName()) {
207+
s_SelectionInfo.ClearSelection();
208+
m_PropertyPage->ClearValues();
209+
} else {
210+
s_SelectionInfo.ReleaseAnyGrabs();
211+
s_SelectionInfo.SetControl(control);
212+
}
213+
UpdateCollectionBoxChildrenList(dynamic_cast<GUICollectionBox *>(control));
214+
m_ControlsInCollectionBoxList->SetSelectedIndex(-1);
215+
}
216+
} else {
217+
// Deselection if clicked on no list item
218+
ClearCurrentSelection();
219+
// When nothing is selected populate the children list with the root control's children to show any "loose" controls
220+
UpdateCollectionBoxChildrenList(dynamic_cast<GUICollectionBox *>(m_RootControl));
221+
m_ControlsInCollectionBoxList->SetSelectedIndex(-1);
222+
}
223+
RemoveFocus();
224+
}
225+
226+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
227+
228+
void EditorManager::SelectActiveControlFromChildrenList() const {
229+
const GUIListPanel::Item *selectedItem = m_ControlsInCollectionBoxList->GetSelected();
230+
if (selectedItem) {
231+
// Try to find the control of that name, and select it
232+
GUIControl *control = m_WorkspaceManager->GetControl(selectedItem->m_Name);
233+
if (control) {
234+
s_SelectionInfo.ReleaseAnyGrabs();
235+
s_SelectionInfo.SetControl(control);
236+
}
237+
} else {
238+
// Deselection if clicked on no list item
239+
s_SelectionInfo.ClearSelection();
240+
m_PropertyPage->ClearValues();
241+
}
242+
RemoveFocus();
243+
}
244+
245+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
246+
247+
void EditorManager::SelectActiveControlInParentList(GUIControl *control) const {
248+
// Check if this is selected in the editor and select it in the list too
249+
for (const GUIListBox::Item *listEntry : *m_CollectionBoxList->GetItemList()) {
250+
if (listEntry->m_Name.substr(listEntry->m_Name.find_first_not_of('\t'), std::string::npos) == control->GetName()) {
251+
m_CollectionBoxList->SetSelectedIndex(listEntry->m_ID);
252+
break;
253+
}
254+
}
255+
UpdateCollectionBoxChildrenList(dynamic_cast<GUICollectionBox *>(control));
256+
m_ControlsInCollectionBoxList->SetSelectedIndex(-1);
257+
}
258+
259+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
260+
261+
void EditorManager::SelectActiveControlInChildrenList(GUIControl *control) const {
262+
// Check if this is selected in the editor and select it's parent in the parent list and then select it in the children list
263+
SelectActiveControlInParentList(control->GetParent());
264+
for (const GUIListBox::Item *listEntry : *m_ControlsInCollectionBoxList->GetItemList()) {
265+
if (listEntry->m_Name == control->GetName()) {
266+
m_ControlsInCollectionBoxList->SetSelectedIndex(listEntry->m_ID);
267+
break;
268+
}
269+
}
270+
}
271+
272+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
273+
274+
void EditorManager::UpdateCollectionBoxList() const {
275+
m_CollectionBoxList->ClearList();
276+
m_CollectionBoxList->AddItem(m_RootControl->GetName());
277+
278+
// Lambda expression to recursively add lower-level CollectionBoxes belonging to the higher-level CollectionBoxes
279+
std::function<void(GUICollectionBox *, const std::string &)> recursiveAddItem = [&recursiveAddItem, this](GUICollectionBox *control, const std::string &indent) {
280+
m_CollectionBoxList->AddItem(indent + control->GetName());
281+
for (GUIControl *childControl : *control->GetChildren()) {
282+
if ((control = dynamic_cast<GUICollectionBox *>(childControl))) { recursiveAddItem(control, indent + "\t"); }
283+
}
284+
};
285+
286+
GUICollectionBox *collectionBox = nullptr;
287+
for (GUIControl *control : *m_WorkspaceManager->GetControlList()) {
288+
if ((collectionBox = dynamic_cast<GUICollectionBox *>(control)) && collectionBox->GetParent() == m_RootControl) { recursiveAddItem(collectionBox, "\t"); }
289+
}
290+
}
291+
292+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
293+
294+
void EditorManager::UpdateCollectionBoxChildrenList(GUICollectionBox *collectionBox) const {
295+
m_ControlsInCollectionBoxList->ClearList();
296+
297+
// Go through all the top-level (directly under root) controls and add only the CollectionBoxes to the list here
298+
for (GUIControl *control : *collectionBox->GetChildren()) {
299+
if (control->GetID() != "COLLECTIONBOX") { m_ControlsInCollectionBoxList->AddItem(control->GetName()); }
300+
// Check if this is selected in the editor, and if so, select it in the list too
301+
//if (collectionBox == s_SelectionInfo.GetControl()) { m_ControlsInCollectionBoxList->SetSelectedIndex(m_CollectionBoxList->GetItemList()->size() - 1); }
302+
if (collectionBox == s_SelectionInfo.GetControl()) {
303+
m_ControlsInCollectionBoxList->SetSelectedIndex(-1);
304+
break;
305+
}
306+
}
307+
}
308+
239309
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
240310

241311
bool EditorManager::MouseInsideBox(int mousePosX, int mousePosY, int boxPosX, int boxPosY, int boxWidth, int boxHeight) const {
@@ -303,34 +373,11 @@ namespace RTEGUI {
303373
m_PropertyPage->ClearValues();
304374
m_CollectionBoxList->SetSelectedIndex(-1);
305375
m_ControlsInCollectionBoxList->ClearList();
306-
//PopulateCollectionBoxChildrenList(dynamic_cast<GUICollectionBox *>(m_RootControl));
307376

308377
// Clear focused control of the manager itself so it doesn't persist between selection changes (e.g property page line remains selected after clearing or changing selection)
309378
RemoveFocus();
310379
}
311380

312-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
313-
314-
void EditorManager::UpdateCollectionBoxList() const {
315-
const GUIListPanel::Item *selectedItem = m_CollectionBoxList->GetSelected();
316-
317-
// If the selected item is the root control treat it as if no item was selected
318-
if (selectedItem && selectedItem->m_Name != m_RootControl->GetName()) {
319-
// Try to find the box of that name, and select it
320-
GUIControl *control = m_WorkspaceManager->GetControl(selectedItem->m_Name.substr(selectedItem->m_Name.find_first_not_of('\t'), std::string::npos));
321-
if (control) {
322-
s_SelectionInfo.ReleaseAnyGrabs();
323-
s_SelectionInfo.SetControl(control);
324-
325-
PopulateCollectionBoxChildrenList(dynamic_cast<GUICollectionBox *>(control));
326-
}
327-
} else {
328-
// Deselection if clicked on no list item
329-
ClearCurrentSelection();
330-
}
331-
RemoveFocus();
332-
}
333-
334381
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
335382

336383
void EditorManager::UpdateSnapGridSize(GUIEvent &editorEvent) const {

0 commit comments

Comments
 (0)