diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 21e0a92..27eda12 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -122,7 +122,7 @@ void MainWindow::OnMenuItemClick(wxCommandEvent &event) { if (item->GetId() == id) { wxLogDebug("Selected %s", item->GetItemLabel()); EngineTab *et = new EngineTab((wxWindow *)notebook, - item->GetItemLabel().ToStdString()); + item->GetId()-100); AddPage(et,et); } } @@ -174,14 +174,15 @@ void MainWindow::OnRefreshEngineList(wxCommandEvent &event) { // Refresh items CONFIG_OPEN(conf); conf->SetPath("engines/"); - wxString engine_name; + wxString engine_id; long index; - if (conf->GetFirstGroup(engine_name, index)) { + if (conf->GetFirstGroup(engine_id, index)) { std::uint32_t id = 0; do { + wxString engine_name=conf->Read(engine_id+"/name"); manageMenu->Append(100 + id, engine_name, "Configure " + engine_name); id++; - } while (conf->GetNextGroup(engine_name, index)); + } while (conf->GetNextGroup(engine_id, index)); } CONFIG_CLOSE(conf); ApplyPreferences(); // Propagate informations to the tabs that require it diff --git a/src/engine_tab/EngineTab.cpp b/src/engine_tab/EngineTab.cpp index 1f82218..345a650 100644 --- a/src/engine_tab/EngineTab.cpp +++ b/src/engine_tab/EngineTab.cpp @@ -1,26 +1,22 @@ #include "EngineTab.hpp" EngineTab::EngineTab(wxWindow *parent, uciadapter::UCI *engine, - std::string engine_path_or_name) + std::string engine_path) : TabEngine(parent), TabInfos(TabInfos::ENGINE), - enginePath(engine_path_or_name), engine(engine) { + enginePath(engine_path), engine(engine) { + + // Init engine name and location SetLabel("New Engine"); + engine_location->SetValue(engine_path); - engine_location->SetValue(engine_path_or_name); + // Fetch engine id CONFIG_OPEN(conf); - // 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)); + conf->SetPath("engines/"); + engine_id=conf->GetNumberOfGroups(); CONFIG_CLOSE(conf); + confGroup = "engines/" + std::to_string(engine_id); + + // Init data InitConfiguration(); LoadConfiguration(); RefreshItemList(); @@ -30,15 +26,21 @@ EngineTab::EngineTab(wxWindow *parent, uciadapter::UCI *engine, Bind(wxEVT_PG_CHANGED, [p=this](wxPropertyGridEvent& event){p->is_dirty=true;}); } -EngineTab::EngineTab(wxWindow *parent, std::string name) +EngineTab::EngineTab(wxWindow *parent, std::uint32_t id) : TabEngine(parent), TabInfos(TabInfos::ENGINE), engine(nullptr) { - SetLabel(name); - engineName = name; - confGroup = "engines/" + engineName; - engine_name->SetValue(engineName); + // Init engine group + std::string id_str=std::to_string(id); + confGroup = "engines/" + std::to_string(id); + + // Fetch name and path CONFIG_OPEN(conf); + wxString name=conf->Read(confGroup + "/name"); + SetLabel(name); + engine_name->SetValue(name); engine_location->SetValue(conf->Read(confGroup + "/path")); CONFIG_CLOSE(conf); + + // Load existing configuration LoadConfiguration(); Bind(wxEVT_BUTTON, &EngineTab::OnSave, this, ENGINE_SAVE_CONF_BUTTON); @@ -101,20 +103,10 @@ void EngineTab::LoadConfiguration() { void EngineTab::OnSave(wxCommandEvent &event) { CONFIG_OPEN(conf2); - wxString new_engine_name = engine_name->GetValue(); - if (new_engine_name != engineName) { - conf2->SetPath("engines/"); - conf2->RenameGroup(engineName, new_engine_name); - engineName = new_engine_name; - confGroup = "engines/" + engineName; - conf2->SetPath(".."); - SetLabel(new_engine_name); - // First refresh tab title - wxCommandEvent refreshTitle(REFRESH_TAB_TITLE, GetId()); - refreshTitle.SetEventObject(this); - wxLogDebug("New engine name is %s",this->GetLabel()); - ProcessEvent(refreshTitle); - } + // Update engine name: + conf2->Write(confGroup + "/name", engine_name->GetValue()); + + // Update engine configuration: 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 711c3c7..975ddf6 100644 --- a/src/engine_tab/EngineTab.hpp +++ b/src/engine_tab/EngineTab.hpp @@ -9,14 +9,14 @@ wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); class EngineTab : public TabEngine, public TabInfos { uciadapter::UCI *engine; std::string confGroup, enginePath; - std::string engineName; + std::uint32_t engine_id; void InitConfiguration(); void LoadConfiguration(); void RefreshItemList(); public: EngineTab(wxWindow *parent, uciadapter::UCI *engine, - std::string engine_path_or_name); - EngineTab(wxWindow *parent, std::string name); + std::string engine_path); + EngineTab(wxWindow *parent, std::uint32_t id); ~EngineTab(); void ApplyPreferences() {} std::shared_ptr GetGame() { return nullptr; } diff --git a/src/game_tab/right_panel/GameTabRightPanel.cpp b/src/game_tab/right_panel/GameTabRightPanel.cpp index 5d933b8..ff9576d 100644 --- a/src/game_tab/right_panel/GameTabRightPanel.cpp +++ b/src/game_tab/right_panel/GameTabRightPanel.cpp @@ -56,7 +56,7 @@ void GameTabRightPanel::OnLiveAnalysis(wxCommandEvent &event) { ProcessEvent(notifyEvent); live_engine = new LiveEngineDialog( - this, engine_list->GetString(selection).ToStdString()); + this, selection); live_engine->SetFEN(game->GetFen()); live_engine->Show(); live_engine->Bind(wxEVT_CLOSE_WINDOW, @@ -168,12 +168,12 @@ void GameTabRightPanel::ApplyPreferences() { engine_list->Clear(); CONFIG_OPEN(conf); conf->SetPath("engines/"); - wxString engine_name; + wxString engine_id; long index; - if (conf->GetFirstGroup(engine_name, index)) { + if (conf->GetFirstGroup(engine_id, index)) { do { - engine_list->Append(engine_name); - } while (conf->GetNextGroup(engine_name, index)); + engine_list->Append(conf->Read(engine_id+"/name")); + } while (conf->GetNextGroup(engine_id, index)); } CONFIG_CLOSE(conf); editor_canvas->ApplyPreferences(); diff --git a/src/game_tab/right_panel/LiveEngineDialog.cpp b/src/game_tab/right_panel/LiveEngineDialog.cpp index 64d75e3..d0b1896 100644 --- a/src/game_tab/right_panel/LiveEngineDialog.cpp +++ b/src/game_tab/right_panel/LiveEngineDialog.cpp @@ -1,12 +1,19 @@ #include "LiveEngineDialog.hpp" -LiveEngineDialog::LiveEngineDialog(wxWindow *parent, std::string engine_name) - : DialogLiveEngine(parent), engine_name(engine_name), interval(1000), +LiveEngineDialog::LiveEngineDialog(wxWindow *parent, std::uint32_t engine_id) + : DialogLiveEngine(parent), interval(1000), engine(nullptr) { lines_list->InsertColumn(0, "#", wxLIST_FORMAT_LEFT, 50); lines_list->InsertColumn(1, "CP", wxLIST_FORMAT_LEFT, 70); lines_list->InsertColumn(2, "Line", wxLIST_FORMAT_LEFT, 300); + + // Load engine name + confGroup="engines/"+std::to_string(engine_id); + CONFIG_OPEN(conf); + engine_name=conf->Read(confGroup+"/name"); + CONFIG_CLOSE(conf); current_engine->SetLabel(engine_name); + InitEngine(); Bind(wxEVT_BUTTON, &LiveEngineDialog::TogglePauseEngine, this, LIVE_ENGINE_PAUSE_BUTTON); @@ -25,11 +32,11 @@ void LiveEngineDialog::InitEngine() { wxLogDebug("Start engine: %s", engine_name); CONFIG_OPEN(conf); engine = new uciadapter::UCI( - conf->Read("engines/" + engine_name + "/path").ToStdString()); + conf->Read(confGroup + "/path").ToStdString()); engine->ucinewgame(); long index; - std::string optsPath = "engines/" + engine_name + "/options"; + std::string optsPath = confGroup + "/options"; conf->SetPath(optsPath); wxString opt_name; if (conf->GetFirstGroup(opt_name, index)) { diff --git a/src/game_tab/right_panel/LiveEngineDialog.hpp b/src/game_tab/right_panel/LiveEngineDialog.hpp index b7be674..2ba39c1 100644 --- a/src/game_tab/right_panel/LiveEngineDialog.hpp +++ b/src/game_tab/right_panel/LiveEngineDialog.hpp @@ -15,12 +15,14 @@ typedef struct EngineEvaluation { class LiveEngineDialog : public DialogLiveEngine { uciadapter::UCI *engine; std::string engine_name; + std::string confGroup; + wxTimer timer; /// @brief The following time interval definitely need to be configure in the user settings (set to 1s for now) std::uint32_t interval; public: - LiveEngineDialog(wxWindow *parent, std::string engine_name); + LiveEngineDialog(wxWindow *parent, std::uint32_t engine_id); ~LiveEngineDialog(); void InitEngine(); void TogglePauseEngine(wxCommandEvent &event);