DYTSrouce/src/workspace/WorkSpaceManager.cpp

163 lines
4.1 KiB
C++
Raw Normal View History

2025-01-04 04:12:51 +00:00
#include "workspace/WorkSpaceManager.h"
2025-01-05 11:12:18 +00:00
#include<time.h>
#include <QSettings>
2025-01-04 04:12:51 +00:00
#include "workspace/WorkSpace.h"
#include "workspace/Timestep.h"
#include "common/SpdLogger.h"
template<> WorkSpaceManager* Singleton<WorkSpaceManager>::instance_ = nullptr;
WorkSpaceManager::WorkSpaceManager(QObject* parent)
2025-01-05 13:49:36 +00:00
: QObject(parent) {}
2025-01-04 04:12:51 +00:00
WorkSpaceManager::~WorkSpaceManager() {
}
void WorkSpaceManager::OnDestory() {
2025-01-05 11:12:18 +00:00
SaveDefaultWorkspace();
}
2025-01-05 13:49:36 +00:00
WorkSpace* WorkSpaceManager::LoadDefaultWorkspace(class OEScene* secen) {
dyt_check(nullptr != secen);
scene_ = secen;
2025-01-05 11:12:18 +00:00
const QString iniFile = GetDefaultWorkSpaceName();
if (!QFile::exists(iniFile)) {
LOG_ERROR("default workspace file not exist:{}", iniFile.toStdString());
return nullptr;
}
QSettings settings(iniFile, QSettings::IniFormat);
const QString path = settings.value("workspace/path", "").toString();
if (path.isEmpty()) {
LOG_ERROR("default workspace path is empty");
return nullptr;
}
2025-01-05 13:49:36 +00:00
WorkSpace* workspace = LoadDyt(path);
2025-01-05 11:12:18 +00:00
if (nullptr == workspace) {
LOG_ERROR("load default workspace failed");
return nullptr;
}
2025-01-04 04:12:51 +00:00
2025-01-05 11:12:18 +00:00
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;
}
2025-01-05 15:12:58 +00:00
current->Save();
2025-01-05 11:12:18 +00:00
const QString path = current->GetPath();
QSettings settings(iniFile, QSettings::IniFormat);
settings.setValue("workspace/path", path);
2025-01-04 04:12:51 +00:00
}
bool WorkSpaceManager::Contains(const QString& name) const {
const auto itor = workSpaces_.find(name);
if (workSpaces_.end() != itor) {
return true;
}
return false;
}
2025-01-04 15:41:53 +00:00
bool WorkSpaceManager::Remove(const QString& name) {
auto itor = workSpaces_.find(name);
if (workSpaces_.end() == itor) {
return true;
}
2025-01-05 13:49:36 +00:00
2025-01-04 15:41:53 +00:00
WorkSpace* workspace = itor->second;
if (nullptr != workspace) {
2025-01-05 03:33:33 +00:00
workspace->Unlaod();
2025-01-04 15:41:53 +00:00
workspace->deleteLater();
}
workSpaces_.erase(itor);
2025-01-05 03:33:33 +00:00
return true;
2025-01-04 15:41:53 +00:00
}
2025-01-05 13:49:36 +00:00
WorkSpace* WorkSpaceManager::LoadDyt(const QString& dyt) {
2025-01-05 15:54:44 +00:00
dyt_check(nullptr != scene_);
2025-01-05 13:49:36 +00:00
WorkSpace* workspace = new WorkSpace(this);
2025-01-05 15:54:44 +00:00
workspace->SetActiveScene(scene_);
2025-01-04 04:12:51 +00:00
if (!workspace->Load(dyt)) {
LOG_WARN("load workspace failed");
workspace->deleteLater();
return nullptr;
}
workSpaces_[dyt] = workspace;
return workspace;
}
2025-01-05 13:49:36 +00:00
WorkSpace* WorkSpaceManager::GetOrCreate(const QString& path, const QString& name) {
2025-01-04 04:12:51 +00:00
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);
2025-01-05 13:49:36 +00:00
workspace->SetName(name);
2025-01-04 04:12:51 +00:00
workSpaces_[path] = workspace;
return workspace;
}
void WorkSpaceManager::SetCurrent(WorkSpace* workspace) {
2025-01-05 13:49:36 +00:00
if (current_ == workspace) {
LOG_INFO("current == workspace");
return;
}
2025-01-04 04:12:51 +00:00
if (nullptr != current_) {
current_->End();
}
current_ = workspace;
2025-01-05 13:49:36 +00:00
workspace->SetActiveScene(scene_);
2025-01-04 04:12:51 +00:00
lastTime_ = clock();
emit WorkSpaceChanged(current_);
2025-01-05 11:12:18 +00:00
current_->OnLoaded();
2025-01-04 04:12:51 +00:00
}
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;
}
2025-01-05 11:12:18 +00:00
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;
}