Add helper class for safer mask handling

pull/2713/head
Nelsson Huotari 5 years ago
parent af434cffba
commit 41aa90bfa7

@ -791,8 +791,57 @@ void CSVRender::InstanceMode::handleDropMethod(DropMode dropMode, QString comman
CSMWorld::CommandMacro macro (undoStack, commandMsg); CSMWorld::CommandMacro macro (undoStack, commandMsg);
std::vector<osg::Node::NodeMask> oldMasks; DropObjectDataHandler dropObjectDataHandler(&getWorldspaceWidget());
std::vector<float> objectHeights;
switch (dropMode)
{
case Terrain:
case Collision:
{
float smallestDropHeight = std::numeric_limits<float>::max();
int counter = 0;
for(osg::ref_ptr<TagBase> tag: selection)
if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag.get()))
{
float thisDrop = getDropHeight(dropMode, objectTag->mObject, dropObjectDataHandler.mObjectHeights[counter]);
if (thisDrop < smallestDropHeight) smallestDropHeight = thisDrop;
counter++;
}
for(osg::ref_ptr<TagBase> tag: selection)
if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag.get()))
{
objectTag->mObject->setEdited (Object::Override_Position);
ESM::Position position = objectTag->mObject->getPosition();
position.pos[2] -= smallestDropHeight;
objectTag->mObject->setPosition(position.pos);
objectTag->mObject->apply (macro);
}
}
break;
case Terrain_sep:
case Collision_sep:
{
int counter = 0;
for(osg::ref_ptr<TagBase> tag: selection)
if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag.get()))
{
dropInstance(dropMode, objectTag->mObject, dropObjectDataHandler.mObjectHeights[counter]);
objectTag->mObject->apply (macro);
counter++;
}
}
break;
default:
break;
}
}
CSVRender::DropObjectDataHandler::DropObjectDataHandler(WorldspaceWidget* worldspacewidget)
: mWorldspaceWidget(worldspacewidget)
{
std::vector<osg::ref_ptr<TagBase> > selection = mWorldspaceWidget->getSelection (SceneUtil::Mask_EditorReference);
for(osg::ref_ptr<TagBase> tag: selection) for(osg::ref_ptr<TagBase> tag: selection)
{ {
if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag.get())) if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag.get()))
@ -807,70 +856,24 @@ void CSVRender::InstanceMode::handleDropMethod(DropMode dropMode, QString comman
float boundingBoxOffset = 0.0f; float boundingBoxOffset = 0.0f;
if (bounds.valid()) boundingBoxOffset = bounds.zMin(); if (bounds.valid()) boundingBoxOffset = bounds.zMin();
objectHeights.emplace_back(boundingBoxOffset); mObjectHeights.emplace_back(boundingBoxOffset);
oldMasks.emplace_back(objectNodeWithGUI->getNodeMask()); mOldMasks.emplace_back(objectNodeWithGUI->getNodeMask());
objectNodeWithGUI->setNodeMask(SceneUtil::Mask_Disabled); objectNodeWithGUI->setNodeMask(SceneUtil::Mask_Disabled);
} }
} }
}
try CSVRender::DropObjectDataHandler::~DropObjectDataHandler()
{ {
switch (dropMode) std::vector<osg::ref_ptr<TagBase> > selection = mWorldspaceWidget->getSelection (SceneUtil::Mask_EditorReference);
{
case Terrain:
case Collision:
{
float smallestDropHeight = std::numeric_limits<float>::max();
int counter = 0;
for(osg::ref_ptr<TagBase> tag: selection)
if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag.get()))
{
float thisDrop = getDropHeight(dropMode, objectTag->mObject, objectHeights[counter]);
if (thisDrop < smallestDropHeight) smallestDropHeight = thisDrop;
counter++;
}
for(osg::ref_ptr<TagBase> tag: selection)
if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag.get()))
{
objectTag->mObject->setEdited (Object::Override_Position);
ESM::Position position = objectTag->mObject->getPosition();
position.pos[2] -= smallestDropHeight;
objectTag->mObject->setPosition(position.pos);
objectTag->mObject->apply (macro);
}
}
break;
case Terrain_sep:
case Collision_sep:
{
int counter = 0;
for(osg::ref_ptr<TagBase> tag: selection)
if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag.get()))
{
dropInstance(dropMode, objectTag->mObject, objectHeights[counter]);
objectTag->mObject->apply (macro);
counter++;
}
}
break;
default:
break;
}
}
catch (const std::exception& e)
{
Log(Debug::Error) << "Error in dropping instance: " << e.what();
}
int counter = 0; int counter = 0;
for(osg::ref_ptr<TagBase> tag: selection) for(osg::ref_ptr<TagBase> tag: selection)
{ {
if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag.get())) if (CSVRender::ObjectTag *objectTag = dynamic_cast<CSVRender::ObjectTag *> (tag.get()))
{ {
osg::ref_ptr<osg::Group> objectNodeWithGUI = objectTag->mObject->getRootNode(); osg::ref_ptr<osg::Group> objectNodeWithGUI = objectTag->mObject->getRootNode();
objectNodeWithGUI->setNodeMask(oldMasks[counter]); objectNodeWithGUI->setNodeMask(mOldMasks[counter]);
counter++; counter++;
} }
} }

@ -114,6 +114,19 @@ namespace CSVRender
void dropSelectedInstancesToTerrainSeparately(); void dropSelectedInstancesToTerrainSeparately();
void handleDropMethod(DropMode dropMode, QString commandMsg); void handleDropMethod(DropMode dropMode, QString commandMsg);
}; };
/// \brief Helper class to handle object mask data in safe way
class DropObjectDataHandler
{
public:
DropObjectDataHandler(WorldspaceWidget* worldspacewidget);
~DropObjectDataHandler();
std::vector<float> mObjectHeights;
private:
WorldspaceWidget* mWorldspaceWidget;
std::vector<osg::Node::NodeMask> mOldMasks;
};
} }
#endif #endif

Loading…
Cancel
Save