2
0

InterfaceObjectConfigurable.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 InterfaceObjectConfigurable: public CIntObject
  20. {
  21. public:
  22. InterfaceObjectConfigurable();
  23. InterfaceObjectConfigurable(const JsonNode & config);
  24. protected:
  25. //must be called after adding callbacks
  26. void init(const JsonNode & config);
  27. void addCallback(const std::string & callbackName, std::function<void(int)> callback);
  28. template<class T>
  29. const std::shared_ptr<T> widget(const std::string & name) const
  30. {
  31. auto iter = widgets.find(name);
  32. if(iter == widgets.end())
  33. return nullptr;
  34. return std::dynamic_pointer_cast<T>(iter->second);
  35. }
  36. virtual std::shared_ptr<CIntObject> buildCustomWidget(const JsonNode & config) const;
  37. private: //field deserializers
  38. //basic serializers
  39. Point readPosition(const JsonNode &) const;
  40. ETextAlignment readTextAlignment(const JsonNode &) const;
  41. SDL_Color readColor(const JsonNode &) const;
  42. EFonts readFont(const JsonNode &) const;
  43. std::string readText(const JsonNode &) const;
  44. std::pair<std::string, std::string> readHintText(const JsonNode &) const;
  45. //basic widgets
  46. std::shared_ptr<CPicture> buildPicture(const JsonNode &) const;
  47. std::shared_ptr<CLabel> buildLabel(const JsonNode &) const;
  48. std::shared_ptr<CToggleGroup> buildToggleGroup(const JsonNode &) const;
  49. std::shared_ptr<CToggleButton> buildToggleButton(const JsonNode &) const;
  50. std::shared_ptr<CButton> buildButton(const JsonNode &) const;
  51. std::shared_ptr<CLabelGroup> buildLabelGroup(const JsonNode &) const;
  52. std::shared_ptr<CIntObject> buildWidget(const JsonNode & config) const;
  53. private:
  54. std::map<std::string, std::shared_ptr<CIntObject>> widgets;
  55. std::map<std::string, std::function<void(int)>> callbacks;
  56. };