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