aboutsummaryrefslogtreecommitdiff
path: root/src/game_tab/editor/EditorCanvas.cpp
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2022-02-23 18:11:55 +0100
committerLoic Guegan <manzerbredes@mailbox.org>2022-02-23 18:11:55 +0100
commitce941c146aea7925bded6b9d2a0d0559d3156ad3 (patch)
tree4c52e02600e3fd127bfb28b3e974d45541ec9e4e /src/game_tab/editor/EditorCanvas.cpp
Create repository
Diffstat (limited to 'src/game_tab/editor/EditorCanvas.cpp')
-rw-r--r--src/game_tab/editor/EditorCanvas.cpp186
1 files changed, 186 insertions, 0 deletions
diff --git a/src/game_tab/editor/EditorCanvas.cpp b/src/game_tab/editor/EditorCanvas.cpp
new file mode 100644
index 0000000..441118d
--- /dev/null
+++ b/src/game_tab/editor/EditorCanvas.cpp
@@ -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()