InterfaceObjectConfigurable.h 2.8 KB

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