1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Use kind-of-factory approach for widget builders

This commit is contained in:
nordsoft
2022-12-22 02:05:29 +04:00
parent 8f7025328f
commit e245dbaf9d
4 changed files with 34 additions and 62 deletions

View File

@@ -25,6 +25,8 @@ class CAnimImage;
class CShowableAnim;
class CFilledTexture;
#define REGISTER_BUILDER(type, method) registerBuilder(type, std::bind(method, this, std::placeholders::_1))
class InterfaceObjectConfigurable: public CIntObject
{
public:
@@ -32,6 +34,10 @@ public:
InterfaceObjectConfigurable(const JsonNode & config, int used=0, Point offset=Point());
protected:
using BuilderFunction = std::function<std::shared_ptr<CIntObject>(const JsonNode &)>;
void registerBuilder(const std::string &, BuilderFunction);
//must be called after adding callbacks
void init(const JsonNode & config);
@@ -67,14 +73,13 @@ protected:
std::shared_ptr<CAnimImage> buildImage(const JsonNode &) const;
std::shared_ptr<CShowableAnim> buildAnimation(const JsonNode &) const;
std::shared_ptr<CFilledTexture> buildTexture(const JsonNode &) const;
//composite widgets
virtual std::shared_ptr<CIntObject> buildCustomWidget(const JsonNode & config);
std::shared_ptr<CIntObject> buildWidget(JsonNode config) const;
private:
std::map<std::string, BuilderFunction> builders;
std::map<std::string, std::shared_ptr<CIntObject>> widgets;
std::map<std::string, std::function<void(int)>> callbacks;
};