DYTSrouce/src/workspace/WorkSpace.cpp

215 lines
5.3 KiB
C++

#include "workspace/WorkSpace.h"
#include <QUUID>
#include <QFileInfo>
#include <QDir>
#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*)
WorkSpace::WorkSpace(QObject* parent) noexcept
: QObject(parent) {
uuid_ = QUuid::createUuid().toString();
homeViewpoint_ = osgEarth::Viewpoint("home", 120.000000, 25.000000, 100.000000, -2.500000, -90.000000, 8200000.000000);
}
WorkSpace::WorkSpace(const QString& path, QObject* parent)
: QObject(parent)
, path_(path){
uuid_ = QUuid::createUuid().toString();
homeViewpoint_ = osgEarth::Viewpoint("home", 120.000000, 25.000000, 100.000000, -2.500000, -90.000000, 8200000.000000);
}
const QString WorkSpace::GetDir() const {
QFileInfo info(path_);
return info.absolutePath();
}
void WorkSpace::AddEntity(Entity* entity) {
if (nullptr == entity) {
LOG_WARN("entity is nullptr");
return;
}
LOG_INFO("add entity: {}", entity->GetName().toLocal8Bit().constData());
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::TrackEntity(Entity* entity) {
dyt_check(nullptr != entity);
if (trackedEntity_ == entity) {
LOG_INFO("entity already tracked: {}", entity->GetName().toStdString());
return true;
}
if (nullptr == scene_) {
LOG_WARN("scene is nullptr, cannot track entity: {}", entity->GetName().toStdString());
return false;
}
if (trackedEntity_ != entity) {
UntrackEntity();
}
trackedEntity_ = entity;
auto mt = entity->GetRootComponent()->GetMatrixTransform();
LOG_INFO("track entity: {}", entity->GetName().toStdString());
return scene_->TrackEntity(mt, true);
}
void WorkSpace::UntrackEntity() {
if (trackedEntity_ != nullptr) {
if (scene_ != nullptr) {
LOG_INFO("untrack entity: {}", trackedEntity_->GetName().toStdString());
auto mt = trackedEntity_->GetRootComponent()->GetMatrixTransform();
scene_->TrackEntity(mt, false);
}
}
trackedEntity_ = nullptr;
}
void WorkSpace::SetActiveScene(OEScene* scene) {
dyt_check(nullptr != scene);
scene_ = scene;
//osgEarth::Viewpoint vp;
//vp.name() = "home";
//vp.focalPoint()->set(scene->GetSrs(), -121.488, 46.2054, 0, osgEarth::AltitudeMode::ALTMODE_ABSOLUTE);
//vp.pitch() = -50.0;
//vp.range() = 100000;
//homeViewpoint_ = vp;
}
bool WorkSpace::SetTimestep(Timestep* timestep) {
if (!timestep) {
return false;
}
if (nullptr != timestep_ && timestep_ != timestep) {
timestep_->deleteLater();
}
timestep_ = timestep;
emit TimestepChanged(timestep_);
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;
emit LampStatusChanged(lampStatus_);
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) {
if (leaded_) {
LOG_INFO("dyt {} loaded", dyt.toStdString());
return true;
}
LOG_INFO("dyt {} loading", dyt.toLocal8Bit().constData());
path_ = dyt;
WorkSpaceXMLParse parse(this);
bool success = parse.Load(dyt);
if (!success) {
path_ = "";
}
leaded_ = success;
return success;
}
void WorkSpace::Unlaod() {
if (!leaded_) {
LOG_INFO("dyt {} unloaded", name_.toStdString());
return;
}
while (!entities_.empty()) {
auto entity = entities_.front();
entity->Destory();
}
leaded_ = false;
}
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();
}
}
void WorkSpace::OnLoaded() {
if (nullptr != lampStatus_) {
emit LampStatusChanged(lampStatus_);
}
if (nullptr != timestep_) {
emit TimestepChanged(timestep_);
}
}