InterfaceObjectConfigurable.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * InterfaceBuilder.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 "CIntObject.h"
  12. #include "../../lib/JsonNode.h"
  13. class CPicture;
  14. class CLabel;
  15. class CToggleGroup;
  16. class CToggleButton;
  17. class CButton;
  18. class CLabelGroup;
  19. class CSlider;
  20. class CAnimImage;
  21. class CShowableAnim;
  22. class CFilledTexture;
  23. #define REGISTER_BUILDER(type, method) registerBuilder(type, std::bind(method, this, std::placeholders::_1))
  24. class InterfaceObjectConfigurable: public CIntObject
  25. {
  26. public:
  27. InterfaceObjectConfigurable(int used=0, Point offset=Point());
  28. InterfaceObjectConfigurable(const JsonNode & config, int used=0, Point offset=Point());
  29. protected:
  30. using BuilderFunction = std::function<std::shared_ptr<CIntObject>(const JsonNode &)>;
  31. void registerBuilder(const std::string &, BuilderFunction);
  32. //must be called after adding callbacks
  33. void build(const JsonNode & config);
  34. void addCallback(const std::string & callbackName, std::function<void(int)> callback);
  35. JsonNode variables;
  36. template<class T>
  37. const std::shared_ptr<T> widget(const std::string & name) const
  38. {
  39. auto iter = widgets.find(name);
  40. if(iter == widgets.end())
  41. return nullptr;
  42. return std::dynamic_pointer_cast<T>(iter->second);
  43. }
  44. //basic serializers
  45. Point readPosition(const JsonNode &) const;
  46. Rect readRect(const JsonNode &) const;
  47. ETextAlignment readTextAlignment(const JsonNode &) const;
  48. SDL_Color readColor(const JsonNode &) const;
  49. EFonts readFont(const JsonNode &) const;
  50. std::string readText(const JsonNode &) const;
  51. std::pair<std::string, std::string> readHintText(const JsonNode &) const;
  52. int readKeycode(const JsonNode &) const;
  53. //basic widgets
  54. std::shared_ptr<CPicture> buildPicture(const JsonNode &) const;
  55. std::shared_ptr<CLabel> buildLabel(const JsonNode &) const;
  56. std::shared_ptr<CToggleGroup> buildToggleGroup(const JsonNode &) const;
  57. std::shared_ptr<CToggleButton> buildToggleButton(const JsonNode &) const;
  58. std::shared_ptr<CButton> buildButton(const JsonNode &) const;
  59. std::shared_ptr<CLabelGroup> buildLabelGroup(const JsonNode &) const;
  60. std::shared_ptr<CSlider> buildSlider(const JsonNode &) const;
  61. std::shared_ptr<CAnimImage> buildImage(const JsonNode &) const;
  62. std::shared_ptr<CShowableAnim> buildAnimation(const JsonNode &) const;
  63. std::shared_ptr<CFilledTexture> buildTexture(const JsonNode &) const;
  64. //composite widgets
  65. std::shared_ptr<CIntObject> buildWidget(JsonNode config) const;
  66. private:
  67. int unnamedObjectId = 0;
  68. std::map<std::string, BuilderFunction> builders;
  69. std::map<std::string, std::shared_ptr<CIntObject>> widgets;
  70. std::map<std::string, std::function<void(int)>> callbacks;
  71. };