-
Notifications
You must be signed in to change notification settings - Fork 2
/
trains.cpp
437 lines (353 loc) · 12 KB
/
trains.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <string>
#include <vector>
#include <memory>
#include <wx/wx.h>
#include <wx/app.h>
#include <wx/grid.h>
#include <wx/glcanvas.h>
#include "cStation.h"
#include "cTrain.h"
#include "cSimulator.h"
#include "c3D.h"
#include "cTwoD.h"
train::cTwoD * theGraphic;
train::cThreeD * the3D;
train::cSimulator theSim;
/** Display table of stations */
class cDLGStations : public wxDialog
{
wxGrid * myGrid;
public:
cDLGStations( wxWindow* parent );
void Populate();
/** User has double clicked on station table
Click on a particular station to zoom in the 3D animation on that stations
Click on the table header row to zoom out on the entire railway
*/
void onSelectStation( wxGridEvent& event );
};
class cDLGTrains : public wxDialog
{
wxGrid * myGrid;
public:
cDLGTrains( wxWindow* parent )
: wxDialog( parent, -1, "Trains" )
{
wxBoxSizer * szr = new wxBoxSizer( wxVERTICAL );
myGrid = new wxGrid( this, -1,
wxDefaultPosition, // position
wxSize(400,-1) );
myGrid->CreateGrid( 100, 3 );
myGrid->EnableEditing( false );
myGrid->SetColLabelValue(0,"Status");
myGrid->SetColLabelValue(1,"Location");
myGrid->SetColLabelValue(2,"Track");
szr->Add( myGrid );
SetSizer( szr );
}
void Populate()
{
myGrid->ClearGrid();
int row = 0;
for( auto train : theSim.Trains )
{
myGrid->SetCellValue( row,0, train->TextStatus() );
myGrid->SetCellValue( row,1,
train->TextPosition());
myGrid->SetCellValue( row,2,
train->TextTrack());
row++;
}
}
};
// Define a new application type, each program should derive a class from wxApp
class MyApp : public wxApp
{
public:
// override base class virtuals
// ----------------------------
// this one is called on application startup and is a good place for the app
// initialization (doing it here and not in the ctor allows to have an error
// return: if OnInit() returns false, the application terminates)
virtual bool OnInit();
};
// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
// ctor(s)
MyFrame(const wxString& title);
void ConstructMenu();
// event handlers
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnSize(wxSizeEvent& event);
void onTimer( wxTimerEvent& event);
void onViewStations(wxCommandEvent& WXUNUSED(event));
void onViewTrains(wxCommandEvent& WXUNUSED(event));
void onView2D(wxCommandEvent& WXUNUSED(event));
void onView3D(wxCommandEvent& WXUNUSED(event));
void onFileOpen(wxCommandEvent& WXUNUSED(event));
void onFileSave(wxCommandEvent& WXUNUSED(event));
wxTimer myTimer;
cDLGStations * dlgStations;
cDLGTrains * dlgTrains;
private:
// any class wishing to process wxWidgets events must use this macro
DECLARE_EVENT_TABLE()
};
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// IDs for the controls and the menu commands
enum
{
// menu items
Minimal_Quit = wxID_EXIT,
// it is important for the id corresponding to the "About" command to have
// this standard value as otherwise it won't be handled properly under Mac
// (where it is special and put into the "Apple" menu)
Minimal_About = wxID_ABOUT,
TIMER_ID,
IDM_FILE_OPEN,
IDM_FILE_SAVE,
IDM_VIEW_STATIONS,
IDM_VIEW_TRAINS,
IDM_VIEW_2D,
IDM_VIEW_3D,
};
// ----------------------------------------------------------------------------
// event tables and other macros for wxWidgets
// ----------------------------------------------------------------------------
// the event tables connect the wxWidgets events with the functions (event
// handlers) which process them. It can be also done at run-time, but for the
// simple menu events like this the static method is much simpler.
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
EVT_MENU(Minimal_About, MyFrame::OnAbout)
END_EVENT_TABLE()
// Create a new application object: this macro will allow wxWidgets to create
// the application object during program execution (it's better than using a
// static object for many reasons) and also implements the accessor function
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
// not wxApp)
IMPLEMENT_APP(MyApp)
// ============================================================================
// implementation
// ============================================================================
// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------
// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
// call the base class initialization method, currently it only parses a
// few common command-line options but it could be do more in the future
if ( !wxApp::OnInit() )
return false;
// create the main application window
MyFrame *frame = new MyFrame(_T("Trains"));
// and show it (the frames, unlike simple controls, are not shown when
// created initially)
//frame->Show(true);
std::vector< std::string > StationNames
{
"berceni",
"Dimitrie Leonida",
"Aparatorii Patrei",
"Piata Sudului",
"Constantin Brancoveanu",
"Eroii Revoltiei",
"Tineretului",
"Piata Unirii",
"Universtate",
"Piata Romana",
"Piata Victoriei",
"Aviatorilor",
"Aurel Viaacu",
"Pipera"
};
for( int k = 0; k < 14; k++ )
{
station_t stat( new train::cStation(
StationNames[k],
k * 1000 / 13 ));
theSim.Stations.push_back( stat );
}
// success: wxApp::OnRun() will be called which will enter the main message
// loop and the application will run. If we returned false here, the
// application would exit immediately.
return true;
}
// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------
// frame constructor
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
, myTimer(this, TIMER_ID)
{
// set the frame icon
SetIcon(wxICON(sample));
ConstructMenu();
//
//#if wxUSE_STATUSBAR
// // create a status bar just for fun (by default with 1 pane only)
// CreateStatusBar(2);
// SetStatusText(_T("Welcome to Pinmed Licenser!"));
//#endif // wxUSE_STATUSBAR
wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
theGraphic = new train::cTwoD(this);
sizer->Add(theGraphic, 1, wxEXPAND);
SetSizer(sizer);
SetAutoLayout(true);
the3D = new train::cThreeD( this );
Show();
dlgStations = new cDLGStations(this);
dlgTrains = new cDLGTrains(this);
Bind(wxEVT_SIZE,&MyFrame::OnSize,this);
Bind(wxEVT_TIMER, &MyFrame::onTimer,this);
myTimer.Start( 100 );
}
void MyFrame::ConstructMenu()
{
wxMenuBar *menuBar = new wxMenuBar();
wxMenu *fileMenu = new wxMenu;
fileMenu->Append(IDM_FILE_OPEN,"Open");
Bind(wxEVT_MENU,&MyFrame::onFileOpen,this, IDM_FILE_OPEN);
fileMenu->Append(IDM_FILE_SAVE,"Save");
Bind(wxEVT_MENU,&MyFrame::onFileSave,this, IDM_FILE_SAVE);
fileMenu->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
menuBar->Append(fileMenu, _T("&File"));
wxMenu *viewMenu = new wxMenu;
wxMenu *tableMenu = new wxMenu;
tableMenu->AppendRadioItem(IDM_VIEW_STATIONS, "Stations");
Bind(wxEVT_MENU,&MyFrame::onViewStations,this,IDM_VIEW_STATIONS);
tableMenu->AppendRadioItem(IDM_VIEW_TRAINS, "Trains");
Bind(wxEVT_MENU,&MyFrame::onViewTrains,this,IDM_VIEW_TRAINS);
viewMenu->AppendSubMenu( tableMenu, "Tables");
wxMenu *animMenu = new wxMenu;
animMenu->AppendRadioItem(IDM_VIEW_2D, "Two D ( GDI )");
Bind(wxEVT_MENU,&MyFrame::onView2D,this,IDM_VIEW_2D);
animMenu->AppendRadioItem(IDM_VIEW_3D, "Three D ( OpenGL )");
Bind(wxEVT_MENU,&MyFrame::onView3D,this,IDM_VIEW_3D);
menuBar->Append(viewMenu, _T("View"));
viewMenu->AppendSubMenu( animMenu, "Animation");
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
menuBar->Append(helpMenu, _T("&Help"));
SetMenuBar(menuBar);
}
// event handlers
void MyFrame::OnSize(wxSizeEvent& event )
{
Refresh();
int w, h;
GetClientSize( &w, &h );
the3D->Size( w, h );
event.Skip();
}
void MyFrame::onTimer( wxTimerEvent& event)
{
theSim.Advance();
Refresh();
the3D->Render();
if( theSim.Time == 1 )
{
dlgStations->Populate();
}
dlgTrains->Populate();
}
void MyFrame::onFileSave(wxCommandEvent& WXUNUSED(event))
{
wxFileDialog saveFileDialog(this, _("Save file"), "", "",
"", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
if (saveFileDialog.ShowModal() == wxID_CANCEL)
return; // the user changed idea...
if( ! theSim.FileSave( saveFileDialog.GetPath().ToStdString() ) )
wxMessageBox("Failed to save model");
}
void MyFrame::onFileOpen(wxCommandEvent& WXUNUSED(event))
{
wxFileDialog
openFileDialog(this, _("Open file"), "", "",
"", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() == wxID_CANCEL)
return; // the user changed idea...
if( ! theSim.FileOpen( openFileDialog.GetPath().ToStdString() ) )
wxMessageBox("Failed to read model");
}
void MyFrame::onViewStations(wxCommandEvent& WXUNUSED(event))
{
dlgStations->Show();
dlgTrains->Hide();
}
void MyFrame::onViewTrains(wxCommandEvent& WXUNUSED(event))
{
dlgStations->Hide();
dlgTrains->Show();
}
void MyFrame::onView2D(wxCommandEvent& WXUNUSED(event))
{
the3D->myCanvas->Hide();
theGraphic->Show();
}
void MyFrame::onView3D(wxCommandEvent& WXUNUSED(event))
{
the3D->myCanvas->Show();
theGraphic->Hide();
int w, h;
GetClientSize( &w, &h );
the3D->Size( w, h );
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
// true is to force the frame to close
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox("Train Simulator",
_T("Trains"),
wxOK | wxICON_INFORMATION,
this);
}
void cDLGStations::onSelectStation( wxGridEvent& event )
{
int index = event.GetRow();
if( index >= 0 )
the3D->FocusStation( theSim.Stations[ index ] );
else
{
station_t null;
the3D->FocusStation( null );
}
}
cDLGStations::cDLGStations( wxWindow* parent )
: wxDialog( parent, -1, "Stations" )
{
wxBoxSizer * szr = new wxBoxSizer( wxVERTICAL );
myGrid = new wxGrid( this, -1,
wxDefaultPosition, // position
wxSize(-1,-1) );
myGrid->CreateGrid( 14, 2 );
myGrid->EnableEditing( false );
myGrid->SetColLabelValue(0,"Name");
myGrid->SetColSize( 0, 220 );
myGrid->SetColLabelValue(1,"Distance");
szr->Add( myGrid );
SetSizerAndFit( szr );
Bind( wxEVT_GRID_CELL_LEFT_DCLICK, &cDLGStations::onSelectStation, this );
Bind( wxEVT_GRID_LABEL_LEFT_DCLICK, &cDLGStations::onSelectStation, this );
}
void cDLGStations::Populate()
{
int row = 0;
for( auto station : theSim.Stations )
{
myGrid->SetCellValue( row,0, station->myName );
myGrid->SetCellValue( row,1,
wxString::Format("%d",station->myLocation ));
row++;
}
}