Variable.h 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  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_UTIL_VARIABLE_H
  9. #define CRYINCLUDE_EDITOR_UTIL_VARIABLE_H
  10. #pragma once
  11. #include "RefCountBase.h"
  12. #include "Include/EditorCoreAPI.h"
  13. #include <QObject>
  14. AZ_PUSH_DISABLE_WARNING(4458, "-Wunknown-warning-option")
  15. #include <QtUtilWin.h>
  16. AZ_POP_DISABLE_WARNING
  17. #include <QVariant>
  18. #include <StlUtils.h>
  19. #include <AzCore/std/string/string.h>
  20. #include <AzCore/Math/Color.h>
  21. #include <AzCore/Math/Vector2.h>
  22. #include <AzCore/Math/Vector3.h>
  23. #include <AzCore/Math/Vector4.h>
  24. #include <AzCore/Math/Quaternion.h>
  25. inline const char* to_c_str(const char* str) { return str; }
  26. #define MAX_VAR_STRING_LENGTH 4096
  27. struct IVarEnumList;
  28. struct ISplineInterpolator;
  29. class CUsedResources;
  30. struct IVariable;
  31. namespace
  32. {
  33. template<typename T>
  34. T nextNiceNumberBelow(T number);
  35. template<typename T>
  36. T nextNiceNumberAbove(T number)
  37. {
  38. if (number == 0)
  39. {
  40. return 0;
  41. }
  42. if (number < 0)
  43. {
  44. return -nextNiceNumberBelow(-number);
  45. }
  46. // number = 8000
  47. auto l = log10(number); // 3.90
  48. AZ::s64 i = aznumeric_cast<AZ::s64>(l); // 3
  49. int f = aznumeric_cast<int>(pow(10, l - i)); // 10^0.9 = 8
  50. f = f < 2 ? 2 : f < 5 ? 5 : 10; // f -> 10
  51. return aznumeric_cast<T>(pow(10, i) * f);
  52. }
  53. template<typename T>
  54. T nextNiceNumberBelow(T number)
  55. {
  56. if (number == 0)
  57. {
  58. return 0;
  59. }
  60. if (number < 0)
  61. {
  62. return -nextNiceNumberAbove(-number);
  63. }
  64. // number = 8000
  65. auto l = log10(number); // 3.90
  66. AZ::s64 i = aznumeric_cast<AZ::s64>(l); // 3
  67. int f = aznumeric_cast<int>(pow(10, l - i)); // 10^0.9 = 8
  68. f = f > 5 ? 5 : f > 2 ? 2 : 1; // f -> 5
  69. return aznumeric_cast<T>(pow(10, i) * f);
  70. }
  71. }
  72. /** IVariableContainer
  73. * Interface for all classes that hold child variables
  74. */
  75. struct IVariableContainer
  76. : public CRefCountBase
  77. {
  78. //! Add child variable
  79. virtual void AddVariable(IVariable* var) = 0;
  80. //! Delete specific variable
  81. virtual bool DeleteVariable(IVariable* var, bool recursive = false) = 0;
  82. //! Delete all variables
  83. virtual void DeleteAllVariables() = 0;
  84. //! Gets number of variables
  85. virtual int GetNumVariables() const = 0;
  86. //! Get variable at index
  87. virtual IVariable* GetVariable(int index) const = 0;
  88. //! Returns true if var block contains specified variable.
  89. virtual bool IsContainsVariable(IVariable* pVar, bool bRecursive = false) const = 0;
  90. //! Find variable by name.
  91. virtual IVariable* FindVariable(const char* name, bool bRecursive = false, bool bHumanName = false) const = 0;
  92. //! Return true if variable block is empty (Does not have any vars).
  93. virtual bool IsEmpty() const = 0;
  94. };
  95. //////////////////////////////////////////////////////////////////////////
  96. //////////////////////////////////////////////////////////////////////////
  97. /** IVariable is the variant variable interface.
  98. */
  99. struct IVariable
  100. : public IVariableContainer
  101. {
  102. /** Type of data stored in variable.
  103. */
  104. enum EType
  105. {
  106. UNKNOWN, //!< Unknown parameter type.
  107. INT, //!< Integer property.
  108. BOOL, //!< Boolean property.
  109. FLOAT, //!< Float property.
  110. VECTOR2, //!< Vector property.
  111. VECTOR, //!< Vector property.
  112. VECTOR4, //!< Vector property.
  113. QUAT, //!< Quaternion property.
  114. STRING, //!< String property.
  115. ARRAY, //!< Array of parameters.
  116. FLOW_CUSTOM_DATA, //!< FLow graph custom data
  117. DOUBLE //!< Double property.
  118. };
  119. //! Type of data hold by variable.
  120. enum EDataType
  121. {
  122. DT_SIMPLE = 0, //!< Standard param type (float).
  123. DT_BOOLEAN,
  124. DT_PERCENT, //!< Percent data type, (Same as simple but value is from 0-1 and UI will be from 0-100).
  125. DT_COLOR, //!< RGB Color data type (Vector3).
  126. DT_ANGLE,
  127. DT_TEXTURE,
  128. DT_SHADER,
  129. DT_LOCAL_STRING,
  130. DT_EQUIP,
  131. DT_MATERIALLOOKUP,
  132. DT_EXTARRAY, // Extendable Array
  133. DT_SEQUENCE, // Movie Sequence (DEPRECATED, use DT_SEQUENCE_ID, instead.)
  134. DT_MISSIONOBJ, // Mission Objective
  135. DT_USERITEMCB, // Use a callback GetItemsCallback in user data of variable
  136. DT_UIENUM, // DEPRECATED
  137. DT_SEQUENCE_ID, // Movie Sequence
  138. DT_LIGHT_ANIMATION, // Light Animation Node in the global Light Animation Set
  139. DT_PARTICLE_EFFECT,
  140. DT_DEPRECATED, // formerly DT_FLARE
  141. DT_AUDIO_TRIGGER,
  142. DT_AUDIO_SWITCH,
  143. DT_AUDIO_SWITCH_STATE,
  144. DT_AUDIO_RTPC,
  145. DT_AUDIO_ENVIRONMENT,
  146. DT_AUDIO_PRELOAD_REQUEST,
  147. DT_UI_ELEMENT,
  148. DT_COLORA, //!< RGBA Color data type (Vector4).
  149. DT_MOTION, // Motion animation asset
  150. DT_CURVE = BIT(7), // Combined with other types
  151. };
  152. // Flags that can used with variables.
  153. enum EFlags
  154. {
  155. // User interface related flags.
  156. UI_DISABLED = BIT(0), //!< This variable will be disabled in UI.
  157. UI_BOLD = BIT(1), //!< Variable name in properties will be bold.
  158. UI_SHOW_CHILDREN = BIT(2), //!< Display children in parent field.
  159. UI_USE_GLOBAL_ENUMS = BIT(3), //!< Use CUIEnumsDatabase to fetch enums for this variable name.
  160. UI_INVISIBLE = BIT(4), //!< This variable will not be displayed in the UI
  161. UI_ROLLUP2 = BIT(5), //!< Prefer right roll-up bar for extended property control.
  162. UI_COLLAPSED = BIT(6), //!< Category collapsed by default.
  163. UI_UNSORTED = BIT(7), //!< Do not sort list-box alphabetically.
  164. UI_EXPLICIT_STEP = BIT(8), //!< Use the step size set in the variable for the UI directly.
  165. UI_NOT_DISPLAY_VALUE = BIT(9), //!< The UI will display an empty string where it would normally draw the variable value.
  166. UI_HIGHLIGHT_EDITED = BIT(10), //!< Edited (non-default value) properties show highlight color.
  167. UI_AUTO_EXPAND = BIT(11), //!< Category expanded by default. to auto-expand child in ReflectedPropertyCtrl.
  168. UI_CREATE_SPLINE = BIT(12), //!< To indicate the spline need to be re-created. This is usually because the data was changed directly through the data address
  169. };
  170. typedef AZStd::function<void(IVariable*)> OnSetCallback;
  171. using OnSetEnumCallback = OnSetCallback;
  172. // Store IGetCustomItems into IVariable's UserData and set data-type to
  173. // DT_USERITEMCB
  174. // RefCounting is NOT handled by IVariable!
  175. struct IGetCustomItems
  176. : public CRefCountBase
  177. {
  178. struct SItem
  179. {
  180. SItem() {}
  181. SItem(const QString& name, const QString& desc = QString())
  182. : name(name)
  183. , desc(desc) {}
  184. SItem(const char* name, const char* desc = "")
  185. : name(name)
  186. , desc(desc) {}
  187. QString name;
  188. QString desc;
  189. };
  190. virtual bool GetItems (IVariable* /* pVar */, std::vector<SItem>& /* items */, QString& /* outDialogTitle */) = 0;
  191. virtual bool UseTree() = 0;
  192. virtual const char* GetTreeSeparator() = 0;
  193. };
  194. //! Get name of parameter.
  195. virtual QString GetName() const = 0;
  196. //! Set name of parameter.
  197. virtual void SetName(const QString& name) = 0;
  198. //! Get human readable name of parameter (Normally same as name).
  199. virtual QString GetHumanName() const = 0;
  200. //! Set human readable name of parameter (name without prefix).
  201. virtual void SetHumanName(const QString& name) = 0;
  202. //! Get variable description.
  203. virtual QString GetDescription() const = 0;
  204. //! Set variable description.
  205. virtual void SetDescription(const char* desc) = 0;
  206. //! Set variable description.
  207. virtual void SetDescription(const QString& desc) = 0;
  208. //! Get parameter type.
  209. virtual EType GetType() const = 0;
  210. //! Get size of parameter.
  211. virtual int GetSize() const = 0;
  212. //! Type of data stored in this variable.
  213. virtual unsigned char GetDataType() const = 0;
  214. virtual void SetDataType(unsigned char dataType) = 0;
  215. virtual void SetUserData(const QVariant &data) = 0;
  216. virtual QVariant GetUserData() const = 0;
  217. //////////////////////////////////////////////////////////////////////////
  218. // Flags
  219. //////////////////////////////////////////////////////////////////////////
  220. //! Set variable flags, (Limited to 16 flags).
  221. virtual void SetFlags(int flags) = 0;
  222. virtual int GetFlags() const = 0;
  223. virtual void SetFlagRecursive(EFlags flag) = 0;
  224. /////////////////////////////////////////////////////////////////////////////
  225. // Set methods.
  226. /////////////////////////////////////////////////////////////////////////////
  227. virtual void Set(int value) = 0;
  228. virtual void Set(bool value) = 0;
  229. virtual void Set(float value) = 0;
  230. virtual void Set(double value) = 0;
  231. virtual void Set(const Vec2& value) = 0;
  232. virtual void Set(const Vec3& value) = 0;
  233. virtual void Set(const Vec4& value) = 0;
  234. virtual void Set(const Ang3& value) = 0;
  235. virtual void Set(const Quat& value) = 0;
  236. virtual void Set(const QString& value) = 0;
  237. virtual void Set(const char* value) = 0;
  238. virtual void SetDisplayValue(const QString& value) = 0;
  239. virtual void Set(const AZ::Color& value) = 0;
  240. virtual void Set(const AZ::Vector2& value) = 0;
  241. virtual void Set(const AZ::Vector3& value) = 0;
  242. virtual void Set(const AZ::Vector4& value) = 0;
  243. virtual void Set(const AZ::Quaternion& value) = 0;
  244. virtual void Set(const AZStd::string& value) = 0;
  245. // Called when value updated by any means (including internally).
  246. virtual void OnSetValue(bool bRecursive) = 0;
  247. /////////////////////////////////////////////////////////////////////////////
  248. // Get methods.
  249. /////////////////////////////////////////////////////////////////////////////
  250. virtual void Get(int& value) const = 0;
  251. virtual void Get(bool& value) const = 0;
  252. virtual void Get(float& value) const = 0;
  253. virtual void Get(double& value) const = 0;
  254. virtual void Get(Vec2& value) const = 0;
  255. virtual void Get(Vec3& value) const = 0;
  256. virtual void Get(Vec4& value) const = 0;
  257. virtual void Get(Ang3& value) const = 0;
  258. virtual void Get(Quat& value) const = 0;
  259. virtual void Get(QString& value) const = 0;
  260. virtual QString GetDisplayValue() const = 0;
  261. virtual bool HasDefaultValue() const = 0;
  262. virtual void Get(AZ::Color& value) const = 0;
  263. virtual void Get(AZ::Vector2& value) const = 0;
  264. virtual void Get(AZ::Vector3& value) const = 0;
  265. virtual void Get(AZ::Vector4& value) const = 0;
  266. virtual void Get(AZ::Quaternion& value) const = 0;
  267. virtual void Get(AZStd::string& value) const = 0;
  268. //! reset value to default value
  269. virtual void ResetToDefault() = 0;
  270. //! Return cloned value of variable.
  271. virtual IVariable* Clone(bool bRecursive) const = 0;
  272. //! Copy variable value from specified variable.
  273. //! This method executed always recursively on all sub hierarchy of variables,
  274. //! In Array vars, will never create new variables, only copy values of corresponding children.
  275. //! @param fromVar Source variable to copy value from.
  276. virtual void CopyValue(IVariable* fromVar) = 0;
  277. //////////////////////////////////////////////////////////////////////////
  278. // Value Limits.
  279. //////////////////////////////////////////////////////////////////////////
  280. //! Set value limits.
  281. virtual void SetLimits([[maybe_unused]] float fMin, [[maybe_unused]] float fMax, [[maybe_unused]] float fStep = 0.f, [[maybe_unused]] bool bHardMin = true, [[maybe_unused]] bool bHardMax = true) {}
  282. //! Get value limits.
  283. virtual void GetLimits([[maybe_unused]] float& fMin, [[maybe_unused]] float& fMax, [[maybe_unused]] float& fStep, [[maybe_unused]] bool& bHardMin, [[maybe_unused]] bool& bHardMax) {}
  284. void GetLimits(float& fMin, float& fMax)
  285. {
  286. float f;
  287. bool b;
  288. GetLimits(fMin, fMax, f, b, b);
  289. }
  290. virtual bool HasCustomLimits() { return false; }
  291. virtual void EnableNotifyWithoutValueChange([[maybe_unused]] bool bFlag){}
  292. //////////////////////////////////////////////////////////////////////////
  293. // Wire/Unwire variables.
  294. //////////////////////////////////////////////////////////////////////////
  295. //! Wire variable, wired variable will be changed when this var changes.
  296. virtual void Wire(IVariable* targetVar) = 0;
  297. //! Unwire variable.
  298. virtual void Unwire(IVariable* targetVar) = 0;
  299. //////////////////////////////////////////////////////////////////////////
  300. // Assign on set callback.
  301. //////////////////////////////////////////////////////////////////////////
  302. virtual void AddOnSetCallback(OnSetCallback* func) = 0;
  303. virtual void RemoveOnSetCallback(OnSetCallback* func) = 0;
  304. virtual void ClearOnSetCallbacks() {}
  305. //////////////////////////////////////////////////////////////////////////
  306. // Assign callback triggered when enums change.
  307. //////////////////////////////////////////////////////////////////////////
  308. virtual void AddOnSetEnumCallback(OnSetEnumCallback* func) = 0;
  309. virtual void RemoveOnSetEnumCallback(OnSetCallback* func) = 0;
  310. virtual void ClearOnSetEnumCallbacks() {}
  311. //////////////////////////////////////////////////////////////////////////
  312. //! Retrieve pointer to selection list used by variable.
  313. virtual IVarEnumList* GetEnumList() const { return 0; }
  314. virtual ISplineInterpolator* GetSpline() { return 0; }
  315. //////////////////////////////////////////////////////////////////////////
  316. //! Serialize variable to XML.
  317. //////////////////////////////////////////////////////////////////////////
  318. virtual void Serialize(XmlNodeRef node, bool load) = 0;
  319. // From CObject, (not implemented)
  320. virtual void Serialize([[maybe_unused]] CArchive& ar) {}
  321. //////////////////////////////////////////////////////////////////////////
  322. // Disables the update callbacks for certain operations in order to avoid
  323. // too many function calls when not needed.
  324. //////////////////////////////////////////////////////////////////////////
  325. virtual void EnableUpdateCallbacks(bool boEnable) = 0;
  326. // Setup to true to force save Undo in case the value the same.
  327. virtual void SetForceModified([[maybe_unused]] bool bForceModified) {}
  328. };
  329. // Smart pointer to this parameter.
  330. typedef _smart_ptr<IVariable> IVariablePtr;
  331. AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  332. /**
  333. **************************************************************************************
  334. * CVariableBase implements IVariable interface and provide default implementation
  335. * for basic IVariable functionality (Name, Flags, etc...)
  336. * CVariableBase cannot be instantiated directly and should be used as the base class for
  337. * actual Variant implementation classes.
  338. ***************************************************************************************
  339. */
  340. class EDITOR_CORE_API CVariableBase
  341. : public IVariable
  342. {
  343. AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  344. public:
  345. virtual ~CVariableBase() {}
  346. void SetName(const QString& name) override { m_name = name; };
  347. //! Get name of parameter.
  348. QString GetName() const override { return m_name; };
  349. QString GetHumanName() const override
  350. {
  351. if (!m_humanName.isEmpty())
  352. {
  353. return m_humanName;
  354. }
  355. return m_name;
  356. }
  357. void SetHumanName(const QString& name) override { m_humanName = name; }
  358. void SetDescription(const char* desc) override { m_description = desc; };
  359. void SetDescription(const QString& desc) override { m_description = desc; };
  360. //! Get name of parameter.
  361. QString GetDescription() const override { return m_description; };
  362. EType GetType() const override { return IVariable::UNKNOWN; };
  363. int GetSize() const override { return sizeof(*this); };
  364. unsigned char GetDataType() const override { return m_dataType; };
  365. void SetDataType(unsigned char dataType) override { m_dataType = dataType; }
  366. void SetFlags(int flags) override { m_flags = static_cast<uint16>(flags); }
  367. int GetFlags() const override { return m_flags; }
  368. void SetFlagRecursive(EFlags flag) override { m_flags |= flag; }
  369. void SetUserData(const QVariant &data) override { m_userData = data; };
  370. QVariant GetUserData() const override { return m_userData; }
  371. //////////////////////////////////////////////////////////////////////////
  372. // Set methods.
  373. //////////////////////////////////////////////////////////////////////////
  374. void Set([[maybe_unused]] int value) override { assert(0); }
  375. void Set([[maybe_unused]] bool value) override { assert(0); }
  376. void Set([[maybe_unused]] float value) override { assert(0); }
  377. void Set([[maybe_unused]] double value) override { assert(0); }
  378. void Set([[maybe_unused]] const Vec2& value) override { assert(0); }
  379. void Set([[maybe_unused]] const Vec3& value) override { assert(0); }
  380. void Set([[maybe_unused]] const Vec4& value) override { assert(0); }
  381. void Set([[maybe_unused]] const Ang3& value) override { assert(0); }
  382. void Set([[maybe_unused]] const Quat& value) override { assert(0); }
  383. void Set([[maybe_unused]] const QString& value) override { assert(0); }
  384. void Set([[maybe_unused]] const char* value) override { assert(0); }
  385. void SetDisplayValue(const QString& value) override { Set(value); }
  386. void Set([[maybe_unused]] const AZ::Color& value) override { assert(0); }
  387. void Set([[maybe_unused]] const AZ::Vector2& value) override { assert(0); }
  388. void Set([[maybe_unused]] const AZ::Vector3& value) override { assert(0); }
  389. void Set([[maybe_unused]] const AZ::Vector4& value) override { assert(0); }
  390. void Set([[maybe_unused]] const AZ::Quaternion& value) override { assert(0); }
  391. void Set([[maybe_unused]] const AZStd::string& value) override { assert(0); }
  392. //////////////////////////////////////////////////////////////////////////
  393. // Get methods.
  394. //////////////////////////////////////////////////////////////////////////
  395. void Get([[maybe_unused]] int& value) const override { assert(0); }
  396. void Get([[maybe_unused]] bool& value) const override { assert(0); }
  397. void Get([[maybe_unused]] float& value) const override { assert(0); }
  398. void Get([[maybe_unused]] double& value) const override { assert(0); }
  399. void Get([[maybe_unused]] Vec2& value) const override { assert(0); }
  400. void Get([[maybe_unused]] Vec3& value) const override { assert(0); }
  401. void Get([[maybe_unused]] Vec4& value) const override { assert(0); }
  402. void Get([[maybe_unused]] Ang3& value) const override { assert(0); }
  403. void Get([[maybe_unused]] Quat& value) const override { assert(0); }
  404. void Get([[maybe_unused]] QString& value) const override { assert(0); }
  405. QString GetDisplayValue() const override { QString val; Get(val); return val; }
  406. void Get([[maybe_unused]] AZ::Color& value) const override { assert(0); }
  407. void Get([[maybe_unused]] AZ::Vector2& value) const override { assert(0); }
  408. void Get([[maybe_unused]] AZ::Vector3& value) const override { assert(0); }
  409. void Get([[maybe_unused]] AZ::Vector4& value) const override { assert(0); }
  410. void Get([[maybe_unused]] AZ::Quaternion& value) const override { assert(0); }
  411. void Get([[maybe_unused]] AZStd::string& value) const override { assert(0); }
  412. //////////////////////////////////////////////////////////////////////////
  413. // IVariableContainer functions
  414. //////////////////////////////////////////////////////////////////////////
  415. void AddVariable([[maybe_unused]] IVariable* var) override { assert(0); }
  416. bool DeleteVariable([[maybe_unused]] IVariable* var, [[maybe_unused]] bool recursive = false) override { return false; }
  417. void DeleteAllVariables() override {}
  418. int GetNumVariables() const override { return 0; }
  419. IVariable* GetVariable([[maybe_unused]] int index) const override { return nullptr; }
  420. bool IsContainsVariable([[maybe_unused]] IVariable* pVar, [[maybe_unused]] bool bRecursive = false) const override { return false; }
  421. IVariable* FindVariable([[maybe_unused]] const char* name, [[maybe_unused]] bool bRecursive = false, [[maybe_unused]] bool bHumanName = false) const override { return nullptr; }
  422. bool IsEmpty() const override { return true; }
  423. //////////////////////////////////////////////////////////////////////////
  424. void Wire(IVariable* var) override
  425. {
  426. m_wiredVars.push_back(var);
  427. }
  428. //////////////////////////////////////////////////////////////////////////
  429. void Unwire(IVariable* var) override
  430. {
  431. if (!var)
  432. {
  433. // Unwire all.
  434. m_wiredVars.clear();
  435. }
  436. else
  437. {
  438. stl::find_and_erase(m_wiredVars, var);
  439. }
  440. }
  441. //////////////////////////////////////////////////////////////////////////
  442. void AddOnSetCallback(OnSetCallback* func) override
  443. {
  444. if (!stl::find(m_onSetFuncs, func))
  445. {
  446. m_onSetFuncs.push_back(func);
  447. }
  448. }
  449. //////////////////////////////////////////////////////////////////////////
  450. void RemoveOnSetCallback(OnSetCallback* func) override
  451. {
  452. stl::find_and_erase(m_onSetFuncs, func);
  453. }
  454. //////////////////////////////////////////////////////////////////////////
  455. void ClearOnSetCallbacks() override
  456. {
  457. m_onSetFuncs.clear();
  458. }
  459. void AddOnSetEnumCallback(OnSetEnumCallback* func) override
  460. {
  461. if (!stl::find(m_onSetEnumFuncs, func))
  462. {
  463. m_onSetEnumFuncs.push_back(func);
  464. }
  465. }
  466. //////////////////////////////////////////////////////////////////////////
  467. void RemoveOnSetEnumCallback(OnSetCallback* func) override
  468. {
  469. stl::find_and_erase(m_onSetEnumFuncs, func);
  470. }
  471. void ClearOnSetEnumCallbacks() override
  472. {
  473. m_onSetEnumFuncs.clear();
  474. }
  475. void OnSetValue([[maybe_unused]] bool bRecursive) override
  476. {
  477. // If have wired variables or OnSet callback, process them.
  478. // Send value to wired variable.
  479. for (CVariableBase::WiredList::iterator it = m_wiredVars.begin(); it != m_wiredVars.end(); ++it)
  480. {
  481. if (m_bForceModified)
  482. {
  483. (*it)->SetForceModified(true);
  484. }
  485. // Copy value to wired vars.
  486. (*it)->CopyValue(this);
  487. }
  488. if (!m_boUpdateCallbacksEnabled)
  489. {
  490. return;
  491. }
  492. // Call on set callback.
  493. for (auto it = m_onSetFuncs.begin(); it != m_onSetFuncs.end(); ++it)
  494. {
  495. // Call on set callback.
  496. (*it)->operator()(this);
  497. }
  498. }
  499. //////////////////////////////////////////////////////////////////////////
  500. using IVariable::Serialize;
  501. void Serialize(XmlNodeRef node, bool load) override
  502. {
  503. if (load)
  504. {
  505. if (node->haveAttr(m_name.toUtf8().data()))
  506. {
  507. Set(node->getAttr(m_name.toUtf8().data()));
  508. }
  509. }
  510. else
  511. {
  512. // Saving.
  513. QString str;
  514. Get(str);
  515. node->setAttr(m_name.toUtf8().data(), str.toUtf8().data());
  516. }
  517. }
  518. void EnableUpdateCallbacks(bool boEnable) override{m_boUpdateCallbacksEnabled = boEnable; };
  519. void SetForceModified(bool bForceModified) override { m_bForceModified = bForceModified; }
  520. protected:
  521. // Constructor.
  522. CVariableBase()
  523. : m_dataType(DT_SIMPLE)
  524. , m_flags(0)
  525. , m_boUpdateCallbacksEnabled(true)
  526. , m_bForceModified(false)
  527. {
  528. }
  529. // Copy constructor.
  530. CVariableBase(const CVariableBase& var)
  531. {
  532. m_name = var.m_name;
  533. m_humanName = var.m_humanName;
  534. m_description = var.m_description;
  535. m_flags = var.m_flags;
  536. m_dataType = var.m_dataType;
  537. m_userData = var.m_userData;
  538. m_boUpdateCallbacksEnabled = true;
  539. m_bForceModified = var.m_bForceModified;
  540. // Never copy callback function or wired variables they are private to specific variable,
  541. }
  542. // Not allow.
  543. CVariableBase& operator=([[maybe_unused]] const CVariableBase& var)
  544. {
  545. return *this;
  546. }
  547. //////////////////////////////////////////////////////////////////////////
  548. // Variables.
  549. //////////////////////////////////////////////////////////////////////////
  550. typedef std::vector<OnSetCallback*> OnSetCallbackList;
  551. typedef std::vector<IVariablePtr> WiredList;
  552. using OnSetEnumCallbackList = std::vector<OnSetEnumCallback*>;
  553. QString m_name;
  554. QString m_humanName;
  555. QString m_description;
  556. //! Optional user data pointer
  557. QVariant m_userData;
  558. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  559. //! Extended data (Extended data is never copied, it's always private to this variable).
  560. WiredList m_wiredVars;
  561. OnSetCallbackList m_onSetFuncs;
  562. OnSetEnumCallbackList m_onSetEnumFuncs;
  563. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  564. uint16 m_flags;
  565. //! Limited to 8 flags.
  566. unsigned char m_dataType;
  567. bool m_boUpdateCallbacksEnabled;
  568. bool m_bForceModified;
  569. };
  570. /**
  571. **************************************************************************************
  572. * CVariableArray implements variable of type array of IVariables.
  573. ***************************************************************************************
  574. */
  575. class EDITOR_CORE_API CVariableArray
  576. : public CVariableBase
  577. {
  578. public:
  579. CVariableArray(){}
  580. //! Get name of parameter.
  581. EType GetType() const override { return IVariable::ARRAY; };
  582. int GetSize() const override { return sizeof(CVariableArray); };
  583. //////////////////////////////////////////////////////////////////////////
  584. // Set methods.
  585. //////////////////////////////////////////////////////////////////////////
  586. void Set(const QString& value) override
  587. {
  588. if (m_strValue != value)
  589. {
  590. m_strValue = value;
  591. OnSetValue(false);
  592. }
  593. }
  594. void OnSetValue(bool bRecursive) override
  595. {
  596. CVariableBase::OnSetValue(bRecursive);
  597. if (bRecursive)
  598. {
  599. for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  600. {
  601. (*it)->OnSetValue(true);
  602. }
  603. }
  604. }
  605. void SetFlagRecursive(EFlags flag) override
  606. {
  607. CVariableBase::SetFlagRecursive(flag);
  608. for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  609. {
  610. (*it)->SetFlagRecursive(flag);
  611. }
  612. }
  613. //////////////////////////////////////////////////////////////////////////
  614. // Get methods.
  615. //////////////////////////////////////////////////////////////////////////
  616. void Get(QString& value) const override { value = m_strValue; }
  617. void Get(AZStd::string& value) const override { value.assign(m_strValue.toLatin1().data()); }
  618. bool HasDefaultValue() const override
  619. {
  620. for (Variables::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  621. {
  622. if (!(*it)->HasDefaultValue())
  623. {
  624. return false;
  625. }
  626. }
  627. return true;
  628. }
  629. void ResetToDefault() override
  630. {
  631. for (Variables::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  632. {
  633. (*it)->ResetToDefault();
  634. }
  635. }
  636. //////////////////////////////////////////////////////////////////////////
  637. IVariable* Clone(bool bRecursive) const override
  638. {
  639. CVariableArray* var = new CVariableArray(*this);
  640. // m_vars was shallow-duplicated, clone elements.
  641. for (int i = 0; i < m_vars.size(); i++)
  642. {
  643. var->m_vars[i] = m_vars[i]->Clone(bRecursive);
  644. }
  645. return var;
  646. }
  647. //////////////////////////////////////////////////////////////////////////
  648. void CopyValue(IVariable* fromVar) override
  649. {
  650. assert(fromVar);
  651. if (fromVar->GetType() != IVariable::ARRAY)
  652. {
  653. return;
  654. }
  655. int numSrc = fromVar->GetNumVariables();
  656. int numTrg = static_cast<int>(m_vars.size());
  657. for (int i = 0; i < numSrc && i < numTrg; i++)
  658. {
  659. // Copy Every child variable.
  660. m_vars[i]->CopyValue(fromVar->GetVariable(i));
  661. }
  662. QString strValue;
  663. fromVar->Get(strValue);
  664. Set(strValue);
  665. }
  666. //////////////////////////////////////////////////////////////////////////
  667. int GetNumVariables() const override { return static_cast<int>(m_vars.size()); }
  668. IVariable* GetVariable(int index) const override
  669. {
  670. assert(index >= 0 && index < (int)m_vars.size());
  671. return m_vars[index];
  672. }
  673. void AddVariable(IVariable* var) override
  674. {
  675. m_vars.push_back(var);
  676. }
  677. bool DeleteVariable(IVariable* var, bool recursive /*=false*/) override
  678. {
  679. bool found = stl::find_and_erase(m_vars, var);
  680. if (!found && recursive)
  681. {
  682. for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  683. {
  684. if ((*it)->DeleteVariable(var, recursive))
  685. {
  686. return true;
  687. }
  688. }
  689. }
  690. return found;
  691. }
  692. void DeleteAllVariables() override
  693. {
  694. m_vars.clear();
  695. }
  696. bool IsContainsVariable(IVariable* pVar, bool bRecursive) const override
  697. {
  698. for (Variables::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  699. {
  700. if (*it == pVar)
  701. {
  702. return true;
  703. }
  704. }
  705. // If not found search children.
  706. if (bRecursive)
  707. {
  708. // Search all top level variables.
  709. for (Variables::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  710. {
  711. if ((*it)->IsContainsVariable(pVar))
  712. {
  713. return true;
  714. }
  715. }
  716. }
  717. return false;
  718. }
  719. IVariable* FindVariable(const char* name, bool bRecursive, bool bHumanName) const override;
  720. bool IsEmpty() const override
  721. {
  722. return m_vars.empty();
  723. }
  724. using IVariable::Serialize;
  725. void Serialize(XmlNodeRef node, bool load) override
  726. {
  727. if (load)
  728. {
  729. // Loading.
  730. QString name;
  731. for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  732. {
  733. IVariable* var = *it;
  734. if (var->GetNumVariables())
  735. {
  736. XmlNodeRef child = node->findChild(var->GetName().toUtf8().data());
  737. if (child)
  738. {
  739. var->Serialize(child, load);
  740. }
  741. }
  742. else
  743. {
  744. var->Serialize(node, load);
  745. }
  746. }
  747. }
  748. else
  749. {
  750. // Saving.
  751. for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  752. {
  753. IVariable* var = *it;
  754. if (var->GetNumVariables())
  755. {
  756. XmlNodeRef child = node->newChild(var->GetName().toUtf8().data());
  757. var->Serialize(child, load);
  758. }
  759. else
  760. {
  761. var->Serialize(node, load);
  762. }
  763. }
  764. }
  765. }
  766. protected:
  767. typedef std::vector<IVariablePtr> Variables;
  768. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  769. Variables m_vars;
  770. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  771. //! Any string value displayed in properties.
  772. QString m_strValue;
  773. };
  774. /** var_type namespace includes type definitions needed for CVariable implementation.
  775. */
  776. namespace var_type
  777. {
  778. //////////////////////////////////////////////////////////////////////////
  779. template <int TypeID, bool IsStandart, bool IsInteger, bool IsSigned, bool SupportsRange>
  780. struct type_traits_base
  781. {
  782. static int type() { return TypeID; };
  783. //! Return true if standard C++ type.
  784. static bool is_standart() { return IsStandart; };
  785. static bool is_integer() { return IsInteger; };
  786. static bool is_signed() { return IsSigned; };
  787. static bool supports_range() { return SupportsRange; };
  788. };
  789. template <class Type>
  790. struct type_traits
  791. : public type_traits_base<IVariable::UNKNOWN, false, false, false, false> {};
  792. // Types specialization.
  793. template<>
  794. struct type_traits<int>
  795. : public type_traits_base<IVariable::INT, true, true, true, true> {};
  796. template<>
  797. struct type_traits<bool>
  798. : public type_traits_base<IVariable::BOOL, true, true, false, true> {};
  799. template<>
  800. struct type_traits<float>
  801. : public type_traits_base<IVariable::FLOAT, true, false, false, true> {};
  802. template<>
  803. struct type_traits<double>
  804. : public type_traits_base<IVariable::DOUBLE, true, false, false, true>{};
  805. template<>
  806. struct type_traits<Vec2>
  807. : public type_traits_base<IVariable::VECTOR2, false, false, false, false> {};
  808. template<>
  809. struct type_traits<Vec3>
  810. : public type_traits_base<IVariable::VECTOR, false, false, false, false> {};
  811. template<>
  812. struct type_traits<Vec4>
  813. : public type_traits_base<IVariable::VECTOR4, false, false, false, false> {};
  814. template<>
  815. struct type_traits<Quat>
  816. : public type_traits_base<IVariable::QUAT, false, false, false, false> {};
  817. template<>
  818. struct type_traits<AZStd::string>
  819. : public type_traits_base<IVariable::STRING, false, false, false, false> {};
  820. template<>
  821. struct type_traits<AZ::Vector2>
  822. : public type_traits_base<IVariable::VECTOR2, false, false, false, false> {};
  823. template<>
  824. struct type_traits<AZ::Vector3>
  825. : public type_traits_base<IVariable::VECTOR, false, false, false, false> {};
  826. template<>
  827. struct type_traits<AZ::Vector4>
  828. : public type_traits_base<IVariable::VECTOR4, false, false, false, false> {};
  829. template<>
  830. struct type_traits<AZ::Quaternion>
  831. : public type_traits_base<IVariable::QUAT, false, false, false, false> {};
  832. //////////////////////////////////////////////////////////////////////////
  833. //////////////////////////////////////////////////////////////////////////
  834. // General one type to another type converter class.
  835. //////////////////////////////////////////////////////////////////////////
  836. struct type_converter
  837. {
  838. template <class From, class To>
  839. void operator()([[maybe_unused]] const From& from, [[maybe_unused]] To& to) const { assert(0); }
  840. void operator()(const int& from, int& to) const { to = from; }
  841. void operator()(const int& from, bool& to) const { to = from != 0; }
  842. void operator()(const int& from, float& to) const { to = (float)from; }
  843. //////////////////////////////////////////////////////////////////////////
  844. void operator()(const bool& from, int& to) const { to = from; }
  845. void operator()(const bool& from, bool& to) const { to = from; }
  846. void operator()(const bool& from, float& to) const { to = from; }
  847. //////////////////////////////////////////////////////////////////////////
  848. void operator()(const float& from, int& to) const { to = (int)from; }
  849. void operator()(const float& from, bool& to) const { to = from != 0; }
  850. void operator()(const float& from, float& to) const { to = from; }
  851. void operator()(const double& from, int& to) const { to = (int)from; }
  852. void operator()(const double& from, bool& to) const { to = from != 0; }
  853. void operator()(const double& from, float& to) const { to = aznumeric_cast<float>(from); }
  854. void operator()(const Vec2& from, Vec2& to) const { to = from; }
  855. void operator()(const Vec3& from, Vec3& to) const { to = from; }
  856. void operator()(const Vec4& from, Vec4& to) const { to = from; }
  857. void operator()(const Quat& from, Quat& to) const { to = from; }
  858. void operator()(const QString& from, QString& to) const { to = from; }
  859. void operator()(const AZ::Color& from, AZ::Color& to) const { to = from; }
  860. void operator()(const AZ::Vector2& from, AZ::Vector2& to) const { to = from; }
  861. void operator()(const AZ::Vector3& from, AZ::Vector3& to) const { to = from; }
  862. void operator()(const AZ::Vector4& from, AZ::Vector4& to) const { to = from; }
  863. void operator()(const AZ::Quaternion& from, AZ::Quaternion& to) const { to = from; }
  864. void operator()(const AZStd::string& from, AZStd::string& to) const { to = from; }
  865. void operator()(bool value, QString& to) const { to = QString::number((value) ? (int)1 : (int)0); }
  866. void operator()(int value, QString& to) const { to = QString::number(value); }
  867. void operator()(float value, QString& to) const { to = QString::number(value); };
  868. void operator()(double value, QString& to) const { to = QString::number(value); };
  869. void operator()(const Vec2& value, QString& to) const { to = QString::fromLatin1("%1,%2").arg(value.x).arg(value.y); }
  870. void operator()(const Vec3& value, QString& to) const { to = QString::fromLatin1("%1,%2,%3").arg(value.x).arg(value.y).arg(value.z); }
  871. void operator()(const Vec4& value, QString& to) const { to = QString::fromLatin1("%1,%2,%3,%4").arg(value.x).arg(value.y).arg(value.z).arg(value.w); }
  872. void operator()(const Ang3& value, QString& to) const { to = QString::fromLatin1("%1,%2,%3").arg(value.x).arg(value.y).arg(value.z); }
  873. void operator()(const Quat& value, QString& to) const { to = QString::fromLatin1("%1,%2,%3,%4").arg(value.w).arg(value.v.x).arg(value.v.y).arg(value.v.z); }
  874. void operator()(const AZ::Color& from, QString& to) const { to = QString::fromLatin1("%1,%2,%3,%4").arg(from.GetR()).arg(from.GetG()).arg(from.GetB()).arg(from.GetA()); }
  875. void operator()(const AZ::Vector2& from, QString& to) const { to = QString::fromLatin1("%1,%2").arg(from.GetX()).arg(from.GetY()); }
  876. void operator()(const AZ::Vector3& from, QString& to) const { to = QString::fromLatin1("%1,%2,%3").arg(from.GetX()).arg(from.GetY()).arg(from.GetZ()); }
  877. void operator()(const AZ::Vector4& from, QString& to) const { to = QString::fromLatin1("%1,%2,%3,%4").arg(from.GetX()).arg(from.GetY()).arg(from.GetZ()).arg(from.GetW()); }
  878. void operator()(const AZ::Quaternion& from, QString& to) const { to = QString::fromLatin1("%1,%2,%3,%4").arg(from.GetX()).arg(from.GetY()).arg(from.GetZ()).arg(from.GetW()); }
  879. void operator()(const AZStd::string& from, QString& to) const { to = QString::fromLatin1("%1").arg(from.c_str()); }
  880. void operator()(bool value, AZStd::string& to) const { to = value ? "1" : "0"; }
  881. void operator()(int value, AZStd::string& to) const { to = AZStd::to_string(value); }
  882. void operator()(float value, AZStd::string& to) const { to = AZStd::to_string(value); };
  883. void operator()(double value, AZStd::string& to) const { to = AZStd::to_string(value); };
  884. void operator()(const Vec2& value, AZStd::string& to) const
  885. {
  886. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  887. char buf[128] = { 0 };
  888. azsprintf(buf, "%f,%f", value.x, value.y);
  889. to.assign(buf);
  890. }
  891. void operator()(const Vec3& value, AZStd::string& to) const
  892. {
  893. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  894. char buf[192] = { 0 };
  895. azsprintf(buf, "%f,%f,%f", value.x, value.y, value.z);
  896. to.assign(buf);
  897. }
  898. void operator()(const Vec4& value, AZStd::string& to) const
  899. {
  900. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  901. char buf[256] = { 0 };
  902. azsprintf(buf, "%f,%f,%f,%f", value.x, value.y, value.z, value.w);
  903. to.assign(buf);
  904. }
  905. void operator()(const Ang3& value, AZStd::string& to) const
  906. {
  907. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  908. char buf[192] = { 0 };
  909. azsprintf(buf, "%f,%f,%f", value.x, value.y, value.z);
  910. to.assign(buf);
  911. }
  912. void operator()(const Quat& value, AZStd::string& to) const
  913. {
  914. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  915. char buf[256] = { 0 };
  916. azsprintf(buf, "%f,%f,%f,%f", value.w, value.v.x, value.v.y, value.v.z);
  917. to.assign(buf);
  918. }
  919. void operator()(const AZ::Color& from, AZStd::string& to) const
  920. {
  921. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  922. char buf[256] = { 0 };
  923. azsprintf(buf, "%f,%f,%f,%f", from.GetR(), from.GetG(), from.GetB(), from.GetA());
  924. to.assign(buf);
  925. }
  926. void operator()(const AZ::Vector2& from, AZStd::string& to) const
  927. {
  928. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  929. char buf[128] = { 0 };
  930. azsprintf(buf, "%f,%f", from.GetX(), from.GetY());
  931. to.assign(buf);
  932. }
  933. void operator()(const AZ::Vector3& from, AZStd::string& to) const
  934. {
  935. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  936. char buf[192] = { 0 };
  937. azsprintf(buf, "%f,%f,%f", from.GetX(), from.GetY(), from.GetZ());
  938. to.assign(buf);
  939. }
  940. void operator()(const AZ::Vector4& from, AZStd::string& to) const
  941. {
  942. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  943. char buf[256] = { 0 };
  944. azsprintf(buf, "%f,%f,%f,%f", from.GetX(), from.GetY(), from.GetZ(), from.GetZ());
  945. to.assign(buf);
  946. }
  947. void operator()(const AZ::Quaternion& from, AZStd::string& to) const
  948. {
  949. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  950. char buf[256] = { 0 };
  951. azsprintf(buf, "%f,%f,%f,%f", from.GetX(), from.GetY(), from.GetZ(), from.GetZ());
  952. to.assign(buf);
  953. }
  954. /////////////////////////////////////////// Legal to Current Math Types
  955. void operator()(const Vec2& from, AZ::Vector2& to) const { to.Set(from.x, from.y); }
  956. void operator()(const Vec3& from, AZ::Vector3& to) const { to.Set(from.x, from.y, from.z); }
  957. void operator()(const Vec4& from, AZ::Vector4& to) const { to.Set(from.x, from.y, from.z, from.w); }
  958. void operator()(const Ang3& from, AZ::Vector3& to) const { to.Set(from.x, from.y, from.z); }
  959. void operator()(const Quat& from, AZ::Quaternion& to) const { to.Set(from.v.x, from.v.y, from.v.z, from.w); }
  960. /////////////////////////////////////////// Current to Legal Math Types
  961. void operator()(const AZ::Vector2& from, Vec2& to) const { to.set(from.GetX(), from.GetY()); }
  962. void operator()(const AZ::Vector3& from, Vec3& to) const { to.Set(from.GetX(), from.GetY(), from.GetZ()); }
  963. void operator()(const AZ::Vector4& from, Vec4& to) const { to = Vec4(from.GetX(), from.GetY(), from.GetZ(), from.GetW()); }
  964. void operator()(const AZ::Vector3& from, Ang3& to) const { to.Set(from.GetX(), from.GetY(), from.GetZ()); }
  965. void operator()(const AZ::Quaternion& from, Quat& to) const { to = Quat(from.GetW(), from.GetX(), from.GetY(), from.GetZ()); }
  966. /////////////////////////////////////////// From QString
  967. void operator()(const QString& from, int& value) const { value = from.toInt(); }
  968. void operator()(const QString& from, bool& value) const { value = from.toInt() != 0; }
  969. void operator()(const QString& from, float& value) const { value = from.toFloat(); }
  970. void operator()(const QString& from, double& value) const { value = from.toDouble(); }
  971. void operator()(const QString& from, Vec2& value) const
  972. {
  973. QStringList parts = from.split(QStringLiteral(","));
  974. while (parts.size() < 2)
  975. {
  976. parts.push_back(QString());
  977. }
  978. value.x = parts[0].toFloat();
  979. value.y = parts[1].toFloat();
  980. };
  981. void operator()(const QString& from, Vec3& value) const
  982. {
  983. QStringList parts = from.split(QStringLiteral(","));
  984. while (parts.size() < 3)
  985. {
  986. parts.push_back(QString());
  987. }
  988. value.x = parts[0].toFloat();
  989. value.y = parts[1].toFloat();
  990. value.z = parts[2].toFloat();
  991. };
  992. void operator()(const QString& from, Vec4& value) const
  993. {
  994. QStringList parts = from.split(QStringLiteral(","));
  995. while (parts.size() < 4)
  996. {
  997. parts.push_back(QString());
  998. }
  999. value.x = parts[0].toFloat();
  1000. value.y = parts[1].toFloat();
  1001. value.z = parts[2].toFloat();
  1002. value.w = parts[3].toFloat();
  1003. };
  1004. void operator()(const QString& from, Ang3& value) const
  1005. {
  1006. QStringList parts = from.split(QStringLiteral(","));
  1007. while (parts.size() < 3)
  1008. {
  1009. parts.push_back(QString());
  1010. }
  1011. value.x = parts[0].toFloat();
  1012. value.y = parts[1].toFloat();
  1013. value.z = parts[2].toFloat();
  1014. };
  1015. void operator()(const QString& from, Quat& value) const
  1016. {
  1017. QStringList parts = from.split(QStringLiteral(","));
  1018. while (parts.size() < 4)
  1019. {
  1020. parts.push_back(QString());
  1021. }
  1022. value.w = parts[0].toFloat();
  1023. value.v.x = parts[1].toFloat();
  1024. value.v.y = parts[2].toFloat();
  1025. value.v.z = parts[3].toFloat();
  1026. };
  1027. void operator()(const QString& from, AZ::Color& value) const
  1028. {
  1029. QStringList parts = from.split(QStringLiteral(","));
  1030. while (parts.size() < 4)
  1031. {
  1032. parts.push_back(QString());
  1033. }
  1034. for (int i = 0; i < 4; ++i)
  1035. {
  1036. value.SetElement(i, parts[i].toFloat());
  1037. }
  1038. }
  1039. void operator()(const QString& from, AZ::Vector2& value) const
  1040. {
  1041. QStringList parts = from.split(QStringLiteral(","));
  1042. while (parts.size() < 2)
  1043. {
  1044. parts.push_back(QString());
  1045. }
  1046. for (int i = 0; i < 2; ++i)
  1047. {
  1048. value.SetElement(i, parts[i].toFloat());
  1049. }
  1050. }
  1051. void operator()(const QString& from, AZ::Vector3& value) const
  1052. {
  1053. QStringList parts = from.split(QStringLiteral(","));
  1054. while (parts.size() < 3)
  1055. {
  1056. parts.push_back(QString());
  1057. }
  1058. for (int i = 0; i < 3; ++i)
  1059. {
  1060. value.SetElement(i, parts[i].toFloat());
  1061. }
  1062. }
  1063. void operator()(const QString& from, AZ::Vector4& value) const
  1064. {
  1065. QStringList parts = from.split(QStringLiteral(","));
  1066. while (parts.size() < 4)
  1067. {
  1068. parts.push_back(QString());
  1069. }
  1070. for (int i = 0; i < 4; ++i)
  1071. {
  1072. value.SetElement(i, parts[i].toFloat());
  1073. }
  1074. }
  1075. void operator()(const QString& from, AZ::Quaternion& value) const
  1076. {
  1077. QStringList parts = from.split(QStringLiteral(","));
  1078. while (parts.size() < 4)
  1079. {
  1080. parts.push_back(QString());
  1081. }
  1082. for (int i = 0; i < 4; ++i)
  1083. {
  1084. value.SetElement(i, parts[i].toFloat());
  1085. }
  1086. }
  1087. void operator()(const QString& from, AZStd::string& value) const
  1088. {
  1089. value.assign(from.toUtf8().data());
  1090. }
  1091. /////////////////////////////////////////// From AZStd::string
  1092. void operator()(const AZStd::string& from, int& value) const
  1093. {
  1094. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1095. azsscanf(from.c_str(), "%d", &value);
  1096. }
  1097. void operator()(const AZStd::string& from, bool& value) const
  1098. {
  1099. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1100. int val = 0;
  1101. azsscanf(from.c_str(), "%d", &val);
  1102. value = val != 0;
  1103. }
  1104. void operator()(const AZStd::string& from, float& value) const
  1105. {
  1106. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1107. azsscanf(from.c_str(), "%f", &value);
  1108. }
  1109. void operator()(const AZStd::string& from, double& value) const
  1110. {
  1111. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1112. azsscanf(from.c_str(), "%lf", &value);
  1113. }
  1114. void operator()(const AZStd::string& from, Vec2& value) const
  1115. {
  1116. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1117. azsscanf(from.c_str(), "%f,%f", &value.x, &value.y);
  1118. }
  1119. void operator()(const AZStd::string& from, Vec3& value) const
  1120. {
  1121. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1122. azsscanf(from.c_str(), "%f,%f,%f", &value.x, &value.y, &value.z);
  1123. }
  1124. void operator()(const AZStd::string& from, Vec4& value) const
  1125. {
  1126. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1127. azsscanf(from.c_str(), "%f,%f,%f,%f", &value.x, &value.y, &value.z, &value.w);
  1128. }
  1129. void operator()(const AZStd::string& from, Ang3& value) const
  1130. {
  1131. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1132. azsscanf(from.c_str(), "%f,%f,%f", &value.x, &value.y, &value.z);
  1133. }
  1134. void operator()(const AZStd::string& from, Quat& value) const
  1135. {
  1136. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1137. azsscanf(from.c_str(), "%f,%f,%f,%f", &value.w, &value.v.x, &value.v.y, &value.v.z);
  1138. }
  1139. void operator()(const AZStd::string& from, AZ::Color& value) const
  1140. {
  1141. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1142. float val[4];
  1143. azsscanf(from.c_str(), "%f,%f,%f,%f", val, val + 1, val + 2, val + 3);
  1144. value.Set(val);
  1145. }
  1146. void operator()(const AZStd::string& from, AZ::Vector2& value) const
  1147. {
  1148. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1149. float val[2];
  1150. azsscanf(from.c_str(), "%f,%f,", val, val + 1);
  1151. value = AZ::Vector2::CreateFromFloat2(val);
  1152. }
  1153. void operator()(const AZStd::string& from, AZ::Vector3& value) const
  1154. {
  1155. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1156. float val[3];
  1157. azsscanf(from.c_str(), "%f,%f,%f", val, val + 1, val + 2);
  1158. value.Set(val);
  1159. }
  1160. void operator()(const AZStd::string& from, AZ::Vector4& value) const
  1161. {
  1162. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1163. float val[4];
  1164. azsscanf(from.c_str(), "%f,%f,%f,%f", val, val + 1, val + 2, val + 3);
  1165. value.Set(val);
  1166. };
  1167. void operator()(const AZStd::string& from, AZ::Quaternion& value) const
  1168. {
  1169. AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
  1170. float val[4];
  1171. azsscanf(from.c_str(), "%f,%f,%f,%f", val, val + 1, val + 2, val + 3);
  1172. value.Set(val);
  1173. };
  1174. };
  1175. //////////////////////////////////////////////////////////////////////////
  1176. // Custom comparison functions for different variable type's values,.
  1177. //////////////////////////////////////////////////////////////////////////
  1178. template <class Type>
  1179. inline bool compare(const Type& arg1, const Type& arg2)
  1180. {
  1181. return arg1 == arg2;
  1182. };
  1183. inline bool compare(const Vec2& v1, const Vec2& v2)
  1184. {
  1185. return v1.x == v2.x && v1.y == v2.y;
  1186. }
  1187. inline bool compare(const Vec3& v1, const Vec3& v2)
  1188. {
  1189. return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
  1190. }
  1191. inline bool compare(const Vec4& v1, const Vec4& v2)
  1192. {
  1193. return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z && v1.w == v2.w;
  1194. }
  1195. inline bool compare(const Ang3& v1, const Ang3& v2)
  1196. {
  1197. return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
  1198. }
  1199. inline bool compare(const Quat& q1, const Quat& q2)
  1200. {
  1201. return q1.v.x == q2.v.x && q1.v.y == q2.v.y && q1.v.z == q2.v.z && q1.w == q2.w;
  1202. }
  1203. inline bool compare(const char* s1, const char* s2)
  1204. {
  1205. return strcmp(s1, s2) == 0;
  1206. }
  1207. //////////////////////////////////////////////////////////////////////////
  1208. //////////////////////////////////////////////////////////////////////////
  1209. // Custom Initialization functions for different variable type's values,.
  1210. //////////////////////////////////////////////////////////////////////////
  1211. template <class Type>
  1212. inline void init(Type& val)
  1213. {
  1214. val = 0;
  1215. }
  1216. inline void init(Vec2& val) { val.x = 0; val.y = 0; };
  1217. inline void init(Vec3& val) { val.x = 0; val.y = 0; val.z = 0; };
  1218. inline void init(Vec4& val) { val.x = 0; val.y = 0; val.z = 0; val.w = 0; };
  1219. inline void init(Ang3& val) { val.x = 0; val.y = 0; val.z = 0; };
  1220. inline void init(Quat& val) { val.v.x = 0; val.v.y = 0; val.v.z = 0; val.w = 0; };
  1221. inline void init(AZ::Color& val) { val = AZ::Color::CreateZero(); }
  1222. inline void init(AZ::Vector2& val) { val = AZ::Vector2::CreateZero(); }
  1223. inline void init(AZ::Vector3& val) { val = AZ::Vector3::CreateZero(); }
  1224. inline void init(AZ::Vector4& val) { val = AZ::Vector4::CreateZero(); }
  1225. inline void init(AZ::Quaternion& val) { val = AZ::Quaternion::CreateIdentity(); }
  1226. inline void init(AZStd::string& val) { val = AZStd::string(); }
  1227. inline void init(const char*& val) {
  1228. val = "";
  1229. }
  1230. inline void init([[maybe_unused]] QString& val)
  1231. {
  1232. // self initializing.
  1233. }
  1234. //////////////////////////////////////////////////////////////////////////
  1235. };
  1236. //////////////////////////////////////////////////////////////////////////
  1237. // Void variable does not contain any value.
  1238. //////////////////////////////////////////////////////////////////////////
  1239. class CVariableVoid
  1240. : public CVariableBase
  1241. {
  1242. public:
  1243. CVariableVoid(){};
  1244. EType GetType() const override { return IVariable::UNKNOWN; };
  1245. IVariable* Clone([[maybe_unused]] bool bRecursive) const override { return new CVariableVoid(*this); }
  1246. void CopyValue([[maybe_unused]] IVariable* fromVar) override {};
  1247. bool HasDefaultValue() const override { return true; }
  1248. void ResetToDefault() override {};
  1249. protected:
  1250. CVariableVoid(const CVariableVoid& v)
  1251. : CVariableBase(v) {};
  1252. };
  1253. //////////////////////////////////////////////////////////////////////////
  1254. template <class T>
  1255. class CVariable
  1256. : public CVariableBase
  1257. {
  1258. typedef CVariable<T> Self;
  1259. public:
  1260. // Constructor.
  1261. CVariable()
  1262. : m_valueMin(0.0f)
  1263. , m_valueMax(100.0f)
  1264. , m_valueStep(0.0f)
  1265. , m_bHardMin(false)
  1266. , m_bHardMax(false)
  1267. , m_customLimits(false)
  1268. {
  1269. // Initialize value to zero or empty string.
  1270. var_type::init(m_valueDef);
  1271. }
  1272. explicit CVariable(const T& set)
  1273. : CVariable()
  1274. {
  1275. var_type::init(m_valueDef); // Update F32NAN values in Debug mode
  1276. SetValue(set);
  1277. }
  1278. //! Get name of parameter.
  1279. EType GetType() const override { return (EType)var_type::type_traits<T>::type(); };
  1280. int GetSize() const override { return sizeof(T); };
  1281. //////////////////////////////////////////////////////////////////////////
  1282. // Set methods.
  1283. //////////////////////////////////////////////////////////////////////////
  1284. void Set(int value) override { SetValue(value); }
  1285. void Set(bool value) override { SetValue(value); }
  1286. void Set(float value) override { SetValue(value); }
  1287. void Set(double value) override { SetValue(value); }
  1288. void Set(const Vec2& value) override { SetValue(value); }
  1289. void Set(const Vec3& value) override { SetValue(value); }
  1290. void Set(const Vec4& value) override { SetValue(value); }
  1291. void Set(const Ang3& value) override { SetValue(value); }
  1292. void Set(const Quat& value) override { SetValue(value); }
  1293. void Set(const QString& value) override { SetValue(value); }
  1294. void Set(const char* value) override { SetValue(QString(value)); }
  1295. void Set(const AZ::Color& value) override { SetValue(value); }
  1296. void Set(const AZ::Vector2& value) override { SetValue(value); }
  1297. void Set(const AZ::Vector3& value) override { SetValue(value); }
  1298. void Set(const AZ::Vector4& value) override { SetValue(value); }
  1299. void Set(const AZ::Quaternion& value) override { SetValue(value); }
  1300. void Set(const AZStd::string& value) override { SetValue(value); }
  1301. //////////////////////////////////////////////////////////////////////////
  1302. // Get methods.
  1303. //////////////////////////////////////////////////////////////////////////
  1304. void Get(int& value) const override { GetValue(value); }
  1305. void Get(bool& value) const override { GetValue(value); }
  1306. void Get(float& value) const override { GetValue(value); }
  1307. void Get(double& value) const override { GetValue(value); }
  1308. void Get(Vec2& value) const override { GetValue(value); }
  1309. void Get(Vec3& value) const override { GetValue(value); }
  1310. void Get(Vec4& value) const override { GetValue(value); }
  1311. void Get(Quat& value) const override { GetValue(value); }
  1312. void Get(QString& value) const override { GetValue(value); }
  1313. void Get(AZ::Color& value) const override { GetValue(value); }
  1314. void Get(AZ::Vector2& value) const override { GetValue(value); }
  1315. void Get(AZ::Vector3& value) const override { GetValue(value); }
  1316. void Get(AZ::Vector4& value) const override { GetValue(value); }
  1317. void Get(AZ::Quaternion& value) const override { GetValue(value); }
  1318. void Get(AZStd::string& value) const override { GetValue(value); }
  1319. bool HasDefaultValue() const override
  1320. {
  1321. T defval;
  1322. var_type::init(defval);
  1323. return m_valueDef == defval;
  1324. }
  1325. void ResetToDefault() override
  1326. {
  1327. T defval;
  1328. var_type::init(defval);
  1329. SetValue(defval);
  1330. }
  1331. //////////////////////////////////////////////////////////////////////////
  1332. // Limits.
  1333. //////////////////////////////////////////////////////////////////////////
  1334. void SetLimits(float fMin, float fMax, float fStep = 0.f, bool bHardMin = true, bool bHardMax = true) override
  1335. {
  1336. m_valueMin = fMin;
  1337. m_valueMax = fMax;
  1338. m_valueStep = fStep;
  1339. m_bHardMin = bHardMin;
  1340. m_bHardMax = bHardMax;
  1341. // Flag to determine when this variable has custom limits set
  1342. m_customLimits = true;
  1343. }
  1344. void GetLimits(float& fMin, float& fMax, float& fStep, bool& bHardMin, bool& bHardMax) override
  1345. {
  1346. if (!m_customLimits && var_type::type_traits<T>::supports_range())
  1347. {
  1348. float value;
  1349. GetValue(value);
  1350. if (value > m_valueMax && !m_bHardMax)
  1351. {
  1352. SetLimits(m_valueMin, nextNiceNumberAbove(value), 0.0, false, false);
  1353. }
  1354. if (value < m_valueMin && !m_bHardMin)
  1355. {
  1356. SetLimits(nextNiceNumberBelow(value), m_valueMax, 0.0, false, false);
  1357. }
  1358. }
  1359. fMin = m_valueMin;
  1360. fMax = m_valueMax;
  1361. fStep = m_valueStep;
  1362. bHardMin = m_bHardMin;
  1363. bHardMax = m_bHardMax;
  1364. }
  1365. void ClearLimits()
  1366. {
  1367. SetLimits(0.0f, 0.0f, 0.0f, false, false);
  1368. m_customLimits = false;
  1369. }
  1370. bool HasCustomLimits() override
  1371. {
  1372. return m_customLimits;
  1373. }
  1374. //////////////////////////////////////////////////////////////////////////
  1375. // Access operators.
  1376. //////////////////////////////////////////////////////////////////////////
  1377. //! Cast to held type.
  1378. operator T const& () const {
  1379. return m_valueDef;
  1380. }
  1381. //! Assign operator for variable.
  1382. void operator=(const T& value) { SetValue(value); }
  1383. //////////////////////////////////////////////////////////////////////////
  1384. IVariable* Clone([[maybe_unused]] bool bRecursive) const override
  1385. {
  1386. Self* var = new Self(*this);
  1387. return var;
  1388. }
  1389. //////////////////////////////////////////////////////////////////////////
  1390. void CopyValue(IVariable* fromVar) override
  1391. {
  1392. assert(fromVar);
  1393. T val;
  1394. fromVar->Get(val);
  1395. SetValue(val);
  1396. }
  1397. protected:
  1398. //////////////////////////////////////////////////////////////////////////
  1399. template <class P>
  1400. void SetValue(const P& value)
  1401. {
  1402. T newValue = T();
  1403. //var_type::type_converter<P,T> converter;
  1404. var_type::type_converter converter;
  1405. converter(value, newValue);
  1406. // compare old and new values.
  1407. if (m_bForceModified || !var_type::compare(m_valueDef, newValue))
  1408. {
  1409. m_valueDef = newValue;
  1410. m_bForceModified = false;
  1411. OnSetValue(false);
  1412. }
  1413. }
  1414. //////////////////////////////////////////////////////////////////////////
  1415. template <class P>
  1416. void GetValue(P& value) const
  1417. {
  1418. var_type::type_converter converter;
  1419. converter(m_valueDef, value);
  1420. }
  1421. protected:
  1422. T m_valueDef;
  1423. bool m_customLimits;
  1424. bool m_bResolving;
  1425. unsigned char m_bHardMin : 1;
  1426. unsigned char m_bHardMax : 1;
  1427. // Min/Max value.
  1428. float m_valueMin, m_valueMax, m_valueStep;
  1429. };
  1430. //////////////////////////////////////////////////////////////////////////
  1431. /**
  1432. **************************************************************************************
  1433. * CVariableArray implements variable of type array of IVariables.
  1434. ***************************************************************************************
  1435. */
  1436. template <class T>
  1437. class TVariableArray
  1438. : public CVariable<T>
  1439. {
  1440. typedef TVariableArray<T> Self;
  1441. public:
  1442. using CVariable<T>::GetType;
  1443. TVariableArray()
  1444. : CVariable<T>() {};
  1445. // Copy Constructor.
  1446. TVariableArray(const Self& var)
  1447. : CVariable<T>(var)
  1448. {}
  1449. //! Get name of parameter.
  1450. virtual int GetSize() const { return sizeof(Self); };
  1451. virtual bool HasDefaultValue() const
  1452. {
  1453. for (Vars::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  1454. {
  1455. if (!(*it)->HasDefaultValue())
  1456. {
  1457. return false;
  1458. }
  1459. }
  1460. return true;
  1461. }
  1462. virtual void ResetToDefault()
  1463. {
  1464. for (Vars::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  1465. {
  1466. (*it)->ResetToDefault();
  1467. }
  1468. }
  1469. //////////////////////////////////////////////////////////////////////////
  1470. IVariable* Clone(bool bRecursive) const
  1471. {
  1472. Self* var = new Self(*this);
  1473. for (Vars::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  1474. {
  1475. var->m_vars.push_back((*it)->Clone(bRecursive));
  1476. }
  1477. return var;
  1478. }
  1479. //////////////////////////////////////////////////////////////////////////
  1480. void CopyValue(IVariable* fromVar)
  1481. {
  1482. assert(fromVar);
  1483. if (fromVar->GetType() != GetType())
  1484. {
  1485. return;
  1486. }
  1487. CVariable<T>::CopyValue(fromVar);
  1488. int numSrc = fromVar->GetNumVariables();
  1489. int numTrg = m_vars.size();
  1490. for (int i = 0; i < numSrc && i < numTrg; i++)
  1491. {
  1492. // Copy Every child variable.
  1493. m_vars[i]->CopyValue(fromVar->GetVariable(i));
  1494. }
  1495. }
  1496. //////////////////////////////////////////////////////////////////////////
  1497. int GetNumVariables() const { return m_vars.size(); }
  1498. IVariable* GetVariable(int index) const
  1499. {
  1500. assert(index >= 0 && index < (int)m_vars.size());
  1501. return m_vars[index];
  1502. }
  1503. void AddVariable(IVariable* var) { m_vars.push_back(var); }
  1504. void DeleteAllVariables() { m_vars.clear(); }
  1505. //////////////////////////////////////////////////////////////////////////
  1506. void Serialize(XmlNodeRef node, bool load)
  1507. {
  1508. CVariable<T>::Serialize(node, load);
  1509. if (load)
  1510. {
  1511. // Loading.
  1512. QString name;
  1513. for (Vars::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  1514. {
  1515. IVariable* var = *it;
  1516. if (var->GetNumVariables())
  1517. {
  1518. XmlNodeRef child = node->findChild(var->GetName().toUtf8().data());
  1519. if (child)
  1520. {
  1521. var->Serialize(child, load);
  1522. }
  1523. }
  1524. else
  1525. {
  1526. var->Serialize(node, load);
  1527. }
  1528. }
  1529. }
  1530. else
  1531. {
  1532. // Saving.
  1533. for (Vars::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
  1534. {
  1535. IVariable* var = *it;
  1536. if (var->GetNumVariables())
  1537. {
  1538. XmlNodeRef child = node->newChild(var->GetName().toUtf8().data());
  1539. var->Serialize(child, load);
  1540. }
  1541. else
  1542. {
  1543. var->Serialize(node, load);
  1544. }
  1545. }
  1546. }
  1547. }
  1548. protected:
  1549. typedef std::vector<IVariablePtr> Vars;
  1550. Vars m_vars;
  1551. };
  1552. //////////////////////////////////////////////////////////////////////////
  1553. //////////////////////////////////////////////////////////////////////////
  1554. //! Selection list shown in combo box, for enumerated variable.
  1555. struct IVarEnumList
  1556. : public CRefCountBase
  1557. {
  1558. //! Get the name of specified value in enumeration, or empty string if out of range.
  1559. virtual QString GetItemName(uint index) = 0;
  1560. };
  1561. typedef _smart_ptr<IVarEnumList> IVarEnumListPtr;
  1562. //////////////////////////////////////////////////////////////////////////
  1563. //////////////////////////////////////////////////////////////////////////
  1564. AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING;
  1565. //! Selection list shown in combo box, for enumerated variable.
  1566. template <class T>
  1567. class CVarEnumListBase
  1568. : public IVarEnumList
  1569. {
  1570. AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  1571. public:
  1572. CVarEnumListBase(){}
  1573. //////////////////////////////////////////////////////////////////////////
  1574. virtual T NameToValue(const QString& name) = 0;
  1575. //////////////////////////////////////////////////////////////////////////
  1576. virtual QString ValueToName(T const& value) = 0;
  1577. //! Add new item to the selection.
  1578. virtual void AddItem(const QString& name, const T& value) = 0;
  1579. template <class TVal>
  1580. static bool IsValueEqual(const TVal& v1, const TVal& v2)
  1581. {
  1582. return v1 == v2;
  1583. }
  1584. static bool IsValueEqual(const QString& v1, const QString& v2)
  1585. {
  1586. // Case insensitive compare.
  1587. return QString::compare(v1, v2, Qt::CaseInsensitive) == 0;
  1588. }
  1589. protected:
  1590. virtual ~CVarEnumListBase() {};
  1591. friend class _smart_ptr<CVarEnumListBase<T> >;
  1592. };
  1593. //////////////////////////////////////////////////////////////////////////
  1594. //////////////////////////////////////////////////////////////////////////
  1595. //! Selection list shown in combo box, for enumerated variable.
  1596. template <class T>
  1597. class CVarEnumList
  1598. : public CVarEnumListBase<T>
  1599. {
  1600. public:
  1601. using CVarEnumListBase<T>::IsValueEqual;
  1602. CVarEnumList(){}
  1603. struct Item
  1604. {
  1605. QString name;
  1606. T value;
  1607. };
  1608. QString GetItemName(uint index)
  1609. {
  1610. if (index >= m_items.size())
  1611. {
  1612. return QString();
  1613. }
  1614. return m_items[index].name;
  1615. };
  1616. //////////////////////////////////////////////////////////////////////////
  1617. T NameToValue(const QString& name)
  1618. {
  1619. for (int i = 0; i < m_items.size(); i++)
  1620. {
  1621. if (name == m_items[i].name)
  1622. {
  1623. return m_items[i].value;
  1624. }
  1625. }
  1626. return T();
  1627. }
  1628. //////////////////////////////////////////////////////////////////////////
  1629. QString ValueToName(T const& value)
  1630. {
  1631. for (int i = 0; i < m_items.size(); i++)
  1632. {
  1633. if (CVarEnumListBase<T>::IsValueEqual(value, m_items[i].value))
  1634. {
  1635. return m_items[i].name;
  1636. }
  1637. }
  1638. return "";
  1639. }
  1640. //! Add new item to the selection.
  1641. void AddItem(const QString& name, const T& value)
  1642. {
  1643. Item item;
  1644. item.name = name;
  1645. item.value = value;
  1646. m_items.push_back(item);
  1647. };
  1648. protected:
  1649. ~CVarEnumList() {};
  1650. private:
  1651. std::vector<Item> m_items;
  1652. };
  1653. //////////////////////////////////////////////////////////////////////////////////
  1654. // CVariableEnum is the same as CVariable but it display enumerated values in UI
  1655. //////////////////////////////////////////////////////////////////////////////////
  1656. template <class T>
  1657. class CVariableEnum
  1658. : public CVariable<T>
  1659. {
  1660. public:
  1661. using CVariable<T>::SetValue;
  1662. using CVariable<T>::GetFlags;
  1663. using CVariable<T>::OnSetValue;
  1664. using CVariable<T>::Set;
  1665. using CVariable<T>::m_valueDef;
  1666. using CVariable<T>::m_onSetEnumFuncs;
  1667. //////////////////////////////////////////////////////////////////////////
  1668. CVariableEnum(){}
  1669. //! Assign operator for variable.
  1670. void operator=(const T& value) { CVariable<T>::SetValue(value); }
  1671. //! Add new item to the enumeration.
  1672. void AddEnumItem(const char* name, const T& value)
  1673. {
  1674. AddEnumItem(QString(name), value);
  1675. };
  1676. //! Add new item to the enumeration.
  1677. void AddEnumItem(const QString& name, const T& value)
  1678. {
  1679. if (GetFlags() & IVariable::UI_USE_GLOBAL_ENUMS) // don't think adding makes sense
  1680. {
  1681. return;
  1682. }
  1683. if (!m_enum)
  1684. {
  1685. m_enum = new CVarEnumList<T>();
  1686. }
  1687. m_enum->AddItem(name, value);
  1688. OnEnumsChanged();
  1689. };
  1690. void SetEnumList(CVarEnumListBase<T>* enumList)
  1691. {
  1692. m_enum = enumList;
  1693. OnSetValue(false);
  1694. OnEnumsChanged();
  1695. }
  1696. IVarEnumList* GetEnumList() const
  1697. {
  1698. return m_enum;
  1699. }
  1700. //////////////////////////////////////////////////////////////////////////
  1701. IVariable* Clone([[maybe_unused]] bool bRecursive) const
  1702. {
  1703. CVariableEnum<T>* var = new CVariableEnum<T>(*this);
  1704. return var;
  1705. }
  1706. virtual QString GetDisplayValue() const
  1707. {
  1708. if (m_enum)
  1709. {
  1710. return m_enum->ValueToName(m_valueDef);
  1711. }
  1712. else
  1713. {
  1714. return CVariable<T>::GetDisplayValue();
  1715. }
  1716. }
  1717. virtual void SetDisplayValue(const QString& value)
  1718. {
  1719. if (m_enum)
  1720. {
  1721. Set(m_enum->NameToValue(value));
  1722. }
  1723. else
  1724. {
  1725. Set(value);
  1726. }
  1727. }
  1728. protected:
  1729. void OnEnumsChanged()
  1730. {
  1731. // Call on set callback.
  1732. for (auto it = m_onSetEnumFuncs.begin(); it != m_onSetEnumFuncs.end(); ++it)
  1733. {
  1734. // Call on set callback.
  1735. (*it)->operator()(this);
  1736. }
  1737. }
  1738. // Copy Constructor.
  1739. CVariableEnum(const CVariableEnum<T>& var)
  1740. : CVariable<T>(var)
  1741. {
  1742. m_enum = var.m_enum;
  1743. }
  1744. private:
  1745. _smart_ptr<CVarEnumListBase<T> > m_enum;
  1746. };
  1747. //////////////////////////////////////////////////////////////////////////
  1748. // Smart pointers to variables.
  1749. //////////////////////////////////////////////////////////////////////////
  1750. //////////////////////////////////////////////////////////////////////////
  1751. template <class T, class TVar>
  1752. struct CSmartVariableBase
  1753. {
  1754. typedef TVar VarType;
  1755. CSmartVariableBase() { pVar = new VarType(); }
  1756. operator T const& () const {
  1757. VarType* pV = pVar;
  1758. return *pV;
  1759. }
  1760. //void operator=( const T& value ) { VarType *pV = pVar; *pV = value; }
  1761. // Compare with pointer to variable.
  1762. bool operator==(IVariable* pV) { return pVar == pV; }
  1763. bool operator!=(IVariable* pV) { return pVar != pV; }
  1764. operator VarType&() {
  1765. VarType* pV = pVar;
  1766. return *pV;
  1767. } // Cast to CVariableBase&
  1768. VarType& operator*() const { return *pVar; }
  1769. VarType* operator->() const { return pVar; }
  1770. VarType* GetVar() const { return pVar; };
  1771. protected:
  1772. _smart_ptr<VarType> pVar;
  1773. };
  1774. //////////////////////////////////////////////////////////////////////////
  1775. template <class T>
  1776. struct CSmartVariable
  1777. : public CSmartVariableBase<T, CVariable<T> >
  1778. {
  1779. using CSmartVariableBase<T, CVariable<T> >::pVar;
  1780. void operator=(const T& value) { CVariable<T>* pV = pVar; *pV = value; }
  1781. };
  1782. //////////////////////////////////////////////////////////////////////////
  1783. template <class T>
  1784. struct CSmartVariableArrayT
  1785. : public CSmartVariableBase<T, TVariableArray<T> >
  1786. {
  1787. using CSmartVariableBase<T, TVariableArray<T> >::pVar;
  1788. void operator=(const T& value) { TVariableArray<T>* pV = pVar; *pV = value; }
  1789. };
  1790. //////////////////////////////////////////////////////////////////////////
  1791. template <class T>
  1792. struct CSmartVariableEnum
  1793. : public CSmartVariableBase<T, CVariableEnum<T> >
  1794. {
  1795. using CSmartVariableBase<T, CVariableEnum<T> >::pVar;
  1796. CSmartVariableEnum(){}
  1797. void operator=(const T& value) { CVariableEnum<T>* pV = pVar; *pV = value; }
  1798. void AddEnumItem(const QString& name, const T& value)
  1799. {
  1800. pVar->AddEnumItem(name, value);
  1801. };
  1802. void SetEnumList(CVarEnumListBase<T>* enumList)
  1803. {
  1804. pVar->EnableUpdateCallbacks(false);
  1805. pVar->SetEnumList(enumList);
  1806. pVar->EnableUpdateCallbacks(true);
  1807. }
  1808. };
  1809. //////////////////////////////////////////////////////////////////////////
  1810. struct CSmartVariableArray
  1811. {
  1812. typedef CVariableArray VarType;
  1813. CSmartVariableArray() { pVar = new VarType(); }
  1814. //////////////////////////////////////////////////////////////////////////
  1815. // Access operators.
  1816. //////////////////////////////////////////////////////////////////////////
  1817. operator VarType&() {
  1818. VarType* pV = pVar;
  1819. return *pV;
  1820. }
  1821. VarType& operator*() const { return *pVar; }
  1822. VarType* operator->() const { return pVar; }
  1823. VarType* GetVar() const { return pVar; };
  1824. private:
  1825. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  1826. _smart_ptr<VarType> pVar;
  1827. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  1828. };
  1829. //////////////////////////////////////////////////////////////////////////
  1830. //////////////////////////////////////////////////////////////////////////
  1831. AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  1832. class EDITOR_CORE_API CVarBlock
  1833. : public IVariableContainer
  1834. {
  1835. AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  1836. public:
  1837. // Dtor.
  1838. virtual ~CVarBlock() {}
  1839. //! Add variable to block.
  1840. void AddVariable(IVariable* var) override;
  1841. //! Remove variable from block
  1842. bool DeleteVariable(IVariable* var, bool bRecursive = false) override;
  1843. void AddVariable(IVariable* pVar, const char* varName, unsigned char dataType = IVariable::DT_SIMPLE);
  1844. // This used from smart variable pointer.
  1845. void AddVariable(CVariableBase& var, const char* varName, unsigned char dataType = IVariable::DT_SIMPLE);
  1846. //! Returns number of variables in block.
  1847. int GetNumVariables() const override { return static_cast<int>(m_vars.size()); }
  1848. //! Get pointer to stored variable by index.
  1849. IVariable* GetVariable(int index) const override
  1850. {
  1851. assert(index >= 0 && index < m_vars.size());
  1852. return m_vars[index];
  1853. }
  1854. // Clear all vars from VarBlock.
  1855. void DeleteAllVariables() override { m_vars.clear(); };
  1856. //! Return true if variable block is empty (Does not have any vars).
  1857. bool IsEmpty() const override { return m_vars.empty(); }
  1858. // Returns true if var block contains specified variable.
  1859. bool IsContainsVariable(IVariable* pVar, bool bRecursive = true) const override;
  1860. //! Find variable by name.
  1861. IVariable* FindVariable(const char* name, bool bRecursive = true, bool bHumanName = false) const override;
  1862. //////////////////////////////////////////////////////////////////////////
  1863. //! Clone var block.
  1864. CVarBlock* Clone(bool bRecursive) const;
  1865. //////////////////////////////////////////////////////////////////////////
  1866. //! Copy variable values from specified var block.
  1867. //! Do not create new variables, only copy values of existing ones.
  1868. //! Should only be used to copy identical var blocks (e.g. not Array type var copied to String type var)
  1869. //! @param fromVarBlock Source variable block that contain copied values, must be identical to this var block.
  1870. void CopyValues(const CVarBlock* fromVarBlock);
  1871. //////////////////////////////////////////////////////////////////////////
  1872. //! Copy variable values from specified var block.
  1873. //! Do not create new variables, only copy values of existing ones.
  1874. //! Can be used to copy slightly different var blocks, matching performed by variable name.
  1875. //! @param fromVarBlock Source variable block that contain copied values.
  1876. void CopyValuesByName(CVarBlock* fromVarBlock);
  1877. void OnSetValues();
  1878. //////////////////////////////////////////////////////////////////////////
  1879. //! Set UI_CREATE_SPLINE flags for all the variables in this Var Block.
  1880. //! This is because when the data was changed for a spline variable, the spline need to be recreated to reflect the change.
  1881. //! Set the flag to notify the spline variable.
  1882. void SetRecreateSplines();
  1883. //////////////////////////////////////////////////////////////////////////
  1884. // Wire/Unwire other variable blocks.
  1885. //////////////////////////////////////////////////////////////////////////
  1886. //! Wire to other variable block.
  1887. //! Only equivalent VarBlocks can be wired (same number of variables with same type).
  1888. //! Recursive wiring of array variables is supported.
  1889. void Wire(CVarBlock* toVarBlock);
  1890. //! Unwire var block.
  1891. void Unwire(CVarBlock* varBlock);
  1892. //! Add this callback to every variable in block (recursively).
  1893. void AddOnSetCallback(IVariable::OnSetCallback* func);
  1894. //! Remove this callback from every variable in block (recursively).
  1895. void RemoveOnSetCallback(IVariable::OnSetCallback* func);
  1896. //////////////////////////////////////////////////////////////////////////
  1897. void Serialize(XmlNodeRef vbNode, bool load);
  1898. void ReserveNumVariables(int numVars);
  1899. //////////////////////////////////////////////////////////////////////////
  1900. //! Gather resources in this variable block.
  1901. virtual void GatherUsedResources(CUsedResources& resources);
  1902. void EnableUpdateCallbacks(bool boEnable);
  1903. IVariable* FindChildVar(const char* name, IVariable* pParentVar) const;
  1904. void Sort();
  1905. protected:
  1906. void SetCallbackToVar(IVariable::OnSetCallback* func, IVariable* pVar, bool bAdd);
  1907. void WireVar(IVariable* src, IVariable* trg, bool bWire);
  1908. void GatherUsedResourcesInVar(IVariable* pVar, CUsedResources& resources);
  1909. typedef std::vector<IVariablePtr> Variables;
  1910. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  1911. Variables m_vars;
  1912. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  1913. };
  1914. typedef _smart_ptr<CVarBlock> CVarBlockPtr;
  1915. //////////////////////////////////////////////////////////////////////////
  1916. class EDITOR_CORE_API CVarObject
  1917. : public QObject
  1918. , public CRefCountBase
  1919. {
  1920. public:
  1921. typedef IVariable::OnSetCallback VarOnSetCallback;
  1922. CVarObject();
  1923. ~CVarObject();
  1924. void Serialize(XmlNodeRef node, bool load);
  1925. CVarBlock* GetVarBlock() const { return m_vars; };
  1926. void AddVariable(CVariableBase& var, const QString& varName, VarOnSetCallback* cb = nullptr, unsigned char dataType = IVariable::DT_SIMPLE);
  1927. void AddVariable(CVariableBase& var, const QString& varName, const QString& varHumanName, VarOnSetCallback* cb = nullptr, unsigned char dataType = IVariable::DT_SIMPLE);
  1928. void AddVariable(CVariableArray& table, CVariableBase& var, const QString& varName, const QString& varHumanName, VarOnSetCallback* cb = nullptr, unsigned char dataType = IVariable::DT_SIMPLE);
  1929. void ReserveNumVariables(int numVars);
  1930. void RemoveVariable(IVariable* var);
  1931. void EnableUpdateCallbacks(bool boEnable);
  1932. void OnSetValues();
  1933. protected:
  1934. //! Copy values of variables from other VarObject.
  1935. //! Source object must be of same type.
  1936. void CopyVariableValues(CVarObject* sourceObject);
  1937. private:
  1938. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  1939. CVarBlockPtr m_vars;
  1940. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  1941. };
  1942. Q_DECLARE_METATYPE(IVariable *);
  1943. #endif // CRYINCLUDE_EDITOR_UTIL_VARIABLE_H