]>
Commit | Line | Data |
---|---|---|
5a1dca12 GRG |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: life.cpp | |
29b07a38 | 3 | // Purpose: The game of Life, created by J. H. Conway |
5a1dca12 GRG |
4 | // Author: Guillermo Rodriguez Garcia, <guille@iies.es> |
5 | // Modified by: | |
6 | // Created: Jan/2000 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000, Guillermo Rodriguez Garcia | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ========================================================================== | |
e0a40292 | 13 | // headers, declarations, constants |
5a1dca12 GRG |
14 | // ========================================================================== |
15 | ||
5a1dca12 | 16 | #ifdef __GNUG__ |
2480be69 | 17 | #pragma implementation "life.h" |
5a1dca12 GRG |
18 | #endif |
19 | ||
2fa7c206 JS |
20 | // For compilers that support precompilation, includes "wx/wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
29b07a38 | 24 | #pragma hdrstop |
2fa7c206 JS |
25 | #endif |
26 | ||
27 | #ifndef WX_PRECOMP | |
29b07a38 | 28 | #include "wx/wx.h" |
2fa7c206 JS |
29 | #endif |
30 | ||
5a1dca12 | 31 | #include "wx/statline.h" |
f6bcfd97 BP |
32 | #include "wx/wfstream.h" |
33 | #include "wx/filedlg.h" | |
2480be69 GRG |
34 | |
35 | #include "life.h" | |
36 | #include "game.h" | |
37 | #include "dialogs.h" | |
f6bcfd97 | 38 | #include "reader.h" |
5a1dca12 GRG |
39 | |
40 | // -------------------------------------------------------------------------- | |
41 | // resources | |
42 | // -------------------------------------------------------------------------- | |
43 | ||
e334d0ea | 44 | #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__) |
29b07a38 | 45 | // application icon |
5a1dca12 GRG |
46 | #include "mondrian.xpm" |
47 | ||
48 | // bitmap buttons for the toolbar | |
49 | #include "bitmaps/reset.xpm" | |
f6bcfd97 | 50 | #include "bitmaps/open.xpm" |
5a1dca12 GRG |
51 | #include "bitmaps/play.xpm" |
52 | #include "bitmaps/stop.xpm" | |
e0a40292 GRG |
53 | #include "bitmaps/zoomin.xpm" |
54 | #include "bitmaps/zoomout.xpm" | |
f6bcfd97 | 55 | #include "bitmaps/info.xpm" |
29b07a38 GRG |
56 | |
57 | // navigator | |
58 | #include "bitmaps/north.xpm" | |
59 | #include "bitmaps/south.xpm" | |
60 | #include "bitmaps/east.xpm" | |
61 | #include "bitmaps/west.xpm" | |
62 | #include "bitmaps/center.xpm" | |
5a1dca12 GRG |
63 | #endif |
64 | ||
5a1dca12 GRG |
65 | // -------------------------------------------------------------------------- |
66 | // constants | |
67 | // -------------------------------------------------------------------------- | |
68 | ||
22c1c01b DS |
69 | // IDs for the controls and the menu commands. Exluding those already defined |
70 | // by wxWindows, such as wxID_NEW. | |
5a1dca12 GRG |
71 | enum |
72 | { | |
29b07a38 | 73 | // timer |
22c1c01b | 74 | ID_TIMER = wxID_HIGHEST, |
29b07a38 | 75 | |
f6bcfd97 | 76 | // file menu |
087e4f4a | 77 | ID_SAMPLES, |
29b07a38 | 78 | |
f6bcfd97 | 79 | // view menu |
29b07a38 GRG |
80 | ID_SHOWNAV, |
81 | ID_ORIGIN, | |
82 | ID_CENTER, | |
83 | ID_NORTH, | |
84 | ID_SOUTH, | |
85 | ID_EAST, | |
86 | ID_WEST, | |
f6bcfd97 BP |
87 | ID_ZOOMIN, |
88 | ID_ZOOMOUT, | |
89 | ID_INFO, | |
90 | ||
91 | // game menu | |
92 | ID_START, | |
93 | ID_STEP, | |
94 | ID_STOP, | |
95 | ID_TOPSPEED, | |
96 | ||
97 | // speed selection slider | |
22c1c01b | 98 | ID_SLIDER |
5a1dca12 GRG |
99 | }; |
100 | ||
101 | // -------------------------------------------------------------------------- | |
102 | // event tables and other macros for wxWindows | |
103 | // -------------------------------------------------------------------------- | |
104 | ||
105 | // Event tables | |
5a1dca12 | 106 | BEGIN_EVENT_TABLE(LifeFrame, wxFrame) |
22c1c01b DS |
107 | EVT_MENU (wxID_NEW, LifeFrame::OnMenu) |
108 | EVT_MENU (wxID_OPEN, LifeFrame::OnOpen) | |
e0a40292 | 109 | EVT_MENU (ID_SAMPLES, LifeFrame::OnSamples) |
22c1c01b DS |
110 | EVT_MENU (wxID_ABOUT, LifeFrame::OnMenu) |
111 | EVT_MENU (wxID_EXIT, LifeFrame::OnMenu) | |
29b07a38 GRG |
112 | EVT_MENU (ID_SHOWNAV, LifeFrame::OnMenu) |
113 | EVT_MENU (ID_ORIGIN, LifeFrame::OnNavigate) | |
114 | EVT_BUTTON (ID_CENTER, LifeFrame::OnNavigate) | |
115 | EVT_BUTTON (ID_NORTH, LifeFrame::OnNavigate) | |
116 | EVT_BUTTON (ID_SOUTH, LifeFrame::OnNavigate) | |
117 | EVT_BUTTON (ID_EAST, LifeFrame::OnNavigate) | |
118 | EVT_BUTTON (ID_WEST, LifeFrame::OnNavigate) | |
119 | EVT_MENU (ID_ZOOMIN, LifeFrame::OnZoom) | |
120 | EVT_MENU (ID_ZOOMOUT, LifeFrame::OnZoom) | |
f6bcfd97 BP |
121 | EVT_MENU (ID_INFO, LifeFrame::OnMenu) |
122 | EVT_MENU (ID_START, LifeFrame::OnMenu) | |
123 | EVT_MENU (ID_STEP, LifeFrame::OnMenu) | |
124 | EVT_MENU (ID_STOP, LifeFrame::OnMenu) | |
125 | EVT_MENU (ID_TOPSPEED, LifeFrame::OnMenu) | |
e0a40292 | 126 | EVT_COMMAND_SCROLL (ID_SLIDER, LifeFrame::OnSlider) |
29b07a38 | 127 | EVT_TIMER (ID_TIMER, LifeFrame::OnTimer) |
e0a40292 | 128 | EVT_CLOSE ( LifeFrame::OnClose) |
ecbdd409 | 129 | END_EVENT_TABLE() |
5a1dca12 | 130 | |
29b07a38 GRG |
131 | BEGIN_EVENT_TABLE(LifeNavigator, wxMiniFrame) |
132 | EVT_CLOSE ( LifeNavigator::OnClose) | |
133 | END_EVENT_TABLE() | |
134 | ||
e0a40292 GRG |
135 | BEGIN_EVENT_TABLE(LifeCanvas, wxWindow) |
136 | EVT_PAINT ( LifeCanvas::OnPaint) | |
137 | EVT_SCROLLWIN ( LifeCanvas::OnScroll) | |
138 | EVT_SIZE ( LifeCanvas::OnSize) | |
f6bcfd97 BP |
139 | EVT_MOTION ( LifeCanvas::OnMouse) |
140 | EVT_LEFT_DOWN ( LifeCanvas::OnMouse) | |
141 | EVT_LEFT_UP ( LifeCanvas::OnMouse) | |
142 | EVT_LEFT_DCLICK ( LifeCanvas::OnMouse) | |
e0a40292 | 143 | EVT_ERASE_BACKGROUND( LifeCanvas::OnEraseBackground) |
ecbdd409 | 144 | END_EVENT_TABLE() |
5a1dca12 | 145 | |
5a1dca12 GRG |
146 | |
147 | // Create a new application object | |
148 | IMPLEMENT_APP(LifeApp) | |
149 | ||
e0a40292 | 150 | |
5a1dca12 GRG |
151 | // ========================================================================== |
152 | // implementation | |
153 | // ========================================================================== | |
154 | ||
2480be69 | 155 | // some shortcuts |
f6bcfd97 | 156 | #define ADD_TOOL(id, bmp, tooltip, help) \ |
22c1c01b | 157 | toolBar->AddTool(id, bmp, wxNullBitmap, false, -1, -1, (wxObject *)0, tooltip, help) |
2480be69 | 158 | |
2480be69 | 159 | |
5a1dca12 GRG |
160 | // -------------------------------------------------------------------------- |
161 | // LifeApp | |
162 | // -------------------------------------------------------------------------- | |
163 | ||
e0a40292 | 164 | // 'Main program' equivalent: the program execution "starts" here |
5a1dca12 GRG |
165 | bool LifeApp::OnInit() |
166 | { | |
167 | // create the main application window | |
168 | LifeFrame *frame = new LifeFrame(); | |
169 | ||
170 | // show it and tell the application that it's our main window | |
22c1c01b | 171 | frame->Show(true); |
5a1dca12 GRG |
172 | SetTopWindow(frame); |
173 | ||
dbf75be7 GRG |
174 | // just for Motif |
175 | #ifdef __WXMOTIF__ | |
176 | frame->UpdateInfoText(); | |
177 | #endif | |
178 | ||
5a1dca12 | 179 | // enter the main message loop and run the app |
22c1c01b | 180 | return true; |
5a1dca12 GRG |
181 | } |
182 | ||
183 | // -------------------------------------------------------------------------- | |
184 | // LifeFrame | |
185 | // -------------------------------------------------------------------------- | |
186 | ||
187 | // frame constructor | |
22c1c01b DS |
188 | LifeFrame::LifeFrame() : wxFrame( (wxFrame *) NULL, wxID_ANY, |
189 | _("Life!"), wxPoint(200, 200) ) | |
5a1dca12 GRG |
190 | { |
191 | // frame icon | |
192 | SetIcon(wxICON(mondrian)); | |
193 | ||
194 | // menu bar | |
22c1c01b DS |
195 | wxMenu *menuFile = new wxMenu(wxMENU_TEAROFF); |
196 | wxMenu *menuView = new wxMenu(wxMENU_TEAROFF); | |
197 | wxMenu *menuGame = new wxMenu(wxMENU_TEAROFF); | |
198 | wxMenu *menuHelp = new wxMenu(wxMENU_TEAROFF); | |
5a1dca12 | 199 | |
22c1c01b DS |
200 | menuFile->Append(wxID_NEW, _("&New"), _("Start a new game")); |
201 | menuFile->Append(wxID_OPEN, _("&Open..."), _("Open an existing Life pattern")); | |
f6bcfd97 | 202 | menuFile->Append(ID_SAMPLES, _("&Sample game..."), _("Select a sample configuration")); |
5a1dca12 | 203 | menuFile->AppendSeparator(); |
22c1c01b | 204 | menuFile->Append(wxID_EXIT, _("E&xit\tAlt-X"), _("Quit this program")); |
087e4f4a | 205 | |
22c1c01b DS |
206 | menuView->Append(ID_SHOWNAV, _("Navigation &toolbox"), _("Show or hide toolbox"), wxITEM_CHECK); |
207 | menuView->Check(ID_SHOWNAV, true); | |
29b07a38 | 208 | menuView->AppendSeparator(); |
f6bcfd97 BP |
209 | menuView->Append(ID_ORIGIN, _("&Absolute origin"), _("Go to (0, 0)")); |
210 | menuView->Append(ID_CENTER, _("&Center of mass"), _("Find center of mass")); | |
211 | menuView->Append(ID_NORTH, _("&North"), _("Find northernmost cell")); | |
212 | menuView->Append(ID_SOUTH, _("&South"), _("Find southernmost cell")); | |
213 | menuView->Append(ID_EAST, _("&East"), _("Find easternmost cell")); | |
214 | menuView->Append(ID_WEST, _("&West"), _("Find westernmost cell")); | |
29b07a38 | 215 | menuView->AppendSeparator(); |
f6bcfd97 BP |
216 | menuView->Append(ID_ZOOMIN, _("Zoom &in\tCtrl-I"), _("Zoom in")); |
217 | menuView->Append(ID_ZOOMOUT, _("Zoom &out\tCtrl-O"), _("Zoom out")); | |
22c1c01b | 218 | menuView->Append(ID_INFO, _("&Description\tCtrl-D"), _("View pattern description")); |
29b07a38 | 219 | |
087e4f4a GRG |
220 | menuGame->Append(ID_START, _("&Start\tCtrl-S"), _("Start")); |
221 | menuGame->Append(ID_STEP, _("&Next\tCtrl-N"), _("Single step")); | |
222 | menuGame->Append(ID_STOP, _("S&top\tCtrl-T"), _("Stop")); | |
22c1c01b | 223 | menuGame->Enable(ID_STOP, false); |
087e4f4a | 224 | menuGame->AppendSeparator(); |
f6bcfd97 | 225 | menuGame->Append(ID_TOPSPEED, _("T&op speed!"), _("Go as fast as possible")); |
087e4f4a | 226 | |
22c1c01b DS |
227 | menuHelp->Append(wxID_ABOUT, _("&About\tCtrl-A"), _("Show about dialog")); |
228 | ||
5a1dca12 GRG |
229 | wxMenuBar *menuBar = new wxMenuBar(); |
230 | menuBar->Append(menuFile, _("&File")); | |
29b07a38 | 231 | menuBar->Append(menuView, _("&View")); |
087e4f4a | 232 | menuBar->Append(menuGame, _("&Game")); |
22c1c01b | 233 | menuBar->Append(menuHelp, _("&Help")); |
5a1dca12 GRG |
234 | SetMenuBar(menuBar); |
235 | ||
236 | // tool bar | |
f6bcfd97 | 237 | wxBitmap tbBitmaps[7]; |
471ed537 | 238 | |
5a1dca12 | 239 | tbBitmaps[0] = wxBITMAP(reset); |
f6bcfd97 BP |
240 | tbBitmaps[1] = wxBITMAP(open); |
241 | tbBitmaps[2] = wxBITMAP(zoomin); | |
242 | tbBitmaps[3] = wxBITMAP(zoomout); | |
243 | tbBitmaps[4] = wxBITMAP(info); | |
244 | tbBitmaps[5] = wxBITMAP(play); | |
245 | tbBitmaps[6] = wxBITMAP(stop); | |
5a1dca12 GRG |
246 | |
247 | wxToolBar *toolBar = CreateToolBar(); | |
248 | toolBar->SetMargins(5, 5); | |
249 | toolBar->SetToolBitmapSize(wxSize(16, 16)); | |
f6bcfd97 | 250 | |
22c1c01b DS |
251 | ADD_TOOL(wxID_NEW, tbBitmaps[0], _("New"), _("Start a new game")); |
252 | ADD_TOOL(wxID_OPEN, tbBitmaps[1], _("Open"), _("Open an existing Life pattern")); | |
f6bcfd97 BP |
253 | toolBar->AddSeparator(); |
254 | ADD_TOOL(ID_ZOOMIN, tbBitmaps[2], _("Zoom in"), _("Zoom in")); | |
255 | ADD_TOOL(ID_ZOOMOUT, tbBitmaps[3], _("Zoom out"), _("Zoom out")); | |
256 | ADD_TOOL(ID_INFO, tbBitmaps[4], _("Description"), _("Show description")); | |
e0a40292 | 257 | toolBar->AddSeparator(); |
f6bcfd97 BP |
258 | ADD_TOOL(ID_START, tbBitmaps[5], _("Start"), _("Start")); |
259 | ADD_TOOL(ID_STOP, tbBitmaps[6], _("Stop"), _("Stop")); | |
260 | ||
5a1dca12 | 261 | toolBar->Realize(); |
22c1c01b | 262 | toolBar->EnableTool(ID_STOP, false); // must be after Realize() ! |
5a1dca12 GRG |
263 | |
264 | // status bar | |
265 | CreateStatusBar(2); | |
266 | SetStatusText(_("Welcome to Life!")); | |
267 | ||
29b07a38 GRG |
268 | // game and timer |
269 | m_life = new Life(); | |
270 | m_timer = new wxTimer(this, ID_TIMER); | |
22c1c01b DS |
271 | m_running = false; |
272 | m_topspeed = false; | |
29b07a38 GRG |
273 | m_interval = 500; |
274 | m_tics = 0; | |
5a1dca12 | 275 | |
29b07a38 GRG |
276 | // We use two different panels to reduce flicker in wxGTK, because |
277 | // some widgets (like wxStaticText) don't have their own X11 window, | |
278 | // and thus updating the text would result in a refresh of the canvas | |
279 | // if they belong to the same parent. | |
280 | ||
22c1c01b DS |
281 | wxPanel *panel1 = new wxPanel(this, wxID_ANY); |
282 | wxPanel *panel2 = new wxPanel(this, wxID_ANY); | |
29b07a38 GRG |
283 | |
284 | // canvas | |
285 | m_canvas = new LifeCanvas(panel1, m_life); | |
286 | ||
287 | // info panel | |
22c1c01b | 288 | m_text = new wxStaticText(panel2, wxID_ANY, |
29b07a38 GRG |
289 | wxEmptyString, |
290 | wxDefaultPosition, | |
291 | wxDefaultSize, | |
292 | wxALIGN_CENTER | wxST_NO_AUTORESIZE); | |
293 | ||
294 | wxSlider *slider = new wxSlider(panel2, ID_SLIDER, | |
2480be69 GRG |
295 | 5, 1, 10, |
296 | wxDefaultPosition, | |
297 | wxSize(200, -1), | |
298 | wxSL_HORIZONTAL | wxSL_AUTOTICKS); | |
5a1dca12 | 299 | |
29b07a38 GRG |
300 | UpdateInfoText(); |
301 | ||
5a1dca12 | 302 | // component layout |
29b07a38 GRG |
303 | wxBoxSizer *sizer1 = new wxBoxSizer(wxVERTICAL); |
304 | wxBoxSizer *sizer2 = new wxBoxSizer(wxVERTICAL); | |
305 | wxBoxSizer *sizer3 = new wxBoxSizer(wxVERTICAL); | |
306 | ||
22c1c01b | 307 | sizer1->Add( new wxStaticLine(panel1, wxID_ANY), 0, wxGROW ); |
29b07a38 | 308 | sizer1->Add( m_canvas, 1, wxGROW | wxALL, 2 ); |
22c1c01b | 309 | sizer1->Add( new wxStaticLine(panel1, wxID_ANY), 0, wxGROW ); |
29b07a38 | 310 | panel1->SetSizer( sizer1 ); |
29b07a38 GRG |
311 | sizer1->Fit( panel1 ); |
312 | ||
313 | sizer2->Add( m_text, 0, wxGROW | wxTOP, 4 ); | |
314 | sizer2->Add( slider, 0, wxCENTRE | wxALL, 4 ); | |
281b0186 | 315 | |
29b07a38 | 316 | panel2->SetSizer( sizer2 ); |
29b07a38 GRG |
317 | sizer2->Fit( panel2 ); |
318 | ||
319 | sizer3->Add( panel1, 1, wxGROW ); | |
320 | sizer3->Add( panel2, 0, wxGROW ); | |
321 | SetSizer( sizer3 ); | |
29b07a38 GRG |
322 | sizer3->Fit( this ); |
323 | ||
324 | // set minimum frame size | |
325 | sizer3->SetSizeHints( this ); | |
326 | ||
327 | // navigator frame | |
328 | m_navigator = new LifeNavigator(this); | |
5a1dca12 GRG |
329 | } |
330 | ||
331 | LifeFrame::~LifeFrame() | |
332 | { | |
333 | delete m_timer; | |
5a1dca12 GRG |
334 | } |
335 | ||
336 | void LifeFrame::UpdateInfoText() | |
337 | { | |
338 | wxString msg; | |
339 | ||
29b07a38 | 340 | msg.Printf(_(" Generation: %u (T: %u ms), Population: %u "), |
e0a40292 GRG |
341 | m_tics, |
342 | m_topspeed? 0 : m_interval, | |
343 | m_life->GetNumCells()); | |
5a1dca12 GRG |
344 | m_text->SetLabel(msg); |
345 | } | |
346 | ||
e0a40292 GRG |
347 | // Enable or disable tools and menu entries according to the current |
348 | // state. See also wxEVT_UPDATE_UI events for a slightly different | |
349 | // way to do this. | |
350 | void LifeFrame::UpdateUI() | |
351 | { | |
a9cf4097 | 352 | // start / stop |
e0a40292 GRG |
353 | GetToolBar()->EnableTool(ID_START, !m_running); |
354 | GetToolBar()->EnableTool(ID_STOP, m_running); | |
4efbec35 JS |
355 | GetMenuBar()->Enable(ID_START, !m_running); |
356 | GetMenuBar()->Enable(ID_STEP, !m_running); | |
357 | GetMenuBar()->Enable(ID_STOP, m_running); | |
358 | GetMenuBar()->Enable(ID_TOPSPEED, !m_topspeed); | |
a9cf4097 GRG |
359 | |
360 | // zooming | |
361 | int cellsize = m_canvas->GetCellSize(); | |
362 | GetToolBar()->EnableTool(ID_ZOOMIN, cellsize < 32); | |
363 | GetToolBar()->EnableTool(ID_ZOOMOUT, cellsize > 1); | |
4efbec35 JS |
364 | GetMenuBar()->Enable(ID_ZOOMIN, cellsize < 32); |
365 | GetMenuBar()->Enable(ID_ZOOMOUT, cellsize > 1); | |
e0a40292 GRG |
366 | } |
367 | ||
f6bcfd97 BP |
368 | // Event handlers ----------------------------------------------------------- |
369 | ||
370 | // OnMenu handles all events which don't have their own event handler | |
5a1dca12 GRG |
371 | void LifeFrame::OnMenu(wxCommandEvent& event) |
372 | { | |
373 | switch (event.GetId()) | |
374 | { | |
22c1c01b | 375 | case wxID_NEW: |
f6bcfd97 BP |
376 | { |
377 | // stop if it was running | |
378 | OnStop(); | |
379 | m_life->Clear(); | |
380 | m_canvas->Recenter(0, 0); | |
381 | m_tics = 0; | |
382 | UpdateInfoText(); | |
383 | break; | |
384 | } | |
22c1c01b | 385 | case wxID_ABOUT: |
f6bcfd97 BP |
386 | { |
387 | LifeAboutDialog dialog(this); | |
388 | dialog.ShowModal(); | |
389 | break; | |
390 | } | |
22c1c01b | 391 | case wxID_EXIT: |
f6bcfd97 | 392 | { |
22c1c01b DS |
393 | // true is to force the frame to close |
394 | Close(true); | |
f6bcfd97 BP |
395 | break; |
396 | } | |
22c1c01b | 397 | case ID_SHOWNAV: |
087e4f4a | 398 | { |
29b07a38 GRG |
399 | bool checked = GetMenuBar()->GetMenu(1)->IsChecked(ID_SHOWNAV); |
400 | m_navigator->Show(checked); | |
e0a40292 GRG |
401 | break; |
402 | } | |
f6bcfd97 BP |
403 | case ID_INFO: |
404 | { | |
405 | wxString desc = m_life->GetDescription(); | |
22c1c01b | 406 | |
f6bcfd97 BP |
407 | if ( desc.IsEmpty() ) |
408 | desc = _("Not available"); | |
409 | ||
410 | // should we make the description editable here? | |
411 | wxMessageBox(desc, _("Description"), wxOK | wxICON_INFORMATION); | |
412 | ||
413 | break; | |
414 | } | |
415 | case ID_START : OnStart(); break; | |
416 | case ID_STEP : OnStep(); break; | |
417 | case ID_STOP : OnStop(); break; | |
e0a40292 GRG |
418 | case ID_TOPSPEED: |
419 | { | |
22c1c01b DS |
420 | m_running = true; |
421 | m_topspeed = true; | |
e0a40292 | 422 | UpdateUI(); |
22c1c01b | 423 | while (m_running && m_topspeed) |
e0a40292 | 424 | { |
29b07a38 | 425 | OnStep(); |
e0a40292 GRG |
426 | wxYield(); |
427 | } | |
428 | break; | |
429 | } | |
f6bcfd97 BP |
430 | } |
431 | } | |
432 | ||
433 | void LifeFrame::OnOpen(wxCommandEvent& WXUNUSED(event)) | |
434 | { | |
435 | wxFileDialog filedlg(this, | |
436 | _("Choose a file to open"), | |
22c1c01b DS |
437 | wxEmptyString, |
438 | wxEmptyString, | |
f6bcfd97 BP |
439 | _("Life patterns (*.lif)|*.lif|All files (*.*)|*.*"), |
440 | wxOPEN | wxFILE_MUST_EXIST); | |
441 | ||
442 | if (filedlg.ShowModal() == wxID_OK) | |
443 | { | |
2b15fdfc | 444 | wxFileInputStream stream(filedlg.GetPath()); |
f6bcfd97 BP |
445 | LifeReader reader(stream); |
446 | ||
447 | // the reader handles errors itself, no need to do anything here | |
448 | if (reader.IsOk()) | |
e0a40292 | 449 | { |
f6bcfd97 | 450 | // stop if running and put the pattern |
5a1dca12 GRG |
451 | OnStop(); |
452 | m_life->Clear(); | |
f6bcfd97 BP |
453 | m_life->SetPattern(reader.GetPattern()); |
454 | ||
455 | // recenter canvas | |
e0a40292 | 456 | m_canvas->Recenter(0, 0); |
5a1dca12 GRG |
457 | m_tics = 0; |
458 | UpdateInfoText(); | |
5a1dca12 GRG |
459 | } |
460 | } | |
461 | } | |
462 | ||
087e4f4a GRG |
463 | void LifeFrame::OnSamples(wxCommandEvent& WXUNUSED(event)) |
464 | { | |
465 | // stop if it was running | |
466 | OnStop(); | |
467 | ||
471ed537 | 468 | // dialog box |
087e4f4a GRG |
469 | LifeSamplesDialog dialog(this); |
470 | ||
087e4f4a GRG |
471 | if (dialog.ShowModal() == wxID_OK) |
472 | { | |
f6bcfd97 | 473 | const LifePattern pattern = dialog.GetPattern(); |
087e4f4a | 474 | |
f6bcfd97 | 475 | // put the pattern |
e0a40292 | 476 | m_life->Clear(); |
f6bcfd97 | 477 | m_life->SetPattern(pattern); |
087e4f4a | 478 | |
e0a40292 GRG |
479 | // recenter canvas |
480 | m_canvas->Recenter(0, 0); | |
087e4f4a GRG |
481 | m_tics = 0; |
482 | UpdateInfoText(); | |
483 | } | |
484 | } | |
485 | ||
29b07a38 GRG |
486 | void LifeFrame::OnZoom(wxCommandEvent& event) |
487 | { | |
488 | int cellsize = m_canvas->GetCellSize(); | |
489 | ||
490 | if ((event.GetId() == ID_ZOOMIN) && cellsize < 32) | |
491 | { | |
492 | m_canvas->SetCellSize(cellsize * 2); | |
493 | UpdateUI(); | |
494 | } | |
495 | else if ((event.GetId() == ID_ZOOMOUT) && cellsize > 1) | |
496 | { | |
497 | m_canvas->SetCellSize(cellsize / 2); | |
498 | UpdateUI(); | |
499 | } | |
500 | } | |
501 | ||
502 | void LifeFrame::OnNavigate(wxCommandEvent& event) | |
503 | { | |
764835a5 | 504 | LifeCell c; |
29b07a38 GRG |
505 | |
506 | switch (event.GetId()) | |
507 | { | |
22c1c01b | 508 | case ID_NORTH: c = m_life->FindNorth(); break; |
29b07a38 GRG |
509 | case ID_SOUTH: c = m_life->FindSouth(); break; |
510 | case ID_WEST: c = m_life->FindWest(); break; | |
511 | case ID_EAST: c = m_life->FindEast(); break; | |
512 | case ID_CENTER: c = m_life->FindCenter(); break; | |
22c1c01b DS |
513 | default : |
514 | wxFAIL; | |
515 | // Fall through! | |
29b07a38 GRG |
516 | case ID_ORIGIN: c.i = c.j = 0; break; |
517 | } | |
518 | ||
519 | m_canvas->Recenter(c.i, c.j); | |
520 | } | |
521 | ||
522 | void LifeFrame::OnSlider(wxScrollEvent& event) | |
523 | { | |
524 | m_interval = event.GetPosition() * 100; | |
525 | ||
526 | if (m_running) | |
527 | { | |
528 | OnStop(); | |
529 | OnStart(); | |
530 | } | |
22c1c01b | 531 | |
29b07a38 GRG |
532 | UpdateInfoText(); |
533 | } | |
534 | ||
535 | void LifeFrame::OnTimer(wxTimerEvent& WXUNUSED(event)) | |
536 | { | |
537 | OnStep(); | |
538 | } | |
539 | ||
f6bcfd97 BP |
540 | void LifeFrame::OnClose(wxCloseEvent& WXUNUSED(event)) |
541 | { | |
542 | // Stop if it was running; this is absolutely needed because | |
543 | // the frame won't be actually destroyed until there are no | |
544 | // more pending events, and this in turn won't ever happen | |
545 | // if the timer is running faster than the window can redraw. | |
546 | OnStop(); | |
22c1c01b | 547 | Destroy(); |
f6bcfd97 BP |
548 | } |
549 | ||
5a1dca12 GRG |
550 | void LifeFrame::OnStart() |
551 | { | |
087e4f4a GRG |
552 | if (!m_running) |
553 | { | |
087e4f4a | 554 | m_timer->Start(m_interval); |
22c1c01b | 555 | m_running = true; |
e0a40292 | 556 | UpdateUI(); |
087e4f4a | 557 | } |
5a1dca12 GRG |
558 | } |
559 | ||
560 | void LifeFrame::OnStop() | |
561 | { | |
087e4f4a GRG |
562 | if (m_running) |
563 | { | |
087e4f4a | 564 | m_timer->Stop(); |
22c1c01b DS |
565 | m_running = false; |
566 | m_topspeed = false; | |
e0a40292 | 567 | UpdateUI(); |
087e4f4a | 568 | } |
5a1dca12 GRG |
569 | } |
570 | ||
29b07a38 | 571 | void LifeFrame::OnStep() |
5a1dca12 | 572 | { |
a36f0f83 GRG |
573 | if (m_life->NextTic()) |
574 | m_tics++; | |
575 | else | |
576 | OnStop(); | |
5a1dca12 | 577 | |
e0a40292 | 578 | m_canvas->DrawChanged(); |
a36f0f83 | 579 | UpdateInfoText(); |
5a1dca12 GRG |
580 | } |
581 | ||
a36f0f83 | 582 | |
5a1dca12 | 583 | // -------------------------------------------------------------------------- |
29b07a38 | 584 | // LifeNavigator miniframe |
5a1dca12 GRG |
585 | // -------------------------------------------------------------------------- |
586 | ||
29b07a38 | 587 | LifeNavigator::LifeNavigator(wxWindow *parent) |
22c1c01b | 588 | : wxMiniFrame(parent, wxID_ANY, |
29b07a38 GRG |
589 | _("Navigation"), |
590 | wxDefaultPosition, | |
591 | wxDefaultSize, | |
592 | wxCAPTION | wxSIMPLE_BORDER) | |
5a1dca12 | 593 | { |
22c1c01b | 594 | wxPanel *panel = new wxPanel(this, wxID_ANY); |
29b07a38 GRG |
595 | wxBoxSizer *sizer1 = new wxBoxSizer(wxVERTICAL); |
596 | wxBoxSizer *sizer2 = new wxBoxSizer(wxHORIZONTAL); | |
597 | ||
598 | // create bitmaps and masks for the buttons | |
599 | wxBitmap | |
600 | bmpn = wxBITMAP(north), | |
22c1c01b | 601 | bmpw = wxBITMAP(west), |
29b07a38 | 602 | bmpc = wxBITMAP(center), |
22c1c01b | 603 | bmpe = wxBITMAP(east), |
29b07a38 GRG |
604 | bmps = wxBITMAP(south); |
605 | ||
a99c96b0 | 606 | #if !defined(__WXGTK__) && !defined(__WXMOTIF__) && !defined(__WXMAC__) |
29b07a38 GRG |
607 | bmpn.SetMask(new wxMask(bmpn, *wxLIGHT_GREY)); |
608 | bmpw.SetMask(new wxMask(bmpw, *wxLIGHT_GREY)); | |
609 | bmpc.SetMask(new wxMask(bmpc, *wxLIGHT_GREY)); | |
22c1c01b | 610 | bmpe.SetMask(new wxMask(bmpe, *wxLIGHT_GREY)); |
29b07a38 GRG |
611 | bmps.SetMask(new wxMask(bmps, *wxLIGHT_GREY)); |
612 | #endif | |
613 | ||
614 | // create the buttons and attach tooltips to them | |
615 | wxBitmapButton | |
616 | *bn = new wxBitmapButton(panel, ID_NORTH, bmpn), | |
617 | *bw = new wxBitmapButton(panel, ID_WEST , bmpw), | |
618 | *bc = new wxBitmapButton(panel, ID_CENTER, bmpc), | |
619 | *be = new wxBitmapButton(panel, ID_EAST , bmpe), | |
620 | *bs = new wxBitmapButton(panel, ID_SOUTH, bmps); | |
621 | ||
7a85f3f9 | 622 | #if wxUSE_TOOLTIPS |
29b07a38 GRG |
623 | bn->SetToolTip(_("Find northernmost cell")); |
624 | bw->SetToolTip(_("Find westernmost cell")); | |
625 | bc->SetToolTip(_("Find center of mass")); | |
626 | be->SetToolTip(_("Find easternmost cell")); | |
627 | bs->SetToolTip(_("Find southernmost cell")); | |
7a85f3f9 | 628 | #endif |
29b07a38 GRG |
629 | |
630 | // add buttons to sizers | |
631 | sizer2->Add( bw, 0, wxCENTRE | wxWEST, 4 ); | |
632 | sizer2->Add( bc, 0, wxCENTRE); | |
633 | sizer2->Add( be, 0, wxCENTRE | wxEAST, 4 ); | |
634 | sizer1->Add( bn, 0, wxCENTRE | wxNORTH, 4 ); | |
635 | sizer1->Add( sizer2 ); | |
636 | sizer1->Add( bs, 0, wxCENTRE | wxSOUTH, 4 ); | |
637 | ||
b538bf7e | 638 | // set the panel and miniframe size |
29b07a38 | 639 | panel->SetSizer(sizer1); |
b538bf7e JS |
640 | |
641 | sizer1->Fit(panel); | |
642 | SetClientSize(panel->GetSize()); | |
643 | wxSize sz = GetSize(); | |
644 | SetSizeHints(sz.x, sz.y, sz.x, sz.y); | |
29b07a38 GRG |
645 | |
646 | // move it to a sensible position | |
647 | wxRect parentRect = parent->GetRect(); | |
648 | wxSize childSize = GetSize(); | |
649 | int x = parentRect.GetX() + | |
650 | parentRect.GetWidth(); | |
651 | int y = parentRect.GetY() + | |
652 | (parentRect.GetHeight() - childSize.GetHeight()) / 4; | |
653 | Move(x, y); | |
654 | ||
655 | // done | |
22c1c01b | 656 | Show(true); |
29b07a38 GRG |
657 | } |
658 | ||
659 | void LifeNavigator::OnClose(wxCloseEvent& event) | |
660 | { | |
dbf75be7 | 661 | // avoid if we can |
29b07a38 GRG |
662 | if (event.CanVeto()) |
663 | event.Veto(); | |
664 | else | |
665 | Destroy(); | |
666 | } | |
667 | ||
5a1dca12 GRG |
668 | |
669 | // -------------------------------------------------------------------------- | |
087e4f4a | 670 | // LifeCanvas |
5a1dca12 GRG |
671 | // -------------------------------------------------------------------------- |
672 | ||
673 | // canvas constructor | |
087e4f4a | 674 | LifeCanvas::LifeCanvas(wxWindow *parent, Life *life, bool interactive) |
22c1c01b | 675 | : wxWindow(parent, wxID_ANY, wxDefaultPosition, wxSize(100, 100), |
ee710d75 | 676 | wxSUNKEN_BORDER|wxFULL_REPAINT_ON_RESIZE) |
5a1dca12 | 677 | { |
087e4f4a GRG |
678 | m_life = life; |
679 | m_interactive = interactive; | |
680 | m_cellsize = 8; | |
e0a40292 GRG |
681 | m_status = MOUSE_NOACTION; |
682 | m_viewportX = 0; | |
683 | m_viewportY = 0; | |
684 | m_viewportH = 0; | |
685 | m_viewportW = 0; | |
686 | ||
687 | if (m_interactive) | |
688 | SetCursor(*wxCROSS_CURSOR); | |
689 | ||
690 | // reduce flicker if wxEVT_ERASE_BACKGROUND is not available | |
22c1c01b | 691 | SetBackgroundColour(*wxWHITE); |
5a1dca12 GRG |
692 | } |
693 | ||
694 | LifeCanvas::~LifeCanvas() | |
695 | { | |
22c1c01b | 696 | delete m_life; |
5a1dca12 GRG |
697 | } |
698 | ||
e0a40292 GRG |
699 | // recenter at the given position |
700 | void LifeCanvas::Recenter(wxInt32 i, wxInt32 j) | |
5a1dca12 | 701 | { |
e0a40292 GRG |
702 | m_viewportX = i - m_viewportW / 2; |
703 | m_viewportY = j - m_viewportH / 2; | |
5a1dca12 | 704 | |
087e4f4a | 705 | // redraw everything |
22c1c01b | 706 | Refresh(false); |
5a1dca12 GRG |
707 | } |
708 | ||
e0a40292 GRG |
709 | // set the cell size and refresh display |
710 | void LifeCanvas::SetCellSize(int cellsize) | |
5a1dca12 | 711 | { |
e0a40292 GRG |
712 | m_cellsize = cellsize; |
713 | ||
714 | // find current center | |
715 | wxInt32 cx = m_viewportX + m_viewportW / 2; | |
716 | wxInt32 cy = m_viewportY + m_viewportH / 2; | |
717 | ||
718 | // get current canvas size and adjust viewport accordingly | |
2fa7c206 | 719 | int w, h; |
e0a40292 GRG |
720 | GetClientSize(&w, &h); |
721 | m_viewportW = (w + m_cellsize - 1) / m_cellsize; | |
722 | m_viewportH = (h + m_cellsize - 1) / m_cellsize; | |
22c1c01b | 723 | |
e0a40292 GRG |
724 | // recenter |
725 | m_viewportX = cx - m_viewportW / 2; | |
726 | m_viewportY = cy - m_viewportH / 2; | |
727 | ||
728 | // adjust scrollbars | |
729 | if (m_interactive) | |
730 | { | |
731 | SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW); | |
732 | SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH); | |
733 | m_thumbX = m_viewportW; | |
734 | m_thumbY = m_viewportH; | |
735 | } | |
22c1c01b DS |
736 | |
737 | Refresh(false); | |
e0a40292 | 738 | } |
2480be69 | 739 | |
e0a40292 GRG |
740 | // draw a cell |
741 | void LifeCanvas::DrawCell(wxInt32 i, wxInt32 j, bool alive) | |
742 | { | |
743 | wxClientDC dc(this); | |
5a1dca12 | 744 | |
e0a40292 GRG |
745 | dc.SetPen(alive? *wxBLACK_PEN : *wxWHITE_PEN); |
746 | dc.SetBrush(alive? *wxBLACK_BRUSH : *wxWHITE_BRUSH); | |
5a1dca12 | 747 | |
e0a40292 GRG |
748 | dc.BeginDrawing(); |
749 | DrawCell(i, j, dc); | |
5a1dca12 | 750 | dc.EndDrawing(); |
5a1dca12 GRG |
751 | } |
752 | ||
e0a40292 | 753 | void LifeCanvas::DrawCell(wxInt32 i, wxInt32 j, wxDC &dc) |
5a1dca12 | 754 | { |
e0a40292 GRG |
755 | wxCoord x = CellToX(i); |
756 | wxCoord y = CellToY(j); | |
5a1dca12 | 757 | |
e0a40292 GRG |
758 | // if cellsize is 1 or 2, there will be no grid |
759 | switch (m_cellsize) | |
760 | { | |
761 | case 1: | |
762 | dc.DrawPoint(x, y); | |
763 | break; | |
764 | case 2: | |
765 | dc.DrawRectangle(x, y, 2, 2); | |
766 | break; | |
767 | default: | |
768 | dc.DrawRectangle(x + 1, y + 1, m_cellsize - 1, m_cellsize - 1); | |
769 | } | |
5a1dca12 GRG |
770 | } |
771 | ||
e0a40292 GRG |
772 | // draw all changed cells |
773 | void LifeCanvas::DrawChanged() | |
5a1dca12 | 774 | { |
e0a40292 GRG |
775 | wxClientDC dc(this); |
776 | ||
777 | size_t ncells; | |
764835a5 | 778 | LifeCell *cells; |
22c1c01b | 779 | bool done = false; |
e0a40292 GRG |
780 | |
781 | m_life->BeginFind(m_viewportX, | |
782 | m_viewportY, | |
783 | m_viewportX + m_viewportW, | |
784 | m_viewportY + m_viewportH, | |
22c1c01b DS |
785 | true); |
786 | ||
e0a40292 | 787 | dc.BeginDrawing(); |
e0a40292 GRG |
788 | |
789 | if (m_cellsize == 1) | |
5a1dca12 GRG |
790 | { |
791 | dc.SetPen(*wxBLACK_PEN); | |
5a1dca12 GRG |
792 | } |
793 | else | |
794 | { | |
e0a40292 GRG |
795 | dc.SetPen(*wxTRANSPARENT_PEN); |
796 | dc.SetBrush(*wxBLACK_BRUSH); | |
797 | } | |
030d06e1 | 798 | dc.SetLogicalFunction(wxINVERT); |
e0a40292 GRG |
799 | |
800 | while (!done) | |
801 | { | |
802 | done = m_life->FindMore(&cells, &ncells); | |
803 | ||
804 | for (size_t m = 0; m < ncells; m++) | |
805 | DrawCell(cells[m].i, cells[m].j, dc); | |
5a1dca12 | 806 | } |
e0a40292 | 807 | dc.EndDrawing(); |
5a1dca12 | 808 | } |
ecbdd409 | 809 | |
5a1dca12 | 810 | // event handlers |
babd36de | 811 | void LifeCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) |
5a1dca12 GRG |
812 | { |
813 | wxPaintDC dc(this); | |
e0a40292 GRG |
814 | wxRect rect = GetUpdateRegion().GetBox(); |
815 | wxCoord x, y, w, h; | |
816 | wxInt32 i0, j0, i1, j1; | |
817 | ||
818 | // find damaged area | |
819 | x = rect.GetX(); | |
820 | y = rect.GetY(); | |
821 | w = rect.GetWidth(); | |
822 | h = rect.GetHeight(); | |
823 | ||
824 | i0 = XToCell(x); | |
825 | j0 = YToCell(y); | |
826 | i1 = XToCell(x + w - 1); | |
827 | j1 = YToCell(y + h - 1); | |
828 | ||
829 | size_t ncells; | |
764835a5 | 830 | LifeCell *cells; |
5a1dca12 | 831 | |
22c1c01b DS |
832 | m_life->BeginFind(i0, j0, i1, j1, false); |
833 | bool done = m_life->FindMore(&cells, &ncells); | |
5a1dca12 | 834 | |
e0a40292 | 835 | // erase all damaged cells and draw the grid |
5a1dca12 | 836 | dc.BeginDrawing(); |
e0a40292 | 837 | dc.SetBrush(*wxWHITE_BRUSH); |
5a1dca12 | 838 | |
e0a40292 | 839 | if (m_cellsize <= 2) |
5a1dca12 | 840 | { |
e0a40292 GRG |
841 | // no grid |
842 | dc.SetPen(*wxWHITE_PEN); | |
843 | dc.DrawRectangle(x, y, w, h); | |
5a1dca12 | 844 | } |
e0a40292 GRG |
845 | else |
846 | { | |
847 | x = CellToX(i0); | |
848 | y = CellToY(j0); | |
849 | w = CellToX(i1 + 1) - x + 1; | |
850 | h = CellToY(j1 + 1) - y + 1; | |
851 | ||
852 | dc.SetPen(*wxLIGHT_GREY_PEN); | |
853 | for (wxInt32 yy = y; yy <= (y + h - m_cellsize); yy += m_cellsize) | |
854 | dc.DrawRectangle(x, yy, w, m_cellsize + 1); | |
855 | for (wxInt32 xx = x; xx <= (x + w - m_cellsize); xx += m_cellsize) | |
856 | dc.DrawLine(xx, y, xx, y + h); | |
857 | } | |
858 | ||
859 | // draw all alive cells | |
860 | dc.SetPen(*wxBLACK_PEN); | |
861 | dc.SetBrush(*wxBLACK_BRUSH); | |
862 | ||
863 | while (!done) | |
864 | { | |
865 | for (size_t m = 0; m < ncells; m++) | |
866 | DrawCell(cells[m].i, cells[m].j, dc); | |
867 | ||
868 | done = m_life->FindMore(&cells, &ncells); | |
869 | } | |
870 | ||
871 | // last set | |
872 | for (size_t m = 0; m < ncells; m++) | |
873 | DrawCell(cells[m].i, cells[m].j, dc); | |
5a1dca12 | 874 | |
5a1dca12 GRG |
875 | dc.EndDrawing(); |
876 | } | |
877 | ||
878 | void LifeCanvas::OnMouse(wxMouseEvent& event) | |
879 | { | |
087e4f4a GRG |
880 | if (!m_interactive) |
881 | return; | |
882 | ||
5a1dca12 | 883 | // which cell are we pointing at? |
e0a40292 GRG |
884 | wxInt32 i = XToCell( event.GetX() ); |
885 | wxInt32 j = YToCell( event.GetY() ); | |
886 | ||
887 | // set statusbar text | |
888 | wxString msg; | |
889 | msg.Printf(_("Cell: (%d, %d)"), i, j); | |
29b07a38 | 890 | ((LifeFrame *) wxGetApp().GetTopWindow())->SetStatusText(msg, 1); |
5a1dca12 | 891 | |
f6bcfd97 BP |
892 | // NOTE that wxMouseEvent::LeftDown() and wxMouseEvent::LeftIsDown() |
893 | // have different semantics. The first one is used to signal that the | |
894 | // button was just pressed (i.e., in "button down" events); the second | |
895 | // one just describes the current status of the button, independently | |
896 | // of the mouse event type. LeftIsDown is typically used in "mouse | |
897 | // move" events, to test if the button is _still_ pressed. | |
898 | ||
899 | // is the button down? | |
5a1dca12 GRG |
900 | if (!event.LeftIsDown()) |
901 | { | |
902 | m_status = MOUSE_NOACTION; | |
7989fb37 | 903 | return; |
5a1dca12 | 904 | } |
5a1dca12 | 905 | |
f6bcfd97 BP |
906 | // was it pressed just now? |
907 | if (event.LeftDown()) | |
7989fb37 | 908 | { |
f6bcfd97 | 909 | // yes: start a new action and toggle this cell |
7989fb37 | 910 | m_status = (m_life->IsAlive(i, j)? MOUSE_ERASING : MOUSE_DRAWING); |
22c1c01b | 911 | |
7989fb37 GRG |
912 | m_mi = i; |
913 | m_mj = j; | |
914 | m_life->SetCell(i, j, m_status == MOUSE_DRAWING); | |
915 | DrawCell(i, j, m_status == MOUSE_DRAWING); | |
916 | } | |
917 | else if ((m_mi != i) || (m_mj != j)) | |
918 | { | |
f6bcfd97 | 919 | // no: continue ongoing action |
5e9ff6ad GRG |
920 | bool alive = (m_status == MOUSE_DRAWING); |
921 | ||
922 | // prepare DC and pen + brush to optimize drawing | |
923 | wxClientDC dc(this); | |
924 | dc.SetPen(alive? *wxBLACK_PEN : *wxWHITE_PEN); | |
925 | dc.SetBrush(alive? *wxBLACK_BRUSH : *wxWHITE_BRUSH); | |
926 | dc.BeginDrawing(); | |
927 | ||
7989fb37 GRG |
928 | // draw a line of cells using Bresenham's algorithm |
929 | wxInt32 d, ii, jj, di, ai, si, dj, aj, sj; | |
930 | di = i - m_mi; | |
931 | ai = abs(di) << 1; | |
932 | si = (di < 0)? -1 : 1; | |
933 | dj = j - m_mj; | |
934 | aj = abs(dj) << 1; | |
935 | sj = (dj < 0)? -1 : 1; | |
936 | ||
937 | ii = m_mi; | |
938 | jj = m_mj; | |
22c1c01b | 939 | |
7989fb37 GRG |
940 | if (ai > aj) |
941 | { | |
942 | // iterate over i | |
22c1c01b DS |
943 | d = aj - (ai >> 1); |
944 | ||
7989fb37 GRG |
945 | while (ii != i) |
946 | { | |
5e9ff6ad GRG |
947 | m_life->SetCell(ii, jj, alive); |
948 | DrawCell(ii, jj, dc); | |
7989fb37 GRG |
949 | if (d >= 0) |
950 | { | |
951 | jj += sj; | |
22c1c01b | 952 | d -= ai; |
7989fb37 GRG |
953 | } |
954 | ii += si; | |
955 | d += aj; | |
956 | } | |
957 | } | |
958 | else | |
5a1dca12 | 959 | { |
7989fb37 GRG |
960 | // iterate over j |
961 | d = ai - (aj >> 1); | |
962 | ||
963 | while (jj != j) | |
964 | { | |
5e9ff6ad GRG |
965 | m_life->SetCell(ii, jj, alive); |
966 | DrawCell(ii, jj, dc); | |
7989fb37 GRG |
967 | if (d >= 0) |
968 | { | |
969 | ii += si; | |
22c1c01b | 970 | d -= aj; |
7989fb37 GRG |
971 | } |
972 | jj += sj; | |
973 | d += ai; | |
974 | } | |
5a1dca12 | 975 | } |
7989fb37 GRG |
976 | |
977 | // last cell | |
5e9ff6ad GRG |
978 | m_life->SetCell(ii, jj, alive); |
979 | DrawCell(ii, jj, dc); | |
7989fb37 GRG |
980 | m_mi = ii; |
981 | m_mj = jj; | |
5e9ff6ad GRG |
982 | |
983 | dc.EndDrawing(); | |
5a1dca12 | 984 | } |
7989fb37 | 985 | |
29b07a38 | 986 | ((LifeFrame *) wxGetApp().GetTopWindow())->UpdateInfoText(); |
5a1dca12 GRG |
987 | } |
988 | ||
989 | void LifeCanvas::OnSize(wxSizeEvent& event) | |
990 | { | |
e0a40292 GRG |
991 | // find center |
992 | wxInt32 cx = m_viewportX + m_viewportW / 2; | |
993 | wxInt32 cy = m_viewportY + m_viewportH / 2; | |
994 | ||
995 | // get new size | |
5a1dca12 GRG |
996 | wxCoord w = event.GetSize().GetX(); |
997 | wxCoord h = event.GetSize().GetY(); | |
e0a40292 GRG |
998 | m_viewportW = (w + m_cellsize - 1) / m_cellsize; |
999 | m_viewportH = (h + m_cellsize - 1) / m_cellsize; | |
1000 | ||
1001 | // recenter | |
1002 | m_viewportX = cx - m_viewportW / 2; | |
1003 | m_viewportY = cy - m_viewportH / 2; | |
1004 | ||
1005 | // scrollbars | |
1006 | if (m_interactive) | |
1007 | { | |
1008 | SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW); | |
1009 | SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH); | |
1010 | m_thumbX = m_viewportW; | |
1011 | m_thumbY = m_viewportH; | |
1012 | } | |
5a1dca12 GRG |
1013 | |
1014 | // allow default processing | |
1015 | event.Skip(); | |
1016 | } | |
e0a40292 GRG |
1017 | |
1018 | void LifeCanvas::OnScroll(wxScrollWinEvent& event) | |
1019 | { | |
1020 | WXTYPE type = event.GetEventType(); | |
1021 | int pos = event.GetPosition(); | |
1022 | int orient = event.GetOrientation(); | |
e0a40292 GRG |
1023 | |
1024 | // calculate scroll increment | |
33e39147 | 1025 | int scrollinc = 0; |
764835a5 | 1026 | if (type == wxEVT_SCROLLWIN_TOP) |
e0a40292 | 1027 | { |
764835a5 GD |
1028 | if (orient == wxHORIZONTAL) |
1029 | scrollinc = -m_viewportW; | |
1030 | else | |
1031 | scrollinc = -m_viewportH; | |
1032 | } | |
1033 | else | |
1034 | if (type == wxEVT_SCROLLWIN_BOTTOM) | |
1035 | { | |
1036 | if (orient == wxHORIZONTAL) | |
1037 | scrollinc = m_viewportW; | |
1038 | else | |
1039 | scrollinc = m_viewportH; | |
1040 | } | |
1041 | else | |
1042 | if (type == wxEVT_SCROLLWIN_LINEUP) | |
1043 | { | |
1044 | scrollinc = -1; | |
1045 | } | |
1046 | else | |
1047 | if (type == wxEVT_SCROLLWIN_LINEDOWN) | |
1048 | { | |
1049 | scrollinc = +1; | |
1050 | } | |
1051 | else | |
1052 | if (type == wxEVT_SCROLLWIN_PAGEUP) | |
1053 | { | |
1054 | scrollinc = -10; | |
1055 | } | |
1056 | else | |
1057 | if (type == wxEVT_SCROLLWIN_PAGEDOWN) | |
1058 | { | |
b1823f8b | 1059 | scrollinc = +10; |
764835a5 GD |
1060 | } |
1061 | else | |
1062 | if (type == wxEVT_SCROLLWIN_THUMBTRACK) | |
1063 | { | |
1064 | if (orient == wxHORIZONTAL) | |
e0a40292 | 1065 | { |
764835a5 GD |
1066 | scrollinc = pos - m_thumbX; |
1067 | m_thumbX = pos; | |
e0a40292 | 1068 | } |
764835a5 | 1069 | else |
33e39147 | 1070 | { |
764835a5 GD |
1071 | scrollinc = pos - m_thumbY; |
1072 | m_thumbY = pos; | |
33e39147 | 1073 | } |
e0a40292 | 1074 | } |
764835a5 GD |
1075 | else |
1076 | if (type == wxEVT_SCROLLWIN_THUMBRELEASE) | |
1077 | { | |
1078 | m_thumbX = m_viewportW; | |
1079 | m_thumbY = m_viewportH; | |
1080 | } | |
e0a40292 | 1081 | |
dbf75be7 GRG |
1082 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
1083 | // wxGTK and wxMotif update the thumb automatically (wxMSW doesn't); | |
1084 | // so reset it back as we always want it to be in the same position. | |
33e39147 | 1085 | if (type != wxEVT_SCROLLWIN_THUMBTRACK) |
e0a40292 GRG |
1086 | { |
1087 | SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW); | |
1088 | SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH); | |
1089 | } | |
1090 | #endif | |
1091 | ||
1092 | if (scrollinc == 0) return; | |
22c1c01b | 1093 | |
e0a40292 GRG |
1094 | // scroll the window and adjust the viewport |
1095 | if (orient == wxHORIZONTAL) | |
1096 | { | |
1097 | m_viewportX += scrollinc; | |
1098 | ScrollWindow( -m_cellsize * scrollinc, 0, (const wxRect *) NULL); | |
1099 | } | |
1100 | else | |
1101 | { | |
22c1c01b | 1102 | m_viewportY += scrollinc; |
e0a40292 GRG |
1103 | ScrollWindow( 0, -m_cellsize * scrollinc, (const wxRect *) NULL); |
1104 | } | |
1105 | } | |
1106 | ||
1107 | void LifeCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) | |
1108 | { | |
1109 | // do nothing. I just don't want the background to be erased, you know. | |
1110 | } | |
29b07a38 GRG |
1111 | |
1112 |