From 5607057ac33ce3b6933697134b20d7ef0b1a43be Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 12:15:08 +0100 Subject: [PATCH] Debug and clean the game tab code --- src/game_tab/GameTab.cpp | 48 +++------ src/game_tab/GameTab.hpp | 2 +- src/game_tab/left_panel/GameTabLeftPanel.cpp | 101 ++++++++---------- src/game_tab/left_panel/GameTabLeftPanel.hpp | 7 -- src/game_tab/left_panel/board/BoardCanvas.cpp | 3 +- src/game_tab/left_panel/board/BoardCanvas.hpp | 4 - .../right_panel/GameTabRightPanel.cpp | 15 ++- .../right_panel/editor/EditorCanvas.cpp | 23 ++-- .../right_panel/editor/EditorCanvas.hpp | 7 ++ 9 files changed, 89 insertions(+), 121 deletions(-) diff --git a/src/game_tab/GameTab.cpp b/src/game_tab/GameTab.cpp index 743346b..20196f3 100644 --- a/src/game_tab/GameTab.cpp +++ b/src/game_tab/GameTab.cpp @@ -20,40 +20,12 @@ GameTab::GameTab(wxFrame *parent, std::shared_ptr game) SetSizerAndFit(topSizer); // Refresh panels - wxCommandEvent event(REFRESH_TAB_TITLE, GetId()); - event.SetEventObject(this); - OnRefreshTabTitle(event); + RefreshTabTitle(); board_panel->Notify(false, false); editor_panel->Notify(); board_panel->Bind(wxEVT_TOOL,&GameTab::OnToolClick,this); - Bind(REFRESH_TAB_TITLE, &GameTab::OnRefreshTabTitle, this, wxID_ANY); Bind(GAME_CHANGE, &GameTab::OnGameChange, this, wxID_ANY); - splitter->Bind(wxEVT_KEY_DOWN, [p=this,bp=board_panel,ed=editor_panel](wxKeyEvent &e){ - if(e.GetKeyCode() == WXK_RIGHT){ - bp->NextMove(true); - } else if(e.GetKeyCode() == WXK_LEFT){ - bp->PreviousMove(true); - } - ed->Notify(); - }); - splitter->Bind(wxEVT_KEY_UP, [p=this,bp=board_panel,ed=editor_panel](wxKeyEvent &e){ - if(e.GetKeyCode() == WXK_RIGHT){ - bp->NextMove(false); - } else if(e.GetKeyCode() == WXK_LEFT){ - bp->PreviousMove(false); - } - ed->Notify(); - }); - Bind(wxEVT_MOUSEWHEEL, [p=this,bp=board_panel,ed=editor_panel](wxMouseEvent& event){ - if(event.GetWheelRotation()<0){ - bp->NextMove(true); - }else { - bp->PreviousMove(true); - } - ed->Notify(); - }); - } void GameTab::OnToolClick(wxCommandEvent &event){ @@ -76,11 +48,19 @@ void GameTab::OnToolClick(wxCommandEvent &event){ } void GameTab::OnGameChange(wxCommandEvent &event) { - board_panel->Notify(false,false); - editor_panel->Notify(); + if(event.GetEventObject() == board_panel) + editor_panel->Notify(); + else if(event.GetEventObject() == editor_panel){ + board_panel->Notify(); + RefreshTabTitle(); + } + else { + editor_panel->Notify(); + board_panel->Notify(); + } } -void GameTab::OnRefreshTabTitle(wxCommandEvent &event) { +void GameTab::RefreshTabTitle() { std::string white = game->GetTag("White"); std::string black = game->GetTag("Black"); if (white.size() == 0 && black.size() == 0) { @@ -88,8 +68,10 @@ void GameTab::OnRefreshTabTitle(wxCommandEvent &event) { } else { SetLabel(white + "-" + black); } + // Use this way to notify the MainFrame for the tab title: + wxCommandEvent event(REFRESH_TAB_TITLE,GetId()); event.SetEventObject(this); - event.Skip(); + ProcessEvent(event); } void GameTab::ApplyPreferences() { diff --git a/src/game_tab/GameTab.hpp b/src/game_tab/GameTab.hpp index e5cc9f9..d2f2390 100644 --- a/src/game_tab/GameTab.hpp +++ b/src/game_tab/GameTab.hpp @@ -20,7 +20,7 @@ class GameTab : public wxPanel, public TabInfos { std::string related_file; void RefreshLabel(); - void OnRefreshTabTitle(wxCommandEvent &event); + void RefreshTabTitle(); void OnGameChange(wxCommandEvent &event); public: diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp index 70a09cd..50919c0 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.cpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp @@ -4,7 +4,7 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr game) : TabGameLeftPanel(parent), game(game), repeat(false) { - // Configure toolbal + // Configure toolbar (note that toolbar events are processed into the GameTab class) game_toolbar->AddTool(0, wxT("Save As"), wxArtProvider::GetBitmap(wxART_FILE_SAVE, wxART_TOOLBAR)); game_toolbar->AddTool(1, wxT("Duplicate Game"), @@ -22,70 +22,57 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr game) // Configure FEN field fen_text_field->SetFont(wxFont(*wxNORMAL_FONT).Bold().Larger()); + // Bind events: Bind(PLAY_MOVE_EVENT, &GameTabLeftPanel::OnPlay, this, wxID_ANY); - Bind(wxEVT_BUTTON, &GameTabLeftPanel::OnSwap, this, SWAP_BTN); - Bind(wxEVT_BUTTON, &GameTabLeftPanel::OnZoomIn, this, ZOOM_IN_BTN); - Bind(wxEVT_BUTTON, &GameTabLeftPanel::OnZoomOut, this, ZOOM_OUT_BTN); - 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();}); + 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(); + p->Notify(true,false); + p->repeat=true; + } else if(e.GetKeyCode() == WXK_LEFT){ + p->game->Previous(); + p->Notify(true,true); + 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(); + p->Notify(true,false); + }else { + p->game->Previous(); + p->Notify(true,true); + } + // Notify other classes + wxCommandEvent event(GAME_CHANGE, p->GetId()); + event.SetEventObject(p); + p->ProcessEvent(event); + }); } - -void GameTabLeftPanel::PreviousMove(bool isKeyDown) { - if(isKeyDown){ - game->Previous(); - Notify(true,true); - repeat=true; - } else { - repeat=false; - } -} - -void GameTabLeftPanel::NextMove(bool isKeyDown) { - if(isKeyDown){ - game->Next(); - Notify(true,false); - repeat=true; - } - else{ - repeat=false; - } -} - -void GameTabLeftPanel::OnZoomIn(wxCommandEvent &event) { - wxLogDebug("Clicked on zoom in"); - board_canvas->Zoom(10); -} - -void GameTabLeftPanel::OnZoomOut(wxCommandEvent &event) { - wxLogDebug("Clicked on zoom out"); - board_canvas->Zoom(-10); -} - -void GameTabLeftPanel::OnSwap(wxCommandEvent &event) { - wxLogDebug("Clicked on swap"); - board_canvas->Swap(); -} - - - void GameTabLeftPanel::OnPlay(wxCommandEvent &event) { wxLogDebug("Game tab received PLAY_MOVE_EVENT"); if (game->Play(event.GetString().ToStdString())) { - NotifyEditor(); + // Notify other classes + wxCommandEvent event(GAME_CHANGE, GetId()); + event.SetEventObject(this); + ProcessEvent(event); } + // Refresh board canvas: Notify(); - - std::string fen = game->GetFen(); - std::map captures; - HalfMove *m = game->GetCurrentMove(); - if (m != nullptr) { - captures = m->GetLineCaptures(); - } } void GameTabLeftPanel::Notify(bool animate, bool backward) { + // Update fen and captures std::string fen = game->GetFen(); std::map captures; HalfMove *m = game->GetCurrentMove(); @@ -93,6 +80,7 @@ void GameTabLeftPanel::Notify(bool animate, bool backward) { captures = m->GetLineCaptures(); } + // Update board canvas: if(!animate){ if(m){ last_absolute_move=m->GetAbsoluteMove(); @@ -127,13 +115,8 @@ void GameTabLeftPanel::Notify(bool animate, bool backward) { } } + // Update fen field: fen_text_field->SetValue(game->GetFen()); } -void GameTabLeftPanel::NotifyEditor() { - wxCommandEvent previousEvent(GAME_CHANGE, GetId()); - previousEvent.SetEventObject(this); - ProcessEvent(previousEvent); -} - void GameTabLeftPanel::ApplyPreferences() { board_canvas->ApplyPreferences(); } diff --git a/src/game_tab/left_panel/GameTabLeftPanel.hpp b/src/game_tab/left_panel/GameTabLeftPanel.hpp index 3adbaee..b22d6fc 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.hpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.hpp @@ -8,11 +8,9 @@ // Foreign events wxDECLARE_EVENT(GAME_CHANGE, wxCommandEvent); - class GameTabLeftPanel : public TabGameLeftPanel { std::shared_ptr game; BoardCanvas *board_canvas; - void NotifyEditor(); std::string last_absolute_move; bool repeat; @@ -21,11 +19,6 @@ public: void Notify(bool animate=false,bool backward=false); void OnPlay(wxCommandEvent &event); void OnGotoMove(wxCommandEvent &event); - void PreviousMove(bool isKeyDown); - void NextMove(bool isKeyDown); - void OnZoomIn(wxCommandEvent &event); - void OnZoomOut(wxCommandEvent &event); - void OnSwap(wxCommandEvent &event); void OnRefreshBoard(wxCommandEvent &event); void ApplyPreferences(); void DisableSaveTool(){game_toolbar->EnableTool(0,false);}; diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index 386d593..7f2c38d 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -19,7 +19,7 @@ BoardCanvas::BoardCanvas(wxFrame *parent) adata.duration=200; adata.duration_fast=80; adata.fps=30; - + // Let GameTableLeftPanel process keyboard events: 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();}); } @@ -385,6 +385,7 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) { } } } + // Let GameTableLeftPanel process mouse wheel events: if (event.GetWheelRotation() != 0) { event.ResumePropagation(1);event.Skip(); } diff --git a/src/game_tab/left_panel/board/BoardCanvas.hpp b/src/game_tab/left_panel/board/BoardCanvas.hpp index 0ed18a7..30f0d06 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.hpp +++ b/src/game_tab/left_panel/board/BoardCanvas.hpp @@ -12,10 +12,6 @@ // Local events wxDECLARE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent); -// Foreign events -wxDECLARE_EVENT(PREVIOUS_MOVE_EVENT, wxCommandEvent); -wxDECLARE_EVENT(NEXT_MOVE_EVENT, wxCommandEvent); - #define REFRESH_MOUSE_LOCATION() \ { \ const wxPoint pt = wxGetMousePosition(); \ diff --git a/src/game_tab/right_panel/GameTabRightPanel.cpp b/src/game_tab/right_panel/GameTabRightPanel.cpp index 1b53c83..c88202b 100644 --- a/src/game_tab/right_panel/GameTabRightPanel.cpp +++ b/src/game_tab/right_panel/GameTabRightPanel.cpp @@ -120,7 +120,7 @@ void GameTabRightPanel::OnApply(wxCommandEvent &event) { std::string value = valueTextCtrl->GetValue().ToStdString(); game->SetTag(key, value); RefreshTagsList(); - wxCommandEvent event(REFRESH_TAB_TITLE, GetId()); + wxCommandEvent event(GAME_CHANGE, GetId()); event.SetEventObject(this); ProcessEvent(event); } @@ -146,28 +146,24 @@ void GameTabRightPanel::OnDelete(wxCommandEvent &event) { void GameTabRightPanel::OnGotoMove(wxCommandEvent &event) { wxLogDebug("GameTabRightPanel: received GOTO_MOVE_EVENT"); game->SetCurrent((HalfMove *)event.GetClientData()); - NotifyBoard(); - editor_canvas->Refresh(); + Notify(); } void GameTabRightPanel::OnMoveDelete(wxCommandEvent &event) { game->DeleteMove((HalfMove *)event.GetClientData()); - NotifyBoard(); - editor_canvas->Refresh(); + Notify(); } void GameTabRightPanel::OnMovePromote(wxCommandEvent &event) { wxLogDebug("GameTabRightPanel: promote move called"); game->PromoteMove((HalfMove *)event.GetClientData()); - NotifyBoard(); - editor_canvas->Refresh(); + Notify(); } void GameTabRightPanel::OnMoveSetAsMainline(wxCommandEvent &event) { wxLogDebug("GameTabRightPanel: set move as mainline called"); game->SetMoveAsMainline((HalfMove *)event.GetClientData()); - NotifyBoard(); - editor_canvas->Refresh(); + Notify(); } void GameTabRightPanel::Notify() { @@ -181,6 +177,7 @@ void GameTabRightPanel::Notify() { if (live_engine != nullptr) { live_engine->SetFEN(game->GetFen()); } + NotifyBoard(); } void GameTabRightPanel::ApplyPreferences() { diff --git a/src/game_tab/right_panel/editor/EditorCanvas.cpp b/src/game_tab/right_panel/editor/EditorCanvas.cpp index b65ece8..ae0c096 100644 --- a/src/game_tab/right_panel/editor/EditorCanvas.cpp +++ b/src/game_tab/right_panel/editor/EditorCanvas.cpp @@ -5,7 +5,16 @@ EditorCanvas::EditorCanvas(wxFrame *parent) hide_icon = LoadPNG("hide", wxSize(CGEditor::status.MoveIconWidth, CGEditor::status.MoveIconWidth)); t.ResizePieces(CGEditor::status.MoveIconWidth); + + // Theme: default_font=wxFont(*wxNORMAL_FONT).MakeBold(); + color_scrollbar_bg=wxColour(243,243,243); + color_scrollbar=*wxLIGHT_GREY; + color_margin=wxColour(243,243,243); + color_comments_bg=wxColour(255, 255, 204); + color_current_move_bg=wxColour(216, 216, 216); + color_menu_item_bg=wxColour(216, 216, 216); + 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();}); } @@ -46,13 +55,13 @@ void EditorCanvas::DrawElement(const cgeditor::Element &e) { dc->SetFont(default_font); if (e.prop & cgeditor::Property::Rectangle) { if (e.prop & cgeditor::Property::Scrollbarbg) { - dc->SetBrush(wxColour(243,243,243)); + dc->SetBrush(color_scrollbar_bg); } else if (e.prop & cgeditor::Property::Scrollbar) { - dc->SetBrush(*wxGREY_BRUSH); + dc->SetBrush(color_scrollbar); } else if (e.prop & cgeditor::Property::Margin) { - dc->SetBrush(wxBrush(wxColour(243,243,243))); + dc->SetBrush(wxBrush(color_margin)); } else if (e.prop & cgeditor::Property::Comment) { - dc->SetBrush(wxBrush(wxColour(255, 255, 204))); + dc->SetBrush(wxBrush(color_comments_bg)); } else if (e.prop & cgeditor::Property::Button) { if (e.prop & cgeditor::Property::On) { dc->DrawBitmap(hide_icon, e.x, e.y); @@ -84,7 +93,7 @@ void EditorCanvas::DrawElement(const cgeditor::Element &e) { } if (e.prop & cgeditor::Property::Current) { wxRect recToDraw(e.x, e.y, e.width, e.height); - dc->SetBrush(wxBrush(wxColour(216, 216, 216))); + dc->SetBrush(wxBrush(color_current_move_bg)); dc->DrawRectangle(recToDraw); } dc->DrawBitmap(*t.Get(p), e.x, y); @@ -92,7 +101,7 @@ void EditorCanvas::DrawElement(const cgeditor::Element &e) { dc->DrawText(wxString(e.text), wxPoint(e.x, e.y)); } else if (e.prop & cgeditor::Property::Menuitem) { wxRect recToDraw(e.x, e.y, e.width, e.height); - dc->SetBrush(wxBrush(wxColour(216, 216, 216))); + dc->SetBrush(wxBrush(color_menu_item_bg)); dc->DrawRectangle(recToDraw); dc->DrawText(wxString(e.text), wxPoint(e.x, Middle(e).y)); } else { @@ -100,7 +109,7 @@ void EditorCanvas::DrawElement(const cgeditor::Element &e) { if (e.prop & cgeditor::Property::Move) { if (e.prop & cgeditor::Property::Current) { wxRect recToDraw(e.x, e.y, e.width, e.height); - dc->SetBrush(wxBrush(wxColour(216, 216, 216))); + dc->SetBrush(wxBrush(color_current_move_bg)); dc->DrawRectangle(recToDraw); } if(e.prop & cgeditor::Property::Nag){ diff --git a/src/game_tab/right_panel/editor/EditorCanvas.hpp b/src/game_tab/right_panel/editor/EditorCanvas.hpp index 0858dbb..05d6258 100644 --- a/src/game_tab/right_panel/editor/EditorCanvas.hpp +++ b/src/game_tab/right_panel/editor/EditorCanvas.hpp @@ -21,6 +21,13 @@ class EditorCanvas : public wxPanel, public cgeditor::CGEditor { Theme t; wxFont default_font; + wxColour color_scrollbar_bg; + wxColour color_scrollbar; + wxColour color_margin; + wxColour color_comments_bg; + wxColour color_current_move_bg; + wxColour color_menu_item_bg; + public: EditorCanvas(wxFrame *parent); void OnPaint(wxPaintEvent &event);