From 16f6f636aee6acaf52439643502f5ff9cb4ccffc Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 1 Jun 2015 01:08:30 +0300 Subject: Convert UserColorId enum into strong typed enum. --- src/being/being.cpp | 23 ++++++-- src/being/being.h | 14 ++++- src/being/localplayer.cpp | 2 +- src/being/localplayer.h | 5 +- src/enums/gui/usercolorid.h | 102 +++++++++++++++++----------------- src/gui/palette.h | 7 ++- src/gui/userpalette.cpp | 49 ++++++++-------- src/gui/userpalette.h | 63 +++++++++++++-------- src/gui/widgets/tabs/setup_colors.cpp | 12 ++-- src/gui/windows/minimap.cpp | 2 +- 10 files changed, 163 insertions(+), 116 deletions(-) (limited to 'src') diff --git a/src/being/being.cpp b/src/being/being.cpp index e30a1e477..3503b5259 100644 --- a/src/being/being.cpp +++ b/src/being/being.cpp @@ -2594,11 +2594,16 @@ void Being::drawSpriteAt(Graphics *const graphics, if (!maxHP) maxHP = mInfo->getMaxHP(); - drawHpBar(graphics, maxHP, mHP, mDamageTaken, - UserColorId::MONSTER_HP, UserColorId::MONSTER_HP2, + drawHpBar(graphics, + maxHP, + mHP, + mDamageTaken, + UserColorId::MONSTER_HP, + UserColorId::MONSTER_HP2, x - 50 + mapTileSize / 2 + mInfo->getHpBarOffsetX(), y + mapTileSize - 6 + mInfo->getHpBarOffsetY(), - 2 * 50, 4); + 2 * 50, + 4); } if (mShowOwnHP && mInfo @@ -2614,9 +2619,15 @@ void Being::drawSpriteAt(Graphics *const graphics, } } -void Being::drawHpBar(Graphics *const graphics, const int maxHP, const int hp, - const int damage, const int color1, const int color2, - const int x, const int y, const int width, +void Being::drawHpBar(Graphics *const graphics, + const int maxHP, + const int hp, + const int damage, + const UserColorIdT color1, + const UserColorIdT color2, + const int x, + const int y, + const int width, const int height) const { if (maxHP <= 0 || !userPalette) diff --git a/src/being/being.h b/src/being/being.h index 6e8588213..ebce57c41 100644 --- a/src/being/being.h +++ b/src/being/being.h @@ -25,6 +25,8 @@ #include "enums/being/reachable.h" +#include "enums/gui/usercolorid.h" + #include "enums/simpletypes/move.h" #include "resources/beinginfo.h" @@ -533,9 +535,15 @@ class Being notfinal : public ActorSprite, virtual void drawSpritesSDL(Graphics *const graphics, int posX, int posY) const override final; - void drawHpBar(Graphics *const graphics, const int x, const int y, - const int maxHP, const int hp, const int damage, - const int color1, const int color2, const int width, + void drawHpBar(Graphics *const graphics, + const int maxHP, + const int hp, + const int damage, + const UserColorIdT color1, + const UserColorIdT color2, + const int x, + const int y, + const int width, const int height) const; static void load(); diff --git a/src/being/localplayer.cpp b/src/being/localplayer.cpp index a941dc42c..7eea9c18a 100644 --- a/src/being/localplayer.cpp +++ b/src/being/localplayer.cpp @@ -1025,7 +1025,7 @@ void LocalPlayer::handleStatusEffect(const StatusEffect *const effect, } void LocalPlayer::addMessageToQueue(const std::string &message, - const int color) + const UserColorIdT color) { if (mMessages.size() < 20) mMessages.push_back(MessagePair(message, color)); diff --git a/src/being/localplayer.h b/src/being/localplayer.h index e5ae09ae9..2425f7502 100644 --- a/src/being/localplayer.h +++ b/src/being/localplayer.h @@ -280,7 +280,8 @@ class LocalPlayer final : public Being, void setMap(Map *const map) override final; void addMessageToQueue(const std::string &message, - const int color = UserColorId::EXP_INFO); + const UserColorIdT color + = UserColorId::EXP_INFO); /** * Called when a option (set with config.addListener()) is changed @@ -458,7 +459,7 @@ class LocalPlayer final : public Being, std::vector mStatusEffectIcons; - typedef std::pair MessagePair; + typedef std::pair MessagePair; /** Queued messages*/ std::list mMessages; int mMessageTime; diff --git a/src/enums/gui/usercolorid.h b/src/enums/gui/usercolorid.h index 0a66cf6b9..5087c36f8 100644 --- a/src/enums/gui/usercolorid.h +++ b/src/enums/gui/usercolorid.h @@ -24,57 +24,57 @@ #ifndef ENUMS_GUI_USERCOLORID_H #define ENUMS_GUI_USERCOLORID_H -namespace UserColorId +#include "enums/simpletypes/enumdefines.h" + +enumStart(UserColorId) { - /** List of all colors that are configurable. */ - enum T - { - BEING = 0, - FRIEND, - DISREGARDED, - IGNORED, - ERASED, - PC, - SELF, - GM, - NPC, - MONSTER, - MONSTER_HP, - MONSTER_HP2, - PARTY, - GUILD, - PARTICLE, - PICKUP_INFO, - EXP_INFO, - PLAYER_HP, - PLAYER_HP2, - HIT_PLAYER_MONSTER, - HIT_MONSTER_PLAYER, - HIT_PLAYER_PLAYER, - HIT_CRITICAL, - HIT_LOCAL_PLAYER_MONSTER, - HIT_LOCAL_PLAYER_CRITICAL, - HIT_LOCAL_PLAYER_MISS, - MISS, - PORTAL_HIGHLIGHT, - COLLISION_HIGHLIGHT, - AIR_COLLISION_HIGHLIGHT, - WATER_COLLISION_HIGHLIGHT, - GROUNDTOP_COLLISION_HIGHLIGHT, - WALKABLE_HIGHLIGHT, - ATTACK_RANGE, - ATTACK_RANGE_BORDER, - MONSTER_ATTACK_RANGE, - FLOOR_ITEM_TEXT, - HOME_PLACE, - HOME_PLACE_BORDER, - ROAD_POINT, - NET, - PET, - MERCENARY, - HOMUNCULUS, - USER_COLOR_LAST - }; -} // namespace UserColorId + NO_COLOR = -1, + BEING = 0, + FRIEND, + DISREGARDED, + IGNORED, + ERASED, + PC, + SELF, + GM, + NPC, + MONSTER, + MONSTER_HP, + MONSTER_HP2, + PARTY, + GUILD, + PARTICLE, + PICKUP_INFO, + EXP_INFO, + PLAYER_HP, + PLAYER_HP2, + HIT_PLAYER_MONSTER, + HIT_MONSTER_PLAYER, + HIT_PLAYER_PLAYER, + HIT_CRITICAL, + HIT_LOCAL_PLAYER_MONSTER, + HIT_LOCAL_PLAYER_CRITICAL, + HIT_LOCAL_PLAYER_MISS, + MISS, + PORTAL_HIGHLIGHT, + COLLISION_HIGHLIGHT, + AIR_COLLISION_HIGHLIGHT, + WATER_COLLISION_HIGHLIGHT, + GROUNDTOP_COLLISION_HIGHLIGHT, + WALKABLE_HIGHLIGHT, + ATTACK_RANGE, + ATTACK_RANGE_BORDER, + MONSTER_ATTACK_RANGE, + FLOOR_ITEM_TEXT, + HOME_PLACE, + HOME_PLACE_BORDER, + ROAD_POINT, + NET, + PET, + MERCENARY, + HOMUNCULUS, + USER_COLOR_LAST +} +enumEnd(UserColorId); #endif // ENUMS_GUI_USERCOLORID_H diff --git a/src/gui/palette.h b/src/gui/palette.h index aa7d2ecc8..bee83daf3 100644 --- a/src/gui/palette.h +++ b/src/gui/palette.h @@ -27,6 +27,7 @@ #include "logger.h" #include "enums/gui/gradienttype.h" +#include "enums/gui/usercolorid.h" #include "gui/color.h" @@ -132,8 +133,10 @@ class Palette notfinal int delay; int committedDelay; - void set(const int type0, const Color &color0, - const GradientTypeT grad0, const int delay0) + void set(const int type0, + const Color &color0, + const GradientTypeT grad0, + const int delay0) { type = type0; color = color0; diff --git a/src/gui/userpalette.cpp b/src/gui/userpalette.cpp index 629caca73..bb82b3b78 100644 --- a/src/gui/userpalette.cpp +++ b/src/gui/userpalette.cpp @@ -105,14 +105,14 @@ std::string UserPalette::getConfigName(const std::string &typeName) } UserPalette::UserPalette() : - Palette(UserColorId::USER_COLOR_LAST) + Palette(static_cast(UserColorId::USER_COLOR_LAST)) { - mColors[UserColorId::BEING] = ColorElem(); - mColors[UserColorId::PC] = ColorElem(); - mColors[UserColorId::SELF] = ColorElem(); - mColors[UserColorId::GM] = ColorElem(); - mColors[UserColorId::NPC] = ColorElem(); - mColors[UserColorId::MONSTER] = ColorElem(); + mColors[static_cast(UserColorId::BEING)] = ColorElem(); + mColors[static_cast(UserColorId::PC)] = ColorElem(); + mColors[static_cast(UserColorId::SELF)] = ColorElem(); + mColors[static_cast(UserColorId::GM)] = ColorElem(); + mColors[static_cast(UserColorId::NPC)] = ColorElem(); + mColors[static_cast(UserColorId::MONSTER)] = ColorElem(); // TRANSLATORS: palette color addColor(UserColorId::BEING, 0xffffff, GradientType::STATIC, _("Being")); @@ -248,20 +248,21 @@ UserPalette::~UserPalette() } } -void UserPalette::setColor(const int type, +void UserPalette::setColor(const UserColorIdT type, const int r, const int g, const int b) { - Color &color = mColors[type].color; + Color &color = mColors[static_cast(type)].color; color.r = r; color.g = g; color.b = b; } -void UserPalette::setGradient(const int type, const GradientTypeT grad) +void UserPalette::setGradient(const UserColorIdT type, + const GradientTypeT grad) { - ColorElem *const elem = &mColors[type]; + ColorElem *const elem = &mColors[static_cast(type)]; if (elem->grad != GradientType::STATIC && grad == GradientType::STATIC) { @@ -311,12 +312,15 @@ void UserPalette::rollback() FOR_EACH (Colors::iterator, i, mColors) { if (i->grad != i->committedGrad) - setGradient(i->type, i->committedGrad); + setGradient(static_cast(i->type), i->committedGrad); const Color &committedColor = i->committedColor; - setGradientDelay(i->type, i->committedDelay); - setColor(i->type, committedColor.r, - committedColor.g, committedColor.b); + setGradientDelay(static_cast(i->type), + i->committedDelay); + setColor(static_cast(i->type), + committedColor.r, + committedColor.g, + committedColor.b); if (i->grad == GradientType::PULSE) { @@ -331,12 +335,12 @@ void UserPalette::rollback() int UserPalette::getColorTypeAt(const int i) { if (i < 0 || i >= getNumberOfElements()) - return UserColorId::BEING; + return 0; return mColors[i].type; } -void UserPalette::addColor(const unsigned type, +void UserPalette::addColor(const UserColorIdT type, const unsigned rgb, GradientTypeT grad, const std::string &text, @@ -345,10 +349,10 @@ void UserPalette::addColor(const unsigned type, const unsigned maxType = sizeof(ColorTypeNames) / sizeof(ColorTypeNames[0]); - if (type >= maxType) + if (static_cast(type) >= maxType) return; - const std::string &configName = ColorTypeNames[type]; + const std::string &configName = ColorTypeNames[static_cast(type)]; char buffer[20]; snprintf(buffer, sizeof(buffer), "0x%06x", rgb); buffer[19] = 0; @@ -365,11 +369,12 @@ void UserPalette::addColor(const unsigned type, configName + "Gradient", static_cast(grad))); delay = config.getValueInt(configName + "Delay", delay); - mColors[type].set(type, trueCol, grad, delay); - mColors[type].text = text; + mColors[static_cast(type)].set(static_cast(type), + trueCol, grad, delay); + mColors[static_cast(type)].text = text; if (grad != GradientType::STATIC) - mGradVector.push_back(&mColors[type]); + mGradVector.push_back(&mColors[static_cast(type)]); } int UserPalette::getIdByChar(const signed char c, bool &valid) const diff --git a/src/gui/userpalette.h b/src/gui/userpalette.h index 525a18492..22203560c 100644 --- a/src/gui/userpalette.h +++ b/src/gui/userpalette.h @@ -24,6 +24,8 @@ #ifndef GUI_USERPALETTE_H #define GUI_USERPALETTE_H +#include "enums/gui/usercolorid.h" + #include "gui/palette.h" #include "gui/models/listmodel.h" @@ -53,10 +55,10 @@ class UserPalette final : public Palette, public ListModel * * @return the requested committed color */ - inline const Color &getCommittedColor(const int type) + inline const Color &getCommittedColor(const UserColorIdT type) const A_WARN_UNUSED { - return mColors[type].committedColor; + return mColors[static_cast(type)].committedColor; } /** @@ -66,8 +68,9 @@ class UserPalette final : public Palette, public ListModel * * @return the requested test color */ - inline const Color &getTestColor(const int type) const A_WARN_UNUSED - { return mColors[type].testColor; } + inline const Color &getTestColor(const UserColorIdT type) + const A_WARN_UNUSED + { return mColors[static_cast(type)].testColor; } /** * Sets the test color associated with the specified type. @@ -75,8 +78,9 @@ class UserPalette final : public Palette, public ListModel * @param type the color type requested * @param color the color that should be tested */ - inline void setTestColor(const int type, const Color &color) - { mColors[type].testColor = color; } + inline void setTestColor(const UserColorIdT type, + const Color &color) + { mColors[static_cast(type)].testColor = color; } /** * Sets the color for the specified type. @@ -86,22 +90,27 @@ class UserPalette final : public Palette, public ListModel * @param g green component * @param b blue component */ - void setColor(const int type, const int r, const int g, const int b); + void setColor(const UserColorIdT type, + const int r, + const int g, + const int b); /** * Sets the gradient type for the specified color. * * @param grad gradient type to set */ - void setGradient(const int type, const GradientTypeT grad); + void setGradient(const UserColorIdT type, + const GradientTypeT grad); /** * Sets the gradient delay for the specified color. * * @param grad gradient type to set */ - void setGradientDelay(const int type, const int delay) - { mColors[type].delay = delay; } + void setGradientDelay(const UserColorIdT type, + const int delay) + { mColors[static_cast(type)].delay = delay; } /** * Returns the number of colors known. @@ -150,16 +159,17 @@ class UserPalette final : public Palette, public ListModel * * @return the requested color */ - inline const Color &getColor(int type, + inline const Color &getColor(UserColorIdT type, const int alpha = 255) A_WARN_UNUSED { - if (type >= static_cast(mColors.size()) || type < 0) + if (static_cast(type) >= mColors.size()) { logger->log("incorrect color request type: %d from %u", - type, static_cast(mColors.size())); - type = 0; + static_cast(type), + static_cast(mColors.size())); + type = UserColorId::BEING; } - Color* col = &mColors[type].color; + Color* col = &mColors[static_cast(type)].color; col->a = alpha; return *col; } @@ -173,8 +183,9 @@ class UserPalette final : public Palette, public ListModel * * @return the gradient type of the color with the given index */ - inline GradientTypeT getGradientType(const int type) const A_WARN_UNUSED - { return mColors[type].grad; } + inline GradientTypeT getGradientType(const UserColorId type) + const A_WARN_UNUSED + { return mColors[static_cast(type)].grad; } /** * Gets the gradient delay for the specified type. @@ -183,13 +194,15 @@ class UserPalette final : public Palette, public ListModel * * @return the gradient delay of the color with the given index */ - inline int getGradientDelay(const int type) const A_WARN_UNUSED - { return mColors[type].delay; } + inline int getGradientDelay(const UserColorIdT type) + const A_WARN_UNUSED + { return mColors[static_cast(type)].delay; } - inline const Color &getColorWithAlpha(const int type) A_WARN_UNUSED + inline const Color &getColorWithAlpha(const UserColorIdT type) + A_WARN_UNUSED { - Color *const col = &mColors[type].color; - col->a = mColors[type].delay; + Color *const col = &mColors[static_cast(type)].color; + col->a = mColors[static_cast(type)].delay; return *col; } @@ -230,8 +243,10 @@ class UserPalette final : public Palette, public ListModel * @param rgb default color if not found in config * @param text identifier of color */ - void addColor(const unsigned type, const unsigned rgb, - GradientTypeT grad, const std::string &text, + void addColor(const UserColorIdT type, + const unsigned rgb, + GradientTypeT grad, + const std::string &text, int delay = GRADIENT_DELAY); }; diff --git a/src/gui/widgets/tabs/setup_colors.cpp b/src/gui/widgets/tabs/setup_colors.cpp index 1ba6461ea..f7a2dcfd6 100644 --- a/src/gui/widgets/tabs/setup_colors.cpp +++ b/src/gui/widgets/tabs/setup_colors.cpp @@ -250,7 +250,8 @@ void Setup_Colors::valueChanged(const SelectionEvent &event A_UNUSED) return; mSelected = mColorBox->getSelected(); - const int type = userPalette->getColorTypeAt(mSelected); + const UserColorIdT type = static_cast( + userPalette->getColorTypeAt(mSelected)); const Color *col = &userPalette->getColor(type); const GradientTypeT grad = userPalette->getGradientType(type); const int delay = userPalette->getGradientDelay(type); @@ -380,7 +381,8 @@ void Setup_Colors::cancel() return; userPalette->rollback(); - const int type = userPalette->getColorTypeAt(mSelected); + const UserColorIdT type = static_cast( + userPalette->getColorTypeAt(mSelected)); const Color *const col = &userPalette->getColor(type); mGradTypeSlider->setValue(static_cast( userPalette->getGradientType(type))); @@ -397,7 +399,8 @@ void Setup_Colors::updateGradType() return; mSelected = mColorBox->getSelected(); - const int type = userPalette->getColorTypeAt(mSelected); + const UserColorIdT type = static_cast( + userPalette->getColorTypeAt(mSelected)); const GradientTypeT grad = userPalette->getGradientType(type); mGradTypeText->setCaption( @@ -427,7 +430,8 @@ void Setup_Colors::updateColor() if (mSelected == -1 || !userPalette) return; - const int type = userPalette->getColorTypeAt(mSelected); + const UserColorIdT type = static_cast( + userPalette->getColorTypeAt(mSelected)); const GradientTypeT grad = static_cast( static_cast(mGradTypeSlider->getValue())); const int delay = static_cast(mGradDelaySlider->getValue()); diff --git a/src/gui/windows/minimap.cpp b/src/gui/windows/minimap.cpp index 2f73d50e6..ea22db3f4 100644 --- a/src/gui/windows/minimap.cpp +++ b/src/gui/windows/minimap.cpp @@ -312,7 +312,7 @@ void Minimap::draw2(Graphics *const graphics) continue; int dotSize = 2; - int type = UserColorId::PC; + UserColorIdT type = UserColorId::PC; if (being == localPlayer) { -- cgit v1.2.3-60-g2f50