Skip to content

Commit

Permalink
Merge branch 'copy-that' into 'master'
Browse files Browse the repository at this point in the history
Feat(CS): Implement Instance Cloning

See merge request OpenMW/openmw!3654
  • Loading branch information
Assumeru committed Dec 19, 2023
2 parents 7922f0e + bc662ae commit 36b61d7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/opencs/model/prefs/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ void CSMPrefs::State::declare()
declareShortcut("scene-edit-abort", "Abort", QKeySequence(Qt::Key_Escape));
declareShortcut("scene-focus-toolbar", "Toggle Toolbar Focus", QKeySequence(Qt::Key_T));
declareShortcut("scene-render-stats", "Debug Rendering Stats", QKeySequence(Qt::Key_F3));
declareShortcut("scene-duplicate", "Duplicate Instance", QKeySequence(Qt::ShiftModifier | Qt::Key_C));

declareSubcategory("1st/Free Camera");
declareShortcut("free-forward", "Forward", QKeySequence(Qt::Key_W));
Expand Down
1 change: 1 addition & 0 deletions apps/opencs/model/prefs/values.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ namespace CSMPrefs
"scene-instance-drop-terrain-separately", "" };
Settings::SettingValue<std::string> mSceneInstanceDropCollisionSeparately{ mIndex, sName,
"scene-instance-drop-collision-separately", "" };
Settings::SettingValue<std::string> mSceneDuplicate{ mIndex, sName, "scene-duplicate", "Shift+C" };
Settings::SettingValue<std::string> mSceneLoadCamCell{ mIndex, sName, "scene-load-cam-cell", "Keypad+5" };
Settings::SettingValue<std::string> mSceneLoadCamEastcell{ mIndex, sName, "scene-load-cam-eastcell",
"Keypad+6" };
Expand Down
10 changes: 10 additions & 0 deletions apps/opencs/model/world/refcollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,23 @@ void CSMWorld::RefCollection::cloneRecord(
const ESM::RefId& origin, const ESM::RefId& destination, const UniversalId::Type type)
{
auto copy = std::make_unique<Record<CellRef>>();
int index = getAppendIndex(ESM::RefId(), type);

copy->mModified = getRecord(origin).get();
copy->mState = RecordBase::State_ModifiedOnly;

copy->get().mId = destination;
copy->get().mIdNum = extractIdNum(destination.getRefIdString());

if (copy->get().mRefNum.mContentFile != 0)
{
mRefIndex.insert(std::make_pair(static_cast<Record<CellRef>*>(copy.get())->get().mIdNum, index));
copy->get().mRefNum.mContentFile = 0;
copy->get().mRefNum.mIndex = index;
}
else
copy->get().mRefNum.mIndex = copy->get().mIdNum;

insertRecord(std::move(copy), getAppendIndex(destination, type)); // call RefCollection::insertRecord()
}

Expand Down
31 changes: 29 additions & 2 deletions apps/opencs/view/render/instancemode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,21 @@ CSVRender::InstanceMode::InstanceMode(
connect(this, &InstanceMode::requestFocus, worldspaceWidget, &WorldspaceWidget::requestFocus);

CSMPrefs::Shortcut* deleteShortcut = new CSMPrefs::Shortcut("scene-delete", worldspaceWidget);
connect(deleteShortcut, qOverload<>(&CSMPrefs::Shortcut::activated), this, &InstanceMode::deleteSelectedInstances);

CSMPrefs::Shortcut* duplicateShortcut = new CSMPrefs::Shortcut("scene-duplicate", worldspaceWidget);

connect(
deleteShortcut, qOverload<bool>(&CSMPrefs::Shortcut::activated), this, &InstanceMode::deleteSelectedInstances);
duplicateShortcut, qOverload<>(&CSMPrefs::Shortcut::activated), this, &InstanceMode::cloneSelectedInstances);

// Following classes could be simplified by using QSignalMapper, which is obsolete in Qt5.10, but not in Qt4.8 and
// Qt5.14
CSMPrefs::Shortcut* dropToCollisionShortcut
= new CSMPrefs::Shortcut("scene-instance-drop-collision", worldspaceWidget);

connect(dropToCollisionShortcut, qOverload<>(&CSMPrefs::Shortcut::activated), this,
&InstanceMode::dropSelectedInstancesToCollision);

CSMPrefs::Shortcut* dropToTerrainLevelShortcut
= new CSMPrefs::Shortcut("scene-instance-drop-terrain", worldspaceWidget);
connect(dropToTerrainLevelShortcut, qOverload<>(&CSMPrefs::Shortcut::activated), this,
Expand Down Expand Up @@ -1068,7 +1074,7 @@ void CSVRender::InstanceMode::handleSelectDrag(const QPoint& pos)
mDragMode = DragMode_None;
}

void CSVRender::InstanceMode::deleteSelectedInstances(bool active)
void CSVRender::InstanceMode::deleteSelectedInstances()
{
std::vector<osg::ref_ptr<TagBase>> selection = getWorldspaceWidget().getSelection(Mask_Reference);
if (selection.empty())
Expand All @@ -1087,6 +1093,27 @@ void CSVRender::InstanceMode::deleteSelectedInstances(bool active)
getWorldspaceWidget().clearSelection(Mask_Reference);
}

void CSVRender::InstanceMode::cloneSelectedInstances()
{
std::vector<osg::ref_ptr<TagBase>> selection = getWorldspaceWidget().getSelection(Mask_Reference);
if (selection.empty())
return;

CSMDoc::Document& document = getWorldspaceWidget().getDocument();
CSMWorld::IdTable& referencesTable
= dynamic_cast<CSMWorld::IdTable&>(*document.getData().getTableModel(CSMWorld::UniversalId::Type_References));
QUndoStack& undoStack = document.getUndoStack();

CSMWorld::CommandMacro macro(undoStack, "Clone Instances");
for (osg::ref_ptr<TagBase> tag : selection)
if (CSVRender::ObjectTag* objectTag = dynamic_cast<CSVRender::ObjectTag*>(tag.get()))
{
macro.push(new CSMWorld::CloneCommand(referencesTable, objectTag->mObject->getReferenceId(),
"ref#" + std::to_string(referencesTable.rowCount()), CSMWorld::UniversalId::Type_Reference));
}
// getWorldspaceWidget().clearSelection(Mask_Reference);
}

void CSVRender::InstanceMode::dropInstance(CSVRender::Object* object, float dropHeight)
{
object->setEdited(Object::Override_Position);
Expand Down
3 changes: 2 additions & 1 deletion apps/opencs/view/render/instancemode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ namespace CSVRender
private slots:

void subModeChanged(const std::string& id);
void deleteSelectedInstances(bool active);
void deleteSelectedInstances();
void cloneSelectedInstances();
void dropSelectedInstancesToCollision();
void dropSelectedInstancesToTerrain();
void dropSelectedInstancesToCollisionSeparately();
Expand Down

0 comments on commit 36b61d7

Please sign in to comment.