life.h 4.7 KB

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