From 6c34ef20fb217d240c790b24ea330592679ac010 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sat, 26 Feb 2022 21:43:09 +0100 Subject: Now engines can be added --- src/MainWindow.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 20 deletions(-) (limited to 'src/MainWindow.cpp') diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 348e772..b048c62 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -8,6 +8,7 @@ wxDEFINE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); wxDEFINE_EVENT(NEW_GAME_EVENT, wxCommandEvent); wxDEFINE_EVENT(CLOSE_TAB_EVENT, wxCommandEvent); +wxDEFINE_EVENT(REFRESH_ENGINE_LIST, wxCommandEvent); /// ---------- MainWindow ---------- @@ -21,23 +22,18 @@ MainWindow::MainWindow() /// File menu wxMenu *menuFile = new wxMenu; menuFile->Append(1, "Open", "Open file"); - Bind(wxEVT_MENU, &MainWindow::OnOpen, this, 1); menuFile->AppendSeparator(); menuFile->Append(10, "Save", "Save current game"); menuFile->Append(11, "Save As", "Save current game as"); menuFile->AppendSeparator(); menuFile->Append(4, "Settings", "Configure OChess"); - Bind(wxEVT_MENU, &MainWindow::OnSettings, this, 4); menuFile->AppendSeparator(); menuFile->Append(wxID_EXIT); - Bind(wxEVT_MENU, &MainWindow::OnExit, this, wxID_EXIT); // Game menu menuGame = new wxMenu; menuGame->Append(2, "New", "Create new game"); - Bind(wxEVT_MENU, &MainWindow::OnMenuNewGame, this, 2); menuGame->Append(3, "New from FEN", "Create new game using FEN"); - Bind(wxEVT_MENU, &MainWindow::OnMenuNewGame, this, 3); // Game base menu wxMenu *menuBase = new wxMenu; @@ -46,7 +42,10 @@ MainWindow::MainWindow() // Engine menu wxMenu *engineMenu = new wxMenu; engineMenu->Append(6, "New", "Create a new engine configuration"); - Bind(wxEVT_MENU, &MainWindow::OnNewEngine, this, 6); + manageMenu = new wxMenu; + engineMenu->AppendSubMenu(manageMenu, "Manage"); + wxCommandEvent dummy(REFRESH_ENGINE_LIST, GetId()); + OnRefreshEngineList(dummy); /// Menu bar menuBar = new wxMenuBar; @@ -66,24 +65,77 @@ MainWindow::MainWindow() Bind(NEW_GAME_EVENT, &MainWindow::OnNewGame, this, wxID_ANY); Bind(wxEVT_CLOSE_WINDOW, &MainWindow::OnClose, this); Bind(CLOSE_TAB_EVENT, &MainWindow::OnCloseTabEvent, this, wxID_ANY); + Bind(wxEVT_MENU, &MainWindow::OnMenuItemClick, this, wxID_ANY); + Bind(REFRESH_ENGINE_LIST, &MainWindow::OnRefreshEngineList, this, wxID_ANY); /*BaseTab *bt = new BaseTab((wxFrame *)notebook, "/home/loic/hartwig_tests.pgn"); notebook->AddPage(bt, bt->GetLabel()); notebook->SetSelection(notebook->GetPageIndex(bt));*/ -/* - EngineTab *bt = - new EngineTab((wxWindow *)notebook, - new uciadapter::UCI("/home/loic/.local/bin/stockfish"), - "/home/loic/.local/bin/stockfish"); - notebook->AddPage(bt, bt->GetLabel()); - notebook->SetSelection(notebook->GetPageIndex(bt));*/ + /* + EngineTab *bt = + new EngineTab((wxWindow *)notebook, + new uciadapter::UCI("/home/loic/.local/bin/stockfish"), + "/home/loic/.local/bin/stockfish"); + notebook->AddPage(bt, bt->GetLabel()); + notebook->SetSelection(notebook->GetPageIndex(bt));*/ } void MainWindow::OnCloseTabEvent(wxCommandEvent &event) { notebook->DeletePage(notebook->GetSelection()); } -void MainWindow::OnNewEngine(wxCommandEvent &event) { +void MainWindow::OnMenuItemClick(wxCommandEvent &event) { + std::uint32_t id = event.GetId(); + if (id == wxID_EXIT) { + Close(true); + } else if (id >= 100) { + wxLogDebug("Engine selected!"); + wxMenuItemList items = manageMenu->GetMenuItems(); + for (wxMenuItem *item : items) { + if (item->GetId() == id) { + wxLogDebug("Selected %s", item->GetItemLabel()); + EngineTab *bt = new EngineTab((wxWindow *)notebook, + item->GetItemLabel().ToStdString()); + notebook->AddPage(bt, bt->GetLabel()); + notebook->SetSelection(notebook->GetPageIndex(bt)); + } + } + } else if (id == 1) { + OpenFile(); + } else if (id == 2) { + NewGame(false); + } else if (id == 3) { + NewGame(true); + } else if (id == 4) { + OpenSettings(); + } else if (id == 6) { + NewEngine(); + } +} + +void MainWindow::OnRefreshEngineList(wxCommandEvent &event) { + // Delete all items + wxMenuItemList items = manageMenu->GetMenuItems(); + for (wxMenuItem *item : items) { + manageMenu->Delete(item->GetId()); + } + // Refresh items + CONFIG_OPEN(conf); + conf->SetPath("engines/"); + wxString engine_name; + long index; + if (conf->GetFirstGroup(engine_name, index)) { + std::uint32_t id = 0; + do { + manageMenu->Append(100 + id, engine_name, "Configure " + engine_name); + id++; + } while (conf->GetNextGroup(engine_name, index)); + } + + CONFIG_CLOSE(conf); +} + +void MainWindow::NewEngine() { wxFileDialog openFileDialog(this, _("Use engine"), "", "", "Executable|*", wxFD_OPEN | wxFD_FILE_MUST_EXIST); if (openFileDialog.ShowModal() != wxID_CANCEL) { @@ -100,7 +152,7 @@ void MainWindow::OnNewEngine(wxCommandEvent &event) { } } -void MainWindow::OnSettings(wxCommandEvent &event) { +void MainWindow::OpenSettings() { if (prefsEditor != NULL) { delete prefsEditor; } @@ -117,8 +169,6 @@ void MainWindow::ApplyPreferences() { } } -void MainWindow::OnExit(wxCommandEvent &event) { Close(true); } - std::vector MainWindow::ListTabInfos() { std::vector tinfos; for (int i = 0; i < notebook->GetPageCount(); i++) { @@ -134,7 +184,7 @@ void MainWindow::OnClose(wxCloseEvent &e) { e.Skip(); } -void MainWindow::OnOpen(wxCommandEvent &event) { +void MainWindow::OpenFile() { wxFileDialog openFileDialog(this, _("Open file"), "", "", "PGN files (*.pgn)|*.pgn", wxFD_OPEN | wxFD_FILE_MUST_EXIST); @@ -147,8 +197,8 @@ void MainWindow::OnOpen(wxCommandEvent &event) { } } -void MainWindow::OnMenuNewGame(wxCommandEvent &event) { - if (event.GetId() == 3) { +void MainWindow::NewGame(bool useFen) { + if (useFen) { wxTextEntryDialog *dial = new wxTextEntryDialog(NULL, wxT("Enter FEN:"), wxT("Error")); if (dial->ShowModal() == wxID_OK) { -- cgit v1.2.3