]>
Commit | Line | Data |
---|---|---|
5a1dca12 GRG |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: life.cpp | |
29b07a38 | 3 | // Purpose: The game of Life, created by J. H. Conway |
5a1dca12 GRG |
4 | // Author: Guillermo Rodriguez Garcia, <guille@iies.es> |
5 | // Modified by: | |
6 | // Created: Jan/2000 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000, Guillermo Rodriguez Garcia | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ========================================================================== | |
e0a40292 | 13 | // headers, declarations, constants |
5a1dca12 GRG |
14 | // ========================================================================== |
15 | ||
5a1dca12 | 16 | #ifdef __GNUG__ |
2480be69 | 17 | #pragma implementation "life.h" |
5a1dca12 GRG |
18 | #endif |
19 | ||
2fa7c206 JS |
20 | // For compilers that support precompilation, includes "wx/wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
29b07a38 | 24 | #pragma hdrstop |
2fa7c206 JS |
25 | #endif |
26 | ||
27 | #ifndef WX_PRECOMP | |
29b07a38 | 28 | #include "wx/wx.h" |
2fa7c206 JS |
29 | #endif |
30 | ||
5a1dca12 | 31 | #include "wx/statline.h" |
f6bcfd97 BP |
32 | #include "wx/wfstream.h" |
33 | #include "wx/filedlg.h" | |
5d2ac6b8 | 34 | #include "wx/stockitem.h" |
2480be69 GRG |
35 | |
36 | #include "life.h" | |
37 | #include "game.h" | |
38 | #include "dialogs.h" | |
f6bcfd97 | 39 | #include "reader.h" |
5a1dca12 GRG |
40 | |
41 | // -------------------------------------------------------------------------- | |
42 | // resources | |
43 | // -------------------------------------------------------------------------- | |
44 | ||
e334d0ea | 45 | #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__) |
29b07a38 | 46 | // application icon |
5a1dca12 GRG |
47 | #include "mondrian.xpm" |
48 | ||
49 | // bitmap buttons for the toolbar | |
50 | #include "bitmaps/reset.xpm" | |
f6bcfd97 | 51 | #include "bitmaps/open.xpm" |
5a1dca12 GRG |
52 | #include "bitmaps/play.xpm" |
53 | #include "bitmaps/stop.xpm" | |
e0a40292 GRG |
54 | #include "bitmaps/zoomin.xpm" |
55 | #include "bitmaps/zoomout.xpm" | |
f6bcfd97 | 56 | #include "bitmaps/info.xpm" |
29b07a38 GRG |
57 | |
58 | // navigator | |
59 | #include "bitmaps/north.xpm" | |
60 | #include "bitmaps/south.xpm" | |
61 | #include "bitmaps/east.xpm" | |
62 | #include "bitmaps/west.xpm" | |
63 | #include "bitmaps/center.xpm" | |
5a1dca12 GRG |
64 | #endif |
65 | ||
5a1dca12 GRG |
66 | // -------------------------------------------------------------------------- |
67 | // constants | |
68 | // -------------------------------------------------------------------------- | |
69 | ||
22c1c01b | 70 | // IDs for the controls and the menu commands. Exluding those already defined |
be5a51fb | 71 | // by wxWidgets, such as wxID_NEW. |
5a1dca12 GRG |
72 | enum |
73 | { | |
29b07a38 | 74 | // timer |
22c1c01b | 75 | ID_TIMER = wxID_HIGHEST, |
29b07a38 | 76 | |
f6bcfd97 | 77 | // file menu |
087e4f4a | 78 | ID_SAMPLES, |
29b07a38 | 79 | |
f6bcfd97 | 80 | // view menu |
29b07a38 GRG |
81 | ID_SHOWNAV, |
82 | ID_ORIGIN, | |
83 | ID_CENTER, | |
84 | ID_NORTH, | |
85 | ID_SOUTH, | |
86 | ID_EAST, | |
87 | ID_WEST, | |
f6bcfd97 BP |
88 | ID_INFO, |
89 | ||
90 | // game menu | |
91 | ID_START, | |
92 | ID_STEP, | |
f6bcfd97 BP |
93 | ID_TOPSPEED, |
94 | ||
95 | // speed selection slider | |
22c1c01b | 96 | ID_SLIDER |
5a1dca12 GRG |
97 | }; |
98 | ||
99 | // -------------------------------------------------------------------------- | |
be5a51fb | 100 | // event tables and other macros for wxWidgets |
5a1dca12 GRG |
101 | // -------------------------------------------------------------------------- |
102 | ||
103 | // Event tables | |
5a1dca12 | 104 | BEGIN_EVENT_TABLE(LifeFrame, wxFrame) |
5d2ac6b8 WS |
105 | EVT_MENU (wxID_NEW, LifeFrame::OnMenu) |
106 | EVT_MENU (wxID_OPEN, LifeFrame::OnOpen) | |
107 | EVT_MENU (ID_SAMPLES, LifeFrame::OnSamples) | |
108 | EVT_MENU (wxID_ABOUT, LifeFrame::OnMenu) | |
109 | EVT_MENU (wxID_EXIT, LifeFrame::OnMenu) | |
110 | EVT_MENU (ID_SHOWNAV, LifeFrame::OnMenu) | |
111 | EVT_MENU (ID_ORIGIN, LifeFrame::OnNavigate) | |
112 | EVT_BUTTON (ID_CENTER, LifeFrame::OnNavigate) | |
113 | EVT_BUTTON (ID_NORTH, LifeFrame::OnNavigate) | |
114 | EVT_BUTTON (ID_SOUTH, LifeFrame::OnNavigate) | |
115 | EVT_BUTTON (ID_EAST, LifeFrame::OnNavigate) | |
116 | EVT_BUTTON (ID_WEST, LifeFrame::OnNavigate) | |
117 | EVT_MENU (wxID_ZOOM_IN, LifeFrame::OnZoom) | |
118 | EVT_MENU (wxID_ZOOM_OUT,LifeFrame::OnZoom) | |
119 | EVT_MENU (ID_INFO, LifeFrame::OnMenu) | |
120 | EVT_MENU (ID_START, LifeFrame::OnMenu) | |
121 | EVT_MENU (ID_STEP, LifeFrame::OnMenu) | |
122 | EVT_MENU (wxID_STOP, LifeFrame::OnMenu) | |
123 | EVT_MENU (ID_TOPSPEED, LifeFrame::OnMenu) | |
124 | EVT_COMMAND_SCROLL (ID_SLIDER, LifeFrame::OnSlider) | |
125 | EVT_TIMER (ID_TIMER, LifeFrame::OnTimer) | |
126 | EVT_CLOSE ( LifeFrame::OnClose) | |
ecbdd409 | 127 | END_EVENT_TABLE() |
5a1dca12 | 128 | |
29b07a38 GRG |
129 | BEGIN_EVENT_TABLE(LifeNavigator, wxMiniFrame) |
130 | EVT_CLOSE ( LifeNavigator::OnClose) | |
131 | END_EVENT_TABLE() | |
132 | ||
e0a40292 GRG |
133 | BEGIN_EVENT_TABLE(LifeCanvas, wxWindow) |
134 | EVT_PAINT ( LifeCanvas::OnPaint) | |
135 | EVT_SCROLLWIN ( LifeCanvas::OnScroll) | |
136 | EVT_SIZE ( LifeCanvas::OnSize) | |
f6bcfd97 BP |
137 | EVT_MOTION ( LifeCanvas::OnMouse) |
138 | EVT_LEFT_DOWN ( LifeCanvas::OnMouse) | |
139 | EVT_LEFT_UP ( LifeCanvas::OnMouse) | |
140 | EVT_LEFT_DCLICK ( LifeCanvas::OnMouse) | |
e0a40292 | 141 | EVT_ERASE_BACKGROUND( LifeCanvas::OnEraseBackground) |
ecbdd409 | 142 | END_EVENT_TABLE() |
5a1dca12 | 143 | |
5a1dca12 GRG |
144 | |
145 | // Create a new application object | |
146 | IMPLEMENT_APP(LifeApp) | |
147 | ||
e0a40292 | 148 | |
5a1dca12 GRG |
149 | // ========================================================================== |
150 | // implementation | |
151 | // ========================================================================== | |
152 | ||
2480be69 | 153 | // some shortcuts |
f6bcfd97 | 154 | #define ADD_TOOL(id, bmp, tooltip, help) \ |
422d0ff0 | 155 | toolBar->AddTool(id, bmp, wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, (wxObject *)NULL, tooltip, help) |
2480be69 | 156 | |
2480be69 | 157 | |
5a1dca12 GRG |
158 | // -------------------------------------------------------------------------- |
159 | // LifeApp | |
160 | // -------------------------------------------------------------------------- | |
161 | ||
e0a40292 | 162 | // 'Main program' equivalent: the program execution "starts" here |
5a1dca12 GRG |
163 | bool LifeApp::OnInit() |
164 | { | |
165 | // create the main application window | |
166 | LifeFrame *frame = new LifeFrame(); | |
167 | ||
168 | // show it and tell the application that it's our main window | |
22c1c01b | 169 | frame->Show(true); |
5a1dca12 GRG |
170 | SetTopWindow(frame); |
171 | ||
dbf75be7 GRG |
172 | // just for Motif |
173 | #ifdef __WXMOTIF__ | |
174 | frame->UpdateInfoText(); | |
175 | #endif | |
176 | ||
5a1dca12 | 177 | // enter the main message loop and run the app |
22c1c01b | 178 | return true; |
5a1dca12 GRG |
179 | } |
180 | ||
181 | // -------------------------------------------------------------------------- | |
182 | // LifeFrame | |
183 | // -------------------------------------------------------------------------- | |
184 | ||
185 | // frame constructor | |
e5c990b3 JS |
186 | LifeFrame::LifeFrame() : |
187 | wxFrame( (wxFrame *) NULL, wxID_ANY, _("Life!"), wxDefaultPosition ), | |
188 | m_navigator(NULL) | |
5a1dca12 GRG |
189 | { |
190 | // frame icon | |
191 | SetIcon(wxICON(mondrian)); | |
192 | ||
193 | // menu bar | |
22c1c01b DS |
194 | wxMenu *menuFile = new wxMenu(wxMENU_TEAROFF); |
195 | wxMenu *menuView = new wxMenu(wxMENU_TEAROFF); | |
196 | wxMenu *menuGame = new wxMenu(wxMENU_TEAROFF); | |
197 | wxMenu *menuHelp = new wxMenu(wxMENU_TEAROFF); | |
5a1dca12 | 198 | |
5d2ac6b8 WS |
199 | menuFile->Append(wxID_NEW, wxGetStockLabel(wxID_NEW), _("Start a new game")); |
200 | menuFile->Append(wxID_OPEN, wxGetStockLabel(wxID_OPEN), _("Open an existing Life pattern")); | |
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__ |
5d2ac6b8 | 255 | ADD_TOOL(wxID_OPEN, tbBitmaps[1], wxGetStockLabel(wxID_OPEN, false), _("Open an existing Life pattern")); |
e4535381 | 256 | |
f6bcfd97 | 257 | toolBar->AddSeparator(); |
5d2ac6b8 WS |
258 | ADD_TOOL(wxID_ZOOM_IN, tbBitmaps[2], wxGetStockLabel(wxID_ZOOM_IN, false), _("Zoom in")); |
259 | ADD_TOOL(wxID_ZOOM_OUT, tbBitmaps[3], wxGetStockLabel(wxID_ZOOM_OUT, false), _("Zoom out")); | |
f6bcfd97 | 260 | ADD_TOOL(ID_INFO, tbBitmaps[4], _("Description"), _("Show description")); |
e0a40292 | 261 | toolBar->AddSeparator(); |
5292c055 | 262 | #endif |
f6bcfd97 | 263 | ADD_TOOL(ID_START, tbBitmaps[5], _("Start"), _("Start")); |
5d2ac6b8 | 264 | ADD_TOOL(wxID_STOP, tbBitmaps[6], wxGetStockLabel(wxID_STOP, false), _("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 | ||
29b07a38 | 355 | msg.Printf(_(" Generation: %u (T: %u ms), Population: %u "), |
e0a40292 GRG |
356 | m_tics, |
357 | m_topspeed? 0 : m_interval, | |
358 | 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 | ||
449 | void LifeFrame::OnOpen(wxCommandEvent& WXUNUSED(event)) | |
450 | { | |
451 | wxFileDialog filedlg(this, | |
452 | _("Choose a file to open"), | |
22c1c01b DS |
453 | wxEmptyString, |
454 | wxEmptyString, | |
f6bcfd97 BP |
455 | _("Life patterns (*.lif)|*.lif|All files (*.*)|*.*"), |
456 | wxOPEN | wxFILE_MUST_EXIST); | |
457 | ||
458 | if (filedlg.ShowModal() == wxID_OK) | |
459 | { | |
2b15fdfc | 460 | wxFileInputStream stream(filedlg.GetPath()); |
f6bcfd97 BP |
461 | LifeReader reader(stream); |
462 | ||
463 | // the reader handles errors itself, no need to do anything here | |
464 | if (reader.IsOk()) | |
e0a40292 | 465 | { |
f6bcfd97 | 466 | // stop if running and put the pattern |
5a1dca12 GRG |
467 | OnStop(); |
468 | m_life->Clear(); | |
f6bcfd97 BP |
469 | m_life->SetPattern(reader.GetPattern()); |
470 | ||
471 | // recenter canvas | |
e0a40292 | 472 | m_canvas->Recenter(0, 0); |
5a1dca12 GRG |
473 | m_tics = 0; |
474 | UpdateInfoText(); | |
5a1dca12 GRG |
475 | } |
476 | } | |
477 | } | |
478 | ||
087e4f4a GRG |
479 | void LifeFrame::OnSamples(wxCommandEvent& WXUNUSED(event)) |
480 | { | |
481 | // stop if it was running | |
482 | OnStop(); | |
483 | ||
471ed537 | 484 | // dialog box |
087e4f4a GRG |
485 | LifeSamplesDialog dialog(this); |
486 | ||
087e4f4a GRG |
487 | if (dialog.ShowModal() == wxID_OK) |
488 | { | |
f6bcfd97 | 489 | const LifePattern pattern = dialog.GetPattern(); |
087e4f4a | 490 | |
f6bcfd97 | 491 | // put the pattern |
e0a40292 | 492 | m_life->Clear(); |
f6bcfd97 | 493 | m_life->SetPattern(pattern); |
087e4f4a | 494 | |
e0a40292 GRG |
495 | // recenter canvas |
496 | m_canvas->Recenter(0, 0); | |
087e4f4a GRG |
497 | m_tics = 0; |
498 | UpdateInfoText(); | |
499 | } | |
500 | } | |
501 | ||
29b07a38 GRG |
502 | void LifeFrame::OnZoom(wxCommandEvent& event) |
503 | { | |
504 | int cellsize = m_canvas->GetCellSize(); | |
505 | ||
5d2ac6b8 | 506 | if ((event.GetId() == wxID_ZOOM_IN) && cellsize < 32) |
29b07a38 GRG |
507 | { |
508 | m_canvas->SetCellSize(cellsize * 2); | |
509 | UpdateUI(); | |
510 | } | |
5d2ac6b8 | 511 | else if ((event.GetId() == wxID_ZOOM_OUT) && cellsize > 1) |
29b07a38 GRG |
512 | { |
513 | m_canvas->SetCellSize(cellsize / 2); | |
514 | UpdateUI(); | |
515 | } | |
516 | } | |
517 | ||
518 | void LifeFrame::OnNavigate(wxCommandEvent& event) | |
519 | { | |
764835a5 | 520 | LifeCell c; |
29b07a38 GRG |
521 | |
522 | switch (event.GetId()) | |
523 | { | |
22c1c01b | 524 | case ID_NORTH: c = m_life->FindNorth(); break; |
29b07a38 GRG |
525 | case ID_SOUTH: c = m_life->FindSouth(); break; |
526 | case ID_WEST: c = m_life->FindWest(); break; | |
527 | case ID_EAST: c = m_life->FindEast(); break; | |
528 | case ID_CENTER: c = m_life->FindCenter(); break; | |
22c1c01b DS |
529 | default : |
530 | wxFAIL; | |
531 | // Fall through! | |
29b07a38 GRG |
532 | case ID_ORIGIN: c.i = c.j = 0; break; |
533 | } | |
534 | ||
535 | m_canvas->Recenter(c.i, c.j); | |
536 | } | |
537 | ||
538 | void LifeFrame::OnSlider(wxScrollEvent& event) | |
539 | { | |
540 | m_interval = event.GetPosition() * 100; | |
541 | ||
542 | if (m_running) | |
543 | { | |
544 | OnStop(); | |
545 | OnStart(); | |
546 | } | |
22c1c01b | 547 | |
29b07a38 GRG |
548 | UpdateInfoText(); |
549 | } | |
550 | ||
551 | void LifeFrame::OnTimer(wxTimerEvent& WXUNUSED(event)) | |
552 | { | |
553 | OnStep(); | |
554 | } | |
555 | ||
f6bcfd97 BP |
556 | void LifeFrame::OnClose(wxCloseEvent& WXUNUSED(event)) |
557 | { | |
558 | // Stop if it was running; this is absolutely needed because | |
559 | // the frame won't be actually destroyed until there are no | |
560 | // more pending events, and this in turn won't ever happen | |
561 | // if the timer is running faster than the window can redraw. | |
562 | OnStop(); | |
22c1c01b | 563 | Destroy(); |
f6bcfd97 BP |
564 | } |
565 | ||
5a1dca12 GRG |
566 | void LifeFrame::OnStart() |
567 | { | |
087e4f4a GRG |
568 | if (!m_running) |
569 | { | |
087e4f4a | 570 | m_timer->Start(m_interval); |
22c1c01b | 571 | m_running = true; |
e0a40292 | 572 | UpdateUI(); |
087e4f4a | 573 | } |
5a1dca12 GRG |
574 | } |
575 | ||
576 | void LifeFrame::OnStop() | |
577 | { | |
087e4f4a GRG |
578 | if (m_running) |
579 | { | |
087e4f4a | 580 | m_timer->Stop(); |
22c1c01b DS |
581 | m_running = false; |
582 | m_topspeed = false; | |
e0a40292 | 583 | UpdateUI(); |
087e4f4a | 584 | } |
5a1dca12 GRG |
585 | } |
586 | ||
29b07a38 | 587 | void LifeFrame::OnStep() |
5a1dca12 | 588 | { |
a36f0f83 GRG |
589 | if (m_life->NextTic()) |
590 | m_tics++; | |
591 | else | |
592 | OnStop(); | |
5a1dca12 | 593 | |
e0a40292 | 594 | m_canvas->DrawChanged(); |
a36f0f83 | 595 | UpdateInfoText(); |
5a1dca12 GRG |
596 | } |
597 | ||
a36f0f83 | 598 | |
5a1dca12 | 599 | // -------------------------------------------------------------------------- |
29b07a38 | 600 | // LifeNavigator miniframe |
5a1dca12 GRG |
601 | // -------------------------------------------------------------------------- |
602 | ||
29b07a38 | 603 | LifeNavigator::LifeNavigator(wxWindow *parent) |
22c1c01b | 604 | : wxMiniFrame(parent, wxID_ANY, |
29b07a38 GRG |
605 | _("Navigation"), |
606 | wxDefaultPosition, | |
607 | wxDefaultSize, | |
608 | wxCAPTION | wxSIMPLE_BORDER) | |
5a1dca12 | 609 | { |
22c1c01b | 610 | wxPanel *panel = new wxPanel(this, wxID_ANY); |
29b07a38 GRG |
611 | wxBoxSizer *sizer1 = new wxBoxSizer(wxVERTICAL); |
612 | wxBoxSizer *sizer2 = new wxBoxSizer(wxHORIZONTAL); | |
613 | ||
614 | // create bitmaps and masks for the buttons | |
615 | wxBitmap | |
616 | bmpn = wxBITMAP(north), | |
22c1c01b | 617 | bmpw = wxBITMAP(west), |
29b07a38 | 618 | bmpc = wxBITMAP(center), |
22c1c01b | 619 | bmpe = wxBITMAP(east), |
29b07a38 GRG |
620 | bmps = wxBITMAP(south); |
621 | ||
a99c96b0 | 622 | #if !defined(__WXGTK__) && !defined(__WXMOTIF__) && !defined(__WXMAC__) |
29b07a38 GRG |
623 | bmpn.SetMask(new wxMask(bmpn, *wxLIGHT_GREY)); |
624 | bmpw.SetMask(new wxMask(bmpw, *wxLIGHT_GREY)); | |
625 | bmpc.SetMask(new wxMask(bmpc, *wxLIGHT_GREY)); | |
22c1c01b | 626 | bmpe.SetMask(new wxMask(bmpe, *wxLIGHT_GREY)); |
29b07a38 GRG |
627 | bmps.SetMask(new wxMask(bmps, *wxLIGHT_GREY)); |
628 | #endif | |
629 | ||
630 | // create the buttons and attach tooltips to them | |
631 | wxBitmapButton | |
632 | *bn = new wxBitmapButton(panel, ID_NORTH, bmpn), | |
633 | *bw = new wxBitmapButton(panel, ID_WEST , bmpw), | |
634 | *bc = new wxBitmapButton(panel, ID_CENTER, bmpc), | |
635 | *be = new wxBitmapButton(panel, ID_EAST , bmpe), | |
636 | *bs = new wxBitmapButton(panel, ID_SOUTH, bmps); | |
637 | ||
7a85f3f9 | 638 | #if wxUSE_TOOLTIPS |
29b07a38 GRG |
639 | bn->SetToolTip(_("Find northernmost cell")); |
640 | bw->SetToolTip(_("Find westernmost cell")); | |
641 | bc->SetToolTip(_("Find center of mass")); | |
642 | be->SetToolTip(_("Find easternmost cell")); | |
643 | bs->SetToolTip(_("Find southernmost cell")); | |
7a85f3f9 | 644 | #endif |
29b07a38 GRG |
645 | |
646 | // add buttons to sizers | |
647 | sizer2->Add( bw, 0, wxCENTRE | wxWEST, 4 ); | |
648 | sizer2->Add( bc, 0, wxCENTRE); | |
649 | sizer2->Add( be, 0, wxCENTRE | wxEAST, 4 ); | |
650 | sizer1->Add( bn, 0, wxCENTRE | wxNORTH, 4 ); | |
651 | sizer1->Add( sizer2 ); | |
652 | sizer1->Add( bs, 0, wxCENTRE | wxSOUTH, 4 ); | |
653 | ||
b538bf7e | 654 | // set the panel and miniframe size |
29b07a38 | 655 | panel->SetSizer(sizer1); |
b538bf7e JS |
656 | |
657 | sizer1->Fit(panel); | |
658 | SetClientSize(panel->GetSize()); | |
659 | wxSize sz = GetSize(); | |
660 | SetSizeHints(sz.x, sz.y, sz.x, sz.y); | |
29b07a38 GRG |
661 | |
662 | // move it to a sensible position | |
663 | wxRect parentRect = parent->GetRect(); | |
664 | wxSize childSize = GetSize(); | |
665 | int x = parentRect.GetX() + | |
666 | parentRect.GetWidth(); | |
667 | int y = parentRect.GetY() + | |
668 | (parentRect.GetHeight() - childSize.GetHeight()) / 4; | |
669 | Move(x, y); | |
670 | ||
671 | // done | |
22c1c01b | 672 | Show(true); |
29b07a38 GRG |
673 | } |
674 | ||
675 | void LifeNavigator::OnClose(wxCloseEvent& event) | |
676 | { | |
dbf75be7 | 677 | // avoid if we can |
29b07a38 GRG |
678 | if (event.CanVeto()) |
679 | event.Veto(); | |
680 | else | |
681 | Destroy(); | |
682 | } | |
683 | ||
5a1dca12 GRG |
684 | |
685 | // -------------------------------------------------------------------------- | |
087e4f4a | 686 | // LifeCanvas |
5a1dca12 GRG |
687 | // -------------------------------------------------------------------------- |
688 | ||
689 | // canvas constructor | |
087e4f4a | 690 | LifeCanvas::LifeCanvas(wxWindow *parent, Life *life, bool interactive) |
22c1c01b | 691 | : wxWindow(parent, wxID_ANY, wxDefaultPosition, wxSize(100, 100), |
ee710d75 | 692 | wxSUNKEN_BORDER|wxFULL_REPAINT_ON_RESIZE) |
5a1dca12 | 693 | { |
087e4f4a GRG |
694 | m_life = life; |
695 | m_interactive = interactive; | |
696 | m_cellsize = 8; | |
e0a40292 GRG |
697 | m_status = MOUSE_NOACTION; |
698 | m_viewportX = 0; | |
699 | m_viewportY = 0; | |
700 | m_viewportH = 0; | |
701 | m_viewportW = 0; | |
702 | ||
703 | if (m_interactive) | |
704 | SetCursor(*wxCROSS_CURSOR); | |
705 | ||
706 | // reduce flicker if wxEVT_ERASE_BACKGROUND is not available | |
22c1c01b | 707 | SetBackgroundColour(*wxWHITE); |
5a1dca12 GRG |
708 | } |
709 | ||
710 | LifeCanvas::~LifeCanvas() | |
711 | { | |
22c1c01b | 712 | delete m_life; |
5a1dca12 GRG |
713 | } |
714 | ||
e0a40292 GRG |
715 | // recenter at the given position |
716 | void LifeCanvas::Recenter(wxInt32 i, wxInt32 j) | |
5a1dca12 | 717 | { |
e0a40292 GRG |
718 | m_viewportX = i - m_viewportW / 2; |
719 | m_viewportY = j - m_viewportH / 2; | |
5a1dca12 | 720 | |
087e4f4a | 721 | // redraw everything |
22c1c01b | 722 | Refresh(false); |
5a1dca12 GRG |
723 | } |
724 | ||
e0a40292 GRG |
725 | // set the cell size and refresh display |
726 | void LifeCanvas::SetCellSize(int cellsize) | |
5a1dca12 | 727 | { |
e0a40292 GRG |
728 | m_cellsize = cellsize; |
729 | ||
730 | // find current center | |
731 | wxInt32 cx = m_viewportX + m_viewportW / 2; | |
732 | wxInt32 cy = m_viewportY + m_viewportH / 2; | |
733 | ||
734 | // get current canvas size and adjust viewport accordingly | |
2fa7c206 | 735 | int w, h; |
e0a40292 GRG |
736 | GetClientSize(&w, &h); |
737 | m_viewportW = (w + m_cellsize - 1) / m_cellsize; | |
738 | m_viewportH = (h + m_cellsize - 1) / m_cellsize; | |
22c1c01b | 739 | |
e0a40292 GRG |
740 | // recenter |
741 | m_viewportX = cx - m_viewportW / 2; | |
742 | m_viewportY = cy - m_viewportH / 2; | |
743 | ||
744 | // adjust scrollbars | |
745 | if (m_interactive) | |
746 | { | |
747 | SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW); | |
748 | SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH); | |
749 | m_thumbX = m_viewportW; | |
750 | m_thumbY = m_viewportH; | |
751 | } | |
22c1c01b DS |
752 | |
753 | Refresh(false); | |
e0a40292 | 754 | } |
2480be69 | 755 | |
e0a40292 GRG |
756 | // draw a cell |
757 | void LifeCanvas::DrawCell(wxInt32 i, wxInt32 j, bool alive) | |
758 | { | |
759 | wxClientDC dc(this); | |
5a1dca12 | 760 | |
e0a40292 GRG |
761 | dc.SetPen(alive? *wxBLACK_PEN : *wxWHITE_PEN); |
762 | dc.SetBrush(alive? *wxBLACK_BRUSH : *wxWHITE_BRUSH); | |
5a1dca12 | 763 | |
e0a40292 GRG |
764 | dc.BeginDrawing(); |
765 | DrawCell(i, j, dc); | |
5a1dca12 | 766 | dc.EndDrawing(); |
5a1dca12 GRG |
767 | } |
768 | ||
e0a40292 | 769 | void LifeCanvas::DrawCell(wxInt32 i, wxInt32 j, wxDC &dc) |
5a1dca12 | 770 | { |
e0a40292 GRG |
771 | wxCoord x = CellToX(i); |
772 | wxCoord y = CellToY(j); | |
5a1dca12 | 773 | |
e0a40292 GRG |
774 | // if cellsize is 1 or 2, there will be no grid |
775 | switch (m_cellsize) | |
776 | { | |
777 | case 1: | |
778 | dc.DrawPoint(x, y); | |
779 | break; | |
780 | case 2: | |
781 | dc.DrawRectangle(x, y, 2, 2); | |
782 | break; | |
783 | default: | |
784 | dc.DrawRectangle(x + 1, y + 1, m_cellsize - 1, m_cellsize - 1); | |
785 | } | |
5a1dca12 GRG |
786 | } |
787 | ||
e0a40292 GRG |
788 | // draw all changed cells |
789 | void LifeCanvas::DrawChanged() | |
5a1dca12 | 790 | { |
e0a40292 GRG |
791 | wxClientDC dc(this); |
792 | ||
793 | size_t ncells; | |
764835a5 | 794 | LifeCell *cells; |
22c1c01b | 795 | bool done = false; |
e0a40292 GRG |
796 | |
797 | m_life->BeginFind(m_viewportX, | |
798 | m_viewportY, | |
799 | m_viewportX + m_viewportW, | |
800 | m_viewportY + m_viewportH, | |
22c1c01b DS |
801 | true); |
802 | ||
e0a40292 | 803 | dc.BeginDrawing(); |
e0a40292 GRG |
804 | |
805 | if (m_cellsize == 1) | |
5a1dca12 GRG |
806 | { |
807 | dc.SetPen(*wxBLACK_PEN); | |
5a1dca12 GRG |
808 | } |
809 | else | |
810 | { | |
e0a40292 GRG |
811 | dc.SetPen(*wxTRANSPARENT_PEN); |
812 | dc.SetBrush(*wxBLACK_BRUSH); | |
813 | } | |
030d06e1 | 814 | dc.SetLogicalFunction(wxINVERT); |
e0a40292 GRG |
815 | |
816 | while (!done) | |
817 | { | |
818 | done = m_life->FindMore(&cells, &ncells); | |
819 | ||
820 | for (size_t m = 0; m < ncells; m++) | |
821 | DrawCell(cells[m].i, cells[m].j, dc); | |
5a1dca12 | 822 | } |
e0a40292 | 823 | dc.EndDrawing(); |
5a1dca12 | 824 | } |
ecbdd409 | 825 | |
5a1dca12 | 826 | // event handlers |
babd36de | 827 | void LifeCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) |
5a1dca12 GRG |
828 | { |
829 | wxPaintDC dc(this); | |
e0a40292 GRG |
830 | wxRect rect = GetUpdateRegion().GetBox(); |
831 | wxCoord x, y, w, h; | |
832 | wxInt32 i0, j0, i1, j1; | |
833 | ||
834 | // find damaged area | |
835 | x = rect.GetX(); | |
836 | y = rect.GetY(); | |
837 | w = rect.GetWidth(); | |
838 | h = rect.GetHeight(); | |
839 | ||
840 | i0 = XToCell(x); | |
841 | j0 = YToCell(y); | |
842 | i1 = XToCell(x + w - 1); | |
843 | j1 = YToCell(y + h - 1); | |
844 | ||
845 | size_t ncells; | |
764835a5 | 846 | LifeCell *cells; |
5a1dca12 | 847 | |
22c1c01b DS |
848 | m_life->BeginFind(i0, j0, i1, j1, false); |
849 | bool done = m_life->FindMore(&cells, &ncells); | |
5a1dca12 | 850 | |
e0a40292 | 851 | // erase all damaged cells and draw the grid |
5a1dca12 | 852 | dc.BeginDrawing(); |
e0a40292 | 853 | dc.SetBrush(*wxWHITE_BRUSH); |
5a1dca12 | 854 | |
e0a40292 | 855 | if (m_cellsize <= 2) |
5a1dca12 | 856 | { |
e0a40292 GRG |
857 | // no grid |
858 | dc.SetPen(*wxWHITE_PEN); | |
859 | dc.DrawRectangle(x, y, w, h); | |
5a1dca12 | 860 | } |
e0a40292 GRG |
861 | else |
862 | { | |
863 | x = CellToX(i0); | |
864 | y = CellToY(j0); | |
865 | w = CellToX(i1 + 1) - x + 1; | |
866 | h = CellToY(j1 + 1) - y + 1; | |
867 | ||
868 | dc.SetPen(*wxLIGHT_GREY_PEN); | |
869 | for (wxInt32 yy = y; yy <= (y + h - m_cellsize); yy += m_cellsize) | |
870 | dc.DrawRectangle(x, yy, w, m_cellsize + 1); | |
871 | for (wxInt32 xx = x; xx <= (x + w - m_cellsize); xx += m_cellsize) | |
872 | dc.DrawLine(xx, y, xx, y + h); | |
873 | } | |
874 | ||
875 | // draw all alive cells | |
876 | dc.SetPen(*wxBLACK_PEN); | |
877 | dc.SetBrush(*wxBLACK_BRUSH); | |
878 | ||
879 | while (!done) | |
880 | { | |
881 | for (size_t m = 0; m < ncells; m++) | |
882 | DrawCell(cells[m].i, cells[m].j, dc); | |
883 | ||
884 | done = m_life->FindMore(&cells, &ncells); | |
885 | } | |
886 | ||
887 | // last set | |
888 | for (size_t m = 0; m < ncells; m++) | |
889 | DrawCell(cells[m].i, cells[m].j, dc); | |
5a1dca12 | 890 | |
5a1dca12 GRG |
891 | dc.EndDrawing(); |
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); | |
944 | dc.BeginDrawing(); | |
945 | ||
7989fb37 GRG |
946 | // draw a line of cells using Bresenham's algorithm |
947 | wxInt32 d, ii, jj, di, ai, si, dj, aj, sj; | |
948 | di = i - m_mi; | |
949 | ai = abs(di) << 1; | |
950 | si = (di < 0)? -1 : 1; | |
951 | dj = j - m_mj; | |
952 | aj = abs(dj) << 1; | |
953 | sj = (dj < 0)? -1 : 1; | |
954 | ||
955 | ii = m_mi; | |
956 | jj = m_mj; | |
22c1c01b | 957 | |
7989fb37 GRG |
958 | if (ai > aj) |
959 | { | |
960 | // iterate over i | |
22c1c01b DS |
961 | d = aj - (ai >> 1); |
962 | ||
7989fb37 GRG |
963 | while (ii != i) |
964 | { | |
5e9ff6ad GRG |
965 | m_life->SetCell(ii, jj, alive); |
966 | DrawCell(ii, jj, dc); | |
7989fb37 GRG |
967 | if (d >= 0) |
968 | { | |
969 | jj += sj; | |
22c1c01b | 970 | d -= ai; |
7989fb37 GRG |
971 | } |
972 | ii += si; | |
973 | d += aj; | |
974 | } | |
975 | } | |
976 | else | |
5a1dca12 | 977 | { |
7989fb37 GRG |
978 | // iterate over j |
979 | d = ai - (aj >> 1); | |
980 | ||
981 | while (jj != j) | |
982 | { | |
5e9ff6ad GRG |
983 | m_life->SetCell(ii, jj, alive); |
984 | DrawCell(ii, jj, dc); | |
7989fb37 GRG |
985 | if (d >= 0) |
986 | { | |
987 | ii += si; | |
22c1c01b | 988 | d -= aj; |
7989fb37 GRG |
989 | } |
990 | jj += sj; | |
991 | d += ai; | |
992 | } | |
5a1dca12 | 993 | } |
7989fb37 GRG |
994 | |
995 | // last cell | |
5e9ff6ad GRG |
996 | m_life->SetCell(ii, jj, alive); |
997 | DrawCell(ii, jj, dc); | |
7989fb37 GRG |
998 | m_mi = ii; |
999 | m_mj = jj; | |
5e9ff6ad GRG |
1000 | |
1001 | dc.EndDrawing(); | |
5a1dca12 | 1002 | } |
7989fb37 | 1003 | |
29b07a38 | 1004 | ((LifeFrame *) wxGetApp().GetTopWindow())->UpdateInfoText(); |
5a1dca12 GRG |
1005 | } |
1006 | ||
1007 | void LifeCanvas::OnSize(wxSizeEvent& event) | |
1008 | { | |
e0a40292 GRG |
1009 | // find center |
1010 | wxInt32 cx = m_viewportX + m_viewportW / 2; | |
1011 | wxInt32 cy = m_viewportY + m_viewportH / 2; | |
1012 | ||
1013 | // get new size | |
5a1dca12 GRG |
1014 | wxCoord w = event.GetSize().GetX(); |
1015 | wxCoord h = event.GetSize().GetY(); | |
e0a40292 GRG |
1016 | m_viewportW = (w + m_cellsize - 1) / m_cellsize; |
1017 | m_viewportH = (h + m_cellsize - 1) / m_cellsize; | |
1018 | ||
1019 | // recenter | |
1020 | m_viewportX = cx - m_viewportW / 2; | |
1021 | m_viewportY = cy - m_viewportH / 2; | |
1022 | ||
1023 | // scrollbars | |
1024 | if (m_interactive) | |
1025 | { | |
1026 | SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW); | |
1027 | SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH); | |
1028 | m_thumbX = m_viewportW; | |
1029 | m_thumbY = m_viewportH; | |
1030 | } | |
5a1dca12 GRG |
1031 | |
1032 | // allow default processing | |
1033 | event.Skip(); | |
1034 | } | |
e0a40292 GRG |
1035 | |
1036 | void LifeCanvas::OnScroll(wxScrollWinEvent& event) | |
1037 | { | |
254a2129 | 1038 | WXTYPE type = (WXTYPE)event.GetEventType(); |
e0a40292 GRG |
1039 | int pos = event.GetPosition(); |
1040 | int orient = event.GetOrientation(); | |
e0a40292 GRG |
1041 | |
1042 | // calculate scroll increment | |
33e39147 | 1043 | int scrollinc = 0; |
764835a5 | 1044 | if (type == wxEVT_SCROLLWIN_TOP) |
e0a40292 | 1045 | { |
764835a5 GD |
1046 | if (orient == wxHORIZONTAL) |
1047 | scrollinc = -m_viewportW; | |
1048 | else | |
1049 | scrollinc = -m_viewportH; | |
1050 | } | |
1051 | else | |
1052 | if (type == wxEVT_SCROLLWIN_BOTTOM) | |
1053 | { | |
1054 | if (orient == wxHORIZONTAL) | |
1055 | scrollinc = m_viewportW; | |
1056 | else | |
1057 | scrollinc = m_viewportH; | |
1058 | } | |
1059 | else | |
1060 | if (type == wxEVT_SCROLLWIN_LINEUP) | |
1061 | { | |
1062 | scrollinc = -1; | |
1063 | } | |
1064 | else | |
1065 | if (type == wxEVT_SCROLLWIN_LINEDOWN) | |
1066 | { | |
1067 | scrollinc = +1; | |
1068 | } | |
1069 | else | |
1070 | if (type == wxEVT_SCROLLWIN_PAGEUP) | |
1071 | { | |
1072 | scrollinc = -10; | |
1073 | } | |
1074 | else | |
1075 | if (type == wxEVT_SCROLLWIN_PAGEDOWN) | |
1076 | { | |
b1823f8b | 1077 | scrollinc = +10; |
764835a5 GD |
1078 | } |
1079 | else | |
1080 | if (type == wxEVT_SCROLLWIN_THUMBTRACK) | |
1081 | { | |
1082 | if (orient == wxHORIZONTAL) | |
e0a40292 | 1083 | { |
764835a5 GD |
1084 | scrollinc = pos - m_thumbX; |
1085 | m_thumbX = pos; | |
e0a40292 | 1086 | } |
764835a5 | 1087 | else |
33e39147 | 1088 | { |
764835a5 GD |
1089 | scrollinc = pos - m_thumbY; |
1090 | m_thumbY = pos; | |
33e39147 | 1091 | } |
e0a40292 | 1092 | } |
764835a5 GD |
1093 | else |
1094 | if (type == wxEVT_SCROLLWIN_THUMBRELEASE) | |
1095 | { | |
1096 | m_thumbX = m_viewportW; | |
1097 | m_thumbY = m_viewportH; | |
1098 | } | |
e0a40292 | 1099 | |
dbf75be7 GRG |
1100 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
1101 | // wxGTK and wxMotif update the thumb automatically (wxMSW doesn't); | |
1102 | // so reset it back as we always want it to be in the same position. | |
33e39147 | 1103 | if (type != wxEVT_SCROLLWIN_THUMBTRACK) |
e0a40292 GRG |
1104 | { |
1105 | SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW); | |
1106 | SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH); | |
1107 | } | |
1108 | #endif | |
1109 | ||
1110 | if (scrollinc == 0) return; | |
22c1c01b | 1111 | |
e0a40292 GRG |
1112 | // scroll the window and adjust the viewport |
1113 | if (orient == wxHORIZONTAL) | |
1114 | { | |
1115 | m_viewportX += scrollinc; | |
1116 | ScrollWindow( -m_cellsize * scrollinc, 0, (const wxRect *) NULL); | |
1117 | } | |
1118 | else | |
1119 | { | |
22c1c01b | 1120 | m_viewportY += scrollinc; |
e0a40292 GRG |
1121 | ScrollWindow( 0, -m_cellsize * scrollinc, (const wxRect *) NULL); |
1122 | } | |
1123 | } | |
1124 | ||
1125 | void LifeCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) | |
1126 | { | |
1127 | // do nothing. I just don't want the background to be erased, you know. | |
1128 | } | |
29b07a38 GRG |
1129 | |
1130 |