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