ReflectedPropertyItem.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef CRYINCLUDE_EDITOR_UTILS_REFLECTEDPROPERTYITEM_H
  9. #define CRYINCLUDE_EDITOR_UTILS_REFLECTEDPROPERTYITEM_H
  10. #pragma once
  11. #include "Util/Variable.h"
  12. #include <Util/VariablePropertyType.h>
  13. namespace AzToolsFramework {
  14. class ReflectedPropertyEditor;
  15. }
  16. class CReflectedVar;
  17. class CPropertyContainer;
  18. class ReflectedPropertyControl;
  19. class ReflectedVarAdapter;
  20. class ReflectedVarContainerAdapter;
  21. // Class representing a property inside a ReflectedPropertyCtrl.
  22. // It contains the IVariable and corresponding CReflectedVar for that property
  23. // and any children properties if the IVariable is a container.
  24. // This class is loosely based on the MFC-based CPropertyItem to make porting easier.
  25. // The CPropertyItem created editor widgets for each property type, but this class
  26. // just holds a CReflectedVar and updates it's values. The editing is done by the
  27. // reflection system and registered property handlers for each CReflectedVar.
  28. class EDITOR_CORE_API ReflectedPropertyItem
  29. : public CRefCountBase
  30. {
  31. public:
  32. ReflectedPropertyItem(ReflectedPropertyControl* control, ReflectedPropertyItem* parent);
  33. ~ReflectedPropertyItem();
  34. void SetVariable(IVariable* var);
  35. IVariable* GetVariable() const { return m_pVariable; }
  36. void ReplaceVarBlock(CVarBlock* varBlock);
  37. CReflectedVar* GetReflectedVar() const;
  38. ReflectedPropertyItem* findItem(CReflectedVar* var);
  39. ReflectedPropertyItem* findItem(IVariable* var);
  40. ReflectedPropertyItem* findItem(const QString &name);
  41. ReflectedPropertyItem* FindItemByFullName(const QString& fullName);
  42. //update the internal IVariable as result of ReflectedVar changing
  43. void OnReflectedVarChanged();
  44. //update the ReflectedVar to current value of IVar
  45. void SyncReflectedVarToIVar();
  46. //! Return true if this property item is modified.
  47. bool IsModified() const { return m_modified; }
  48. void ReloadValues();
  49. ReflectedVarContainerAdapter* GetContainer() { return m_reflectedVarContainerAdapter; }
  50. ReflectedPropertyItem* GetParent() { return m_parent; }
  51. /** Get script default value of property item.
  52. */
  53. virtual bool HasScriptDefault() const { return m_strScriptDefault != m_strNoScriptDefault; };
  54. /** Get script default value of property item.
  55. */
  56. virtual QString GetScriptDefault() const { return m_strScriptDefault; };
  57. /** Set script default value of property item.
  58. */
  59. virtual void SetScriptDefault(const QString& sScriptDefault) { m_strScriptDefault = sScriptDefault; };
  60. /** Set script default value of property item.
  61. */
  62. virtual void ClearScriptDefault() { m_strScriptDefault = m_strNoScriptDefault; };
  63. /** Changes value of item.
  64. */
  65. virtual void SetValue(const QString& sValue, bool bRecordUndo = true, bool bForceModified = false);
  66. //hack for calling ReflectedPropertyControl::OnItemChange from a wrapper class
  67. //this is used because changes to Splines should not actually change anything in the IVariable,
  68. //but we need OnItemChanged as if the IVariable did change.
  69. void SendOnItemChange();
  70. void ExpandAllChildren(bool recursive);
  71. void Expand(bool expand);
  72. QString GetPropertyName() const;
  73. void AddChild(ReflectedPropertyItem* item);
  74. void RemoveAllChildren();
  75. void RemoveChild(ReflectedPropertyItem* item);
  76. // default number of increments to cover the range of a property
  77. static const float s_DefaultNumStepIncrements;
  78. // for a consistent Feel, compute the step size for a numerical slider for the specified min/max, rounded to precision
  79. inline static float ComputeSliderStep(float sliderMin, float sliderMax, const float precision = .01f)
  80. {
  81. float step;
  82. step = int_round(((sliderMax - sliderMin) / ReflectedPropertyItem::s_DefaultNumStepIncrements) / precision) * precision;
  83. // prevent rounding down to zero
  84. return (step > precision) ? step : precision;
  85. }
  86. protected:
  87. friend class ReflectedPropertyControl;
  88. //! Release used variable.
  89. void ReleaseVariable();
  90. //! Callback called when variable change.
  91. void OnVariableChange(IVariable* var);
  92. void OnVariableEnumChange(IVariable* var);
  93. public:
  94. //! Get number of child nodes.
  95. int GetChildCount() const { return static_cast<int>(m_childs.size()); };
  96. //! Get Child by id.
  97. ReflectedPropertyItem* GetChild(int index) const { return m_childs[index]; }
  98. PropertyType GetType() const { return m_type; }
  99. /** Get name of property item.
  100. */
  101. virtual QString GetName() const;
  102. QString GetFullName() const;
  103. protected:
  104. PropertyType m_type;
  105. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  106. //The variable being edited.
  107. _smart_ptr<IVariable> m_pVariable;
  108. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  109. //holds the CReflectedVar and syncs its value with IVariable when either changes
  110. ReflectedVarAdapter* m_reflectedVarAdapter;
  111. ReflectedVarContainerAdapter* m_reflectedVarContainerAdapter;
  112. ReflectedPropertyItem* m_parent;
  113. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  114. std::vector<_smart_ptr<ReflectedPropertyItem> > m_childs;
  115. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  116. ReflectedPropertyControl* m_propertyCtrl;
  117. unsigned int m_modified : 1;
  118. bool m_syncingIVar;
  119. QString m_strNoScriptDefault;
  120. QString m_strScriptDefault;
  121. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  122. IVariable::OnSetCallback m_onSetCallback;
  123. IVariable::OnSetEnumCallback m_onSetEnumCallback;
  124. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  125. };
  126. typedef _smart_ptr<ReflectedPropertyItem> ReflectedPropertyItemPtr;
  127. #endif // CRYINCLUDE_EDITOR_UTILS_REFLECTEDPROPERTYITEM_H