00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "simulationView.hpp"
00022 #include "resources/play.xpm"
00023 #include "resources/step.xpm"
00024 #include "resources/pause.xpm"
00025 #include "resources/fforward.xpm"
00026 #include "nanoStatusBar.hpp"
00027 #include <wx/statline.h>
00028
00029 #include <iostream>
00030
00031 using namespace std;
00032
00033 enum
00034 {
00035 ID_BUTTON_PLAYPAUSE = 0,
00036 ID_BUTTON_STEP,
00037 ID_BUTTON_NEXT,
00038 ID_BUTTON_FORWARD,
00039 ID_BUTTON_RESULTS,
00040 ID_TIMER,
00041 ID_SLIDER
00042 };
00043
00044
00045 BEGIN_EVENT_TABLE(SimulationView, wxFrame)
00046 EVT_BUTTON (ID_BUTTON_PLAYPAUSE, SimulationView::OnPlayPause)
00047 EVT_BUTTON (ID_BUTTON_STEP, SimulationView::OnStep)
00048 EVT_BUTTON (ID_BUTTON_NEXT, SimulationView::OnNext)
00049 EVT_BUTTON (ID_BUTTON_FORWARD, SimulationView::OnForward)
00050 EVT_BUTTON (ID_BUTTON_RESULTS, SimulationView::OnResults)
00051 EVT_TIMER (ID_TIMER, SimulationView::OnTimer)
00052 EVT_SLIDER (ID_SLIDER, SimulationView::OnSlider)
00053 END_EVENT_TABLE()
00054
00056
00060 SimulationView::SimulationView(wxWindow* parent, wxWindowID id)
00061 :wxFrame(parent, id, _("Simulation")), chrono(this, ID_TIMER)
00062 {
00063 playing = false;
00064 finished = false;
00065 Maximize();
00066 initControls();
00067 initSizers();
00068 NanoStatusBar *statusBar = new NanoStatusBar(this);
00069 SetStatusBar(statusBar);
00070 timerInterval = 1000;
00071 SetMinSize(wxSize(640, 480));
00072 }
00073
00074
00076 SimulationView::~SimulationView()
00077 {
00078 }
00079
00080
00082 void SimulationView::initControls()
00083 {
00084 canvas = new LayoutCanvas(this, wxID_ANY, Grid());
00085 bitmapPlay = new wxBitmap(play_xpm);
00086 buttonPlayPause = new wxBitmapButton(this, ID_BUTTON_PLAYPAUSE, *bitmapPlay, wxDefaultPosition, wxDefaultSize, wxNO_BORDER);
00087 buttonPlayPause->SetToolTip(_("Start automated simulation"));
00088 bitmapStep = new wxBitmap(step_xpm);
00089 bitmapPause = new wxBitmap(pause_xpm);
00090 bitmapForward = new wxBitmap(fforward_xpm);
00091 buttonStep = new wxBitmapButton(this, ID_BUTTON_STEP, *bitmapStep, wxDefaultPosition, wxDefaultSize, wxNO_BORDER);
00092 buttonStep->SetToolTip(_("Advance one step"));
00093 buttonForward = new wxBitmapButton(this, ID_BUTTON_FORWARD, *bitmapForward, wxDefaultPosition, wxDefaultSize, wxNO_BORDER);
00094 buttonForward->SetToolTip(_("Simulate in one step"));
00095 textInfo = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY | wxTE_MULTILINE);
00096 buttonNextRow = new wxButton(this, ID_BUTTON_NEXT, _("Simulate next row"));
00097 buttonResults = new wxButton(this, ID_BUTTON_RESULTS, _("Show results"));
00098 buttonNextRow->Enable(false);
00099 buttonResults->Enable(false);
00100 slider = new wxSlider(this, ID_SLIDER, 1000, 100, 3000);
00101 labelTimer = new wxStaticText(this, wxID_ANY, _("Timer interval: 1000ms"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE);
00102 }
00103
00104
00106 void SimulationView::initSizers()
00107 {
00108
00109
00110 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
00111
00112
00113
00114
00115
00116 wxStaticBoxSizer *simulationControlsSizer = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Controls"));
00117 wxBoxSizer *canvasButtonsSizer = new wxBoxSizer(wxVERTICAL);
00118 canvasButtonsSizer->Add(canvas, 2, wxEXPAND | wxALL, 2);
00119 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
00120 buttonSizer->Add(buttonPlayPause,
00121 0,
00122 wxSHAPED | wxALIGN_CENTER,
00123 2);
00124 buttonSizer->Add(buttonStep,
00125 0,
00126 wxSHAPED | wxALIGN_CENTER,
00127 2);
00128 buttonSizer->Add(buttonForward,
00129 0,
00130 wxSHAPED | wxALIGN_CENTER,
00131 2);
00132 canvasButtonsSizer->Add(buttonSizer,
00133 0,
00134 wxSHAPED | wxALIGN_CENTER,
00135 2);
00136 canvasButtonsSizer->Add(labelTimer,
00137 0,
00138 wxEXPAND | wxALL,
00139 2);
00140 canvasButtonsSizer->Add(slider,
00141 0,
00142 wxEXPAND | wxALL,
00143 2);
00144 wxBoxSizer *controlButtonSizer = new wxBoxSizer(wxHORIZONTAL);
00145 controlButtonSizer->Add(buttonNextRow,
00146 0,
00147 wxSHAPED | wxALIGN_CENTER,
00148 2);
00149 controlButtonSizer->AddStretchSpacer();
00150 controlButtonSizer->Add(buttonResults,
00151 0,
00152 wxSHAPED | wxALIGN_CENTER,
00153 2);
00154 simulationControlsSizer->Add(canvasButtonsSizer,
00155 3,
00156 wxEXPAND | wxALL,
00157 2);
00158 simulationControlsSizer->Add(textInfo,
00159 2,
00160 wxEXPAND | wxALL,
00161 2);
00162
00163
00164
00165
00166 mainSizer->Add(simulationControlsSizer,
00167 2,
00168 wxEXPAND | wxALL,
00169 2);
00170 mainSizer->Add(controlButtonSizer,
00171 0,
00172 wxEXPAND | wxALL,
00173 2);
00174
00175
00176
00177
00178
00179
00180
00181
00182 SetSizer(mainSizer);
00183
00184 }
00185
00186
00188
00191 void SimulationView::OnPlayPause(wxCommandEvent &event)
00192 {
00193 if (playing)
00194 {
00195 buttonPlayPause->SetBitmapLabel(*bitmapPlay);
00196 buttonPlayPause->SetToolTip(_("Start automated simulation"));
00197 chrono.Stop();
00198 }
00199 else
00200 {
00201 buttonPlayPause->SetBitmapLabel(*bitmapPause);
00202 buttonPlayPause->SetToolTip(_("Pause automated simulation"));
00203 chrono.Start(timerInterval, false);
00204 }
00205 playing = !playing;
00206 }
00207
00208
00210
00213 void SimulationView::OnStep(wxCommandEvent &event)
00214 {
00215 if (!playing)
00216 {
00217 simulation->nextStep(true);
00218 }
00219 }
00220
00221
00223
00227 void SimulationView::setGrid(Grid newGrid, bool changed)
00228 {
00229 canvas->setGrid(newGrid, changed);
00230 Layout();
00231 }
00232
00233
00235
00238 void SimulationView::setInfo(wxString info)
00239 {
00240 textInfo->AppendText(info + _("\n"));
00241 }
00242
00243
00245 void SimulationView::setBlankLine()
00246 {
00247 textInfo->AppendText(_("\n"));
00248 }
00249
00250
00252
00255 void SimulationView::setFinished(bool finished)
00256 {
00257 this->finished = finished;
00258 }
00259
00260
00262
00266 void SimulationView::OnTimer(wxTimerEvent& event)
00267 {
00268 if (playing)
00269 {
00270 simulation->nextStep(true);
00271 }
00272 }
00273
00274
00276
00279 void SimulationView::setSimulation(Simulation *simulation)
00280 {
00281 this->simulation = simulation;
00282 }
00283
00284
00286
00289 void SimulationView::setNextRow(bool enable)
00290 {
00291 buttonNextRow->Enable(enable);
00292 if (enable)
00293 {
00294 setInfo(_("Press the \"Next row\" button to continue simulating"));
00295 }
00296 }
00297
00298
00300
00303 void SimulationView::setResults(bool enable)
00304 {
00305 buttonResults->Enable(enable);
00306 if (enable)
00307 {
00308 setInfo(_("Press the \"Show results\" button to see the simulation results"));
00309 }
00310 }
00311
00312
00314
00317 void SimulationView::OnNext(wxCommandEvent &event)
00318 {
00319 simulation->nextRow();
00320 }
00321
00322
00324
00327 void SimulationView::OnResults(wxCommandEvent &event)
00328 {
00329 simulation->results();
00330 }
00331
00332
00334
00337 void SimulationView::enableSimulation(bool enabled)
00338 {
00339 buttonPlayPause->Enable(enabled);
00340 buttonStep->Enable(enabled);
00341 buttonForward->Enable(enabled);
00342 if (!enabled)
00343 {
00344 buttonPlayPause->SetBitmapLabel(*bitmapPlay);
00345 buttonPlayPause->SetToolTip(_("Start automated simulation"));
00346 chrono.Stop();
00347 playing = false;
00348 }
00349 Update();
00350 }
00351
00352
00354 void SimulationView::stopSimulation()
00355 {
00356 playing = false;
00357 }
00358
00359
00361
00364 void SimulationView::OnSlider(wxCommandEvent &event)
00365 {
00366 timerInterval = slider->GetValue();
00367 labelTimer->SetLabel(wxString::Format(_("Timer interval: %ldms"), timerInterval));
00368 Layout();
00369
00370 if (playing)
00371 {
00372 chrono.Start(timerInterval, false);
00373 }
00374 }
00375
00376
00378
00381 void SimulationView::OnForward(wxCommandEvent &event)
00382 {
00383 simulation->simulateAll();
00384 }