Skyscraper 2.0
gui.cpp
Go to the documentation of this file.
1/*
2 Skyscraper 2.1 - GUI Manager
3 Copyright (C)2003-2024 Ryan Thoryk
4 https://www.skyscrapersim.net
5 https://sourceforge.net/projects/skyscraper/
6 Contact - ryan@skyscrapersim.net
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21*/
22
23#include "wx/wx.h"
24#include "wx/dir.h"
25#include "wx/progdlg.h"
26#include "wx/cmdline.h"
27#include "wx/filename.h"
28#include "wx/filefn.h"
29#include "wx/stdpaths.h"
30#include "globals.h"
31#include "sbs.h"
32#include "vm.h"
33#include "hal.h"
34#include "enginecontext.h"
35#include "debugpanel.h"
36#include "console.h"
37#include "loaddialog.h"
38#include "textwindow.h"
39#include "gui.h"
40
41namespace Skyscraper {
42
43GUI::GUI(VM *parent)
44{
45 this->vm = parent;
46 console = 0;
47 progdialog = 0;
48 dpanel = 0;
49 loaddialog = 0;
50 show_progress = false;
51 wxInitAllImageHandlers();
52}
53
55{
56 Unload();
57
58 //delete building load dialog
59 if (loaddialog)
60 loaddialog->Destroy();
61 loaddialog = 0;
62
63 //delete progress dialog
64 if (progdialog)
65 progdialog->Destroy();
66 progdialog = 0;
67
68 //delete console window
69 if (console)
70 console->Destroy();
71 console = 0;
72
74}
75
77{
78 //delete control panel object
79 if(dpanel)
80 delete dpanel;
81 dpanel = 0;
82}
83
84void GUI::ShowError(const std::string &message)
85{
86 //show error dialog
87 wxMessageDialog dialog(0, message, _("Skyscraper"), wxOK | wxICON_ERROR);
88 dialog.ShowModal();
89}
90
91void GUI::ShowMessage(const std::string &message)
92{
93 //show message dialog
94 wxMessageDialog dialog(0, message, _("Skyscraper"), wxOK | wxICON_INFORMATION);
95 dialog.ShowModal();
96}
97
99{
100 //show progress dialog if needed
101 if (show_progress == true)
103}
104
105std::string GUI::SelectBuilding(const std::string &data_path)
106{
107 //choose a building from a script file
108
109 std::string filename = "";
110
111 //get listing of building files
112 wxArrayString filelist;
113 wxArrayString filelist2;
114 wxDir::GetAllFiles(_("buildings/"), &filelist, _("*.bld"), wxDIR_FILES);
115 wxDir::GetAllFiles(_(data_path + "buildings/"), &filelist2, _("*.bld"), wxDIR_FILES);
116
117 //strip directory name and extension from entries
118 for (size_t i = 0; i < filelist.size(); i++)
119 {
120 filelist[i] = filelist[i].substr(10);
121 filelist[i] = filelist[i].substr(0, filelist[i].length() - 4);
122 }
123 for (size_t i = 0; i < filelist2.size(); i++)
124 {
125 filelist2[i] = filelist2[i].substr(data_path.length() + 10);
126 filelist2[i] = filelist2[i].substr(0, filelist2[i].length() - 4);
127 }
128
129 for (size_t i = 0; i < filelist2.size(); i++)
130 {
131 bool found = false;
132 for (size_t j = 0; j < filelist.size(); j++)
133 {
134 if (filelist[j] == filelist2[i])
135 {
136 found = true;
137 break;
138 }
139 }
140 if (found == false)
141 filelist.Add(filelist2[i]);
142 }
143
144 //sort list
145 filelist.Sort();
146
147 //show selection dialog window
148 wxSingleChoiceDialog Selector (0, _("Select a Building"), _("Load Building"), filelist);
149 Selector.SetSize(wxSize(500, 400));
150 Selector.CenterOnScreen();
151
152 if (Selector.ShowModal() == wxID_OK)
153 {
154 filename = Selector.GetStringSelection();
155 filename += ".bld";
156 }
157
158 return filename;
159}
160
161std::string GUI::SelectBuildingNative(const std::string &data_path)
162{
163 //choose a building from a script file, using a native file selection dialog
164 std::string filename = "";
165 srand (time (0));
166
167 //set building file
168 //wxFileDialog *Selector = new wxFileDialog(0, _("Select a Building"), _("buildings/"), _(""), _("Building files (*.bld;*.txt)|*.bld;*.txt"), wxFD_OPEN);
169 wxFileDialog *Selector = new wxFileDialog(0, _("Select a Building"), _("buildings/"), _(""), _("Building files (*.bld)|*.bld"), wxFD_OPEN);
170 int result = Selector->ShowModal();
171 if (result == wxID_CANCEL)
172 {
173 //delete dialog
174 delete Selector;
175 Selector = 0;
176 //quit
177 return "";
178 }
179
180#if defined(wxUSE_UNICODE) && wxUSE_UNICODE
181 filename = Selector->GetFilename().mb_str().data();
182#else
183 filename = Selector->GetFilename();
184#endif
185
186 //delete dialog
187 delete Selector;
188 Selector = 0;
189
190 return filename;
191}
192
194{
195 if (!dpanel)
196 dpanel = new DebugPanel(vm, vm->GetParent(), -1);
197 dpanel->Show(true);
198 HAL *hal = vm->GetHAL();
199 dpanel->SetPosition(wxPoint(hal->GetConfigInt(hal->configfile, "Skyscraper.Frontend.ControlPanelX", 10), hal->GetConfigInt(hal->configfile, "Skyscraper.Frontend.ControlPanelY", 25)));
200}
201
202void GUI::EnableConsole(bool value)
203{
204 if (console)
205 console->bSend->Enable(value);
206}
207
208void GUI::EnableTimer(bool value)
209{
210 if(dpanel)
211 dpanel->EnableTimer(value);
212}
213
214void GUI::ShowConsole(bool send_button)
215{
216 if (!console)
217 console = new Console(vm, vm->GetParent(), -1);
218 console->Show();
219 console->Raise();
220 HAL *hal = vm->GetHAL();
221 console->SetPosition(wxPoint(hal->GetConfigInt(hal->configfile, "Skyscraper.Frontend.ConsoleX", 10), hal->GetConfigInt(hal->configfile, "Skyscraper.Frontend.ConsoleY", 25)));
222 console->bSend->Enable(send_button);
223}
224
225void GUI::CreateProgressDialog(const std::string &message)
226{
227 //don't create progress dialog if concurrent loading is enabled, and one engine is already running
228 if (vm->GetEngineCount() > 1 && vm->ConcurrentLoads == true)
229 {
230 if (vm->GetFirstValidEngine()->IsRunning() == true)
231 return;
232 }
233
234 if (!progdialog)
235 {
236 //show progress dialog in a queued fashion
237 show_progress = true;
238 prog_text = message;
239 }
240 else
241 {
242 wxString msg = progdialog->GetMessage();
243 msg += "\n";
244 msg += message;
245 progdialog->Update(progdialog->GetValue(), msg);
246 progdialog->Fit();
247 }
248
249 //stop control panel timer
250 EnableTimer(false);
251}
252
254{
255 //close progress dialog
256 if (progdialog)
257 progdialog->Destroy();
258 progdialog = 0;
259
260 //start control panel timer
261 EnableTimer(true);
262}
263
265{
266 if (!progdialog)
267 progdialog = new wxProgressDialog(wxT("Loading..."), prog_text, 100, vm->GetParent(), wxPD_AUTO_HIDE|wxPD_APP_MODAL|wxPD_CAN_ABORT|wxPD_ELAPSED_TIME|wxPD_ESTIMATED_TIME);
268
269 show_progress = false;
270}
271
272bool GUI::UpdateProgress(int percent)
273{
274 //update progress dialog with the specified percent
275 if (!progdialog)
276 return true;
277
278 bool result = progdialog->Update(percent);
279 progdialog->Fit();
280 return result;
281}
282
284{
285 if (!progdialog)
286 return false;
287
288 return progdialog->WasCancelled();
289}
290
292{
293 if (console)
294 {
295 console->Refresh();
296 console->Update();
297 }
298}
299
301{
302 vm->GetParent()->Raise();
303 vm->GetParent()->SetFocus();
304}
305
307{
308 if (dpanel)
309 return;
310
311 //show control panel if closed
312 dpanel = new DebugPanel(vm, vm->GetParent(), -1);
313 dpanel->Show(true);
314 dpanel->SetPosition(wxPoint(vm->GetHAL()->GetConfigInt(vm->GetHAL()->configfile, "Skyscraper.Frontend.ControlPanelX", 10), vm->GetHAL()->GetConfigInt(vm->GetHAL()->configfile, "Skyscraper.Frontend.ControlPanelY", 25)));
315}
316
321
323{
324 if (!loaddialog)
325 loaddialog = new LoadDialog(dpanel, vm->GetParent(), -1);
326 loaddialog->CenterOnScreen();
327 loaddialog->Show();
328}
329
330void GUI::WriteToConsole(const std::string &message, const std::string &color)
331{
332 if (console)
333 {
334 console->Write(message, color);
335 console->Update();
336 }
337}
338
339bool GUI::ReportMissingFiles(std::vector<std::string> &missing_files)
340{
341 //report on missing files
342 //returns true if any files are missing
343
344 if (missing_files.size() > 0)
345 {
346 sort(missing_files.begin(), missing_files.end());
347 for (size_t i = 0; i < missing_files.size(); i++)
348 vm->GetHAL()->Report("Missing file: " + missing_files[i], "gui:");
349
350 //create text window
351 TextWindow *twindow = new TextWindow(NULL, -1);
352 twindow->SetMinSize(wxSize(350, 250));
353 twindow->tMain->SetMinSize(wxSize(350, 250));
354 twindow->Fit();
355 twindow->Center();
356 twindow->SetTitle(wxT("Missing Files"));
357 twindow->Show(true);
358 wxString message;
359 message = wxT("Skyscraper was unable to load the following files.\nThis will result in texture and/or sound problems:\n\n");
360 for (size_t i = 0; i < missing_files.size(); i++)
361 {
362 message.Append(missing_files[i]);
363 message.Append(wxT("\n"));
364 }
365 twindow->tMain->WriteText(message);
366 twindow->tMain->SetInsertionPoint(0);
367 return true;
368 }
369 else
370 return false;
371 return true;
372}
373
375{
376 return (console != 0);
377}
378
379}
wxButton * bSend
Definition console.h:46
void Write(const std::string &message, const std::string &color)
Definition console.cpp:128
void EnableTimer(bool value)
bool ProgressCancelled()
Definition gui.cpp:283
void ShowProgress()
Definition gui.cpp:98
void CreateDebugPanel()
Definition gui.cpp:193
void ShowError(const std::string &message)
Definition gui.cpp:84
void WriteToConsole(const std::string &message, const std::string &color="white")
Definition gui.cpp:330
void ShowMessage(const std::string &message)
Definition gui.cpp:91
bool UpdateProgress(int percent)
Definition gui.cpp:272
void ShowConsole(bool send_button=true)
Definition gui.cpp:214
std::string prog_text
Definition gui.h:88
GUI(VM *parent)
Definition gui.cpp:43
void CreateProgressDialog(const std::string &message)
Definition gui.cpp:225
wxProgressDialog * progdialog
Definition gui.h:75
std::string SelectBuilding(const std::string &data_path)
Definition gui.cpp:105
VM * vm
Definition gui.h:90
Console * console
Definition gui.h:81
void RefreshConsole()
Definition gui.cpp:291
bool IsConsoleVisible()
Definition gui.cpp:374
void ShowDebugPanel()
Definition gui.cpp:306
bool show_progress
Definition gui.h:87
void ShowProgressDialog()
Definition gui.cpp:264
void CloseProgressDialog()
Definition gui.cpp:253
void EnableTimer(bool value)
Definition gui.cpp:208
LoadDialog * loaddialog
Definition gui.h:84
void ShowControlReference()
Definition gui.cpp:317
void EnableConsole(bool value)
Definition gui.cpp:202
void ShowLoadDialog()
Definition gui.cpp:322
DebugPanel * dpanel
Definition gui.h:78
void RaiseWindow()
Definition gui.cpp:300
void Unload()
Definition gui.cpp:76
std::string SelectBuildingNative(const std::string &data_path)
Definition gui.cpp:161
bool ReportMissingFiles(std::vector< std::string > &missing_files)
Definition gui.cpp:339
int GetConfigInt(Ogre::ConfigFile *file, const std::string &key, int default_value)
Definition hal.cpp:286
void Report(const std::string &message, const std::string &prompt)
Definition hal.cpp:219
Ogre::ConfigFile * configfile
Definition hal.h:97
HAL * GetHAL()
Definition vm.cpp:128
EngineContext * GetFirstValidEngine()
Definition vm.cpp:568
int GetEngineCount(bool loading_only=false)
Definition vm.cpp:442
bool ConcurrentLoads
Definition vm.h:116
wxWindow * GetParent()
Definition vm.cpp:902
TextWindow * twindow
Definition uexception.h:23