2022-12-12 09:48:39 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
2023-01-18 15:50:52 +02:00
|
|
|
#include "TextAlignment.h"
|
2023-07-31 18:50:55 +02:00
|
|
|
#include "../render/EFont.h"
|
2022-12-12 09:48:39 +02:00
|
|
|
|
2024-02-11 23:09:01 +02:00
|
|
|
#include "../../lib/json/JsonNode.h"
|
2022-12-12 09:48:39 +02:00
|
|
|
|
2022-12-15 22:46:36 +02:00
|
|
|
class CPicture;
|
|
|
|
class CLabel;
|
2023-08-23 17:29:50 +02:00
|
|
|
class CMultiLineLabel;
|
2022-12-15 22:46:36 +02:00
|
|
|
class CToggleGroup;
|
|
|
|
class CToggleButton;
|
|
|
|
class CButton;
|
|
|
|
class CLabelGroup;
|
2022-12-16 00:15:53 +02:00
|
|
|
class CSlider;
|
2022-12-17 06:19:16 +02:00
|
|
|
class CAnimImage;
|
|
|
|
class CShowableAnim;
|
|
|
|
class CFilledTexture;
|
2023-08-25 12:07:38 +02:00
|
|
|
class ComboBox;
|
2023-08-25 18:20:26 +02:00
|
|
|
class CTextInput;
|
2023-09-30 22:49:22 +02:00
|
|
|
class TransparentFilledRectangle;
|
|
|
|
class CTextBox;
|
2022-12-15 22:46:36 +02:00
|
|
|
|
2022-12-22 00:05:29 +02:00
|
|
|
#define REGISTER_BUILDER(type, method) registerBuilder(type, std::bind(method, this, std::placeholders::_1))
|
|
|
|
|
2022-12-12 09:48:39 +02:00
|
|
|
class InterfaceObjectConfigurable: public CIntObject
|
|
|
|
{
|
|
|
|
public:
|
2022-12-16 00:15:53 +02:00
|
|
|
InterfaceObjectConfigurable(int used=0, Point offset=Point());
|
|
|
|
InterfaceObjectConfigurable(const JsonNode & config, int used=0, Point offset=Point());
|
2022-12-12 09:48:39 +02:00
|
|
|
|
|
|
|
protected:
|
2024-06-24 03:23:26 +02:00
|
|
|
/// Set blocked status for all buttons associated with provided shortcut
|
2023-04-29 12:48:21 +02:00
|
|
|
void setShortcutBlocked(EShortcut shortcut, bool isBlocked);
|
|
|
|
|
|
|
|
/// Registers provided callback to be called whenever specified shortcut is triggered
|
|
|
|
void addShortcut(EShortcut shortcut, std::function<void()> callback);
|
|
|
|
|
|
|
|
void keyPressed(EShortcut key) override;
|
|
|
|
|
2022-12-22 00:05:29 +02:00
|
|
|
using BuilderFunction = std::function<std::shared_ptr<CIntObject>(const JsonNode &)>;
|
|
|
|
void registerBuilder(const std::string &, BuilderFunction);
|
2023-05-14 23:53:26 +02:00
|
|
|
|
|
|
|
void loadCustomBuilders(const JsonNode & config);
|
2022-12-22 00:05:29 +02:00
|
|
|
|
2022-12-12 09:48:39 +02:00
|
|
|
//must be called after adding callbacks
|
2022-12-25 12:22:07 +02:00
|
|
|
void build(const JsonNode & config);
|
2023-05-04 18:04:36 +02:00
|
|
|
|
2023-05-19 22:19:43 +02:00
|
|
|
void addConditional(const std::string & name, bool active);
|
|
|
|
|
2023-05-04 18:04:36 +02:00
|
|
|
void addWidget(const std::string & name, std::shared_ptr<CIntObject> widget);
|
2022-12-12 09:48:39 +02:00
|
|
|
|
|
|
|
void addCallback(const std::string & callbackName, std::function<void(int)> callback);
|
2023-08-25 18:20:26 +02:00
|
|
|
void addCallback(const std::string & callbackName, std::function<void(std::string)> callback);
|
2022-12-17 18:38:16 +02:00
|
|
|
JsonNode variables;
|
2022-12-12 09:48:39 +02:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
const std::shared_ptr<T> widget(const std::string & name) const
|
|
|
|
{
|
|
|
|
auto iter = widgets.find(name);
|
|
|
|
if(iter == widgets.end())
|
|
|
|
return nullptr;
|
|
|
|
return std::dynamic_pointer_cast<T>(iter->second);
|
|
|
|
}
|
2022-12-25 12:33:10 +02:00
|
|
|
|
|
|
|
void deleteWidget(const std::string & name);
|
2022-12-17 18:38:16 +02:00
|
|
|
|
2022-12-15 22:46:36 +02:00
|
|
|
//basic serializers
|
|
|
|
Point readPosition(const JsonNode &) const;
|
2022-12-17 06:19:16 +02:00
|
|
|
Rect readRect(const JsonNode &) const;
|
2022-12-15 22:46:36 +02:00
|
|
|
ETextAlignment readTextAlignment(const JsonNode &) const;
|
2023-07-31 18:50:55 +02:00
|
|
|
ColorRGBA readColor(const JsonNode &) const;
|
2022-12-15 22:46:36 +02:00
|
|
|
EFonts readFont(const JsonNode &) const;
|
|
|
|
std::string readText(const JsonNode &) const;
|
|
|
|
std::pair<std::string, std::string> readHintText(const JsonNode &) const;
|
2023-04-29 12:48:21 +02:00
|
|
|
EShortcut readHotkey(const JsonNode &) const;
|
2023-08-27 20:34:02 +02:00
|
|
|
PlayerColor readPlayerColor(const JsonNode &) const;
|
2022-12-15 22:46:36 +02:00
|
|
|
|
2023-05-12 21:58:21 +02:00
|
|
|
void loadToggleButtonCallback(std::shared_ptr<CToggleButton> button, const JsonNode & config) const;
|
2023-05-02 15:09:36 +02:00
|
|
|
void loadButtonCallback(std::shared_ptr<CButton> button, const JsonNode & config) const;
|
|
|
|
void loadButtonHotkey(std::shared_ptr<CButton> button, const JsonNode & config) const;
|
2023-05-05 20:50:20 +02:00
|
|
|
void loadButtonBorderColor(std::shared_ptr<CButton> button, const JsonNode & config) const;
|
2023-05-02 15:09:36 +02:00
|
|
|
|
2022-12-15 22:46:36 +02:00
|
|
|
//basic widgets
|
|
|
|
std::shared_ptr<CPicture> buildPicture(const JsonNode &) const;
|
|
|
|
std::shared_ptr<CLabel> buildLabel(const JsonNode &) const;
|
2023-08-23 17:29:50 +02:00
|
|
|
std::shared_ptr<CMultiLineLabel> buildMultiLineLabel(const JsonNode &) const;
|
2022-12-15 22:46:36 +02:00
|
|
|
std::shared_ptr<CToggleGroup> buildToggleGroup(const JsonNode &) const;
|
|
|
|
std::shared_ptr<CToggleButton> buildToggleButton(const JsonNode &) const;
|
|
|
|
std::shared_ptr<CButton> buildButton(const JsonNode &) const;
|
|
|
|
std::shared_ptr<CLabelGroup> buildLabelGroup(const JsonNode &) const;
|
2022-12-16 00:15:53 +02:00
|
|
|
std::shared_ptr<CSlider> buildSlider(const JsonNode &) const;
|
2022-12-17 06:19:16 +02:00
|
|
|
std::shared_ptr<CAnimImage> buildImage(const JsonNode &) const;
|
|
|
|
std::shared_ptr<CShowableAnim> buildAnimation(const JsonNode &) const;
|
|
|
|
std::shared_ptr<CFilledTexture> buildTexture(const JsonNode &) const;
|
2023-05-15 11:48:58 +02:00
|
|
|
std::shared_ptr<CIntObject> buildLayout(const JsonNode &);
|
2023-08-25 12:07:38 +02:00
|
|
|
std::shared_ptr<ComboBox> buildComboBox(const JsonNode &);
|
2023-08-25 18:20:26 +02:00
|
|
|
std::shared_ptr<CTextInput> buildTextInput(const JsonNode &) const;
|
2023-09-30 22:49:22 +02:00
|
|
|
std::shared_ptr<TransparentFilledRectangle> buildTransparentFilledRectangle(const JsonNode & config) const;
|
2024-01-18 16:23:12 +02:00
|
|
|
std::shared_ptr<CIntObject> buildGraphicalPrimitive(const JsonNode & config) const;
|
2023-09-30 22:49:22 +02:00
|
|
|
std::shared_ptr<CTextBox> buildTextBox(const JsonNode & config) const;
|
2022-12-22 00:05:29 +02:00
|
|
|
|
2022-12-16 00:15:53 +02:00
|
|
|
//composite widgets
|
2022-12-17 18:38:16 +02:00
|
|
|
std::shared_ptr<CIntObject> buildWidget(JsonNode config) const;
|
2022-12-15 22:46:36 +02:00
|
|
|
|
2022-12-12 09:48:39 +02:00
|
|
|
private:
|
2023-04-29 12:48:21 +02:00
|
|
|
struct ShortcutState
|
|
|
|
{
|
|
|
|
std::function<void()> callback;
|
2024-05-19 10:23:28 +02:00
|
|
|
mutable std::vector<std::shared_ptr<CButton>> assignedButtons;
|
2023-04-29 12:48:21 +02:00
|
|
|
bool blocked = false;
|
|
|
|
};
|
2022-12-12 09:48:39 +02:00
|
|
|
|
2022-12-25 12:22:07 +02:00
|
|
|
int unnamedObjectId = 0;
|
2022-12-22 00:05:29 +02:00
|
|
|
std::map<std::string, BuilderFunction> builders;
|
2022-12-12 09:48:39 +02:00
|
|
|
std::map<std::string, std::shared_ptr<CIntObject>> widgets;
|
2023-08-25 18:20:26 +02:00
|
|
|
std::map<std::string, std::function<void(int)>> callbacks_int;
|
|
|
|
std::map<std::string, std::function<void(std::string)>> callbacks_string;
|
2023-05-19 22:19:43 +02:00
|
|
|
std::map<std::string, bool> conditionals;
|
2023-04-29 12:48:21 +02:00
|
|
|
std::map<EShortcut, ShortcutState> shortcuts;
|
2022-12-12 09:48:39 +02:00
|
|
|
};
|