#include "ControlEditorDC.h" #include "Camera.h" #include "ConnectionLine.h" ControlEditorDC::ControlEditorDC(wxWindow* parent, int ioflags) : ControlEditor(parent) { BuildControlElementPanel(); m_camera = new Camera(); m_selectionRect = wxRect2DDouble(0, 0, 0, 0); m_ioFlags = ioflags; // Disconnect events from GLCanvas m_glCanvas->Disconnect(wxEVT_PAINT, wxPaintEventHandler(ControlEditor::OnPaint), nullptr, this); m_glCanvas->Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(ControlEditor::OnLeftClickDown), nullptr, this); m_glCanvas->Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(ControlEditor::OnLeftClickUp), nullptr, this); m_glCanvas->Disconnect(wxEVT_LEFT_DCLICK, wxMouseEventHandler(ControlEditor::OnDoubleClick), nullptr, this); m_glCanvas->Disconnect(wxEVT_MIDDLE_DOWN, wxMouseEventHandler(ControlEditor::OnMiddleDown), nullptr, this); m_glCanvas->Disconnect(wxEVT_MIDDLE_UP, wxMouseEventHandler(ControlEditor::OnMiddleUp), nullptr, this); m_glCanvas->Disconnect(wxEVT_MOTION, wxMouseEventHandler(ControlEditor::OnMouseMotion), nullptr, this); m_glCanvas->Disconnect(wxEVT_MOUSEWHEEL, wxMouseEventHandler(ControlEditor::OnScroll), nullptr, this); m_glCanvas->Disconnect(wxEVT_IDLE, wxIdleEventHandler(ControlEditor::OnIdle), nullptr, this); m_glCanvas->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(ControlEditor::OnKeyDown), nullptr, this); // Reconnect events to this m_panelWorkspace->Connect(wxEVT_PAINT, wxPaintEventHandler(ControlEditorDC::OnPaint), nullptr, this); // Connect to overloaded method m_panelWorkspace->Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(ControlEditor::OnLeftClickDown), nullptr, this); m_panelWorkspace->Connect(wxEVT_LEFT_UP, wxMouseEventHandler(ControlEditor::OnLeftClickUp), nullptr, this); m_panelWorkspace->Connect(wxEVT_LEFT_DCLICK, wxMouseEventHandler(ControlEditor::OnDoubleClick), nullptr, this); m_panelWorkspace->Connect(wxEVT_MIDDLE_DOWN, wxMouseEventHandler(ControlEditor::OnMiddleDown), nullptr, this); m_panelWorkspace->Connect(wxEVT_MIDDLE_UP, wxMouseEventHandler(ControlEditor::OnMiddleUp), nullptr, this); m_panelWorkspace->Connect(wxEVT_MOTION, wxMouseEventHandler(ControlEditor::OnMouseMotion), nullptr, this); m_panelWorkspace->Connect(wxEVT_MOUSEWHEEL, wxMouseEventHandler(ControlEditor::OnScroll), nullptr, this); m_panelWorkspace->Connect(wxEVT_IDLE, wxIdleEventHandler(ControlEditorDC::OnIdle), nullptr, this); // Connect to overloaded method m_panelWorkspace->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(ControlEditor::OnKeyDown), nullptr, this); m_panelWorkspace->GetSizer()->Remove(m_panelWorkspace->GetSizer()->GetChildren()[0]->GetId()); // remove m_glCanvas object from sizer delete m_glCanvas; // Delete GLcanvas to allow drawing with wxDC m_glCanvas = nullptr; if (m_glContext) delete m_glContext; m_glContext = nullptr; m_panelWorkspace->SetBackgroundColour(wxColour(255, 255, 255)); m_panelWorkspace->SetBackgroundStyle(wxBG_STYLE_PAINT); // To allow wxBufferedPaintDC works properly. Redraw(); } ControlEditorDC::~ControlEditorDC() { // Recreate the GLCanvas. This is necessary to prevent WorkspaceBase destructor access nullptr. // Also avoid the code editing of WorkspaceBase, since is automatically generated by wxCrafter. // TODO(?): Find a better way to solve this problem. m_glCanvas = new wxGLCanvas(this); } void ControlEditorDC::OnPaint(wxPaintEvent& event) { wxBufferedPaintDC dc(m_panelWorkspace); dc.Clear(); wxGraphicsContext* gc = wxGraphicsContext::Create(dc); // Draw if (gc) { gc->Scale(m_camera->GetScale(), m_camera->GetScale()); gc->Translate(m_camera->GetTranslation().m_x, m_camera->GetTranslation().m_y); for (auto line : m_connectionList) { //ConnectionLine* line = *it; line->DrawDC(m_camera->GetTranslation(), m_camera->GetScale(), gc); } for (auto element : m_elementList) { element->DrawDC(m_camera->GetTranslation(), m_camera->GetScale(), gc); } // Selection rectangle gc->SetPen(wxPen(wxColour(0, 125, 255, 255))); gc->SetBrush(wxBrush(wxColour(0, 125, 255, 125))); gc->DrawRectangle(m_selectionRect.m_x, m_selectionRect.m_y, m_selectionRect.m_width, m_selectionRect.m_height); delete gc; } event.Skip(); }