/* * InterfaceBuilder.h, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * * License: GNU General Public License v2.0 or later * Full text of license available in license.txt file, in main folder * */ #pragma once #include "CIntObject.h" #include "TextAlignment.h" #include "../render/EFont.h" #include "../../lib/json/JsonNode.h" class CPicture; class CLabel; class CMultiLineLabel; class CToggleGroup; class CToggleButton; class CButton; class CLabelGroup; class CSlider; class CAnimImage; class CShowableAnim; class CFilledTexture; class ComboBox; class CTextInput; class TransparentFilledRectangle; class CTextBox; #define REGISTER_BUILDER(type, method) registerBuilder(type, std::bind(method, this, std::placeholders::_1)) class InterfaceObjectConfigurable: public CIntObject { public: InterfaceObjectConfigurable(int used=0, Point offset=Point()); InterfaceObjectConfigurable(const JsonNode & config, int used=0, Point offset=Point()); protected: /// Set blocked status for all buttons assotiated with provided shortcut void setShortcutBlocked(EShortcut shortcut, bool isBlocked); /// Registers provided callback to be called whenever specified shortcut is triggered void addShortcut(EShortcut shortcut, std::function callback); void keyPressed(EShortcut key) override; using BuilderFunction = std::function(const JsonNode &)>; void registerBuilder(const std::string &, BuilderFunction); void loadCustomBuilders(const JsonNode & config); //must be called after adding callbacks void build(const JsonNode & config); void addConditional(const std::string & name, bool active); void addWidget(const std::string & name, std::shared_ptr widget); void addCallback(const std::string & callbackName, std::function callback); void addCallback(const std::string & callbackName, std::function callback); JsonNode variables; template const std::shared_ptr widget(const std::string & name) const { auto iter = widgets.find(name); if(iter == widgets.end()) return nullptr; return std::dynamic_pointer_cast(iter->second); } void deleteWidget(const std::string & name); //basic serializers Point readPosition(const JsonNode &) const; Rect readRect(const JsonNode &) const; ETextAlignment readTextAlignment(const JsonNode &) const; ColorRGBA readColor(const JsonNode &) const; EFonts readFont(const JsonNode &) const; std::string readText(const JsonNode &) const; std::pair readHintText(const JsonNode &) const; EShortcut readHotkey(const JsonNode &) const; PlayerColor readPlayerColor(const JsonNode &) const; void loadToggleButtonCallback(std::shared_ptr button, const JsonNode & config) const; void loadButtonCallback(std::shared_ptr button, const JsonNode & config) const; void loadButtonHotkey(std::shared_ptr button, const JsonNode & config) const; void loadButtonBorderColor(std::shared_ptr button, const JsonNode & config) const; //basic widgets std::shared_ptr buildPicture(const JsonNode &) const; std::shared_ptr buildLabel(const JsonNode &) const; std::shared_ptr buildMultiLineLabel(const JsonNode &) const; std::shared_ptr buildToggleGroup(const JsonNode &) const; std::shared_ptr buildToggleButton(const JsonNode &) const; std::shared_ptr buildButton(const JsonNode &) const; std::shared_ptr buildLabelGroup(const JsonNode &) const; std::shared_ptr buildSlider(const JsonNode &) const; std::shared_ptr buildImage(const JsonNode &) const; std::shared_ptr buildAnimation(const JsonNode &) const; std::shared_ptr buildTexture(const JsonNode &) const; std::shared_ptr buildLayout(const JsonNode &); std::shared_ptr buildComboBox(const JsonNode &); std::shared_ptr buildTextInput(const JsonNode &) const; std::shared_ptr buildTransparentFilledRectangle(const JsonNode & config) const; std::shared_ptr buildGraphicalPrimitive(const JsonNode & config) const; std::shared_ptr buildTextBox(const JsonNode & config) const; //composite widgets std::shared_ptr buildWidget(JsonNode config) const; private: struct ShortcutState { std::function callback; mutable bool assignedToButton = false; bool blocked = false; }; int unnamedObjectId = 0; std::map builders; std::map> widgets; std::map> callbacks_int; std::map> callbacks_string; std::map conditionals; std::map shortcuts; };