Skip to content

Commit 96f552b

Browse files
committed
Add copy-pasting controls in the workspace
1 parent 8e053ae commit 96f552b

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

Editor/EditorApp.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ namespace RTEGUI {
134134
// Escape key - Undo any grab
135135
if (m_KeyStates.at(KEY_ESC) == pressed) { m_EditorManager->ClearCurrentSelection(); }
136136

137+
const EditorSelection &currentSelection = m_EditorManager->GetCurrentSelection();
138+
137139
if (modCtrl) {
140+
if (m_KeyStates.at(KEY_C) == pressed && m_PrevKeyStates.at(KEY_C) != pressed) {
141+
m_EditorManager->StoreCurrentSelectionCopyInfo();
142+
} else if (m_KeyStates.at(KEY_V) == pressed && m_PrevKeyStates.at(KEY_V) != pressed) {
143+
m_EditorManager->AddNewControlFromStoredCopyInfo();
144+
}
145+
138146
if (m_KeyStates.at(KEY_S) == pressed) {
139147
OnSaveButton(modShift ? true : false);
140148
} else if (m_KeyStates.at(KEY_O) == pressed) {
@@ -152,8 +160,6 @@ namespace RTEGUI {
152160
}
153161
}
154162

155-
const EditorSelection &currentSelection = m_EditorManager->GetCurrentSelection();
156-
157163
if (currentSelection.GetControl() && !m_EditorManager->GetPropertyPage()->HasTextFocus()) {
158164
if (m_KeyStates.at(KEY_DEL) == pressed) {
159165
m_EditorManager->RemoveControl(currentSelection.GetControl());

Editor/EditorManager.cpp

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace RTEGUI {
1010

1111
EditorSelection EditorManager::s_SelectionInfo;
12+
EditorManager::EditorSelectionCopyInfo EditorManager::s_SelectionCopyInfo;
1213

1314
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1415

@@ -152,6 +153,24 @@ namespace RTEGUI {
152153
dynamic_cast<GUILabel *>(m_EditorControlManager->GetControl("FrameTimer"))->SetText("Frame Time: " + std::to_string(frameTime) + "ms");
153154
}
154155

156+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
157+
158+
void EditorManager::StoreCurrentSelectionCopyInfo() const {
159+
GUIControl *selectedControl = s_SelectionInfo.GetControl();
160+
if (selectedControl) {
161+
s_SelectionCopyInfo = {
162+
selectedControl->GetName(),
163+
selectedControl->GetID(),
164+
selectedControl->GetPanel()->GetRelXPos(),
165+
selectedControl->GetPanel()->GetRelYPos(),
166+
selectedControl->GetPanel()->GetWidth(),
167+
selectedControl->GetPanel()->GetHeight(),
168+
selectedControl->GetParent(),
169+
selectedControl->GetProperties()
170+
};
171+
}
172+
}
173+
155174
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
156175

157176
bool EditorManager::AddNewControl(GUIEvent &editorEvent) {
@@ -175,6 +194,55 @@ namespace RTEGUI {
175194
return true;
176195
}
177196

197+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
198+
199+
void EditorManager::AddNewControlFromStoredCopyInfo() const {
200+
if (s_SelectionCopyInfo.Class.empty()) {
201+
return;
202+
}
203+
std::string newControlName;
204+
for (int i = 1; i < 1000; ++i) {
205+
newControlName = s_SelectionCopyInfo.Name + " - copy " + std::to_string(i);
206+
// Check if this name exists
207+
bool found = false;
208+
for (GUIControl *control : *m_WorkspaceManager->GetControlList()) {
209+
if (control->GetName() == newControlName) {
210+
found = true;
211+
break;
212+
}
213+
}
214+
if (!found) {
215+
break;
216+
}
217+
}
218+
int offset = 10;
219+
220+
GUIControl *createdControl = nullptr;
221+
createdControl = m_WorkspaceManager->AddControl(newControlName, s_SelectionCopyInfo.Class, s_SelectionCopyInfo.Parent, s_SelectionCopyInfo.PosX + offset, s_SelectionCopyInfo.PosY + offset, s_SelectionCopyInfo.Width, s_SelectionCopyInfo.Height);
222+
s_SelectionCopyInfo.PosX += offset;
223+
s_SelectionCopyInfo.PosY += offset;
224+
225+
GUIProperties properties;
226+
createdControl->StoreProperties();
227+
properties.Update(s_SelectionCopyInfo.Properties, true);
228+
properties.SetValue("Name", newControlName);
229+
createdControl->GetPanel()->BuildProperties(&properties);
230+
createdControl->ApplyProperties(&properties);
231+
m_PropertyPage->SetPropertyValues(&properties);
232+
233+
s_SelectionInfo.SetControl(createdControl);
234+
235+
UpdateCollectionBoxList();
236+
UpdateCollectionBoxChildrenList(dynamic_cast<GUICollectionBox *>(createdControl->GetParent()));
237+
238+
if (createdControl->GetID() == "COLLECTIONBOX") {
239+
SelectActiveControlInParentList(createdControl);
240+
} else {
241+
SelectActiveControlInChildrenList(createdControl);
242+
}
243+
RemoveFocus();
244+
}
245+
178246
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
179247

180248
void EditorManager::RemoveControl(GUIControl *controlToRemove) const {
@@ -430,7 +498,7 @@ namespace RTEGUI {
430498
control->ApplyProperties(m_PropertyPage->GetPropertyValues());
431499
result = true;
432500
} else {
433-
//control->StoreProperties();
501+
control->StoreProperties();
434502
GUIProperties properties;
435503
properties.Update(control->GetProperties(), true);
436504
control->GetPanel()->BuildProperties(&properties);

Editor/EditorManager.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,23 @@ namespace RTEGUI {
142142
#pragma endregion
143143

144144
#pragma region GUI Element Creation
145+
/// <summary>
146+
/// Stores the properties of the currently selected GUI element in the workspace for later recreation.
147+
/// </summary>
148+
void StoreCurrentSelectionCopyInfo() const;
149+
145150
/// <summary>
146151
/// Create a new GUI element in the workspace.
147152
/// </summary>
148153
/// <param name="editorEvent">The editor event (button press) to create the element from.</param>
149154
/// <retruns>True to set unsaved changes state.</returns>
150155
bool AddNewControl(GUIEvent &editorEvent);
151156

157+
/// <summary>
158+
/// Create a new GUI element in the workspace from stored copy info.
159+
/// </summary>
160+
void AddNewControlFromStoredCopyInfo() const;
161+
152162
/// <summary>
153163
///
154164
/// </summary>
@@ -253,7 +263,22 @@ namespace RTEGUI {
253263

254264
private:
255265

266+
/// <summary>
267+
/// Struct containing property information for creating an identical GUI element in the workspace. Used for copy-pasting.
268+
/// </summary>
269+
struct EditorSelectionCopyInfo {
270+
std::string Name = "";
271+
std::string Class = "";
272+
int PosX = 0;
273+
int PosY = 0;
274+
int Width = 0;
275+
int Height = 0;
276+
GUIControl *Parent = nullptr;
277+
GUIProperties *Properties = nullptr;
278+
};
279+
256280
static EditorSelection s_SelectionInfo; //!< EditorSelection instance that contains the information of the currently selected element in the workspace.
281+
static EditorSelectionCopyInfo s_SelectionCopyInfo; //!< A copy of an existing GUIControl. Used for copy-pasting.
257282

258283
std::unique_ptr<GUIControlManager> m_EditorControlManager = nullptr; //!< The GUIControlManager that handles this EditorManager.
259284
std::unique_ptr<GUICollectionBox> m_EditorBase = nullptr; //!< The lowest level collection box containing the workspace.

0 commit comments

Comments
 (0)