2
0

CBonusTypeHandler.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * CBonusTypeHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "IBonusTypeHandler.h"
  12. #include "HeroBonus.h"
  13. class JsonNode;
  14. typedef Bonus::BonusType BonusTypeID;
  15. class MacroString
  16. {
  17. struct Item
  18. {
  19. enum ItemType
  20. {
  21. STRING, MACRO
  22. };
  23. Item(ItemType _type, std::string _value): type(_type), value(_value){};
  24. ItemType type;
  25. std::string value; //consant string or macro name
  26. };
  27. std::vector<Item> items;
  28. public:
  29. typedef std::function <std::string(const std::string&)> GetValue;
  30. MacroString(){};
  31. MacroString(const std::string &format);
  32. std::string build(const GetValue& getValue) const;
  33. };
  34. class DLL_LINKAGE CBonusType
  35. {
  36. public:
  37. CBonusType();
  38. ~CBonusType();
  39. template <typename Handler> void serialize(Handler &h, const int version)
  40. {
  41. h & icon & nameTemplate & descriptionTemplate & hidden;
  42. if (!h.saving)
  43. buildMacros();
  44. }
  45. protected:
  46. private:
  47. void buildMacros();
  48. MacroString name, description;
  49. friend class CBonusTypeHandler;
  50. std::string icon;
  51. std::string nameTemplate, descriptionTemplate;
  52. bool hidden;
  53. };
  54. class DLL_LINKAGE CBonusTypeHandler : public IBonusTypeHandler
  55. {
  56. public:
  57. CBonusTypeHandler();
  58. virtual ~CBonusTypeHandler();
  59. std::string bonusToString(Bonus *bonus, const IBonusBearer *bearer, bool description) const override;
  60. std::string bonusToGraphics(Bonus *bonus) const override;
  61. void load();
  62. void load(const JsonNode& config);
  63. template <typename Handler> void serialize(Handler &h, const int version)
  64. {
  65. h & bonusTypes;
  66. }
  67. private:
  68. void loadItem(const JsonNode &source, CBonusType &dest);
  69. std::vector<CBonusType> bonusTypes; //index = BonusTypeID
  70. };