Ember
Loading...
Searching...
No Matches
LoadingDialog.cpp
Go to the documentation of this file.
2#include <wx/datetime.h>
3#include <wx/statline.h>
4
7
8 LoadingDialog::LoadingDialog(wxWindow *parent, const wxString &title)
9 : EmberUI::ScalableDialog(parent, wxID_ANY, title, wxSize(1200, 800), wxDEFAULT_DIALOG_STYLE | wxSTAY_ON_TOP),
10 progress_bar_(nullptr), log_text_(nullptr), cancel_button_(nullptr), status_label_(nullptr),
11 was_cancelled_(false), is_complete_(false), current_progress_(0), max_progress_(100) {
12 CreateControls();
13 Centre();
14}
15
17
19 // Create main sizer
20 wxBoxSizer *main_sizer = new wxBoxSizer(wxVERTICAL);
21
22 // Status label (shows current operation)
23 status_label_ = new wxStaticText(this, wxID_ANY, "Initializing...");
24 wxFont status_font = status_label_->GetFont();
25 status_font.SetPointSize(10);
26 status_font.SetWeight(wxFONTWEIGHT_BOLD);
27 status_label_->SetFont(status_font);
28 main_sizer->Add(status_label_, 0, wxALL | wxEXPAND, 10);
29
30 // Progress bar
31 progress_bar_ = new wxGauge(this, wxID_ANY, 100, wxDefaultPosition, wxSize(-1, 25));
32 main_sizer->Add(progress_bar_, 0, wxLEFT | wxRIGHT | wxEXPAND, 10);
33
34 // Progress text (e.g., "50/100")
35 wxStaticText *progress_text = new wxStaticText(this, wxID_ANY, "0%");
36 progress_text->SetName("progress_text");
37 main_sizer->Add(progress_text, 0, wxALL | wxALIGN_CENTER, 5);
38
39 // Separator
40 main_sizer->Add(new wxStaticLine(this), 0, wxALL | wxEXPAND, 5);
41
42 // Log label
43 wxStaticText *log_label = new wxStaticText(this, wxID_ANY, "Parsing Details:");
44 wxFont log_label_font = log_label->GetFont();
45 log_label_font.SetPointSize(9);
46 log_label_font.SetWeight(wxFONTWEIGHT_BOLD);
47 log_label->SetFont(log_label_font);
48 main_sizer->Add(log_label, 0, wxLEFT | wxRIGHT | wxTOP, 10);
49
50 // Log text control (terminal-like, scrollable)
51 log_text_ = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
52 wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2 | wxTE_WORDWRAP | wxHSCROLL | wxVSCROLL);
53
54 // Style the log to look like a terminal
55 wxFont log_font(9, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
56 log_text_->SetFont(log_font);
57 log_text_->SetBackgroundColour(wxColour(30, 30, 30)); // Dark background
58 log_text_->SetForegroundColour(wxColour(200, 200, 200)); // Light gray text
59
60 main_sizer->Add(log_text_, 1, wxALL | wxEXPAND, 10);
61
62 // Cancel button
63 cancel_button_ = new wxButton(this, wxID_CANCEL, "Cancel");
64 main_sizer->Add(cancel_button_, 0, wxALL | wxALIGN_CENTER, 10);
65
66 SetSizer(main_sizer);
67
68 // Initial log message
69 AppendLog("Loading started...", "[INIT]");
70}
71
72bool LoadingDialog::UpdateProgress(int current, int max, const wxString &message) {
73 if (was_cancelled_) {
74 return false;
75 }
76
77 current_progress_ = current;
78 max_progress_ = max;
79
80 // Update progress bar
81 if (progress_bar_) {
82 progress_bar_->SetRange(max);
83 progress_bar_->SetValue(current);
84 }
85
86 // Update progress percentage text
87 wxWindow *progress_text = FindWindowByName("progress_text");
88 if (progress_text) {
89 int percentage = (max > 0) ? (current * 100 / max) : 0;
90 wxStaticText *text = dynamic_cast<wxStaticText *>(progress_text);
91 if (text) {
92 text->SetLabel(wxString::Format("%d%% (%d/%d)", percentage, current, max));
93 }
94 }
95
96 // Update status label if message provided
97 if (!message.IsEmpty()) {
98 if (status_label_) {
99 status_label_->SetLabel(message);
100 }
101
102 // Also append to log
103 AppendLog(message, "[STEP]");
104 }
105
106 // Force UI update
107 Update();
108 wxYield();
109
110 return !was_cancelled_;
111}
112
113void LoadingDialog::AppendLog(const wxString &message, const wxString &prefix) {
114 if (!log_text_)
115 return;
116
117 // Get current timestamp
118 wxDateTime now = wxDateTime::Now();
119 wxString timestamp = now.Format("%H:%M:%S");
120
121 // Format the log message
122 wxString formatted_message;
123 if (!prefix.IsEmpty()) {
124 formatted_message = wxString::Format("[%s] %s %s\n", timestamp.c_str(), prefix.c_str(), message.c_str());
125 } else {
126 formatted_message = wxString::Format("[%s] %s\n", timestamp.c_str(), message.c_str());
127 }
128
129 // Append to log with color coding based on prefix
130 if (prefix.Contains("ERROR") || prefix.Contains("FAIL")) {
131 log_text_->SetDefaultStyle(wxTextAttr(wxColour(255, 100, 100))); // Red
132 } else if (prefix.Contains("WARN")) {
133 log_text_->SetDefaultStyle(wxTextAttr(wxColour(255, 200, 100))); // Orange
134 } else if (prefix.Contains("SUCCESS") || prefix.Contains("COMPLETE")) {
135 log_text_->SetDefaultStyle(wxTextAttr(wxColour(100, 255, 100))); // Green
136 } else if (prefix.Contains("STEP")) {
137 log_text_->SetDefaultStyle(wxTextAttr(wxColour(100, 200, 255))); // Light blue
138 } else {
139 log_text_->SetDefaultStyle(wxTextAttr(wxColour(200, 200, 200))); // Default light gray
140 }
141
142 log_text_->AppendText(formatted_message);
143
144 // Auto-scroll to bottom
145 log_text_->SetInsertionPointEnd();
146 log_text_->ShowPosition(log_text_->GetLastPosition());
147}
148
150 if (log_text_) {
151 log_text_->Clear();
152 }
153}
154
156 max_progress_ = max;
157 if (progress_bar_) {
158 progress_bar_->SetRange(max);
159 }
160}
161
163 is_complete_ = true;
164
165 // Change button to "Close"
166 if (cancel_button_) {
167 cancel_button_->SetLabel("Close");
168 }
169
170 // Update status
171 if (status_label_) {
172 status_label_->SetLabel("Loading Complete - Review logs and click Close");
173 }
174}
175
176void LoadingDialog::OnCancel(wxCommandEvent &event) {
177 if (is_complete_) {
178 // Loading is complete, just close the dialog
179 if (IsModal()) {
180 EndModal(wxID_OK);
181 } else {
182 Hide();
183 }
184 } else {
185 // Loading in progress, cancel it
186 was_cancelled_ = true;
187 AppendLog("Loading cancelled by user", "[CANCEL]");
188
189 // Close the dialog after a brief delay
190 wxMilliSleep(500);
191 if (IsModal()) {
192 EndModal(wxID_CANCEL);
193 } else {
194 Hide();
195 }
196 }
197}
198
199void LoadingDialog::OnClose(wxCloseEvent &event) {
200 if (!is_complete_) {
201 was_cancelled_ = true;
202 }
203 if (IsModal()) {
204 EndModal(is_complete_ ? wxID_OK : wxID_CANCEL);
205 } else {
206 event.Skip();
207 }
208}
BehaviorTreeProjectDialog::OnProjectNameChanged BehaviorTreeProjectDialog::OnRemoveFiles wxEND_EVENT_TABLE() BehaviorTreeProjectDialog
LoadingDialog::OnCancel EVT_CLOSE(LoadingDialog::OnClose) wxEND_EVENT_TABLE() LoadingDialog
wxBEGIN_EVENT_TABLE(LoadingDialog, EmberUI::ScalableDialog) EVT_BUTTON(wxID_CANCEL
MainFrame::OnExit MainFrame::OnNewProject MainFrame::OnCloseProject MainFrame::OnToggleMaximize MainFrame::OnPreviousScene MainFrame::OnPreferences MainFrame::OnEditorTool EVT_BUTTON(ID_MonitorTool, MainFrame::OnMonitorTool) EVT_PAINT(MainFrame
Definition MainFrame.cpp:63
DPI-aware dialog base class for scalable layouts.
Custom loading dialog with detailed progress information.
void MarkComplete()
Mark loading as complete and change button to "Close".
wxButton * cancel_button_
void OnCancel(wxCommandEvent &event)
wxGauge * progress_bar_
void OnClose(wxCloseEvent &event)
void ClearLog()
Clear all log messages.
void SetMaxProgress(int max)
Set the maximum value for the progress bar.
virtual ~LoadingDialog()
bool UpdateProgress(int current, int max, const wxString &message=wxEmptyString)
Update progress and optionally add a log message.
wxStaticText * status_label_
wxTextCtrl * log_text_
void AppendLog(const wxString &message, const wxString &prefix="")
Add a log message without updating progress.
Definition Panel.h:8