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