From 40c6df0e7c3f17204a6187eb2bc241f396720272 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Thu, 24 Feb 2022 12:09:21 +0100 Subject: [PATCH] Improve BaseTab implementation for PGN files --- src/base_tab/BasePanelBF.cpp | 2 +- src/base_tab/BaseTab.cpp | 23 +++++++++++++---- src/base_tab/gamebase/PGNGameBase.cpp | 36 ++++++++++++++++++++++++--- src/base_tab/gamebase/PGNGameBase.hpp | 9 +++++-- 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/base_tab/BasePanelBF.cpp b/src/base_tab/BasePanelBF.cpp index 4edf8b0..59dd152 100644 --- a/src/base_tab/BasePanelBF.cpp +++ b/src/base_tab/BasePanelBF.cpp @@ -33,7 +33,7 @@ BasePanelBF::BasePanelBF( wxWindow* parent, wxWindowID id, const wxPoint& pos, c main_sizer->Add( top_sizer, 0, wxEXPAND, 5 ); - game_list = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxSize( -1,-1 ), wxLC_ICON ); + game_list = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxSize( -1,-1 ), wxLC_ICON|wxLC_REPORT ); main_sizer->Add( game_list, 1, wxALL|wxEXPAND, 5 ); wxBoxSizer* bottom_sizer; diff --git a/src/base_tab/BaseTab.cpp b/src/base_tab/BaseTab.cpp index 9c46d9b..9be9274 100644 --- a/src/base_tab/BaseTab.cpp +++ b/src/base_tab/BaseTab.cpp @@ -3,7 +3,20 @@ BaseTab::BaseTab(wxFrame *parent) : BasePanelBF(parent), TabInfos(TabInfos::BASE), base(NULL) { - LoadFile("/home/loic/text.PGn"); + + wxListItem col0; + col0.SetId(0); + col0.SetText(_("White")); + col0.SetWidth(200); + game_list->InsertColumn(0, col0); + + wxListItem col1; + col1.SetId(1); + col1.SetText(_("Black")); + col1.SetWidth(200); + game_list->InsertColumn(1, col1); + + //LoadFile("/home/loic/test.pgn"); } void BaseTab::ApplyPreferences() {} @@ -16,9 +29,9 @@ void BaseTab::LoadFile(std::string path) { } if (base != NULL) { - while(base->HasNextGame()){ - Game *g=base->GetNextGame(); - - } + while (base->HasNextGame()) { + Game *g = base->GetNextGame(); + long itemIndex = game_list->InsertItem(0, g->GetTag("White")); // want this for col. 1 + } } } \ No newline at end of file diff --git a/src/base_tab/gamebase/PGNGameBase.cpp b/src/base_tab/gamebase/PGNGameBase.cpp index 09d87d8..39775d1 100644 --- a/src/base_tab/gamebase/PGNGameBase.cpp +++ b/src/base_tab/gamebase/PGNGameBase.cpp @@ -1,5 +1,35 @@ #include "PGNGameBase.hpp" -PGNGameBase::PGNGameBase(std::string pgn_file) { - -} \ No newline at end of file +PGNGameBase::PGNGameBase(std::string pgn_file) + : pgn(new pgnp::PGN()), hasNextGame(false) { + pgn->FromFile(pgn_file); + ParseNextGame(); +} + +bool PGNGameBase::HasNextGame() { return (hasNextGame); } + +void PGNGameBase::ParseNextGame() { + try { + pgn->ParseNextGame(); + hasNextGame = true; + } catch (...) { + hasNextGame = false; + } +} + +Game *PGNGameBase::GetNextGame() { + pgnp::HalfMove *pgnp_moves = new pgnp::HalfMove(); + pgn->GetMoves(pgnp_moves); + std::string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; + if (pgn->HasTag("FEN")) { + fen = pgn->GetTagValue("FEN"); + } + HalfMove *m = new HalfMove(pgnp_moves, fen); + Game *g = new Game(m, fen); + for (std::string &s : pgn->GetTagList()) { + g->SetTag(s, pgn->GetTagValue(s)); + } + + ParseNextGame(); + return (g); +} diff --git a/src/base_tab/gamebase/PGNGameBase.hpp b/src/base_tab/gamebase/PGNGameBase.hpp index 2aa0840..b58bdca 100644 --- a/src/base_tab/gamebase/PGNGameBase.hpp +++ b/src/base_tab/gamebase/PGNGameBase.hpp @@ -1,10 +1,15 @@ #include "GameBase.hpp" +#include "pgnp.hpp" class PGNGameBase : public GameBase { + pgnp::PGN *pgn; + bool hasNextGame; + + void ParseNextGame(); public: PGNGameBase(std::string pgn_file); - bool HasNextGame() { return (false); } + bool HasNextGame(); Game *GetGame(std::uint32_t id) { return (new Game()); }; - Game *GetNextGame() { return (new Game()); }; + Game *GetNextGame(); }; \ No newline at end of file