Skip to content

Commit fbc18f2

Browse files
committed
Refactor EditorApp
1 parent bbfcc18 commit fbc18f2

File tree

3 files changed

+124
-125
lines changed

3 files changed

+124
-125
lines changed

Editor/EditorApp.cpp

Lines changed: 86 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#include "EditorApp.h"
22
#include "EditorUtil.h"
3-
#include "GUIButton.h"
43
#include "GUICheckbox.h"
5-
#include "GUILabel.h"
64
#include "GUITextBox.h"
75
#include "winalleg.h"
86

97
namespace RTEGUI {
108

9+
int64_t EditorApp::s_FrameTime = 0;
10+
1111
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1212

13-
bool EditorApp::Initialize() {
13+
void EditorApp::Initialize() {
1414
set_color_depth(32);
1515
set_color_conversion(COLORCONV_MOST);
1616
set_window_title("Cortex Command GUI Editor");
@@ -24,73 +24,28 @@ namespace RTEGUI {
2424
m_BackBuffer = create_bitmap(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
2525
clear_to_color(m_BackBuffer, 0);
2626

27-
m_ZoomBuffer = create_bitmap(m_WorkspaceWidth * 2, m_WorkspaceHeight * 2);
28-
clear_to_color(m_ZoomBuffer, 0);
29-
3027
m_Screen = std::make_unique<AllegroScreen>(m_BackBuffer);
3128
m_Input = std::make_unique<AllegroInput>(-1);
3229

3330
// Initialize the UI
34-
35-
return true;
36-
}
37-
31+
m_EditorManager = std::make_unique<EditorManager>(m_Screen.get(), m_Input.get(), "Assets", "EditorSkin.ini");
3832

3933
// Only allow workspace zoom if the screen resolution is FHD or above, smaller resolutions can't fully display it
34+
if (m_BackBuffer->w < 1920 && m_BackBuffer->h < 1080) {
35+
m_EditorManager->DisableZoomCheckbox();
36+
} else {
37+
m_ZoomBuffer = create_bitmap(m_EditorManager->GetWorkspaceWidth() * 2, m_EditorManager->GetWorkspaceHeight() * 2);
38+
clear_to_color(m_ZoomBuffer, 0);
4039
}
4140

41+
//show_os_cursor(MOUSE_CURSOR_ARROW);
4242
}
4343

4444
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
4545

4646
void EditorApp::DestroyBackBuffers() {
4747
destroy_bitmap(m_BackBuffer);
48-
destroy_bitmap(m_ZoomBuffer);
49-
}
50-
51-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
52-
53-
bool EditorApp::Update() {
54-
m_EditorManager->Update();
55-
GUIEvent editorEvent;
56-
while (m_EditorManager->GetEvent(&editorEvent)) {
57-
switch (editorEvent.GetType()) {
58-
case GUIEvent::Command:
59-
if (editorEvent.GetControl()->GetName() == "QuitButton") {
60-
OnQuitButton();
61-
} else if (editorEvent.GetControl()->GetName() == "LoadButton") {
62-
OnLoadButton();
63-
} else if (editorEvent.GetControl()->GetName() == "AddButton") {
64-
OnLoadButton(true);
65-
} else if (editorEvent.GetControl()->GetName() == "SaveButton") {
66-
OnSaveButton();
67-
} else if (editorEvent.GetControl()->GetName() == "SaveAsButton") {
68-
OnSaveButton(true);
69-
} else if (editorEvent.GetControl()->GetName().substr(0, 2).compare("C_") == 0) {
70-
AddNewControl(editorEvent);
71-
}
72-
break;
73-
case GUIEvent::Notification:
74-
if (editorEvent.GetControl()->GetName() == "ActiveCollectionBoxes" && editorEvent.GetMsg() == GUIListBox::MouseDown) {
75-
UpdateCollectionBoxList();
76-
} else if (editorEvent.GetControl()->GetName() == "PropertyPage") {
77-
UpdatePropertyPage(editorEvent);
78-
} else if (editorEvent.GetControl()->GetName() == "GridSizeTextBox" && editorEvent.GetMsg() == GUITextBox::Enter) {
79-
UpdateGridSize(editorEvent);
80-
} else if (editorEvent.GetControl()->GetName() == "SnapCheckBox") {
81-
m_SnapToGrid = (dynamic_cast<GUICheckbox *>(editorEvent.GetControl()))->GetCheck() == GUICheckbox::Checked;
82-
} else if (editorEvent.GetControl()->GetName() == "ZoomCheckBox") {
83-
m_Zoom = (dynamic_cast<GUICheckbox *>(editorEvent.GetControl()))->GetCheck() == GUICheckbox::Checked;
84-
}
85-
break;
86-
default:
87-
break;
88-
}
89-
}
90-
ProcessMouseInput();
91-
ProcessKeyboardInput();
92-
93-
return !m_Quit;
48+
if (m_ZoomBuffer) { destroy_bitmap(m_ZoomBuffer); }
9449
}
9550

9651
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -256,9 +211,52 @@ namespace RTEGUI {
256211

257212
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
258213

214+
bool EditorApp::UpdateEditor() {
215+
m_EditorManager->GetControlManager()->Update();
216+
GUIEvent editorEvent;
217+
while (m_EditorManager->GetControlManager()->GetEvent(&editorEvent)) {
218+
std::string controlName = editorEvent.GetControl()->GetName();
219+
switch (editorEvent.GetType()) {
220+
case GUIEvent::Command:
221+
if (controlName == "QuitButton") {
222+
OnQuitButton();
223+
} else if (controlName == "LoadButton") {
224+
OnLoadButton();
225+
} else if (controlName == "AddButton") {
226+
OnLoadButton(true);
227+
} else if (controlName == "SaveButton") {
228+
OnSaveButton();
229+
} else if (controlName == "SaveAsButton") {
230+
OnSaveButton(true);
231+
} else if (controlName.substr(0, 2).compare("C_") == 0) {
232+
m_UnsavedChanges = m_EditorManager->AddNewControl(editorEvent);
233+
}
234+
break;
235+
case GUIEvent::Notification:
236+
if (controlName == "PropertyPage") {
237+
m_UnsavedChanges = m_EditorManager->UpdatePropertyPage(editorEvent);
238+
} else if (controlName == "CollectionBoxList" && editorEvent.GetMsg() == GUIListBox::MouseDown) {
239+
m_EditorManager->UpdateCollectionBoxList();
240+
} else if (controlName == "ControlsInCollectionBoxList" && editorEvent.GetMsg() == GUIListBox::MouseDown) {
241+
//m_EditorManager->UpdateControlsInCollectionBoxList();
242+
} else if (controlName == "GridSizeTextBox" && editorEvent.GetMsg() == GUITextBox::Enter) {
243+
m_EditorManager->UpdateSnapGridSize(editorEvent);
244+
} else if (controlName == "SnapCheckBox") {
245+
EditorSelection::s_SnapToGrid = dynamic_cast<GUICheckbox *>(editorEvent.GetControl())->GetCheck() == GUICheckbox::Checked;
246+
} else if (controlName == "ZoomCheckBox") {
247+
m_ZoomWorkspace = (dynamic_cast<GUICheckbox *>(editorEvent.GetControl()))->GetCheck() == GUICheckbox::Checked;
248+
}
249+
break;
250+
default:
251+
break;
252+
}
259253
}
254+
ProcessMouseInput();
255+
ProcessKeyboardInput();
260256

257+
m_EditorManager->SetFrameTimeLabelText(s_FrameTime);
261258

259+
return !m_Quit;
262260
}
263261

264262
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -270,59 +268,66 @@ namespace RTEGUI {
270268
}
271269
clear_to_color(m_BackBuffer, 0);
272270

273-
m_EditorBase->Draw(m_Screen.get());
274-
m_ControlManager->Draw();
275-
if (m_SelectionInfo.Control) { DrawSelectionBox(m_SelectionInfo.Control); }
276-
m_LeftColumn->Draw(m_Screen.get());
277-
m_RightColumn->Draw(m_Screen.get());
278-
m_EditorManager->DrawMouse();
271+
m_EditorManager->GetEditorBase()->Draw(m_Screen.get());
272+
m_EditorManager->GetWorkspaceManager()->Draw();
273+
m_EditorManager->GetCurrentSelection().DrawSelectionBox(m_Screen.get(), m_Input.get());
274+
m_EditorManager->GetLeftColumn()->Draw(m_Screen.get());
275+
m_EditorManager->GetRightColumn()->Draw(m_Screen.get());
276+
m_EditorManager->GetControlManager()->DrawMouse();
279277

280-
if (m_Zoom) {
281-
stretch_blit(m_BackBuffer, m_ZoomBuffer, m_WorkspacePosX, m_WorkspacePosY, m_WorkspaceWidth, m_WorkspaceHeight, 0, 0, m_WorkspaceWidth * 2, m_WorkspaceHeight * 2);
282-
blit(m_ZoomBuffer, m_BackBuffer, 0, 0, m_WorkspacePosX, m_WorkspacePosY - 30, m_WorkspaceWidth * 2, m_WorkspaceHeight * 2);
278+
if (m_ZoomWorkspace) {
279+
stretch_blit(m_BackBuffer, m_ZoomBuffer, m_EditorManager->GetWorkspacePosX(), m_EditorManager->GetWorkspacePosY(), m_EditorManager->GetWorkspaceWidth(), m_EditorManager->GetWorkspaceHeight(), 0, 0, m_EditorManager->GetWorkspaceWidth() * 2, m_EditorManager->GetWorkspaceHeight() * 2);
280+
blit(m_ZoomBuffer, m_BackBuffer, 0, 0, m_EditorManager->GetWorkspacePosX(), m_EditorManager->GetWorkspacePosY() - 30, m_EditorManager->GetWorkspaceWidth() * 2, m_EditorManager->GetWorkspaceHeight() * 2);
283281
}
284282
blit(m_BackBuffer, screen, 0, 0, 0, 0, screen->w, screen->h);
285283
}
286284

287285
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
288286

289287
void EditorApp::OnLoadButton(bool addControls) {
288+
if (m_UnsavedChanges) {
289+
int result = EditorUtil::DisplayDialogBox("Save changes made?", win_get_window());
290+
if (result == 0) {
291+
return;
292+
} else if (result == 1) {
293+
OnSaveButton();
294+
}
295+
}
290296
std::string newFilename;
291-
if (EditorUtil::DisplayLoadGUIFile(&newFilename, win_get_window())) {
292-
m_ControlManager->Load(newFilename, addControls);
293-
m_Filename = newFilename;
297+
if (EditorUtil::DisplayLoadFileDialogBox(newFilename, win_get_window())) {
298+
m_EditorManager->GetWorkspaceManager()->Load(newFilename, addControls);
299+
m_ActiveFileName = newFilename;
294300

295-
GUIControl *newRootControl = m_ControlManager->GetControlList()->front();
296-
m_RootControl = newRootControl;
297-
newRootControl->Move(m_WorkspacePosX, m_WorkspacePosY);
301+
GUIControl *newRootControl = m_EditorManager->GetWorkspaceManager()->GetControlList()->front();
302+
newRootControl->Move(m_EditorManager->GetWorkspacePosX(), m_EditorManager->GetWorkspacePosY());
298303
newRootControl->StoreProperties();
299304

300305
GUIProperties newRootControlProps;
301306
newRootControlProps.Update(newRootControl->GetProperties(), true);
302307
newRootControl->GetPanel()->BuildProperties(&newRootControlProps);
303308

304-
m_SelectionInfo.ClearSelection();
305-
m_PropertyPage->ClearValues();
309+
m_EditorManager->ClearCurrentSelection();
310+
m_EditorManager->SetRootControl(newRootControl);
306311
m_UnsavedChanges = false;
307312

308-
PopulateCollectionBoxList();
313+
m_EditorManager->PopulateCollectionBoxList();
309314
}
310315
}
311316

312317
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
313318

314319
void EditorApp::OnSaveButton(bool saveAsNewFile) {
315-
if (saveAsNewFile || m_Filename.empty()) {
320+
if (saveAsNewFile || m_ActiveFileName.empty()) {
316321
std::string newFilename;
317-
if (EditorUtil::DisplaySaveGUIFile(&newFilename, win_get_window())) { m_Filename = newFilename; }
322+
if (EditorUtil::DisplaySaveFileDialogBox(newFilename, win_get_window())) { m_ActiveFileName = newFilename; }
318323
}
319324
// Move the root object to top left corner before saving so it is displayed correctly in-game.
320-
m_RootControl->Move(0, 0);
325+
m_EditorManager->GetRootControl()->Move(0, 0);
321326

322-
m_ControlManager->Save(m_Filename);
327+
m_EditorManager->GetWorkspaceManager()->Save(m_ActiveFileName);
323328

324329
// Move the root object back to the workspace position in the editor
325-
m_RootControl->Move(m_WorkspacePosX, m_WorkspacePosY);
330+
m_EditorManager->GetRootControl()->Move(m_EditorManager->GetWorkspacePosX(), m_EditorManager->GetWorkspacePosY());
326331

327332
m_UnsavedChanges = false;
328333
}
@@ -332,7 +337,7 @@ namespace RTEGUI {
332337
void EditorApp::OnQuitButton() {
333338
int quitResult = 1;
334339
if (m_UnsavedChanges) {
335-
quitResult = EditorUtil::QuitMessageBox("Save changes made?", win_get_window());
340+
quitResult = EditorUtil::DisplayDialogBox("Save changes made?", win_get_window());
336341
if (quitResult == 1) { OnSaveButton(); }
337342
}
338343
m_Quit = (quitResult != 0) ? true : false;
@@ -341,11 +346,7 @@ namespace RTEGUI {
341346
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
342347

343348
void EditorApp::OnWindowResize(RESIZE_DISPLAY_EVENT *resizeInfo) {
344-
m_EditorBase->Resize(resizeInfo->new_w, resizeInfo->new_h);
345-
m_LeftColumn->Resize(m_LeftColumn->GetWidth(), resizeInfo->new_h);
346-
m_RightColumn->Resize(m_RightColumn->GetWidth(), resizeInfo->new_h);
347-
m_RightColumn->Move(resizeInfo->new_w - m_RightColumn->GetWidth(), 0);
348-
m_ControlsInActiveCollectionBoxList->Resize(m_CollectionBoxList->GetWidth(), resizeInfo->new_h - m_ControlsInActiveCollectionBoxList->GetRelYPos() - 5);
349+
m_EditorManager->GetRightColumn()->Move(resizeInfo->new_w - m_EditorManager->GetRightColumn()->GetWidth(), 0);
349350
m_WindowResized = true;
350351
}
351352
}

Editor/EditorApp.h

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#ifndef _RTEGUIEDITORAPP_
22
#define _RTEGUIEDITORAPP_
33

4-
#include "allegro.h"
5-
6-
#include "GUI.h"
7-
#include "GUIPropertyPage.h"
8-
#include "GUICollectionBox.h"
9-
#include "GUIListBox.h"
4+
#include "EditorManager.h"
105
#include "AllegroScreen.h"
116
#include "AllegroInput.h"
127

8+
#include "allegro.h"
9+
1310
#include "Singleton.h"
1411

1512
#define g_GUIEditor EditorApp::Instance()
@@ -25,6 +22,8 @@ namespace RTEGUI {
2522

2623
public:
2724

25+
static int64_t s_FrameTime; //!< Duration between frames in milliseconds.
26+
2827
#pragma region Creation
2928
/// <summary>
3029
/// Constructor method used to instantiate a EditorApp object in system memory.
@@ -34,8 +33,7 @@ namespace RTEGUI {
3433
/// <summary>
3534
/// Initializes the editor app.
3635
/// </summary>
37-
/// <returns>False if initialization failed.</returns>
38-
bool Initialize();
36+
void Initialize();
3937
#pragma endregion
4038

4139
#pragma region Destruction
@@ -45,12 +43,24 @@ namespace RTEGUI {
4543
void DestroyBackBuffers();
4644
#pragma endregion
4745

46+
#pragma region Input Handling
47+
/// <summary>
48+
/// Process the mouse input of the editor.
49+
/// </summary>
50+
void ProcessMouseInput();
51+
52+
/// <summary>
53+
/// Process the keyboard input of the editor.
54+
/// </summary>
55+
void ProcessKeyboardInput();
56+
#pragma endregion
57+
4858
#pragma region Concrete Methods
4959
/// <summary>
5060
/// Updates the editor app.
5161
/// </summary>
5262
/// <returns>False if the editor has quit.</returns>
53-
bool Update();
63+
bool UpdateEditor();
5464

5565
/// <summary>
5666
/// Draws the editor to the screen.
@@ -86,39 +96,24 @@ namespace RTEGUI {
8696

8797
private:
8898

99+
std::unique_ptr<AllegroScreen> m_Screen = nullptr; //!< GUI backbuffer.
100+
std::unique_ptr<AllegroInput> m_Input = nullptr; //!< Input wrapper for Allegro.
101+
std::unique_ptr<EditorManager> m_EditorManager = nullptr; //!< The editor manager that handles all the editor GUI and workspace.
89102

90-
bool m_Quit = false;
91-
bool m_WindowResized = false;
92-
93-
int m_ResX = 1280;
94-
int m_ResY = 600;
95-
BITMAP *m_BackBuffer = nullptr;
96-
BITMAP *m_ZoomBuffer = nullptr;
97-
std::unique_ptr<AllegroScreen> m_Screen = nullptr;
98-
std::unique_ptr<AllegroInput> m_Input = nullptr;
99-
100-
std::array<int, KEY_MAX> m_KeyStates;
101-
std::array<int, KEY_MAX> m_PrevKeyStates;
103+
std::array<int, KEY_MAX> m_KeyStates; //!< The states of the keyboard keys in this update.
104+
std::array<int, KEY_MAX> m_PrevKeyStates; //!< The states of the keyboard keys in the previous update.
102105

103-
GUIControl *m_RootControl = nullptr;
104-
Selection m_SelectionInfo;
105-
std::string m_Filename = "";
106+
bool m_Quit = false; //!< Used for quitting logic.
107+
bool m_WindowResized = false; //!< Indicates the process window dimensions were changed.
106108

107-
// Editor setup
108-
bool m_UnsavedChanges = false;
109-
bool m_Zoom = false;
109+
int m_ResX = 1280; //!< The initial width of the process window.
110+
int m_ResY = 600; //!< The initial height of the process window.
111+
BITMAP *m_BackBuffer = nullptr; //!< Main backbuffer used for drawing the editor.
112+
BITMAP *m_ZoomBuffer = nullptr; //!< Backbuffer used for drawing the zoomed workspace.
110113

111-
#pragma region Input Handling
112-
/// <summary>
113-
/// Process the mouse input of the editor.
114-
/// </summary>
115-
void ProcessMouseInput();
116-
117-
/// <summary>
118-
/// Process the keyboard input of the editor.
119-
/// </summary>
120-
void ProcessKeyboardInput();
121-
#pragma endregion
114+
std::string m_ActiveFileName = ""; //!< The file name the editor is currently editing. If working from a blank workspace, will be assigned once the file is saved.
115+
bool m_UnsavedChanges = false; //!< Indicates there are unsaved changes made to the current file.
116+
bool m_ZoomWorkspace = false; //!< Indicates the workspace should be drawn zoomed in at the drawing stage.
122117

123118
// Disallow the use of some implicit methods.
124119
EditorApp(const EditorApp &reference) = delete;

0 commit comments

Comments
 (0)