Explorar o código

Implemented simple layout to simplify widget json definitions

Ivan Savenko %!s(int64=2) %!d(string=hai) anos
pai
achega
7cbfd8117e

+ 49 - 0
client/gui/InterfaceObjectConfigurable.cpp

@@ -46,6 +46,7 @@ InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset)
 	REGISTER_BUILDER("button", &InterfaceObjectConfigurable::buildButton);
 	REGISTER_BUILDER("labelGroup", &InterfaceObjectConfigurable::buildLabelGroup);
 	REGISTER_BUILDER("slider", &InterfaceObjectConfigurable::buildSlider);
+	REGISTER_BUILDER("layout", &InterfaceObjectConfigurable::buildLayout);
 }
 
 void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f)
@@ -468,6 +469,54 @@ std::shared_ptr<CFilledTexture> InterfaceObjectConfigurable::buildTexture(const
 	return std::make_shared<CFilledTexture>(image, rect);
 }
 
+/// Small helper class that provides ownership for shared_ptr's of child elements
+class InterfaceLayoutWidget : public CIntObject
+{
+public:
+	std::vector<std::shared_ptr<CIntObject>> ownedChildren;
+};
+
+std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildLayout(const JsonNode & config)
+{
+	logGlobal->debug("Building widget Layout");
+	bool vertical = config["vertical"].Bool();
+	bool horizontal = config["horizontal"].Bool();
+	bool dynamic = config["dynamic"].Bool();
+	int distance = config["distance"].Integer();
+	std::string customType = config["customType"].String();
+	auto position = readPosition(config["position"]);
+
+	auto result = std::make_shared<InterfaceLayoutWidget>();
+	result->moveBy(position);
+	Point layoutPosition;
+
+	for(auto item : config["items"].Vector())
+	{
+		if (item["type"].String().empty())
+			item["type"].String() = customType;
+
+		auto widget = buildWidget(item);
+
+		addWidget(item["name"].String(), widget);
+		result->ownedChildren.push_back(widget);
+		result->addChild(widget.get(), false);
+
+		widget->moveBy(position + layoutPosition);
+
+		if (dynamic && vertical)
+			layoutPosition.y += widget->pos.h;
+		if (dynamic && horizontal)
+			layoutPosition.x += widget->pos.w;
+
+		if (vertical)
+			layoutPosition.y += distance;
+		if (horizontal)
+			layoutPosition.x += distance;
+	}
+
+	return result;
+}
+
 std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const JsonNode & config) const
 {
 	logGlobal->debug("Building widget CShowableAnim");

+ 1 - 0
client/gui/InterfaceObjectConfigurable.h

@@ -93,6 +93,7 @@ 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;
+	std::shared_ptr<CIntObject> buildLayout(const JsonNode &);
 		
 	//composite widgets
 	std::shared_ptr<CIntObject> buildWidget(JsonNode config) const;

+ 90 - 91
config/widgets/settings/generalOptionsTab.json

@@ -43,9 +43,15 @@
 			"orientation": "horizontal",
 			"itemsVisible": 0,
 			"itemsTotal": 100,
+		},
+		"verticalLayout" : {
+			"type" : "layout",
+			"vertical" : true,
+			"dynamic" : false,
+			"distance" : 30
 		}
 	},
