|
From: Tapio V. <aa...@us...> - 2010-08-21 18:04:24
|
Module: performous
Branch: master
Commit: 1a00a40e755e22b1f8c394957089aff3b7451388
Author: Tapio Vierros <tap...@gm...>
Date: Tue Aug 17 00:50:29 2010 +0300
Implement new "option_list" ConfigItem type.
Does not yet support selection saving/loading to/from xml.
Also, typedef collides with StringList, so both cannot have constructors ATM.
---
game/configuration.cc | 19 ++++++++++++++++++-
game/configuration.hh | 5 +++++
2 files changed, 23 insertions(+), 1 deletions(-)
diff --git a/game/configuration.cc b/game/configuration.cc
index 9d91a53..d6fa8af 100644
--- a/game/configuration.cc
+++ b/game/configuration.cc
@@ -26,6 +26,8 @@ ConfigItem::ConfigItem(float fval): m_type("float"), m_value(fval) { }
ConfigItem::ConfigItem(std::string sval): m_type("string"), m_value(sval) { }
+ConfigItem::ConfigItem(OptionList opts): m_type("option_list"), m_value(opts), m_sel(0) { }
+
ConfigItem& ConfigItem::incdec(int dir) {
if (m_type == "int") {
@@ -39,6 +41,8 @@ ConfigItem& ConfigItem::incdec(int dir) {
} else if (m_type == "bool") {
bool& val = boost::get<bool>(m_value);
val = !val;
+ } else if (m_type == "option_list") {
+ m_sel = clamp<int>(m_sel + dir, 0, boost::get<OptionList>(m_value).size()-1);
}
return *this;
}
@@ -49,6 +53,7 @@ bool ConfigItem::isDefaultImpl(ConfigItem::Value const& defaultValue) const {
if (m_type == "float") return boost::get<double>(m_value) == boost::get<double>(defaultValue);
if (m_type == "string") return boost::get<std::string>(m_value) == boost::get<std::string>(defaultValue);
if (m_type == "string_list") return boost::get<StringList>(m_value) == boost::get<StringList>(defaultValue);
+ if (m_type == "option_list") return boost::get<OptionList>(m_value) == boost::get<OptionList>(defaultValue);
throw std::logic_error("ConfigItem::is_default doesn't know type '" + m_type + "'");
}
@@ -68,6 +73,8 @@ bool& ConfigItem::b() { verifyType("bool"); return boost::get<bool>(m_value); }
double& ConfigItem::f() { verifyType("float"); return boost::get<double>(m_value); }
std::string& ConfigItem::s() { verifyType("string"); return boost::get<std::string>(m_value); }
ConfigItem::StringList& ConfigItem::sl() { verifyType("string_list"); return boost::get<StringList>(m_value); }
+ConfigItem::OptionList& ConfigItem::ol() { verifyType("option_list"); return boost::get<OptionList>(m_value); }
+std::string& ConfigItem::so() { verifyType("option_list"); return boost::get<OptionList>(m_value).at(m_sel); }
namespace {
template <typename T, typename VariantAll, typename VariantNum> std::string numericFormat(VariantAll const& value, VariantNum const& multiplier, VariantNum const& step) {
@@ -94,6 +101,7 @@ std::string ConfigItem::getValue() const {
StringList const& sl = boost::get<StringList>(m_value);
return sl.size() == 1 ? "{" + sl[0] + "}" : (boost::format("%d items") % sl.size()).str();
}
+ if (m_type == "option_list") return boost::get<OptionList>(m_value).at(m_sel);
throw std::logic_error("ConfigItem::getValue doesn't know type '" + m_type + "'");
}
@@ -179,7 +187,8 @@ void ConfigItem::update(xmlpp::Element& elem, int mode) {
value = elem2.get_content();
}
m_value = value;
- } else if (m_type == "string_list") {
+ } else if (m_type == "string_list" || m_type == "option_list") {
+ //TODO: Option list should also update selection (from attribute?)
std::vector<std::string> value;
xmlpp::NodeSet n2 = elem.find("stringvalue/text()");
for (xmlpp::NodeSet::const_iterator it2 = n2.begin(), end2 = n2.end(); it2 != end2; ++it2) {
@@ -236,6 +245,14 @@ void writeConfig(bool system) {
stringvalueNode->add_child_text(*it);
}
}
+ else if (item.get_type() == "option_list") {
+ //TODO: Write selected also (as attribute?)
+ ConfigItem::OptionList const& value = item.ol();
+ for(ConfigItem::OptionList::const_iterator it = value.begin(); it != value.end(); ++it) {
+ xmlpp::Element* stringvalueNode = entryNode->add_child("stringvalue");
+ stringvalueNode->add_child_text(*it);
+ }
+ }
}
fs::path const& conf = system ? systemConfFile : userConfFile;
std::string tmp = conf.string() + "tmp";
diff --git a/game/configuration.hh b/game/configuration.hh
index 33a6055..e0c92c9 100644
--- a/game/configuration.hh
+++ b/game/configuration.hh
@@ -11,11 +11,13 @@ namespace xmlpp { class Element; } // Forward declaration for libxml++ stuff
class ConfigItem {
public:
typedef std::vector<std::string> StringList; ///< a list of strings
+ typedef std::vector<std::string> OptionList; ///< a list of string options
ConfigItem() {}
ConfigItem(bool bval);
ConfigItem(int ival);
ConfigItem(float fval);
ConfigItem(std::string sval);
+ ConfigItem(OptionList opts);
void update(xmlpp::Element& elem, int mode); ///< Load XML config file, elem = Entry, mode = 0 for schema, 1 for system config and 2 for user config
ConfigItem& operator++() { return incdec(1); } ///< increments config value
ConfigItem& operator--() { return incdec(-1); } ///< decrements config value
@@ -27,6 +29,8 @@ class ConfigItem {
double& f(); ///< Access floating-point item
std::string& s(); ///< Access string item
StringList& sl(); ///< Access stringlist item
+ OptionList& ol(); ///< Access optionlist item
+ std::string& so(); ///< Access currently selected string option
void reset(bool factory = false) { m_value = factory ? m_factoryDefaultValue : m_defaultValue; } ///< Reset to default
void makeSystem() { m_defaultValue = m_value; } ///< Make current value the system default (used when saving system config)
std::string getValue() const; ///< Get a human-readable representation of the current value
@@ -49,6 +53,7 @@ class ConfigItem {
boost::variant<int, double> m_step, m_min, m_max;
boost::variant<int, double> m_multiplier;
std::string m_unit;
+ int m_sel;
};
typedef std::map<std::string, ConfigItem> Config;
|