1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        life.h
3 // Purpose:     The game of Life, created by J. H. Conway
4 // Author:      Guillermo Rodriguez Garcia, <guille@iies.es>
5 // Modified by:
6 // Created:     Jan/2000
7 // RCS-ID:      $Id: life.h 41020 2006-09-05 20:47:48Z VZ $
8 // Copyright:   (c) 2000, Guillermo Rodriguez Garcia
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef _LIFE_APP_H_
13 #define _LIFE_APP_H_
14 
15 // for compilers that support precompilation, includes "wx/wx.h"
16 #include "wx/wxprec.h"
17 
18 #ifdef __BORLANDC__
19     #pragma hdrstop
20 #endif
21 
22 // for all others, include the necessary headers
23 #ifndef WX_PRECOMP
24     #include "wx/wx.h"
25 #endif
26 
27 #include "wx/minifram.h"
28 
29 #include "game.h"
30 
31 
32 // --------------------------------------------------------------------------
33 // LifeCanvas
34 // --------------------------------------------------------------------------
35 
36 // Note that in LifeCanvas, all cell coordinates are
37 // named i, j, while screen coordinates are named x, y.
38 
39 class LifeCanvas : public wxWindow
40 {
41 public:
42     // ctor and dtor
43     LifeCanvas(wxWindow* parent, Life* life, bool interactive = true);
44     virtual ~LifeCanvas();
45 
46     // view management
GetCellSize()47     int  GetCellSize() const { return m_cellsize; };
48     void SetCellSize(int cellsize);
49     void Recenter(wxInt32 i, wxInt32 j);
50 
51     // drawing
52     void DrawChanged();
53     void DrawCell(wxInt32 i, wxInt32 j, bool alive);
54 
55 private:
56     // any class wishing to process wxWidgets events must use this macro
57     DECLARE_EVENT_TABLE()
58 
59     // draw a cell (parametrized by DC)
60     void DrawCell(wxInt32 i, wxInt32 j, wxDC &dc);
61 
62     // event handlers
63     void OnPaint(wxPaintEvent& event);
64     void OnMouse(wxMouseEvent& event);
65     void OnSize(wxSizeEvent& event);
66     void OnScroll(wxScrollWinEvent& event);
67     void OnEraseBackground(wxEraseEvent& event);
68 
69     // conversion between cell and screen coordinates
XToCell(wxCoord x)70     inline wxInt32 XToCell(wxCoord x) const { return (x / m_cellsize) + m_viewportX; };
YToCell(wxCoord y)71     inline wxInt32 YToCell(wxCoord y) const { return (y / m_cellsize) + m_viewportY; };
CellToX(wxInt32 i)72     inline wxCoord CellToX(wxInt32 i) const { return (i - m_viewportX) * m_cellsize; };
CellToY(wxInt32 j)73     inline wxCoord CellToY(wxInt32 j) const { return (j - m_viewportY) * m_cellsize; };
74 
75     // what is the user doing?
76     enum MouseStatus
77     {
78         MOUSE_NOACTION,
79         MOUSE_DRAWING,
80         MOUSE_ERASING
81     };
82 
83     Life        *m_life;            // Life object
84     int          m_cellsize;        // current cell size, in pixels
85     bool         m_interactive;     // is this canvas interactive?
86     MouseStatus  m_status;          // what is the user doing?
87     wxInt32      m_viewportX;       // first visible cell (x coord)
88     wxInt32      m_viewportY;       // first visible cell (y coord)
89     wxInt32      m_viewportW;       // number of visible cells (w)
90     wxInt32      m_viewportH;       // number of visible cells (h)
91     int          m_thumbX;          // horiz. scrollbar thumb position
92     int          m_thumbY;          // vert. scrollbar thumb position
93     wxInt32      m_mi, m_mj;        // last mouse position
94 };
95 
96 
97 // --------------------------------------------------------------------------
98 // LifeNavigator
99 // --------------------------------------------------------------------------
100 
101 class LifeNavigator : public wxMiniFrame
102 {
103 public:
104     // ctor
105     LifeNavigator(wxWindow *parent);
106 
107 private:
108     // any class wishing to process wxWidgets events must use this macro
109     DECLARE_EVENT_TABLE()
110 
111     // event handlers
112     void OnClose(wxCloseEvent& event);
113 };
114 
115 
116 // --------------------------------------------------------------------------
117 // LifeFrame
118 // --------------------------------------------------------------------------
119 
120 class LifeFrame : public wxFrame
121 {
122 public:
123     // ctor and dtor
124     LifeFrame();
125     virtual ~LifeFrame();
126 
127     // member functions
128     void UpdateInfoText();
129     void UpdateUI();
130 
131 private:
132     // any class wishing to process wxWidgets events must use this macro
133     DECLARE_EVENT_TABLE()
134 
135     // event handlers
136     void OnMenu(wxCommandEvent& event);
137     void OnOpen(wxCommandEvent& event);
138     void OnSamples(wxCommandEvent& event);
139     void OnNavigate(wxCommandEvent& event);
140     void OnZoom(wxCommandEvent& event);
141     void OnSlider(wxScrollEvent& event);
142     void OnTimer(wxTimerEvent& event);
143     void OnClose(wxCloseEvent& event);
144 
145     // event handler helpers
146     void OnStart();
147     void OnStop();
148     void OnStep();
149 
150     Life           *m_life;
151     LifeCanvas     *m_canvas;
152     LifeNavigator  *m_navigator;
153     wxStaticText   *m_text;
154     wxTimer        *m_timer;
155     bool            m_running;
156     bool            m_topspeed;
157     long            m_interval;
158     long            m_tics;
159 };
160 
161 
162 // --------------------------------------------------------------------------
163 // LifeApp
164 // --------------------------------------------------------------------------
165 
166 class LifeApp : public wxApp
167 {
168 public:
169     virtual bool OnInit();
170 };
171 
172 #endif  // _LIFE_APP_H_
173