From 9e12b480ce2a4e5d2b33bd5bc25e4d905e3bc58f Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Thu, 26 Nov 2020 12:07:41 -0800 Subject: [PATCH] Moved DrawBackground to lcGLWidget. --- common/lc_glwidget.cpp | 116 ++++++++++++++++++++++++++++++++++++ common/lc_glwidget.h | 3 +- common/lc_model.cpp | 51 ---------------- common/lc_model.h | 1 - common/lc_previewwidget.cpp | 2 +- common/minifig.cpp | 2 +- common/view.cpp | 2 +- leocad.pro | 1 + qt/lc_qglwidget.cpp | 66 -------------------- 9 files changed, 122 insertions(+), 122 deletions(-) create mode 100644 common/lc_glwidget.cpp diff --git a/common/lc_glwidget.cpp b/common/lc_glwidget.cpp new file mode 100644 index 00000000..a6827c81 --- /dev/null +++ b/common/lc_glwidget.cpp @@ -0,0 +1,116 @@ +#include "lc_global.h" +#include "lc_glwidget.h" +#include "lc_application.h" + +void lcGLWidget::MakeCurrent() +{ + mWidget->makeCurrent(); +} + +void lcGLWidget::Redraw() +{ + mWidget->update(); +} + +void lcGLWidget::SetCursor(lcCursor CursorType) +{ + if (mCursor == CursorType) + return; + + struct lcCursorInfo + { + int x, y; + const char* Name; + }; + + const lcCursorInfo Cursors[] = + { + { 0, 0, "" }, // lcCursor::Default + { 8, 3, ":/resources/cursor_insert" }, // lcCursor::Brick + { 15, 15, ":/resources/cursor_light" }, // lcCursor::Light + { 7, 10, ":/resources/cursor_spotlight" }, // lcCursor::Spotlight + { 15, 9, ":/resources/cursor_camera" }, // lcCursor::Camera + { 0, 2, ":/resources/cursor_select" }, // lcCursor::Select + { 0, 2, ":/resources/cursor_select_add" }, // lcCursor::SelectAdd + { 0, 2, ":/resources/cursor_select_remove" }, // lcCursor::SelectRemove + { 15, 15, ":/resources/cursor_move" }, // lcCursor::Move + { 15, 15, ":/resources/cursor_rotate" }, // lcCursor::Rotate + { 15, 15, ":/resources/cursor_rotatex" }, // lcCursor::RotateX + { 15, 15, ":/resources/cursor_rotatey" }, // lcCursor::RotateY + { 0, 10, ":/resources/cursor_delete" }, // lcCursor::Delete + { 14, 14, ":/resources/cursor_paint" }, // lcCursor::Paint + { 1, 13, ":/resources/cursor_color_picker" }, // lcCursor::ColorPicker + { 15, 15, ":/resources/cursor_zoom" }, // lcCursor::Zoom + { 9, 9, ":/resources/cursor_zoom_region" }, // lcCursor::ZoomRegion + { 15, 15, ":/resources/cursor_pan" }, // lcCursor::Pan + { 15, 15, ":/resources/cursor_roll" }, // lcCursor::Roll + { 15, 15, ":/resources/cursor_rotate_view" }, // lcCursor::RotateView + }; + + static_assert(LC_ARRAY_COUNT(Cursors) == static_cast(lcCursor::Count), "Array size mismatch"); + + QGLWidget* widget = (QGLWidget*)mWidget; + + if (CursorType > lcCursor::Default && CursorType < lcCursor::Count) + { + const lcCursorInfo& Cursor = Cursors[static_cast(CursorType)]; + widget->setCursor(QCursor(QPixmap(Cursor.Name), Cursor.x, Cursor.y)); + mCursor = CursorType; + } + else + { + widget->unsetCursor(); + mCursor = lcCursor::Default; + } +} + +void lcGLWidget::DrawBackground() const +{ + const lcPreferences& Preferences = lcGetPreferences(); + + if (!Preferences.mBackgroundGradient) + { + lcVector3 BackgroundColor = lcVector3FromColor(Preferences.mBackgroundSolidColor); + glClearColor(BackgroundColor[0], BackgroundColor[1], BackgroundColor[2], 0.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + return; + } + + lcContext* Context = mContext; + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + Context->SetDepthWrite(false); + glDisable(GL_DEPTH_TEST); + + float ViewWidth = (float)mWidth; + float ViewHeight = (float)mHeight; + + Context->SetWorldMatrix(lcMatrix44Identity()); + Context->SetViewMatrix(lcMatrix44Translation(lcVector3(0.375, 0.375, 0.0))); + Context->SetProjectionMatrix(lcMatrix44Ortho(0.0f, ViewWidth, 0.0f, ViewHeight, -1.0f, 1.0f)); + + Context->SetSmoothShading(true); + + const lcVector3 Color1 = lcVector3FromColor(Preferences.mBackgroundGradientColorTop); + const lcVector3 Color2 = lcVector3FromColor(Preferences.mBackgroundGradientColorBottom); + + float Verts[] = + { + ViewWidth, ViewHeight, Color1[0], Color1[1], Color1[2], 1.0f, + 0.0f, ViewHeight, Color1[0], Color1[1], Color1[2], 1.0f, + 0.0f, 0.0f, Color2[0], Color2[1], Color2[2], 1.0f, + ViewWidth, 0.0f, Color2[0], Color2[1], Color2[2], 1.0f + }; + + Context->SetMaterial(lcMaterialType::UnlitVertexColor); + Context->SetVertexBufferPointer(Verts); + Context->SetVertexFormat(0, 2, 0, 0, 4, false); + + Context->DrawPrimitives(GL_TRIANGLE_FAN, 0, 4); + + Context->SetSmoothShading(false); + + glEnable(GL_DEPTH_TEST); + Context->SetDepthWrite(true); +} diff --git a/common/lc_glwidget.h b/common/lc_glwidget.h index d3be8429..bd334e1e 100644 --- a/common/lc_glwidget.h +++ b/common/lc_glwidget.h @@ -75,6 +75,7 @@ public: void MakeCurrent(); void Redraw(); void SetCursor(lcCursor Cursor); + void DrawBackground() const; virtual void OnDraw() { } virtual void OnInitialUpdate() { } @@ -100,7 +101,7 @@ public: int mWidth; int mHeight; lcCursor mCursor; - void* mWidget; + QGLWidget* mWidget; lcContext* mContext; bool mDeleteContext; }; diff --git a/common/lc_model.cpp b/common/lc_model.cpp index 94af6e81..6b830fd2 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -1257,57 +1257,6 @@ void lcModel::AddSubModelRenderMeshes(lcScene& Scene, const lcMatrix44& WorldMat Piece->AddSubModelRenderMeshes(Scene, WorldMatrix, DefaultColorIndex, RenderMeshState, ParentActive); } -void lcModel::DrawBackground(lcGLWidget* Widget) -{ - const lcPreferences& Preferences = lcGetPreferences(); - - if (!Preferences.mBackgroundGradient) - { - lcVector3 BackgroundColor = lcVector3FromColor(Preferences.mBackgroundSolidColor); - glClearColor(BackgroundColor[0], BackgroundColor[1], BackgroundColor[2], 0.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - return; - } - - lcContext* Context = Widget->mContext; - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - Context->SetDepthWrite(false); - glDisable(GL_DEPTH_TEST); - - float ViewWidth = (float)Widget->mWidth; - float ViewHeight = (float)Widget->mHeight; - - Context->SetWorldMatrix(lcMatrix44Identity()); - Context->SetViewMatrix(lcMatrix44Translation(lcVector3(0.375, 0.375, 0.0))); - Context->SetProjectionMatrix(lcMatrix44Ortho(0.0f, ViewWidth, 0.0f, ViewHeight, -1.0f, 1.0f)); - - Context->SetSmoothShading(true); - - const lcVector3 Color1 = lcVector3FromColor(Preferences.mBackgroundGradientColorTop); - const lcVector3 Color2 = lcVector3FromColor(Preferences.mBackgroundGradientColorBottom); - - float Verts[] = - { - ViewWidth, ViewHeight, Color1[0], Color1[1], Color1[2], 1.0f, - 0.0f, ViewHeight, Color1[0], Color1[1], Color1[2], 1.0f, - 0.0f, 0.0f, Color2[0], Color2[1], Color2[2], 1.0f, - ViewWidth, 0.0f, Color2[0], Color2[1], Color2[2], 1.0f - }; - - Context->SetMaterial(lcMaterialType::UnlitVertexColor); - Context->SetVertexBufferPointer(Verts); - Context->SetVertexFormat(0, 2, 0, 0, 4, false); - - Context->DrawPrimitives(GL_TRIANGLE_FAN, 0, 4); - - Context->SetSmoothShading(false); - - glEnable(GL_DEPTH_TEST); - Context->SetDepthWrite(true); -} - QImage lcModel::GetStepImage(bool Zoom, int Width, int Height, lcStep Step) { View* ActiveView = gMainWindow->GetActiveView(); diff --git a/common/lc_model.h b/common/lc_model.h index 47e26846..7cb39115 100644 --- a/common/lc_model.h +++ b/common/lc_model.h @@ -236,7 +236,6 @@ public: void GetScene(lcScene& Scene, lcCamera* ViewCamera, bool AllowHighlight, bool AllowFade) const; void AddSubModelRenderMeshes(lcScene& Scene, const lcMatrix44& WorldMatrix, int DefaultColorIndex, lcRenderMeshState RenderMeshState, bool ParentActive) const; - void DrawBackground(lcGLWidget* Widget); QImage GetStepImage(bool Zoom, int Width, int Height, lcStep Step); QImage GetPartsListImage(int MaxWidth, lcStep Step) const; void SaveStepImages(const QString& BaseName, bool AddStepSuffix, bool Zoom, int Width, int Height, lcStep Start, lcStep End); diff --git a/common/lc_previewwidget.cpp b/common/lc_previewwidget.cpp index e21109cb..1e84bfcc 100644 --- a/common/lc_previewwidget.cpp +++ b/common/lc_previewwidget.cpp @@ -495,7 +495,7 @@ void lcPreviewWidget::OnDraw() mContext->SetViewport(0, 0, mWidth, mHeight); - mModel->DrawBackground(this); + DrawBackground(); mContext->SetProjectionMatrix(GetProjectionMatrix()); diff --git a/common/minifig.cpp b/common/minifig.cpp index 6d784452..563fe984 100644 --- a/common/minifig.cpp +++ b/common/minifig.cpp @@ -326,7 +326,7 @@ void MinifigWizard::OnDraw() const float Aspect = (float)mWidth/(float)mHeight; mContext->SetViewport(0, 0, mWidth, mHeight); - lcGetActiveModel()->DrawBackground(this); + DrawBackground(); lcVector3 Min(FLT_MAX, FLT_MAX, FLT_MAX), Max(-FLT_MAX, -FLT_MAX, -FLT_MAX); diff --git a/common/view.cpp b/common/view.cpp index dba45fe3..31f0a980 100644 --- a/common/view.cpp +++ b/common/view.cpp @@ -864,7 +864,7 @@ void View::OnDraw() mContext->SetDefaultState(); mContext->SetViewport(0, 0, mWidth, mHeight); - mModel->DrawBackground(this); + DrawBackground(); int CurrentTileWidth, CurrentTileHeight; diff --git a/leocad.pro b/leocad.pro index 52068f0e..7dcc83c6 100644 --- a/leocad.pro +++ b/leocad.pro @@ -187,6 +187,7 @@ SOURCES += common/view.cpp \ common/lc_context.cpp \ common/lc_file.cpp \ common/lc_glextensions.cpp \ + common/lc_glwidget.cpp \ common/lc_http.cpp \ common/lc_library.cpp \ common/lc_lxf.cpp \ diff --git a/qt/lc_qglwidget.cpp b/qt/lc_qglwidget.cpp index a0a136fc..2279f595 100644 --- a/qt/lc_qglwidget.cpp +++ b/qt/lc_qglwidget.cpp @@ -19,72 +19,6 @@ static QList gWidgetList; -void lcGLWidget::MakeCurrent() -{ - QGLWidget* Widget = (QGLWidget*)mWidget; - - Widget->makeCurrent(); -} - -void lcGLWidget::Redraw() -{ - lcQGLWidget* Widget = (lcQGLWidget*)mWidget; - - Widget->update(); -} - -void lcGLWidget::SetCursor(lcCursor CursorType) -{ - if (mCursor == CursorType) - return; - - struct lcCursorInfo - { - int x, y; - const char* Name; - }; - - const lcCursorInfo Cursors[] = - { - { 0, 0, "" }, // lcCursor::Default - { 8, 3, ":/resources/cursor_insert" }, // lcCursor::Brick - { 15, 15, ":/resources/cursor_light" }, // lcCursor::Light - { 7, 10, ":/resources/cursor_spotlight" }, // lcCursor::Spotlight - { 15, 9, ":/resources/cursor_camera" }, // lcCursor::Camera - { 0, 2, ":/resources/cursor_select" }, // lcCursor::Select - { 0, 2, ":/resources/cursor_select_add" }, // lcCursor::SelectAdd - { 0, 2, ":/resources/cursor_select_remove" }, // lcCursor::SelectRemove - { 15, 15, ":/resources/cursor_move" }, // lcCursor::Move - { 15, 15, ":/resources/cursor_rotate" }, // lcCursor::Rotate - { 15, 15, ":/resources/cursor_rotatex" }, // lcCursor::RotateX - { 15, 15, ":/resources/cursor_rotatey" }, // lcCursor::RotateY - { 0, 10, ":/resources/cursor_delete" }, // lcCursor::Delete - { 14, 14, ":/resources/cursor_paint" }, // lcCursor::Paint - { 1, 13, ":/resources/cursor_color_picker" }, // lcCursor::ColorPicker - { 15, 15, ":/resources/cursor_zoom" }, // lcCursor::Zoom - { 9, 9, ":/resources/cursor_zoom_region" }, // lcCursor::ZoomRegion - { 15, 15, ":/resources/cursor_pan" }, // lcCursor::Pan - { 15, 15, ":/resources/cursor_roll" }, // lcCursor::Roll - { 15, 15, ":/resources/cursor_rotate_view" }, // lcCursor::RotateView - }; - - static_assert(LC_ARRAY_COUNT(Cursors) == static_cast(lcCursor::Count), "Array size mismatch"); - - QGLWidget* widget = (QGLWidget*)mWidget; - - if (CursorType != lcCursor::Default && CursorType < lcCursor::Count) - { - const lcCursorInfo& Cursor = Cursors[static_cast(CursorType)]; - widget->setCursor(QCursor(QPixmap(Cursor.Name), Cursor.x, Cursor.y)); - mCursor = CursorType; - } - else - { - widget->unsetCursor(); - mCursor = lcCursor::Default; - } -} - lcQGLWidget::lcQGLWidget(QWidget* Parent, lcGLWidget* Owner) : QGLWidget(Parent, gWidgetList.isEmpty() ? nullptr : gWidgetList.first()) {