CommentNodeTextComponent.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 <AzCore/PlatformDef.h>
  9. AZ_PUSH_DISABLE_WARNING(4251 4800 4244, "-Wunknown-warning-option")
  10. #include <QGraphicsItem>
  11. #include <QGraphicsGridLayout>
  12. #include <QGraphicsLayoutItem>
  13. #include <QGraphicsScene>
  14. #include <QGraphicsWidget>
  15. #include <QPainter>
  16. AZ_POP_DISABLE_WARNING
  17. #include <AzCore/RTTI/TypeInfo.h>
  18. #include <Components/Nodes/Comment/CommentNodeTextComponent.h>
  19. #include <Components/Nodes/Comment/CommentTextGraphicsWidget.h>
  20. #include <Components/Nodes/General/GeneralNodeFrameComponent.h>
  21. #include <GraphCanvas/Components/GridBus.h>
  22. #include <GraphCanvas/Components/Nodes/NodeBus.h>
  23. #include <GraphCanvas/Components/Slots/SlotBus.h>
  24. #include <GraphCanvas/Editor/GraphModelBus.h>
  25. #include <GraphCanvas/GraphCanvasBus.h>
  26. #include <GraphCanvas/tools.h>
  27. #include <GraphCanvas/Styling/StyleHelper.h>
  28. #include <GraphCanvas/Utils/ConversionUtils.h>
  29. namespace GraphCanvas
  30. {
  31. /////////////////////////////
  32. // CommentNodeTextComponent
  33. /////////////////////////////
  34. bool CommentNodeTextComponentVersionConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  35. {
  36. if (classElement.GetVersion() <= 2)
  37. {
  38. AZ::Crc32 commentId = AZ_CRC("Comment", 0x9474526c);
  39. AZ::Crc32 fontId = AZ_CRC("FontSettings", 0x9d90b4cf);
  40. CommentNodeTextSaveData saveData;
  41. AZ::SerializeContext::DataElementNode* dataNode = classElement.FindSubElement(commentId);
  42. if (dataNode)
  43. {
  44. dataNode->GetData(saveData.m_comment);
  45. }
  46. dataNode = nullptr;
  47. dataNode = classElement.FindSubElement(fontId);
  48. if (dataNode)
  49. {
  50. dataNode->GetData(saveData.m_fontConfiguration);
  51. }
  52. classElement.RemoveElementByName(commentId);
  53. classElement.RemoveElementByName(fontId);
  54. classElement.AddElementWithData(context, "SaveData", saveData);
  55. }
  56. return true;
  57. }
  58. void CommentNodeTextComponent::Reflect(AZ::ReflectContext* context)
  59. {
  60. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  61. if (serializeContext)
  62. {
  63. serializeContext->Class<CommentNodeTextSaveData, ComponentSaveData>()
  64. ->Version(2)
  65. ->Field("Comment", &CommentNodeTextSaveData::m_comment)
  66. ->Field("BackgroundColor", &CommentNodeTextSaveData::m_backgroundColor)
  67. ->Field("FontSettings", &CommentNodeTextSaveData::m_fontConfiguration)
  68. ;
  69. serializeContext->Class<CommentNodeTextComponent, GraphCanvasPropertyComponent>()
  70. ->Version(3, &CommentNodeTextComponentVersionConverter)
  71. ->Field("SaveData", &CommentNodeTextComponent::m_saveData)
  72. ;
  73. serializeContext->Class<FontConfiguration>()
  74. ->Field("FontColor", &FontConfiguration::m_fontColor)
  75. ->Field("FontFamily", &FontConfiguration::m_fontFamily)
  76. ->Field("PixelSize", &FontConfiguration::m_pixelSize)
  77. ->Field("Weight", &FontConfiguration::m_weight)
  78. ->Field("Style", &FontConfiguration::m_style)
  79. ->Field("VAlign", &FontConfiguration::m_verticalAlignment)
  80. ->Field("HAlign", &FontConfiguration::m_horizontalAlignment)
  81. ;
  82. AZ::EditContext* editContext = serializeContext->GetEditContext();
  83. if (editContext)
  84. {
  85. editContext->Class < CommentNodeTextSaveData >("SaveData", "The save information regarding a comment node")
  86. ->ClassElement(AZ::Edit::ClassElements::EditorData, "Properties")
  87. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  88. ->DataElement(AZ::Edit::UIHandlers::Default, &CommentNodeTextSaveData::m_comment, "Title", "The comment to display on this node")
  89. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &CommentNodeTextSaveData::OnCommentChanged)
  90. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, &CommentNodeTextSaveData::GetCommentLabel)
  91. ->DataElement(AZ::Edit::UIHandlers::Default, &CommentNodeTextSaveData::m_backgroundColor, "Background Color", "The background color to display the node comment on")
  92. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &CommentNodeTextSaveData::OnBackgroundColorChanged)
  93. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, &CommentNodeTextSaveData::GetBackgroundLabel)
  94. ->DataElement(AZ::Edit::UIHandlers::Default, &CommentNodeTextSaveData::m_fontConfiguration, "Font Settings", "The font settings used to render the font in the comment.")
  95. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &CommentNodeTextSaveData::UpdateStyleOverrides)
  96. ;
  97. editContext->Class<CommentNodeTextComponent>("Comment", "The node's customizable properties")
  98. ->ClassElement(AZ::Edit::ClassElements::EditorData, "Properties")
  99. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  100. ->DataElement(AZ::Edit::UIHandlers::Default, &CommentNodeTextComponent::m_saveData, "SaveData", "The modifiable information about this comment.")
  101. ;
  102. editContext->Class<FontConfiguration>("Font Settings", "Various settings used to control a font.")
  103. ->DataElement(AZ::Edit::UIHandlers::Default, &FontConfiguration::m_fontColor, "Font Color", "The color that the font of this comment should render with")
  104. ->DataElement(AZ::Edit::UIHandlers::Default, &FontConfiguration::m_fontFamily, "Font Family", "The font family to use when rendering this comment.")
  105. ->DataElement(AZ::Edit::UIHandlers::Default, &FontConfiguration::m_pixelSize, "Pixel Size", "The size of the font(in pixels)")
  106. ->Attribute(AZ::Edit::Attributes::Min, 1)
  107. ->Attribute(AZ::Edit::Attributes::Max, 200)
  108. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &FontConfiguration::m_weight, "Weight", "The weight of the font")
  109. ->EnumAttribute(QFont::Thin, "Thin")
  110. ->EnumAttribute(QFont::ExtraLight, "Extra Light")
  111. ->EnumAttribute(QFont::Light, "Light")
  112. ->EnumAttribute(QFont::Normal, "Normal")
  113. ->EnumAttribute(QFont::Medium, "Medium")
  114. ->EnumAttribute(QFont::DemiBold, "Demi-Bold")
  115. ->EnumAttribute(QFont::Bold, "Bold")
  116. ->EnumAttribute(QFont::ExtraBold, "Extra Bold")
  117. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &FontConfiguration::m_style, "Style", "The style of the font")
  118. ->EnumAttribute(QFont::Style::StyleNormal, "Normal")
  119. ->EnumAttribute(QFont::Style::StyleItalic, "Italic")
  120. ->EnumAttribute(QFont::Style::StyleOblique, "Oblique")
  121. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &FontConfiguration::m_verticalAlignment, "Vertical Alignment", "The Vertical Alignment of the font")
  122. ->EnumAttribute(Qt::AlignmentFlag::AlignTop, "Top")
  123. ->EnumAttribute(Qt::AlignmentFlag::AlignVCenter, "Middle")
  124. ->EnumAttribute(Qt::AlignmentFlag::AlignBottom, "Bottom")
  125. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &FontConfiguration::m_horizontalAlignment, "Horizontal Alignment", "The Horizontal Alignment of the font")
  126. ->EnumAttribute(Qt::AlignmentFlag::AlignLeft, "Left")
  127. ->EnumAttribute(Qt::AlignmentFlag::AlignHCenter, "Center")
  128. ->EnumAttribute(Qt::AlignmentFlag::AlignRight, "Right")
  129. ;
  130. }
  131. }
  132. }
  133. CommentNodeTextComponent::CommentNodeTextComponent()
  134. : m_saveData(this)
  135. , m_commentTextWidget(nullptr)
  136. , m_commentMode(CommentMode::Comment)
  137. {
  138. }
  139. CommentNodeTextComponent::CommentNodeTextComponent(AZStd::string_view initialText)
  140. : CommentNodeTextComponent()
  141. {
  142. m_saveData.m_comment = initialText;
  143. }
  144. void CommentNodeTextComponent::Init()
  145. {
  146. GraphCanvasPropertyComponent::Init();
  147. m_saveData.m_fontConfiguration.InitializePixelSize();
  148. EntitySaveDataRequestBus::Handler::BusConnect(GetEntityId());
  149. }
  150. void CommentNodeTextComponent::Activate()
  151. {
  152. if (m_commentTextWidget == nullptr)
  153. {
  154. m_commentTextWidget = aznew CommentTextGraphicsWidget(GetEntityId());
  155. m_commentTextWidget->SetStyle(Styling::Elements::CommentText);
  156. }
  157. GraphCanvasPropertyComponent::Activate();
  158. CommentRequestBus::Handler::BusConnect(GetEntityId());
  159. CommentLayoutRequestBus::Handler::BusConnect(GetEntityId());
  160. NodeNotificationBus::Handler::BusConnect(GetEntityId());
  161. m_commentTextWidget->Activate();
  162. }
  163. void CommentNodeTextComponent::Deactivate()
  164. {
  165. GraphCanvasPropertyComponent::Deactivate();
  166. NodeNotificationBus::Handler::BusDisconnect();
  167. CommentLayoutRequestBus::Handler::BusDisconnect();
  168. CommentRequestBus::Handler::BusDisconnect();
  169. m_commentTextWidget->Deactivate();
  170. }
  171. void CommentNodeTextComponent::OnAddedToScene(const AZ::EntityId& sceneId)
  172. {
  173. NodeUIRequestBus::Event(GetEntityId(), &NodeUIRequests::SetSnapToGrid, true);
  174. if (m_commentTextWidget->GetCommentMode() == CommentMode::Comment)
  175. {
  176. NodeUIRequestBus::Event(GetEntityId(), &NodeUIRequests::SetResizeToGrid, false);
  177. }
  178. else if (m_commentTextWidget->GetCommentMode() == CommentMode::BlockComment)
  179. {
  180. NodeUIRequestBus::Event(GetEntityId(), &NodeUIRequests::SetResizeToGrid, true);
  181. }
  182. AZ::EntityId grid;
  183. SceneRequestBus::EventResult(grid, sceneId, &SceneRequests::GetGrid);
  184. NodeUIRequestBus::Event(GetEntityId(), &NodeUIRequests::SetGrid, grid);
  185. m_commentTextWidget->OnAddedToScene();
  186. UpdateStyleOverrides();
  187. OnCommentChanged();
  188. OnBackgroundColorChanged();
  189. m_saveData.RegisterIds(GetEntityId(), sceneId);
  190. }
  191. void CommentNodeTextComponent::SetComment(const AZStd::string& comment)
  192. {
  193. if (m_saveData.m_comment.compare(comment) != 0)
  194. {
  195. m_saveData.m_comment = comment;
  196. m_commentTextWidget->SetComment(comment);
  197. AZ::EntityId sceneId;
  198. SceneMemberRequestBus::EventResult(sceneId, GetEntityId(), &SceneMemberRequests::GetScene);
  199. GraphModelRequestBus::Event(sceneId, &GraphModelRequests::RequestUndoPoint);
  200. }
  201. }
  202. void CommentNodeTextComponent::SetCommentMode(CommentMode commentMode)
  203. {
  204. NodeUIRequestBus::Event(GetEntityId(), &NodeUIRequests::SetSnapToGrid, true);
  205. m_commentTextWidget->SetCommentMode(commentMode);
  206. m_commentMode = commentMode;
  207. if (m_commentTextWidget->GetCommentMode() == CommentMode::Comment)
  208. {
  209. NodeUIRequestBus::Event(GetEntityId(), &NodeUIRequests::SetResizeToGrid, false);
  210. }
  211. else if (m_commentTextWidget->GetCommentMode() == CommentMode::BlockComment)
  212. {
  213. NodeUIRequestBus::Event(GetEntityId(), &NodeUIRequests::SetResizeToGrid, true);
  214. }
  215. }
  216. void CommentNodeTextComponent::SetBackgroundColor(const AZ::Color& backgroundColor)
  217. {
  218. m_saveData.m_backgroundColor = backgroundColor;
  219. m_saveData.SignalDirty();
  220. OnBackgroundColorChanged();
  221. }
  222. AZ::Color CommentNodeTextComponent::GetBackgroundColor() const
  223. {
  224. return m_saveData.m_backgroundColor;
  225. }
  226. CommentMode CommentNodeTextComponent::GetCommentMode() const
  227. {
  228. return m_commentMode;
  229. }
  230. const AZStd::string& CommentNodeTextComponent::GetComment() const
  231. {
  232. return m_saveData.m_comment;
  233. }
  234. QGraphicsLayoutItem* CommentNodeTextComponent::GetGraphicsLayoutItem()
  235. {
  236. return m_commentTextWidget;
  237. }
  238. void CommentNodeTextComponent::WriteSaveData(EntitySaveDataContainer& saveDataContainer) const
  239. {
  240. CommentNodeTextSaveData* saveData = saveDataContainer.FindCreateSaveData<CommentNodeTextSaveData>();
  241. if (saveData)
  242. {
  243. (*saveData) = m_saveData;
  244. }
  245. }
  246. void CommentNodeTextComponent::ReadSaveData(const EntitySaveDataContainer& saveDataContainer)
  247. {
  248. CommentNodeTextSaveData* saveData = saveDataContainer.FindSaveDataAs<CommentNodeTextSaveData>();
  249. if (saveData)
  250. {
  251. m_saveData = (*saveData);
  252. }
  253. }
  254. void CommentNodeTextComponent::ApplyPresetData(const EntitySaveDataContainer& saveDataContainer)
  255. {
  256. CommentNodeTextSaveData* saveData = saveDataContainer.FindSaveDataAs<CommentNodeTextSaveData>();
  257. if (saveData)
  258. {
  259. // Copy over everything but the save comment.
  260. AZStd::string previousComment = m_saveData.m_comment;
  261. m_saveData = (*saveData);
  262. m_saveData.m_comment = previousComment;
  263. UpdateStyleOverrides();
  264. OnBackgroundColorChanged();
  265. }
  266. }
  267. void CommentNodeTextComponent::OnCommentChanged()
  268. {
  269. if (m_commentTextWidget)
  270. {
  271. m_commentTextWidget->SetComment(m_saveData.m_comment);
  272. CommentNotificationBus::Event(GetEntityId(), &CommentNotifications::OnCommentChanged, m_saveData.m_comment);
  273. }
  274. }
  275. void CommentNodeTextComponent::OnBackgroundColorChanged()
  276. {
  277. CommentNotificationBus::Event(GetEntityId(), &CommentNotifications::OnBackgroundColorChanged, m_saveData.m_backgroundColor);
  278. }
  279. void CommentNodeTextComponent::UpdateStyleOverrides()
  280. {
  281. CommentNotificationBus::Event(GetEntityId(), &CommentNotifications::OnCommentFontReloadBegin);
  282. QColor fontColor = ConversionUtils::AZToQColor(m_saveData.m_fontConfiguration.m_fontColor);
  283. Styling::StyleHelper& styleHelper = m_commentTextWidget->GetStyleHelper();
  284. styleHelper.AddAttributeOverride(Styling::Attribute::Color, fontColor);
  285. styleHelper.AddAttributeOverride(Styling::Attribute::FontFamily, QString(m_saveData.m_fontConfiguration.m_fontFamily.c_str()));
  286. styleHelper.AddAttributeOverride(Styling::Attribute::FontSize, m_saveData.m_fontConfiguration.m_pixelSize);
  287. styleHelper.AddAttributeOverride(Styling::Attribute::FontWeight, m_saveData.m_fontConfiguration.m_weight);
  288. styleHelper.AddAttributeOverride(Styling::Attribute::FontStyle, m_saveData.m_fontConfiguration.m_style);
  289. styleHelper.AddAttributeOverride(Styling::Attribute::TextAlignment, m_saveData.m_fontConfiguration.m_horizontalAlignment);
  290. styleHelper.AddAttributeOverride(Styling::Attribute::TextVerticalAlignment, m_saveData.m_fontConfiguration.m_verticalAlignment);
  291. m_commentTextWidget->OnStyleChanged();
  292. CommentNotificationBus::Event(GetEntityId(), &CommentNotifications::OnCommentFontReloadEnd);
  293. }
  294. }