ochess/src/game_tab/right_panel/editor/EditorCanvas.cpp

211 lines
7 KiB
C++
Raw Normal View History

2022-02-23 18:11:55 +01:00
#include "EditorCanvas.hpp"
2023-01-01 14:12:57 +01:00
EditorCanvas::EditorCanvas(wxFrame *parent, std::shared_ptr<Game> game)
2023-01-01 17:30:55 +01:00
: wxPanel(parent), game(game) {
2023-06-03 20:33:17 +02:00
hide_icon = LoadPNG("ui_eye_off", wxSize(CGEditor::status.MoveIconWidth,
2022-02-23 18:11:55 +01:00
CGEditor::status.MoveIconWidth));
t.ResizePieces(CGEditor::status.MoveIconWidth);
2023-01-01 12:15:08 +01:00
// Theme:
2022-12-30 10:02:56 +01:00
default_font=wxFont(*wxNORMAL_FONT).MakeBold();
2023-01-09 15:22:31 +01:00
color_scrollbar_bg=wxColour("#FFFFFF");
color_scrollbar=wxColour("#838383");
color_margin=wxColour("#F3F3F3");
color_comments_bg=wxColour("#ffffcc");
2023-01-01 12:15:08 +01:00
color_current_move_bg=wxColour(216, 216, 216);
color_menu_item_bg=wxColour(216, 216, 216);
2023-01-01 20:28:39 +01:00
// The following should be called when using an EVT_PAINT handler
SetBackgroundStyle(wxBG_STYLE_PAINT);
2022-12-31 13:59:55 +01:00
Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
2022-02-23 18:11:55 +01:00
}
void EditorCanvas::OnPaint(wxPaintEvent &event) {
2023-05-10 10:49:31 +02:00
UNUSED(event);
2022-02-23 18:11:55 +01:00
wxPaintDC current_dc(this);
2023-01-01 20:28:39 +01:00
current_dc.SetBackground(*wxWHITE_BRUSH);
current_dc.Clear();
2022-02-23 18:11:55 +01:00
dc = &current_dc;
// Refresh canvas size
wxSize sz = GetClientSize();
CGEditor::status.CanvasWidth = sz.GetWidth();
CGEditor::status.CanvasHeight = sz.GetHeight();
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) {
2022-12-28 10:37:57 +01:00
// TODO: Optimize brush!!!! Always instanciated at each call
2022-02-23 18:11:55 +01:00
dc->SetPen(wxNullPen);
dc->SetBrush(*wxRED_BRUSH);
2022-12-30 10:02:56 +01:00
dc->SetFont(default_font);
2022-02-23 18:11:55 +01:00
if (e.prop & cgeditor::Property::Rectangle) {
if (e.prop & cgeditor::Property::Scrollbarbg) {
2023-01-01 12:15:08 +01:00
dc->SetBrush(color_scrollbar_bg);
2022-02-23 18:11:55 +01:00
} else if (e.prop & cgeditor::Property::Scrollbar) {
2023-01-01 12:15:08 +01:00
dc->SetBrush(color_scrollbar);
2022-02-23 18:11:55 +01:00
} else if (e.prop & cgeditor::Property::Margin) {
2023-01-01 12:15:08 +01:00
dc->SetBrush(wxBrush(color_margin));
2022-12-31 12:19:45 +01:00
} else if (e.prop & cgeditor::Property::Comment) {
2023-01-01 12:15:08 +01:00
dc->SetBrush(wxBrush(color_comments_bg));
2022-02-23 18:11:55 +01:00
} 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';
2022-02-24 19:11:44 +01:00
} else if (e.prop & cgeditor::Property::Rook) {
p = 'R';
2022-02-23 18:11:55 +01:00
}
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);
2023-01-01 12:15:08 +01:00
dc->SetBrush(wxBrush(color_current_move_bg));
2022-02-23 18:11:55 +01:00
dc->DrawRectangle(recToDraw);
}
dc->DrawBitmap(*t.Get(p), e.x, y);
} else if (e.prop & cgeditor::Property::Comment) {
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);
2023-01-01 12:15:08 +01:00
dc->SetBrush(wxBrush(color_menu_item_bg));
2022-02-23 18:11:55 +01:00
dc->DrawRectangle(recToDraw);
dc->DrawText(wxString(e.text), wxPoint(e.x, Middle(e).y));
} else {
2022-12-30 10:02:56 +01:00
std::string text=e.text;
2022-02-23 18:11:55 +01:00
if (e.prop & cgeditor::Property::Move) {
if (e.prop & cgeditor::Property::Current) {
wxRect recToDraw(e.x, e.y, e.width, e.height);
2023-01-01 12:15:08 +01:00
dc->SetBrush(wxBrush(color_current_move_bg));
2022-02-23 18:11:55 +01:00
dc->DrawRectangle(recToDraw);
}
2022-12-30 10:02:56 +01:00
if(e.prop & cgeditor::Property::Nag){
2023-01-19 16:33:44 +01:00
text=e.text;
2022-12-30 10:02:56 +01:00
}
// Draw move text
2022-02-23 18:11:55 +01:00
if (CGEditor::status.UseMoveIcons) {
2022-12-30 10:02:56 +01:00
dc->DrawText(wxString(text), wxPoint(e.x, Middle(e).y));
2022-02-23 18:11:55 +01:00
} else {
2022-12-30 10:02:56 +01:00
dc->DrawText(wxString(text), Middle(e));
2022-02-23 18:11:55 +01:00
}
} else {
2022-12-30 10:02:56 +01:00
dc->DrawText(wxString(text), Middle(e));
2022-02-23 18:11:55 +01:00
}
}
}
}
void EditorCanvas::HandleEvent(const cgeditor::Event &e) {
if (e.type == cgeditor::Event::Goto) {
2023-01-01 14:12:57 +01:00
game->SetCurrent((HalfMove *)e.move);
2023-01-19 16:33:44 +01:00
SyncCache();
2022-02-23 18:11:55 +01:00
} else if (e.type == cgeditor::Event::Delete) {
2023-01-19 16:33:44 +01:00
game->DeleteMove((HalfMove *)e.move);
SyncCache();
2022-02-23 18:11:55 +01:00
} else if (e.type == cgeditor::Event::Promote) {
2023-01-01 14:12:57 +01:00
game->PromoteMove((HalfMove *)e.move);
2023-01-19 16:33:44 +01:00
SyncCache();
2022-02-23 18:11:55 +01:00
} else if (e.type == cgeditor::Event::SetAsMainline) {
2023-01-19 16:33:44 +01:00
game->SetMoveAsMainline((HalfMove *)e.move);
SyncCache();
2022-02-23 18:11:55 +01:00
}
2023-01-01 14:12:57 +01:00
wxCommandEvent event(GAME_CHANGE, GetId());
event.SetEventObject(this);
ProcessEvent(event);
2022-02-23 18:11:55 +01:00
}
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();
}
2023-01-01 17:30:55 +01:00
// Handle editor events
Update();
2023-01-02 08:15:51 +01:00
if(ProcessEvents()){
2022-02-23 18:11:55 +01:00
Refresh();
2023-01-01 17:30:55 +01:00
Update();
2022-02-23 18:11:55 +01:00
}
}
void EditorCanvas::SetMoves(HalfMove *moves, HalfMove *current) {
CGEditor::status.Moves = moves;
CGEditor::status.CurrentMove = current;
Refresh();
2023-06-05 18:55:14 +02:00
// Focus on current move:
if(current!=nullptr){
Update(); // Wait for preview call to Refresh() to finish (otherwise heisenbugs)
CGEditor::FocusOnMove(current);
Refresh();
}
2022-02-23 18:11:55 +01:00
}
void EditorCanvas::ApplyPreferences(){
CONFIG_OPEN(conf);
conf->SetPath("editor/");
CGEditor::status.MoveHeight=conf->Read("row_size", 50);
CGEditor::status.MoveWidth=conf->Read("col_size", 100);
CGEditor::status.UseMoveIcons=conf->Read("show_move_icons", true);
2023-01-09 15:22:31 +01:00
color_margin=wxColour(conf->Read("color_margin", "#F3F3F3"));
color_scrollbar=wxColour(conf->Read("color_scrollbar", "#838383"));
color_scrollbar_bg=wxColour(conf->Read("color_scrollbarbg", "#FFFFFF"));
color_comments_bg=wxColour(conf->Read("color_commentbg", "#ffffcc"));
CONFIG_CLOSE(conf);
Refresh();
}
2022-02-23 18:11:55 +01:00
wxBEGIN_EVENT_TABLE(EditorCanvas, wxPanel) EVT_PAINT(EditorCanvas::OnPaint)
2022-12-31 13:59:55 +01:00
EVT_MOUSE_EVENTS(EditorCanvas::MouseEvent) wxEND_EVENT_TABLE()