diff options
author | Andrei Karas <akaras@inbox.ru> | 2015-08-15 13:58:32 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2015-08-16 01:52:14 +0300 |
commit | 41cc92f73e39cec5dfea6b1164176610cccc7df4 (patch) | |
tree | 22fd3d388084d2c61fe80f0441c3c30ab8ffd33c /src/resources | |
parent | 7aa637abc8b0bca35aacdb9492e65f557ed32038 (diff) | |
download | manaplus-41cc92f73e39cec5dfea6b1164176610cccc7df4.tar.gz manaplus-41cc92f73e39cec5dfea6b1164176610cccc7df4.tar.bz2 manaplus-41cc92f73e39cec5dfea6b1164176610cccc7df4.tar.xz manaplus-41cc92f73e39cec5dfea6b1164176610cccc7df4.zip |
Add strong typed int for item color.
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/beinginfo.cpp | 4 | ||||
-rw-r--r-- | src/resources/beinginfo.h | 7 | ||||
-rw-r--r-- | src/resources/db/colordb.cpp | 36 | ||||
-rw-r--r-- | src/resources/db/colordb.h | 29 | ||||
-rw-r--r-- | src/resources/iteminfo.cpp | 20 | ||||
-rw-r--r-- | src/resources/iteminfo.h | 17 |
6 files changed, 62 insertions, 51 deletions
diff --git a/src/resources/beinginfo.cpp b/src/resources/beinginfo.cpp index f63e65134..1f1033e3c 100644 --- a/src/resources/beinginfo.cpp +++ b/src/resources/beinginfo.cpp @@ -206,12 +206,12 @@ void BeingInfo::setColorsList(const std::string &name) mColors = ColorDB::getColorsList(name); } -std::string BeingInfo::getColor(const int idx) const +std::string BeingInfo::getColor(const ItemColor idx) const { if (!mColors) return std::string(); - const std::map <int, ColorDB::ItemColor>::const_iterator + const std::map <ItemColor, ColorDB::ItemColorData>::const_iterator it = mColors->find(idx); if (it == mColors->end()) return std::string(); diff --git a/src/resources/beinginfo.h b/src/resources/beinginfo.h index 0a6330470..3429515e0 100644 --- a/src/resources/beinginfo.h +++ b/src/resources/beinginfo.h @@ -28,6 +28,7 @@ #include "enums/resources/map/blocktype.h" #include "enums/simpletypes/beingtypeid.h" +#include "enums/simpletypes/itemcolor.h" #include "resources/beingmenuitem.h" #include "resources/cursor.h" @@ -38,7 +39,7 @@ struct Attack; namespace ColorDB { - class ItemColor; + class ItemColorData; } typedef std::map<int, Attack*> Attacks; @@ -312,7 +313,7 @@ class BeingInfo final void setColorsList(const std::string &name); - std::string getColor(const int idx) const A_WARN_UNUSED; + std::string getColor(const ItemColor idx) const A_WARN_UNUSED; void addMenu(const std::string &name, const std::string &command); @@ -332,7 +333,7 @@ class BeingInfo final std::vector<BeingMenuItem> mMenu; unsigned char mBlockWalkMask; BlockType::BlockType mBlockType; - const std::map <int, ColorDB::ItemColor> *mColors; + const std::map <ItemColor, ColorDB::ItemColorData> *mColors; int mTargetOffsetX; int mTargetOffsetY; int mNameOffsetX; diff --git a/src/resources/db/colordb.cpp b/src/resources/db/colordb.cpp index 1b636e407..62f7beef6 100644 --- a/src/resources/db/colordb.cpp +++ b/src/resources/db/colordb.cpp @@ -41,7 +41,7 @@ void ColorDB::load() if (mLoaded) unload(); - std::map<int, ItemColor> colors; + std::map<ItemColor, ItemColorData> colors; ColorListsIterator it = mColorLists.find("hair"); if (it != mColorLists.end()) colors = it->second; @@ -68,7 +68,7 @@ void ColorDB::load() } void ColorDB::loadHair(const std::string &fileName, - std::map<int, ItemColor> &colors) + std::map<ItemColor, ItemColorData> &colors) { XML::Document *doc = new XML::Document(fileName, UseResman_true, @@ -78,8 +78,8 @@ void ColorDB::loadHair(const std::string &fileName, if (!root || !xmlNameEqual(root, "colors")) { logger->log("ColorDB: Failed to find hair colors file."); - if (colors.find(0) == colors.end()) - colors[0] = ItemColor(0, "", ""); + if (colors.find(ItemColor_zero) == colors.end()) + colors[ItemColor_zero] = ItemColorData(ItemColor_zero, "", ""); delete doc; return; } @@ -95,12 +95,13 @@ void ColorDB::loadHair(const std::string &fileName, } else if (xmlNameEqual(node, "color")) { - const int id = XML::getProperty(node, "id", 0); + const ItemColor id = fromInt(XML::getProperty( + node, "id", 0), ItemColor); if (colors.find(id) != colors.end()) - logger->log("ColorDB: Redefinition of dye ID %d", id); + logger->log("ColorDB: Redefinition of dye ID %d", toInt(id, int)); - colors[id] = ItemColor(id, XML::langProperty(node, "name", ""), + colors[id] = ItemColorData(id, XML::langProperty(node, "name", ""), XML::getProperty(node, "value", "#FFFFFF")); } } @@ -135,7 +136,7 @@ void ColorDB::loadColorLists(const std::string &fileName) if (name.empty()) continue; - std::map <int, ItemColor> colors; + std::map <ItemColor, ItemColorData> colors; const ColorListsIterator it = mColorLists.find(name); if (it != mColorLists.end()) @@ -145,11 +146,14 @@ void ColorDB::loadColorLists(const std::string &fileName) { if (xmlNameEqual(colorNode, "color")) { - ItemColor c(XML::getProperty(colorNode, "id", -1), - XML::langProperty(colorNode, "name", ""), - XML::getProperty(colorNode, "value", "")); - if (c.id > -1) - colors[c.id] = c; + const int id = XML::getProperty(colorNode, "id", -1); + if (id > -1) + { + ItemColorData c(fromInt(id, ItemColor), + XML::langProperty(colorNode, "name", ""), + XML::getProperty(colorNode, "value", "")); + colors[c.id] = c; + } } } mColorLists[name] = colors; @@ -166,7 +170,7 @@ void ColorDB::unload() mLoaded = false; } -std::string &ColorDB::getHairColorName(const int id) +std::string &ColorDB::getHairColorName(const ItemColor id) { if (!mLoaded) load(); @@ -182,7 +186,7 @@ std::string &ColorDB::getHairColorName(const int id) if (i == (*it).second.end()) { - logger->log("ColorDB: Error, unknown dye ID# %d", id); + logger->log("ColorDB: Error, unknown dye ID# %d", toInt(id, int)); return mFail; } else @@ -196,7 +200,7 @@ int ColorDB::getHairSize() return mHairColorsSize; } -const std::map <int, ColorDB::ItemColor> +const std::map <ItemColor, ColorDB::ItemColorData> *ColorDB::getColorsList(const std::string &name) { const ColorListsIterator it = mColorLists.find(name); diff --git a/src/resources/db/colordb.h b/src/resources/db/colordb.h index c2923f382..6468b76e3 100644 --- a/src/resources/db/colordb.h +++ b/src/resources/db/colordb.h @@ -22,6 +22,8 @@ #ifndef RESOURCES_DB_COLORDB_H #define RESOURCES_DB_COLORDB_H +#include "enums/simpletypes/itemcolor.h" + #include <map> #include <string> @@ -32,24 +34,25 @@ */ namespace ColorDB { - class ItemColor final + class ItemColorData final { public: - ItemColor() : - id(0), + ItemColorData() : + id(ItemColor_zero), name(), color() { } - ItemColor(const int id0, const std::string &name0, - const std::string &color0) : + ItemColorData(const ItemColor id0, + const std::string &name0, + const std::string &color0) : id(id0), name(name0), color(color0) { } - int id; + ItemColor id; std::string name; std::string color; }; @@ -63,7 +66,7 @@ namespace ColorDB * Loads the color data from <code>colors.xml</code>. */ void loadHair(const std::string &fileName, - std::map<int, ItemColor> &colors); + std::map<ItemColor, ItemColorData> &colors); void loadColorLists(const std::string &fileName); @@ -72,17 +75,19 @@ namespace ColorDB */ void unload(); - std::string &getHairColorName(const int id) A_WARN_UNUSED; + std::string &getHairColorName(const ItemColor id) A_WARN_UNUSED; int getHairSize() A_WARN_UNUSED; - const std::map <int, ItemColor> *getColorsList(const std::string - &name) A_WARN_UNUSED; + const std::map <ItemColor, ItemColorData> *getColorsList(const std::string + &name) + A_WARN_UNUSED; // Color DB - typedef std::map<int, ItemColor> Colors; + typedef std::map<ItemColor, ItemColorData> Colors; typedef Colors::iterator ColorIterator; - typedef std::map <std::string, std::map <int, ItemColor> > ColorLists; + typedef std::map <std::string, std::map <ItemColor, ItemColorData> > + ColorLists; typedef ColorLists::iterator ColorListsIterator; } // namespace ColorDB diff --git a/src/resources/iteminfo.cpp b/src/resources/iteminfo.cpp index 768825f44..c439be5c5 100644 --- a/src/resources/iteminfo.cpp +++ b/src/resources/iteminfo.cpp @@ -201,12 +201,12 @@ void ItemInfo::setColorsList(const std::string &name) } } -std::string ItemInfo::getDyeColorsString(const int color) const +std::string ItemInfo::getDyeColorsString(const ItemColor color) const { if (!mColors || mColorList.empty()) return ""; - const std::map <int, ColorDB::ItemColor>::const_iterator + const std::map <ItemColor, ColorDB::ItemColorData>::const_iterator it = mColors->find(color); if (it == mColors->end()) return ""; @@ -214,23 +214,23 @@ std::string ItemInfo::getDyeColorsString(const int color) const return it->second.color; } -const std::string ItemInfo::getDescription(const unsigned char color) const +const std::string ItemInfo::getDescription(const ItemColor color) const { return replaceColors(mDescription, color); } -const std::string ItemInfo::getName(const unsigned char color) const +const std::string ItemInfo::getName(const ItemColor color) const { return replaceColors(mName, color); } const std::string ItemInfo::replaceColors(std::string str, - const unsigned char color) const + const ItemColor color) const { std::string name; if (mColors && !mColorList.empty()) { - const std::map <int, ColorDB::ItemColor>::const_iterator + const std::map <ItemColor, ColorDB::ItemColorData>::const_iterator it = mColors->find(color); if (it == mColors->end()) name = "unknown"; @@ -369,24 +369,24 @@ void ItemInfo::setSprite(const std::string &animationFile, mAnimationFiles[static_cast<int>(gender) + race * 4] = animationFile; } -std::string ItemInfo::getColorName(const int idx) const +std::string ItemInfo::getColorName(const ItemColor idx) const { if (!mColors) return std::string(); - const std::map <int, ColorDB::ItemColor>::const_iterator + const std::map <ItemColor, ColorDB::ItemColorData>::const_iterator it = mColors->find(idx); if (it == mColors->end()) return std::string(); return it->second.name; } -std::string ItemInfo::getColor(const int idx) const +std::string ItemInfo::getColor(const ItemColor idx) const { if (!mColors) return std::string(); - const std::map <int, ColorDB::ItemColor>::const_iterator + const std::map <ItemColor, ColorDB::ItemColorData>::const_iterator it = mColors->find(idx); if (it == mColors->end()) return std::string(); diff --git a/src/resources/iteminfo.h b/src/resources/iteminfo.h index 4d40390d9..976d30c1d 100644 --- a/src/resources/iteminfo.h +++ b/src/resources/iteminfo.h @@ -26,6 +26,7 @@ #include "enums/being/gender.h" #include "enums/simpletypes/beingtypeid.h" +#include "enums/simpletypes/itemcolor.h" #include "resources/cursor.h" #include "resources/itemtype.h" @@ -34,7 +35,7 @@ namespace ColorDB { - class ItemColor; + class ItemColorData; } // sprite, <itemfrom, itemto> @@ -69,7 +70,7 @@ class ItemInfo final const std::string &getName() const A_WARN_UNUSED { return mName; } - const std::string getName(const unsigned char color) + const std::string getName(const ItemColor color) const A_WARN_UNUSED; void setDisplay(const SpriteDisplay &display) @@ -84,7 +85,7 @@ class ItemInfo final const std::string &getDescription() const A_WARN_UNUSED { return mDescription; } - const std::string getDescription(const unsigned char color) + const std::string getDescription(const ItemColor color) const A_WARN_UNUSED; void setEffect(const std::string &effect) @@ -238,7 +239,7 @@ class ItemInfo final const SpriteToItemMap *getSpriteToItemReplaceMap(const int directions) const A_WARN_UNUSED; - std::string getDyeColorsString(const int color) const A_WARN_UNUSED; + std::string getDyeColorsString(const ItemColor color) const A_WARN_UNUSED; void setColorsList(const std::string &name); @@ -246,7 +247,7 @@ class ItemInfo final { return !mColorList.empty(); } const std::string replaceColors(std::string str, - const unsigned char color) + const ItemColor color) const A_WARN_UNUSED; void setPickupCursor(const std::string &cursor) @@ -267,9 +268,9 @@ class ItemInfo final int getColorsSize() const { return mColors ? static_cast<int>(mColors->size()) : 0; } - std::string getColorName(const int idx) const; + std::string getColorName(const ItemColor idx) const; - std::string getColor(const int idx) const; + std::string getColor(const ItemColor idx) const; int mDrawBefore[10]; int mDrawAfter[10]; @@ -317,7 +318,7 @@ class ItemInfo final /** Stores the names of sounds to be played at certain event. */ std::map <ItemSoundEvent::Type, SoundInfoVect> mSounds; std::map <int, int> mTags; - const std::map <int, ColorDB::ItemColor> *mColors; + const std::map <ItemColor, ColorDB::ItemColorData> *mColors; std::string mColorList; int mHitEffectId; int mCriticalHitEffectId; |