181 lines
4.9 KiB
C++
181 lines
4.9 KiB
C++
#include "workspace/WorkSpaceManager.h"
|
|
|
|
#include<time.h>
|
|
#include <QSettings>
|
|
#include <QFileInfo>
|
|
|
|
#include "app/Application.h"
|
|
#include "workspace/WorkSpace.h"
|
|
#include "workspace/Timestep.h"
|
|
#include "common/SpdLogger.h"
|
|
|
|
template<> WorkSpaceManager* Singleton<WorkSpaceManager>::instance_ = nullptr;
|
|
|
|
WorkSpaceManager::WorkSpaceManager(QObject* parent)
|
|
: QObject(parent) {}
|
|
|
|
WorkSpaceManager::~WorkSpaceManager() {
|
|
|
|
}
|
|
|
|
void WorkSpaceManager::OnDestory() {
|
|
SaveDefaultWorkspace();
|
|
}
|
|
|
|
WorkSpace* WorkSpaceManager::LoadDefaultWorkspace(class OEScene* secen) {
|
|
dyt_check(nullptr != secen);
|
|
|
|
scene_ = secen;
|
|
const QString iniFile = GetDefaultWorkSpaceName();
|
|
if (!QFile::exists(iniFile)) {
|
|
LOG_ERROR("default workspace file not exist:{}", iniFile.toStdString());
|
|
return nullptr;
|
|
}
|
|
|
|
QSettings settings(iniFile, QSettings::IniFormat);
|
|
QString path = settings.value("workspace/name", "").toString();
|
|
if (path.isEmpty()) {
|
|
LOG_ERROR("default workspace path is empty");
|
|
return nullptr;
|
|
}
|
|
LOG_INFO("load default workspace name:{}", path.toLocal8Bit().constData());
|
|
|
|
WorkSpace* workspace = nullptr;
|
|
|
|
const QString workspaceDir = Application::GetWorkSpacePath();
|
|
QFileInfo fileInfo(path);
|
|
QString newPath = QString("%1/%2/%3").arg(workspaceDir).arg(fileInfo.baseName()).arg(fileInfo.fileName());
|
|
QFileInfo newFileInfo(newPath);
|
|
if (newFileInfo.exists()) {
|
|
workspace = LoadDyt(newPath);
|
|
} else {
|
|
newPath = QString("%1/%2").arg(workspaceDir).arg(fileInfo.fileName());
|
|
workspace = LoadDyt(newPath);
|
|
}
|
|
if (nullptr == workspace) {
|
|
LOG_ERROR("load default workspace failed");
|
|
return nullptr;
|
|
}
|
|
|
|
SetCurrent(workspace);
|
|
return workspace;
|
|
}
|
|
|
|
void WorkSpaceManager::SaveDefaultWorkspace() {
|
|
const QString iniFile = GetDefaultWorkSpaceName();
|
|
if (!QFile::exists(iniFile)) {
|
|
LOG_ERROR("default workspace file not exist:{}", iniFile.toStdString());
|
|
return;
|
|
}
|
|
|
|
WorkSpace* current = GetCurrent();
|
|
if (nullptr == current) {
|
|
LOG_ERROR("current workspace is nullptr");
|
|
return;
|
|
}
|
|
|
|
current->Save();
|
|
QString path = current->GetPath();
|
|
QFileInfo fileInfo(path);
|
|
LOG_INFO("save default workspace name:{}", fileInfo.fileName().toLocal8Bit().constData());
|
|
QSettings settings(iniFile, QSettings::IniFormat);
|
|
settings.setValue("workspace/name", fileInfo.fileName());
|
|
}
|
|
|
|
bool WorkSpaceManager::Contains(const QString& name) const {
|
|
const auto itor = workSpaces_.find(name);
|
|
if (workSpaces_.end() != itor) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool WorkSpaceManager::Remove(const QString& name) {
|
|
auto itor = workSpaces_.find(name);
|
|
if (workSpaces_.end() == itor) {
|
|
return true;
|
|
}
|
|
|
|
WorkSpace* workspace = itor->second;
|
|
if (nullptr != workspace) {
|
|
workspace->Unlaod();
|
|
workspace->deleteLater();
|
|
}
|
|
workSpaces_.erase(itor);
|
|
return true;
|
|
}
|
|
|
|
WorkSpace* WorkSpaceManager::LoadDyt(const QString& dyt) {
|
|
dyt_check(nullptr != scene_);
|
|
LOG_INFO("load workspace name:{}", dyt.toLocal8Bit().constData());
|
|
WorkSpace* workspace = new WorkSpace(this);
|
|
workspace->SetActiveScene(scene_);
|
|
if (!workspace->Load(dyt)) {
|
|
LOG_WARN("load workspace failed");
|
|
workspace->deleteLater();
|
|
return nullptr;
|
|
}
|
|
|
|
workSpaces_[dyt] = workspace;
|
|
return workspace;
|
|
}
|
|
|
|
WorkSpace* WorkSpaceManager::GetOrCreate(const QString& path, const QString& name) {
|
|
const auto itor = workSpaces_.find(path);
|
|
if (workSpaces_.end() != itor) {
|
|
return itor->second;
|
|
}
|
|
|
|
LOG_INFO("create workspace name:{}", path.toStdString());
|
|
WorkSpace* workspace = new WorkSpace(path, this);
|
|
workspace->SetName(name);
|
|
workSpaces_[path] = workspace;
|
|
return workspace;
|
|
}
|
|
|
|
void WorkSpaceManager::SetCurrent(WorkSpace* workspace) {
|
|
if (current_ == workspace) {
|
|
LOG_INFO("current == workspace");
|
|
return;
|
|
}
|
|
|
|
if (nullptr != current_) {
|
|
current_->End();
|
|
}
|
|
current_ = workspace;
|
|
workspace->SetActiveScene(scene_);
|
|
|
|
lastTime_ = clock();
|
|
|
|
emit WorkSpaceChanged(current_);
|
|
|
|
current_->OnLoaded();
|
|
}
|
|
|
|
void WorkSpaceManager::OnFrame() {
|
|
if (nullptr == current_) {
|
|
return;
|
|
}
|
|
|
|
constexpr double lockPreSecFre = 1.0 / CLOCKS_PER_SEC;
|
|
int64_t t = clock();
|
|
double dt = static_cast<double>(t - lastTime_) * lockPreSecFre;
|
|
|
|
Timestep* timeStep = current_->GetTimestep();
|
|
if (nullptr != timeStep) {
|
|
timeStep->Update(dt);
|
|
}
|
|
|
|
lastTime_ = t;
|
|
}
|
|
|
|
QString WorkSpaceManager::GetDefaultWorkSpaceName() {
|
|
#if _DEBUG
|
|
const QString iniFile = QString("%1workspace/config.ini").arg(QString(CONFIG_PATH)).arg(skin);
|
|
#else
|
|
const QString appDirPath = QApplication::applicationDirPath();
|
|
const QString iniFile = QString("%1/config/workspace/config.ini").arg(appDirPath);
|
|
#endif
|
|
return iniFile;
|
|
}
|