Create repository

This commit is contained in:
Loic Guegan 2022-02-23 18:11:55 +01:00
commit ce941c146a
127 changed files with 16162 additions and 0 deletions

View file

@ -0,0 +1,186 @@
#include "EditorCanvas.hpp"
EditorCanvas::EditorCanvas(wxFrame *parent)
: wxPanel(parent), NeedRedraw(false) {
hide_icon = LoadPNG("hide", wxSize(CGEditor::status.MoveIconWidth,
CGEditor::status.MoveIconWidth));
t.ResizePieces(CGEditor::status.MoveIconWidth);
}
void EditorCanvas::OnPaint(wxPaintEvent &event) {
wxPaintDC current_dc(this);
dc = &current_dc;
// Refresh canvas size
wxSize sz = GetClientSize();
CGEditor::status.CanvasWidth = sz.GetWidth();
CGEditor::status.CanvasHeight = sz.GetHeight();
CGEditor::status.UseMoveIcons =
true; // Piece image should be drawn before the move ?
const wxPoint pt = wxGetMousePosition();
CGEditor::status.MouseX = pt.x - this->GetScreenPosition().x;
CGEditor::status.MouseY = pt.y - this->GetScreenPosition().y;
CGEditor::Draw();
}
/**
* @brief Convenient fonction to center text
*
* @param e Element to center
* @return wxPoint The centered version of e according to wdWidget API
*/
wxPoint EditorCanvas::Middle(cgeditor::Element e) {
wxSize sz = dc->GetTextExtent(e.text);
return (wxPoint(e.x + (e.width - sz.GetWidth()) / 2,
e.y + (e.height - sz.GetHeight()) / 2));
}
void EditorCanvas::DrawElement(const cgeditor::Element &e) {
dc->SetPen(wxNullPen);
dc->SetBrush(*wxRED_BRUSH);
if (e.prop & cgeditor::Property::Rectangle) {
if (e.prop & cgeditor::Property::Scrollbarbg) {
dc->SetBrush(*wxCYAN_BRUSH);
} else if (e.prop & cgeditor::Property::Scrollbar) {
dc->SetBrush(*wxGREY_BRUSH);
} else if (e.prop & cgeditor::Property::Margin) {
dc->SetBrush(*wxLIGHT_GREY_BRUSH);
} else if (e.prop & cgeditor::Property::Button) {
if (e.prop & cgeditor::Property::On) {
dc->DrawBitmap(hide_icon, e.x, e.y);
return;
}
dc->SetBrush(*wxBLACK_BRUSH);
}
wxRect recToDraw(e.x, e.y, e.width, e.height);
dc->DrawRectangle(recToDraw);
} else if (e.prop & cgeditor::Property::Text ||
e.prop & cgeditor::Property::Image) {
if (e.prop & cgeditor::Property::Image) {
// Draw your pieces images instead
std::uint32_t y = Middle(e).y - CGEditor::status.MoveIconWidth / 2;
char p = 'P';
if (e.prop & cgeditor::Property::Knight) {
p = 'N';
} else if (e.prop & cgeditor::Property::Bishop) {
p = 'B';
} else if (e.prop & cgeditor::Property::Queen) {
p = 'Q';
} else if (e.prop & cgeditor::Property::King) {
p = 'K';
}
if (e.prop & cgeditor::Property::Black) {
p = std::tolower(p);
}
if (e.prop & cgeditor::Property::Current) {
wxRect recToDraw(e.x, e.y, e.width, e.height);
dc->SetBrush(*wxLIGHT_GREY_BRUSH);
dc->DrawRectangle(recToDraw);
}
dc->DrawBitmap(*t.Get(p), e.x, y);
} else if (e.prop & cgeditor::Property::Comment) {
wxRect recToDraw(e.x, e.y, e.width, e.height);
dc->SetBrush(*wxYELLOW_BRUSH);
dc->DrawRectangle(recToDraw);
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(*wxLIGHT_GREY_BRUSH);
dc->DrawRectangle(recToDraw);
dc->DrawText(wxString(e.text), wxPoint(e.x, Middle(e).y));
} else {
if (e.prop & cgeditor::Property::Move) {
if (e.prop & cgeditor::Property::Current) {
wxRect recToDraw(e.x, e.y, e.width, e.height);
dc->SetBrush(*wxLIGHT_GREY_BRUSH);
dc->DrawRectangle(recToDraw);
}
if (CGEditor::status.UseMoveIcons) {
dc->DrawText(wxString(e.text), wxPoint(e.x, Middle(e).y));
} else {
dc->DrawText(wxString(e.text), Middle(e));
}
} else {
dc->DrawText(wxString(e.text), Middle(e));
}
}
}
}
void EditorCanvas::HandleEvent(const cgeditor::Event &e) {
wxLogDebug("Editor event!");
if (e.type == cgeditor::Event::Goto) {
wxCommandEvent event(GOTO_MOVE_EVENT, GetId());
event.SetEventObject(this);
event.SetClientData(e.move);
ProcessEvent(event);
} else if (e.type == cgeditor::Event::Delete) {
wxCommandEvent event(DELETE_MOVE_EVENT, GetId());
event.SetEventObject(this);
event.SetClientData(e.move);
ProcessEvent(event);
} else if (e.type == cgeditor::Event::Promote) {
wxCommandEvent event(PROMOTE_MOVE_EVENT, GetId());
event.SetEventObject(this);
event.SetClientData(e.move);
ProcessEvent(event);
} else if (e.type == cgeditor::Event::SetAsMainline) {
wxCommandEvent event(SET_AS_MAINLINE_EVENT, GetId());
event.SetEventObject(this);
event.SetClientData(e.move);
ProcessEvent(event);
}
}
void EditorCanvas::MouseEvent(wxMouseEvent &event) {
if (event.Dragging()) {
CGEditor::status.LeftClick = false;
CGEditor::status.IsDrag = true;
Refresh();
} else if (event.LeftDown()) {
SetFocus();
CGEditor::status.LeftClick = true;
Refresh();
} else if (event.RightDown()) {
SetFocus();
CGEditor::status.RightClick = true;
Refresh();
} else if (event.GetWheelRotation() != 0) {
SetFocus();
if (event.GetWheelRotation() < 0) {
CGEditor::status.EventVScroll = 50;
} else {
CGEditor::status.EventVScroll = -50;
}
Refresh();
}
// Should another draw of CGEditor be made?
if (NeedRedraw) {
Refresh();
NeedRedraw = false;
}
}
void EditorCanvas::SetMoves(HalfMove *moves, HalfMove *current) {
CGEditor::status.Moves = moves;
CGEditor::status.CurrentMove = current;
Refresh();
}
void EditorCanvas::OnKeyEvent(wxKeyEvent &event) {
if (event.GetKeyCode() == WXK_LEFT) {
wxCommandEvent previousEvent(PREVIOUS_MOVE_EVENT, GetId());
previousEvent.SetEventObject(this);
ProcessEvent(previousEvent);
} else if (event.GetKeyCode() == WXK_RIGHT) {
wxCommandEvent nextEvent(NEXT_MOVE_EVENT, GetId());
nextEvent.SetEventObject(this);
ProcessEvent(nextEvent);
}
}
wxBEGIN_EVENT_TABLE(EditorCanvas, wxPanel) EVT_PAINT(EditorCanvas::OnPaint)
EVT_MOUSE_EVENTS(EditorCanvas::MouseEvent)
EVT_CHAR_HOOK(EditorCanvas::OnKeyEvent) wxEND_EVENT_TABLE()

