DYTSrouce/src/workspace/WorkSpace.cpp

169 lines
3.9 KiB
C++
Raw Normal View History

2025-01-04 04:12:51 +00:00
#include "workspace/WorkSpace.h"
#include <QUUID>
2025-01-05 15:12:58 +00:00
#include <QFileInfo>
#include <QDir>
2025-01-04 04:12:51 +00:00
#include "workspace/WorkSpaceXMLParse.h"
#include "workspace/WorkSpaceXMLWrite.h"
#include "workspace/WorkSpaceItem.h"
#include "workspace/Timestep.h"
#include "workspace/LampStatus.h"
#include "xml/tinyxml2.h"
#include "common/SpdLogger.h"
#include "entities/Entity.h"
//#include "workspace/WorkSpaceItemGroup.h"
//#include "workspace/WorkSpaceRiverGroup.h"
//#include "workspace/WorkSpaceRiverNetGroup.h"
Q_DECLARE_METATYPE(WorkSpace*)
2025-01-05 13:49:36 +00:00
WorkSpace::WorkSpace(QObject* parent) noexcept
: QObject(parent) {
2025-01-04 04:12:51 +00:00
uuid_ = QUuid::createUuid().toString();
2025-01-05 18:18:16 +00:00
homeViewpoint_ = osgEarth::Viewpoint("home", 120.000000, 25.000000, 100.000000, -2.500000, -90.000000, 8200000.000000);
2025-01-04 04:12:51 +00:00
}
2025-01-05 13:49:36 +00:00
WorkSpace::WorkSpace(const QString& path, QObject* parent)
2025-01-04 04:12:51 +00:00
: QObject(parent)
2025-01-05 13:49:36 +00:00
, path_(path){
2025-01-04 04:12:51 +00:00
uuid_ = QUuid::createUuid().toString();
2025-01-05 18:18:16 +00:00
homeViewpoint_ = osgEarth::Viewpoint("home", 120.000000, 25.000000, 100.000000, -2.500000, -90.000000, 8200000.000000);
2025-01-04 04:12:51 +00:00
}
2025-01-05 15:12:58 +00:00
const QString WorkSpace::GetDir() const {
QFileInfo info(path_);
return info.absolutePath();
}
2025-01-04 04:12:51 +00:00
void WorkSpace::AddEntity(Entity* entity) {
if (nullptr == entity) {
LOG_WARN("entity is nullptr");
return;
}
2025-01-05 03:33:33 +00:00
LOG_INFO("add entity: {}", entity->GetName().toLocal8Bit().constData());
2025-01-04 04:12:51 +00:00
entities_.push_back(entity);
entity->SetWorkspace(this);
emit EntityAdded(entity);
}
void WorkSpace::RemoveEntity(Entity* entity) {
auto itor = std::find_if(entities_.begin(), entities_.end(),
[this, entity](const auto* item) { return item == entity; });
if (itor != entities_.end()) {
emit EntityRemoved(entity);
entities_.erase(itor);
}
}
bool WorkSpace::SetTimestep(Timestep* timestep) {
if (!timestep) {
return false;
}
if (nullptr != timestep_ && timestep_ != timestep) {
timestep_->deleteLater();
}
timestep_ = timestep;
2025-01-05 11:12:18 +00:00
emit TimestepChanged(timestep_);
2025-01-04 04:12:51 +00:00
return true;
}
bool WorkSpace::SetTimestepPath(const QString& path) {
Timestep* timestep = Timestep::Load(path, this);
return SetTimestep(timestep);
}
bool WorkSpace::SetLampStatus(class LampStatus* lampStatus) {
if (!lampStatus) {
return false;
}
if (nullptr != lampStatus_ && lampStatus_ != lampStatus) {
lampStatus_->deleteLater();
}
lampStatus_ = lampStatus;
2025-01-05 11:12:18 +00:00
emit LampStatusChanged(lampStatus_);
2025-01-04 04:12:51 +00:00
return true;
}
bool WorkSpace::SetLampPath(const QString& path) {
LampStatus* timestep = LampStatus::Load(path, this);
return SetLampStatus(timestep);
}
bool WorkSpace::Save(const QString& path) {
path_ = path;
return Save();
}
bool WorkSpace::Save() {
WorkSpaceXMLWrite xmlWrite(this);
return xmlWrite.Save(path_);
}
bool WorkSpace::Load(const QString& dyt) {
2025-01-05 03:33:33 +00:00
if (leaded_) {
LOG_INFO("dyt {} loaded", dyt.toStdString());
return true;
}
2025-01-04 04:12:51 +00:00
WorkSpaceXMLParse parse(this);
bool success = parse.Load(dyt);
if (success) {
path_ = dyt;
}
2025-01-05 03:33:33 +00:00
leaded_ = success;
2025-01-04 04:12:51 +00:00
return success;
}
void WorkSpace::Unlaod() {
2025-01-05 03:33:33 +00:00
if (!leaded_) {
LOG_INFO("dyt {} unloaded", name_.toStdString());
return;
}
while (!entities_.empty()) {
auto entity = entities_.front();
entity->Destory();
2025-01-04 04:12:51 +00:00
}
2025-01-05 03:33:33 +00:00
leaded_ = false;
2025-01-04 04:12:51 +00:00
}
void WorkSpace::Begin() {
for (auto item : entities_) {
item->Begin();
}
}
void WorkSpace::OnFrame(double dt) {
if (nullptr != lampStatus_) {
double current = timestep_->GetCurrent();
lampStatus_->OnFrame(current);
}
for (auto item : entities_) {
item->Update(dt);
}
}
void WorkSpace::End() {
for (auto item : entities_) {
item->End();
}
}
2025-01-05 11:12:18 +00:00
void WorkSpace::OnLoaded() {
if (nullptr != lampStatus_) {
emit LampStatusChanged(lampStatus_);
}
if (nullptr != timestep_) {
emit TimestepChanged(timestep_);
}
}