]> git.saurik.com Git - wxWidgets.git/blame - demos/life/life.h
1. use a manifest constant wxNO_LEN instead of -1 for lengths everywhere
[wxWidgets.git] / demos / life / life.h
CommitLineData
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
2480be69
GRG
15// for compilers that support precompilation, includes "wx/wx.h"
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22// for all others, include the necessary headers
23#ifndef WX_PRECOMP
24 #include "wx/wx.h"
25#endif
26
29b07a38
GRG
27#include "wx/minifram.h"
28
2480be69
GRG
29#include "game.h"
30
29b07a38 31
2480be69
GRG
32// --------------------------------------------------------------------------
33// LifeCanvas
34// --------------------------------------------------------------------------
35
29b07a38
GRG
36// Note that in LifeCanvas, all cell coordinates are
37// named i, j, while screen coordinates are named x, y.
38
e0a40292 39class LifeCanvas : public wxWindow
2480be69
GRG
40{
41public:
42 // ctor and dtor
2a21ac15 43 LifeCanvas(wxWindow* parent, Life* life, bool interactive = true);
2480be69
GRG
44 ~LifeCanvas();
45
e0a40292
GRG
46 // view management
47 int GetCellSize() const { return m_cellsize; };
48 void SetCellSize(int cellsize);
49 void Recenter(wxInt32 i, wxInt32 j);
50
51 // drawing
52 void DrawChanged();
53 void DrawCell(wxInt32 i, wxInt32 j, bool alive);
54
55private:
be5a51fb 56 // any class wishing to process wxWidgets events must use this macro
e0a40292
GRG
57 DECLARE_EVENT_TABLE()
58
59 // draw a cell (parametrized by DC)
60 void DrawCell(wxInt32 i, wxInt32 j, wxDC &dc);
2480be69
GRG
61
62 // event handlers
63 void OnPaint(wxPaintEvent& event);
64 void OnMouse(wxMouseEvent& event);
65 void OnSize(wxSizeEvent& event);
e0a40292
GRG
66 void OnScroll(wxScrollWinEvent& event);
67 void OnEraseBackground(wxEraseEvent& event);
2480be69 68
e0a40292
GRG
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; };
2480be69 74
e0a40292 75 // what is the user doing?
2480be69
GRG
76 enum MouseStatus
77 {
78 MOUSE_NOACTION,
79 MOUSE_DRAWING,
80 MOUSE_ERASING
81 };
82
e0a40292
GRG
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)
5d2ac6b8
WS
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
2480be69
GRG
94};
95
29b07a38 96
2480be69 97// --------------------------------------------------------------------------
29b07a38 98// LifeNavigator
2480be69
GRG
99// --------------------------------------------------------------------------
100
29b07a38 101class LifeNavigator : public wxMiniFrame
2480be69
GRG
102{
103public:
29b07a38
GRG
104 // ctor
105 LifeNavigator(wxWindow *parent);
106
107private:
be5a51fb 108 // any class wishing to process wxWidgets events must use this macro
29b07a38
GRG
109 DECLARE_EVENT_TABLE()
110
111 // event handlers
112 void OnClose(wxCloseEvent& event);
2480be69
GRG
113};
114
29b07a38 115
2480be69
GRG
116// --------------------------------------------------------------------------
117// LifeFrame
118// --------------------------------------------------------------------------
119
120class LifeFrame : public wxFrame
121{
122public:
123 // ctor and dtor
124 LifeFrame();
125 ~LifeFrame();
126
127 // member functions
128 void UpdateInfoText();
e0a40292 129 void UpdateUI();
e0a40292
GRG
130
131private:
be5a51fb 132 // any class wishing to process wxWidgets events must use this macro
e0a40292 133 DECLARE_EVENT_TABLE()
2480be69
GRG
134
135 // event handlers
29b07a38 136 void OnMenu(wxCommandEvent& event);
f6bcfd97
BP
137 void OnOpen(wxCommandEvent& event);
138 void OnSamples(wxCommandEvent& event);
29b07a38
GRG
139 void OnNavigate(wxCommandEvent& event);
140 void OnZoom(wxCommandEvent& event);
e0a40292 141 void OnSlider(wxScrollEvent& event);
29b07a38 142 void OnTimer(wxTimerEvent& event);
e0a40292 143 void OnClose(wxCloseEvent& event);
29b07a38
GRG
144
145 // event handler helpers
2480be69
GRG
146 void OnStart();
147 void OnStop();
29b07a38
GRG
148 void OnStep();
149
5d2ac6b8 150 Life *m_life;
29b07a38
GRG
151 LifeCanvas *m_canvas;
152 LifeNavigator *m_navigator;
153 wxStaticText *m_text;
154 wxTimer *m_timer;
155 bool m_running;
156 bool m_topspeed;
157 long m_interval;
158 long m_tics;
2480be69
GRG
159};
160
29b07a38 161
2480be69
GRG
162// --------------------------------------------------------------------------
163// LifeApp
164// --------------------------------------------------------------------------
165
166class LifeApp : public wxApp
167{
168public:
169 virtual bool OnInit();
170};
171
172#endif // _LIFE_APP_H_