ReflectedVar.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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_REFLECTEDVAR_H
  9. #define CRYINCLUDE_EDITOR_UTILS_REFLECTEDVAR_H
  10. #pragma once
  11. #include <algorithm>
  12. #include <limits>
  13. #include "Util/VariablePropertyType.h"
  14. #include <AzCore/Asset/AssetCommon.h>
  15. #include <AzCore/Math/Vector2.h>
  16. #include <AzCore/Math/Vector3.h>
  17. #include <AzCore/Math/Vector4.h>
  18. //Base class for generic reflected variables
  19. class CReflectedVar
  20. {
  21. public:
  22. AZ_RTTI(CReflectedVar, "{9CF461B5-4093-4F7E-9A28-75531F0D046C}")
  23. CReflectedVar() = default;
  24. CReflectedVar(const AZStd::string& name)
  25. : m_varName(name){}
  26. virtual ~CReflectedVar(){}
  27. AZStd::string m_varName;
  28. AZStd::string m_description;
  29. };
  30. // Reflected container of reflected values. Also holds ePropertyTable data
  31. class CPropertyContainer
  32. : public CReflectedVar
  33. {
  34. public:
  35. AZ_RTTI(CPropertyContainer, "{99500790-241A-4274-BAD8-C4510E869FC6}", CReflectedVar)
  36. CPropertyContainer(const AZStd::string& name)
  37. : CReflectedVar(name) {}
  38. CPropertyContainer() = default;
  39. AZStd::string varName() const { return m_varName; }
  40. AZStd::string description() const { return m_description; }
  41. void AddProperty(CReflectedVar* property);
  42. void Clear();
  43. //If we're an unnamed container, just show our children in flat list. Otherwise show the container name with children underneath
  44. AZ::u32 GetVisibility() const
  45. {
  46. return m_varName.empty() ? AZ_CRC("PropertyVisibility_ShowChildrenOnly", 0xef428f20) : AZ_CRC("PropertyVisibility_Show", 0xa43c82dd);
  47. }
  48. void SetAutoExpand(bool autoExpand) { m_autoExpand = autoExpand; }
  49. bool AutoExpand() const { return m_autoExpand; }
  50. AZStd::vector<CReflectedVar*> GetProperties() const { return m_properties; }
  51. void SetValueText(const AZStd::string& valueText) { m_valueText = valueText; }
  52. friend class ReflectedVarInit;
  53. private:
  54. AZStd::vector<CReflectedVar*> m_properties;
  55. bool m_autoExpand = false;
  56. AZStd::string m_valueText;
  57. };
  58. template<class T>
  59. class CReflectedVarAny
  60. : public CReflectedVar
  61. {
  62. public:
  63. AZ_RTTI((CReflectedVarAny, "{EE8293C3-9B1E-470B-9922-2CBB8DA13D78}", T), CReflectedVar);
  64. CReflectedVarAny(const AZStd::string& name, const T& val = T())
  65. : CReflectedVar(name)
  66. , m_value(val) {}
  67. CReflectedVarAny() = default;
  68. AZStd::string varName() const { return m_varName; }
  69. AZStd::string description() const { return m_description; }
  70. static void reflect(AZ::SerializeContext* serializeContext);
  71. T m_value;
  72. };
  73. // Class to hold values that have min/max
  74. // T = data type held in this variable
  75. // R = data type of the range
  76. template<class T, class R>
  77. class CReflectedVarRanged
  78. : public CReflectedVar
  79. {
  80. public:
  81. AZ_RTTI((CReflectedVarRanged, "{6AB4EC29-E17B-4B3B-A153-BFDAA48B8CF8}", T, R), CReflectedVar);
  82. CReflectedVarRanged(const AZStd::string& name, const T& val = T())
  83. : CReflectedVar(name)
  84. , m_value(val)
  85. , m_minVal(std::numeric_limits<R>::lowest())
  86. , m_maxVal(std::numeric_limits<R>::max())
  87. , m_stepSize(1)
  88. , m_softMinVal(std::numeric_limits<R>::lowest())
  89. , m_softMaxVal(std::numeric_limits<R>::max())
  90. {}
  91. CReflectedVarRanged()
  92. : CReflectedVarRanged(AZStd::string(), T()){}
  93. AZStd::string varName() const { return m_varName; }
  94. AZStd::string description() const { return m_description; }
  95. R minValue() const { return m_minVal; }
  96. R maxValue() const { return m_maxVal; }
  97. R stepSize() const { return m_stepSize; }
  98. R softMinVal() const { return m_softMinVal; }
  99. R softMaxVal() const { return m_softMaxVal; }
  100. static void reflect(AZ::SerializeContext* serializeContext);
  101. T m_value;
  102. R m_minVal;
  103. R m_maxVal;
  104. R m_stepSize;
  105. R m_softMinVal;
  106. R m_softMaxVal;
  107. };
  108. //name some commonly-used variable types
  109. template <class T>
  110. using CReflectedVarNumeric = CReflectedVarRanged<T, T>;
  111. //ePropertyFloat
  112. using CReflectedVarFloat = CReflectedVarNumeric<float>;
  113. //ePropertyInt
  114. using CReflectedVarInt = CReflectedVarNumeric<int>;
  115. //ePropertyString
  116. using CReflectedVarString = CReflectedVarAny<AZStd::string>;
  117. //ePropertyBool
  118. using CReflectedVarBool = CReflectedVarAny<bool>;
  119. //ePropertyVector2
  120. using CReflectedVarVector2 = CReflectedVarRanged<AZ::Vector2, float>;
  121. //ePropertyVector
  122. using CReflectedVarVector3 = CReflectedVarRanged<AZ::Vector3, float>;
  123. //ePropertyVector4
  124. using CReflectedVarVector4 = CReflectedVarRanged<AZ::Vector4, float>;
  125. // Class for holding enumerated values, ePropertySelection
  126. // Keeps a key-value pair values (int, string, float, etc) and names corresponding to each value
  127. // The names are displayed to user when editing, the values are used by underlying code.
  128. template<class T>
  129. class CReflectedVarEnum
  130. : public CReflectedVar
  131. {
  132. public:
  133. AZ_RTTI((CReflectedVarEnum, "{40AE7D74-7E3A-41A9-8F71-2BBC3067118B}", T), CReflectedVar);
  134. CReflectedVarEnum(const AZStd::string& name)
  135. : CReflectedVar(name) {}
  136. CReflectedVarEnum() = default;
  137. void setEnums(const AZStd::vector<AZStd::pair<T, AZStd::string> >& enums)
  138. {
  139. m_enums = enums;
  140. if (m_enums.size() > 0)
  141. {
  142. m_value = m_enums.at(0).first;
  143. m_selectedEnumName = m_enums.at(0).second;
  144. }
  145. else
  146. {
  147. m_value = T();
  148. m_selectedEnumName.clear();
  149. }
  150. }
  151. void addEnum(const T& value, const AZStd::string& name)
  152. {
  153. m_enums.push_back(AZStd::pair<T, AZStd::string>(value, name));
  154. if (m_enums.size() == 1)
  155. {
  156. m_selectedEnumName = name;
  157. m_value = value;
  158. }
  159. }
  160. void setEnumValue(const T& value)
  161. {
  162. auto it = std::find_if(m_enums.cbegin(), m_enums.cend(), [value](const AZStd::pair<T, AZStd::string>& item) -> bool { return item.first == value; });
  163. if (it != m_enums.end())
  164. {
  165. m_value = it->first;
  166. m_selectedEnumName = it->second;
  167. }
  168. }
  169. void setEnumByName(const AZStd::string& name)
  170. {
  171. auto it = std::find_if(m_enums.cbegin(), m_enums.cend(), [name](const AZStd::pair<T, AZStd::string>& item) -> bool { return item.second == name; });
  172. if (it != m_enums.end())
  173. {
  174. m_value = it->first;
  175. m_selectedEnumName = it->second;
  176. }
  177. }
  178. void OnEnumChanged()
  179. {
  180. setEnumByName(m_selectedEnumName);
  181. }
  182. AZStd::vector < AZStd::string> GetEnums() const
  183. {
  184. AZStd::vector < AZStd::string> returnVal;
  185. for (const auto& i : m_enums)
  186. {
  187. returnVal.push_back(i.second);
  188. }
  189. return returnVal;
  190. }
  191. AZStd::string varName() const { return m_varName; }
  192. AZStd::string description() const { return m_description; }
  193. static void reflect(AZ::SerializeContext* serializeContext);
  194. T m_value;
  195. AZStd::string m_selectedEnumName;
  196. AZStd::vector<AZStd::pair<T, AZStd::string> > m_enums;
  197. };
  198. //Class to hold ePropertyColor (IVariable::DT_COLOR)
  199. class CReflectedVarColor
  200. : public CReflectedVar
  201. {
  202. public:
  203. AZ_RTTI(CReflectedVarColor, "{CC69E773-B4FA-4B6D-8A46-0B580097B6D2}", CReflectedVar)
  204. CReflectedVarColor(const AZStd::string& name, AZ::Vector3 color = AZ::Vector3())
  205. : CReflectedVar(name)
  206. , m_color(color) {}
  207. CReflectedVarColor() {}
  208. AZStd::string varName() const { return m_varName; }
  209. AZStd::string description() const { return m_description; }
  210. AZ::Vector3 m_color;
  211. };
  212. //Class to hold:
  213. // ePropertyTexture (IVariable::DT_TEXTURE)
  214. // ePropertyAudioTrigger (IVariable::DT_AUDIO_TRIGGER)
  215. // ePropertyAudioSwitch (IVariable::DT_AUDIO_SWITCH )
  216. // ePropertyAudioSwitchState (IVariable::DT_AUDIO_SWITCH_STATE)
  217. // ePropertyAudioRTPC (IVariable::DT_AUDIO_RTPC)
  218. // ePropertyAudioEnvironment (IVariable::DT_AUDIO_ENVIRONMENT)
  219. // ePropertyAudioPreloadRequest (IVariable::DT_AUDIO_PRELOAD_REQUEST)
  220. class CReflectedVarResource
  221. : public CReflectedVar
  222. {
  223. public:
  224. AZ_RTTI(CReflectedVarResource, "{162864C2-0C3E-4B6A-84D3-BBAD975B4FD2}", CReflectedVar)
  225. CReflectedVarResource(const AZStd::string& name)
  226. : CReflectedVar(name)
  227. , m_propertyType(ePropertyInvalid)
  228. {}
  229. CReflectedVarResource()
  230. : m_propertyType(ePropertyInvalid){}
  231. AZStd::string varName() const { return m_varName; }
  232. AZStd::string description() const { return m_description; }
  233. AZStd::string m_path;
  234. PropertyType m_propertyType;
  235. };
  236. //Class to hold ePropertyUser (IVariable::DT_USERITEMCB)
  237. class CReflectedVarUser
  238. : public CReflectedVar
  239. {
  240. public:
  241. AZ_RTTI(CReflectedVarUser, "{A901DA91-3893-4848-9AE8-62C0ED074970}", CReflectedVar)
  242. CReflectedVarUser(const AZStd::string &name)
  243. : CReflectedVar(name)
  244. , m_enableEdit(false)
  245. , m_useTree(false)
  246. {}
  247. CReflectedVarUser() : m_enableEdit(false), m_useTree(false) {}
  248. AZStd::string varName() const { return m_varName; }
  249. AZStd::string m_value;
  250. bool m_enableEdit;
  251. bool m_useTree;
  252. AZStd::string m_dialogTitle;
  253. AZStd::string m_treeSeparator;
  254. AZStd::vector<AZStd::string> m_itemNames;
  255. AZStd::vector<AZStd::string> m_itemDescriptions;
  256. };
  257. class CReflectedVarSpline
  258. : public CReflectedVar
  259. {
  260. public:
  261. AZ_RTTI(CReflectedVarSpline, "{9A928683-7C84-48BF-8A2E-F7BEC423EE4E}", CReflectedVar)
  262. CReflectedVarSpline(PropertyType propertyType, const AZStd::string &name)
  263. : CReflectedVar(name)
  264. , m_spline(0)
  265. , m_propertyType(propertyType)
  266. {}
  267. CReflectedVarSpline()
  268. : m_spline(0)
  269. , m_propertyType(ePropertyInvalid)
  270. {}
  271. AZStd::string varName() const { return m_varName; }
  272. AZ::u32 handler();
  273. uint64_t m_spline;
  274. PropertyType m_propertyType;
  275. };
  276. //Class to wrap all the many properties that can be represented by a string and edited via a popup
  277. class CReflectedVarGenericProperty
  278. : public CReflectedVar
  279. {
  280. public:
  281. AZ_RTTI(CReflectedVarGenericProperty, "{C4A34C95-3D71-40CE-86D2-DDE314B33CC5}", CReflectedVar)
  282. CReflectedVarGenericProperty(PropertyType pType, const AZStd::string& name = AZStd::string(), const AZStd::string& val = AZStd::string())
  283. : CReflectedVar(name)
  284. , m_propertyType(pType)
  285. , m_value(val) {}
  286. CReflectedVarGenericProperty()
  287. : CReflectedVar()
  288. , m_propertyType(ePropertyInvalid){}
  289. AZStd::string varName() const { return m_varName; }
  290. AZStd::string description() const { return m_description; }
  291. PropertyType propertyType() const { return m_propertyType; }
  292. AZ::u32 handler();
  293. static void reflect(AZ::SerializeContext* serializeContext);
  294. PropertyType m_propertyType;
  295. AZStd::string m_value;
  296. };
  297. class EDITOR_CORE_API ReflectedVarInit
  298. {
  299. public:
  300. static void setupReflection(AZ::SerializeContext* serializeContext);
  301. private:
  302. static bool s_reflectionDone;
  303. };
  304. //Class to hold ePropertyMotion (IVariable::DT_MOTION )
  305. class CReflectedVarMotion
  306. : public CReflectedVar
  307. {
  308. public:
  309. AZ_RTTI(CReflectedVarMotion, "{66397EFB-620A-40B8-8C66-D6AECF690DF5}", CReflectedVar)
  310. CReflectedVarMotion(const AZStd::string& name)
  311. : CReflectedVar(name) {}
  312. CReflectedVarMotion() = default;
  313. AZStd::string varName() const { return m_varName; }
  314. AZStd::string description() const { return m_description; }
  315. AZStd::string m_motion;
  316. AZ::Data::AssetId m_assetId;
  317. };
  318. #endif // CRYINCLUDE_EDITOR_UTILS_REFLECTEDVAR_H