CBonusTypeHandler.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "IHandlerBase.h"
  13. #include "HeroBonus.h"
  14. class JsonNode;
  15. typedef Bonus::BonusType BonusTypeID;
  16. class MacroString
  17. {
  18. struct Item
  19. {
  20. enum ItemType
  21. {
  22. STRING, MACRO
  23. };
  24. Item(ItemType _type, std::string _value): type(_type), value(_value){};
  25. ItemType type;
  26. std::string value; //constant string or macro name
  27. };
  28. std::vector<Item> items;
  29. public:
  30. typedef std::function <std::string(const std::string&)> GetValue;
  31. MacroString(){};
  32. MacroString(const std::string &format);
  33. std::string build(const GetValue& getValue) const;
  34. };
  35. class DLL_LINKAGE CBonusType
  36. {
  37. public:
  38. CBonusType();
  39. ~CBonusType();
  40. template <typename Handler> void serialize(Handler &h, const int version)
  41. {
  42. h & icon & nameTemplate & descriptionTemplate & hidden;
  43. if (!h.saving)
  44. buildMacros();
  45. }
  46. protected:
  47. private:
  48. void buildMacros();
  49. MacroString name, description;
  50. friend class CBonusTypeHandler;
  51. std::string icon;
  52. std::string nameTemplate, descriptionTemplate;
  53. bool hidden;
  54. };
  55. class DLL_LINKAGE CBonusTypeHandler : public IBonusTypeHandler
  56. {
  57. public:
  58. CBonusTypeHandler();
  59. virtual ~CBonusTypeHandler();
  60. std::string bonusToString(const std::shared_ptr<Bonus>& bonus, const IBonusBearer *bearer, bool description) const override;
  61. std::string bonusToGraphics(const std::shared_ptr<Bonus>& bonus) const override;
  62. void load();
  63. void load(const JsonNode& config);
  64. template <typename Handler> void serialize(Handler &h, const int version)
  65. {
  66. h & bonusTypes;
  67. }
  68. private:
  69. void loadItem(const JsonNode &source, CBonusType &dest);
  70. std::vector<CBonusType> bonusTypes; //index = BonusTypeID
  71. };