DefaultSlotLayoutComponent.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <Components/Slots/Default/DefaultSlotLayoutComponent.h>
  9. #include <Components/Slots/SlotConnectionPin.h>
  10. #include <GraphCanvas/Components/Slots/SlotBus.h>
  11. namespace GraphCanvas
  12. {
  13. //////////////////////
  14. // DefaultSlotLayout
  15. //////////////////////
  16. DefaultSlotLayout::DefaultSlotLayout(DefaultSlotLayoutComponent& owner)
  17. : m_owner(owner)
  18. {
  19. m_slotConnectionPin = aznew SlotConnectionPin(owner.GetEntityId());
  20. m_slotText = aznew GraphCanvasLabel();
  21. OnStyleChanged();
  22. }
  23. DefaultSlotLayout::~DefaultSlotLayout()
  24. {
  25. }
  26. void DefaultSlotLayout::Activate()
  27. {
  28. StyleNotificationBus::Handler::BusConnect(m_owner.GetEntityId());
  29. SceneMemberNotificationBus::Handler::BusConnect(m_owner.GetEntityId());
  30. m_slotConnectionPin->Activate();
  31. }
  32. void DefaultSlotLayout::Deactivate()
  33. {
  34. m_slotConnectionPin->Deactivate();
  35. StyleNotificationBus::Handler::BusDisconnect();
  36. SceneMemberNotificationBus::Handler::BusDisconnect();
  37. }
  38. void DefaultSlotLayout::OnSceneSet(const AZ::EntityId&)
  39. {
  40. AZStd::string slotName;
  41. SlotRequestBus::EventResult(slotName, m_owner.GetEntityId(), &SlotRequests::GetName);
  42. SlotRequestBus::EventResult(m_connectionType, m_owner.GetEntityId(), &SlotRequests::GetConnectionType);
  43. m_slotText->SetLabel(slotName);
  44. UpdateLayout();
  45. OnStyleChanged();
  46. }
  47. void DefaultSlotLayout::OnSceneReady()
  48. {
  49. UpdateLayout();
  50. OnStyleChanged();
  51. }
  52. void DefaultSlotLayout::OnStyleChanged()
  53. {
  54. m_style.SetStyle(m_owner.GetEntityId());
  55. m_slotText->SetStyle(m_owner.GetEntityId(), ".slotName");
  56. m_slotConnectionPin->RefreshStyle();
  57. qreal padding = m_style.GetAttribute(Styling::Attribute::Padding, 2.);
  58. setContentsMargins(padding, padding, padding, padding);
  59. setSpacing(m_style.GetAttribute(Styling::Attribute::Spacing, 2.));
  60. UpdateGeometry();
  61. }
  62. void DefaultSlotLayout::UpdateLayout()
  63. {
  64. for (int i = count() - 1; i >= 0; --i)
  65. {
  66. removeAt(i);
  67. }
  68. switch (m_connectionType)
  69. {
  70. case ConnectionType::CT_Input:
  71. setOrientation(Qt::Horizontal);
  72. addItem(m_slotConnectionPin);
  73. addItem(m_slotText);
  74. setAlignment(m_slotConnectionPin, Qt::AlignLeft);
  75. setAlignment(m_slotText, Qt::AlignLeft);
  76. break;
  77. case ConnectionType::CT_Output:
  78. setOrientation(Qt::Horizontal);
  79. addItem(m_slotText);
  80. addItem(m_slotConnectionPin);
  81. setAlignment(m_slotText, Qt::AlignRight);
  82. setAlignment(m_slotConnectionPin, Qt::AlignRight);
  83. break;
  84. default:
  85. setOrientation(Qt::Horizontal);
  86. addItem(m_slotConnectionPin);
  87. addItem(m_slotText);
  88. break;
  89. }
  90. UpdateGeometry();
  91. }
  92. void DefaultSlotLayout::UpdateGeometry()
  93. {
  94. m_slotConnectionPin->updateGeometry();
  95. m_slotText->update();
  96. invalidate();
  97. updateGeometry();
  98. }
  99. ///////////////////////////////
  100. // DefaultSlotLayoutComponent
  101. ///////////////////////////////
  102. void DefaultSlotLayoutComponent::Reflect(AZ::ReflectContext* reflectContext)
  103. {
  104. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflectContext);
  105. if (serializeContext)
  106. {
  107. serializeContext->Class<DefaultSlotLayoutComponent, AZ::Component>()
  108. ->Version(1)
  109. ;
  110. }
  111. }
  112. DefaultSlotLayoutComponent::DefaultSlotLayoutComponent()
  113. : m_defaultSlotLayout(nullptr)
  114. {
  115. }
  116. void DefaultSlotLayoutComponent::Init()
  117. {
  118. SlotLayoutComponent::Init();
  119. m_defaultSlotLayout = aznew DefaultSlotLayout((*this));
  120. SetLayout(m_defaultSlotLayout);
  121. }
  122. void DefaultSlotLayoutComponent::Activate()
  123. {
  124. SlotLayoutComponent::Activate();
  125. m_defaultSlotLayout->Activate();
  126. }
  127. void DefaultSlotLayoutComponent::Deactivate()
  128. {
  129. m_defaultSlotLayout->Deactivate();
  130. SlotLayoutComponent::Deactivate();
  131. }
  132. }