summaryrefslogtreecommitdiff
path: root/src/resources/userpalette.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-01-20 10:26:22 +0100
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-01-20 10:51:24 +0100
commitdb9b9f316d7bdcb9504092908bb18e82fc21de2f (patch)
tree3f7bb1577c5210a9523fd240556cf840665f3efb /src/resources/userpalette.cpp
parent0d1024b64155a05f45f247ad57d0f444db01c1e2 (diff)
downloadmana-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/resources/userpalette.cpp')
-rw-r--r--src/resources/userpalette.cpp107
1 files changed, 39 insertions, 68 deletions
diff --git a/src/resources/userpalette.cpp b/src/resources/userpalette.cpp
index 12e4d5bc..55935d8c 100644
--- a/src/resources/userpalette.cpp
+++ b/src/resources/userpalette.cpp
@@ -27,52 +27,27 @@
#include "utils/gettext.h"
#include "utils/stringutils.h"
-static const std::string ColorTypeNames[] = {
- "ColorBeing",
- "ColorPlayer",
- "ColorSelf",
- "ColorGM",
- "ColorNPC",
- "ColorMonster",
- "ColorParty",
- "ColorGuild",
- "ColorParticle",
- "ColorExperience",
- "ColorPickup",
- "ColorHitPlayerMonster",
- "ColorHitMonsterPlayer",
- "ColorHitCritical",
- "ColorHitLocalPlayerMonster",
- "ColorHitLocalPlayerCritical",
- "ColorHitLocalPlayerMiss",
- "ColorMiss"
+static constexpr const char *ColorTypeNames[] = {
+ "Being",
+ "Player",
+ "Self",
+ "GM",
+ "NPC",
+ "Monster",
+ "Party",
+ "Guild",
+ "Particle",
+ "Experience",
+ "Pickup",
+ "HitPlayerMonster",
+ "HitMonsterPlayer",
+ "HitCritical",
+ "HitLocalPlayerMonster",
+ "HitLocalPlayerCritical",
+ "HitLocalPlayerMiss",
+ "Miss"
};
-std::string UserPalette::getConfigName(const std::string &typeName)
-{
- std::string res = "Color" + typeName;
-
- int pos = 5;
- for (size_t i = 0; i < typeName.length(); i++)
- {
- if (i == 0 || typeName[i] == '_')
- {
- if (i > 0)
- i++;
-
- res[pos] = typeName[i];
- }
- else
- {
- res[pos] = tolower(typeName[i]);
- }
- pos++;
- }
- res.erase(pos, res.length() - pos);
-
- return res;
-}
-
UserPalette::UserPalette():
Palette(USER_COLOR_LAST)
{
@@ -110,20 +85,20 @@ UserPalette::UserPalette():
UserPalette::~UserPalette()
{
+ config.colors.clear();
+
+ // TODO: Don't write out colors when they have the default value
for (auto &color : mColors)
{
- const std::string &configName = ColorTypeNames[color.type];
- config.setValue(configName + "Gradient", color.committedGrad);
+ auto &configColor = config.colors[ColorTypeNames[color.type]];
- if (color.grad != STATIC)
- config.setValue(configName + "Delay", color.delay);
+ configColor.gradient = color.committedGrad;
+
+ if (color.grad != STATIC && color.delay != GRADIENT_DELAY)
+ configColor.delay = color.delay;
if (color.grad == STATIC || color.grad == PULSE)
- {
- char buffer[20];
- snprintf(buffer, 20, "0x%06x", color.getRGB());
- config.setValue(configName, std::string(buffer));
- }
+ configColor.color = strprintf("0x%06x", color.getRGB());
}
}
@@ -215,23 +190,19 @@ int UserPalette::getColorTypeAt(int i)
return mColors[i].type;
}
-void UserPalette::addColor(int type, int rgb, Palette::GradientType grad,
+void UserPalette::addColor(int type, unsigned rgb, GradientType grad,
const std::string &text, int delay)
{
- const std::string &configName = ColorTypeNames[type];
- char buffer[20];
- snprintf(buffer, 20, "0x%06x", rgb);
- const std::string rgbString = config.getValue(configName,
- std::string(buffer));
- unsigned int rgbValue = 0;
- if (rgbString.length() == 8 && rgbString[0] == '0' && rgbString[1] == 'x')
- rgbValue = atox(rgbString);
- else
- rgbValue = atoi(rgbString.c_str());
- gcn::Color trueCol = rgbValue;
- grad = (GradientType) config.getValue(configName + "Gradient", grad);
- delay = (int) config.getValue(configName + "Delay", delay);
- mColors[type].set(type, trueCol, grad, delay);
+ auto colorIt = config.colors.find(ColorTypeNames[type]);
+ if (colorIt != config.colors.end())
+ {
+ const UserColor &userColor = colorIt->second;
+ rgb = atox(userColor.color);
+ grad = static_cast<GradientType>(userColor.gradient);
+ delay = userColor.delay.value_or(GRADIENT_DELAY);
+ }
+
+ mColors[type].set(type, gcn::Color(rgb), grad, delay);
mColors[type].text = text;
if (grad != STATIC)