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