Viewport Ui Cluster Locked State Overlay (#1139)

* Viewport Ui Cluster Locked State Overlay

* PR feedback changes.
This commit is contained in:
Hasareej
2021-06-07 13:32:13 +01:00
committed by GitHub
parent 1ffcfa07e6
commit 5b940e8ed6
10 changed files with 98 additions and 1 deletions
@@ -2646,6 +2646,9 @@ namespace AzToolsFramework
m_spaceCluster.m_spaceLock = ReferenceFrame::World;
}
}
ViewportUi::ViewportUiRequestBus::Event(
ViewportUi::DefaultViewportId, &ViewportUi::ViewportUiRequestBus::Events::SetClusterButtonLocked,
m_spaceCluster.m_spaceClusterId, buttonId, m_spaceCluster.m_spaceLock.has_value());
};
m_spaceCluster.m_spaceSelectionHandler = AZ::Event<ViewportUi::ButtonId>::Handler(onButtonClicked);
@@ -108,6 +108,61 @@ namespace AzToolsFramework::ViewportUi::Internal
m_widgetCallbacks.Update();
}
void ViewportUiCluster::SetButtonLocked(const ButtonId buttonId, const bool isLocked)
{
const auto& buttons = m_buttonGroup->GetButtons();
// unlocked previously locked button
if (m_lockedButtonId.has_value() && isLocked)
{
// find the button to extract the old icon (without overlay)
auto findLocked = [this](const Button* button) { return (button->m_buttonId == m_lockedButtonId); };
if (auto lockedButtonIt = AZStd::find_if(buttons.begin(), buttons.end(), findLocked); lockedButtonIt != buttons.end())
{
// get the action corresponding to the lockedButtonId
if (auto actionEntry = m_buttonActionMap.find(m_lockedButtonId.value()); actionEntry != m_buttonActionMap.end())
{
// remove the overlay
auto action = actionEntry->second;
action->setIcon(QIcon(QString((*lockedButtonIt)->m_icon.c_str())));
}
}
}
auto found = [buttonId](Button* button) { return (button->m_buttonId == buttonId); };
if (auto buttonIt = AZStd::find_if(buttons.begin(), buttons.end(), found); buttonIt != buttons.end())
{
QIcon newIcon;
if (isLocked)
{
// overlay the locked icon ontop of the button's icon
QPixmap comboPixmap(24, 24);
comboPixmap.fill(Qt::transparent);
QPixmap firstImage(QString((*buttonIt)->m_icon.c_str()));
QPixmap secondImage(QString(":/stylesheet/img/UI20/toolbar/Locked_Status.svg"));
QPainter painter(&comboPixmap);
painter.drawPixmap(0, 0, firstImage);
painter.drawPixmap(0, 0, secondImage);
newIcon.addPixmap(comboPixmap);
m_lockedButtonId = buttonId;
}
else
{
// remove the overlay
newIcon = QIcon(QString((*buttonIt)->m_icon.c_str()));
m_lockedButtonId = AZStd::nullopt;
}
if (auto actionEntry = m_buttonActionMap.find(buttonId); actionEntry != m_buttonActionMap.end())
{
auto action = actionEntry->second;
action->setIcon(newIcon);
}
}
}
ViewportUiWidgetCallbacks ViewportUiCluster::GetWidgetCallbacks()
{
return m_widgetCallbacks;
@@ -17,6 +17,7 @@
#include <AzToolsFramework/ViewportUi/Button.h>
#include <AzToolsFramework/ViewportUi/ViewportUiWidgetCallbacks.h>
#include <QToolBar>
#include <QPainter>
namespace AzToolsFramework::ViewportUi::Internal
{
@@ -38,6 +39,8 @@ namespace AzToolsFramework::ViewportUi::Internal
void RemoveButton(ButtonId buttonId);
//! Updates all registered actions.
void Update();
//! Adds a locked overlay to the button's icon.
void SetButtonLocked(ButtonId buttonId, bool isLocked);
//! Returns the widget manager.
ViewportUiWidgetCallbacks GetWidgetCallbacks();
@@ -52,5 +55,6 @@ namespace AzToolsFramework::ViewportUi::Internal
AZStd::shared_ptr<ButtonGroup> m_buttonGroup; //!< Data structure which the cluster will be displaying to the Viewport UI.
AZStd::unordered_map<ButtonId, QPointer<QAction>> m_buttonActionMap; //!< Map for buttons to their corresponding actions.
ViewportUiWidgetCallbacks m_widgetCallbacks; //!< Registers actions and manages updates.
AZStd::optional<ButtonId> m_lockedButtonId = AZStd::nullopt; //!< Used to track the last button locked.
};
} // namespace AzToolsFramework::ViewportUi::Internal
@@ -100,6 +100,14 @@ namespace AzToolsFramework::ViewportUi::Internal
}
}
void ViewportUiDisplay::SetClusterButtonLocked(const ViewportUiElementId clusterId, const ButtonId buttonId, const bool isLocked)
{
if (auto viewportUiCluster = qobject_cast<ViewportUiCluster*>(GetViewportUiElement(clusterId).get()))
{
viewportUiCluster->SetButtonLocked(buttonId, isLocked);
}
}
void ViewportUiDisplay::RemoveClusterButton(ViewportUiElementId clusterId, ButtonId buttonId)
{
if (auto cluster = qobject_cast<ViewportUiCluster*>(GetViewportUiElement(clusterId).get()))
@@ -58,6 +58,7 @@ namespace AzToolsFramework::ViewportUi::Internal
void AddCluster(AZStd::shared_ptr<ButtonGroup> buttonGroup, Alignment align);
void AddClusterButton(ViewportUiElementId clusterId, Button* button);
void SetClusterButtonLocked(ViewportUiElementId clusterId, ButtonId buttonId, bool isLocked);
void RemoveClusterButton(ViewportUiElementId clusterId, ButtonId buttonId);
void UpdateCluster(const ViewportUiElementId clusterId);
@@ -67,6 +67,16 @@ namespace AzToolsFramework::ViewportUi
}
}
void ViewportUiManager::SetClusterButtonLocked(const ClusterId clusterId, const ButtonId buttonId, const bool isLocked)
{
if (auto clusterIt = m_clusterButtonGroups.find(clusterId); clusterIt != m_clusterButtonGroups.end())
{
auto cluster = clusterIt->second;
m_viewportUi->SetClusterButtonLocked(cluster->GetViewportUiElementId(), buttonId, isLocked);
UpdateButtonGroupUi(cluster.get());
}
}
void ViewportUiManager::RegisterClusterEventHandler(const ClusterId clusterId, AZ::Event<ButtonId>::Handler& handler)
{
if (auto clusterIt = m_clusterButtonGroups.find(clusterId); clusterIt != m_clusterButtonGroups.end())
@@ -35,6 +35,7 @@ namespace AzToolsFramework::ViewportUi
const SwitcherId CreateSwitcher(Alignment align) override;
void SetClusterActiveButton(ClusterId clusterId, ButtonId buttonId) override;
void SetSwitcherActiveButton(SwitcherId switcherId, ButtonId buttonId) override;
void SetClusterButtonLocked(ClusterId clusterId, ButtonId buttonId, bool isLocked) override;
const ButtonId CreateClusterButton(ClusterId clusterId, const AZStd::string& icon) override;
const ButtonId CreateSwitcherButton(
SwitcherId switcherId, const AZStd::string& icon, const AZStd::string& name = AZStd::string()) override;
@@ -65,6 +65,8 @@ namespace AzToolsFramework::ViewportUi
virtual void SetClusterActiveButton(ClusterId clusterId, ButtonId buttonId) = 0;
//! Sets the active button of the switcher. This is the button which has a text label.
virtual void SetSwitcherActiveButton(SwitcherId clusterId, ButtonId buttonId) = 0;
//! Adds a locked overlay to the cluster button's icon.
virtual void SetClusterButtonLocked(ClusterId clusterId, ButtonId buttonId, bool isLocked) = 0;
//! Registers a new button onto a cluster.
virtual const ButtonId CreateClusterButton(const ClusterId clusterId, const AZStd::string& icon) = 0;
//! Registers a new button onto a switcher.