From 3f3735ac7a85da0d2a4baca17ffb7aea427a1978 Mon Sep 17 00:00:00 2001 From: parax0 Date: Tue, 30 Aug 2016 00:47:02 -0600 Subject: [PATCH] Created application class to manage editor windows, improved camera AABox orbit --- src/Core/Render/CCamera.cpp | 15 ++------ src/Core/Render/CCamera.h | 2 +- src/Editor/CEditorApplication.cpp | 37 +++++++++++++++++++ src/Editor/CEditorApplication.h | 33 +++++++++++++++++ src/Editor/CProjectOverviewDialog.cpp | 4 +- .../CharacterEditor/CCharacterEditor.cpp | 27 +++++++------- src/Editor/CharacterEditor/CCharacterEditor.h | 11 +++--- src/Editor/Editor.pro | 7 +++- src/Editor/IEditor.h | 21 +++++++++++ src/Editor/INodeEditor.cpp | 2 +- src/Editor/INodeEditor.h | 3 +- src/Editor/ModelEditor/CModelEditorWindow.cpp | 11 +++--- src/Editor/ModelEditor/CModelEditorWindow.h | 5 ++- src/Editor/WorldEditor/CWorldEditor.cpp | 6 +-- src/Editor/WorldEditor/CWorldEditor.h | 1 - src/Editor/main.cpp | 4 +- 16 files changed, 137 insertions(+), 52 deletions(-) create mode 100644 src/Editor/CEditorApplication.cpp create mode 100644 src/Editor/CEditorApplication.h create mode 100644 src/Editor/IEditor.h diff --git a/src/Core/Render/CCamera.cpp b/src/Core/Render/CCamera.cpp index 93724f36..4a2da17d 100644 --- a/src/Core/Render/CCamera.cpp +++ b/src/Core/Render/CCamera.cpp @@ -154,21 +154,12 @@ void CCamera::SetOrbit(const CVector3f& OrbitTarget, float Distance) } } -void CCamera::SetOrbit(const CAABox& OrbitTarget, float DistScale /*= 4.f*/) +void CCamera::SetOrbit(const CAABox& OrbitTarget, float DistScale /*= 1.75f*/) { - CVector3f Min = OrbitTarget.Min(); - CVector3f Max = OrbitTarget.Max(); - mOrbitTarget = OrbitTarget.Center(); - // Find largest extent - CVector3f Extent = (Max - Min) / 2.f; - float Dist = 0.f; - - if (Extent.X >= Extent.Y && Extent.X >= Extent.Z) Dist = Extent.X; - else if (Extent.Y >= Extent.X && Extent.Y >= Extent.Z) Dist = Extent.Y; - else Dist = Extent.Z; - + // Determine orbit radius, which should be enough to cover the entire box with some buffer room + float Dist = OrbitTarget.Center().Distance(OrbitTarget.Max()); mOrbitDistance = Dist * DistScale; if (mMode == eOrbitCamera) diff --git a/src/Core/Render/CCamera.h b/src/Core/Render/CCamera.h index 54edba7f..ef2ce501 100644 --- a/src/Core/Render/CCamera.h +++ b/src/Core/Render/CCamera.h @@ -64,7 +64,7 @@ public: void SetMoveMode(ECameraMoveMode Mode); void SetOrbit(const CVector3f& rkOrbitTarget, float Distance); - void SetOrbit(const CAABox& rkOrbitTarget, float DistScale = 4.f); + void SetOrbit(const CAABox& rkOrbitTarget, float DistScale = 1.75f); void SetOrbitTarget(const CVector3f& rkOrbitTarget); void SetOrbitDistance(float Distance); diff --git a/src/Editor/CEditorApplication.cpp b/src/Editor/CEditorApplication.cpp new file mode 100644 index 00000000..39f1d456 --- /dev/null +++ b/src/Editor/CEditorApplication.cpp @@ -0,0 +1,37 @@ +#include "CEditorApplication.h" +#include "CEditorUpdateEvent.h" +#include "IEditor.h" +#include "CBasicViewport.h" +#include + +CEditorApplication::CEditorApplication(int& rArgc, char **ppArgv) + : QApplication(rArgc, ppArgv) +{ + mLastUpdate = CTimer::GlobalTime(); + + connect(&mRefreshTimer, SIGNAL(timeout()), this, SLOT(TickEditors())); + mRefreshTimer.start(8); +} + +void CEditorApplication::TickEditors() +{ + double LastUpdate = mLastUpdate; + mLastUpdate = CTimer::GlobalTime(); + double DeltaTime = mLastUpdate - LastUpdate; + + foreach(IEditor *pEditor, mEditorWindows) + { + if (pEditor->isVisible()) + { + pEditor->EditorTick((float) DeltaTime); + + CBasicViewport *pViewport = pEditor->Viewport(); + + if (pViewport && pViewport->isVisible()) + { + pViewport->ProcessInput(); + pViewport->Render(); + } + } + } +} diff --git a/src/Editor/CEditorApplication.h b/src/Editor/CEditorApplication.h new file mode 100644 index 00000000..525bf022 --- /dev/null +++ b/src/Editor/CEditorApplication.h @@ -0,0 +1,33 @@ +#ifndef CEDITORAPPLICATION_H +#define CEDITORAPPLICATION_H + +#include +#include +#include + +class CBasicViewport; +class IEditor; + +class CEditorApplication : public QApplication +{ + Q_OBJECT + + QTimer mRefreshTimer; + QVector mEditorWindows; + double mLastUpdate; + +public: + CEditorApplication(int& rArgc, char **ppArgv); + +public slots: + void TickEditors(); + + // Accessors +public: + inline void AddEditor(IEditor *pEditor) { mEditorWindows << pEditor; } + inline void RemoveEditor(IEditor *pEditor) { mEditorWindows.removeOne(pEditor); } +}; + +#define gpEdApp static_cast(qApp) + +#endif // CEDITORAPPLICATION_H diff --git a/src/Editor/CProjectOverviewDialog.cpp b/src/Editor/CProjectOverviewDialog.cpp index 7671e4ea..20d63b3b 100644 --- a/src/Editor/CProjectOverviewDialog.cpp +++ b/src/Editor/CProjectOverviewDialog.cpp @@ -181,8 +181,8 @@ void CProjectOverviewDialog::LaunchEditor() void CProjectOverviewDialog::LaunchResourceBrowser() { - CResourceBrowser Browser(this); - Browser.exec(); + CResourceBrowser *pBrowser = new CResourceBrowser(mpWorldEditor); + pBrowser->show(); } void CProjectOverviewDialog::CookPackage() diff --git a/src/Editor/CharacterEditor/CCharacterEditor.cpp b/src/Editor/CharacterEditor/CCharacterEditor.cpp index 5e386984..a7ce5f55 100644 --- a/src/Editor/CharacterEditor/CCharacterEditor.cpp +++ b/src/Editor/CharacterEditor/CCharacterEditor.cpp @@ -11,15 +11,15 @@ const CVector3f CCharacterEditor::skDefaultOrbitTarget = CVector3f(0,0,1); const float CCharacterEditor::skDefaultOrbitDistance = 4.f; CCharacterEditor::CCharacterEditor(QWidget *parent) - : QMainWindow(parent) + : IEditor(parent) , ui(new Ui::CCharacterEditor) , mpScene(new CScene()) , mpCharNode(new CCharacterNode(mpScene, -1)) , mpSelectedBone(nullptr) , mBindPose(false) - , mAnimTime(0.f) , mPlayAnim(true) , mLoopAnim(true) + , mAnimTime(0.f) , mPlaybackSpeed(1.f) { ui->setupUi(this); @@ -43,9 +43,6 @@ CCharacterEditor::CCharacterEditor(QWidget *parent) mpAnimComboBox->setMinimumWidth(175); ui->ToolBar->addWidget(mpAnimComboBox); - connect(&mRefreshTimer, SIGNAL(timeout()), this, SLOT(RefreshViewport())); - mRefreshTimer.start(0); - connect(ui->Viewport, SIGNAL(HoverBoneChanged(u32)), this, SLOT(OnViewportHoverBoneChanged(u32))); connect(ui->Viewport, SIGNAL(ViewportClick(QMouseEvent*)), this, SLOT(OnViewportClick())); connect(ui->ActionOpen, SIGNAL(triggered()), this, SLOT(Open())); @@ -84,12 +81,14 @@ CCharacterEditor::~CCharacterEditor() delete ui; } -void CCharacterEditor::UpdateAnimTime() +void CCharacterEditor::EditorTick(float DeltaTime) { - double Time = CTimer::GlobalTime(); - double DeltaTime = Time - mLastAnimUpdate; - mLastAnimUpdate = Time; + UpdateAnimTime(DeltaTime); + UpdateCameraOrbit(); +} +void CCharacterEditor::UpdateAnimTime(float DeltaTime) +{ CAnimation *pAnim = CurrentAnimation(); if (pAnim && mPlayAnim && !mBindPose && !ui->AnimSlider->isSliderDown()) @@ -216,7 +215,7 @@ void CCharacterEditor::SetActiveAnimSet(CAnimSet *pSet) ui->SkeletonHierarchyTreeView->selectionModel()->setCurrentIndex( mSkeletonModel.index(0, 0, RootIndex), QItemSelectionModel::ClearAndSelect ); // Run CCamera::SetOrbit to reset orbit distance. - ui->Viewport->Camera().SetOrbit(mpCharNode->AABox(), 4.f); + ui->Viewport->Camera().SetOrbit(mpCharNode->AABox()); } void CCharacterEditor::SetSelectedBone(CBone *pBone) @@ -229,6 +228,11 @@ void CCharacterEditor::SetSelectedBone(CBone *pBone) } } +CCharacterEditorViewport* CCharacterEditor::Viewport() const +{ + return ui->Viewport; +} + // ************ PUBLIC SLOTS ************ void CCharacterEditor::Open() { @@ -291,8 +295,6 @@ void CCharacterEditor::ToggleOrbit(bool Enable) void CCharacterEditor::RefreshViewport() { - UpdateAnimTime(); - UpdateCameraOrbit(); ui->Viewport->ProcessInput(); ui->Viewport->Render(); } @@ -341,7 +343,6 @@ void CCharacterEditor::SetActiveAnimation(int AnimIndex) { mCurrentAnim = AnimIndex; mpCharNode->SetActiveAnim((u32) AnimIndex); - mLastAnimUpdate = CTimer::GlobalTime(); ui->AnimSlider->blockSignals(true); ui->AnimSlider->setMaximum((int) (CurrentAnimation() ? CurrentAnimation()->Duration() * 1000 : 0)); diff --git a/src/Editor/CharacterEditor/CCharacterEditor.h b/src/Editor/CharacterEditor/CCharacterEditor.h index 2d42e8c8..ef72d84d 100644 --- a/src/Editor/CharacterEditor/CCharacterEditor.h +++ b/src/Editor/CharacterEditor/CCharacterEditor.h @@ -1,6 +1,7 @@ #ifndef CCHARACTEREDITOR_H #define CCHARACTEREDITOR_H +#include "IEditor.h" #include "CCharacterEditorViewport.h" #include "CSkeletonHierarchyModel.h" #include @@ -14,7 +15,7 @@ namespace Ui { class CCharacterEditor; } -class CCharacterEditor : public QMainWindow +class CCharacterEditor : public IEditor { Q_OBJECT @@ -26,7 +27,6 @@ class CCharacterEditor : public QMainWindow CSkeletonHierarchyModel mSkeletonModel; QComboBox *mpCharComboBox; QComboBox *mpAnimComboBox; - QTimer mRefreshTimer; TResPtr mpSet; u32 mCurrentChar; @@ -34,10 +34,9 @@ class CCharacterEditor : public QMainWindow bool mBindPose; // Playback Controls - double mLastAnimUpdate; - float mAnimTime; bool mPlayAnim; bool mLoopAnim; + float mAnimTime; float mPlaybackSpeed; // Constants @@ -47,12 +46,14 @@ class CCharacterEditor : public QMainWindow public: explicit CCharacterEditor(QWidget *parent = 0); ~CCharacterEditor(); - void UpdateAnimTime(); + void EditorTick(float DeltaTime); + void UpdateAnimTime(float DeltaTime); void UpdateCameraOrbit(); CSkeleton* CurrentSkeleton() const; CAnimation* CurrentAnimation() const; void SetActiveAnimSet(CAnimSet *pSet); void SetSelectedBone(CBone *pBone); + CCharacterEditorViewport* Viewport() const; public slots: void Open(); diff --git a/src/Editor/Editor.pro b/src/Editor/Editor.pro index e4cdeae1..126c4a85 100644 --- a/src/Editor/Editor.pro +++ b/src/Editor/Editor.pro @@ -168,7 +168,9 @@ HEADERS += \ ResourceBrowser/CResourceBrowser.h \ ResourceBrowser/CResourceTableModel.h \ ResourceBrowser/CResourceProxyModel.h \ - ResourceBrowser/CVirtualDirectoryModel.h + ResourceBrowser/CVirtualDirectoryModel.h \ + CEditorApplication.h \ + IEditor.h # Source Files SOURCES += \ @@ -230,7 +232,8 @@ SOURCES += \ CharacterEditor/CCharacterEditorViewport.cpp \ CharacterEditor/CSkeletonHierarchyModel.cpp \ CProjectOverviewDialog.cpp \ - ResourceBrowser/CResourceBrowser.cpp + ResourceBrowser/CResourceBrowser.cpp \ + CEditorApplication.cpp # UI Files FORMS += \ diff --git a/src/Editor/IEditor.h b/src/Editor/IEditor.h new file mode 100644 index 00000000..b54e3581 --- /dev/null +++ b/src/Editor/IEditor.h @@ -0,0 +1,21 @@ +#ifndef IEDITOR +#define IEDITOR + +#include +#include "CEditorApplication.h" + +class IEditor : public QMainWindow +{ +public: + IEditor(QWidget *pParent) + : QMainWindow(pParent) + { + gpEdApp->AddEditor(this); + } + + virtual void EditorTick(float /*DeltaTime*/) { } + virtual CBasicViewport* Viewport() const { return nullptr; } +}; + +#endif // IEDITOR + diff --git a/src/Editor/INodeEditor.cpp b/src/Editor/INodeEditor.cpp index d6713d27..21c933f1 100644 --- a/src/Editor/INodeEditor.cpp +++ b/src/Editor/INodeEditor.cpp @@ -4,7 +4,7 @@ #include INodeEditor::INodeEditor(QWidget *pParent) - : QMainWindow(pParent) + : IEditor(pParent) , mPickMode(false) , mpSelection(new CNodeSelection) , mSelectionLocked(false) diff --git a/src/Editor/INodeEditor.h b/src/Editor/INodeEditor.h index 152defdd..f95369d3 100644 --- a/src/Editor/INodeEditor.h +++ b/src/Editor/INodeEditor.h @@ -1,6 +1,7 @@ #ifndef INODEEDITOR_H #define INODEEDITOR_H +#include "IEditor.h" #include "CGizmo.h" #include "CNodeSelection.h" #include @@ -13,7 +14,7 @@ #include #include -class INodeEditor : public QMainWindow +class INodeEditor : public IEditor { Q_OBJECT diff --git a/src/Editor/ModelEditor/CModelEditorWindow.cpp b/src/Editor/ModelEditor/CModelEditorWindow.cpp index 4727123a..01c3b817 100644 --- a/src/Editor/ModelEditor/CModelEditorWindow.cpp +++ b/src/Editor/ModelEditor/CModelEditorWindow.cpp @@ -23,7 +23,7 @@ #include CModelEditorWindow::CModelEditorWindow(QWidget *pParent) - : QMainWindow(pParent) + : IEditor(pParent) , ui(new Ui::CModelEditorWindow) , mpScene(new CScene()) , mpCurrentMat(nullptr) @@ -54,10 +54,6 @@ CModelEditorWindow::CModelEditorWindow(QWidget *pParent) ui->PassTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents); ui->ClearColorPicker->SetColor(QColor(76, 76, 76, 255)); - // Viewport Signal/Slot setup - connect(&mRefreshTimer, SIGNAL(timeout()), this, SLOT(RefreshViewport())); - mRefreshTimer.start(0); - // Editor UI Signal/Slot setup ui->SetSelectionComboBox->setProperty ("ModelEditorWidgetType", eSetSelectComboBox); ui->MatSelectionComboBox->setProperty ("ModelEditorWidgetType", eMatSelectComboBox); @@ -895,3 +891,8 @@ void CModelEditorWindow::closeEvent(QCloseEvent*) { emit Closed(); } + +CModelEditorViewport* CModelEditorWindow::Viewport() const +{ + return ui->Viewport; +} diff --git a/src/Editor/ModelEditor/CModelEditorWindow.h b/src/Editor/ModelEditor/CModelEditorWindow.h index 43210180..7bd0b842 100644 --- a/src/Editor/ModelEditor/CModelEditorWindow.h +++ b/src/Editor/ModelEditor/CModelEditorWindow.h @@ -1,6 +1,7 @@ #ifndef CMODELEDITORWINDOW_H #define CMODELEDITORWINDOW_H +#include "IEditor.h" #include "CModelEditorViewport.h" #include #include @@ -16,7 +17,7 @@ namespace Ui { class CModelEditorWindow; } -class CModelEditorWindow : public QMainWindow +class CModelEditorWindow : public IEditor { Q_OBJECT @@ -28,13 +29,13 @@ class CModelEditorWindow : public QMainWindow CMaterial *mpCurrentMat; CMaterialPass *mpCurrentPass; bool mIgnoreSignals; - QTimer mRefreshTimer; public: explicit CModelEditorWindow(QWidget *pParent = 0); ~CModelEditorWindow(); void SetActiveModel(CModel *pModel); void closeEvent(QCloseEvent *pEvent); + CModelEditorViewport* Viewport() const; public slots: void RefreshViewport(); diff --git a/src/Editor/WorldEditor/CWorldEditor.cpp b/src/Editor/WorldEditor/CWorldEditor.cpp index d0b1f631..c367bec6 100644 --- a/src/Editor/WorldEditor/CWorldEditor.cpp +++ b/src/Editor/WorldEditor/CWorldEditor.cpp @@ -46,10 +46,6 @@ CWorldEditor::CWorldEditor(QWidget *parent) mpSelection->SetAllowedNodeTypes(eScriptNode | eLightNode); - // Start refresh timer - connect(&mRefreshTimer, SIGNAL(timeout()), this, SLOT(RefreshViewport())); - mRefreshTimer.start(0); - // Initialize splitter QList SplitterSizes; SplitterSizes << width() * 0.775 << width() * 0.225; @@ -912,7 +908,7 @@ void CWorldEditor::UpdateCameraOrbit() if (!mpSelection->IsEmpty()) pCamera->SetOrbit(mpSelection->Bounds()); else if (mpArea) - pCamera->SetOrbit(mpArea->AABox(), 1.5f); + pCamera->SetOrbit(mpArea->AABox(), 1.2f); } void CWorldEditor::OnCameraSpeedChange(double Speed) diff --git a/src/Editor/WorldEditor/CWorldEditor.h b/src/Editor/WorldEditor/CWorldEditor.h index a38d413c..898308ec 100644 --- a/src/Editor/WorldEditor/CWorldEditor.h +++ b/src/Editor/WorldEditor/CWorldEditor.h @@ -37,7 +37,6 @@ class CWorldEditor : public INodeEditor TResPtr mpWorld; TResPtr mpArea; - QTimer mRefreshTimer; CLinkDialog *mpLinkDialog; CPoiMapEditDialog *mpPoiDialog; diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index 99b0782d..f5621bbf 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -1,4 +1,4 @@ -#include "CStartWindow.h" +#include "CEditorApplication.h" #include "CProjectOverviewDialog.h" #include #include @@ -24,7 +24,7 @@ int main(int argc, char *argv[]) { // Create application QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts); - QApplication App(argc, argv); + CEditorApplication App(argc, argv); App.setWindowIcon(QIcon(":/icons/AppIcon.ico")); // Init log