Decouple editor event from the Draw class

This commit is contained in:
Loic Guegan 2023-01-01 17:25:27 +01:00
parent 3271972f9e
commit 31c332da9a
4 changed files with 15 additions and 19 deletions

View file

@ -1,19 +1,21 @@
[![license](https://img.shields.io/badge/License-LGPL_v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0)
# cgeditor: Chess Game Editor
cgeditor is a dependency-free chess game editor library written in C++. It can be used with any library that provides 2D canvas drawing and mouse inputs features.
cgeditor is a dependency-free chess game editor library written in C++. It can be used with any library that provides 2D canvas drawing and mouse/keyboard events.
# Features
- Show move, move number, variations, NAGs, comments etc.
- *Delete*, *Promote* and *Set as main line* features
- *Delete*, *Promote* and *Set as main line* menu entries
- Handle pieces icons
- Its graphical appareance is entirely customizable
# Architecture
To run cgeditor you need to extend 2 classes:
To run cgeditor 2 classes need to be extended:
- CGEditor (To draw and handle events)
- CGEHalfMove (The data structure displayed by the editor)
See example for more informations.
# Example
An example based on *wxWidgets* is available in the `examples/` folder:
![wxWidgets](examples/wxWidgets/demo.gif)

View file

@ -12,7 +12,6 @@
*/
class MyFrame : public wxFrame, public cgeditor::CGEditor {
wxPaintDC *dc;
bool NeedRedraw = false;
public:
MyFrame()
@ -72,10 +71,16 @@ private:
Refresh();
}
// Should another draw of CGEditor be made?
if (NeedRedraw) {
// Now handle event
bool redraw=false;
Update();
for(const cgeditor::Event &e: status.Events){
HandleEvent(e);
redraw=true;
}
status.Events.clear();
if(redraw){
Refresh();
NeedRedraw = false;
}
}
@ -163,7 +168,6 @@ private:
else if (e.type == cgeditor::Event::Type::Promote) {
str = "Promote";
static_cast<MyHalfMove *>(e.move)->MyHalfMove::Promote();
NeedRedraw = true;
} else if (e.type == cgeditor::Event::Type::Delete) {
str = "Delete";
if (e.move->Parent != NULL) {
@ -172,11 +176,9 @@ private:
} else {
CGEditor::status.Moves = NULL;
}
NeedRedraw = true;
} else if (e.type == cgeditor::Event::Type::SetAsMainline) {
str = "Set as main line";
static_cast<MyHalfMove *>(e.move)->MyHalfMove::SetAsMainline();
NeedRedraw = true;
} else if (e.type == cgeditor::Event::Type::Goto) {
str = "Goto move";
}

View file

@ -40,12 +40,6 @@ void CGEditor::Draw() {
DrawComponent(SBH);
DrawComponent(ME);
// Handle events
for (Event &e : status.Events) {
HandleEvent(e);
}
status.Events.clear();
// Update mouse events
status.LeftClick = false;
status.RightClick = false;

View file

@ -27,8 +27,6 @@ protected:
void Draw();
/// @brief Draw an element on the canvas
virtual void DrawElement(const Element &) = 0;
/// @brief Handle event that occured during editor drawing
virtual void HandleEvent(const Event &) = 0;
public:
CGEditor();