ochess/src/game_tab/right_panel/GameTabRightPanel.cpp

239 lines
7.1 KiB
C++
Raw Normal View History

2022-02-28 13:44:27 +01:00
#include "GameTabRightPanel.hpp"
2022-02-23 18:11:55 +01:00
wxDEFINE_EVENT(LIVE_ANALYSIS_STATUS, wxCommandEvent);
2022-02-23 18:11:55 +01:00
2022-02-28 20:16:57 +01:00
GameTabRightPanel::GameTabRightPanel(wxFrame *parent, std::shared_ptr<Game> game)
2022-02-28 15:52:19 +01:00
: TabGameRightPanel(parent), game(game), selected_item(-1),
2022-12-31 20:45:03 +01:00
live_engine(nullptr) {
2023-01-01 14:12:57 +01:00
editor_canvas = new EditorCanvas((wxFrame *)editor_page,game);
2022-02-26 15:37:03 +01:00
editor_canvas_sizer->Add(editor_canvas, 1, wxEXPAND);
2022-02-23 18:47:18 +01:00
tags_list->InsertColumn(0, L"Name", wxLIST_FORMAT_LEFT, 200);
2022-02-23 18:11:55 +01:00
tags_list->InsertColumn(1, L"Value", wxLIST_FORMAT_LEFT, 500);
2022-02-26 15:37:03 +01:00
tagTextCtrl->SetHint("Tag");
valueTextCtrl->SetHint("Value");
2023-01-16 14:55:48 +01:00
opening_label->SetHint("Current opening");
2022-02-23 18:11:55 +01:00
RefreshTagsList();
// Bind events
2022-02-28 13:44:27 +01:00
this->Bind(wxEVT_TEXT, &GameTabRightPanel::OnCommentChange, this,
2022-02-23 18:11:55 +01:00
COMMENT_INPUT_BOX);
2022-02-28 15:52:19 +01:00
this->Bind(wxEVT_LIST_ITEM_SELECTED, &GameTabRightPanel::OnTagSelected, this,
2022-02-23 18:11:55 +01:00
wxID_ANY);
2022-02-28 15:52:19 +01:00
this->Bind(wxEVT_LIST_ITEM_DESELECTED, &GameTabRightPanel::OnTagDeselected,
this, wxID_ANY);
2022-02-28 13:44:27 +01:00
this->Bind(wxEVT_BUTTON, &GameTabRightPanel::OnApply, this, UPDATE_BTN);
this->Bind(wxEVT_BUTTON, &GameTabRightPanel::OnDelete, this, DELETE_BTN);
this->Bind(wxEVT_BUTTON, &GameTabRightPanel::OnLiveAnalysis, this,
2022-02-27 19:55:30 +01:00
LIVE_ANALYSIS_GAME_BUTTON);
2022-12-30 10:02:56 +01:00
nag_panel->Bind(wxEVT_BUTTON, [p=this](wxCommandEvent &e){
HalfMove *m = p->game->GetCurrentMove();
2022-12-31 20:45:03 +01:00
if (m != nullptr) {
2023-01-19 16:33:44 +01:00
m->SetNAG(p->editor_canvas->GetNAGId(((wxButton*)e.GetEventObject())->GetLabel().ToStdString()));
2022-12-30 10:02:56 +01:00
p->editor_canvas->Refresh();
}
});
2022-02-27 19:55:30 +01:00
2022-12-31 13:59:55 +01:00
// Propagate key events of the game editor
editor_page->Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
editor_page->Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
notebook->Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
notebook->Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
ApplyPreferences();
2023-01-16 14:55:48 +01:00
analyze_game_button->Disable();
2022-02-23 18:11:55 +01:00
}
2022-02-28 13:44:27 +01:00
void GameTabRightPanel::OnLiveAnalysis(wxCommandEvent &event) {
2022-12-31 20:45:03 +01:00
if (live_engine == nullptr) {
2022-02-28 15:52:19 +01:00
int selection = engine_list->GetSelection();
if (selection != wxNOT_FOUND) {
// Notify about the state of the LiveEngine
wxCommandEvent notifyEvent(LIVE_ANALYSIS_STATUS,GetId());
notifyEvent.SetEventObject(this);
notifyEvent.SetInt(1);
ProcessEvent(notifyEvent);
2022-02-28 15:52:19 +01:00
live_engine = new LiveEngineDialog(
2023-01-31 10:26:40 +01:00
this, selection);
2022-02-28 15:52:19 +01:00
live_engine->SetFEN(game->GetFen());
live_engine->Show();
live_engine->Bind(wxEVT_CLOSE_WINDOW,
&GameTabRightPanel::OnLiveEngineClose, this,
ID_LIVE_ENGINE_DIALOG);
live_engine->Bind(SHOW_ENGINE_EVALUATION, [p=this](wxCommandEvent &e){
wxCommandEvent notifyEvent(SHOW_ENGINE_EVALUATION,p->GetId());
notifyEvent.SetEventObject(p);
notifyEvent.SetClientData(e.GetClientData());
p->ProcessEvent(notifyEvent);
});
2022-02-28 15:52:19 +01:00
}
2022-02-27 19:55:30 +01:00
}
}
2022-02-28 13:44:27 +01:00
void GameTabRightPanel::OnTagSelected(wxListEvent &event) {
2022-02-23 18:11:55 +01:00
wxListItem item = event.GetItem();
std::string key = item.GetText().ToStdString();
tagTextCtrl->ChangeValue(key);
item.SetColumn(1);
tags_list->GetItem(item);
valueTextCtrl->ChangeValue(item.GetText().ToStdString());
selected_item = item.GetId();
delete_button->Enable(true);
}
2022-02-28 13:44:27 +01:00
void GameTabRightPanel::OnTagDeselected(wxListEvent &event) {
2022-02-23 18:11:55 +01:00
selected_item = -1;
delete_button->Enable(false);
}
2022-02-28 15:52:19 +01:00
void GameTabRightPanel::OnLiveEngineClose(wxCloseEvent &e) {
// Notify about the state of the LiveEngine
wxCommandEvent notifyEvent(LIVE_ANALYSIS_STATUS,GetId());
notifyEvent.SetEventObject(this);
notifyEvent.SetInt(0);
ProcessEvent(notifyEvent);
// Refresh pointer
2022-12-31 20:45:03 +01:00
live_engine = nullptr;
2022-02-28 15:52:19 +01:00
e.Skip();
}
2022-02-28 13:44:27 +01:00
void GameTabRightPanel::OnCommentChange(wxCommandEvent &event) {
wxLogDebug("GameTabRightPanel: comment input change");
2022-02-23 18:11:55 +01:00
HalfMove *m = game->GetCurrentMove();
2022-12-31 20:45:03 +01:00
if (m != nullptr) {
2023-01-19 16:33:44 +01:00
m->SetComment(event.GetString().Trim().ToStdString());
2023-01-02 11:58:40 +01:00
// Remove newlines:
2023-01-19 16:33:44 +01:00
for(char &c:m->GetComment()){
2023-01-02 11:58:40 +01:00
if(c=='\n')
c=' ';
}
2022-02-23 18:11:55 +01:00
}
editor_canvas->Refresh();
}
2022-02-28 13:44:27 +01:00
void GameTabRightPanel::OnApply(wxCommandEvent &event) {
2022-02-23 18:11:55 +01:00
std::string key = tagTextCtrl->GetValue().ToStdString();
if (key == "FEN") {
SHOW_DIALOG_ERROR("Editing the FEN tag is forbidden");
return;
}
if (key.size() > 0) {
std::string value = valueTextCtrl->GetValue().ToStdString();
game->SetTag(key, value);
RefreshTagsList();
2023-01-01 12:15:08 +01:00
wxCommandEvent event(GAME_CHANGE, GetId());
2022-02-23 18:11:55 +01:00
event.SetEventObject(this);
ProcessEvent(event);
}
}
2022-02-28 13:44:27 +01:00
void GameTabRightPanel::OnDelete(wxCommandEvent &event) {
2022-02-23 18:11:55 +01:00
if (selected_item >= 0) {
wxListItem item;
item.SetColumn(0);
item.SetId(selected_item);
tags_list->GetItem(item);
std::string key = item.GetText().ToStdString();
if (key != "FEN") {
game->DeleteTag(key);
selected_item = -1;
RefreshTagsList();
} else {
SHOW_DIALOG_ERROR("Deleting the FEN tag is forbidden.");
}
}
}
2022-02-28 13:44:27 +01:00
void GameTabRightPanel::Notify() {
2022-02-23 18:11:55 +01:00
HalfMove *m = game->GetCurrentMove();
2022-12-31 20:45:03 +01:00
if (m != nullptr) {
2022-02-23 18:11:55 +01:00
comment_input->ChangeValue(
2023-01-19 16:33:44 +01:00
m->GetComment()); // ChangeValue do not raise events
2022-02-23 18:11:55 +01:00
}
editor_canvas->SetMoves(game->GetMoves(), m);
2022-02-28 15:52:19 +01:00
// Put it here for now:
2022-12-31 20:45:03 +01:00
if (live_engine != nullptr) {
2022-02-28 15:52:19 +01:00
live_engine->SetFEN(game->GetFen());
}
2023-01-16 14:55:48 +01:00
// Update opening name
std::string opening,eco;
game->GetOpening(opening,eco);
if(eco.size()>0)
opening_label->SetValue(eco+": "+opening);
2022-02-23 18:11:55 +01:00
}
2022-02-28 13:44:27 +01:00
void GameTabRightPanel::ApplyPreferences() {
engine_list->Clear();
CONFIG_OPEN(conf);
conf->SetPath("engines/");
2023-01-31 10:26:40 +01:00
wxString engine_id;
long index;
2023-01-31 10:26:40 +01:00
if (conf->GetFirstGroup(engine_id, index)) {
do {
2023-01-31 10:26:40 +01:00
engine_list->Append(conf->Read(engine_id+"/name"));
} while (conf->GetNextGroup(engine_id, index));
}
CONFIG_CLOSE(conf);
editor_canvas->ApplyPreferences();
}
2022-02-28 13:44:27 +01:00
void GameTabRightPanel::RefreshTagsList() {
2022-02-23 18:11:55 +01:00
tags_list->DeleteAllItems();
for (std::string s : game->ListTags()) {
long index = tags_list->InsertItem(0, s);
tags_list->SetItem(index, 1, game->GetTag(s));
if (s == "FEN") {
tags_list->SetItemBackgroundColour(index, wxColour(200, 200, 200));
}
}
}
2023-01-19 16:33:44 +01:00
std::uint8_t GameTabRightPanel::GetNagFromStr(std::string str){
2022-12-30 10:02:56 +01:00
// TODO: Bind more NAG!
if(str=="!")
2023-01-19 16:33:44 +01:00
return 1;
2022-12-30 13:35:01 +01:00
else if(str=="?")
2023-01-19 16:33:44 +01:00
return 2;
2022-12-30 13:35:01 +01:00
else if(str=="!!")
2023-01-19 16:33:44 +01:00
return 3;
2022-12-30 13:35:01 +01:00
else if(str=="??")
2023-01-19 16:33:44 +01:00
return 4;
2022-12-30 13:35:01 +01:00
else if(str=="!?")
2023-01-19 16:33:44 +01:00
return 5;
2022-12-30 13:35:01 +01:00
else if(str=="?!")
2023-01-19 16:33:44 +01:00
return 6;
2022-12-30 13:35:01 +01:00
else if(str=="=")
2023-01-19 16:33:44 +01:00
return 10;
2022-12-30 13:35:01 +01:00
else if(str=="")
2023-01-19 16:33:44 +01:00
return 13;
2022-12-30 13:35:01 +01:00
else if(str=="")
2023-01-19 16:33:44 +01:00
return 14;
2022-12-30 13:35:01 +01:00
else if(str=="")
2023-01-19 16:33:44 +01:00
return 15;
2022-12-30 13:35:01 +01:00
else if(str=="±")
2023-01-19 16:33:44 +01:00
return 16;
2022-12-30 13:35:01 +01:00
else if(str=="")
2023-01-19 16:33:44 +01:00
return 17;
2022-12-30 13:35:01 +01:00
else if(str=="+-")
2023-01-19 16:33:44 +01:00
return 18;
2022-12-30 13:35:01 +01:00
else if(str=="-+")
2023-01-19 16:33:44 +01:00
return 19;
2022-12-30 13:35:01 +01:00
else if(str=="")
2023-01-19 16:33:44 +01:00
return 22;
2022-12-30 13:35:01 +01:00
else if(str=="")
2023-01-19 16:33:44 +01:00
return 26;
2022-12-30 13:35:01 +01:00
else if(str=="")
2023-01-19 16:33:44 +01:00
return 32;
2022-12-30 13:35:01 +01:00
else if(str=="")
2023-01-19 16:33:44 +01:00
return 36;
2022-12-30 13:35:01 +01:00
else if(str=="")
2023-01-19 16:33:44 +01:00
return 40;
2022-12-30 13:35:01 +01:00
else if(str=="")
2023-01-19 16:33:44 +01:00
return 44;
2022-12-30 13:35:01 +01:00
else if(str=="")
2023-01-19 16:33:44 +01:00
return 138;
return 0;
2022-12-30 13:35:01 +01:00
}