InterfaceBuilder.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 InterfaceObjectConfigurable: public CIntObject
  14. {
  15. public:
  16. InterfaceObjectConfigurable();
  17. InterfaceObjectConfigurable(const JsonNode & config);
  18. protected:
  19. //must be called after adding callbacks
  20. void init(const JsonNode & config);
  21. void addCallback(const std::string & callbackName, std::function<void(int)> callback);
  22. template<class T>
  23. const std::shared_ptr<T> widget(const std::string & name) const
  24. {
  25. auto iter = widgets.find(name);
  26. if(iter == widgets.end())
  27. return nullptr;
  28. return std::dynamic_pointer_cast<T>(iter->second);
  29. }
  30. private:
  31. std::map<std::string, std::shared_ptr<CIntObject>> widgets;
  32. std::map<std::string, std::function<void(int)>> callbacks;
  33. std::shared_ptr<CIntObject> buildWidget(const JsonNode & config);
  34. std::string buildText(const JsonNode & param) const;
  35. };