diff options
-rw-r--r-- | src/being.cpp | 2 | ||||
-rw-r--r-- | src/configuration.h | 13 | ||||
-rw-r--r-- | src/event.h | 40 | ||||
-rw-r--r-- | src/eventlistener.h | 2 | ||||
-rw-r--r-- | src/gui/gui.cpp | 2 | ||||
-rw-r--r-- | src/gui/setup_interface.cpp | 12 | ||||
-rw-r--r-- | src/gui/setup_video.cpp | 4 | ||||
-rw-r--r-- | src/gui/widgets/chattab.cpp | 2 | ||||
-rw-r--r-- | src/localplayer.cpp | 12 | ||||
-rw-r--r-- | src/resources/theme.cpp | 2 |
10 files changed, 71 insertions, 20 deletions
diff --git a/src/being.cpp b/src/being.cpp index 821f7054..56ed65f7 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -1232,7 +1232,7 @@ void Being::event(Event::Channel channel, const Event &event) else if (channel == Event::ConfigChannel && event.getType() == Event::ConfigOptionChanged) { - if (getType() == PLAYER && event.getString("option") == "visiblenames") + if (getType() == PLAYER && event.hasValue(&Config::visibleNames)) { setShowName(config.visibleNames); } diff --git a/src/configuration.h b/src/configuration.h index 69a211be..e0ffae51 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -261,4 +261,17 @@ extern Config config; extern Configuration branding; extern Configuration paths; +/** + * Sets the given Config member and sends a change event. + */ +template<typename T> +void setConfigValue(T Config::*member, const T &value) +{ + if (config.*member == value) + return; + + config.*member = value; + Event(Event::ConfigOptionChanged, member).trigger(Event::ConfigChannel); +} + #endif diff --git a/src/event.h b/src/event.h index 9867c8aa..d3d670e3 100644 --- a/src/event.h +++ b/src/event.h @@ -21,6 +21,7 @@ #ifndef EVENT_H #define EVENT_H +#include <any> #include <map> #include <set> #include <string> @@ -64,7 +65,7 @@ public: Close, CloseAll, CloseDialog, - ConfigOptionChanged, // todo: replace with more specific events + ConfigOptionChanged, Constructed, LoadingDatabases, Destroyed, @@ -109,7 +110,17 @@ public: * Makes an event with the given name. */ Event(Type type) - { mType = type; } + : mType(type) + {} + + /** + * Makes an event with the given name and value. + */ + template<typename T> + Event(Type type, const T &value) + : mType(type) + , mValue(value) + {} ~Event(); @@ -119,6 +130,28 @@ public: Type getType() const { return mType; } + /** + * Sets the value of the event. + */ + template<typename T> + void setValue(const T &value) + { mValue = value; } + + /** + * Returns the value of the event. Throws an exception if the event has no + * value of the given type. + */ + template<typename T> + const T &value() const + { return std::any_cast<const T &>(mValue); } + + /** + * Returns whether the event has the given the value. + */ + template<typename T> + bool hasValue(const T &value) const + { return mValue.type() == typeid(T) && Event::value<T>() == value; } + // Integers /** @@ -280,8 +313,9 @@ private: using ListenMap = std::map<Channel, std::set<EventListener *>>; static ListenMap mBindings; - Type mType; + const Type mType; std::map<std::string, VariableData *> mData; + std::any mValue; }; inline void serverNotice(const std::string &message) diff --git a/src/eventlistener.h b/src/eventlistener.h index 60403332..3d4f8967 100644 --- a/src/eventlistener.h +++ b/src/eventlistener.h @@ -23,8 +23,6 @@ #include "event.h" -#include <string> - class EventListener { public: diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 39314fcf..a59242dc 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -177,7 +177,7 @@ void Gui::event(Event::Channel channel, const Event &event) if (channel == Event::ConfigChannel) { if (event.getType() == Event::ConfigOptionChanged && - event.getString("option") == "customcursor") + event.hasValue(&Config::customCursor)) { setUseCustomCursor(config.customCursor); } diff --git a/src/gui/setup_interface.cpp b/src/gui/setup_interface.cpp index 229898c7..7eef974c 100644 --- a/src/gui/setup_interface.cpp +++ b/src/gui/setup_interface.cpp @@ -205,11 +205,11 @@ void Setup_Interface::cancel() //mAlphaSlider->setEnabled(!mSDLTransparencyDisabled); config.showMonstersTakedDamage = mShowMonsterDamageEnabled; - config.visibleNames = mVisibleNamesEnabled; + setConfigValue(&Config::visibleNames, mVisibleNamesEnabled); config.speech = mSpeechMode; - config.showOwnName = mNameEnabled; + setConfigValue(&Config::showOwnName, mNameEnabled); config.logNpcInGui = mNPCLogEnabled; - config.guiAlpha = mOpacity; + setConfigValue<float>(&Config::guiAlpha, mOpacity); config.showPickupChat = mPickupChatEnabled; config.showPickupParticle = mPickupParticleEnabled; } @@ -220,7 +220,7 @@ void Setup_Interface::action(const gcn::ActionEvent &event) if (id == "guialpha") { - config.guiAlpha = mAlphaSlider->getValue(); + setConfigValue<float>(&Config::guiAlpha, mAlphaSlider->getValue()); } else if (id == "monsterdamage") { @@ -228,7 +228,7 @@ void Setup_Interface::action(const gcn::ActionEvent &event) } else if (id == "visiblenames") { - config.visibleNames = mVisibleNamesCheckBox->isSelected(); + setConfigValue(&Config::visibleNames, mVisibleNamesCheckBox->isSelected()); } else if (id == "pickupchat") { @@ -247,7 +247,7 @@ void Setup_Interface::action(const gcn::ActionEvent &event) } else if (id == "showownname") { - config.showOwnName = mNameCheckBox->isSelected(); + setConfigValue(&Config::showOwnName, mNameCheckBox->isSelected()); } else if (id == "lognpc") { diff --git a/src/gui/setup_video.cpp b/src/gui/setup_video.cpp index efa56cb8..38602a5c 100644 --- a/src/gui/setup_video.cpp +++ b/src/gui/setup_video.cpp @@ -453,7 +453,7 @@ void Setup_Video::cancel() config.windowMode = mVideoSettings.windowMode; - config.customCursor = mCustomCursorEnabled; + setConfigValue(&Config::customCursor, mCustomCursorEnabled); config.particleEffects = mParticleEffectsEnabled; config.opengl = mVideoSettings.openGL; config.disableTransparency = mSDLTransparencyDisabled; @@ -489,7 +489,7 @@ void Setup_Video::action(const gcn::ActionEvent &event) } else if (id == "customcursor") { - config.customCursor = mCustomCursorCheckBox->isSelected(); + setConfigValue(&Config::customCursor, mCustomCursorCheckBox->isSelected()); } else if (id == "particleeffects") { diff --git a/src/gui/widgets/chattab.cpp b/src/gui/widgets/chattab.cpp index 1584eb0a..485de566 100644 --- a/src/gui/widgets/chattab.cpp +++ b/src/gui/widgets/chattab.cpp @@ -102,7 +102,7 @@ void ChatTab::event(Event::Channel channel, const Event &event) // Update the text outline and shadow according to the gui opacity. if (channel == Event::ConfigChannel && event.getType() == Event::ConfigOptionChanged && - event.getString("option") == "guialpha") + event.hasValue(&Config::guiAlpha)) { updateTextFormat(Window::getGuiAlpha()); } diff --git a/src/localplayer.cpp b/src/localplayer.cpp index 7e66632b..e2af5fa3 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -1002,10 +1002,16 @@ void LocalPlayer::event(Event::Channel channel, const Event &event) } else if (channel == Event::ConfigChannel) { - if (event.getType() == Event::ConfigOptionChanged && - event.getString("option") == "showownname") + if (event.getType() == Event::ConfigOptionChanged) { - setShowName(config.showOwnName); + if (event.hasValue(&Config::showOwnName)) + { + setShowName(config.showOwnName); + } + else if (event.hasValue(&Config::visibleNames)) + { + return; + } } } diff --git a/src/resources/theme.cpp b/src/resources/theme.cpp index 1f7322a6..ad686e19 100644 --- a/src/resources/theme.cpp +++ b/src/resources/theme.cpp @@ -205,7 +205,7 @@ void Theme::event(Event::Channel channel, const Event &event) { if (channel == Event::ConfigChannel && event.getType() == Event::ConfigOptionChanged && - event.getString("option") == "guialpha") + event.hasValue(&Config::guiAlpha)) { updateAlpha(); } |