IconComponent.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "IconComponent.h"
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContextConstants.inl>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Component/ComponentApplicationBus.h>
  13. namespace ScriptCanvasEditor
  14. {
  15. AZStd::string IconComponent::LookupClassIcon(const AZ::Uuid& classId)
  16. {
  17. AZStd::string iconPath = "Icons/ScriptCanvas/Placeholder.png";
  18. AZ::SerializeContext* serializeContext = nullptr;
  19. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext);
  20. AZ_Assert(serializeContext, "Failed to acquire application serialize context.");
  21. const AZ::SerializeContext::ClassData* classData = serializeContext->FindClassData(classId);
  22. // Find the icon's path in the editor data attributes
  23. if (classData && classData->m_editData)
  24. {
  25. auto editorElementData = classData->m_editData->FindElementData(AZ::Edit::ClassElements::EditorData);
  26. if (editorElementData)
  27. {
  28. if (auto iconAttribute = editorElementData->FindAttribute(AZ::Edit::Attributes::Icon))
  29. {
  30. if (auto iconAttributeData = azdynamic_cast<const AZ::Edit::AttributeData<const char*>*>(iconAttribute))
  31. {
  32. AZStd::string iconAttributeValue = iconAttributeData->Get(nullptr);
  33. if (!iconAttributeValue.empty())
  34. {
  35. iconPath = AZStd::move(iconAttributeValue);
  36. }
  37. }
  38. }
  39. }
  40. }
  41. return iconPath;
  42. }
  43. IconComponent::IconComponent(const AZ::Uuid& classId)
  44. {
  45. m_iconPath = LookupClassIcon(classId);
  46. }
  47. void IconComponent::Reflect(AZ::ReflectContext* context)
  48. {
  49. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  50. if (serializeContext)
  51. {
  52. serializeContext->Class<IconComponent, AZ::Component>()
  53. ->Version(1)
  54. ->Field("m_iconPath", &IconComponent::m_iconPath)
  55. ;
  56. AZ::EditContext* editContext = serializeContext->GetEditContext();
  57. if (editContext)
  58. {
  59. editContext->Class<IconComponent>("Icon", "Represents a icon path")
  60. ->ClassElement(AZ::Edit::ClassElements::EditorData, "Icon Components class attributes")
  61. ;
  62. }
  63. }
  64. }
  65. void IconComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  66. {
  67. provided.push_back(AZ_CRC_CE("GraphCanvas_IconService"));
  68. }
  69. void IconComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  70. {
  71. incompatible.push_back(AZ_CRC_CE("GraphCanvas_IconService"));
  72. }
  73. void IconComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  74. {
  75. (void)dependent;
  76. }
  77. void IconComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  78. {
  79. (void)required;
  80. }
  81. void IconComponent::Init()
  82. {
  83. }
  84. void IconComponent::Activate()
  85. {
  86. IconBus::Handler::BusConnect(GetEntityId());
  87. }
  88. void IconComponent::Deactivate()
  89. {
  90. IconBus::Handler::BusDisconnect();
  91. }
  92. AZStd::string IconComponent::GetIconPath() const
  93. {
  94. return m_iconPath;
  95. }
  96. }