diff --git a/src/scene/OEScene.cpp b/src/scene/OEScene.cpp index 805787db..0260c023 100644 --- a/src/scene/OEScene.cpp +++ b/src/scene/OEScene.cpp @@ -47,8 +47,6 @@ void OEScene::AttachView(osgViewer::View* view) { skyDome_->attach(view); skyDome_->getSunLight()->setAmbient(osg::Vec4(0.5,0.5,0.5,1.0)); addChild(skyDome_); - - view->setCameraManipulator( new osgEarth::Util::EarthManipulator() ); skyDome_->setDateTime(osgEarth::DateTime(2024, 12, 24, 3)); @@ -68,33 +66,18 @@ bool OEScene::AddToScene(osg::Node* node) const { void OEScene::SetHomeViewpoint(const osgEarth::Viewpoint& viewpoint, double duration_s) { dyt_check(nullptr != earthManipulator_); earthManipulator_->setViewpoint(viewpoint, duration_s); - earthManipulator_->setHomeViewpoint(viewpoint, duration_s); + //earthManipulator_->setHomeViewpoint(viewpoint, duration_s); } -// -// void OEScene::InitEventHandle(osgViewer::View* view) { -// if (nullptr == view) { -// LOG_WARN("view is nullptr"); -// return; -// } -// -// //view->GetView()->addEventHandler(new osgEarth::Util::EarthManipulator()); -// -// view->addEventHandler(new osgViewer::HelpHandler); -// view->addEventHandler(new osgViewer::StatsHandler); -// } - -// void OEScene::DetachView(osgViewer::View* view) { -// if (nullptr != earthRootNode_) { -// std::vector parents = earthRootNode_->getParents(); -// for (const auto& parent : parents) { -// parent->removeChild(earthRootNode_); -// } -// } -// logarithmicDepthBuffer_->uninstall(view->getCamera()); -// view->setSceneData(nullptr); -// -// } +bool OEScene::TrackEntity(osg::Node* entity, bool track) const { + dyt_check(nullptr != earthManipulator_); + if (track) { + earthManipulator_->setTetherNode(entity, 5.0); + } else { + earthManipulator_->clearViewpoint(); + } + return true; +} OESceneUI* OEScene::GetOrCreateSceneUI() { if (sceneUI_) { @@ -130,6 +113,8 @@ void OEScene::Init() { g_srs_ = earthMapNode_->getMapSRS(); earthManipulator_ = new osgEarth::Util::EarthManipulator(); + earthManipulator_->getSettings()->setMinMaxPitch(-90.0, -10.0); + entityRoot_ = new osg::Group; addChild(entityRoot_); diff --git a/src/scene/OEScene.h b/src/scene/OEScene.h index 3cbd46ba..fd9b92e7 100644 --- a/src/scene/OEScene.h +++ b/src/scene/OEScene.h @@ -23,6 +23,7 @@ public: bool AddToScene(osg::Node* node) const; void SetHomeViewpoint(const osgEarth::Viewpoint& viewpoint, double duration_s); + bool TrackEntity(osg::Node* entity, bool track) const; osgEarth::MapNode* GetMapNode(void) const { dyt_check(earthMapNode_.valid()); diff --git a/src/translations/Dyt_zh_CN.ts b/src/translations/Dyt_zh_CN.ts index 63776138..dd6803f0 100644 --- a/src/translations/Dyt_zh_CN.ts +++ b/src/translations/Dyt_zh_CN.ts @@ -636,57 +636,57 @@ ModelTreeWidget - + Release Track - + Add boke Entity - + Add lsjhqt Entity - + Add nimizi Entity - + Add tkdlj Entity - + Add jiaofan Entity - + Add satellite Entity - + Track - + Add Mesh Component - + Add Path Component - + Delete diff --git a/src/ui/ModelBrowser/ModelTreeWidget.cpp b/src/ui/ModelBrowser/ModelTreeWidget.cpp index 26ad02b9..2f761cf3 100644 --- a/src/ui/ModelBrowser/ModelTreeWidget.cpp +++ b/src/ui/ModelBrowser/ModelTreeWidget.cpp @@ -16,6 +16,7 @@ #include "viewer/OsgCameraManipulator.h" #include "viewer/CameraControlManipulator.h" #include "ui/ModelBrowser/EntityConstent.h" +#include "workspace/WorkSpaceManager.h" ModelTreeWidget::ModelTreeWidget(QWidget* parent) @@ -91,7 +92,7 @@ void ModelTreeWidget::OnEntityRemoved(Entity* entity) { void ModelTreeWidget::contextMenuEvent(QContextMenuEvent* event) { QTreeWidgetItem* item = itemAt(event->pos()); - if (nullptr == item) { + if (nullptr != item) { QMenu menu(this); QAction* addEntiy = new QAction(tr("Release Track"), this); connect(addEntiy, &QAction::triggered, [this]() { @@ -320,6 +321,21 @@ void ModelTreeWidget::OnAddSatelliteEntity() { } void ModelTreeWidget::OnTrackEntity(Entity* entity) { + auto workspace = WorkSpaceManager::Get().GetCurrent(); + if (!workspace){ + LOG_WARN("scene is nullptr"); + return; + } + + if (nullptr == entity) { + LOG_WARN("entity is nullptr, clear camera manipulator"); + return; + } + + + workspace->TrackEntity(entity); + //scene->Track + //MainWindow:: //OsgCameraManipulator* manipulator = OsgViewer::Get().GetView()->GetCameraManipulator(); //if (nullptr == manipulator) { // LOG_WARN("manipulator is nullptr"); diff --git a/src/workspace/WorkSpace.cpp b/src/workspace/WorkSpace.cpp index 36e0c5bc..1970a0d9 100644 --- a/src/workspace/WorkSpace.cpp +++ b/src/workspace/WorkSpace.cpp @@ -59,16 +59,48 @@ void WorkSpace::RemoveEntity(Entity* entity) { } } +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; + //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) { diff --git a/src/workspace/WorkSpace.h b/src/workspace/WorkSpace.h index dbc9d28d..efa07aca 100644 --- a/src/workspace/WorkSpace.h +++ b/src/workspace/WorkSpace.h @@ -83,6 +83,7 @@ public: inline void SetHomeViewpoint(const osgEarth::Viewpoint& viewpoint) { homeViewpoint_ = viewpoint; + homeViewpoint_.setHeading(0.0); // Ensure heading is set to 0.0 } inline const osgEarth::Viewpoint& GetHomeViewpoint() const { return homeViewpoint_; @@ -115,6 +116,8 @@ public: void AddEntity(class Entity* entity); void RemoveEntity(class Entity* entity); + bool TrackEntity(class Entity* entity); + void UntrackEntity(); bool Save(const QString& path); bool Save(); @@ -153,5 +156,6 @@ private: OEScene* scene_{ nullptr }; class Timestep* timestep_{ nullptr }; class LampStatus* lampStatus_{ nullptr }; + class Entity* trackedEntity_{ nullptr }; };