diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-01-20 10:26:22 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-01-20 10:51:24 +0100 |
commit | db9b9f316d7bdcb9504092908bb18e82fc21de2f (patch) | |
tree | 3f7bb1577c5210a9523fd240556cf840665f3efb /src/gui/widgets/window.cpp | |
parent | 0d1024b64155a05f45f247ad57d0f444db01c1e2 (diff) | |
download | mana-db9b9f316d7bdcb9504092908bb18e82fc21de2f.tar.gz mana-db9b9f316d7bdcb9504092908bb18e82fc21de2f.tar.bz2 mana-db9b9f316d7bdcb9504092908bb18e82fc21de2f.tar.xz mana-db9b9f316d7bdcb9504092908bb18e82fc21de2f.zip |
Made client config statically typed
This makes accessing the config values much faster, since it no longer
needs to do a lookup nor string conversion, which means we could remove
some needless copying of the values.
Overall it makes it easier to find out where settings are used and it
puts the defaults along with the declaration.
Options with default values are no longer saved to the config file. This
does not include unrecognized options, which are kept around to provide
some compatibility with older clients.
While most basic options have kept the same name, more complicated
settings like window geometry, shortcuts, outfits, etc. now have their
own XML elements. Older clients will ignore these and erase them when
saving the configuration.
Diffstat (limited to 'src/gui/widgets/window.cpp')
-rw-r--r-- | src/gui/widgets/window.cpp | 79 |
1 files changed, 36 insertions, 43 deletions
diff --git a/src/gui/widgets/window.cpp b/src/gui/widgets/window.cpp index 050e31da..8bf9d081 100644 --- a/src/gui/widgets/window.cpp +++ b/src/gui/widgets/window.cpp @@ -37,6 +37,9 @@ #include <guichan/exception.hpp> #include <guichan/focushandler.hpp> +#include <algorithm> +#include <cassert> + int Window::instances = 0; int Window::mouseResize = 0; @@ -503,33 +506,29 @@ void Window::mouseDragged(gcn::MouseEvent &event) void Window::loadWindowState() { - const std::string &name = mWindowName; - assert(!name.empty()); + assert(!mWindowName.empty()); + + constexpr WindowState defaultState; + auto s = config.windows.find(mWindowName); + const WindowState state = s == config.windows.end() ? defaultState + : s->second; - setPosition((int) config.getValue(name + "WinX", mDefaultX), - (int) config.getValue(name + "WinY", mDefaultY)); + setPosition(state.x.value_or(mDefaultX), + state.y.value_or(mDefaultY)); if (mSaveVisible) - setVisible((bool) config.getValue(name + "Visible", mDefaultVisible)); + setVisible(state.visible.value_or(mDefaultVisible)); if (mStickyButton) - setSticky((bool) config.getValue(name + "Sticky", isSticky())); + setSticky(state.sticky.value_or(isSticky())); if (mGrip) { - int width = (int) config.getValue(name + "WinWidth", mDefaultWidth); - int height = (int) config.getValue(name + "WinHeight", mDefaultHeight); - - if (getMinWidth() > width) - width = getMinWidth(); - else if (getMaxWidth() < width) - width = getMaxWidth(); - if (getMinHeight() > height) - height = getMinHeight(); - else if (getMaxHeight() < height) - height = getMaxHeight(); - - setSize(width, height); + const int width = state.width.value_or(mDefaultWidth); + const int height = state.height.value_or(mDefaultHeight); + + setSize(std::clamp(width, getMinWidth(), getMaxWidth()), + std::clamp(height, getMinHeight(), getMaxHeight())); } else { @@ -540,35 +539,29 @@ void Window::loadWindowState() ensureOnScreen(); } -void Window::saveWindowState() +void Window::saveWindowState() const { // Saving X, Y and Width and Height for resizables in the config - if (!mWindowName.empty() && mWindowName != "window") - { - config.setValue(mWindowName + "WinX", getX()); - config.setValue(mWindowName + "WinY", getY()); + if (mWindowName.empty()) + return; - if (mSaveVisible) - config.setValue(mWindowName + "Visible", isVisible()); + WindowState state; + state.x = getX(); + state.y = getY(); - if (mStickyButton) - config.setValue(mWindowName + "Sticky", isSticky()); + if (mSaveVisible) + state.visible = isVisible(); - if (mGrip) - { - if (getMinWidth() > getWidth()) - setWidth(getMinWidth()); - else if (getMaxWidth() < getWidth()) - setWidth(getMaxWidth()); - if (getMinHeight() > getHeight()) - setHeight(getMinHeight()); - else if (getMaxHeight() < getHeight()) - setHeight(getMaxHeight()); - - config.setValue(mWindowName + "WinWidth", getWidth()); - config.setValue(mWindowName + "WinHeight", getHeight()); - } + if (mStickyButton) + state.sticky = isSticky(); + + if (mGrip) + { + state.width = getWidth(); + state.height = getHeight(); } + + config.windows[mWindowName] = state; } void Window::setDefaultSize(int defaultX, int defaultY, @@ -701,7 +694,7 @@ int Window::getResizeHandles(gcn::MouseEvent &event) int Window::getGuiAlpha() { - float alpha = std::max(config.getFloatValue("guialpha"), + float alpha = std::max(config.guiAlpha, Theme::instance()->getMinimumOpacity()); return (int) (alpha * 255.0f); } |