diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2009-04-13 21:33:06 +0200 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2009-04-13 21:35:19 +0200 |
commit | 28bc8c0edb549cdbbe0832c4202630a039f738b1 (patch) | |
tree | 9989f31a5121f82af5aa264ebfe7096c5ea5030f | |
parent | 627e1bc7eee5b821d29b2161dec4991cae7bf9c0 (diff) | |
download | mana-client-28bc8c0edb549cdbbe0832c4202630a039f738b1.tar.gz mana-client-28bc8c0edb549cdbbe0832c4202630a039f738b1.tar.bz2 mana-client-28bc8c0edb549cdbbe0832c4202630a039f738b1.tar.xz mana-client-28bc8c0edb549cdbbe0832c4202630a039f738b1.zip |
Some cleanup of the Configuration interface
Mainly avoid all the convertions from integer to float and then to
string and also back from string to float and then to integer.
-rw-r--r-- | src/configuration.cpp | 40 | ||||
-rw-r--r-- | src/configuration.h | 63 | ||||
-rw-r--r-- | src/gui/chat.cpp | 2 | ||||
-rw-r--r-- | src/gui/minimap.cpp | 18 | ||||
-rw-r--r-- | src/gui/setup_audio.cpp | 19 | ||||
-rw-r--r-- | src/gui/setup_players.cpp | 2 | ||||
-rw-r--r-- | src/gui/setup_video.cpp | 41 | ||||
-rw-r--r-- | src/main.cpp | 14 | ||||
-rw-r--r-- | src/net/ea/inventoryhandler.cpp | 6 | ||||
-rw-r--r-- | src/player_relations.cpp | 13 | ||||
-rw-r--r-- | src/player_relations.h | 10 |
11 files changed, 121 insertions, 107 deletions
diff --git a/src/configuration.cpp b/src/configuration.cpp index 3c3ae1d5..0f73e2fb 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -26,22 +26,13 @@ #include "utils/stringutils.h" #include "utils/xml.h" -void ConfigurationObject::setValue(const std::string &key, std::string value) +void ConfigurationObject::setValue(const std::string &key, + const std::string &value) { mOptions[key] = value; } -void ConfigurationObject::setValue(const std::string &key, float value) -{ - setValue(key, toString((value == (int)value) ? (int)value : value)); -} - -void Configuration::setValue(const std::string &key, float value) -{ - setValue(key, toString((value == (int)value) ? (int)value : value)); -} - -void Configuration::setValue(const std::string &key, std::string value) +void Configuration::setValue(const std::string &key, const std::string &value) { ConfigurationObject::setValue(key, value); @@ -57,15 +48,29 @@ void Configuration::setValue(const std::string &key, std::string value) } std::string ConfigurationObject::getValue(const std::string &key, - std::string deflt) + const std::string &deflt) const { - OptionIterator iter = mOptions.find(key); + Options::const_iterator iter = mOptions.find(key); return ((iter != mOptions.end()) ? iter->second : deflt); } -float ConfigurationObject::getValue(const std::string &key, float deflt) +int ConfigurationObject::getValue(const std::string &key, int deflt) const +{ + Options::const_iterator iter = mOptions.find(key); + return (iter != mOptions.end()) ? atoi(iter->second.c_str()) : deflt; +} + +unsigned ConfigurationObject::getValue(const std::string &key, + unsigned deflt) const +{ + Options::const_iterator iter = mOptions.find(key); + return (iter != mOptions.end()) ? atol(iter->second.c_str()) : deflt; +} + +double ConfigurationObject::getValue(const std::string &key, + double deflt) const { - OptionIterator iter = mOptions.find(key); + Options::const_iterator iter = mOptions.find(key); return (iter != mOptions.end()) ? atof(iter->second.c_str()) : deflt; } @@ -158,7 +163,8 @@ void Configuration::init(const std::string &filename) void ConfigurationObject::writeToXML(xmlTextWriterPtr writer) { - for (OptionIterator i = mOptions.begin(); i != mOptions.end(); i++) + for (Options::const_iterator i = mOptions.begin(), i_end = mOptions.end(); + i != i_end; ++i) { xmlTextWriterStartElement(writer, BAD_CAST "option"); xmlTextWriterWriteAttribute(writer, diff --git a/src/configuration.h b/src/configuration.h index 0134f29a..c6ff1186 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -22,6 +22,8 @@ #ifndef CONFIGURATION_H #define CONFIGURATION_H +#include "utils/stringutils.h" + #include <libxml/xmlwriter.h> #include <cassert> @@ -33,8 +35,8 @@ class ConfigListener; class ConfigurationObject; /** - * Configuration list manager interface; responsible for serlialising/deserialising - * configuration choices in containers. + * Configuration list manager interface; responsible for + * serializing/deserializing configuration choices in containers. * * \param T Type of the container elements to serialise * \param CONT Type of the container we (de)serialise @@ -48,9 +50,11 @@ class ConfigurationListManager * * \param value The value to write out * \param obj The configuation object to write to - * \return obj, or otherwise NULL to indicate that this option should be skipped + * \return obj, or otherwise NULL to indicate that this option should + * be skipped */ - virtual ConfigurationObject *writeConfigItem(T value, ConfigurationObject *obj) = 0; + virtual ConfigurationObject *writeConfigItem(T value, + ConfigurationObject *obj) = 0; /** * Reads a value from a configuration object @@ -58,7 +62,8 @@ class ConfigurationListManager * \param obj The configuration object to read from * \param container The container to insert the object to */ - virtual CONT readConfigItem(ConfigurationObject *obj, CONT container) = 0; + virtual CONT readConfigItem(ConfigurationObject *obj, + CONT container) = 0; }; /** @@ -80,15 +85,8 @@ class ConfigurationObject * \param key Option identifier. * \param value Value. */ - virtual void setValue(const std::string &key, std::string value); - - /** - * Sets an option using a numeric value. - * - * \param key Option identifier. - * \param value Value. - */ - virtual void setValue(const std::string &key, float value); + virtual void setValue(const std::string &key, + const std::string &value); /** * Gets a value as string. @@ -96,15 +94,14 @@ class ConfigurationObject * \param key Option identifier. * \param deflt Default option if not there or error. */ - std::string getValue(const std::string &key, std::string deflt); + std::string getValue(const std::string &key, + const std::string &deflt) const; - /** - * Gets a value as numeric (float). - * - * \param key Option identifier. - * \param deflt Default option if not there or error. - */ - float getValue(const std::string &key, float deflt); + int getValue(const std::string &key, int deflt) const; + + unsigned getValue(const std::string &key, unsigned deflt) const; + + double getValue(const std::string &key, double deflt) const; /** * Re-sets all data in the configuration @@ -124,7 +121,8 @@ class ConfigurationObject * \param manager An object capable of serialising T items */ template <class IT, class T, class CONT> - void setList(const std::string &name, IT begin, IT end, ConfigurationListManager<T, CONT> *manager) + void setList(const std::string &name, IT begin, IT end, + ConfigurationListManager<T, CONT> *manager) { ConfigurationObject *nextobj = new ConfigurationObject; deleteList(name); @@ -173,7 +171,6 @@ class ConfigurationObject void deleteList(const std::string &name); typedef std::map<std::string, std::string> Options; - typedef Options::iterator OptionIterator; Options mOptions; typedef std::list<ConfigurationObject *> ConfigurationList; @@ -213,8 +210,22 @@ class Configuration : public ConfigurationObject */ void removeListener(const std::string &key, ConfigListener *listener); - virtual void setValue(const std::string &key, std::string value); - virtual void setValue(const std::string &key, float value); + void setValue(const std::string &key, const std::string &value); + + inline void setValue(const std::string &key, float value) + { setValue(key, toString(value)); } + + inline void setValue(const std::string &key, double value) + { setValue(key, toString(value)); } + + inline void setValue(const std::string &key, int value) + { setValue(key, toString(value)); } + + inline void setValue(const std::string &key, unsigned value) + { setValue(key, toString(value)); } + + inline void setValue(const std::string &key, bool value) + { setValue(key, value ? std::string("1") : std::string("0")); } private: typedef std::list<ConfigListener*> Listeners; diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index 7f08ec48..48acefee 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -107,7 +107,7 @@ ChatWindow::ChatWindow(): ChatWindow::~ChatWindow() { - config.setValue("ReturnToggles", mReturnToggles ? "1" : "0"); + config.setValue("ReturnToggles", mReturnToggles); delete mRecorder; delete_all(mWhispers); delete mItemLinkHandler; diff --git a/src/gui/minimap.cpp b/src/gui/minimap.cpp index 061a2ace..3762043c 100644 --- a/src/gui/minimap.cpp +++ b/src/gui/minimap.cpp @@ -19,19 +19,19 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include <guichan/font.hpp> +#include "gui/minimap.h" -#include "minimap.h" +#include "being.h" +#include "beingmanager.h" +#include "configuration.h" +#include "graphics.h" +#include "localplayer.h" -#include "../being.h" -#include "../beingmanager.h" -#include "../configuration.h" -#include "../graphics.h" -#include "../localplayer.h" +#include "resources/image.h" -#include "../resources/image.h" +#include "utils/gettext.h" -#include "../utils/gettext.h" +#include <guichan/font.hpp> bool Minimap::mShow = true; diff --git a/src/gui/setup_audio.cpp b/src/gui/setup_audio.cpp index 5e13574e..3f98c44f 100644 --- a/src/gui/setup_audio.cpp +++ b/src/gui/setup_audio.cpp @@ -79,9 +79,14 @@ Setup_Audio::Setup_Audio(): void Setup_Audio::apply() { - if (mSoundCheckBox->isSelected()) + mSoundEnabled = mSoundCheckBox->isSelected(); + mSfxVolume = (int) config.getValue("sfxVolume", 100); + mMusicVolume = (int) config.getValue("musicVolume", 60); + + config.setValue("sound", mSoundEnabled); + + if (mSoundEnabled) { - config.setValue("sound", 1); try { sound.init(); @@ -92,20 +97,16 @@ void Setup_Audio::apply() logger->log("Warning: %s", err); } - if (engine) { + if (engine) + { Map *currentMap = engine->getCurrentMap(); sound.playMusic(currentMap->getProperty("music"), -1); } } else { - config.setValue("sound", 0); sound.close(); } - - mSoundEnabled = config.getValue("sound", 0); - mSfxVolume = (int) config.getValue("sfxVolume", 100); - mMusicVolume = (int) config.getValue("musicVolume", 60); } void Setup_Audio::cancel() @@ -118,7 +119,7 @@ void Setup_Audio::cancel() sound.setMusicVolume(mMusicVolume); mMusicSlider->setValue(mMusicVolume); - config.setValue("sound", mSoundEnabled ? 1 : 0); + config.setValue("sound", mSoundEnabled); config.setValue("sfxVolume", mSfxVolume); config.setValue("musicVolume", mMusicVolume); } diff --git a/src/gui/setup_players.cpp b/src/gui/setup_players.cpp index 8925016b..9a3d6967 100644 --- a/src/gui/setup_players.cpp +++ b/src/gui/setup_players.cpp @@ -157,7 +157,7 @@ public: gcn::DropDown *choicebox = dynamic_cast<gcn::DropDown *>( getElementAt(row, RELATION_CHOICE_COLUMN)); player_relations.setRelation(getPlayerAt(row), - static_cast<PlayerRelation::relation>( + static_cast<PlayerRelation::Relation>( choicebox->getSelected())); } diff --git a/src/gui/setup_video.cpp b/src/gui/setup_video.cpp index 8f3d7399..8f358eda 100644 --- a/src/gui/setup_video.cpp +++ b/src/gui/setup_video.cpp @@ -109,7 +109,7 @@ Setup_Video::Setup_Video(): mFullScreenEnabled(config.getValue("screen", false)), mOpenGLEnabled(config.getValue("opengl", false)), mCustomCursorEnabled(config.getValue("customcursor", true)), - mVisibleNamesEnabled(config.getValue("visiblenames", 1)), + mVisibleNamesEnabled(config.getValue("visiblenames", true)), mParticleEffectsEnabled(config.getValue("particleeffects", true)), mNameEnabled(config.getValue("showownname", false)), mPickupChatEnabled(config.getValue("showpickupchat", true)), @@ -351,13 +351,13 @@ void Setup_Video::apply() _("Restart needed for changes to take effect.")); } #endif - config.setValue("screen", fullscreen ? true : false); + config.setValue("screen", fullscreen); } // OpenGL change if (mOpenGLCheckBox->isSelected() != mOpenGLEnabled) { - config.setValue("opengl", mOpenGLCheckBox->isSelected() ? true : false); + config.setValue("opengl", mOpenGLCheckBox->isSelected()); // OpenGL can currently only be changed by restarting, notify user. new OkDialog(_("Changing OpenGL"), @@ -370,7 +370,7 @@ void Setup_Video::apply() // We sync old and new values at apply time mFullScreenEnabled = config.getValue("screen", false); mCustomCursorEnabled = config.getValue("customcursor", true); - mVisibleNamesEnabled = config.getValue("visiblenames", 1); + mVisibleNamesEnabled = config.getValue("visiblenames", true); mParticleEffectsEnabled = config.getValue("particleeffects", true); mNameEnabled = config.getValue("showownname", false); mSpeechMode = (int) config.getValue("speech", 3); @@ -419,19 +419,18 @@ void Setup_Video::cancel() updateSlider(mScrollRadiusSlider, mScrollRadiusField, "ScrollRadius"); updateSlider(mScrollLazinessSlider, mScrollLazinessField, "ScrollLaziness"); - config.setValue("screen", mFullScreenEnabled ? true : false); - config.setValue("customcursor", mCustomCursorEnabled ? true : false); - config.setValue("visiblenames", mVisibleNamesEnabled ? 1 : 0); - config.setValue("particleeffects", mParticleEffectsEnabled ? true : false); + config.setValue("screen", mFullScreenEnabled); + config.setValue("customcursor", mCustomCursorEnabled); + config.setValue("visiblenames", mVisibleNamesEnabled); + config.setValue("particleeffects", mParticleEffectsEnabled); config.setValue("speech", mSpeechMode); - config.setValue("showownname", mNameEnabled ? true : false); + config.setValue("showownname", mNameEnabled); if (player_node) player_node->mUpdateName = true; config.setValue("guialpha", mOpacity); - config.setValue("opengl", mOpenGLEnabled ? true : false); - config.setValue("showpickupchat", mPickupChatEnabled ? true : false); - config.setValue("showpickupparticle", mPickupParticleEnabled ? - true : false); + config.setValue("opengl", mOpenGLEnabled); + config.setValue("showpickupchat", mPickupChatEnabled); + config.setValue("showpickupparticle", mPickupParticleEnabled); } void Setup_Video::action(const gcn::ActionEvent &event) @@ -455,31 +454,28 @@ void Setup_Video::action(const gcn::ActionEvent &event) } else if (event.getId() == "customcursor") { - config.setValue("customcursor", - mCustomCursorCheckBox->isSelected() ? true : false); + config.setValue("customcursor", mCustomCursorCheckBox->isSelected()); } else if (event.getId() == "visiblenames") { - config.setValue("visiblenames", - mVisibleNamesCheckBox->isSelected() ? 1 : 0); + config.setValue("visiblenames", mVisibleNamesCheckBox->isSelected()); } else if (event.getId() == "particleeffects") { config.setValue("particleeffects", - mParticleEffectsCheckBox->isSelected() ? true : false); + mParticleEffectsCheckBox->isSelected()); new OkDialog(_("Particle effect settings changed."), _("Restart your client or change maps " "for the change to take effect.")); } else if (event.getId() == "pickupchat") { - config.setValue("showpickupchat", mPickupChatCheckBox->isSelected() - ? true : false); + config.setValue("showpickupchat", mPickupChatCheckBox->isSelected()); } else if (event.getId() == "pickupparticle") { config.setValue("showpickupparticle", - mPickupParticleCheckBox->isSelected() ? true : false); + mPickupParticleCheckBox->isSelected()); } else if (event.getId() == "speech") { @@ -508,8 +504,7 @@ void Setup_Video::action(const gcn::ActionEvent &event) // and requires an update if (player_node) player_node->mUpdateName = true; - config.setValue("showownname", - mNameCheckBox->isSelected() ? true : false); + config.setValue("showownname", mNameCheckBox->isSelected()); } else if (event.getId() == "fpslimitslider") { diff --git a/src/main.cpp b/src/main.cpp index 5ae86d54..35943941 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -344,23 +344,23 @@ static void initConfiguration(const Options &options) int defaultPort = (int)branding.getValue("defaultPort", 6901); #endif config.setValue("port", defaultPort); - config.setValue("hwaccel", 0); + config.setValue("hwaccel", false); #if (defined __APPLE__ || defined WIN32) && defined USE_OPENGL - config.setValue("opengl", 1); + config.setValue("opengl", true); #else - config.setValue("opengl", 0); + config.setValue("opengl", false); #endif - config.setValue("screen", 0); - config.setValue("sound", 1); + config.setValue("screen", false); + config.setValue("sound", true); config.setValue("guialpha", 0.8f); - config.setValue("remember", 1); + config.setValue("remember", true); config.setValue("sfxVolume", 100); config.setValue("musicVolume", 60); config.setValue("fpslimit", 60); std::string defaultUpdateHost = branding.getValue("defaultUpdateHost", "http://updates.themanaworld.org"); config.setValue("updatehost", defaultUpdateHost); - config.setValue("customcursor", 1); + config.setValue("customcursor", true); config.setValue("ChatLogLength", 128); // Checking if the configuration file exists... otherwise create it with diff --git a/src/net/ea/inventoryhandler.cpp b/src/net/ea/inventoryhandler.cpp index b4d05de2..4fdff023 100644 --- a/src/net/ea/inventoryhandler.cpp +++ b/src/net/ea/inventoryhandler.cpp @@ -180,7 +180,7 @@ void InventoryHandler::handleMessage(MessageIn &msg) if (msg.readInt8() > 0) { - if (config.getValue("showpickupchat", true)) + if (config.getValue("showpickupchat", 1)) localChatTab->chatLog(_("Unable to pick up item"), BY_SERVER); } else @@ -189,14 +189,14 @@ void InventoryHandler::handleMessage(MessageIn &msg) const std::string amountStr = (amount > 1) ? toString(amount) : "a"; - if (config.getValue("showpickupchat", true)) + if (config.getValue("showpickupchat", 1)) { localChatTab->chatLog(strprintf(_("You picked up %s [%s]"), amountStr.c_str(), itemInfo.getName().c_str()), BY_SERVER); } - if (config.getValue("showpickupparticle", false)) + if (config.getValue("showpickupparticle", 0)) { player_node->pickedUp(itemInfo.getName()); } diff --git a/src/player_relations.cpp b/src/player_relations.cpp index ef2ef1bc..898996fa 100644 --- a/src/player_relations.cpp +++ b/src/player_relations.cpp @@ -49,7 +49,7 @@ class PlayerConfSerialiser : public ConfigurationListManager<std::pair<std::stri if (!value.second) return NULL; cobj->setValue(NAME, value.first); - cobj->setValue(RELATION, value.second->mRelation); + cobj->setValue(RELATION, toString(value.second->mRelation)); return cobj; } @@ -64,7 +64,7 @@ class PlayerConfSerialiser : public ConfigurationListManager<std::pair<std::stri if (!(*container)[name]) { int v = (int)cobj->getValue(RELATION, PlayerRelation::NEUTRAL); - (*container)[name] = new PlayerRelation(static_cast<PlayerRelation::relation>(v)); + (*container)[name] = new PlayerRelation(static_cast<PlayerRelation::Relation>(v)); } // otherwise ignore the duplicate entry @@ -81,7 +81,7 @@ const unsigned int PlayerRelation::RELATION_PERMISSIONS[RELATIONS_NR] = { /* IGNORED */ 0 }; -PlayerRelation::PlayerRelation(relation relation) +PlayerRelation::PlayerRelation(Relation relation) { mRelation = relation; } @@ -126,7 +126,7 @@ void PlayerRelationsManager::load() clear(); mPersistIgnores = config.getValue(PERSIST_IGNORE_LIST, 0); - mDefaultPermissions = (int)config.getValue(DEFAULT_PERMISSIONS, mDefaultPermissions); + mDefaultPermissions = (int) config.getValue(DEFAULT_PERMISSIONS, mDefaultPermissions); std::string ignore_strategy_name = config.getValue(PLAYER_IGNORE_STRATEGY, DEFAULT_IGNORE_STRATEGY); int ignore_strategy_index = getPlayerIgnoreStrategyIndex(ignore_strategy_name); if (ignore_strategy_index >= 0) @@ -222,7 +222,8 @@ bool PlayerRelationsManager::hasPermission(const std::string &name, unsigned int return permitted; } -void PlayerRelationsManager::setRelation(const std::string &player_name, PlayerRelation::relation relation) +void PlayerRelationsManager::setRelation(const std::string &player_name, + PlayerRelation::Relation relation) { PlayerRelation *r = mRelations[player_name]; if (r == NULL) @@ -257,7 +258,7 @@ void PlayerRelationsManager::removePlayer(const std::string &name) } -PlayerRelation::relation PlayerRelationsManager::getRelation(const std::string &name) +PlayerRelation::Relation PlayerRelationsManager::getRelation(const std::string &name) { if (mRelations[name]) return mRelations[name]->mRelation; diff --git a/src/player_relations.h b/src/player_relations.h index a6c6a115..adc6a95f 100644 --- a/src/player_relations.h +++ b/src/player_relations.h @@ -46,16 +46,16 @@ struct PlayerRelation | SPEECH_LOG | WHISPER | TRADE; - enum relation { + enum Relation { NEUTRAL = 0, FRIEND = 1, DISREGARDED = 2, IGNORED = 3 }; - PlayerRelation(relation relation); + PlayerRelation(Relation relation); - relation mRelation; // bitmask for all of the above + Relation mRelation; // bitmask for all of the above }; @@ -131,12 +131,12 @@ public: * Updates the relationship with this player. */ void setRelation(const std::string &name, - PlayerRelation::relation relation); + PlayerRelation::Relation relation); /** * Updates the relationship with this player. */ - PlayerRelation::relation getRelation(const std::string &name); + PlayerRelation::Relation getRelation(const std::string &name); /** * Deletes the information recorded for a player. |