ochess/src/game_tab/left_panel/GameTabLeftPanel.cpp

177 lines
5.7 KiB
C++
Raw Normal View History

2022-02-28 13:44:27 +01:00
#include "GameTabLeftPanel.hpp"
2022-02-23 18:11:55 +01:00
#include <wx/clipbrd.h>
2022-02-28 20:16:57 +01:00
GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
: TabGameLeftPanel(parent), game(game), repeat(false), is_engine_on(false) {
2022-02-23 18:11:55 +01:00
2023-01-01 12:15:08 +01:00
// Configure toolbar (note that toolbar events are processed into the GameTab class)
2022-12-30 15:58:13 +01:00
game_toolbar->AddTool(0, wxT("Save As"),
2022-12-30 13:17:06 +01:00
wxArtProvider::GetBitmap(wxART_FILE_SAVE, wxART_TOOLBAR));
2022-12-30 16:24:44 +01:00
game_toolbar->AddTool(1, wxT("Duplicate Game"),
wxArtProvider::GetBitmap(wxART_COPY, wxART_TOOLBAR));
2022-12-30 13:17:06 +01:00
2022-02-28 14:02:02 +01:00
// Add board
2022-02-23 18:11:55 +01:00
board_canvas = new BoardCanvas((wxFrame *)this);
2022-12-30 13:17:06 +01:00
main_sizer->Insert(1, board_canvas, 1, wxEXPAND);
2022-02-23 18:11:55 +01:00
2022-02-28 14:02:02 +01:00
// Configure buttons
swap_button->SetBitmapLabel(LoadPNG("swap"));
zoomin_button->SetBitmapLabel(LoadPNG("zoomin"));
zoomout_button->SetBitmapLabel(LoadPNG("zoomout"));
2022-02-23 18:11:55 +01:00
2022-02-28 15:27:51 +01:00
// Configure FEN field
2022-12-27 17:42:30 +01:00
fen_text_field->SetFont(wxFont(*wxNORMAL_FONT).Bold().Larger());
2023-01-01 20:21:23 +01:00
last_move=game->GetCurrentMove();
2023-01-01 14:35:20 +01:00
2023-01-01 12:15:08 +01:00
// Bind events:
2022-02-28 13:44:27 +01:00
Bind(PLAY_MOVE_EVENT, &GameTabLeftPanel::OnPlay, this, wxID_ANY);
2023-01-01 12:15:08 +01:00
Bind(wxEVT_BUTTON, [bc=board_canvas](wxCommandEvent &event){bc->Zoom(10);}, ZOOM_IN_BTN);
Bind(wxEVT_BUTTON, [bc=board_canvas](wxCommandEvent &event){bc->Zoom(-10);}, ZOOM_OUT_BTN);
Bind(wxEVT_BUTTON, [bc=board_canvas](wxCommandEvent &event){bc->Swap();}, SWAP_BTN);
Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){p->repeat=false;});
Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){
if(e.GetKeyCode() == WXK_RIGHT){
p->game->Next();
2023-01-01 18:18:27 +01:00
p->Notify();
2023-01-01 12:15:08 +01:00
p->repeat=true;
} else if(e.GetKeyCode() == WXK_LEFT){
p->game->Previous();
2023-01-01 18:18:27 +01:00
p->Notify();
2023-01-01 12:15:08 +01:00
p->repeat=true;
}
// Notify other classes
wxCommandEvent event(GAME_CHANGE, p->GetId());
event.SetEventObject(p);
p->ProcessEvent(event);
});
Bind(wxEVT_MOUSEWHEEL, [p=this](wxMouseEvent& e){
if(e.GetWheelRotation()<0){
p->game->Next();
2023-01-01 18:18:27 +01:00
p->Notify();
2023-01-01 12:15:08 +01:00
}else {
p->game->Previous();
2023-01-01 18:18:27 +01:00
p->Notify();
2023-01-01 12:15:08 +01:00
}
// Notify other classes
wxCommandEvent event(GAME_CHANGE, p->GetId());
event.SetEventObject(p);
p->ProcessEvent(event);
});
2022-02-23 18:11:55 +01:00
}
2022-02-28 13:44:27 +01:00
void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
2023-01-11 10:46:14 +01:00
engine_arrows.clear(); // Remove displayed engine arrows
2023-01-10 16:01:26 +01:00
std::string move=event.GetString().ToStdString();
int size=move.size();
char promote=(char)event.GetInt();
// First check if it is a promotion move (to prompt the user for a piece)
if(size>0 && game->IsPromotionMove(move)){
promote_on=move.substr(2,2); // Will trigger the piece prompt on next Notify()
promotion_move=move; // Save the move that should be played for the promotion
} else {
if(size==0){ // It is a promotion move?
move=promotion_move; // Play the save promotion move
promote_on.clear(); // Clear user prompte (cf. Notify())
promotion_move.clear();
}
if(game->Play(move,promote)){
// Notify other classes
wxCommandEvent event(GAME_CHANGE, GetId());
event.SetEventObject(this);
ProcessEvent(event);
}
2022-02-23 18:11:55 +01:00
}
2023-01-02 08:15:51 +01:00
Notify(true); // Redraw event is move failed! Otherwise piece not resets to it initial position after dragging
2022-02-23 18:11:55 +01:00
}
2023-01-11 10:46:14 +01:00
void GameTabLeftPanel::SetEngineEvaluation(EngineEvaluation eval){
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);
2023-01-09 18:38:19 +01:00
}
}
void GameTabLeftPanel::SetLiveEngineState(bool isOn){
is_engine_on=isOn;
if(!is_engine_on){
engine_arrows.clear();
eval_cp=0;
Notify(true);
}
}
2023-01-01 18:18:27 +01:00
void GameTabLeftPanel::Notify(bool skip_animation) {
2023-01-01 12:15:08 +01:00
// Update fen and captures
2022-02-23 18:11:55 +01:00
std::string fen = game->GetFen();
std::map<char, std::uint8_t> captures;
2023-01-01 18:18:27 +01:00
bool animate=false;
2022-02-23 18:11:55 +01:00
HalfMove *m = game->GetCurrentMove();
2023-01-01 14:35:20 +01:00
std::string src,dst;
2023-01-01 20:21:23 +01:00
// Update capture and check if we should to animations during moves change:
2023-01-01 19:05:15 +01:00
if (m){
2022-02-23 18:11:55 +01:00
captures = m->GetLineCaptures();
if(m->HasParent(last_move) && !m->IsVariation()){
m->GetAbsoluteMove(src,dst);
animate=true;
}else if(m->HasChild(last_move) && !last_move->IsVariation()){ // call to IsVariation is safe because of HasChild() before! cf below
// Accessing last_move here is safe since it is still
// in the tree of moves (since HasChild found it so not deleted)
last_move->GetAbsoluteMove(dst,src);
animate=true;
2023-01-01 20:21:23 +01:00
}
} else if(game->GetNextMove()){ // First move animation
HalfMove *next=game->GetNextMove();
if(next==last_move){
2023-01-02 10:12:20 +01:00
game->GetNextMove()->GetAbsoluteMove(dst,src);
2023-01-01 20:21:23 +01:00
animate=true;
}
2023-01-01 19:05:15 +01:00
}
2023-01-02 10:12:20 +01:00
2023-01-01 12:15:08 +01:00
// Update board canvas:
2023-01-02 11:36:13 +01:00
GameState gs;
gs.board=chessarbiter::FENParser::Parse(fen).board;
gs.is_black_turn=game->IsBlackToPlay();
gs.captures=captures;
gs.white=game->GetTag("White");
gs.black=game->GetTag("Black");
gs.mat_black=game->IsCheckmate(true);
gs.mat_white=game->IsCheckmate(false);
gs.arrows=engine_arrows;
gs.promotion=promote_on;
gs.show_evalbar=is_engine_on;
2023-01-11 10:46:14 +01:00
gs.eval=eval_cp/100;
if(m){
// There should be a valid src_hl or dst_hl ortherwise it explode:
std::string src_hl, dst_hl;
m->GetAbsoluteMove(src_hl,dst_hl);
if(src_hl.size()>0){ // Just in case
gs.squares_hl.push_back({src_hl,wxColour(255,190,190)});
gs.squares_hl.push_back({dst_hl,wxColour(255,100,100)});
}
}
2023-01-01 19:05:15 +01:00
if(skip_animation || !animate){
2023-01-02 11:36:13 +01:00
board_canvas->SetupBoard(gs);
2023-01-01 18:18:27 +01:00
}
2022-12-28 12:40:16 +01:00
else{
2023-01-02 11:36:13 +01:00
board_canvas->Animate(gs, src,dst,repeat);
2022-12-28 12:40:16 +01:00
}
2023-01-01 20:21:23 +01:00
// Update last move
last_move=m;
2023-01-01 12:15:08 +01:00
// Update fen field:
2022-02-28 15:27:51 +01:00
fen_text_field->SetValue(game->GetFen());
2022-02-23 18:11:55 +01:00
}
2022-02-28 14:02:02 +01:00
void GameTabLeftPanel::ApplyPreferences() { board_canvas->ApplyPreferences(); }