소스 검색

Rename InterfaceBuilder

nordsoft 2 년 전
부모
커밋
cd58e8a860

+ 2 - 2
client/CMakeLists.txt

@@ -15,7 +15,7 @@ set(client_SRCS
 		gui/Geometries.cpp
 		gui/SDL_Extensions.cpp
 		gui/NotificationHandler.cpp
-		gui/InterfaceBuilder.cpp
+		gui/InterfaceObjectConfigurable.cpp
 
 		widgets/AdventureMapClasses.cpp
 		widgets/Buttons.cpp
@@ -91,7 +91,7 @@ set(client_HEADERS
 		gui/SDL_Extensions.h
 		gui/SDL_Pixels.h
 		gui/NotificationHandler.h
-		gui/InterfaceBuilder.h
+		gui/InterfaceObjectConfigurable.h
 
 		widgets/AdventureMapClasses.h
 		widgets/Buttons.h

+ 7 - 7
client/gui/InterfaceBuilder.cpp

@@ -10,7 +10,7 @@
 
 #include "StdInc.h"
 
-#include "InterfaceBuilder.h"
+#include "InterfaceObjectConfigurable.h"
 
 #include "../CGameInfo.h"
 #include "../gui/CAnimation.h"
@@ -26,23 +26,23 @@
 #include "../../lib/CGeneralTextHandler.h"
 
 
-InterfaceBuilder::InterfaceBuilder(const JsonNode & config):
+InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config):
 	CIntObject()
 {
 	init(config);
 }
 
-InterfaceBuilder::InterfaceBuilder():
+InterfaceObjectConfigurable::InterfaceObjectConfigurable():
 	CIntObject()
 {
 }
 
-void InterfaceBuilder::addCallback(const std::string & callbackName, std::function<void(int)> callback)
+void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
 {
 	callbacks[callbackName] = callback;
 }
 
-void InterfaceBuilder::init(const JsonNode &config)
+void InterfaceObjectConfigurable::init(const JsonNode &config)
 {
 	OBJ_CONSTRUCTION;
 	int unnamedObjectId = 0;
@@ -57,7 +57,7 @@ void InterfaceBuilder::init(const JsonNode &config)
 	}
 }
 
-std::string InterfaceBuilder::buildText(const JsonNode & config) const
+std::string InterfaceObjectConfigurable::buildText(const JsonNode & config) const
 {
 	if(config.isNull())
 		return "";
@@ -69,7 +69,7 @@ std::string InterfaceBuilder::buildText(const JsonNode & config) const
 	return config.String();
 }
 
