Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective Move event handler binding from constructor code to event tables Instructions Up until now all events have been configured using the this->Bind method. This

Objective

Move event handler binding from constructor code to event tables

Instructions

  1. Up until now all events have been configured using the this->Bind method.  This is useful, or even necessary, in some instances, but for code readability most events should be configured in an event table.
  2. Add the macro wxDECLARE_EVENT_TABLE(); to the main window header file below the all the methods that have been declared.
  3. At the top of the main window cpp file, below the #include entries, is where the event table will be declared.   See below for an example of how the event table should look.
  4. The first event to move to the event table is the size change event.
  5. The same steps will need to be repeated for the drawing panel.  The PAINT and LEFT_UP events can be move into the drawing panel event table.
  6. As events are added to the event table, any events configured using Bind can be removed.

class DrawingPanel : public wxPanel
{
public:
   DrawingPanel(wxFrame* parent, std::vector>& gameBoard);
   ~DrawingPanel();

   void CalculateCellSize();
   void OnPaint(wxPaintEvent& event);
 

   void SetGridSize(int size);
   void OnMouseUp(wxMouseEvent& event);
   
private:
 
   int gridSize = 15;
   int cellWidth;
   int cellHeight;

   std::vector>& _gameBoard;
};
DrawingPanel::DrawingPanel(wxFrame* parent, std::vector>& gameBoard) : wxPanel(parent, wxID_ANY, wxPoint(0, 0), wxSize(200, 200)), _gameBoard(gameBoard)
{
this->SetBackgroundStyle(wxBG_STYLE_PAINT);

this->Bind(wxEVT_PAINT, &DrawingPanel::OnPaint, this);

this->Bind(wxEVT_LEFT_UP, &DrawingPanel::OnMouseUp, this);
CalculateCellSize();


}
DrawingPanel::~DrawingPanel()
{

}

void DrawingPanel::CalculateCellSize()
{
wxSize panelSize = GetParent()->GetClientSize();
cellWidth = panelSize.GetWidth() / gridSize;
cellHeight = panelSize.GetHeight() / gridSize;
}

void DrawingPanel::OnPaint(wxPaintEvent& event)
{
wxAutoBufferedPaintDC dc(this);
dc.Clear();

wxGraphicsContext* context = wxGraphicsContext::Create(dc);
if (!context)
{
 return;
}

context->SetPen(*wxBLACK);
context->SetBrush(*wxWHITE);


for (int row = 0; row < gridSize; row++)
{
 for (int col = 0; col < gridSize; col++)
 {
  int x = col * cellWidth;
  int y = row * cellHeight;
  // Check the corresponding bool in the game board
  if (_gameBoard[row][col])
  {
   context->SetBrush(*wxLIGHT_GREY); // Living cell color
  }
  else
  {
   context->SetBrush(*wxWHITE); // Dead cell color
  }

  context->DrawRectangle(x, y, cellWidth, cellHeight);
 }
}

}
void DrawingPanel::SetGridSize(int size)
{
gridSize = size;
CalculateCellSize(); // Recalculate cell size based on the new grid size
}

void DrawingPanel::OnMouseUp(wxMouseEvent& event)
{
int mouseX = event.GetX();
int mouseY = event.GetY();

int row = mouseY / cellHeight;
int col = mouseX / cellWidth;

if (row >= 0 && row < gridSize && col >= 0 && col < gridSize)
{
 // Toggle the state of the clicked cell
 _gameBoard[row][col] = !_gameBoard[row][col];
 Refresh();
}
}

wxBEGIN_EVENT_TABLE(MainWindow, wxFrame)
       
wxEND_EVENT_TABLE();

MainWindow::MainWindow() :wxFrame(nullptr, wxID_ANY, "Game of Life", wxPoint(0, 0), wxSize(200, 200))
{
_drawingPanel = new DrawingPanel(this, _gameBoard);


_sizer = new wxBoxSizer(wxVERTICAL);

_sizer->Add(_drawingPanel, 1, wxEXPAND | wxALL);

Bind(wxEVT_SIZE, &MainWindow::OnSizeChanged, this);

SetSizer(_sizer);
InitializeGameBoard();
}
MainWindow::~MainWindow()
{

}
void MainWindow::OnSizeChanged(wxSizeEvent& event)
{
_drawingPanel->SetSize(event.GetSize());
_drawingPanel->Refresh();
}

void MainWindow::InitializeGameBoard()
{
_gameBoard.resize(gridSize); // Resize the game board vector to the grid size

for (int i = 0; i < gridSize; i++)
{
 _gameBoard[i].resize(gridSize); // Resize each sub-vector to the grid size
}

_drawingPanel->SetGridSize(gridSize); // Pass the grid size to the drawing panel
}

class MainWindow : public wxFrame
{
private:

   DrawingPanel* _drawingPanel = nullptr;
   wxBoxSizer* _sizer = nullptr;

   std::vector> _gameBoard;
   int gridSize = 15;
public:
   MainWindow();
   ~MainWindow();

   void OnSizeChanged(wxSizeEvent& event);
   void InitializeGameBoard();

   wxDECLARE_EVENT_TABLE();
};

 


C++

wxBEGIN_EVENT_TABLE(MainWindow, wxFrame)
EVT_SIZE(MainWindow::OnSizeChange)
wxEND_EVENT_TABLE()
  




Step by Step Solution

3.38 Rating (151 Votes )

There are 3 Steps involved in it

Step: 1

class DrawingPanel public wxPanel public DrawingPanelwxFrame parent stdvector gameBoard DrawingPanel void CalculateCellSize void OnPaintwxPaintEvent e... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

International Marketing And Export Management

Authors: Gerald Albaum , Alexander Josiassen , Edwin Duerr

8th Edition

1292016922, 978-1292016924

More Books

Students also viewed these Algorithms questions