]> git.saurik.com Git - wxWidgets.git/blob - demos/life/life.h
Changed note that enter/leave events ARE supported
[wxWidgets.git] / demos / life / life.h
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 #if defined(__GNUG__) && !defined(__APPLE__)
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
31 #include "wx/minifram.h"
32
33 #include "game.h"
34
35
36 // --------------------------------------------------------------------------
37 // LifeCanvas
38 // --------------------------------------------------------------------------
39
40 // Note that in LifeCanvas, all cell coordinates are
41 // named i, j, while screen coordinates are named x, y.
42
43 class LifeCanvas : public wxWindow
44 {
45 public:
46 // ctor and dtor
47 LifeCanvas(wxWindow* parent, Life* life, bool interactive = true);
48 ~LifeCanvas();
49
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 wxWidgets 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);
65
66 // event handlers
67 void OnPaint(wxPaintEvent& event);
68 void OnMouse(wxMouseEvent& event);
69 void OnSize(wxSizeEvent& event);
70 void OnScroll(wxScrollWinEvent& event);
71 void OnEraseBackground(wxEraseEvent& event);
72
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; };
78
79 // what is the user doing?
80 enum MouseStatus
81 {
82 MOUSE_NOACTION,
83 MOUSE_DRAWING,
84 MOUSE_ERASING
85 };
86
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
97 wxInt32 m_mi, m_mj; // last mouse position
98 };
99
100
101 // --------------------------------------------------------------------------
102 // LifeNavigator
103 // --------------------------------------------------------------------------
104
105 class LifeNavigator : public wxMiniFrame
106 {
107 public:
108 // ctor
109 LifeNavigator(wxWindow *parent);
110
111 private:
112 // any class wishing to process wxWidgets events must use this macro
113 DECLARE_EVENT_TABLE()
114
115 // event handlers
116 void OnClose(wxCloseEvent& event);
117 };
118
119
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();
133 void UpdateUI();
134
135 private:
136 // any class wishing to process wxWidgets events must use this macro
137 DECLARE_EVENT_TABLE()
138
139 // event handlers
140 void OnMenu(wxCommandEvent& event);
141 void OnOpen(wxCommandEvent& event);
142 void OnSamples(wxCommandEvent& event);
143 void OnNavigate(wxCommandEvent& event);
144 void OnZoom(wxCommandEvent& event);
145 void OnSlider(wxScrollEvent& event);
146 void OnTimer(wxTimerEvent& event);
147 void OnClose(wxCloseEvent& event);
148
149 // event handler helpers
150 void OnStart();
151 void OnStop();
152 void OnStep();
153
154 Life *m_life;
155 LifeCanvas *m_canvas;
156 LifeNavigator *m_navigator;
157 wxStaticText *m_text;
158 wxTimer *m_timer;
159 bool m_running;
160 bool m_topspeed;
161 long m_interval;
162 long m_tics;
163 };
164
165
166 // --------------------------------------------------------------------------
167 // LifeApp
168 // --------------------------------------------------------------------------
169
170 class LifeApp : public wxApp
171 {
172 public:
173 virtual bool OnInit();
174 };
175
176 #endif // _LIFE_APP_H_