InterfaceObjectConfigurable.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. class InterfaceObjectConfigurable: public CIntObject
  24. {
  25. public:
  26. InterfaceObjectConfigurable(int used=0, Point offset=Point());
  27. InterfaceObjectConfigurable(const JsonNode & config, int used=0, Point offset=Point());
  28. protected:
  29. //must be called after adding callbacks
  30. void init(const JsonNode & config);
  31. void addCallback(const std::string & callbackName, std::function<void(int)> callback);
  32. JsonNode variables;
  33. template<class T>
  34. const std::shared_ptr<T> widget(const std::string & name) const
  35. {
  36. auto iter = widgets.find(name);
  37. if(iter == widgets.end())
  38. return nullptr;
  39. return std::dynamic_pointer_cast<T>(iter->second);
  40. }
  41. //basic serializers
  42. Point readPosition(const JsonNode &) const;
  43. Rect readRect(const JsonNode &) const;
  44. ETextAlignment readTextAlignment(const JsonNode &) const;
  45. SDL_Color readColor(const JsonNode &) const;
  46. EFonts readFont(const JsonNode &) const;
  47. std::string readText(const JsonNode &) const;
  48. std::pair<std::string, std::string> readHintText(const JsonNode &) const;
  49. //basic widgets
  50. std::shared_ptr<CPicture> buildPicture(const JsonNode &) const;
  51. std::shared_ptr<CLabel> buildLabel(const JsonNode &) const;
  52. std::shared_ptr<CToggleGroup> buildToggleGroup(const JsonNode &) const;
  53. std::shared_ptr<CToggleButton> buildToggleButton(const JsonNode &) const;
  54. std::shared_ptr<CButton> buildButton(const JsonNode &) const;
  55. std::shared_ptr<CLabelGroup> buildLabelGroup(const JsonNode &) const;
  56. std::shared_ptr<CSlider> buildSlider(const JsonNode &) const;
  57. std::shared_ptr<CAnimImage> buildImage(const JsonNode &) const;
  58. std::shared_ptr<CShowableAnim> buildAnimation(const JsonNode &) const;
  59. std::shared_ptr<CFilledTexture> buildTexture(const JsonNode &) const;
  60. //composite widgets
  61. virtual std::shared_ptr<CIntObject> buildCustomWidget(const JsonNode & config);
  62. std::shared_ptr<CIntObject> buildWidget(JsonNode config) const;
  63. private:
  64. std::map<std::string, std::shared_ptr<CIntObject>> widgets;
  65. std::map<std::string, std::function<void(int)>> callbacks;
  66. };