-std::shared_ptr<CIntObject> InterfaceBuilder::buildWidget(const JsonNode & config)
+std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(const JsonNode & config)
 {
 	assert(!config.isNull());
 	auto type = config["type"].String();

+ 3 - 3
client/gui/InterfaceBuilder.h

@@ -14,11 +14,11 @@
 
 #include "../../lib/JsonNode.h"
 
-class InterfaceBuilder: public CIntObject
+class InterfaceObjectConfigurable: public CIntObject
 {
 public:
-	InterfaceBuilder();
-	InterfaceBuilder(const JsonNode & config);
+	InterfaceObjectConfigurable();
+	InterfaceObjectConfigurable(const JsonNode & config);
 
 protected:
 	//must be called after adding callbacks

+ 204 - 0
client/gui/InterfaceObjectConfigurable.cpp

@@ -0,0 +1,204 @@
+/*
+* InterfaceBuilder.cpp, 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
+*
+*/
+
+#include "StdInc.h"
+
+#include "InterfaceObjectConfigurable.h"
+
+#include "../CGameInfo.h"
+#include "../gui/CAnimation.h"
+#include "../gui/CGuiHandler.h"
+#include "../widgets/CComponent.h"
+#include "../widgets/Buttons.h"
+#include "../widgets/MiscWidgets.h"
+#include "../widgets/ObjectLists.h"
+#include "../widgets/TextControls.h"
+#include "../windows/GUIClasses.h"
+#include "../windows/InfoWindows.h"
+
+#include "../../lib/CGeneralTextHandler.h"
+
+
+InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config):
+	CIntObject()
+{
+	init(config);
+}
+
+InterfaceObjectConfigurable::InterfaceObjectConfigurable():
+	CIntObject()
+{
+}
+
+void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
+{
+	callbacks[callbackName] = callback;
+}
+
+void InterfaceObjectConfigurable::init(const JsonNode &config)
+{
+	OBJ_CONSTRUCTION;
+	int unnamedObjectId = 0;
+	const std::string unnamedObjectPrefix = "__widget_";
+	
+	for(const auto & item : config["items"].Vector())
+	{
+		std::string name = item["name"].isNull()
+						? unnamedObjectPrefix + std::to_string(unnamedObjectId++)
+						: item["name"].String();
+		widgets[name] = buildWidget(item);
+	}
+}
+
+std::string InterfaceObjectConfigurable::buildText(const JsonNode & config) const
+{
+	if(config.isNull())
+		return "";
+	
+	if(config.isNumber())
+	{
+		return CGI->generaltexth->allTexts[config.Integer()];
+	}
+	return config.String();
+}
+
+std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(const JsonNode & config)
+{
+	assert(!config.isNull());
+	auto type = config["type"].String();
+	
+	int x = 0, y = 0;
+	if(!config["position"].isNull())
+	{
+		x = config["position"]["x"].Integer();
+		y = config["position"]["y"].Integer();
+	}
+	
+	std::string image;
+	std::string text = buildText(config["text"]);
+	auto alignment = EAlignment::CENTER;
+	auto color = Colors::DEFAULT_KEY_COLOR;
+	auto font = EFonts::FONT_TIMES;
+	
+	if(!config["image"].isNull())
+		image = config["image"].String();
+	if(!config["alignment"].isNull())
+	{
+		if(config["alignment"].String() == "left")
+			alignment = EAlignment::TOPLEFT;
+		if(config["alignment"].String() == "center")
+			alignment = EAlignment::CENTER;
+		if(config["alignment"].String() == "right")
+			alignment = EAlignment::BOTTOMRIGHT;
+	}
+	if(!config["color"].isNull())
+	{
+		if(config["color"].String() == "yellow")
+			color = Colors::YELLOW;
+		if(config["color"].String() == "white")
+			color = Colors::WHITE;
+		if(config["color"].String() == "gold")
+			color = Colors::METALLIC_GOLD;
+		if(config["color"].String() == "green")
+			color = Colors::GREEN;
+		if(config["color"].String() == "orange")
+			color = Colors::ORANGE;
+		if(config["color"].String() == "bright-yellow")
+			color = Colors::BRIGHT_YELLOW;
+	}
+	if(!config["font"].isNull())
+	{
+		if(config["font"].String() == "big")
+			font = EFonts::FONT_BIG;
+		if(config["font"].String() == "medium")
+			font = EFonts::FONT_MEDIUM;
+		if(config["font"].String() == "small")
+			font = EFonts::FONT_SMALL;
+		if(config["font"].String() == "tiny")
+			font = EFonts::FONT_TINY;
+	}
+	
+	
+	if(type == "picture")
+	{
+		return std::make_shared<CPicture>(image, x, y);
+	}
+	if(type == "label")
+	{
+		return std::make_shared<CLabel>(x, y, font, alignment, color, text);
+	}
+	if(type == "toggleGroup")
+	{
+		auto group = std::make_shared<CToggleGroup>(0);
+		group->pos.x += x;
+		group->pos.y += y;
+		if(!config["items"].isNull())
+		{
+			SObjectConstruction obj__i(group.get());
+			int itemIdx = -1;
+			for(const auto & item : config["items"].Vector())
+			{
+				itemIdx = item["index"].isNull() ? itemIdx + 1 : item["index"].Integer();
+				group->addToggle(itemIdx, std::dynamic_pointer_cast<CToggleBase>(buildWidget(item)));
+			}
+		}
+		if(!config["selected"].isNull())
+			group->setSelected(config["selected"].Integer());
+		if(!config["callback"].isNull())
+			group->addCallback(callbacks[config["callback"].String()]);
+		return group;
+	}
+	if(type == "toggleButton")
+	{
+		std::pair<std::string, std::string> zelp;
+		if(!config["zelp"].isNull())
+			zelp = CGI->generaltexth->zelp[config["zelp"].Integer()];
+		auto button = std::make_shared<CToggleButton>(Point(x, y), image, zelp);
+		if(!config["selected"].isNull())
+			button->setSelected(config["selected"].Bool());
+		if(!config["imageOrder"].isNull())
+		{
+			auto imgOrder = config["imageOrder"].Vector();
+			assert(imgOrder.size() >= 4);
+			button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
+		}
+		if(!config["callback"].isNull())
+			button->addCallback(callbacks[config["callback"].String()]);
+		return button;
+	}
+	if(type == "button")
+	{
+		std::pair<std::string, std::string> zelp;
+		if(!config["zelp"].isNull())
+			zelp = CGI->generaltexth->zelp[config["zelp"].Integer()];
+		auto button = std::make_shared<CButton>(Point(x, y), image, zelp);
+		return button;
+	}
+	if(type == "labelGroup")
+	{
+		auto group = std::make_shared<CLabelGroup>(font, alignment, color);
+		if(!config["items"].isNull())
+		{
+			for(const auto & item : config["items"].Vector())
+			{
+				if(!item["position"].isNull())
+				{
+					x = item["position"]["x"].Integer();
+					y = item["position"]["y"].Integer();
+				}
+				if(!item["text"].isNull())
+					text = buildText(item["text"]);
+				group->add(x, y, text);
+			}
+		}
+		return group;
+	}
+	return std::shared_ptr<CIntObject>(nullptr);
+}

+ 46 - 0
client/gui/InterfaceObjectConfigurable.h

@@ -0,0 +1,46 @@
+/*
+* 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 "../../lib/JsonNode.h"
+
+class InterfaceObjectConfigurable: public CIntObject
+{
+public:
+	InterfaceObjectConfigurable();
+	InterfaceObjectConfigurable(const JsonNode & config);
+
+protected:
+	//must be called after adding callbacks
+	void init(const JsonNode & config);
+	
+	void addCallback(const std::string & callbackName, std::function<void(int)> callback);
+	
+	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);
+	}
+	
+private:
+	
+	std::map<std::string, std::shared_ptr<CIntObject>> widgets;
+	std::map<std::string, std::function<void(int)>> callbacks;
+	
+	std::shared_ptr<CIntObject> buildWidget(const JsonNode & config);
+	
+	std::string buildText(const JsonNode & param) const;
+};

+ 1 - 1
client/lobby/RandomMapTab.cpp

@@ -30,7 +30,7 @@
 #include "../../lib/CModHandler.h"
 
 RandomMapTab::RandomMapTab():
-	InterfaceBuilder()
+	InterfaceObjectConfigurable()
 {
 	recActions = 0;
 	mapGenOptions = std::make_shared<CMapGenOptions>();

+ 2 - 2
client/lobby/RandomMapTab.h

@@ -13,7 +13,7 @@
 
 #include "../../lib/FunctionList.h"
 #include "../../lib/GameConstants.h"
-#include "../gui/InterfaceBuilder.h"
+#include "../gui/InterfaceObjectConfigurable.h"
 
 VCMI_LIB_NAMESPACE_BEGIN
 
@@ -25,7 +25,7 @@ class CToggleButton;
 class CLabel;
 class CLabelGroup;
 
-class RandomMapTab : public InterfaceBuilder
+class RandomMapTab : public InterfaceObjectConfigurable
 {
 public:
 	RandomMapTab();