Improve drawing

This commit is contained in:
Loic Guegan 2022-12-28 10:37:57 +01:00
parent 35d9a6f0d9
commit 2151ccbe65
3 changed files with 21 additions and 16 deletions

View file

@ -14,7 +14,6 @@ BoardCanvas::BoardCanvas(wxFrame *parent)
ApplyPreferences(); ApplyPreferences();
// The following should be called when using an EVT_PAINT handler // The following should be called when using an EVT_PAINT handler
SetBackgroundStyle(wxBG_STYLE_PAINT); SetBackgroundStyle(wxBG_STYLE_PAINT);
//buffer=new wxBitmap(500,500,32);
} }
BoardCanvas::~BoardCanvas() { BoardCanvas::~BoardCanvas() {
@ -35,6 +34,8 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) {
wxBufferedPaintDC dc(this); wxBufferedPaintDC dc(this);
dc.SetBackground(*wxWHITE_BRUSH); dc.SetBackground(*wxWHITE_BRUSH);
dc.Clear(); dc.Clear();
// Setting up required attributes
REFRESH_MOUSE_LOCATION(); REFRESH_MOUSE_LOCATION();
square_width = t->GetSquaresSizes(); square_width = t->GetSquaresSizes();
canvas_size = dc.GetSize(); canvas_size = dc.GetSize();
@ -44,15 +45,12 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) {
boardX = 0; boardX = 0;
if (boardY > canvas_size.y) if (boardY > canvas_size.y)
boardY = 0; boardY = 0;
DrawBoard(dc);
// TODO: Make DrawBoard() drawing into wxMemoryDC!
/*wxMemoryDC dc2(*buffer);
dc2.SetBackgroundMode(wxTRANSPARENT);
dc2.SetPen(wxPen(*wxBLACK, 3));
dc2.DrawRectangle(10,10,100,100);
dc.Blit(0, 0, 500,500, (wxDC*)&dc2,0,0);*/
// Setup buffer (later use for animations)
buffer=new wxBitmap(canvas_size.x,canvas_size.y,32);
wxMemoryDC memDC(*buffer);
DrawBoard(memDC);
dc.Blit(0,0,canvas_size.x,canvas_size.y,(wxDC*)&memDC,0,0);
} }
void BoardCanvas::ApplyPreferences() { void BoardCanvas::ApplyPreferences() {
@ -85,7 +83,7 @@ void BoardCanvas::SetupBoard(std::string board, bool is_black_turn,
Refresh(); Refresh();
} }
void BoardCanvas::DrawBoard(wxBufferedPaintDC &dc) { void BoardCanvas::DrawBoard(wxDC &dc) {
std::uint32_t piece_width = t->GetPiecesSizes(); std::uint32_t piece_width = t->GetPiecesSizes();
std::uint32_t centrer_offset = (square_width - piece_width) / 2; std::uint32_t centrer_offset = (square_width - piece_width) / 2;

View file

@ -41,8 +41,13 @@ wxDECLARE_EVENT(NEXT_MOVE_EVENT, wxCommandEvent);
typedef std::tuple<short, short, short> ClockTime; typedef std::tuple<short, short, short> ClockTime;
class BoardCanvas : public wxPanel { class BoardCanvas : public wxPanel {
// *t is theme for board+pieces and
// *t_captures is theme for captured pieces (scale down version of t)
Theme *t, *t_captures; Theme *t, *t_captures;
// Board to draw (char version)
std::string board; std::string board;
// Various canvas state variables
bool black_side, is_dragging, valid_drag, is_black_turn; bool black_side, is_dragging, valid_drag, is_black_turn;
std::uint32_t boardX, boardY, square_width, mouseX, mouseY, lastClickX, std::uint32_t boardX, boardY, square_width, mouseX, mouseY, lastClickX,
lastClickY; lastClickY;
@ -52,6 +57,7 @@ class BoardCanvas : public wxPanel {
ClockTime black_time, white_time; ClockTime black_time, white_time;
bool frozen,lock_square_size; bool frozen,lock_square_size;
// Drawing buffer
wxBitmap *buffer; wxBitmap *buffer;
public: public:
@ -59,7 +65,7 @@ public:
BoardCanvas(wxFrame *parent,std::uint32_t square_width, bool frozen); BoardCanvas(wxFrame *parent,std::uint32_t square_width, bool frozen);
~BoardCanvas(); ~BoardCanvas();
void ApplyPreferences(); void ApplyPreferences();
void DrawBoard(wxBufferedPaintDC &dc); void DrawBoard(wxDC &dc);
void OnPaint(wxPaintEvent &event); void OnPaint(wxPaintEvent &event);
void OnKeyEvent(wxKeyEvent &event); void OnKeyEvent(wxKeyEvent &event);
void MouseEvent(wxMouseEvent &event); void MouseEvent(wxMouseEvent &event);

View file

@ -37,6 +37,7 @@ wxPoint EditorCanvas::Middle(cgeditor::Element e) {
} }
void EditorCanvas::DrawElement(const cgeditor::Element &e) { void EditorCanvas::DrawElement(const cgeditor::Element &e) {
// TODO: Optimize brush!!!! Always instanciated at each call
dc->SetPen(wxNullPen); dc->SetPen(wxNullPen);
dc->SetBrush(*wxRED_BRUSH); dc->SetBrush(*wxRED_BRUSH);
if (e.prop & cgeditor::Property::Rectangle) { if (e.prop & cgeditor::Property::Rectangle) {
@ -45,7 +46,7 @@ void EditorCanvas::DrawElement(const cgeditor::Element &e) {
} else if (e.prop & cgeditor::Property::Scrollbar) { } else if (e.prop & cgeditor::Property::Scrollbar) {
dc->SetBrush(*wxGREY_BRUSH); dc->SetBrush(*wxGREY_BRUSH);
} else if (e.prop & cgeditor::Property::Margin) { } else if (e.prop & cgeditor::Property::Margin) {
dc->SetBrush(*wxLIGHT_GREY_BRUSH); dc->SetBrush(wxBrush(wxColour(243,243,243)));
} else if (e.prop & cgeditor::Property::Button) { } else if (e.prop & cgeditor::Property::Button) {
if (e.prop & cgeditor::Property::On) { if (e.prop & cgeditor::Property::On) {
dc->DrawBitmap(hide_icon, e.x, e.y); dc->DrawBitmap(hide_icon, e.x, e.y);
@ -77,25 +78,25 @@ void EditorCanvas::DrawElement(const cgeditor::Element &e) {
} }
if (e.prop & cgeditor::Property::Current) { if (e.prop & cgeditor::Property::Current) {
wxRect recToDraw(e.x, e.y, e.width, e.height); wxRect recToDraw(e.x, e.y, e.width, e.height);
dc->SetBrush(*wxLIGHT_GREY_BRUSH); dc->SetBrush(wxBrush(wxColour(216, 216, 216)));
dc->DrawRectangle(recToDraw); dc->DrawRectangle(recToDraw);
} }
dc->DrawBitmap(*t.Get(p), e.x, y); dc->DrawBitmap(*t.Get(p), e.x, y);
} else if (e.prop & cgeditor::Property::Comment) { } else if (e.prop & cgeditor::Property::Comment) {
wxRect recToDraw(e.x, e.y, e.width, e.height); wxRect recToDraw(e.x, e.y, e.width, e.height);
dc->SetBrush(*wxYELLOW_BRUSH); dc->SetBrush(wxBrush(wxColour(255, 255, 204)));
dc->DrawRectangle(recToDraw); dc->DrawRectangle(recToDraw);
dc->DrawText(wxString(e.text), wxPoint(e.x, e.y)); dc->DrawText(wxString(e.text), wxPoint(e.x, e.y));
} else if (e.prop & cgeditor::Property::Menuitem) { } else if (e.prop & cgeditor::Property::Menuitem) {
wxRect recToDraw(e.x, e.y, e.width, e.height); wxRect recToDraw(e.x, e.y, e.width, e.height);
dc->SetBrush(*wxLIGHT_GREY_BRUSH); dc->SetBrush(wxBrush(wxColour(216, 216, 216)));
dc->DrawRectangle(recToDraw); dc->DrawRectangle(recToDraw);
dc->DrawText(wxString(e.text), wxPoint(e.x, Middle(e).y)); dc->DrawText(wxString(e.text), wxPoint(e.x, Middle(e).y));
} else { } else {
if (e.prop & cgeditor::Property::Move) { if (e.prop & cgeditor::Property::Move) {
if (e.prop & cgeditor::Property::Current) { if (e.prop & cgeditor::Property::Current) {
wxRect recToDraw(e.x, e.y, e.width, e.height); wxRect recToDraw(e.x, e.y, e.width, e.height);
dc->SetBrush(*wxLIGHT_GREY_BRUSH); dc->SetBrush(wxBrush(wxColour(216, 216, 216)));
dc->DrawRectangle(recToDraw); dc->DrawRectangle(recToDraw);
} }
if (CGEditor::status.UseMoveIcons) { if (CGEditor::status.UseMoveIcons) {