XConsoleVariable.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #pragma once
  9. #include <IConsole.h>
  10. #include <ISystem.h>
  11. #include <AzCore/std/function/function_template.h>
  12. class CXConsole;
  13. class CXConsoleVariableBase
  14. : public ICVar
  15. {
  16. public:
  17. //! constructor
  18. //! \param pConsole must not be 0
  19. CXConsoleVariableBase(CXConsole* pConsole, const char* sName, int nFlags, const char* help);
  20. //! destructor
  21. virtual ~CXConsoleVariableBase();
  22. // interface ICVar --------------------------------------------------------------------------------------
  23. void ClearFlags(int flags) override;
  24. int GetFlags() const override;
  25. int SetFlags(int flags) override;
  26. const char* GetName() const override;
  27. const char* GetHelp() override;
  28. void Release() override;
  29. void ForceSet(const char* s) override;
  30. void SetOnChangeCallback(ConsoleVarFunc pChangeFunc) override;
  31. bool AddOnChangeFunctor(AZ::Name functorName, const AZStd::function<void()>& pChangeFunctor) override;
  32. ConsoleVarFunc GetOnChangeCallback() const override;
  33. bool ShouldReset() const { return (m_nFlags & VF_RESETTABLE) != 0; }
  34. void Reset() override
  35. {
  36. if (ShouldReset())
  37. {
  38. ResetImpl();
  39. }
  40. }
  41. virtual void ResetImpl() = 0;
  42. void SetLimits(float min, float max) override;
  43. void GetLimits(float& min, float& max) override;
  44. bool HasCustomLimits() override;
  45. int GetRealIVal() const override { return GetIVal(); }
  46. bool IsConstCVar() const override {return (m_nFlags & VF_CONST_CVAR) != 0; }
  47. void SetDataProbeString(const char* pDataProbeString) override
  48. {
  49. CRY_ASSERT(m_pDataProbeString == NULL);
  50. m_pDataProbeString = new char[ strlen(pDataProbeString) + 1 ];
  51. azstrcpy(m_pDataProbeString, strlen(pDataProbeString) + 1, pDataProbeString);
  52. }
  53. const char* GetDataProbeString() const override;
  54. protected: // ------------------------------------------------------------------------------------------
  55. virtual const char* GetOwnDataProbeString() const
  56. {
  57. return GetString();
  58. }
  59. void CallOnChangeFunctions();
  60. char* m_szName; // if VF_COPYNAME then this data need to be deleteed, otherwise it's pointer to .dll/.exe
  61. char* m_psHelp; // pointer to the help string, might be 0
  62. char* m_pDataProbeString; // value client is required to have for data probes
  63. int m_nFlags; // e.g. VF_CHEAT, ...
  64. using ChangeFunctorContainer = AZStd::unordered_map<AZ::Name, AZStd::function<void ()>>;
  65. ChangeFunctorContainer m_changeFunctors;
  66. ConsoleVarFunc m_pChangeFunc; // Callback function that is called when this variable changes.
  67. CXConsole* m_pConsole; // used for the callback OnBeforeVarChange()
  68. float m_valueMin;
  69. float m_valueMax;
  70. bool m_hasCustomLimits;
  71. };
  72. //////////////////////////////////////////////////////////////////////////
  73. class CXConsoleVariableString
  74. : public CXConsoleVariableBase
  75. {
  76. public:
  77. // constructor
  78. CXConsoleVariableString(CXConsole* pConsole, const char* sName, const char* szDefault, int nFlags, const char* help)
  79. : CXConsoleVariableBase(pConsole, sName, nFlags, help)
  80. {
  81. if (szDefault)
  82. {
  83. m_sValue = szDefault;
  84. m_sDefault = szDefault;
  85. }
  86. }
  87. // interface ICVar --------------------------------------------------------------------------------------
  88. int GetIVal() const override { return atoi(m_sValue.c_str()); }
  89. int64 GetI64Val() const override { return _atoi64(m_sValue.c_str()); }
  90. float GetFVal() const override { return (float)atof(m_sValue.c_str()); }
  91. const char* GetString() const override { return m_sValue.c_str(); }
  92. void ResetImpl() override
  93. {
  94. Set(m_sDefault.c_str());
  95. }
  96. void Set(const char* s) override;
  97. void Set(float f) override;
  98. void Set(int i) override;
  99. int GetType() override { return CVAR_STRING; }
  100. private: // --------------------------------------------------------------------------------------------
  101. AZStd::string m_sValue;
  102. AZStd::string m_sDefault; //!<
  103. };
  104. class CXConsoleVariableInt
  105. : public CXConsoleVariableBase
  106. {
  107. public:
  108. // constructor
  109. CXConsoleVariableInt(CXConsole* pConsole, const char* sName, const int iDefault, int nFlags, const char* help)
  110. : CXConsoleVariableBase(pConsole, sName, nFlags, help)
  111. , m_iValue(iDefault)
  112. , m_iDefault(iDefault)
  113. {
  114. }
  115. // interface ICVar --------------------------------------------------------------------------------------
  116. int GetIVal() const override { return m_iValue; }
  117. int64 GetI64Val() const override { return m_iValue; }
  118. float GetFVal() const override { return (float)GetIVal(); }
  119. const char* GetString() const override;
  120. void ResetImpl() override { Set(m_iDefault); }
  121. void Set(const char* s) override;
  122. void Set(float f) override;
  123. void Set(int i) override;
  124. int GetType() override { return CVAR_INT; }
  125. protected: // --------------------------------------------------------------------------------------------
  126. int m_iValue;
  127. int m_iDefault; //!<
  128. };
  129. //////////////////////////////////////////////////////////////////////////
  130. class CXConsoleVariableFloat
  131. : public CXConsoleVariableBase
  132. {
  133. public:
  134. // constructor
  135. CXConsoleVariableFloat(CXConsole* pConsole, const char* sName, const float fDefault, int nFlags, const char* help)
  136. : CXConsoleVariableBase(pConsole, sName, nFlags, help)
  137. , m_fValue(fDefault)
  138. , m_fDefault(fDefault)
  139. {
  140. }
  141. // interface ICVar --------------------------------------------------------------------------------------
  142. int GetIVal() const override { return (int)m_fValue; }
  143. int64 GetI64Val() const override { return (int64)m_fValue; }
  144. float GetFVal() const override { return m_fValue; }
  145. const char* GetString() const override;
  146. void ResetImpl() override { Set(m_fDefault); }
  147. void Set(const char* s) override;
  148. void Set(float f) override;
  149. void Set(int i) override;
  150. int GetType() override { return CVAR_FLOAT; }
  151. protected:
  152. const char* GetOwnDataProbeString() const override
  153. {
  154. static char szReturnString[8];
  155. sprintf_s(szReturnString, "%.1g", m_fValue);
  156. return szReturnString;
  157. }
  158. private: // --------------------------------------------------------------------------------------------
  159. float m_fValue;
  160. float m_fDefault; //!<
  161. };
  162. class CXConsoleVariableIntRef
  163. : public CXConsoleVariableBase
  164. {
  165. public:
  166. //! constructor
  167. //!\param pVar must not be 0
  168. CXConsoleVariableIntRef(CXConsole* pConsole, const char* sName, int32* pVar, int nFlags, const char* help)
  169. : CXConsoleVariableBase(pConsole, sName, nFlags, help)
  170. , m_iValue(*pVar)
  171. , m_iDefault(*pVar)
  172. {
  173. assert(pVar);
  174. }
  175. // interface ICVar --------------------------------------------------------------------------------------
  176. int GetIVal() const override { return m_iValue; }
  177. int64 GetI64Val() const override { return m_iValue; }
  178. float GetFVal() const override { return (float)m_iValue; }
  179. const char* GetString() const override;
  180. void ResetImpl() override { Set(m_iDefault); }
  181. void Set(const char* s) override;
  182. void Set(float f) override;
  183. void Set(int i) override;
  184. int GetType() override { return CVAR_INT; }
  185. private: // --------------------------------------------------------------------------------------------
  186. int& m_iValue;
  187. int m_iDefault; //!<
  188. };
  189. class CXConsoleVariableStringRef
  190. : public CXConsoleVariableBase
  191. {
  192. public:
  193. //! constructor
  194. //!\param userBuf must not be 0
  195. CXConsoleVariableStringRef(CXConsole* pConsole, const char* sName, const char** userBuf, const char* defaultValue, int nFlags, const char* help)
  196. : CXConsoleVariableBase(pConsole, sName, nFlags, help)
  197. , m_sValue(defaultValue)
  198. , m_sDefault(defaultValue)
  199. , m_userPtr(*userBuf)
  200. {
  201. m_userPtr = m_sValue.c_str();
  202. assert(userBuf);
  203. }
  204. // interface ICVar --------------------------------------------------------------------------------------
  205. int GetIVal() const override { return atoi(m_sValue.c_str()); }
  206. int64 GetI64Val() const override { return _atoi64(m_sValue.c_str()); }
  207. float GetFVal() const override { return (float)atof(m_sValue.c_str()); }
  208. const char* GetString() const override
  209. {
  210. return m_sValue.c_str();
  211. }
  212. void ResetImpl() override { Set(m_sDefault.c_str()); }
  213. void Set(const char* s) override;
  214. void Set(float f) override
  215. {
  216. AZStd::fixed_string<32> s = AZStd::fixed_string<32>::format("%g", f);
  217. Set(s.c_str());
  218. }
  219. void Set(int i) override
  220. {
  221. AZStd::fixed_string<32> s = AZStd::fixed_string<32>::format("%d", i);
  222. Set(s.c_str());
  223. }
  224. int GetType() override { return CVAR_STRING; }
  225. private: // --------------------------------------------------------------------------------------------
  226. AZStd::string m_sValue;
  227. AZStd::string m_sDefault;
  228. const char*& m_userPtr; //!<
  229. };
  230. class CXConsoleVariableFloatRef
  231. : public CXConsoleVariableBase
  232. {
  233. public:
  234. //! constructor
  235. //!\param pVar must not be 0
  236. CXConsoleVariableFloatRef(CXConsole* pConsole, const char* sName, float* pVar, int nFlags, const char* help)
  237. : CXConsoleVariableBase(pConsole, sName, nFlags, help)
  238. , m_fValue(*pVar)
  239. , m_fDefault(*pVar)
  240. {
  241. assert(pVar);
  242. }
  243. // interface ICVar --------------------------------------------------------------------------------------
  244. int GetIVal() const override { return (int)m_fValue; }
  245. int64 GetI64Val() const override { return (int64)m_fValue; }
  246. float GetFVal() const override { return m_fValue; }
  247. const char* GetString() const override;
  248. void ResetImpl() override { Set(m_fDefault); }
  249. void Set(const char* s) override;
  250. void Set(float f) override;
  251. void Set(int i) override;
  252. int GetType() override { return CVAR_FLOAT; }
  253. protected:
  254. const char *GetOwnDataProbeString() const override
  255. {
  256. static char szReturnString[8];
  257. sprintf_s(szReturnString, "%.1g", m_fValue);
  258. return szReturnString;
  259. }
  260. private: // --------------------------------------------------------------------------------------------
  261. float& m_fValue;
  262. float m_fDefault; //!<
  263. };