Files
o3de/Gems/EMotionFX/Code/Source/Editor/ActorJointBrowseEdit.cpp
T
Chris Burel 8884227fe6 Remove MCore::Array
This translates all usages of MCore::Array to AZStd::vector. It is
designed to be as minimal of a change as possible (no changing to
range-for loops or other C++11 stuff).

We can decide to submit this wholesale, or submit it to a separate
branch that we can then integrate individual files from once we're ready
to do a specific class's transition.

It does not completely solve the `uint32`->`size_t` transition.

One important finding from doing this: `MCore::Array` uses a `memcpy`
when it reallocates. `AZStd::vector` will use the contained type's copy
or move constructor, per element. This is a significant change in
behavior. If you have type, `SomeStruct` that defines a destructor, that
type is copyable and not movable. So if you have a
`MCore::Array<SomeStruct>`, and you call `Add(); Add(); Add()`, that
reallocates 3 times, copying the contents using `memcpy`, and never
invokes `SomeStruct`'s copy constructor or destructor. Translating that
to `AZStd::vector<SomeStruct>` and calling `push_back(); push_back();
push_back();` will still reallocate 3 times, but it sees that
`SomeStruct` is non-movable, and uses the copy constructor to make the
copies, and then the destructor on the previous values. This call to the
destructor wasn't there before, and can cause things to be deleted that
weren't before. The solution to this is to make that struct be a
move-only type. Where possible, this was done by changing that type to
use `AZStd::unique_ptr` instead of a raw pointer, to get the proper move
behavior. Where that is not possible (types that inherit from
`MCore::MemoryObject`), a hand-written move constructor was created.

In general:
GetLength() becomes size()
GetMaxLength() becomes capacity()
GetIsEmpty() becomes empty()
Reserve() becomes reserve()
ReserveExact() becomes reserve()
Resize() becomes resize()
ResizeFast() becomes resize_no_construct()
Add() becomes emplace_back()
AddExact() becomes emplace_back()
AddEmpty() becomes emplace_back()
AddEmptyExact() becomes emplace_back()
GetPtr() becomes data()
GetItem() becomes at()
Shrink() becomes shrink_to_fit()
GetFirst() becomes front()
GetLast() becomes back()
Remove() becomes erase()
RemoveFirst() becomes erase()
RemoveLast() becomes pop_back()
RemoveByValue() becomes if (const auto it = AZStd::find(...); it != end(container)) container.erase(it);
Insert() becomes emplace()
Swap() becomes swap()
Clear(true) becomes clear(); shrink_to_fit()
Clear() becomes clear(); shrink_to_fit()
Clear(false) becomes clear()
Swap() becomes swap()
Find() becomes AZStd::find
MoveElements() becomes AZStd::move
SetMemoryCategory() is removed

Signed-off-by: Chris Burel <burelc@amazon.com>
2021-08-09 08:36:43 -07:00

