Fix css style order when using external css files
[ryzomcore.git] / ryzom / client / src / r2 / config_var.h
blob1007893d21fd1cf976dc3f81d8e032b3aebd0e59
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef R2_CONFIG_VAR_H
21 #define R2_CONFIG_VAR_H
23 #include "nel/gui/lua_object.h"
24 #include "editor.h"
26 namespace R2
29 // base class for he config file variables
30 class CConfigVarBase
32 public:
33 CConfigVarBase(const char *varName);
34 protected:
35 mutable uint32 _TimeStamp; // If this date is not the same than the config file date, then we know the
36 // value should be updated (so, more a counter than a timestamp, actually)
37 std::string _VarName;
38 private:
39 // not copyable
40 CConfigVarBase(const CConfigVarBase &/* other */) { nlassert(0); }
41 CConfigVarBase &operator = (const CConfigVarBase &/* other */) { nlassert(0); return *this; }
42 public:
43 static uint32 &getConfigFileTimeStamp();
48 /** Quick access to variables defined inside r2_config.lua.
50 * Variables are updated when the editor is reseted.
51 * They are usually created at global scope.
53 * Example :
55 * CConfigVarRGBA CV_CreatureDefaultColor("CreatureDefaultColor", CRGBA(255, 255, 255, 0))); // r2_config.lua may contains a 'CreatureDefaultColor' config value
57 * creature.setColor(CV_CreatureDefaultColor.get());
60 template <class T>
61 class CConfigVar : public CConfigVarBase
63 public:
64 CConfigVar(const char *varName, const T &defaultValue = T()) : CConfigVarBase(varName), _Default(defaultValue) {}
65 CConfigVar(const CConfigVar &other) : CConfigVarBase(other._VarName.c_str()), _Value(other._Value), _Default(other._Default) {}
66 const T &get() const;
67 private:
68 mutable T _Value;
69 const T _Default;
72 // forward declarations for specialisations
73 std::string getConfigVarTypename(const float &dummy);
74 bool getConfigVarValue(CLuaObject &luaValue, float &dest);
76 std::string getConfigVarTypename(const double &dummy);
77 bool getConfigVarValue(CLuaObject &luaValue, double &dest);
79 std::string getConfigVarTypename(const sint32 &dummy);
80 bool getConfigVarValue(CLuaObject &luaValue, sint32 &dest);
82 std::string getConfigVarTypename(const std::string &dummy);
83 bool getConfigVarValue(CLuaObject &luaValue, std::string &dest);
85 std::string getConfigVarTypename(const NLMISC::CRGBA &dummy);
86 bool getConfigVarValue(CLuaObject &luaValue, NLMISC::CRGBA &dest);
88 template <class T> const T &CConfigVar<T>::get() const
90 // Relies on R2 'getConfigVarValue' and 'getConfigVarTypename' functions specialization to retrieve the value (see below)
91 if (_TimeStamp != CConfigVarBase::getConfigFileTimeStamp())
93 const CLuaObject &config = getEditor().getConfig();
94 if (config.isNil())
96 nlwarning("Trying to access the config file value '%s' of type '%s' from r2_config.lua, but the file hasn't been parsed yet, or its parsing failed, using default value.", _VarName.c_str(), getConfigVarTypename(_Default).c_str());
97 _Value = _Default;
99 else
101 CLuaObject luaValue = config[_VarName];
102 if (!getConfigVarValue(luaValue, _Value))
104 nlwarning("Can't retrieve value of R2 config var '%s', of type '%s', using default value", _VarName.c_str(), getConfigVarTypename(_Default).c_str());
105 _Value = _Default;
108 _TimeStamp = CConfigVarBase::getConfigFileTimeStamp();
110 return _Value;
113 //////////////////////
114 // Specialisations //
115 //////////////////////
117 //------------------------------------------------------------------------------------------------
118 // Float
119 inline std::string getConfigVarTypename(const float &/* dummy */) { return "float"; }
120 inline bool getConfigVarValue(CLuaObject &luaValue, float &dest)
122 if (luaValue.isNumber())
124 dest = (float) luaValue.toNumber();
125 return true;
127 return false;
129 typedef CConfigVar<float> CConfigVarFloat;
131 //------------------------------------------------------------------------------------------------
132 // Double
133 inline std::string getConfigVarTypename(const double &/* dummy */) { return "float"; }
134 inline bool getConfigVarValue(CLuaObject &luaValue, double &dest)
136 if (luaValue.isNumber())
138 dest = luaValue.toNumber();
139 return true;
141 return false;
143 typedef CConfigVar<double> CConfigVarDouble;
145 //------------------------------------------------------------------------------------------------
146 // sint32
147 inline std::string getConfigVarTypename(const sint32 &/* dummy */) { return "sint32"; }
148 inline bool getConfigVarValue(CLuaObject &luaValue, sint32 &dest)
150 if (luaValue.isInteger())
152 dest = (sint32) luaValue.toInteger();
153 return true;
155 return false;
157 typedef CConfigVar<sint32> CConfigVarSInt32;
160 //------------------------------------------------------------------------------------------------
161 // String
162 inline std::string getConfigVarTypename(const std::string &/* dummy */) { return "string"; }
163 inline bool getConfigVarValue(CLuaObject &luaValue, std::string &dest)
165 if (luaValue.isString())
167 dest = luaValue.toString();
168 return true;
170 return false;
172 typedef CConfigVar<std::string> CConfigVarString;
174 //------------------------------------------------------------------------------------------------
175 // RGBA
176 inline std::string getConfigVarTypename(const NLMISC::CRGBA &/* dummy */) { return "rgba"; }
177 inline bool getConfigVarValue(CLuaObject &luaValue, NLMISC::CRGBA &dest)
179 if (luaValue.isRGBA())
181 dest = luaValue.toRGBA();
182 return true;
184 return false;
186 typedef CConfigVar<NLMISC::CRGBA> CConfigVarRGBA;
188 } // R2
190 #endif