1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #ifndef CRYINCLUDE_EDITOR_UTIL_VARIABLE_H
- #define CRYINCLUDE_EDITOR_UTIL_VARIABLE_H
- #pragma once
- #include "RefCountBase.h"
- #include "Include/EditorCoreAPI.h"
- #include <QObject>
- AZ_PUSH_DISABLE_WARNING(4458, "-Wunknown-warning-option")
- #include <QtUtilWin.h>
- AZ_POP_DISABLE_WARNING
- #include <QVariant>
- #include <StlUtils.h>
- #include <AzCore/std/string/string.h>
- #include <AzCore/Math/Color.h>
- #include <AzCore/Math/Vector2.h>
- #include <AzCore/Math/Vector3.h>
- #include <AzCore/Math/Vector4.h>
- #include <AzCore/Math/Quaternion.h>
- inline const char* to_c_str(const char* str) { return str; }
- #define MAX_VAR_STRING_LENGTH 4096
- struct IVarEnumList;
- struct ISplineInterpolator;
- class CUsedResources;
- struct IVariable;
- namespace
- {
- template<typename T>
- T nextNiceNumberBelow(T number);
- template<typename T>
- T nextNiceNumberAbove(T number)
- {
- if (number == 0)
- {
- return 0;
- }
- if (number < 0)
- {
- return -nextNiceNumberBelow(-number);
- }
- // number = 8000
- auto l = log10(number); // 3.90
- AZ::s64 i = aznumeric_cast<AZ::s64>(l); // 3
- int f = aznumeric_cast<int>(pow(10, l - i)); // 10^0.9 = 8
- f = f < 2 ? 2 : f < 5 ? 5 : 10; // f -> 10
- return aznumeric_cast<T>(pow(10, i) * f);
- }
- template<typename T>
- T nextNiceNumberBelow(T number)
- {
- if (number == 0)
- {
- return 0;
- }
- if (number < 0)
- {
- return -nextNiceNumberAbove(-number);
- }
- // number = 8000
- auto l = log10(number); // 3.90
- AZ::s64 i = aznumeric_cast<AZ::s64>(l); // 3
- int f = aznumeric_cast<int>(pow(10, l - i)); // 10^0.9 = 8
- f = f > 5 ? 5 : f > 2 ? 2 : 1; // f -> 5
- return aznumeric_cast<T>(pow(10, i) * f);
- }
- }
- /** IVariableContainer
- * Interface for all classes that hold child variables
- */
- struct IVariableContainer
- : public CRefCountBase
- {
- //! Add child variable
- virtual void AddVariable(IVariable* var) = 0;
- //! Delete specific variable
- virtual bool DeleteVariable(IVariable* var, bool recursive = false) = 0;
- //! Delete all variables
- virtual void DeleteAllVariables() = 0;
- //! Gets number of variables
- virtual int GetNumVariables() const = 0;
- //! Get variable at index
- virtual IVariable* GetVariable(int index) const = 0;
- //! Returns true if var block contains specified variable.
- virtual bool IsContainsVariable(IVariable* pVar, bool bRecursive = false) const = 0;
- //! Find variable by name.
- virtual IVariable* FindVariable(const char* name, bool bRecursive = false, bool bHumanName = false) const = 0;
- //! Return true if variable block is empty (Does not have any vars).
- virtual bool IsEmpty() const = 0;
- };
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- /** IVariable is the variant variable interface.
- */
- struct IVariable
- : public IVariableContainer
- {
- /** Type of data stored in variable.
- */
- enum EType
- {
- UNKNOWN, //!< Unknown parameter type.
- INT, //!< Integer property.
- BOOL, //!< Boolean property.
- FLOAT, //!< Float property.
- VECTOR2, //!< Vector property.
- VECTOR, //!< Vector property.
- VECTOR4, //!< Vector property.
- QUAT, //!< Quaternion property.
- STRING, //!< String property.
- ARRAY, //!< Array of parameters.
- FLOW_CUSTOM_DATA, //!< FLow graph custom data
- DOUBLE //!< Double property.
- };
- //! Type of data hold by variable.
- enum EDataType
- {
- DT_SIMPLE = 0, //!< Standard param type (float).
- DT_BOOLEAN,
- DT_PERCENT, //!< Percent data type, (Same as simple but value is from 0-1 and UI will be from 0-100).
- DT_COLOR, //!< RGB Color data type (Vector3).
- DT_ANGLE,
- DT_TEXTURE,
- DT_SHADER,
- DT_LOCAL_STRING,
- DT_EQUIP,
- DT_MATERIALLOOKUP,
- DT_EXTARRAY, // Extendable Array
- DT_SEQUENCE, // Movie Sequence (DEPRECATED, use DT_SEQUENCE_ID, instead.)
- DT_MISSIONOBJ, // Mission Objective
- DT_USERITEMCB, // Use a callback GetItemsCallback in user data of variable
- DT_UIENUM, // DEPRECATED
- DT_SEQUENCE_ID, // Movie Sequence
- DT_LIGHT_ANIMATION, // Light Animation Node in the global Light Animation Set
- DT_PARTICLE_EFFECT,
- DT_DEPRECATED, // formerly DT_FLARE
- DT_AUDIO_TRIGGER,
- DT_AUDIO_SWITCH,
- DT_AUDIO_SWITCH_STATE,
- DT_AUDIO_RTPC,
- DT_AUDIO_ENVIRONMENT,
- DT_AUDIO_PRELOAD_REQUEST,
- DT_UI_ELEMENT,
- DT_COLORA, //!< RGBA Color data type (Vector4).
- DT_MOTION, // Motion animation asset
- DT_CURVE = BIT(7), // Combined with other types
- };
- // Flags that can used with variables.
- enum EFlags
- {
- // User interface related flags.
- UI_DISABLED = BIT(0), //!< This variable will be disabled in UI.
- UI_BOLD = BIT(1), //!< Variable name in properties will be bold.
- UI_SHOW_CHILDREN = BIT(2), //!< Display children in parent field.
- UI_USE_GLOBAL_ENUMS = BIT(3), //!< Use CUIEnumsDatabase to fetch enums for this variable name.
- UI_INVISIBLE = BIT(4), //!< This variable will not be displayed in the UI
- UI_ROLLUP2 = BIT(5), //!< Prefer right roll-up bar for extended property control.
- UI_COLLAPSED = BIT(6), //!< Category collapsed by default.
- UI_UNSORTED = BIT(7), //!< Do not sort list-box alphabetically.
- UI_EXPLICIT_STEP = BIT(8), //!< Use the step size set in the variable for the UI directly.
- UI_NOT_DISPLAY_VALUE = BIT(9), //!< The UI will display an empty string where it would normally draw the variable value.
- UI_HIGHLIGHT_EDITED = BIT(10), //!< Edited (non-default value) properties show highlight color.
- UI_AUTO_EXPAND = BIT(11), //!< Category expanded by default. to auto-expand child in ReflectedPropertyCtrl.
- 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
- };
- typedef AZStd::function<void(IVariable*)> OnSetCallback;
- using OnSetEnumCallback = OnSetCallback;
- // Store IGetCustomItems into IVariable's UserData and set data-type to
- // DT_USERITEMCB
- // RefCounting is NOT handled by IVariable!
- struct IGetCustomItems
- : public CRefCountBase
- {
- struct SItem
- {
- SItem() {}
- SItem(const QString& name, const QString& desc = QString())
- : name(name)
- , desc(desc) {}
- SItem(const char* name, const char* desc = "")
- : name(name)
- , desc(desc) {}
- QString name;
- QString desc;
- };
- virtual bool GetItems (IVariable* /* pVar */, std::vector<SItem>& /* items */, QString& /* outDialogTitle */) = 0;
- virtual bool UseTree() = 0;
- virtual const char* GetTreeSeparator() = 0;
- };
- //! Get name of parameter.
- virtual QString GetName() const = 0;
- //! Set name of parameter.
- virtual void SetName(const QString& name) = 0;
- //! Get human readable name of parameter (Normally same as name).
- virtual QString GetHumanName() const = 0;
- //! Set human readable name of parameter (name without prefix).
- virtual void SetHumanName(const QString& name) = 0;
- //! Get variable description.
- virtual QString GetDescription() const = 0;
- //! Set variable description.
- virtual void SetDescription(const char* desc) = 0;
- //! Set variable description.
- virtual void SetDescription(const QString& desc) = 0;
- //! Get parameter type.
- virtual EType GetType() const = 0;
- //! Get size of parameter.
- virtual int GetSize() const = 0;
- //! Type of data stored in this variable.
- virtual unsigned char GetDataType() const = 0;
- virtual void SetDataType(unsigned char dataType) = 0;
- virtual void SetUserData(const QVariant &data) = 0;
- virtual QVariant GetUserData() const = 0;
- //////////////////////////////////////////////////////////////////////////
- // Flags
- //////////////////////////////////////////////////////////////////////////
- //! Set variable flags, (Limited to 16 flags).
- virtual void SetFlags(int flags) = 0;
- virtual int GetFlags() const = 0;
- virtual void SetFlagRecursive(EFlags flag) = 0;
- /////////////////////////////////////////////////////////////////////////////
- // Set methods.
- /////////////////////////////////////////////////////////////////////////////
- virtual void Set(int value) = 0;
- virtual void Set(bool value) = 0;
- virtual void Set(float value) = 0;
- virtual void Set(double value) = 0;
- virtual void Set(const Vec2& value) = 0;
- virtual void Set(const Vec3& value) = 0;
- virtual void Set(const Vec4& value) = 0;
- virtual void Set(const Ang3& value) = 0;
- virtual void Set(const Quat& value) = 0;
- virtual void Set(const QString& value) = 0;
- virtual void Set(const char* value) = 0;
- virtual void SetDisplayValue(const QString& value) = 0;
- virtual void Set(const AZ::Color& value) = 0;
- virtual void Set(const AZ::Vector2& value) = 0;
- virtual void Set(const AZ::Vector3& value) = 0;
- virtual void Set(const AZ::Vector4& value) = 0;
- virtual void Set(const AZ::Quaternion& value) = 0;
- virtual void Set(const AZStd::string& value) = 0;
- // Called when value updated by any means (including internally).
- virtual void OnSetValue(bool bRecursive) = 0;
- /////////////////////////////////////////////////////////////////////////////
- // Get methods.
- /////////////////////////////////////////////////////////////////////////////
- virtual void Get(int& value) const = 0;
- virtual void Get(bool& value) const = 0;
- virtual void Get(float& value) const = 0;
- virtual void Get(double& value) const = 0;
- virtual void Get(Vec2& value) const = 0;
- virtual void Get(Vec3& value) const = 0;
- virtual void Get(Vec4& value) const = 0;
- virtual void Get(Ang3& value) const = 0;
- virtual void Get(Quat& value) const = 0;
- virtual void Get(QString& value) const = 0;
- virtual QString GetDisplayValue() const = 0;
- virtual bool HasDefaultValue() const = 0;
- virtual void Get(AZ::Color& value) const = 0;
- virtual void Get(AZ::Vector2& value) const = 0;
- virtual void Get(AZ::Vector3& value) const = 0;
- virtual void Get(AZ::Vector4& value) const = 0;
- virtual void Get(AZ::Quaternion& value) const = 0;
- virtual void Get(AZStd::string& value) const = 0;
- //! reset value to default value
- virtual void ResetToDefault() = 0;
- //! Return cloned value of variable.
- virtual IVariable* Clone(bool bRecursive) const = 0;
- //! Copy variable value from specified variable.
- //! This method executed always recursively on all sub hierarchy of variables,
- //! In Array vars, will never create new variables, only copy values of corresponding children.
- //! @param fromVar Source variable to copy value from.
- virtual void CopyValue(IVariable* fromVar) = 0;
- //////////////////////////////////////////////////////////////////////////
- // Value Limits.
- //////////////////////////////////////////////////////////////////////////
- //! Set value limits.
- 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) {}
- //! Get value limits.
- virtual void GetLimits([[maybe_unused]] float& fMin, [[maybe_unused]] float& fMax, [[maybe_unused]] float& fStep, [[maybe_unused]] bool& bHardMin, [[maybe_unused]] bool& bHardMax) {}
- void GetLimits(float& fMin, float& fMax)
- {
- float f;
- bool b;
- GetLimits(fMin, fMax, f, b, b);
- }
- virtual bool HasCustomLimits() { return false; }
- virtual void EnableNotifyWithoutValueChange([[maybe_unused]] bool bFlag){}
- //////////////////////////////////////////////////////////////////////////
- // Wire/Unwire variables.
- //////////////////////////////////////////////////////////////////////////
- //! Wire variable, wired variable will be changed when this var changes.
- virtual void Wire(IVariable* targetVar) = 0;
- //! Unwire variable.
- virtual void Unwire(IVariable* targetVar) = 0;
- //////////////////////////////////////////////////////////////////////////
- // Assign on set callback.
- //////////////////////////////////////////////////////////////////////////
- virtual void AddOnSetCallback(OnSetCallback* func) = 0;
- virtual void RemoveOnSetCallback(OnSetCallback* func) = 0;
- virtual void ClearOnSetCallbacks() {}
- //////////////////////////////////////////////////////////////////////////
- // Assign callback triggered when enums change.
- //////////////////////////////////////////////////////////////////////////
- virtual void AddOnSetEnumCallback(OnSetEnumCallback* func) = 0;
- virtual void RemoveOnSetEnumCallback(OnSetCallback* func) = 0;
- virtual void ClearOnSetEnumCallbacks() {}
- //////////////////////////////////////////////////////////////////////////
- //! Retrieve pointer to selection list used by variable.
- virtual IVarEnumList* GetEnumList() const { return 0; }
- virtual ISplineInterpolator* GetSpline() { return 0; }
- //////////////////////////////////////////////////////////////////////////
- //! Serialize variable to XML.
- //////////////////////////////////////////////////////////////////////////
- virtual void Serialize(XmlNodeRef node, bool load) = 0;
- // From CObject, (not implemented)
- virtual void Serialize([[maybe_unused]] CArchive& ar) {}
- //////////////////////////////////////////////////////////////////////////
- // Disables the update callbacks for certain operations in order to avoid
- // too many function calls when not needed.
- //////////////////////////////////////////////////////////////////////////
- virtual void EnableUpdateCallbacks(bool boEnable) = 0;
- // Setup to true to force save Undo in case the value the same.
- virtual void SetForceModified([[maybe_unused]] bool bForceModified) {}
- };
- // Smart pointer to this parameter.
- typedef _smart_ptr<IVariable> IVariablePtr;
- AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING
- /**
- **************************************************************************************
- * CVariableBase implements IVariable interface and provide default implementation
- * for basic IVariable functionality (Name, Flags, etc...)
- * CVariableBase cannot be instantiated directly and should be used as the base class for
- * actual Variant implementation classes.
- ***************************************************************************************
- */
- class EDITOR_CORE_API CVariableBase
- : public IVariable
- {
- AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
- public:
- virtual ~CVariableBase() {}
- void SetName(const QString& name) override { m_name = name; };
- //! Get name of parameter.
- QString GetName() const override { return m_name; };
- QString GetHumanName() const override
- {
- if (!m_humanName.isEmpty())
- {
- return m_humanName;
- }
- return m_name;
- }
- void SetHumanName(const QString& name) override { m_humanName = name; }
- void SetDescription(const char* desc) override { m_description = desc; };
- void SetDescription(const QString& desc) override { m_description = desc; };
- //! Get name of parameter.
- QString GetDescription() const override { return m_description; };
- EType GetType() const override { return IVariable::UNKNOWN; };
- int GetSize() const override { return sizeof(*this); };
- unsigned char GetDataType() const override { return m_dataType; };
- void SetDataType(unsigned char dataType) override { m_dataType = dataType; }
- void SetFlags(int flags) override { m_flags = static_cast<uint16>(flags); }
- int GetFlags() const override { return m_flags; }
- void SetFlagRecursive(EFlags flag) override { m_flags |= flag; }
- void SetUserData(const QVariant &data) override { m_userData = data; };
- QVariant GetUserData() const override { return m_userData; }
- //////////////////////////////////////////////////////////////////////////
- // Set methods.
- //////////////////////////////////////////////////////////////////////////
- void Set([[maybe_unused]] int value) override { assert(0); }
- void Set([[maybe_unused]] bool value) override { assert(0); }
- void Set([[maybe_unused]] float value) override { assert(0); }
- void Set([[maybe_unused]] double value) override { assert(0); }
- void Set([[maybe_unused]] const Vec2& value) override { assert(0); }
- void Set([[maybe_unused]] const Vec3& value) override { assert(0); }
- void Set([[maybe_unused]] const Vec4& value) override { assert(0); }
- void Set([[maybe_unused]] const Ang3& value) override { assert(0); }
- void Set([[maybe_unused]] const Quat& value) override { assert(0); }
- void Set([[maybe_unused]] const QString& value) override { assert(0); }
- void Set([[maybe_unused]] const char* value) override { assert(0); }
- void SetDisplayValue(const QString& value) override { Set(value); }
- void Set([[maybe_unused]] const AZ::Color& value) override { assert(0); }
- void Set([[maybe_unused]] const AZ::Vector2& value) override { assert(0); }
- void Set([[maybe_unused]] const AZ::Vector3& value) override { assert(0); }
- void Set([[maybe_unused]] const AZ::Vector4& value) override { assert(0); }
- void Set([[maybe_unused]] const AZ::Quaternion& value) override { assert(0); }
- void Set([[maybe_unused]] const AZStd::string& value) override { assert(0); }
- //////////////////////////////////////////////////////////////////////////
- // Get methods.
- //////////////////////////////////////////////////////////////////////////
- void Get([[maybe_unused]] int& value) const override { assert(0); }
- void Get([[maybe_unused]] bool& value) const override { assert(0); }
- void Get([[maybe_unused]] float& value) const override { assert(0); }
- void Get([[maybe_unused]] double& value) const override { assert(0); }
- void Get([[maybe_unused]] Vec2& value) const override { assert(0); }
- void Get([[maybe_unused]] Vec3& value) const override { assert(0); }
- void Get([[maybe_unused]] Vec4& value) const override { assert(0); }
- void Get([[maybe_unused]] Ang3& value) const override { assert(0); }
- void Get([[maybe_unused]] Quat& value) const override { assert(0); }
- void Get([[maybe_unused]] QString& value) const override { assert(0); }
- QString GetDisplayValue() const override { QString val; Get(val); return val; }
- void Get([[maybe_unused]] AZ::Color& value) const override { assert(0); }
- void Get([[maybe_unused]] AZ::Vector2& value) const override { assert(0); }
- void Get([[maybe_unused]] AZ::Vector3& value) const override { assert(0); }
- void Get([[maybe_unused]] AZ::Vector4& value) const override { assert(0); }
- void Get([[maybe_unused]] AZ::Quaternion& value) const override { assert(0); }
- void Get([[maybe_unused]] AZStd::string& value) const override { assert(0); }
- //////////////////////////////////////////////////////////////////////////
- // IVariableContainer functions
- //////////////////////////////////////////////////////////////////////////
- void AddVariable([[maybe_unused]] IVariable* var) override { assert(0); }
- bool DeleteVariable([[maybe_unused]] IVariable* var, [[maybe_unused]] bool recursive = false) override { return false; }
- void DeleteAllVariables() override {}
- int GetNumVariables() const override { return 0; }
- IVariable* GetVariable([[maybe_unused]] int index) const override { return nullptr; }
- bool IsContainsVariable([[maybe_unused]] IVariable* pVar, [[maybe_unused]] bool bRecursive = false) const override { return false; }
- IVariable* FindVariable([[maybe_unused]] const char* name, [[maybe_unused]] bool bRecursive = false, [[maybe_unused]] bool bHumanName = false) const override { return nullptr; }
- bool IsEmpty() const override { return true; }
- //////////////////////////////////////////////////////////////////////////
- void Wire(IVariable* var) override
- {
- m_wiredVars.push_back(var);
- }
- //////////////////////////////////////////////////////////////////////////
- void Unwire(IVariable* var) override
- {
- if (!var)
- {
- // Unwire all.
- m_wiredVars.clear();
- }
- else
- {
- stl::find_and_erase(m_wiredVars, var);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void AddOnSetCallback(OnSetCallback* func) override
- {
- if (!stl::find(m_onSetFuncs, func))
- {
- m_onSetFuncs.push_back(func);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void RemoveOnSetCallback(OnSetCallback* func) override
- {
- stl::find_and_erase(m_onSetFuncs, func);
- }
- //////////////////////////////////////////////////////////////////////////
- void ClearOnSetCallbacks() override
- {
- m_onSetFuncs.clear();
- }
- void AddOnSetEnumCallback(OnSetEnumCallback* func) override
- {
- if (!stl::find(m_onSetEnumFuncs, func))
- {
- m_onSetEnumFuncs.push_back(func);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void RemoveOnSetEnumCallback(OnSetCallback* func) override
- {
- stl::find_and_erase(m_onSetEnumFuncs, func);
- }
- void ClearOnSetEnumCallbacks() override
- {
- m_onSetEnumFuncs.clear();
- }
- void OnSetValue([[maybe_unused]] bool bRecursive) override
- {
- // If have wired variables or OnSet callback, process them.
- // Send value to wired variable.
- for (CVariableBase::WiredList::iterator it = m_wiredVars.begin(); it != m_wiredVars.end(); ++it)
- {
- if (m_bForceModified)
- {
- (*it)->SetForceModified(true);
- }
- // Copy value to wired vars.
- (*it)->CopyValue(this);
- }
- if (!m_boUpdateCallbacksEnabled)
- {
- return;
- }
- // Call on set callback.
- for (auto it = m_onSetFuncs.begin(); it != m_onSetFuncs.end(); ++it)
- {
- // Call on set callback.
- (*it)->operator()(this);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- using IVariable::Serialize;
- void Serialize(XmlNodeRef node, bool load) override
- {
- if (load)
- {
- if (node->haveAttr(m_name.toUtf8().data()))
- {
- Set(node->getAttr(m_name.toUtf8().data()));
- }
- }
- else
- {
- // Saving.
- QString str;
- Get(str);
- node->setAttr(m_name.toUtf8().data(), str.toUtf8().data());
- }
- }
- void EnableUpdateCallbacks(bool boEnable) override{m_boUpdateCallbacksEnabled = boEnable; };
- void SetForceModified(bool bForceModified) override { m_bForceModified = bForceModified; }
- protected:
- // Constructor.
- CVariableBase()
- : m_dataType(DT_SIMPLE)
- , m_flags(0)
- , m_boUpdateCallbacksEnabled(true)
- , m_bForceModified(false)
- {
- }
- // Copy constructor.
- CVariableBase(const CVariableBase& var)
- {
- m_name = var.m_name;
- m_humanName = var.m_humanName;
- m_description = var.m_description;
- m_flags = var.m_flags;
- m_dataType = var.m_dataType;
- m_userData = var.m_userData;
- m_boUpdateCallbacksEnabled = true;
- m_bForceModified = var.m_bForceModified;
- // Never copy callback function or wired variables they are private to specific variable,
- }
- // Not allow.
- CVariableBase& operator=([[maybe_unused]] const CVariableBase& var)
- {
- return *this;
- }
- //////////////////////////////////////////////////////////////////////////
- // Variables.
- //////////////////////////////////////////////////////////////////////////
- typedef std::vector<OnSetCallback*> OnSetCallbackList;
- typedef std::vector<IVariablePtr> WiredList;
- using OnSetEnumCallbackList = std::vector<OnSetEnumCallback*>;
- QString m_name;
- QString m_humanName;
- QString m_description;
- //! Optional user data pointer
- QVariant m_userData;
- AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
- //! Extended data (Extended data is never copied, it's always private to this variable).
- WiredList m_wiredVars;
- OnSetCallbackList m_onSetFuncs;
- OnSetEnumCallbackList m_onSetEnumFuncs;
- AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
- uint16 m_flags;
- //! Limited to 8 flags.
- unsigned char m_dataType;
- bool m_boUpdateCallbacksEnabled;
- bool m_bForceModified;
- };
- /**
- **************************************************************************************
- * CVariableArray implements variable of type array of IVariables.
- ***************************************************************************************
- */
- class EDITOR_CORE_API CVariableArray
- : public CVariableBase
- {
- public:
- CVariableArray(){}
- //! Get name of parameter.
- EType GetType() const override { return IVariable::ARRAY; };
- int GetSize() const override { return sizeof(CVariableArray); };
- //////////////////////////////////////////////////////////////////////////
- // Set methods.
- //////////////////////////////////////////////////////////////////////////
- void Set(const QString& value) override
- {
- if (m_strValue != value)
- {
- m_strValue = value;
- OnSetValue(false);
- }
- }
- void OnSetValue(bool bRecursive) override
- {
- CVariableBase::OnSetValue(bRecursive);
- if (bRecursive)
- {
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- (*it)->OnSetValue(true);
- }
- }
- }
- void SetFlagRecursive(EFlags flag) override
- {
- CVariableBase::SetFlagRecursive(flag);
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- (*it)->SetFlagRecursive(flag);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- // Get methods.
- //////////////////////////////////////////////////////////////////////////
- void Get(QString& value) const override { value = m_strValue; }
- void Get(AZStd::string& value) const override { value.assign(m_strValue.toLatin1().data()); }
- bool HasDefaultValue() const override
- {
- for (Variables::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- if (!(*it)->HasDefaultValue())
- {
- return false;
- }
- }
- return true;
- }
- void ResetToDefault() override
- {
- for (Variables::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- (*it)->ResetToDefault();
- }
- }
- //////////////////////////////////////////////////////////////////////////
- IVariable* Clone(bool bRecursive) const override
- {
- CVariableArray* var = new CVariableArray(*this);
- // m_vars was shallow-duplicated, clone elements.
- for (int i = 0; i < m_vars.size(); i++)
- {
- var->m_vars[i] = m_vars[i]->Clone(bRecursive);
- }
- return var;
- }
- //////////////////////////////////////////////////////////////////////////
- void CopyValue(IVariable* fromVar) override
- {
- assert(fromVar);
- if (fromVar->GetType() != IVariable::ARRAY)
- {
- return;
- }
- int numSrc = fromVar->GetNumVariables();
- int numTrg = static_cast<int>(m_vars.size());
- for (int i = 0; i < numSrc && i < numTrg; i++)
- {
- // Copy Every child variable.
- m_vars[i]->CopyValue(fromVar->GetVariable(i));
- }
- QString strValue;
- fromVar->Get(strValue);
- Set(strValue);
- }
- //////////////////////////////////////////////////////////////////////////
- int GetNumVariables() const override { return static_cast<int>(m_vars.size()); }
- IVariable* GetVariable(int index) const override
- {
- assert(index >= 0 && index < (int)m_vars.size());
- return m_vars[index];
- }
- void AddVariable(IVariable* var) override
- {
- m_vars.push_back(var);
- }
- bool DeleteVariable(IVariable* var, bool recursive /*=false*/) override
- {
- bool found = stl::find_and_erase(m_vars, var);
- if (!found && recursive)
- {
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- if ((*it)->DeleteVariable(var, recursive))
- {
- return true;
- }
- }
- }
- return found;
- }
- void DeleteAllVariables() override
- {
- m_vars.clear();
- }
- bool IsContainsVariable(IVariable* pVar, bool bRecursive) const override
- {
- for (Variables::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- if (*it == pVar)
- {
- return true;
- }
- }
- // If not found search children.
- if (bRecursive)
- {
- // Search all top level variables.
- for (Variables::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- if ((*it)->IsContainsVariable(pVar))
- {
- return true;
- }
- }
- }
- return false;
- }
- IVariable* FindVariable(const char* name, bool bRecursive, bool bHumanName) const override;
- bool IsEmpty() const override
- {
- return m_vars.empty();
- }
- using IVariable::Serialize;
- void Serialize(XmlNodeRef node, bool load) override
- {
- if (load)
- {
- // Loading.
- QString name;
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- IVariable* var = *it;
- if (var->GetNumVariables())
- {
- XmlNodeRef child = node->findChild(var->GetName().toUtf8().data());
- if (child)
- {
- var->Serialize(child, load);
- }
- }
- else
- {
- var->Serialize(node, load);
- }
- }
- }
- else
- {
- // Saving.
- for (Variables::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- IVariable* var = *it;
- if (var->GetNumVariables())
- {
- XmlNodeRef child = node->newChild(var->GetName().toUtf8().data());
- var->Serialize(child, load);
- }
- else
- {
- var->Serialize(node, load);
- }
- }
- }
- }
- protected:
- typedef std::vector<IVariablePtr> Variables;
- AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
- Variables m_vars;
- AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
- //! Any string value displayed in properties.
- QString m_strValue;
- };
- /** var_type namespace includes type definitions needed for CVariable implementation.
- */
- namespace var_type
- {
- //////////////////////////////////////////////////////////////////////////
- template <int TypeID, bool IsStandart, bool IsInteger, bool IsSigned, bool SupportsRange>
- struct type_traits_base
- {
- static int type() { return TypeID; };
- //! Return true if standard C++ type.
- static bool is_standart() { return IsStandart; };
- static bool is_integer() { return IsInteger; };
- static bool is_signed() { return IsSigned; };
- static bool supports_range() { return SupportsRange; };
- };
- template <class Type>
- struct type_traits
- : public type_traits_base<IVariable::UNKNOWN, false, false, false, false> {};
- // Types specialization.
- template<>
- struct type_traits<int>
- : public type_traits_base<IVariable::INT, true, true, true, true> {};
- template<>
- struct type_traits<bool>
- : public type_traits_base<IVariable::BOOL, true, true, false, true> {};
- template<>
- struct type_traits<float>
- : public type_traits_base<IVariable::FLOAT, true, false, false, true> {};
- template<>
- struct type_traits<double>
- : public type_traits_base<IVariable::DOUBLE, true, false, false, true>{};
- template<>
- struct type_traits<Vec2>
- : public type_traits_base<IVariable::VECTOR2, false, false, false, false> {};
- template<>
- struct type_traits<Vec3>
- : public type_traits_base<IVariable::VECTOR, false, false, false, false> {};
- template<>
- struct type_traits<Vec4>
- : public type_traits_base<IVariable::VECTOR4, false, false, false, false> {};
- template<>
- struct type_traits<Quat>
- : public type_traits_base<IVariable::QUAT, false, false, false, false> {};
- template<>
- struct type_traits<AZStd::string>
- : public type_traits_base<IVariable::STRING, false, false, false, false> {};
- template<>
- struct type_traits<AZ::Vector2>
- : public type_traits_base<IVariable::VECTOR2, false, false, false, false> {};
- template<>
- struct type_traits<AZ::Vector3>
- : public type_traits_base<IVariable::VECTOR, false, false, false, false> {};
- template<>
- struct type_traits<AZ::Vector4>
- : public type_traits_base<IVariable::VECTOR4, false, false, false, false> {};
- template<>
- struct type_traits<AZ::Quaternion>
- : public type_traits_base<IVariable::QUAT, false, false, false, false> {};
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- // General one type to another type converter class.
- //////////////////////////////////////////////////////////////////////////
- struct type_converter
- {
- template <class From, class To>
- void operator()([[maybe_unused]] const From& from, [[maybe_unused]] To& to) const { assert(0); }
- void operator()(const int& from, int& to) const { to = from; }
- void operator()(const int& from, bool& to) const { to = from != 0; }
- void operator()(const int& from, float& to) const { to = (float)from; }
- //////////////////////////////////////////////////////////////////////////
- void operator()(const bool& from, int& to) const { to = from; }
- void operator()(const bool& from, bool& to) const { to = from; }
- void operator()(const bool& from, float& to) const { to = from; }
- //////////////////////////////////////////////////////////////////////////
- void operator()(const float& from, int& to) const { to = (int)from; }
- void operator()(const float& from, bool& to) const { to = from != 0; }
- void operator()(const float& from, float& to) const { to = from; }
- void operator()(const double& from, int& to) const { to = (int)from; }
- void operator()(const double& from, bool& to) const { to = from != 0; }
- void operator()(const double& from, float& to) const { to = aznumeric_cast<float>(from); }
- void operator()(const Vec2& from, Vec2& to) const { to = from; }
- void operator()(const Vec3& from, Vec3& to) const { to = from; }
- void operator()(const Vec4& from, Vec4& to) const { to = from; }
- void operator()(const Quat& from, Quat& to) const { to = from; }
- void operator()(const QString& from, QString& to) const { to = from; }
- void operator()(const AZ::Color& from, AZ::Color& to) const { to = from; }
- void operator()(const AZ::Vector2& from, AZ::Vector2& to) const { to = from; }
- void operator()(const AZ::Vector3& from, AZ::Vector3& to) const { to = from; }
- void operator()(const AZ::Vector4& from, AZ::Vector4& to) const { to = from; }
- void operator()(const AZ::Quaternion& from, AZ::Quaternion& to) const { to = from; }
- void operator()(const AZStd::string& from, AZStd::string& to) const { to = from; }
- void operator()(bool value, QString& to) const { to = QString::number((value) ? (int)1 : (int)0); }
- void operator()(int value, QString& to) const { to = QString::number(value); }
- void operator()(float value, QString& to) const { to = QString::number(value); };
- void operator()(double value, QString& to) const { to = QString::number(value); };
- void operator()(const Vec2& value, QString& to) const { to = QString::fromLatin1("%1,%2").arg(value.x).arg(value.y); }
- void operator()(const Vec3& value, QString& to) const { to = QString::fromLatin1("%1,%2,%3").arg(value.x).arg(value.y).arg(value.z); }
- 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); }
- void operator()(const Ang3& value, QString& to) const { to = QString::fromLatin1("%1,%2,%3").arg(value.x).arg(value.y).arg(value.z); }
- 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); }
- 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()); }
- void operator()(const AZ::Vector2& from, QString& to) const { to = QString::fromLatin1("%1,%2").arg(from.GetX()).arg(from.GetY()); }
- void operator()(const AZ::Vector3& from, QString& to) const { to = QString::fromLatin1("%1,%2,%3").arg(from.GetX()).arg(from.GetY()).arg(from.GetZ()); }
- 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()); }
- 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()); }
- void operator()(const AZStd::string& from, QString& to) const { to = QString::fromLatin1("%1").arg(from.c_str()); }
- void operator()(bool value, AZStd::string& to) const { to = value ? "1" : "0"; }
- void operator()(int value, AZStd::string& to) const { to = AZStd::to_string(value); }
- void operator()(float value, AZStd::string& to) const { to = AZStd::to_string(value); };
- void operator()(double value, AZStd::string& to) const { to = AZStd::to_string(value); };
- void operator()(const Vec2& value, AZStd::string& to) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- char buf[128] = { 0 };
- azsprintf(buf, "%f,%f", value.x, value.y);
- to.assign(buf);
- }
- void operator()(const Vec3& value, AZStd::string& to) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- char buf[192] = { 0 };
- azsprintf(buf, "%f,%f,%f", value.x, value.y, value.z);
- to.assign(buf);
- }
- void operator()(const Vec4& value, AZStd::string& to) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- char buf[256] = { 0 };
- azsprintf(buf, "%f,%f,%f,%f", value.x, value.y, value.z, value.w);
- to.assign(buf);
- }
- void operator()(const Ang3& value, AZStd::string& to) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- char buf[192] = { 0 };
- azsprintf(buf, "%f,%f,%f", value.x, value.y, value.z);
- to.assign(buf);
- }
- void operator()(const Quat& value, AZStd::string& to) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- char buf[256] = { 0 };
- azsprintf(buf, "%f,%f,%f,%f", value.w, value.v.x, value.v.y, value.v.z);
- to.assign(buf);
- }
- void operator()(const AZ::Color& from, AZStd::string& to) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- char buf[256] = { 0 };
- azsprintf(buf, "%f,%f,%f,%f", from.GetR(), from.GetG(), from.GetB(), from.GetA());
- to.assign(buf);
- }
- void operator()(const AZ::Vector2& from, AZStd::string& to) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- char buf[128] = { 0 };
- azsprintf(buf, "%f,%f", from.GetX(), from.GetY());
- to.assign(buf);
- }
- void operator()(const AZ::Vector3& from, AZStd::string& to) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- char buf[192] = { 0 };
- azsprintf(buf, "%f,%f,%f", from.GetX(), from.GetY(), from.GetZ());
- to.assign(buf);
- }
- void operator()(const AZ::Vector4& from, AZStd::string& to) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- char buf[256] = { 0 };
- azsprintf(buf, "%f,%f,%f,%f", from.GetX(), from.GetY(), from.GetZ(), from.GetZ());
- to.assign(buf);
- }
- void operator()(const AZ::Quaternion& from, AZStd::string& to) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- char buf[256] = { 0 };
- azsprintf(buf, "%f,%f,%f,%f", from.GetX(), from.GetY(), from.GetZ(), from.GetZ());
- to.assign(buf);
- }
- /////////////////////////////////////////// Legal to Current Math Types
- void operator()(const Vec2& from, AZ::Vector2& to) const { to.Set(from.x, from.y); }
- void operator()(const Vec3& from, AZ::Vector3& to) const { to.Set(from.x, from.y, from.z); }
- void operator()(const Vec4& from, AZ::Vector4& to) const { to.Set(from.x, from.y, from.z, from.w); }
- void operator()(const Ang3& from, AZ::Vector3& to) const { to.Set(from.x, from.y, from.z); }
- void operator()(const Quat& from, AZ::Quaternion& to) const { to.Set(from.v.x, from.v.y, from.v.z, from.w); }
- /////////////////////////////////////////// Current to Legal Math Types
- void operator()(const AZ::Vector2& from, Vec2& to) const { to.set(from.GetX(), from.GetY()); }
- void operator()(const AZ::Vector3& from, Vec3& to) const { to.Set(from.GetX(), from.GetY(), from.GetZ()); }
- void operator()(const AZ::Vector4& from, Vec4& to) const { to = Vec4(from.GetX(), from.GetY(), from.GetZ(), from.GetW()); }
- void operator()(const AZ::Vector3& from, Ang3& to) const { to.Set(from.GetX(), from.GetY(), from.GetZ()); }
- void operator()(const AZ::Quaternion& from, Quat& to) const { to = Quat(from.GetW(), from.GetX(), from.GetY(), from.GetZ()); }
- /////////////////////////////////////////// From QString
- void operator()(const QString& from, int& value) const { value = from.toInt(); }
- void operator()(const QString& from, bool& value) const { value = from.toInt() != 0; }
- void operator()(const QString& from, float& value) const { value = from.toFloat(); }
- void operator()(const QString& from, double& value) const { value = from.toDouble(); }
- void operator()(const QString& from, Vec2& value) const
- {
- QStringList parts = from.split(QStringLiteral(","));
- while (parts.size() < 2)
- {
- parts.push_back(QString());
- }
- value.x = parts[0].toFloat();
- value.y = parts[1].toFloat();
- };
- void operator()(const QString& from, Vec3& value) const
- {
- QStringList parts = from.split(QStringLiteral(","));
- while (parts.size() < 3)
- {
- parts.push_back(QString());
- }
- value.x = parts[0].toFloat();
- value.y = parts[1].toFloat();
- value.z = parts[2].toFloat();
- };
- void operator()(const QString& from, Vec4& value) const
- {
- QStringList parts = from.split(QStringLiteral(","));
- while (parts.size() < 4)
- {
- parts.push_back(QString());
- }
- value.x = parts[0].toFloat();
- value.y = parts[1].toFloat();
- value.z = parts[2].toFloat();
- value.w = parts[3].toFloat();
- };
- void operator()(const QString& from, Ang3& value) const
- {
- QStringList parts = from.split(QStringLiteral(","));
- while (parts.size() < 3)
- {
- parts.push_back(QString());
- }
- value.x = parts[0].toFloat();
- value.y = parts[1].toFloat();
- value.z = parts[2].toFloat();
- };
- void operator()(const QString& from, Quat& value) const
- {
- QStringList parts = from.split(QStringLiteral(","));
- while (parts.size() < 4)
- {
- parts.push_back(QString());
- }
- value.w = parts[0].toFloat();
- value.v.x = parts[1].toFloat();
- value.v.y = parts[2].toFloat();
- value.v.z = parts[3].toFloat();
- };
- void operator()(const QString& from, AZ::Color& value) const
- {
- QStringList parts = from.split(QStringLiteral(","));
- while (parts.size() < 4)
- {
- parts.push_back(QString());
- }
- for (int i = 0; i < 4; ++i)
- {
- value.SetElement(i, parts[i].toFloat());
- }
- }
- void operator()(const QString& from, AZ::Vector2& value) const
- {
- QStringList parts = from.split(QStringLiteral(","));
- while (parts.size() < 2)
- {
- parts.push_back(QString());
- }
- for (int i = 0; i < 2; ++i)
- {
- value.SetElement(i, parts[i].toFloat());
- }
- }
- void operator()(const QString& from, AZ::Vector3& value) const
- {
- QStringList parts = from.split(QStringLiteral(","));
- while (parts.size() < 3)
- {
- parts.push_back(QString());
- }
- for (int i = 0; i < 3; ++i)
- {
- value.SetElement(i, parts[i].toFloat());
- }
- }
- void operator()(const QString& from, AZ::Vector4& value) const
- {
- QStringList parts = from.split(QStringLiteral(","));
- while (parts.size() < 4)
- {
- parts.push_back(QString());
- }
- for (int i = 0; i < 4; ++i)
- {
- value.SetElement(i, parts[i].toFloat());
- }
- }
- void operator()(const QString& from, AZ::Quaternion& value) const
- {
- QStringList parts = from.split(QStringLiteral(","));
- while (parts.size() < 4)
- {
- parts.push_back(QString());
- }
- for (int i = 0; i < 4; ++i)
- {
- value.SetElement(i, parts[i].toFloat());
- }
- }
- void operator()(const QString& from, AZStd::string& value) const
- {
- value.assign(from.toUtf8().data());
- }
- /////////////////////////////////////////// From AZStd::string
- void operator()(const AZStd::string& from, int& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- azsscanf(from.c_str(), "%d", &value);
- }
- void operator()(const AZStd::string& from, bool& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- int val = 0;
- azsscanf(from.c_str(), "%d", &val);
- value = val != 0;
- }
- void operator()(const AZStd::string& from, float& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- azsscanf(from.c_str(), "%f", &value);
- }
- void operator()(const AZStd::string& from, double& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- azsscanf(from.c_str(), "%lf", &value);
- }
- void operator()(const AZStd::string& from, Vec2& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- azsscanf(from.c_str(), "%f,%f", &value.x, &value.y);
- }
- void operator()(const AZStd::string& from, Vec3& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- azsscanf(from.c_str(), "%f,%f,%f", &value.x, &value.y, &value.z);
- }
- void operator()(const AZStd::string& from, Vec4& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- azsscanf(from.c_str(), "%f,%f,%f,%f", &value.x, &value.y, &value.z, &value.w);
- }
- void operator()(const AZStd::string& from, Ang3& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- azsscanf(from.c_str(), "%f,%f,%f", &value.x, &value.y, &value.z);
- }
- void operator()(const AZStd::string& from, Quat& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- azsscanf(from.c_str(), "%f,%f,%f,%f", &value.w, &value.v.x, &value.v.y, &value.v.z);
- }
- void operator()(const AZStd::string& from, AZ::Color& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- float val[4];
- azsscanf(from.c_str(), "%f,%f,%f,%f", val, val + 1, val + 2, val + 3);
- value.Set(val);
- }
- void operator()(const AZStd::string& from, AZ::Vector2& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- float val[2];
- azsscanf(from.c_str(), "%f,%f,", val, val + 1);
- value = AZ::Vector2::CreateFromFloat2(val);
- }
- void operator()(const AZStd::string& from, AZ::Vector3& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- float val[3];
- azsscanf(from.c_str(), "%f,%f,%f", val, val + 1, val + 2);
- value.Set(val);
- }
- void operator()(const AZStd::string& from, AZ::Vector4& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- float val[4];
- azsscanf(from.c_str(), "%f,%f,%f,%f", val, val + 1, val + 2, val + 3);
- value.Set(val);
- };
- void operator()(const AZStd::string& from, AZ::Quaternion& value) const
- {
- AZ::Locale::ScopedSerializationLocale scopedLocale; // String should be interpreted in the "C" Locale.
- float val[4];
- azsscanf(from.c_str(), "%f,%f,%f,%f", val, val + 1, val + 2, val + 3);
- value.Set(val);
- };
- };
- //////////////////////////////////////////////////////////////////////////
- // Custom comparison functions for different variable type's values,.
- //////////////////////////////////////////////////////////////////////////
- template <class Type>
- inline bool compare(const Type& arg1, const Type& arg2)
- {
- return arg1 == arg2;
- };
-
- inline bool compare(const Vec2& v1, const Vec2& v2)
- {
- return v1.x == v2.x && v1.y == v2.y;
- }
- inline bool compare(const Vec3& v1, const Vec3& v2)
- {
- return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
- }
- inline bool compare(const Vec4& v1, const Vec4& v2)
- {
- return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z && v1.w == v2.w;
- }
- inline bool compare(const Ang3& v1, const Ang3& v2)
- {
- return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
- }
- inline bool compare(const Quat& q1, const Quat& q2)
- {
- return q1.v.x == q2.v.x && q1.v.y == q2.v.y && q1.v.z == q2.v.z && q1.w == q2.w;
- }
-
- inline bool compare(const char* s1, const char* s2)
- {
- return strcmp(s1, s2) == 0;
- }
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- // Custom Initialization functions for different variable type's values,.
- //////////////////////////////////////////////////////////////////////////
- template <class Type>
- inline void init(Type& val)
- {
- val = 0;
- }
- inline void init(Vec2& val) { val.x = 0; val.y = 0; };
- inline void init(Vec3& val) { val.x = 0; val.y = 0; val.z = 0; };
- inline void init(Vec4& val) { val.x = 0; val.y = 0; val.z = 0; val.w = 0; };
- inline void init(Ang3& val) { val.x = 0; val.y = 0; val.z = 0; };
- inline void init(Quat& val) { val.v.x = 0; val.v.y = 0; val.v.z = 0; val.w = 0; };
- inline void init(AZ::Color& val) { val = AZ::Color::CreateZero(); }
- inline void init(AZ::Vector2& val) { val = AZ::Vector2::CreateZero(); }
- inline void init(AZ::Vector3& val) { val = AZ::Vector3::CreateZero(); }
- inline void init(AZ::Vector4& val) { val = AZ::Vector4::CreateZero(); }
- inline void init(AZ::Quaternion& val) { val = AZ::Quaternion::CreateIdentity(); }
- inline void init(AZStd::string& val) { val = AZStd::string(); }
- inline void init(const char*& val) {
- val = "";
- }
- inline void init([[maybe_unused]] QString& val)
- {
- // self initializing.
- }
- //////////////////////////////////////////////////////////////////////////
- };
- //////////////////////////////////////////////////////////////////////////
- // Void variable does not contain any value.
- //////////////////////////////////////////////////////////////////////////
- class CVariableVoid
- : public CVariableBase
- {
- public:
- CVariableVoid(){};
- EType GetType() const override { return IVariable::UNKNOWN; };
- IVariable* Clone([[maybe_unused]] bool bRecursive) const override { return new CVariableVoid(*this); }
- void CopyValue([[maybe_unused]] IVariable* fromVar) override {};
- bool HasDefaultValue() const override { return true; }
- void ResetToDefault() override {};
- protected:
- CVariableVoid(const CVariableVoid& v)
- : CVariableBase(v) {};
- };
- //////////////////////////////////////////////////////////////////////////
- template <class T>
- class CVariable
- : public CVariableBase
- {
- typedef CVariable<T> Self;
- public:
- // Constructor.
- CVariable()
- : m_valueMin(0.0f)
- , m_valueMax(100.0f)
- , m_valueStep(0.0f)
- , m_bHardMin(false)
- , m_bHardMax(false)
- , m_customLimits(false)
- {
- // Initialize value to zero or empty string.
- var_type::init(m_valueDef);
- }
- explicit CVariable(const T& set)
- : CVariable()
- {
- var_type::init(m_valueDef); // Update F32NAN values in Debug mode
- SetValue(set);
- }
- //! Get name of parameter.
- EType GetType() const override { return (EType)var_type::type_traits<T>::type(); };
- int GetSize() const override { return sizeof(T); };
- //////////////////////////////////////////////////////////////////////////
- // Set methods.
- //////////////////////////////////////////////////////////////////////////
- void Set(int value) override { SetValue(value); }
- void Set(bool value) override { SetValue(value); }
- void Set(float value) override { SetValue(value); }
- void Set(double value) override { SetValue(value); }
- void Set(const Vec2& value) override { SetValue(value); }
- void Set(const Vec3& value) override { SetValue(value); }
- void Set(const Vec4& value) override { SetValue(value); }
- void Set(const Ang3& value) override { SetValue(value); }
- void Set(const Quat& value) override { SetValue(value); }
- void Set(const QString& value) override { SetValue(value); }
- void Set(const char* value) override { SetValue(QString(value)); }
- void Set(const AZ::Color& value) override { SetValue(value); }
- void Set(const AZ::Vector2& value) override { SetValue(value); }
- void Set(const AZ::Vector3& value) override { SetValue(value); }
- void Set(const AZ::Vector4& value) override { SetValue(value); }
- void Set(const AZ::Quaternion& value) override { SetValue(value); }
- void Set(const AZStd::string& value) override { SetValue(value); }
- //////////////////////////////////////////////////////////////////////////
- // Get methods.
- //////////////////////////////////////////////////////////////////////////
- void Get(int& value) const override { GetValue(value); }
- void Get(bool& value) const override { GetValue(value); }
- void Get(float& value) const override { GetValue(value); }
- void Get(double& value) const override { GetValue(value); }
- void Get(Vec2& value) const override { GetValue(value); }
- void Get(Vec3& value) const override { GetValue(value); }
- void Get(Vec4& value) const override { GetValue(value); }
- void Get(Quat& value) const override { GetValue(value); }
- void Get(QString& value) const override { GetValue(value); }
- void Get(AZ::Color& value) const override { GetValue(value); }
- void Get(AZ::Vector2& value) const override { GetValue(value); }
- void Get(AZ::Vector3& value) const override { GetValue(value); }
- void Get(AZ::Vector4& value) const override { GetValue(value); }
- void Get(AZ::Quaternion& value) const override { GetValue(value); }
- void Get(AZStd::string& value) const override { GetValue(value); }
- bool HasDefaultValue() const override
- {
- T defval;
- var_type::init(defval);
- return m_valueDef == defval;
- }
- void ResetToDefault() override
- {
- T defval;
- var_type::init(defval);
- SetValue(defval);
- }
- //////////////////////////////////////////////////////////////////////////
- // Limits.
- //////////////////////////////////////////////////////////////////////////
- void SetLimits(float fMin, float fMax, float fStep = 0.f, bool bHardMin = true, bool bHardMax = true) override
- {
- m_valueMin = fMin;
- m_valueMax = fMax;
- m_valueStep = fStep;
- m_bHardMin = bHardMin;
- m_bHardMax = bHardMax;
- // Flag to determine when this variable has custom limits set
- m_customLimits = true;
- }
- void GetLimits(float& fMin, float& fMax, float& fStep, bool& bHardMin, bool& bHardMax) override
- {
- if (!m_customLimits && var_type::type_traits<T>::supports_range())
- {
- float value;
- GetValue(value);
- if (value > m_valueMax && !m_bHardMax)
- {
- SetLimits(m_valueMin, nextNiceNumberAbove(value), 0.0, false, false);
- }
- if (value < m_valueMin && !m_bHardMin)
- {
- SetLimits(nextNiceNumberBelow(value), m_valueMax, 0.0, false, false);
- }
- }
- fMin = m_valueMin;
- fMax = m_valueMax;
- fStep = m_valueStep;
- bHardMin = m_bHardMin;
- bHardMax = m_bHardMax;
- }
- void ClearLimits()
- {
- SetLimits(0.0f, 0.0f, 0.0f, false, false);
- m_customLimits = false;
- }
- bool HasCustomLimits() override
- {
- return m_customLimits;
- }
- //////////////////////////////////////////////////////////////////////////
- // Access operators.
- //////////////////////////////////////////////////////////////////////////
- //! Cast to held type.
- operator T const& () const {
- return m_valueDef;
- }
- //! Assign operator for variable.
- void operator=(const T& value) { SetValue(value); }
- //////////////////////////////////////////////////////////////////////////
- IVariable* Clone([[maybe_unused]] bool bRecursive) const override
- {
- Self* var = new Self(*this);
- return var;
- }
- //////////////////////////////////////////////////////////////////////////
- void CopyValue(IVariable* fromVar) override
- {
- assert(fromVar);
- T val;
- fromVar->Get(val);
- SetValue(val);
- }
- protected:
- //////////////////////////////////////////////////////////////////////////
- template <class P>
- void SetValue(const P& value)
- {
- T newValue = T();
- //var_type::type_converter<P,T> converter;
- var_type::type_converter converter;
- converter(value, newValue);
- // compare old and new values.
- if (m_bForceModified || !var_type::compare(m_valueDef, newValue))
- {
- m_valueDef = newValue;
- m_bForceModified = false;
- OnSetValue(false);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- template <class P>
- void GetValue(P& value) const
- {
- var_type::type_converter converter;
- converter(m_valueDef, value);
- }
- protected:
- T m_valueDef;
- bool m_customLimits;
- bool m_bResolving;
- unsigned char m_bHardMin : 1;
- unsigned char m_bHardMax : 1;
- // Min/Max value.
- float m_valueMin, m_valueMax, m_valueStep;
- };
- //////////////////////////////////////////////////////////////////////////
- /**
- **************************************************************************************
- * CVariableArray implements variable of type array of IVariables.
- ***************************************************************************************
- */
- template <class T>
- class TVariableArray
- : public CVariable<T>
- {
- typedef TVariableArray<T> Self;
- public:
- using CVariable<T>::GetType;
- TVariableArray()
- : CVariable<T>() {};
- // Copy Constructor.
- TVariableArray(const Self& var)
- : CVariable<T>(var)
- {}
- //! Get name of parameter.
- virtual int GetSize() const { return sizeof(Self); };
- virtual bool HasDefaultValue() const
- {
- for (Vars::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- if (!(*it)->HasDefaultValue())
- {
- return false;
- }
- }
- return true;
- }
- virtual void ResetToDefault()
- {
- for (Vars::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- (*it)->ResetToDefault();
- }
- }
- //////////////////////////////////////////////////////////////////////////
- IVariable* Clone(bool bRecursive) const
- {
- Self* var = new Self(*this);
- for (Vars::const_iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- var->m_vars.push_back((*it)->Clone(bRecursive));
- }
- return var;
- }
- //////////////////////////////////////////////////////////////////////////
- void CopyValue(IVariable* fromVar)
- {
- assert(fromVar);
- if (fromVar->GetType() != GetType())
- {
- return;
- }
- CVariable<T>::CopyValue(fromVar);
- int numSrc = fromVar->GetNumVariables();
- int numTrg = m_vars.size();
- for (int i = 0; i < numSrc && i < numTrg; i++)
- {
- // Copy Every child variable.
- m_vars[i]->CopyValue(fromVar->GetVariable(i));
- }
- }
- //////////////////////////////////////////////////////////////////////////
- int GetNumVariables() const { return m_vars.size(); }
- IVariable* GetVariable(int index) const
- {
- assert(index >= 0 && index < (int)m_vars.size());
- return m_vars[index];
- }
- void AddVariable(IVariable* var) { m_vars.push_back(var); }
- void DeleteAllVariables() { m_vars.clear(); }
- //////////////////////////////////////////////////////////////////////////
- void Serialize(XmlNodeRef node, bool load)
- {
- CVariable<T>::Serialize(node, load);
- if (load)
- {
- // Loading.
- QString name;
- for (Vars::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- IVariable* var = *it;
- if (var->GetNumVariables())
- {
- XmlNodeRef child = node->findChild(var->GetName().toUtf8().data());
- if (child)
- {
- var->Serialize(child, load);
- }
- }
- else
- {
- var->Serialize(node, load);
- }
- }
- }
- else
- {
- // Saving.
- for (Vars::iterator it = m_vars.begin(); it != m_vars.end(); ++it)
- {
- IVariable* var = *it;
- if (var->GetNumVariables())
- {
- XmlNodeRef child = node->newChild(var->GetName().toUtf8().data());
- var->Serialize(child, load);
- }
- else
- {
- var->Serialize(node, load);
- }
- }
- }
- }
- protected:
- typedef std::vector<IVariablePtr> Vars;
- Vars m_vars;
- };
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- //! Selection list shown in combo box, for enumerated variable.
- struct IVarEnumList
- : public CRefCountBase
- {
- //! Get the name of specified value in enumeration, or empty string if out of range.
- virtual QString GetItemName(uint index) = 0;
- };
- typedef _smart_ptr<IVarEnumList> IVarEnumListPtr;
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING;
- //! Selection list shown in combo box, for enumerated variable.
- template <class T>
- class CVarEnumListBase
- : public IVarEnumList
- {
- AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
- public:
- CVarEnumListBase(){}
- //////////////////////////////////////////////////////////////////////////
- virtual T NameToValue(const QString& name) = 0;
- //////////////////////////////////////////////////////////////////////////
- virtual QString ValueToName(T const& value) = 0;
- //! Add new item to the selection.
- virtual void AddItem(const QString& name, const T& value) = 0;
- template <class TVal>
- static bool IsValueEqual(const TVal& v1, const TVal& v2)
- {
- return v1 == v2;
- }
- static bool IsValueEqual(const QString& v1, const QString& v2)
- {
- // Case insensitive compare.
- return QString::compare(v1, v2, Qt::CaseInsensitive) == 0;
- }
- protected:
- virtual ~CVarEnumListBase() {};
- friend class _smart_ptr<CVarEnumListBase<T> >;
- };
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- //! Selection list shown in combo box, for enumerated variable.
- template <class T>
- class CVarEnumList
- : public CVarEnumListBase<T>
- {
- public:
- using CVarEnumListBase<T>::IsValueEqual;
- CVarEnumList(){}
- struct Item
- {
- QString name;
- T value;
- };
- QString GetItemName(uint index)
- {
- if (index >= m_items.size())
- {
- return QString();
- }
- return m_items[index].name;
- };
- //////////////////////////////////////////////////////////////////////////
- T NameToValue(const QString& name)
- {
- for (int i = 0; i < m_items.size(); i++)
- {
- if (name == m_items[i].name)
- {
- return m_items[i].value;
- }
- }
- return T();
- }
- //////////////////////////////////////////////////////////////////////////
- QString ValueToName(T const& value)
- {
- for (int i = 0; i < m_items.size(); i++)
- {
- if (CVarEnumListBase<T>::IsValueEqual(value, m_items[i].value))
- {
- return m_items[i].name;
- }
- }
- return "";
- }
- //! Add new item to the selection.
- void AddItem(const QString& name, const T& value)
- {
- Item item;
- item.name = name;
- item.value = value;
- m_items.push_back(item);
- };
- protected:
- ~CVarEnumList() {};
- private:
- std::vector<Item> m_items;
- };
- //////////////////////////////////////////////////////////////////////////////////
- // CVariableEnum is the same as CVariable but it display enumerated values in UI
- //////////////////////////////////////////////////////////////////////////////////
- template <class T>
- class CVariableEnum
- : public CVariable<T>
- {
- public:
- using CVariable<T>::SetValue;
- using CVariable<T>::GetFlags;
- using CVariable<T>::OnSetValue;
- using CVariable<T>::Set;
- using CVariable<T>::m_valueDef;
- using CVariable<T>::m_onSetEnumFuncs;
- //////////////////////////////////////////////////////////////////////////
- CVariableEnum(){}
- //! Assign operator for variable.
- void operator=(const T& value) { CVariable<T>::SetValue(value); }
- //! Add new item to the enumeration.
- void AddEnumItem(const char* name, const T& value)
- {
- AddEnumItem(QString(name), value);
- };
- //! Add new item to the enumeration.
- void AddEnumItem(const QString& name, const T& value)
- {
- if (GetFlags() & IVariable::UI_USE_GLOBAL_ENUMS) // don't think adding makes sense
- {
- return;
- }
- if (!m_enum)
- {
- m_enum = new CVarEnumList<T>();
- }
- m_enum->AddItem(name, value);
- OnEnumsChanged();
- };
- void SetEnumList(CVarEnumListBase<T>* enumList)
- {
- m_enum = enumList;
- OnSetValue(false);
- OnEnumsChanged();
- }
- IVarEnumList* GetEnumList() const
- {
- return m_enum;
- }
- //////////////////////////////////////////////////////////////////////////
- IVariable* Clone([[maybe_unused]] bool bRecursive) const
- {
- CVariableEnum<T>* var = new CVariableEnum<T>(*this);
- return var;
- }
- virtual QString GetDisplayValue() const
- {
- if (m_enum)
- {
- return m_enum->ValueToName(m_valueDef);
- }
- else
- {
- return CVariable<T>::GetDisplayValue();
- }
- }
- virtual void SetDisplayValue(const QString& value)
- {
- if (m_enum)
- {
- Set(m_enum->NameToValue(value));
- }
- else
- {
- Set(value);
- }
- }
- protected:
- void OnEnumsChanged()
- {
- // Call on set callback.
- for (auto it = m_onSetEnumFuncs.begin(); it != m_onSetEnumFuncs.end(); ++it)
- {
- // Call on set callback.
- (*it)->operator()(this);
- }
- }
- // Copy Constructor.
- CVariableEnum(const CVariableEnum<T>& var)
- : CVariable<T>(var)
- {
- m_enum = var.m_enum;
- }
- private:
- _smart_ptr<CVarEnumListBase<T> > m_enum;
- };
- //////////////////////////////////////////////////////////////////////////
- // Smart pointers to variables.
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- template <class T, class TVar>
- struct CSmartVariableBase
- {
- typedef TVar VarType;
- CSmartVariableBase() { pVar = new VarType(); }
- operator T const& () const {
- VarType* pV = pVar;
- return *pV;
- }
- //void operator=( const T& value ) { VarType *pV = pVar; *pV = value; }
- // Compare with pointer to variable.
- bool operator==(IVariable* pV) { return pVar == pV; }
- bool operator!=(IVariable* pV) { return pVar != pV; }
- operator VarType&() {
- VarType* pV = pVar;
- return *pV;
- } // Cast to CVariableBase&
- VarType& operator*() const { return *pVar; }
- VarType* operator->() const { return pVar; }
- VarType* GetVar() const { return pVar; };
- protected:
- _smart_ptr<VarType> pVar;
- };
- //////////////////////////////////////////////////////////////////////////
- template <class T>
- struct CSmartVariable
- : public CSmartVariableBase<T, CVariable<T> >
- {
- using CSmartVariableBase<T, CVariable<T> >::pVar;
- void operator=(const T& value) { CVariable<T>* pV = pVar; *pV = value; }
- };
- //////////////////////////////////////////////////////////////////////////
- template <class T>
- struct CSmartVariableArrayT
- : public CSmartVariableBase<T, TVariableArray<T> >
- {
- using CSmartVariableBase<T, TVariableArray<T> >::pVar;
- void operator=(const T& value) { TVariableArray<T>* pV = pVar; *pV = value; }
- };
- //////////////////////////////////////////////////////////////////////////
- template <class T>
- struct CSmartVariableEnum
- : public CSmartVariableBase<T, CVariableEnum<T> >
- {
- using CSmartVariableBase<T, CVariableEnum<T> >::pVar;
- CSmartVariableEnum(){}
- void operator=(const T& value) { CVariableEnum<T>* pV = pVar; *pV = value; }
- void AddEnumItem(const QString& name, const T& value)
- {
- pVar->AddEnumItem(name, value);
- };
- void SetEnumList(CVarEnumListBase<T>* enumList)
- {
- pVar->EnableUpdateCallbacks(false);
- pVar->SetEnumList(enumList);
- pVar->EnableUpdateCallbacks(true);
- }
- };
- //////////////////////////////////////////////////////////////////////////
- struct CSmartVariableArray
- {
- typedef CVariableArray VarType;
- CSmartVariableArray() { pVar = new VarType(); }
- //////////////////////////////////////////////////////////////////////////
- // Access operators.
- //////////////////////////////////////////////////////////////////////////
- operator VarType&() {
- VarType* pV = pVar;
- return *pV;
- }
- VarType& operator*() const { return *pVar; }
- VarType* operator->() const { return pVar; }
- VarType* GetVar() const { return pVar; };
- private:
- AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
- _smart_ptr<VarType> pVar;
- AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
- };
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING
- class EDITOR_CORE_API CVarBlock
- : public IVariableContainer
- {
- AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
- public:
- // Dtor.
- virtual ~CVarBlock() {}
- //! Add variable to block.
- void AddVariable(IVariable* var) override;
- //! Remove variable from block
- bool DeleteVariable(IVariable* var, bool bRecursive = false) override;
- void AddVariable(IVariable* pVar, const char* varName, unsigned char dataType = IVariable::DT_SIMPLE);
- // This used from smart variable pointer.
- void AddVariable(CVariableBase& var, const char* varName, unsigned char dataType = IVariable::DT_SIMPLE);
- //! Returns number of variables in block.
- int GetNumVariables() const override { return static_cast<int>(m_vars.size()); }
- //! Get pointer to stored variable by index.
- IVariable* GetVariable(int index) const override
- {
- assert(index >= 0 && index < m_vars.size());
- return m_vars[index];
- }
- // Clear all vars from VarBlock.
- void DeleteAllVariables() override { m_vars.clear(); };
- //! Return true if variable block is empty (Does not have any vars).
- bool IsEmpty() const override { return m_vars.empty(); }
- // Returns true if var block contains specified variable.
- bool IsContainsVariable(IVariable* pVar, bool bRecursive = true) const override;
- //! Find variable by name.
- IVariable* FindVariable(const char* name, bool bRecursive = true, bool bHumanName = false) const override;
- //////////////////////////////////////////////////////////////////////////
- //! Clone var block.
- CVarBlock* Clone(bool bRecursive) const;
- //////////////////////////////////////////////////////////////////////////
- //! Copy variable values from specified var block.
- //! Do not create new variables, only copy values of existing ones.
- //! Should only be used to copy identical var blocks (e.g. not Array type var copied to String type var)
- //! @param fromVarBlock Source variable block that contain copied values, must be identical to this var block.
- void CopyValues(const CVarBlock* fromVarBlock);
- //////////////////////////////////////////////////////////////////////////
- //! Copy variable values from specified var block.
- //! Do not create new variables, only copy values of existing ones.
- //! Can be used to copy slightly different var blocks, matching performed by variable name.
- //! @param fromVarBlock Source variable block that contain copied values.
- void CopyValuesByName(CVarBlock* fromVarBlock);
- void OnSetValues();
- //////////////////////////////////////////////////////////////////////////
- //! Set UI_CREATE_SPLINE flags for all the variables in this Var Block.
- //! This is because when the data was changed for a spline variable, the spline need to be recreated to reflect the change.
- //! Set the flag to notify the spline variable.
- void SetRecreateSplines();
- //////////////////////////////////////////////////////////////////////////
- // Wire/Unwire other variable blocks.
- //////////////////////////////////////////////////////////////////////////
- //! Wire to other variable block.
- //! Only equivalent VarBlocks can be wired (same number of variables with same type).
- //! Recursive wiring of array variables is supported.
- void Wire(CVarBlock* toVarBlock);
- //! Unwire var block.
- void Unwire(CVarBlock* varBlock);
- //! Add this callback to every variable in block (recursively).
- void AddOnSetCallback(IVariable::OnSetCallback* func);
- //! Remove this callback from every variable in block (recursively).
- void RemoveOnSetCallback(IVariable::OnSetCallback* func);
- //////////////////////////////////////////////////////////////////////////
- void Serialize(XmlNodeRef vbNode, bool load);
- void ReserveNumVariables(int numVars);
- //////////////////////////////////////////////////////////////////////////
- //! Gather resources in this variable block.
- virtual void GatherUsedResources(CUsedResources& resources);
- void EnableUpdateCallbacks(bool boEnable);
- IVariable* FindChildVar(const char* name, IVariable* pParentVar) const;
- void Sort();
- protected:
- void SetCallbackToVar(IVariable::OnSetCallback* func, IVariable* pVar, bool bAdd);
- void WireVar(IVariable* src, IVariable* trg, bool bWire);
- void GatherUsedResourcesInVar(IVariable* pVar, CUsedResources& resources);
- typedef std::vector<IVariablePtr> Variables;
- AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
- Variables m_vars;
- AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
- };
- typedef _smart_ptr<CVarBlock> CVarBlockPtr;
- //////////////////////////////////////////////////////////////////////////
- class EDITOR_CORE_API CVarObject
- : public QObject
- , public CRefCountBase
- {
- public:
- typedef IVariable::OnSetCallback VarOnSetCallback;
- CVarObject();
- ~CVarObject();
- void Serialize(XmlNodeRef node, bool load);
- CVarBlock* GetVarBlock() const { return m_vars; };
- void AddVariable(CVariableBase& var, const QString& varName, VarOnSetCallback* cb = nullptr, unsigned char dataType = IVariable::DT_SIMPLE);
- void AddVariable(CVariableBase& var, const QString& varName, const QString& varHumanName, VarOnSetCallback* cb = nullptr, unsigned char dataType = IVariable::DT_SIMPLE);
- void AddVariable(CVariableArray& table, CVariableBase& var, const QString& varName, const QString& varHumanName, VarOnSetCallback* cb = nullptr, unsigned char dataType = IVariable::DT_SIMPLE);
- void ReserveNumVariables(int numVars);
- void RemoveVariable(IVariable* var);
- void EnableUpdateCallbacks(bool boEnable);
- void OnSetValues();
- protected:
- //! Copy values of variables from other VarObject.
- //! Source object must be of same type.
- void CopyVariableValues(CVarObject* sourceObject);
- private:
- AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
- CVarBlockPtr m_vars;
- AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
- };
- Q_DECLARE_METATYPE(IVariable *);
- #endif // CRYINCLUDE_EDITOR_UTIL_VARIABLE_H
|