189 lines
4.3 KiB
C++
189 lines
4.3 KiB
C++
#include "workspace/WorkSpace.h"
|
|
|
|
#include <QUUID>
|
|
|
|
#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 "scene/OsgScene.h"
|
|
//#include "workspace/WorkSpaceItemGroup.h"
|
|
//#include "workspace/WorkSpaceRiverGroup.h"
|
|
//#include "workspace/WorkSpaceRiverNetGroup.h"
|
|
|
|
Q_DECLARE_METATYPE(WorkSpace*)
|
|
|
|
WorkSpace::WorkSpace(OsgView* view, QObject* parent) noexcept
|
|
: QObject(parent)
|
|
, view_(view) {
|
|
uuid_ = QUuid::createUuid().toString();
|
|
}
|
|
|
|
WorkSpace::WorkSpace(const QString& name, QObject* parent)
|
|
: QObject(parent)
|
|
, name_(name){
|
|
uuid_ = QUuid::createUuid().toString();
|
|
}
|
|
|
|
void WorkSpace::ViewReize(int width, int height) {
|
|
if (nullptr != activeScene_ && nullptr != activeScene_->GetOceanScene()) {
|
|
activeScene_->GetOceanScene()->setScreenDims(osg::Vec2s(width * 2, height * 2));
|
|
} else {
|
|
LOG_WARN("secen_:{} or ocean is nullptr", spdlog::fmt_lib::ptr(activeScene_.get()));
|
|
}
|
|
}
|
|
|
|
bool WorkSpace::CreateScene(const std::string& scene) {
|
|
LOG_INFO("create scene {}", scene);
|
|
if (nullptr == view_) {
|
|
return false;
|
|
}
|
|
|
|
if (nullptr != activeScene_) {
|
|
activeScene_->DetachView(view_);
|
|
}
|
|
|
|
name_ = name_.fromStdString(scene);
|
|
activeScene_ = new OsgScene;
|
|
activeScene_->UseShadows(false);
|
|
|
|
activeScene_->ChangeScene(OsgScene::CLOUDY);
|
|
|
|
activeScene_->AttachView(view_);
|
|
activeScene_->InitEventHandle(view_);
|
|
return true;
|
|
}
|
|
|
|
void WorkSpace::AddEntity(Entity* entity) {
|
|
if (nullptr == entity) {
|
|
LOG_WARN("entity is nullptr");
|
|
return;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
OsgScene* WorkSpace::GetActiveScene() const {
|
|
if (!activeScene_.valid()) {
|
|
return nullptr;
|
|
}
|
|
|
|
return activeScene_.get();
|
|
}
|
|
|
|
bool WorkSpace::SetTimestep(Timestep* timestep) {
|
|
if (!timestep) {
|
|
return false;
|
|
}
|
|
|
|
if (nullptr != timestep_ && timestep_ != timestep) {
|
|
timestep_->deleteLater();
|
|
}
|
|
|
|
timestep_ = 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;
|
|
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::BuildDefault() {
|
|
/* WorkSpaceItemGroup* item = new WorkSpaceItemGroup("root", this);
|
|
AddItem(item, nullptr);
|
|
WorkSpaceItemGroup* subItem = new WorkSpaceRiverGroup(tr("River0"));
|
|
AddItem(subItem, item);
|
|
item = subItem;
|
|
subItem = new WorkSpaceRiverNetGroup(tr("RiverNet0"));
|
|
AddItem(subItem, item);*/
|
|
|
|
return true;
|
|
}
|
|
|
|
bool WorkSpace::Load(const QString& dyt) {
|
|
WorkSpaceXMLParse parse(this);
|
|
bool success = parse.Load(dyt);
|
|
if (success) {
|
|
path_ = dyt;
|
|
}
|
|
return success;
|
|
}
|
|
|
|
void WorkSpace::Unlaod() {
|
|
for (auto& entity : entities_) {
|
|
entity->End();
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|