InterfaceObjectConfigurable.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. void deleteWidget(const std::string & name);
  45. //basic serializers
  46. Point readPosition(const JsonNode &) const;
  47. Rect readRect(const JsonNode &) const;
  48. ETextAlignment readTextAlignment(const JsonNode &) const;
  49. SDL_Color readColor(const JsonNode &) const;
  50. EFonts readFont(const JsonNode &) const;
  51. std::string readText(const JsonNode &) const;
  52. std::pair<std::string, std::string> readHintText(const JsonNode &) const;
  53. int readKeycode(const JsonNode &) const;
  54. //basic widgets
  55. std::shared_ptr<CPicture> buildPicture(const JsonNode &) const;
  56. std::shared_ptr<CLabel> buildLabel(const JsonNode &) const;
  57. std::shared_ptr<CToggleGroup> buildToggleGroup(const JsonNode &) const;
  58. std::shared_ptr<CToggleButton> buildToggleButton(const JsonNode &) const;
  59. std::shared_ptr<CButton> buildButton(const JsonNode &) const;
  60. std::shared_ptr<CLabelGroup> buildLabelGroup(const JsonNode &) const;
  61. std::shared_ptr<CSlider> buildSlider(const JsonNode &) const;
  62. std::shared_ptr<CAnimImage> buildImage(const JsonNode &) const;
  63. std::shared_ptr<CShowableAnim> buildAnimation(const JsonNode &) const;
  64. std::shared_ptr<CFilledTexture> buildTexture(const JsonNode &) const;
  65. //composite widgets
  66. std::shared_ptr<CIntObject> buildWidget(JsonNode config) const;
  67. private:
  68. int unnamedObjectId = 0;
  69. std::map<std::string, BuilderFunction> builders;
  70. std::map<std::string, std::shared_ptr<CIntObject>> widgets;
  71. std::map<std::string, std::function<void(int)>> callbacks;
  72. };