From f063eeb36ee34c7230de7e670aaab923f08dcdd6 Mon Sep 17 00:00:00 2001 From: Nelsson Huotari Date: Thu, 27 Feb 2020 11:54:11 +0200 Subject: [PATCH] Drop objects with a keyboard shortcut --- apps/opencs/model/prefs/state.cpp | 2 + apps/opencs/view/render/instancemode.cpp | 84 +++++++++++++++++++- apps/opencs/view/render/instancemode.hpp | 14 +++- apps/opencs/view/render/object.cpp | 5 ++ apps/opencs/view/render/object.hpp | 3 + apps/opencs/view/render/worldspacewidget.cpp | 2 +- 6 files changed, 106 insertions(+), 4 deletions(-) diff --git a/apps/opencs/model/prefs/state.cpp b/apps/opencs/model/prefs/state.cpp index 442e707bf..422e2c1f4 100644 --- a/apps/opencs/model/prefs/state.cpp +++ b/apps/opencs/model/prefs/state.cpp @@ -356,6 +356,8 @@ void CSMPrefs::State::declare() QKeySequence(Qt::ControlModifier | (int)Qt::MiddleButton)); declareModifier ("scene-speed-modifier", "Speed Modifier", Qt::Key_Shift); declareShortcut ("scene-delete", "Delete Instance", QKeySequence(Qt::Key_Delete)); + declareShortcut ("scene-instance-groundlevel", "Instance to Ground Level", QKeySequence(Qt::Key_G)); + declareShortcut ("scene-instance-drop", "Drop Instance", QKeySequence(Qt::Key_H)); declareShortcut ("scene-load-cam-cell", "Load Camera Cell", QKeySequence(Qt::KeypadModifier | Qt::Key_5)); declareShortcut ("scene-load-cam-eastcell", "Load East Cell", QKeySequence(Qt::KeypadModifier | Qt::Key_6)); declareShortcut ("scene-load-cam-northcell", "Load North Cell", QKeySequence(Qt::KeypadModifier | Qt::Key_8)); diff --git a/apps/opencs/view/render/instancemode.cpp b/apps/opencs/view/render/instancemode.cpp index 92f5cbb2d..c0d478113 100644 --- a/apps/opencs/view/render/instancemode.cpp +++ b/apps/opencs/view/render/instancemode.cpp @@ -6,6 +6,10 @@ #include "../../model/prefs/state.hpp" +#include +#include +#include + #include "../../model/world/idtable.hpp" #include "../../model/world/idtree.hpp" #include "../../model/world/commands.hpp" @@ -90,16 +94,20 @@ osg::Vec3f CSVRender::InstanceMode::getScreenCoords(const osg::Vec3f& pos) return pos * combined; } -CSVRender::InstanceMode::InstanceMode (WorldspaceWidget *worldspaceWidget, QWidget *parent) +CSVRender::InstanceMode::InstanceMode (WorldspaceWidget *worldspaceWidget, osg::ref_ptr parentNode, QWidget *parent) : EditMode (worldspaceWidget, QIcon (":scenetoolbar/editing-instance"), SceneUtil::Mask_EditorReference | SceneUtil::Mask_Terrain, "Instance editing", parent), mSubMode (0), mSubModeId ("move"), mSelectionMode (0), mDragMode (DragMode_None), - mDragAxis (-1), mLocked (false), mUnitScaleDist(1) + mDragAxis (-1), mLocked (false), mUnitScaleDist(1), mParentNode (parentNode) { connect(this, SIGNAL(requestFocus(const std::string&)), worldspaceWidget, SIGNAL(requestFocus(const std::string&))); CSMPrefs::Shortcut* deleteShortcut = new CSMPrefs::Shortcut("scene-delete", worldspaceWidget); connect(deleteShortcut, SIGNAL(activated(bool)), this, SLOT(deleteSelectedInstances(bool))); + CSMPrefs::Shortcut* dropShortcut = new CSMPrefs::Shortcut("scene-instance-drop", worldspaceWidget); + connect(dropShortcut, SIGNAL(activated(bool)), this, SLOT(dropSelectedInstances(bool))); + CSMPrefs::Shortcut* groundlevelShortcut = new CSMPrefs::Shortcut("scene-instance-groundlevel", worldspaceWidget); + connect(groundlevelShortcut, SIGNAL(activated(bool)), this, SLOT(groundlevelSelectedInstances(bool))); } void CSVRender::InstanceMode::activate (CSVWidget::SceneToolbar *toolbar) @@ -681,3 +689,75 @@ void CSVRender::InstanceMode::deleteSelectedInstances(bool active) getWorldspaceWidget().clearSelection (SceneUtil::Mask_EditorReference); } + +void CSVRender::InstanceMode::dropInstance(DropMode dropMode, CSVRender::Object* object) +{ + const osg::Vec3d& point = object->getPosition().asVec3(); + osg::ref_ptr objectNode = object->getRootNode(); + osg::Node::NodeMask oldMask = objectNode->getNodeMask(); + objectNode->setNodeMask(SceneUtil::Mask_Disabled); + + osg::Vec3d start = point; + osg::Vec3d end = point; + start.z() += 1.0f; + end.z() = std::numeric_limits::min(); + + osg::ref_ptr intersector (new osgUtil::LineSegmentIntersector( + osgUtil::Intersector::MODEL, start, end) ); + intersector->setIntersectionLimit(osgUtil::LineSegmentIntersector::NO_LIMIT); + osgUtil::IntersectionVisitor visitor(intersector); + + if (dropMode == Ground_level) visitor.setTraversalMask(SceneUtil::Mask_Terrain); + if (dropMode == Coillision_Below) visitor.setTraversalMask(SceneUtil::Mask_Terrain | SceneUtil::Mask_EditorReference); + + mParentNode->accept(visitor); + + for (osgUtil::LineSegmentIntersector::Intersections::iterator it = intersector->getIntersections().begin(); + it != intersector->getIntersections().end(); ++it) + { + osgUtil::LineSegmentIntersector::Intersection intersection = *it; + ESM::Position position = object->getPosition(); + object->setEdited (Object::Override_Position); + position.pos[2] = intersection.getWorldIntersectPoint().z(); + object->setPosition(position.pos); + objectNode->setNodeMask(oldMask); + + return; + } + + objectNode->setNodeMask(oldMask); +} + +void CSVRender::InstanceMode::dropSelectedInstances(bool active) +{ + std::vector > selection = getWorldspaceWidget().getSelection (SceneUtil::Mask_EditorReference); + if (selection.empty()) return; + + CSMDoc::Document& document = getWorldspaceWidget().getDocument(); + QUndoStack& undoStack = document.getUndoStack(); + + CSMWorld::CommandMacro macro (undoStack, "Drop Instances to next coillision"); + for(osg::ref_ptr tag: selection) + if (CSVRender::ObjectTag *objectTag = dynamic_cast (tag.get())) + { + dropInstance(Coillision_Below, objectTag->mObject); + objectTag->mObject->apply (macro); + } +} + +void CSVRender::InstanceMode::groundlevelSelectedInstances(bool active) +{ + std::vector > selection = getWorldspaceWidget().getSelection (SceneUtil::Mask_EditorReference); + if (selection.empty()) return; + + CSMDoc::Document& document = getWorldspaceWidget().getDocument(); + QUndoStack& undoStack = document.getUndoStack(); + + CSMWorld::CommandMacro macro (undoStack, "Drop Instances to Ground Level"); + for(osg::ref_ptr tag: selection) + if (CSVRender::ObjectTag *objectTag = dynamic_cast (tag.get())) + { + dropInstance(Ground_level, objectTag->mObject); + objectTag->mObject->apply (macro); + } +} diff --git a/apps/opencs/view/render/instancemode.hpp b/apps/opencs/view/render/instancemode.hpp index 23df3f37d..4d7ac9cac 100644 --- a/apps/opencs/view/render/instancemode.hpp +++ b/apps/opencs/view/render/instancemode.hpp @@ -2,6 +2,7 @@ #define CSV_RENDER_INSTANCEMODE_H #include +#include #include #include @@ -16,6 +17,7 @@ namespace CSVRender { class TagBase; class InstanceSelectionMode; + class Object; class InstanceMode : public EditMode { @@ -29,6 +31,12 @@ namespace CSVRender DragMode_Scale }; + enum DropMode + { + Coillision_Below, + Ground_level + }; + CSVWidget::SceneToolMode *mSubMode; std::string mSubModeId; InstanceSelectionMode *mSelectionMode; @@ -36,6 +44,7 @@ namespace CSVRender int mDragAxis; bool mLocked; float mUnitScaleDist; + osg::ref_ptr mParentNode; int getSubModeFromId (const std::string& id) const; @@ -44,10 +53,11 @@ namespace CSVRender osg::Vec3f getSelectionCenter(const std::vector >& selection) const; osg::Vec3f getScreenCoords(const osg::Vec3f& pos); + void dropInstance(DropMode dropMode, CSVRender::Object* object); public: - InstanceMode (WorldspaceWidget *worldspaceWidget, QWidget *parent = 0); + InstanceMode (WorldspaceWidget *worldspaceWidget, osg::ref_ptr parentNode, QWidget *parent = 0); virtual void activate (CSVWidget::SceneToolbar *toolbar); @@ -93,6 +103,8 @@ namespace CSVRender void subModeChanged (const std::string& id); void deleteSelectedInstances(bool active); + void dropSelectedInstances(bool active); + void groundlevelSelectedInstances(bool active); }; } diff --git a/apps/opencs/view/render/object.cpp b/apps/opencs/view/render/object.cpp index 02f89b0ab..47754b448 100644 --- a/apps/opencs/view/render/object.cpp +++ b/apps/opencs/view/render/object.cpp @@ -477,6 +477,11 @@ bool CSVRender::Object::getSelected() const return mSelected; } +osg::ref_ptr CSVRender::Object::getRootNode() +{ + return mRootNode; +} + bool CSVRender::Object::referenceableDataChanged (const QModelIndex& topLeft, const QModelIndex& bottomRight) { diff --git a/apps/opencs/view/render/object.hpp b/apps/opencs/view/render/object.hpp index 10a46fc10..b2561d585 100644 --- a/apps/opencs/view/render/object.hpp +++ b/apps/opencs/view/render/object.hpp @@ -146,6 +146,9 @@ namespace CSVRender bool getSelected() const; + /// For direct altering of object rendering + osg::ref_ptr getRootNode(); + /// \return Did this call result in a modification of the visual representation of /// this object? bool referenceableDataChanged (const QModelIndex& topLeft, diff --git a/apps/opencs/view/render/worldspacewidget.cpp b/apps/opencs/view/render/worldspacewidget.cpp index 77ecba918..6ab4b041b 100644 --- a/apps/opencs/view/render/worldspacewidget.cpp +++ b/apps/opencs/view/render/worldspacewidget.cpp @@ -370,7 +370,7 @@ void CSVRender::WorldspaceWidget::addVisibilitySelectorButtons ( void CSVRender::WorldspaceWidget::addEditModeSelectorButtons (CSVWidget::SceneToolMode *tool) { /// \todo replace EditMode with suitable subclasses - tool->addButton (new InstanceMode (this, tool), "object"); + tool->addButton (new InstanceMode (this, mRootNode, tool), "object"); tool->addButton (new PathgridMode (this, tool), "pathgrid"); }