Trying to implement a game menu with buttons. When button clicked it enters a submenu which is a new panel. It all works but the problems come with sizers. I have also sizers for every panel and at first when I boot up the program the main menus sizer works – when the window size is changed the maximum size is the sizes of buttons. Now when I go to a submenu, the sizers stop functioning and also when going back to main menu, it doesn’t work there either. What am I doing wrong?
I tried passing
this
into other panel SetSizeHints(), but it turned out it becomes NULL at some point. So I tried passing the panels themselves, but it doesn’t work aswell. My code:
#include "MainFrame.h"
#include "Stats.h"
#include <cstring>
#include <wx/wx.h>
#include "FileAlgo.h"
#include <string>
char sym;
MainFrame::MainFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title), main_panel(nullptr), stats_panel(nullptr), game_panel(nullptr) {
main_panel = new wxPanel(this);
wxButton* button_start = new wxButton(main_panel, wxID_ANY, "1. Start", wxPoint(325, 150), wxSize(100, 35));
wxButton* button_stats = new wxButton(main_panel, wxID_ANY, "2. Stats", wxPoint(325, 250), wxSize(100, 35));
wxButton* button_exit = new wxButton(main_panel, wxID_ANY, "3. Exit", wxPoint(325, 350), wxSize(100, 35));
wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL);
wxSizerFlags flags = wxSizerFlags().CenterHorizontal().Border(wxALL, 25);
boxSizer->AddStretchSpacer(1);
boxSizer->Add(button_start, flags);
boxSizer->Add(button_stats, flags);
boxSizer->Add(button_exit, flags);
boxSizer->AddStretchSpacer(1);
main_panel->SetSizer(boxSizer);
boxSizer->SetSizeHints(this);
button_start->Bind(wxEVT_BUTTON, &MainFrame::OnStartClicked, this);
button_stats->Bind(wxEVT_BUTTON, &MainFrame::OnStatsClicked, this);
button_exit->Bind(wxEVT_BUTTON, &MainFrame::OnExitClicked, this);
CreateStatusBar();
SetClientSize(700, 500);
}
void MainFrame::OnStartClicked(wxCommandEvent& evt) {
wxLogStatus("Button Clicked1");
if (!game_panel) {
//genFile()
std::vector<std::string> text = readFromFile(genFile());
//std::string tekst_blokk(text.begin(), text.end());
/*for (char sym : text) {
tekst_blokk += sym;
}*/
std::string tekst_to_display;
for (const auto& text_line : text) {
tekst_to_display += text_line;
}
game_panel = new wxPanel(this);
wxStaticText* game_text = new wxStaticText(game_panel, wxID_ANY, tekst_to_display);
wxBoxSizer* boxSizerGM = new wxBoxSizer(wxVERTICAL);
wxSizerFlags flags_gm = wxSizerFlags().CenterHorizontal().Border(wxALL, 25);
boxSizerGM->AddStretchSpacer(1);
boxSizerGM->Add(game_text, flags_gm);
boxSizerGM->AddStretchSpacer(1);
game_panel->SetSizer(boxSizerGM);
boxSizerGM->SetSizeHints(game_panel);
}
main_panel->Hide();
game_panel->Show();
game_panel->SetSize(GetClientSize());
Layout();
}
void MainFrame::OnExitStatsClicked(wxCommandEvent& evt) {
stats_panel->Hide();
main_panel->Show();
}
void MainFrame::OnStatsClicked(wxCommandEvent& evt) {
wxLogStatus("Button Clicked2");
if (!stats_panel) {
Stats gameStats;
stats_panel = new wxPanel(this);
wxButton* button_exit_st = new wxButton(stats_panel, wxID_ANY, "Peamenüü", wxPoint(325, 250), wxSize(100, 35));
std::vector<std::string> lastTenLines = gameStats.getLastTenLines();
std::string statistika{};
for (const auto& line : lastTenLines) {
statistika += line + 'n';
}
//wxLogStatus(statistika);
wxStaticText* stat_box = new wxStaticText(stats_panel, wxID_ANY, statistika);
wxBoxSizer* boxSizerST = new wxBoxSizer(wxVERTICAL);
wxSizerFlags flags_st = wxSizerFlags().CenterHorizontal().Border(wxALL, 25);
boxSizerST->AddStretchSpacer(1);
boxSizerST->Add(stat_box, flags_st);
boxSizerST->Add(button_exit_st, flags_st);
boxSizerST->AddStretchSpacer(1);
stats_panel->SetSizer(boxSizerST);
boxSizerST->SetSizeHints(stats_panel);
button_exit_st->Bind(wxEVT_BUTTON, &MainFrame::OnExitStatsClicked, this);
}
main_panel->Hide();
stats_panel->Show();
stats_panel->SetSize(GetClientSize());
Layout();
}
void MainFrame::OnExitClicked(wxCommandEvent& evt) {
wxLogStatus("Éxiting");
Close(true);
}