-	
+
 	"items":
 	[
 		{
@@ -54,7 +60,6 @@
 			"image": "settingsWindow/lineHorizontal",
 			"rect": { "x" : 5, "y" : 289, "w": 365, "h": 3}
 		},
-		
 		{
 			"type" : "labelTitle",
 			"position": {"x": 10, "y": 55},
@@ -72,67 +77,62 @@
 		},
 /////////////////////////////////////// Left section - Video Settings
 		{
-			"name": "resolutionLabel",
-			"type": "labelDescription",
-			"position": {"x": 45, "y": 85},
-			"text": "vcmi.systemOptions.resolutionButton.hover"
-		},
-		{
-			"name": "resolutionButton",
-			"type": "buttonGear",
-			"position": {"x": 10, "y": 83},
-			"help": "vcmi.systemOptions.resolutionButton",
-			"callback": "setGameResolution",
-		},
-		
-		{
-			"name": "scalingLabel",
-			"type": "labelDescription",
-			"position": {"x": 45, "y": 115},
-			"text": "vcmi.systemOptions.scalingButton.hover"
-		},
-		{
-			"name": "scalingButton",
-			"type": "buttonGear",
-			"position": {"x": 10, "y": 113},
-			"help": "vcmi.systemOptions.scalingButton",
-			"callback": "setGameScaling",
-		},
-		{
-			"type" : "labelDescription",
-			"position": {"x": 45, "y": 145},
-			"text": "vcmi.systemOptions.fullscreenButton.hover"
-		},
-		{
-			"type" : "labelDescription",
-			"position": {"x": 45, "y": 175},
-			"text": "vcmi.systemOptions.framerateButton.hover"
-		},
-		{
-			"type" : "labelDescription",
-			"position": {"x": 45, "y": 205},
-			"text": "core.genrltxt.577"
-		},
-		{
-			"name": "fullscreenCheckbox",
-			"type": "checkbox",
-			"help": "vcmi.systemOptions.fullscreenButton",
-			"position": {"x": 10, "y": 143},
-			"callback": "fullscreenChanged"
-		},
-		{
-			"name": "framerateCheckbox",
-			"type": "checkbox",
-			"help": "vcmi.systemOptions.framerateButton",
-			"position": {"x": 10, "y": 173},
-			"callback": "framerateChanged"
+			"type" : "verticalLayout",
+			"customType" : "labelDescription",
+			"position" : {"x": 45, "y": 85},
+			"items" : [
+				{
+					"name": "resolutionLabel",
+					"text": "vcmi.systemOptions.resolutionButton.hover"
+				},
+				{
+					"name": "scalingLabel",
+					"text": "vcmi.systemOptions.scalingButton.hover"
+				},
+				{
+					"text": "vcmi.systemOptions.fullscreenButton.hover"
+				},
+				{
+					"text": "vcmi.systemOptions.framerateButton.hover"
+				},
+				{
+					"text": "core.genrltxt.577"
+				}
+			]
 		},
 		{
-			"name": "spellbookAnimationCheckbox",
-			"type": "checkbox",
-			"help": "core.help.364",
-			"position": {"x": 10, "y": 203},
-			"callback": "spellbookAnimationChanged"
+			"type" : "verticalLayout",
+			"customType" : "checkbox",
+			"position" : {"x": 10, "y": 83},
+			"items" : [
+				{
+					"name": "resolutionButton",
+					"type": "buttonGear",
+					"help": "vcmi.systemOptions.resolutionButton",
+					"callback": "setGameResolution",
+				},
+				{
+					"name": "scalingButton",
+					"type": "buttonGear",
+					"help": "vcmi.systemOptions.scalingButton",
+					"callback": "setGameScaling",
+				},
+				{
+					"name": "fullscreenCheckbox",
+					"help": "vcmi.systemOptions.fullscreenButton",
+					"callback": "fullscreenChanged"
+				},
+				{
+					"name": "framerateCheckbox",
+					"help": "vcmi.systemOptions.framerateButton",
+					"callback": "framerateChanged"
+				},
+				{
+					"name": "spellbookAnimationCheckbox",
+					"help": "core.help.364",
+					"callback": "spellbookAnimationChanged"
+				},
+			]
 		},
 /////////////////////////////////////// Right section - Audio Settings
 		{
@@ -181,49 +181,48 @@
 		},
 /////////////////////////////////////// Bottom section - Towns Settings
 		{
-			"type" : "labelDescription",
-			"text": "vcmi.otherOptions.creatureGrowthAsDwellingLabel.hover",
-			"position": {"x": 45, "y": 325}
-		},
-		{
-			"type" : "labelDescription",
-			"text": "vcmi.otherOptions.availableCreaturesAsDwellingLabel.hover",
-			"position": {"x": 45, "y": 355}
-		},
-		{
-			"type" : "labelDescription",
-			"text": "vcmi.otherOptions.compactTownCreatureInfo.hover",
-			"position": {"x": 45, "y": 385}
+			"type" : "verticalLayout",
+			"customType" : "labelDescription",
+			"position": {"x": 45, "y": 325},
+			"items" : [
+				{
+					"text": "vcmi.otherOptions.creatureGrowthAsDwellingLabel.hover",
+				},
+				{
+					"text": "vcmi.otherOptions.availableCreaturesAsDwellingLabel.hover",
+				},
+				{
+					"text": "vcmi.otherOptions.compactTownCreatureInfo.hover",
+				}
+			]
 		},
-		
 		{
 			"name": "availableCreaturesAsDwellingPicker",
 			"type": "toggleGroup",
+			"callback": "availableCreaturesAsDwellingChanged"
+		},
+		{
+			"type" : "verticalLayout",
+			"customType" : "checkbox",
 			"position": {"x": 10, "y": 323},
 			"items":
 			[
 				{
-					"index": 0,
-					"type": "checkbox",
 					"help": "vcmi.otherOptions.creatureGrowthAsDwellingLabel",
-					"position": {"x": 0, "y": 0}
+					"group" : "availableCreaturesAsDwellingPicker",
+					"index": 0
 				},
 				{
-					"index": 1,
-					"type": "checkbox",
 					"help": "vcmi.otherOptions.availableCreaturesAsDwellingLabel",
-					"position": {"x": 0, "y": 30}
+					"group" : "availableCreaturesAsDwellingPicker",
+					"index": 1
 				},
-			],
-			"callback": "availableCreaturesAsDwellingChanged"
-		},
-
-		{
-			"name": "compactTownCreatureInfoCheckbox",
-			"type": "checkbox",
-			"help": "vcmi.otherOptions.compactTownCreatureInfo",
-			"position": {"x": 10, "y": 383},
-			"callback": "compactTownCreatureInfoChanged"
+				{
+					"name": "compactTownCreatureInfoCheckbox",
+					"help": "vcmi.otherOptions.compactTownCreatureInfo",
+					"callback": "compactTownCreatureInfoChanged"
+				}
+			]
 		}
 	]
 }