Improve BaseTab pgn loading

This commit is contained in:
Loic Guegan 2022-02-24 15:22:56 +01:00
parent 40c6df0e7c
commit f99a7b699a
7 changed files with 87 additions and 48 deletions

View file

@ -33,7 +33,7 @@ BasePanelBF::BasePanelBF( wxWindow* parent, wxWindowID id, const wxPoint& pos, c
main_sizer->Add( top_sizer, 0, wxEXPAND, 5 ); main_sizer->Add( top_sizer, 0, wxEXPAND, 5 );
game_list = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxSize( -1,-1 ), wxLC_ICON|wxLC_REPORT ); game_list = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxSize( -1,-1 ), wxLC_REPORT );
main_sizer->Add( game_list, 1, wxALL|wxEXPAND, 5 ); main_sizer->Add( game_list, 1, wxALL|wxEXPAND, 5 );
wxBoxSizer* bottom_sizer; wxBoxSizer* bottom_sizer;

View file

@ -4,19 +4,29 @@
BaseTab::BaseTab(wxFrame *parent) BaseTab::BaseTab(wxFrame *parent)
: BasePanelBF(parent), TabInfos(TabInfos::BASE), base(NULL) { : BasePanelBF(parent), TabInfos(TabInfos::BASE), base(NULL) {
wxListItem col0; game_list->InsertColumn(0, L"id", wxLIST_FORMAT_LEFT, 50);
col0.SetId(0); game_list->InsertColumn(1, L"White", wxLIST_FORMAT_LEFT, 200);
col0.SetText(_("White")); game_list->InsertColumn(2, L"Black", wxLIST_FORMAT_LEFT, 200);
col0.SetWidth(200); game_list->InsertColumn(3, L"Event", wxLIST_FORMAT_LEFT, 200);
game_list->InsertColumn(0, col0); game_list->InsertColumn(4, L"Round", wxLIST_FORMAT_LEFT, 100);
game_list->InsertColumn(5, L"Result", wxLIST_FORMAT_LEFT, 200);
game_list->InsertColumn(6, L"Result", wxLIST_FORMAT_LEFT, 200);
wxListItem col1; this->Bind(wxEVT_BUTTON, &BaseTab::OnBim, this, wxID_ANY);
col1.SetId(1); this->Bind(wxEVT_LIST_ITEM_ACTIVATED, &BaseTab::OnOpenGame, this, wxID_ANY);
col1.SetText(_("Black")); }
col1.SetWidth(200);
game_list->InsertColumn(1, col1);
//LoadFile("/home/loic/test.pgn"); void BaseTab::OnBim(wxCommandEvent &event) {
//LoadFile("/home/loic/hartwig.pgn");
}
void BaseTab::OnOpenGame(wxListEvent &event) {
wxLogDebug("Open!");
long id=std::stoi(event.GetItem().GetText().ToStdString());
Game *g = base->GetGame(id);
if (g != NULL) {
wxLogDebug("Open game: %s", g->GetTag("White"));
}
} }
void BaseTab::ApplyPreferences() {} void BaseTab::ApplyPreferences() {}
@ -29,9 +39,15 @@ void BaseTab::LoadFile(std::string path) {
} }
if (base != NULL) { if (base != NULL) {
while (base->HasNextGame()) { long id = 0;
Game *g = base->GetNextGame(); while (base->NextGame()) {
long itemIndex = game_list->InsertItem(0, g->GetTag("White")); // want this for col. 1 long index = game_list->InsertItem(0, std::to_string(id)); // want this for col. 1
game_list->SetItem(index, 1, base->GetTag("White"));
game_list->SetItem(index, 2, base->GetTag("Black"));
game_list->SetItem(index, 3, base->GetTag("Event"));
game_list->SetItem(index, 4, base->GetTag("Round"));
game_list->SetItem(index, 5, base->GetTag("Result"));
id++;
} }
} }
} }

View file

