]>
git.saurik.com Git - wxWidgets.git/blob - demos/life/life.h
dd36aebe3931394231094825131997783bb2311f
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 #include "wx/minifram.h"
20 // --------------------------------------------------------------------------
22 // --------------------------------------------------------------------------
24 // Note that in LifeCanvas, all cell coordinates are
25 // named i, j, while screen coordinates are named x, y.
27 class LifeCanvas
: public wxWindow
31 LifeCanvas(wxWindow
* parent
, Life
* life
, bool interactive
= true);
32 virtual ~LifeCanvas();
35 int GetCellSize() const { return m_cellsize
; };
36 void SetCellSize(int cellsize
);
37 void Recenter(wxInt32 i
, wxInt32 j
);
41 void DrawCell(wxInt32 i
, wxInt32 j
, bool alive
);
44 // any class wishing to process wxWidgets events must use this macro
47 // draw a cell (parametrized by DC)
48 void DrawCell(wxInt32 i
, wxInt32 j
, wxDC
&dc
);
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
);
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
; };
63 // what is the user doing?
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
85 // --------------------------------------------------------------------------
87 // --------------------------------------------------------------------------
89 class LifeNavigator
: public wxMiniFrame
93 LifeNavigator(wxWindow
*parent
);
96 // any class wishing to process wxWidgets events must use this macro
100 void OnClose(wxCloseEvent
& event
);
104 // --------------------------------------------------------------------------
106 // --------------------------------------------------------------------------
108 class LifeFrame
: public wxFrame
113 virtual ~LifeFrame();
116 void UpdateInfoText();
120 // any class wishing to process wxWidgets events must use this macro
121 DECLARE_EVENT_TABLE()
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
);
133 // event handler helpers
139 LifeCanvas
*m_canvas
;
140 LifeNavigator
*m_navigator
;
141 wxStaticText
*m_text
;
150 // --------------------------------------------------------------------------
152 // --------------------------------------------------------------------------
154 class LifeApp
: public wxApp
157 virtual bool OnInit();
160 #endif // _LIFE_APP_H_