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