metaforce/Runtime/CVar.hpp

106 lines
3.3 KiB
C++
Raw Normal View History

2015-11-22 04:24:51 +00:00
#ifndef CVAR_HPP
#define CVAR_HPP
#include <string>
2015-11-22 07:12:38 +00:00
#include <functional>
2015-11-22 04:24:51 +00:00
#include "CColor.hpp"
#include "DataSpec/DNACommon/CVar.hpp"
namespace Retro
{
class CVarManager;
class CVar : protected DNACVAR::CVar
{
2015-11-22 06:51:25 +00:00
friend class CVarManager;
2015-11-22 04:24:51 +00:00
public:
2015-11-22 07:12:38 +00:00
typedef std::function<void(CVar*)> ListenerFunc;
2015-11-22 04:24:51 +00:00
using EType = DNACVAR::EType;
using EFlags = DNACVAR::EFlags;
2015-11-22 06:51:25 +00:00
CVar(const std::string& name, const std::string& value, const std::string& help, EType type, EFlags flags, CVarManager& parent);
CVar(const std::string& name, const std::string& value, const std::string& help, EFlags flags, CVarManager& parent);
CVar(const std::string& name, const Zeus::CColor& value, const std::string& help, EFlags flags, CVarManager& parent);
CVar(const std::string& name, float value, const std::string& help, EFlags flags, CVarManager& parent);
CVar(const std::string& name, bool value, const std::string& help, EFlags flags, CVarManager& parent);
CVar(const std::string& name, int value, const std::string& help, EFlags flags, CVarManager& parent);
2015-11-22 04:24:51 +00:00
std::string name() const;
std::string help() const;
Zeus::CColor toColor(bool* isValid = nullptr) const;
float toFloat(bool* isValid = nullptr) const;
bool toBoolean(bool* isValid = nullptr) const;
int toInteger(bool* isValid = nullptr) const;
2015-11-22 06:51:25 +00:00
const std::wstring toWideLiteral(bool* isValid = nullptr) const;
const std::string toLiteral(bool* isValid = nullptr) const;
2015-11-22 04:24:51 +00:00
bool fromColor(const Zeus::CColor& val);
bool fromFloat(float val);
bool fromBoolean(bool val);
bool fromInteger(int val);
bool fromLiteral(const std::string& val);
2015-11-22 06:51:25 +00:00
bool fromLiteral(const std::wstring& val);
2015-11-22 04:24:51 +00:00
bool isFloat() const { return m_type == EType::Float; }
bool isBoolean() const { return m_type == EType::Boolean; }
bool isInteger() const { return m_type == EType::Integer; }
bool isLiteral() const { return m_type == EType::Literal; }
bool isColor() const { return m_type == EType::Color; }
bool isModified() const;
bool isReadOnly() const;
bool isCheat() const;
bool isHidden() const;
bool isArchive() const;
void clearModified();
void setModified();
EType type() const { return m_type; }
EFlags flags() const { return m_flags; }
/*!
* \brief Unlocks the CVar for writing if it is ReadOnly.
* <b>Handle with care!!!</b> if you use unlock(), make sure
* you lock the cvar using lock()
* \see lock
*/
void unlock();
/*!
* \brief Locks the CVar to prevent writing if it is ReadOnly.
* Unlike it's partner function unlock, lock is harmless
* \see unlock
*/
void lock();
2015-11-22 07:12:38 +00:00
void addListener(ListenerFunc func) { m_listeners.push_back(func); }
2015-11-22 04:24:51 +00:00
private:
2015-11-22 07:12:38 +00:00
void dispatch();
2015-11-22 04:24:51 +00:00
std::string m_help;
std::string m_defaultValue;
2015-11-22 06:51:25 +00:00
EFlags m_flags;
2015-11-22 04:24:51 +00:00
bool m_allowedWrite;
2015-11-22 06:51:25 +00:00
CVarManager& m_mgr;
2015-11-22 07:12:38 +00:00
std::vector<ListenerFunc> m_listeners;
2015-11-22 04:24:51 +00:00
};
class CVarUnlocker
{
CVar* m_cvar;
public:
CVarUnlocker(CVar* cvar) : m_cvar(cvar) { if (m_cvar) m_cvar->unlock(); }
~CVarUnlocker() { if (m_cvar) m_cvar->lock(); }
};
}
#endif // CVAR_HPP