InterfaceObjectConfigurable.h 4.8 KB

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