172 lines
5.9 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AzCore/Memory/SystemAllocator.h>
#include <Editor/ActorJointBrowseEdit.h>
#include <EMotionFX/Tools/EMotionStudio/EMStudioSDK/Source/NodeSelectionWindow.h>
#include <EMotionFX/CommandSystem/Source/CommandManager.h>
#include <EMotionFX/Source/ActorManager.h>
#include <QTreeWidget>
namespace EMStudio
{
AZ_CLASS_ALLOCATOR_IMPL(ActorJointBrowseEdit, AZ::SystemAllocator, 0)
ActorJointBrowseEdit::ActorJointBrowseEdit(QWidget* parent)
: AzQtComponents::BrowseEdit(parent)
{
connect(this, &AzQtComponents::BrowseEdit::attachedButtonTriggered, this, &ActorJointBrowseEdit::OnBrowseButtonClicked);
connect(this, &AzQtComponents::BrowseEdit::textEdited, this, &ActorJointBrowseEdit::OnTextEdited);
SetSingleJointSelection(m_singleJointSelection);
setClearButtonEnabled(true);
setLineEditReadOnly(true);
}
void ActorJointBrowseEdit::SetSingleJointSelection(bool singleSelectionEnabled)
{
m_singleJointSelection = singleSelectionEnabled;
UpdatePlaceholderText();
}
void ActorJointBrowseEdit::UpdatePlaceholderText()
{
const QString placeholderText = QString("Select joint%1").arg(m_singleJointSelection ? "" : "s");
setPlaceholderText(placeholderText);
}
void ActorJointBrowseEdit::OnBrowseButtonClicked()
{
const EMotionFX::ActorInstance* actorInstance = GetActorInstance();
if (!actorInstance)
{
AZ_Warning("EMotionFX", false, "Cannot open joint selection window. Please select an actor instance first.");
return;
}
CommandSystem::SelectionList selectionList;
for (const SelectionItem& selectedJoint : m_selectedJoints)
{
EMotionFX::Node* node = selectedJoint.GetNode();
if (node)
{
selectionList.AddNode(node);
}
}
AZ_Warning("EMotionFX", m_singleJointSelection && selectionList.GetNumSelectedNodes() > 1,
"Single selection actor joint window has multiple pre-selected joints.");
m_previouslySelectedJoints = m_selectedJoints;
m_jointSelectionWindow = new NodeSelectionWindow(this, m_singleJointSelection);
connect(m_jointSelectionWindow, &NodeSelectionWindow::rejected, this, &ActorJointBrowseEdit::OnSelectionRejected);
connect(m_jointSelectionWindow->GetNodeHierarchyWidget()->GetTreeWidget(), &QTreeWidget::itemSelectionChanged, this, &ActorJointBrowseEdit::OnSelectionChanged);
NodeSelectionWindow::connect(m_jointSelectionWindow, &QDialog::finished, [=]([[maybe_unused]] int resultCode)
{
m_jointSelectionWindow->deleteLater();
m_jointSelectionWindow = nullptr;
});
m_jointSelectionWindow->open();
m_jointSelectionWindow->Update(actorInstance->GetID(), &selectionList);
}
void ActorJointBrowseEdit::SetSelectedJoints(const AZStd::vector<SelectionItem>& selectedJoints)
{
if (m_singleJointSelection && selectedJoints.size() > 1)
{
AZ_Warning("EMotionFX", true, "Cannot select multiple joints for single selection actor joint browse edit. Only the first joint will be selected.");
m_selectedJoints = {selectedJoints[0]};
}
else
{
m_selectedJoints = selectedJoints;
}
QString text;
const size_t numJoints = m_selectedJoints.size();
switch (numJoints)
{
case 0:
{
// Leave text empty (shows placeholder text).
break;
}
case 1:
{
text = m_selectedJoints[0].GetNodeName();
break;
}
default:
{
text = QString("%1 joints").arg(numJoints);
}
}
setText(text);
}
void ActorJointBrowseEdit::OnSelectionDone(const AZStd::vector<SelectionItem>& selectedJoints)
{
SetSelectedJoints(selectedJoints);
emit SelectionDone(selectedJoints);
}
void ActorJointBrowseEdit::OnSelectionChanged()
{
if (m_jointSelectionWindow)
{
NodeHierarchyWidget* hierarchyWidget = m_jointSelectionWindow->GetNodeHierarchyWidget();
hierarchyWidget->UpdateSelection();
AZStd::vector<SelectionItem>& selectedJoints = hierarchyWidget->GetSelectedItems();
emit SelectionChanged(selectedJoints);
}
}
void ActorJointBrowseEdit::OnSelectionRejected()
{
emit SelectionRejected(m_previouslySelectedJoints);
}
void ActorJointBrowseEdit::OnTextEdited(const QString& text)
{
if (text.isEmpty())
{
OnSelectionDone(/*selectedJoints=*/{});
}
}
EMotionFX::ActorInstance* ActorJointBrowseEdit::GetActorInstance() const
{
const CommandSystem::SelectionList& selectionList = CommandSystem::GetCommandManager()->GetCurrentSelection();
EMotionFX::ActorInstance* actorInstance = selectionList.GetSingleActorInstance();
if (actorInstance)
{
return actorInstance;
}
EMotionFX::Actor* actor = selectionList.GetSingleActor();
if (actor)
{
const uint32 numActorInstances = EMotionFX::GetActorManager().GetNumActorInstances();
for (uint32 i = 0; i < numActorInstances; ++i)
{
EMotionFX::ActorInstance* actorInstance2 = EMotionFX::GetActorManager().GetActorInstance(i);
if (actorInstance2->GetActor() == actor)
{
return actorInstance2;
}
}
}
return nullptr;
}
} // namespace EMStudio