@ -1,13 +1,16 @@
#include "ochess.hpp"
#include "BasePanelBF.h" #include "BasePanelBF.h"
#include "gamebase/GameBase.hpp" #include "gamebase/GameBase.hpp"
#include "gamebase/PGNGameBase.hpp" #include "gamebase/PGNGameBase.hpp"
#include "ochess.hpp"
class BaseTab : public BasePanelBF, public TabInfos { class BaseTab : public BasePanelBF, public TabInfos {
GameBase *base; GameBase *base;
public: public:
BaseTab(wxFrame *parent); BaseTab(wxFrame *parent);
void ApplyPreferences(); void ApplyPreferences();
void LoadFile(std::string path); void LoadFile(std::string path);
void OnBim(wxCommandEvent &event);
void OnOpenGame(wxListEvent &event);
}; };

View file

@ -4,7 +4,8 @@
class GameBase { class GameBase {
public: public:
virtual bool HasNextGame() = 0;
virtual Game *GetGame(std::uint32_t id) = 0; virtual Game *GetGame(std::uint32_t id) = 0;
virtual Game *GetNextGame() = 0; virtual bool NextGame() = 0;
virtual std::string GetTag(std::string tag) = 0;
virtual void Reset() = 0;
}; };

View file

@ -1,35 +1,54 @@
#include "PGNGameBase.hpp" #include "PGNGameBase.hpp"
PGNGameBase::PGNGameBase(std::string pgn_file) PGNGameBase::PGNGameBase(std::string pgn_file) : pgn(new pgnp::PGN()) {
: pgn(new pgnp::PGN()), hasNextGame(false) { file = pgn_file;
pgn->FromFile(pgn_file); pgn->FromFile(pgn_file);
ParseNextGame();
} }
bool PGNGameBase::HasNextGame() { return (hasNextGame); } bool PGNGameBase::NextGame() {
bool game_found = false;
void PGNGameBase::ParseNextGame() {
try { try {
pgn->ParseNextGame(); pgn->ParseNextGame();
hasNextGame = true; game_found = true;
} catch (...) { } catch (...) {
hasNextGame = false; game_found = false;
} }
return (game_found);
} }
Game *PGNGameBase::GetNextGame() { std::string PGNGameBase::GetTag(std::string tag) {
pgnp::HalfMove *pgnp_moves = new pgnp::HalfMove(); if (pgn->HasTag(tag)) {
pgn->GetMoves(pgnp_moves); return (pgn->GetTagValue(tag));
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); return ("");
Game *g = new Game(m, fen); }
for (std::string &s : pgn->GetTagList()) {
g->SetTag(s, pgn->GetTagValue(s)); void PGNGameBase::Reset() {
} delete pgn;
pgn = new pgnp::PGN();
ParseNextGame(); pgn->FromFile(file);
return (g); }
Game *PGNGameBase::GetGame(std::uint32_t id) {
Reset();
std::uint32_t curid = 0;
while(NextGame()) {
if (id == curid) {
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));
}
return (g);
}
curid++;
}
return (NULL);
} }

View file

@ -2,14 +2,14 @@
#include "pgnp.hpp" #include "pgnp.hpp"
class PGNGameBase : public GameBase { class PGNGameBase : public GameBase {
pgnp::PGN *pgn; pgnp::PGN *pgn;
bool hasNextGame; bool hasNextGame;
std::string file;
void ParseNextGame();
public: public:
PGNGameBase(std::string pgn_file); PGNGameBase(std::string pgn_file);
Game *GetGame(std::uint32_t id);
bool HasNextGame(); bool NextGame();
Game *GetGame(std::uint32_t id) { return (new Game()); }; std::string GetTag(std::string tag);
Game *GetNextGame(); void Reset();
}; };

View file

@ -388,7 +388,7 @@
<property name="resize">Resizable</property> <property name="resize">Resizable</property>
<property name="show">1</property> <property name="show">1</property>
<property name="size">-1,-1</property> <property name="size">-1,-1</property>
<property name="style">wxLC_ICON</property> <property name="style">wxLC_REPORT</property>
<property name="subclass">; ; forward_declare</property> <property name="subclass">; ; forward_declare</property>
<property name="toolbar_pane">0</property> <property name="toolbar_pane">0</property>
<property name="tooltip"></property> <property name="tooltip"></property>