From e601902dd5a9f023594fef6a0f4995e59b4d9a0e Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sat, 26 Feb 2022 20:34:42 +0100 Subject: [PATCH] Improve engine managemen --- src/MainWindow.cpp | 15 ++++-- src/MainWindow.hpp | 2 + src/engine_tab/EngineTab.cpp | 40 ++++++++++++--- src/engine_tab/EngineTab.hpp | 6 ++- src/engine_tab/EngineTabBF.cpp | 3 ++ src/engine_tab/EngineTabBF.h | 2 + src/game_tab/editor/EditorPanel.cpp | 11 ++++ src/game_tab/editor/EditorPanelBF.cpp | 2 +- src/game_tab/editor/EditorPanelBF.h | 3 +- tools/wxframebuilder/EditorPanel.fbp | 5 +- tools/wxframebuilder/EngineTab.fbp | 73 +++++++++++++++++++++++++++ 11 files changed, 146 insertions(+), 16 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index efe3f06..348e772 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -7,6 +7,7 @@ wxDEFINE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); wxDEFINE_EVENT(NEW_GAME_EVENT, wxCommandEvent); +wxDEFINE_EVENT(CLOSE_TAB_EVENT, wxCommandEvent); /// ---------- MainWindow ---------- @@ -64,16 +65,24 @@ MainWindow::MainWindow() Bind(REFRESH_TAB_TITLE, &MainWindow::OnRefreshTabTitle, this, wxID_ANY); 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); /*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, - "/home/loic/.local/bin/stockfish"); notebook->AddPage(bt, bt->GetLabel()); +/* + 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) { wxFileDialog openFileDialog(this, _("Use engine"), "", "", "Executable|*", wxFD_OPEN | wxFD_FILE_MUST_EXIST); diff --git a/src/MainWindow.hpp b/src/MainWindow.hpp index 918dc8a..4111612 100644 --- a/src/MainWindow.hpp +++ b/src/MainWindow.hpp @@ -10,6 +10,7 @@ wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); wxDECLARE_EVENT(NEW_GAME_EVENT, wxCommandEvent); +wxDECLARE_EVENT(CLOSE_TAB_EVENT, wxCommandEvent); class MainWindow : public wxFrame { wxAuiNotebook *notebook; @@ -27,6 +28,7 @@ class MainWindow : public wxFrame { void NewGame(Game *game); void OnSettings(wxCommandEvent &event); void OnNewEngine(wxCommandEvent &event); + void OnCloseTabEvent(wxCommandEvent &event); public: MainWindow(); diff --git a/src/engine_tab/EngineTab.cpp b/src/engine_tab/EngineTab.cpp index ae9b447..ca2880f 100644 --- a/src/engine_tab/EngineTab.cpp +++ b/src/engine_tab/EngineTab.cpp @@ -1,20 +1,27 @@ #include "EngineTab.hpp" -EngineTab::EngineTab(wxWindow *parent, uciadapter::UCI *engine,std::string engine_path_or_name) +EngineTab::EngineTab(wxWindow *parent, uciadapter::UCI *engine, + std::string engine_path_or_name) : EngineTabBF(parent), TabInfos(TabInfos::ENGINE), enginePath(engine_path_or_name), engine(engine) { SetLabel("New Engine"); engine_location->SetValue(engine_path_or_name); - confGroup = "engines/bob"; CONFIG_OPEN(conf); - conf->DeleteGroup(confGroup); - bool configExists = conf->HasGroup(confGroup); - conf->Write(confGroup + "/path", wxString(engine_path_or_name)); - CONFIG_CLOSE(conf); - if (!configExists) { - InitConfiguration(); + // conf->DeleteGroup(confGroup); + engineName = "NewEngine"; + confGroup = "engines/" + engineName; + std::uint32_t key = 2; + while (conf->HasGroup(confGroup)) { + engineName = "NewEngine" + std::to_string(key); + confGroup = "engines/" + engineName; + key++; } + engine_name->SetValue(engineName); + + // conf->Write(confGroup + "/path", wxString(engine_path_or_name)); + CONFIG_CLOSE(conf); + InitConfiguration(); // Build wxPropertyGrid according to engine configuration CONFIG_OPEN(conf2); @@ -43,10 +50,27 @@ EngineTab::EngineTab(wxWindow *parent, uciadapter::UCI *engine,std::string engin CONFIG_CLOSE(conf2); Bind(wxEVT_BUTTON, &EngineTab::OnSave, this, ENGINE_SAVE_CONF_BUTTON); + Bind(wxEVT_BUTTON, &EngineTab::OnDelete, this, ENGINE_DELETE_CONF_BUTTON); +} + +void EngineTab::OnDelete(wxCommandEvent &event) { + CONFIG_OPEN(conf); + conf->DeleteGroup(confGroup); + CONFIG_CLOSE(conf); + + wxCommandEvent closeTabEvent(CLOSE_TAB_EVENT, GetId()); + closeTabEvent.SetEventObject(this); + ProcessEvent(closeTabEvent); } void EngineTab::OnSave(wxCommandEvent &event) { CONFIG_OPEN(conf2); + wxString new_engine_name = engine_name->GetValue(); + if (new_engine_name != engineName) { + conf2->RenameGroup(confGroup, "engines/" + new_engine_name); + engineName = new_engine_name; + confGroup = "engines/" + engineName; + } long index; std::string optsPath = confGroup + "/options"; conf2->SetPath(optsPath); diff --git a/src/engine_tab/EngineTab.hpp b/src/engine_tab/EngineTab.hpp index c4e8a52..6e4913b 100644 --- a/src/engine_tab/EngineTab.hpp +++ b/src/engine_tab/EngineTab.hpp @@ -2,10 +2,13 @@ #include "UCI.hpp" #include "ochess.hpp" +// Foreign event +wxDECLARE_EVENT(CLOSE_TAB_EVENT, wxCommandEvent); + class EngineTab : public EngineTabBF, public TabInfos { uciadapter::UCI *engine; std::string confGroup, enginePath; - + std::string engineName; void InitConfiguration(); public: @@ -15,4 +18,5 @@ public: void *GetGame() { return (NULL); } void *GetBase() { return (NULL); } void OnSave(wxCommandEvent &event); + void OnDelete(wxCommandEvent &event); }; \ No newline at end of file diff --git a/src/engine_tab/EngineTabBF.cpp b/src/engine_tab/EngineTabBF.cpp index 0e33939..90ba99d 100644 --- a/src/engine_tab/EngineTabBF.cpp +++ b/src/engine_tab/EngineTabBF.cpp @@ -53,6 +53,9 @@ EngineTabBF::EngineTabBF( wxWindow* parent, wxWindowID id, const wxPoint& pos, c save_button = new wxButton( this, ENGINE_SAVE_CONF_BUTTON, wxT("Save"), wxDefaultPosition, wxDefaultSize, 0 ); main_sizer->Add( save_button, 0, wxALL|wxEXPAND, 5 ); + delete_button = new wxButton( this, ENGINE_DELETE_CONF_BUTTON, wxT("Delete engine"), wxDefaultPosition, wxDefaultSize, 0 ); + main_sizer->Add( delete_button, 0, wxALL|wxEXPAND, 5 ); + this->SetSizer( main_sizer ); this->Layout(); diff --git a/src/engine_tab/EngineTabBF.h b/src/engine_tab/EngineTabBF.h index fc65a03..fe7dae1 100644 --- a/src/engine_tab/EngineTabBF.h +++ b/src/engine_tab/EngineTabBF.h @@ -29,6 +29,7 @@ /////////////////////////////////////////////////////////////////////////// #define ENGINE_SAVE_CONF_BUTTON 1000 +#define ENGINE_DELETE_CONF_BUTTON 1001 /////////////////////////////////////////////////////////////////////////////// /// Class EngineTabBF @@ -46,6 +47,7 @@ class EngineTabBF : public wxPanel wxStaticText* params_label; wxPropertyGrid* engine_parameters; wxButton* save_button; + wxButton* delete_button; public: diff --git a/src/game_tab/editor/EditorPanel.cpp b/src/game_tab/editor/EditorPanel.cpp index 641448e..3e6a7db 100644 --- a/src/game_tab/editor/EditorPanel.cpp +++ b/src/game_tab/editor/EditorPanel.cpp @@ -15,6 +15,17 @@ EditorPanel::EditorPanel(wxFrame *parent, Game *game) tags_list->InsertColumn(1, L"Value", wxLIST_FORMAT_LEFT, 500); tagTextCtrl->SetHint("Tag"); valueTextCtrl->SetHint("Value"); + CONFIG_OPEN(conf); + conf->SetPath("engines/"); + wxString engine_name; + long index; + if (conf->GetFirstGroup(engine_name, index)) { + do { + engine_list->Append(engine_name); + } while (conf->GetNextGroup(engine_name, index)); + } + + CONFIG_CLOSE(conf); RefreshTagsList(); // Bind events diff --git a/src/game_tab/editor/EditorPanelBF.cpp b/src/game_tab/editor/EditorPanelBF.cpp index f74ef64..8aac38e 100644 --- a/src/game_tab/editor/EditorPanelBF.cpp +++ b/src/game_tab/editor/EditorPanelBF.cpp @@ -82,7 +82,7 @@ EditorPanelBF::EditorPanelBF( wxWindow* parent, wxWindowID id, const wxPoint& po engine_list_label->Wrap( -1 ); engine_page_sizer->Add( engine_list_label, 0, wxALL, 5 ); - engine_list = new wxListCtrl( engine_page, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_ICON ); + engine_list = new wxListBox( engine_page, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, 0 ); engine_page_sizer->Add( engine_list, 1, wxALL|wxEXPAND, 5 ); analyze_game_button = new wxButton( engine_page, wxID_ANY, wxT("Analyze game"), wxDefaultPosition, wxDefaultSize, 0 ); diff --git a/src/game_tab/editor/EditorPanelBF.h b/src/game_tab/editor/EditorPanelBF.h index 092d750..c5b38d5 100644 --- a/src/game_tab/editor/EditorPanelBF.h +++ b/src/game_tab/editor/EditorPanelBF.h @@ -24,6 +24,7 @@ #include #include #include +#include #include /////////////////////////////////////////////////////////////////////////// @@ -55,7 +56,7 @@ class EditorPanelBF : public wxPanel wxButton* delete_button; wxPanel* engine_page; wxStaticText* engine_list_label; - wxListCtrl* engine_list; + wxListBox* engine_list; wxButton* analyze_game_button; wxButton* live_analysis_button; diff --git a/tools/wxframebuilder/EditorPanel.fbp b/tools/wxframebuilder/EditorPanel.fbp index 411938e..3960615 100644 --- a/tools/wxframebuilder/EditorPanel.fbp +++ b/tools/wxframebuilder/EditorPanel.fbp @@ -917,7 +917,7 @@ 5 wxALL|wxEXPAND 1 - + 1 1 1 @@ -931,6 +931,7 @@ 1 0 + 1 1 @@ -962,7 +963,7 @@ Resizable 1 - wxLC_ICON + ; ; forward_declare 0 diff --git a/tools/wxframebuilder/EngineTab.fbp b/tools/wxframebuilder/EngineTab.fbp index 0ee11d0..b37ff08 100644 --- a/tools/wxframebuilder/EngineTab.fbp +++ b/tools/wxframebuilder/EngineTab.fbp @@ -582,6 +582,79 @@ + + 5 + wxALL|wxEXPAND + 0 + + 1 + 1 + 1 + 1 + + + + + 0 + + + + + 1 + 0 + 1 + + 1 + + 0 + 0 + + Dock + 0 + Left + 1 + + 1 + + + 0 + 0 + ENGINE_DELETE_CONF_BUTTON + Delete engine + + 0 + + 0 + + + 0 + + 1 + delete_button + 1 + + + protected + 1 + + + + Resizable + 1 + + + ; ; forward_declare + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + +