ochess/src/game_tab/left_panel/GameTabLeftPanel.cpp

120 lines
4 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)
2022-12-29 12:28:51 +01:00
: TabGameLeftPanel(parent), game(game), repeat(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-12-29 10:08:22 +01:00
2022-02-28 13:44:27 +01:00
void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
2022-02-23 18:11:55 +01:00
wxLogDebug("Game tab received PLAY_MOVE_EVENT");
if (game->Play(event.GetString().ToStdString())) {
2023-01-01 12:15:08 +01:00
// 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-01 18:18:27 +01:00
void GameTabLeftPanel::Notify(bool skip_animation) {
2023-01-01 14:12:57 +01:00
wxLogDebug("Called!");
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();
2023-01-01 20:21:23 +01:00
if(m->HasParent(last_move)){
UNPACK_ABSOLUTE_MOVE(m->GetAbsoluteMove(),src,dst);
animate=true;
}else if(m->HasChild(last_move)){
// Accessing last_move here is safe since it is still
// in the tree of moves (since HasChild found it so not deleted)
UNPACK_ABSOLUTE_MOVE(last_move->GetAbsoluteMove(),dst,src);
animate=true;
}
} else if(game->GetNextMove()){ // First move animation
HalfMove *next=game->GetNextMove();
if(next==last_move){
UNPACK_ABSOLUTE_MOVE(game->GetNextMove()->GetAbsoluteMove(),dst,src);
animate=true;
}
2023-01-01 19:05:15 +01:00
}
2023-01-01 14:12:57 +01:00
2023-01-01 12:15:08 +01:00
// Update board canvas:
2023-01-01 19:05:15 +01:00
if(skip_animation || !animate){
2022-12-28 12:40:16 +01:00
board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board,
2022-12-30 13:17:06 +01:00
game->IsBlackToPlay(), captures,
game->GetTag("White"),game->GetTag("Black"));
2023-01-01 18:18:27 +01:00
}
2022-12-28 12:40:16 +01:00
else{
2023-01-01 14:35:20 +01:00
board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board,
2022-12-29 10:08:22 +01:00
game->IsBlackToPlay(), captures,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(); }