InterfaceObjectConfigurable.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "../render/EFont.h"
  14. #include "../../lib/JsonNode.h"
  15. class CPicture;
  16. class CLabel;
  17. class CToggleGroup;
  18. class CToggleButton;
  19. class CButton;
  20. class CLabelGroup;
  21. class CSlider;
  22. class CAnimImage;
  23. class CShowableAnim;
  24. class CFilledTexture;
  25. #define REGISTER_BUILDER(type, method) registerBuilder(type, std::bind(method, this, std::placeholders::_1))
  26. class InterfaceObjectConfigurable: public CIntObject
  27. {
  28. public:
  29. InterfaceObjectConfigurable(int used=0, Point offset=Point());
  30. InterfaceObjectConfigurable(const JsonNode & config, int used=0, Point offset=Point());
  31. protected:
  32. /// Set blocked status for all buttons assotiated with provided shortcut
  33. void setShortcutBlocked(EShortcut shortcut, bool isBlocked);
  34. /// Registers provided callback to be called whenever specified shortcut is triggered
  35. void addShortcut(EShortcut shortcut, std::function<void()> callback);
  36. void keyPressed(EShortcut key) override;
  37. using BuilderFunction = std::function<std::shared_ptr<CIntObject>(const JsonNode &)>;
  38. void registerBuilder(const std::string &, BuilderFunction);
  39. void loadCustomBuilders(const JsonNode & config);
  40. //must be called after adding callbacks
  41. void build(const JsonNode & config);
  42. void addConditional(const std::string & name, bool active);
  43. void addWidget(const std::string & name, std::shared_ptr<CIntObject> widget);
  44. void addCallback(const std::string & callbackName, std::function<void(int)> callback);
  45. JsonNode variables;
  46. template<class T>
  47. const std::shared_ptr<T> widget(const std::string & name) const
  48. {
  49. auto iter = widgets.find(name);
  50. if(iter == widgets.end())
  51. return nullptr;
  52. return std::dynamic_pointer_cast<T>(iter->second);
  53. }
  54. void deleteWidget(const std::string & name);
  55. //basic serializers
  56. Point readPosition(const JsonNode &) const;
  57. Rect readRect(const JsonNode &) const;
  58. ETextAlignment readTextAlignment(const JsonNode &) const;
  59. ColorRGBA readColor(const JsonNode &) const;
  60. EFonts readFont(const JsonNode &) const;
  61. std::string readText(const JsonNode &) const;
  62. std::pair<std::string, std::string> readHintText(const JsonNode &) const;
  63. EShortcut readHotkey(const JsonNode &) const;
  64. void loadToggleButtonCallback(std::shared_ptr<CToggleButton> button, const JsonNode & config) const;
  65. void loadButtonCallback(std::shared_ptr<CButton> button, const JsonNode & config) const;
  66. void loadButtonHotkey(std::shared_ptr<CButton> button, const JsonNode & config) const;
  67. void loadButtonBorderColor(std::shared_ptr<CButton> button, const JsonNode & config) const;
  68. //basic widgets
  69. std::shared_ptr<CPicture> buildPicture(const JsonNode &) const;
  70. std::shared_ptr<CLabel> buildLabel(const JsonNode &) const;
  71. std::shared_ptr<CToggleGroup> buildToggleGroup(const JsonNode &) const;
  72. std::shared_ptr<CToggleButton> buildToggleButton(const JsonNode &) const;
  73. std::shared_ptr<CButton> buildButton(const JsonNode &) const;
  74. std::shared_ptr<CLabelGroup> buildLabelGroup(const JsonNode &) const;
  75. std::shared_ptr<CSlider> buildSlider(const JsonNode &) const;
  76. std::shared_ptr<CAnimImage> buildImage(const JsonNode &) const;
  77. std::shared_ptr<CShowableAnim> buildAnimation(const JsonNode &) const;
  78. std::shared_ptr<CFilledTexture> buildTexture(const JsonNode &) const;
  79. std::shared_ptr<CIntObject> buildLayout(const JsonNode &);
  80. //composite widgets
  81. std::shared_ptr<CIntObject> buildWidget(JsonNode config) const;
  82. private:
  83. struct ShortcutState
  84. {
  85. std::function<void()> callback;
  86. mutable bool assignedToButton = false;
  87. bool blocked = false;
  88. };
  89. int unnamedObjectId = 0;
  90. std::map<std::string, BuilderFunction> builders;
  91. std::map<std::string, std::shared_ptr<CIntObject>> widgets;
  92. std::map<std::string, std::function<void(int)>> callbacks;
  93. std::map<std::string, bool> conditionals;
  94. std::map<EShortcut, ShortcutState> shortcuts;
  95. };