GradientPreviewDataWidget.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "GradientPreviewDataWidget.h"
  9. #include <GradientSignal/Ebuses/GradientPreviewContextRequestBus.h>
  10. #include <AzCore/Component/EntityId.h>
  11. #include <QVBoxLayout>
  12. namespace GradientSignal
  13. {
  14. //
  15. // GradientPreviewDataWidgetHandler
  16. //
  17. AZ::u32 GradientPreviewDataWidgetHandler::GetHandlerName() const
  18. {
  19. return AZ_CRC_CE("GradientPreviewer");
  20. }
  21. void GradientPreviewDataWidgetHandler::ConsumeAttribute(GradientPreviewDataWidget* GUI, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName)
  22. {
  23. (void)debugName;
  24. if (attrib == AZ_CRC_CE("GradientFilter"))
  25. {
  26. GradientPreviewWidget::SampleFilterFunc filterFunc;
  27. if (attrValue->Read<GradientPreviewWidget::SampleFilterFunc>(filterFunc))
  28. {
  29. GUI->SetGradientSampleFilter(filterFunc);
  30. }
  31. }
  32. else if (attrib == AZ_CRC_CE("GradientSampler"))
  33. {
  34. GradientSampler* sampler = nullptr;
  35. if (attrValue->Read<GradientSampler*>(sampler) && sampler)
  36. {
  37. GUI->SetGradientSampler(*sampler);
  38. }
  39. }
  40. else if (attrib == AZ_CRC_CE("GradientEntity"))
  41. {
  42. AZ::EntityId id;
  43. if (attrValue->Read<AZ::EntityId>(id))
  44. {
  45. GUI->SetGradientEntity(id);
  46. }
  47. }
  48. }
  49. bool GradientPreviewDataWidgetHandler::ReadValueIntoGUI(size_t index, GradientPreviewDataWidget* GUI, void* value, const AZ::Uuid& propertyType)
  50. {
  51. (void)index;
  52. (void)value;
  53. (void)propertyType;
  54. GUI->Refresh();
  55. return false;
  56. }
  57. QWidget* GradientPreviewDataWidgetHandler::CreateGUI(QWidget* pParent)
  58. {
  59. return new GradientPreviewDataWidget(pParent);
  60. }
  61. void GradientPreviewDataWidgetHandler::PreventRefresh(QWidget* GUI, bool preventRefresh)
  62. {
  63. // Notify our preview widget to disable / enable itself during refreshes. Because it uses
  64. // delayed, threaded logic it's possible for it to query a component during a time that the component
  65. // is deactivated / deleted. This notification tells the preview widget to immediately cancel any
  66. // refreshes, or restart them when it's safe again.
  67. auto previewWidget = reinterpret_cast<GradientPreviewDataWidget*>(GUI);
  68. previewWidget->PreventRefresh(preventRefresh);
  69. }
  70. void GradientPreviewDataWidgetHandler::Register()
  71. {
  72. using namespace AzToolsFramework;
  73. // Property handlers are set to auto-delete by default, which means that we're handing off ownership of the pointer to the
  74. // PropertyManagerComponent, where it will get cleaned up on system shutdown.
  75. auto propertyHandler = aznew GradientPreviewDataWidgetHandler();
  76. AZ_Assert(propertyHandler->AutoDelete(),
  77. "GradientPreviewDataWidgetHandler is no longer set to auto-delete, it will leak memory.");
  78. PropertyTypeRegistrationMessages::Bus::Broadcast(
  79. &PropertyTypeRegistrationMessages::Bus::Events::RegisterPropertyType, propertyHandler);
  80. }
  81. void GradientPreviewDataWidgetHandler::Unregister()
  82. {
  83. // We don't need to call UnregisterPropertyType here because it's an autoDelete handler.
  84. }
  85. //
  86. // GradientPreviewDataWidget
  87. //
  88. GradientPreviewDataWidget::GradientPreviewDataWidget(QWidget * parent)
  89. : QWidget(parent)
  90. {
  91. QVBoxLayout* layout = new QVBoxLayout(this);
  92. layout->setContentsMargins(QMargins());
  93. layout->setAlignment(Qt::AlignHCenter);
  94. constexpr bool enablePopout = true;
  95. m_preview = new GradientPreviewWidget(this, enablePopout);
  96. m_preview->setFixedSize(256, 256);
  97. layout->addWidget(m_preview);
  98. QObject::connect(m_preview, &GradientPreviewWidget::popoutClicked, this, [this]()
  99. {
  100. delete m_previewWindow;
  101. m_previewWindow = new GradientPreviewWidget;
  102. // Make sure our popout preview always stays on top
  103. m_previewWindow->setWindowFlag(Qt::WindowStaysOnTopHint, true);
  104. // We need to call show() once before the resize to initialize the window frame width/height,
  105. // so that way the resize correctly takes them into account. We then call show() a second time
  106. // afterwards to cause the resize to take effect.
  107. m_previewWindow->show();
  108. m_previewWindow->resize(750, 750);
  109. m_previewWindow->show();
  110. Refresh();
  111. });
  112. //dependency monitor must be connected to an owner/observer as a target for notifications.
  113. //generating a place holder entity
  114. m_observerEntityStub = AZ::Entity::MakeId();
  115. LmbrCentral::DependencyNotificationBus::Handler::BusConnect(m_observerEntityStub);
  116. }
  117. GradientPreviewDataWidget::~GradientPreviewDataWidget()
  118. {
  119. GradientPreviewRequestBus::Handler::BusDisconnect();
  120. LmbrCentral::DependencyNotificationBus::Handler::BusDisconnect();
  121. m_dependencyMonitor.Reset();
  122. delete m_previewWindow;
  123. }
  124. void GradientPreviewDataWidget::PreventRefresh(bool preventRefresh)
  125. {
  126. m_preventRefresh = preventRefresh;
  127. if (m_preventRefresh)
  128. {
  129. // If we're trying to prevent refreshes, cancel any existing or pending refreshes.
  130. CancelRefresh();
  131. }
  132. else
  133. {
  134. // If we're allowing refreshes again, start one up if it has been requested during
  135. // the time that we weren't allowing them.
  136. if (m_refreshQueued)
  137. {
  138. Refresh();
  139. }
  140. }
  141. }
  142. void GradientPreviewDataWidget::SetGradientSampler(const GradientSampler& sampler)
  143. {
  144. m_sampler = sampler;
  145. GradientPreviewRequestBus::Handler::BusDisconnect();
  146. GradientPreviewRequestBus::Handler::BusConnect(sampler.m_ownerEntityId);
  147. Refresh();
  148. }
  149. void GradientPreviewDataWidget::SetGradientSampleFilter(GradientPreviewWidget::SampleFilterFunc sampleFunc)
  150. {
  151. m_sampleFilterFunc = sampleFunc;
  152. Refresh();
  153. }
  154. void GradientPreviewDataWidget::SetGradientEntity(const AZ::EntityId& id)
  155. {
  156. m_sampler = {};
  157. m_sampler.m_gradientId = id;
  158. m_sampler.m_ownerEntityId = id;
  159. GradientPreviewRequestBus::Handler::BusDisconnect();
  160. GradientPreviewRequestBus::Handler::BusConnect(id);
  161. Refresh();
  162. }
  163. void GradientPreviewDataWidget::OnCompositionChanged()
  164. {
  165. Refresh();
  166. }
  167. void GradientPreviewDataWidget::Refresh()
  168. {
  169. // If we currently aren't allowing refreshes, just note that it's been requested so that
  170. // we can start it up once refreshes are allowed again.
  171. if (m_preventRefresh)
  172. {
  173. m_refreshQueued = true;
  174. return;
  175. }
  176. if (!m_refreshInProgress)
  177. {
  178. m_refreshInProgress = true;
  179. m_dependencyMonitor.Reset();
  180. m_dependencyMonitor.ConnectOwner(m_observerEntityStub);
  181. m_dependencyMonitor.ConnectDependency(m_sampler.m_gradientId);
  182. AZ::EntityId previewEntity;
  183. GradientPreviewContextRequestBus::BroadcastResult(previewEntity, &GradientPreviewContextRequestBus::Events::GetPreviewEntity);
  184. m_dependencyMonitor.ConnectDependency(previewEntity);
  185. for (GradientPreviewWidget* previewer : { m_preview, m_previewWindow })
  186. {
  187. if (previewer)
  188. {
  189. previewer->SetGradientSampler(m_sampler);
  190. previewer->SetGradientSampleFilter(m_sampleFilterFunc);
  191. previewer->QueueUpdate();
  192. }
  193. }
  194. m_refreshInProgress = false;
  195. m_refreshQueued = false;
  196. }
  197. }
  198. AZ::EntityId GradientPreviewDataWidget::CancelRefresh()
  199. {
  200. bool cancelled = false;
  201. for (GradientPreviewWidget* previewer : { m_preview, m_previewWindow })
  202. {
  203. if (previewer)
  204. {
  205. cancelled |= previewer->OnCancelRefresh();
  206. }
  207. }
  208. if (cancelled)
  209. {
  210. return m_sampler.m_gradientId;
  211. }
  212. return AZ::EntityId();
  213. }
  214. } //namespace GradientSignal