]>
git.saurik.com Git - wxWidgets.git/blob - demos/life/life.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: The game of Life, created by J. H. Conway
4 // Author: Guillermo Rodriguez Garcia, <guille@iies.es>
8 // Copyright: (c) 2000, Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 // for compilers that support precompilation, includes "wx/wx.h"
16 #include "wx/wxprec.h"
22 // for all others, include the necessary headers
27 #include "wx/minifram.h"
32 // --------------------------------------------------------------------------
34 // --------------------------------------------------------------------------
36 // Note that in LifeCanvas, all cell coordinates are
37 // named i, j, while screen coordinates are named x, y.
39 class LifeCanvas
: public wxWindow
43 LifeCanvas(wxWindow
* parent
, Life
* life
, bool interactive
= true);
44 virtual ~LifeCanvas();
47 int GetCellSize() const { return m_cellsize
; };
48 void SetCellSize(int cellsize
);
49 void Recenter(wxInt32 i
, wxInt32 j
);
53 void DrawCell(wxInt32 i
, wxInt32 j
, bool alive
);
56 // any class wishing to process wxWidgets events must use this macro
59 // draw a cell (parametrized by DC)
60 void DrawCell(wxInt32 i
, wxInt32 j
, wxDC
&dc
);
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
);
69 // conversion between cell and screen coordinates
70 inline wxInt32
XToCell(wxCoord x
) const { return (x
/ m_cellsize
) + m_viewportX
; };
71 inline wxInt32
YToCell(wxCoord y
) const { return (y
/ m_cellsize
) + m_viewportY
; };
72 inline wxCoord
CellToX(wxInt32 i
) const { return (i
- m_viewportX
) * m_cellsize
; };
73 inline wxCoord
CellToY(wxInt32 j
) const { return (j
- m_viewportY
) * m_cellsize
; };
75 // what is the user doing?
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
97 // --------------------------------------------------------------------------
99 // --------------------------------------------------------------------------
101 class LifeNavigator
: public wxMiniFrame
105 LifeNavigator(wxWindow
*parent
);
108 // any class wishing to process wxWidgets events must use this macro
109 DECLARE_EVENT_TABLE()
112 void OnClose(wxCloseEvent
& event
);
116 // --------------------------------------------------------------------------
118 // --------------------------------------------------------------------------
120 class LifeFrame
: public wxFrame
125 virtual ~LifeFrame();
128 void UpdateInfoText();
132 // any class wishing to process wxWidgets events must use this macro
133 DECLARE_EVENT_TABLE()
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
);
145 // event handler helpers
151 LifeCanvas
*m_canvas
;
152 LifeNavigator
*m_navigator
;
153 wxStaticText
*m_text
;
162 // --------------------------------------------------------------------------
164 // --------------------------------------------------------------------------
166 class LifeApp
: public wxApp
169 virtual bool OnInit();
172 #endif // _LIFE_APP_H_