Improve the binding between LiveEngineDialog and BoardCanvas

This commit is contained in:
Loic Guegan 2023-01-11 11:56:21 +01:00
parent 93c7cb3d24
commit d2f078adb5
6 changed files with 45 additions and 27 deletions

View file

@ -10,7 +10,7 @@
- [ ] Make PGNGameBase use GotoNextGame() instead of ParseNextGame() in the NextGame() method to improve performance
- [x] Clean and debug DragNDrop in BoardCanvas
- [ ] Disable the "Analyze entire game" button (Not Yet Implemented)
- [ ] Keep engine evaluation bar visible (and best move arrows) as long as the live engine dialog is open
- [x] Keep engine evaluation bar visible (and best move arrows) as long as the live engine dialog is open
## Additional Features
- [x] Add a live evaluation bar to the BoardCanvas

View file

@ -33,6 +33,9 @@ GameTab::GameTab(wxFrame *parent, std::shared_ptr<Game> game)
p->board_panel->SetEngineEvaluation(*eval);
free(eval);
});
Bind(LIVE_ANALYSIS_STATUS, [p=this](wxCommandEvent &event){
p->board_panel->SetLiveEngineState((bool)event.GetInt());
});
}
void GameTab::OnToolClick(wxCommandEvent &event){

View file

@ -2,7 +2,7 @@
#include <wx/clipbrd.h>
GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
: TabGameLeftPanel(parent), game(game), repeat(false) {
: TabGameLeftPanel(parent), game(game), repeat(false), is_engine_on(false) {
// Configure toolbar (note that toolbar events are processed into the GameTab class)
game_toolbar->AddTool(0, wxT("Save As"),
@ -86,20 +86,32 @@ void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
}
void GameTabLeftPanel::SetEngineEvaluation(EngineEvaluation eval){
engine_arrows.clear();
float scale=1;
unsigned char color=0;
for(auto const &arrow: eval.best_lines){
std::string src=arrow.substr(0,2);
std::string dst=arrow.substr(2,2);
engine_arrows.push_back({src,dst,wxColour(color,color,color),scale});
scale=std::max(0.1,scale-0.25);
color=std::min(255,color+70);
if(is_engine_on){
engine_arrows.clear();
float scale=1;
unsigned char color=0;
for(auto const &arrow: eval.best_lines){
std::string src=arrow.substr(0,2);
std::string dst=arrow.substr(2,2);
engine_arrows.push_back({src,dst,wxColour(color,color,color),scale});
scale=std::max(0.1,scale-0.25);
color=std::min(255,color+70);
}
eval_cp=eval.eval;
Notify(true);
}
eval_cp=eval.eval;
Notify(true);
}
void GameTabLeftPanel::SetLiveEngineState(bool isOn){
is_engine_on=isOn;
if(!is_engine_on){
engine_arrows.clear();
eval_cp=0;
Notify(true);
}
}
void GameTabLeftPanel::Notify(bool skip_animation) {
// Update fen and captures
std::string fen = game->GetFen();
@ -138,7 +150,7 @@ void GameTabLeftPanel::Notify(bool skip_animation) {
gs.mat_white=game->IsCheckmate(false);
gs.arrows=engine_arrows;
gs.promotion=promote_on;
gs.show_evalbar=engine_arrows.size()>0;
gs.show_evalbar=is_engine_on;
gs.eval=eval_cp/100;
if(m){
// There should be a valid src_hl or dst_hl ortherwise it explode:

View file

@ -18,6 +18,7 @@ class GameTabLeftPanel : public TabGameLeftPanel {
std::string promote_on;
std::string promotion_move;
float eval_cp;
bool is_engine_on;
public:
GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game);
@ -28,4 +29,5 @@ public:
void ApplyPreferences();
void SetSaveToolEnable(bool state){game_toolbar->EnableTool(0,state);};
void SetEngineEvaluation(EngineEvaluation eval);
void SetLiveEngineState(bool isOn);
};

View file

@ -1,11 +1,6 @@
#include "GameTabRightPanel.hpp"
wxDEFINE_EVENT(GOTO_MOVE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(DELETE_MOVE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(PROMOTE_MOVE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(SET_AS_MAINLINE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(PREVIOUS_MOVE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(NEXT_MOVE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(LIVE_ANALYSIS_STATUS, wxCommandEvent);
GameTabRightPanel::GameTabRightPanel(wxFrame *parent, std::shared_ptr<Game> game)
: TabGameRightPanel(parent), game(game), selected_item(-1),
@ -53,6 +48,12 @@ void GameTabRightPanel::OnLiveAnalysis(wxCommandEvent &event) {
if (live_engine == nullptr) {
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);
live_engine = new LiveEngineDialog(
this, engine_list->GetString(selection).ToStdString());
live_engine->SetFEN(game->GetFen());
@ -87,6 +88,12 @@ void GameTabRightPanel::OnTagDeselected(wxListEvent &event) {
}
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
live_engine = nullptr;
e.Skip();
}

View file

@ -7,16 +7,10 @@
#include <wx/listctrl.h>
#include <wx/notebook.h>
// Local events
wxDECLARE_EVENT(GOTO_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(DELETE_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(PROMOTE_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(SET_AS_MAINLINE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
// Foreign events
wxDECLARE_EVENT(GAME_CHANGE, wxCommandEvent);
wxDECLARE_EVENT(SHOW_ENGINE_EVALUATION, wxCommandEvent);
wxDECLARE_EVENT(LIVE_ANALYSIS_STATUS, wxCommandEvent);
class GameTabRightPanel : public TabGameRightPanel {
std::shared_ptr<Game> game;