View file

@ -0,0 +1,33 @@
#pragma once
#include "../HalfMove.hpp"
#include "CGEditor.hpp"
#include "ochess.hpp"
#include "../board/Theme.hpp"
// Foreign events
wxDECLARE_EVENT(GOTO_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(PREVIOUS_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(NEXT_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(DELETE_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(PROMOTE_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(SET_AS_MAINLINE_EVENT, wxCommandEvent);
class EditorCanvas : public wxPanel, public cgeditor::CGEditor {
wxPaintDC *dc;
bool NeedRedraw;
wxPoint Middle(cgeditor::Element e);
wxBitmap hide_icon;
Theme t;
public:
EditorCanvas(wxFrame *parent);
void OnPaint(wxPaintEvent &event);
void MouseEvent(wxMouseEvent &event);
void DrawElement(const cgeditor::Element &e);
void HandleEvent(const cgeditor::Event &e);
void SetMoves(HalfMove *moves, HalfMove *current);
void OnKeyEvent(wxKeyEvent &event);
DECLARE_EVENT_TABLE()
};

View file

@ -0,0 +1,207 @@
#include "EditorPanel.hpp"
wxDEFINE_EVENT(GOTO_MOVE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(DELETE_MOVE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(PROMOTE_MOVE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(SET_AS_MAINLINE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(PREVIOUS_MOVE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(NEXT_MOVE_EVENT, wxCommandEvent);
EditorPanel::EditorPanel(wxFrame *parent, Game *game)
: wxPanel(parent), game(game), selected_item(-1) {
// ----- Init -----
wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
wxNotebook *notebook = new wxNotebook(this, wxID_ANY);
//----- CGEditor Page -----
wxPanel *cgeditor_panel = new wxPanel(notebook, wxID_ANY);
wxBoxSizer *cgeditor_panel_sizer = new wxBoxSizer(wxVERTICAL);
editor_canvas = new EditorCanvas((wxFrame *)cgeditor_panel);
cgeditor_panel_sizer->Add(editor_canvas, 1, wxEXPAND);
// Comment box
wxStaticBox *commentBox =
new wxStaticBox(cgeditor_panel, wxID_ANY, "Move Comment");
wxBoxSizer *commentBoxSizer = new wxBoxSizer(wxVERTICAL);
comment_input = new wxTextCtrl(
commentBox, COMMENT_INPUT_BOX, wxEmptyString, // Use right ID
wxDefaultPosition, wxSize(0, 150), wxTE_MULTILINE);
commentBoxSizer->Add(comment_input, 0, wxEXPAND);
commentBox->SetSizer(commentBoxSizer);
cgeditor_panel_sizer->Add(commentBox, 0, wxEXPAND);
cgeditor_panel->SetSizer(cgeditor_panel_sizer);
//----- Tags Page -----
wxPanel *tag_panel = new wxPanel(notebook, wxID_ANY);
wxBoxSizer *tag_panel_sizer = new wxBoxSizer(wxVERTICAL);
wxPanel *tag_edit_panel = new wxPanel(tag_panel, wxID_ANY);
wxBoxSizer *tag_edit_panel_sizer = new wxBoxSizer(wxVERTICAL);
tagTextCtrl = new wxTextCtrl(tag_edit_panel, wxID_ANY);
tagTextCtrl->SetHint(wxString("Name"));
tag_edit_panel_sizer->Add(tagTextCtrl, 1, wxEXPAND);
valueTextCtrl = new wxTextCtrl(tag_edit_panel, wxID_ANY);
valueTextCtrl->SetHint(wxString("Value"));
tag_edit_panel_sizer->Add(valueTextCtrl, 1, wxEXPAND);
tag_edit_panel_sizer->Add(new wxButton(tag_edit_panel, UPDATE_BTN, L"Update"),
1, wxEXPAND);
tag_edit_panel->SetSizer(tag_edit_panel_sizer);
tag_panel_sizer->Add(tag_edit_panel, 0, wxEXPAND);
tags_list = new wxListCtrl(tag_panel, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxLC_REPORT);
tags_list->InsertColumn(0, L"Name");
tags_list->InsertColumn(1, L"Value", wxLIST_FORMAT_LEFT, 500);
RefreshTagsList();
tag_panel_sizer->Add(tags_list, 1, wxEXPAND);
delete_button = new wxButton(tag_panel, DELETE_BTN, L"Delete");
delete_button->Enable(false);
tag_panel_sizer->Add(delete_button, 0, wxEXPAND);
tag_panel->SetSizer(tag_panel_sizer);
//----- Notebook -----
notebook->AddPage(cgeditor_panel, L"Editor");
notebook->AddPage(tag_panel, L"Tags");
sizer->Add(notebook, 1, wxEXPAND);
//----- Sizer -----
this->SetSizer(sizer);
// Bind events
this->Bind(wxEVT_TEXT, &EditorPanel::OnCommentChange, this,
COMMENT_INPUT_BOX);
this->Bind(GOTO_MOVE_EVENT, &EditorPanel::OnGotoMove, this, wxID_ANY);
this->Bind(DELETE_MOVE_EVENT, &EditorPanel::OnMoveDelete, this, wxID_ANY);
this->Bind(PROMOTE_MOVE_EVENT, &EditorPanel::OnMovePromote, this, wxID_ANY);
this->Bind(SET_AS_MAINLINE_EVENT, &EditorPanel::OnMoveSetAsMainline, this,
wxID_ANY);
this->Bind(NEXT_MOVE_EVENT, &EditorPanel::OnNextMove, this, wxID_ANY);
this->Bind(PREVIOUS_MOVE_EVENT, &EditorPanel::OnPreviousMove, this, wxID_ANY);
this->Bind(wxEVT_LIST_ITEM_SELECTED, &EditorPanel::OnTagSelected, this,
wxID_ANY);
this->Bind(wxEVT_LIST_ITEM_DESELECTED, &EditorPanel::OnTagDeselected, this,
wxID_ANY);
this->Bind(wxEVT_BUTTON, &EditorPanel::OnApply, this, UPDATE_BTN);
this->Bind(wxEVT_BUTTON, &EditorPanel::OnDelete, this, DELETE_BTN);
}
void EditorPanel::OnTagSelected(wxListEvent &event) {
wxListItem item = event.GetItem();
std::string key = item.GetText().ToStdString();
tagTextCtrl->ChangeValue(key);
item.SetColumn(1);
tags_list->GetItem(item);
valueTextCtrl->ChangeValue(item.GetText().ToStdString());
selected_item = item.GetId();
delete_button->Enable(true);
}
void EditorPanel::OnTagDeselected(wxListEvent &event) {
selected_item = -1;
delete_button->Enable(false);
}
void EditorPanel::NotifyBoard() {
wxCommandEvent previousEvent(GAME_CHANGE, GetId());
previousEvent.SetEventObject(this);
ProcessEvent(previousEvent);
}
void EditorPanel::OnCommentChange(wxCommandEvent &event) {
wxLogDebug("EditorPanel: comment input change");
HalfMove *m = game->GetCurrentMove();
if (m != NULL) {
m->SetComment(event.GetString().ToStdString());
}
editor_canvas->Refresh();
}
void EditorPanel::OnApply(wxCommandEvent &event) {
std::string key = tagTextCtrl->GetValue().ToStdString();
if (key == "FEN") {
SHOW_DIALOG_ERROR("Editing the FEN tag is forbidden");
return;
}
if (key.size() > 0) {
std::string value = valueTextCtrl->GetValue().ToStdString();
game->SetTag(key, value);
RefreshTagsList();
wxCommandEvent event(REFRESH_TAB_TITLE, GetId());
event.SetEventObject(this);
ProcessEvent(event);
}
}
void EditorPanel::OnDelete(wxCommandEvent &event) {
if (selected_item >= 0) {
wxListItem item;
item.SetColumn(0);
item.SetId(selected_item);
tags_list->GetItem(item);
std::string key = item.GetText().ToStdString();
if (key != "FEN") {
game->DeleteTag(key);
selected_item = -1;
RefreshTagsList();
} else {
SHOW_DIALOG_ERROR("Deleting the FEN tag is forbidden.");
}
}
}
void EditorPanel::OnGotoMove(wxCommandEvent &event) {
wxLogDebug("EditorPanel: received GOTO_MOVE_EVENT");
game->SetCurrent((HalfMove *)event.GetClientData());
NotifyBoard();
editor_canvas->Refresh();
}
void EditorPanel::OnMoveDelete(wxCommandEvent &event) {
game->DeleteMove((HalfMove *)event.GetClientData());
NotifyBoard();
editor_canvas->Refresh();
}
void EditorPanel::OnMovePromote(wxCommandEvent &event) {
wxLogDebug("EditorPanel: promote move called");
game->PromoteMove((HalfMove *)event.GetClientData());
NotifyBoard();
editor_canvas->Refresh();
}
void EditorPanel::OnMoveSetAsMainline(wxCommandEvent &event) {
wxLogDebug("EditorPanel: set move as mainline called");
game->SetMoveAsMainline((HalfMove *)event.GetClientData());
NotifyBoard();
editor_canvas->Refresh();
}
void EditorPanel::Notify() {
HalfMove *m = game->GetCurrentMove();
if (m != NULL) {
comment_input->ChangeValue(
m->GetComment()); // ChangeValue do not raise events
}
editor_canvas->SetMoves(game->GetMoves(), m);
}
void EditorPanel::RefreshTagsList() {
tags_list->DeleteAllItems();
for (std::string s : game->ListTags()) {
long index = tags_list->InsertItem(0, s);
tags_list->SetItem(index, 1, game->GetTag(s));
if (s == "FEN") {
tags_list->SetItemBackgroundColour(index, wxColour(200, 200, 200));
}
}
}
void EditorPanel::OnPreviousMove(wxCommandEvent &event) {
game->Previous();
Notify();
NotifyBoard();
}
void EditorPanel::OnNextMove(wxCommandEvent &event) {
game->Next();
Notify();
NotifyBoard();
}

View file

@ -0,0 +1,44 @@
#include "../Game.hpp"
#include "EditorCanvas.hpp"
#include "ochess.hpp"
#include <wx/listctrl.h>
#include <wx/notebook.h>
// Local events
wxDECLARE_EVENT(GOTO_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(DELETE_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(PROMOTE_MOVE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(SET_AS_MAINLINE_EVENT, wxCommandEvent);
wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
// Foreign events
wxDECLARE_EVENT(GAME_CHANGE, wxCommandEvent);
enum { COMMENT_INPUT_BOX = wxID_HIGHEST + 100, UPDATE_BTN, DELETE_BTN };
class EditorPanel : public wxPanel {
Game *game;
EditorCanvas *editor_canvas;
wxTextCtrl *comment_input;
wxListCtrl *tags_list;
wxTextCtrl *tagTextCtrl, *valueTextCtrl;
wxButton *delete_button;
long selected_item;
public:
EditorPanel(wxFrame *parent, Game *game);
void NotifyBoard();
void Notify();
void OnCommentChange(wxCommandEvent &event);
void OnGotoMove(wxCommandEvent &event);
void OnMoveDelete(wxCommandEvent &event);
void OnMovePromote(wxCommandEvent &event);
void OnMoveSetAsMainline(wxCommandEvent &event);
void RefreshTagsList();
void OnTagSelected(wxListEvent &event);
void OnTagDeselected(wxListEvent &event);
void OnApply(wxCommandEvent &event);
void OnDelete(wxCommandEvent &event);
void OnPreviousMove(wxCommandEvent &event);
void OnNextMove(wxCommandEvent &event);
};