DisplaySettingsPythonFuncs.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "EditorDefs.h"
  9. #include "DisplaySettingsPythonFuncs.h"
  10. // AzCore
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. // Editor
  14. #include "DisplaySettings.h"
  15. namespace
  16. {
  17. int PyGetMiscDisplaySettings()
  18. {
  19. return GetIEditor()->GetDisplaySettings()->GetSettings();
  20. }
  21. void PySetMiscDisplaySettings(int flags)
  22. {
  23. GetIEditor()->GetDisplaySettings()->SetSettings(flags);
  24. }
  25. }
  26. namespace AzToolsFramework
  27. {
  28. bool DisplaySettingsState::operator==(const DisplaySettingsState& rhs) const
  29. {
  30. return this->m_noCollision == rhs.m_noCollision &&
  31. this->m_noLabels == rhs.m_noLabels &&
  32. this->m_simulate == rhs.m_simulate &&
  33. this->m_hideTracks == rhs.m_hideTracks &&
  34. this->m_hideLinks == rhs.m_hideLinks &&
  35. this->m_hideHelpers == rhs.m_hideHelpers &&
  36. this->m_showDimensionFigures == rhs.m_showDimensionFigures;
  37. }
  38. AZStd::string DisplaySettingsState::ToString() const
  39. {
  40. auto boolToPyString = [](bool v)
  41. {
  42. return v ? "True" : "False";
  43. };
  44. return AZStd::string::format("(no_collision=%s, no_labels=%s, simulate=%s, hide_tracks=%s, hide_links=%s, hide_helpers=%s, show_dimension_figures=%s)",
  45. boolToPyString(this->m_noCollision),
  46. boolToPyString(this->m_noLabels),
  47. boolToPyString(this->m_simulate),
  48. boolToPyString(this->m_hideTracks),
  49. boolToPyString(this->m_hideLinks),
  50. boolToPyString(this->m_hideHelpers),
  51. boolToPyString(this->m_showDimensionFigures)
  52. );
  53. }
  54. void DisplaySettingsPythonFuncsHandler::Reflect(AZ::ReflectContext* context)
  55. {
  56. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  57. {
  58. // this will put these methods into the 'azlmbr.legacy.settings' module
  59. auto addLegacyGeneral = [](AZ::BehaviorContext::GlobalMethodBuilder methodBuilder)
  60. {
  61. methodBuilder->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  62. ->Attribute(AZ::Script::Attributes::Category, "Legacy/Settings")
  63. ->Attribute(AZ::Script::Attributes::Module, "legacy.settings");
  64. };
  65. addLegacyGeneral(behaviorContext->Method("get_misc_editor_settings", PyGetMiscDisplaySettings, nullptr, "Get miscellaneous Editor settings (camera collides with terrain, AI/Physics enabled, etc)."));
  66. addLegacyGeneral(behaviorContext->Method("set_misc_editor_settings", PySetMiscDisplaySettings, nullptr, "Set miscellaneous Editor settings (camera collides with terrain, AI/Physics enabled, etc)."));
  67. behaviorContext->EnumProperty<EDisplaySettingsFlags::SETTINGS_NOCOLLISION>("DisplaySettings_NoCollision")
  68. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation);
  69. behaviorContext->EnumProperty<EDisplaySettingsFlags::SETTINGS_NOLABELS>("DisplaySettings_NoLabels")
  70. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation);
  71. behaviorContext->EnumProperty<EDisplaySettingsFlags::SETTINGS_PHYSICS>("DisplaySettings_Physics")
  72. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation);
  73. behaviorContext->EnumProperty<EDisplaySettingsFlags::SETTINGS_HIDE_TRACKS>("DisplaySettings_HideTracks")
  74. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation);
  75. behaviorContext->EnumProperty<EDisplaySettingsFlags::SETTINGS_HIDE_LINKS>("DisplaySettings_HideLinks")
  76. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation);
  77. behaviorContext->EnumProperty<EDisplaySettingsFlags::SETTINGS_HIDE_HELPERS>("DisplaySettings_HideHelpers")
  78. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation);
  79. behaviorContext->EnumProperty<EDisplaySettingsFlags::SETTINGS_SHOW_DIMENSIONFIGURES>("DisplaySettings_ShowDimensionFigures")
  80. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation);
  81. behaviorContext->EnumProperty<EDisplaySettingsFlags::SETTINGS_SERIALIZABLE_FLAGS_MASK>("DisplaySettings_SerializableFlagsMask")
  82. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation);
  83. }
  84. }
  85. void DisplaySettingsComponent::Reflect(AZ::ReflectContext* context)
  86. {
  87. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  88. {
  89. serializeContext->Class<DisplaySettingsComponent, AZ::Component>();
  90. serializeContext->Class<DisplaySettingsState>()
  91. ->Version(1)
  92. ->Field("NoCollision", &DisplaySettingsState::m_noCollision)
  93. ->Field("NoLabels", &DisplaySettingsState::m_noLabels)
  94. ->Field("Simulate", &DisplaySettingsState::m_simulate)
  95. ->Field("HideTracks", &DisplaySettingsState::m_hideTracks)
  96. ->Field("HideLinks", &DisplaySettingsState::m_hideLinks)
  97. ->Field("HideHelpers", &DisplaySettingsState::m_hideHelpers)
  98. ->Field("ShowDimensionFigures", &DisplaySettingsState::m_showDimensionFigures)
  99. ;
  100. }
  101. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  102. {
  103. behaviorContext->Class<DisplaySettingsState>("DisplaySettingsState")
  104. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  105. ->Attribute(AZ::Script::Attributes::Category, "DisplaySettings")
  106. ->Attribute(AZ::Script::Attributes::Module, "display_settings")
  107. ->Property("NoCollision", BehaviorValueProperty(&DisplaySettingsState::m_noCollision))
  108. ->Attribute(AZ::Script::Attributes::Alias, "no_collision")
  109. ->Property("NoLabels", BehaviorValueProperty(&DisplaySettingsState::m_noLabels))
  110. ->Attribute(AZ::Script::Attributes::Alias, "no_labels")
  111. ->Property("Simulate", BehaviorValueProperty(&DisplaySettingsState::m_simulate))
  112. ->Attribute(AZ::Script::Attributes::Alias, "simulate")
  113. ->Property("HideTracks", BehaviorValueProperty(&DisplaySettingsState::m_hideTracks))
  114. ->Attribute(AZ::Script::Attributes::Alias, "hide_tracks")
  115. ->Property("HideLinks", BehaviorValueProperty(&DisplaySettingsState::m_hideLinks))
  116. ->Attribute(AZ::Script::Attributes::Alias, "hide_links")
  117. ->Property("HideHelpers", BehaviorValueProperty(&DisplaySettingsState::m_hideHelpers))
  118. ->Attribute(AZ::Script::Attributes::Alias, "hide_helpers")
  119. ->Property("ShowDimensionFigures", BehaviorValueProperty(&DisplaySettingsState::m_showDimensionFigures))
  120. ->Attribute(AZ::Script::Attributes::Alias, "show_dimension_figures")
  121. ->Method("ToString", &DisplaySettingsState::ToString)
  122. ;
  123. behaviorContext->EBus<DisplaySettingsBus>("DisplaySettingsBus")
  124. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  125. ->Attribute(AZ::Script::Attributes::Category, "DisplaySettings")
  126. ->Attribute(AZ::Script::Attributes::Module, "display_settings")
  127. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  128. ->Event("GetSettingsState", &DisplaySettingsRequests::GetSettingsState)
  129. ->Event("SetSettingsState", &DisplaySettingsRequests::SetSettingsState)
  130. ;
  131. }
  132. }
  133. void DisplaySettingsComponent::Activate()
  134. {
  135. DisplaySettingsBus::Handler::BusConnect();
  136. }
  137. void DisplaySettingsComponent::Deactivate()
  138. {
  139. DisplaySettingsBus::Handler::BusDisconnect();
  140. }
  141. DisplaySettingsState DisplaySettingsComponent::GetSettingsState() const
  142. {
  143. return ConvertToSettings(GetIEditor()->GetDisplaySettings()->GetSettings());
  144. }
  145. void DisplaySettingsComponent::SetSettingsState(const DisplaySettingsState& settingsState)
  146. {
  147. uint flags = ConvertToFlags(settingsState);
  148. GetIEditor()->GetDisplaySettings()->SetSettings(flags);
  149. }
  150. int DisplaySettingsComponent::ConvertToFlags(const DisplaySettingsState& state) const
  151. {
  152. int flags = 0;
  153. flags |= state.m_noCollision ? SETTINGS_NOCOLLISION : 0x0;
  154. flags |= state.m_noLabels ? SETTINGS_NOLABELS : 0x0;
  155. flags |= state.m_simulate ? SETTINGS_PHYSICS : 0x0;
  156. flags |= state.m_hideTracks ? SETTINGS_HIDE_TRACKS : 0x0;
  157. flags |= state.m_hideLinks ? SETTINGS_HIDE_LINKS : 0x0;
  158. flags |= state.m_hideHelpers ? SETTINGS_HIDE_HELPERS : 0x0;
  159. flags |= state.m_showDimensionFigures ? SETTINGS_SHOW_DIMENSIONFIGURES : 0x0;
  160. return flags;
  161. }
  162. DisplaySettingsState DisplaySettingsComponent::ConvertToSettings(int settings) const
  163. {
  164. DisplaySettingsState state;
  165. state.m_noCollision = settings & SETTINGS_NOCOLLISION;
  166. state.m_noLabels = settings & SETTINGS_NOLABELS;
  167. state.m_simulate = settings & SETTINGS_PHYSICS;
  168. state.m_hideTracks = settings & SETTINGS_HIDE_TRACKS;
  169. state.m_hideLinks = settings & SETTINGS_HIDE_LINKS;
  170. state.m_hideHelpers = settings & SETTINGS_HIDE_HELPERS;
  171. state.m_showDimensionFigures = settings & SETTINGS_SHOW_DIMENSIONFIGURES;
  172. return state;
  173. }
  174. }