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