mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	Allow defining default values of settings per platform
This commit is contained in:
		| @@ -22,17 +22,10 @@ | ||||
|  | ||||
| std::unique_ptr<ICursor> CursorHandler::createCursor() | ||||
| { | ||||
| 	if (settings["video"]["cursor"].String() == "auto") | ||||
| 	{ | ||||
| #if defined(VCMI_MOBILE) | ||||
| 		if (settings["general"]["userRelativePointer"].Bool()) | ||||
| 			return std::make_unique<CursorSoftware>(); | ||||
| 		else | ||||
| 			return std::make_unique<CursorHardware>(); | ||||
| #else | ||||
| 		return std::make_unique<CursorHardware>(); | ||||
| 	if (settings["general"]["userRelativePointer"].Bool()) | ||||
| 		return std::make_unique<CursorSoftware>(); | ||||
| #endif | ||||
| 	} | ||||
|  | ||||
| 	if (settings["video"]["cursor"].String() == "hardware") | ||||
| 		return std::make_unique<CursorHardware>(); | ||||
|   | ||||
| @@ -123,6 +123,8 @@ | ||||
| 						"height" : { "type" : "number" }, | ||||
| 						"scaling" : { "type" : "number" } | ||||
| 					}, | ||||
| 					"defaultIOS" : {"width" : 800, "height" : 600, "scaling" : 200 }, | ||||
| 					"defaultAndroid" : {"width" : 800, "height" : 600, "scaling" : 200 }, | ||||
| 					"default" : {"width" : 800, "height" : 600, "scaling" : 100 } | ||||
| 				}, | ||||
| 				"bitsPerPixel" : { | ||||
| @@ -139,8 +141,8 @@ | ||||
| 				}, | ||||
| 				"cursor" :  { | ||||
| 					"type" : "string", | ||||
| 					"enum" : [ "auto", "hardware", "software" ], | ||||
| 					"default" : "auto" | ||||
| 					"enum" : [ "hardware", "software" ], | ||||
| 					"default" : "hardware" | ||||
| 				}, | ||||
| 				"showIntro" : { | ||||
| 					"type" : "boolean", | ||||
| @@ -210,6 +212,8 @@ | ||||
| 				"borderScroll" :  | ||||
| 				{ | ||||
| 					"type" : "boolean", | ||||
| 					"defaultIOS" : false, | ||||
| 					"defaultAndroid" : false, | ||||
| 					"default" : true | ||||
| 				}, | ||||
| 			} | ||||
| @@ -458,7 +462,7 @@ | ||||
| 			"type" : "object", | ||||
| 			"default" : {}, | ||||
| 			"additionalProperties" : false, | ||||
| 			"required" : [ "setupCompleted", "repositoryURL", "enableInstalledMods", "extraResolutionsModPath", "autoCheckRepositories", "updateOnStartup", "updateConfigUrl", "lobbyUrl", "lobbyPort", "lobbyUsername", "connectionTimeout" ], | ||||
| 			"required" : [ "setupCompleted", "repositoryURL", "enableInstalledMods", "autoCheckRepositories", "updateOnStartup", "updateConfigUrl", "lobbyUrl", "lobbyPort", "lobbyUsername", "connectionTimeout" ], | ||||
| 			"properties" : { | ||||
| 				"repositoryURL" : { | ||||
| 					"type" : "array", | ||||
| @@ -477,10 +481,6 @@ | ||||
| 					"type" : "boolean", | ||||
| 					"default" : true | ||||
| 				}, | ||||
| 				"extraResolutionsModPath" : { | ||||
| 					"type" : "string", | ||||
| 					"default" : "/vcmi-extras/Mods/extraResolutions/Content/config/resolutions.json" | ||||
| 				}, | ||||
| 				"autoCheckRepositories" : { | ||||
| 					"type" : "boolean", | ||||
| 					"default" : true | ||||
|   | ||||
| @@ -38,7 +38,6 @@ QString resolutionToString(const QSize & resolution) | ||||
|  | ||||
| static const std::string cursorTypesList[] = | ||||
| { | ||||
| 	"auto", | ||||
| 	"hardware", | ||||
| 	"software" | ||||
| }; | ||||
|   | ||||
| @@ -112,7 +112,7 @@ | ||||
|       <property name="geometry"> | ||||
|        <rect> | ||||
|         <x>0</x> | ||||
|         <y>-107</y> | ||||
|         <y>0</y> | ||||
|         <width>620</width> | ||||
|         <height>745</height> | ||||
|        </rect> | ||||
| @@ -547,11 +547,6 @@ | ||||
|        </item> | ||||
|        <item row="14" column="1" colspan="3"> | ||||
|         <widget class="QComboBox" name="comboBoxCursorType"> | ||||
|          <item> | ||||
|           <property name="text"> | ||||
|            <string>Default</string> | ||||
|           </property> | ||||
|          </item> | ||||
|          <item> | ||||
|           <property name="text"> | ||||
|            <string>Hardware</string> | ||||
|   | ||||
							
								
								
									
										119
									
								
								lib/JsonNode.cpp
									
									
									
									
									
								
							
							
						
						
									
										119
									
								
								lib/JsonNode.cpp
									
									
									
									
									
								
							| @@ -1135,35 +1135,71 @@ Key reverseMapFirst(const Val & val, const std::map<Key, Val> & map) | ||||
| 	return ""; | ||||
| } | ||||
|  | ||||
| void minimizeNode(JsonNode & node, const JsonNode & schema) | ||||
| static JsonNode getDefaultValue(const JsonNode & schema, std::string fieldName) | ||||
| { | ||||
| 	if (schema["type"].String() == "object") | ||||
| 	const JsonNode & fieldProps = schema["properties"][fieldName]; | ||||
|  | ||||
| #if defined(VCMI_IOS) | ||||
| 	if (!fieldProps["defaultIOS"].isNull()) | ||||
| 		return fieldProps["defaultIOS"]; | ||||
| #elif defined(VCMI_ANDROID) | ||||
| 	if (!fieldProps["defaultAndroid"].isNull()) | ||||
| 		return fieldProps["defaultAndroid"]; | ||||
| #elif !defined(VCMI_MOBILE) | ||||
| 	if (!fieldProps["defaultDesktop"].isNull()) | ||||
| 		return fieldProps["defaultDesktop"]; | ||||
| #endif | ||||
| 	return fieldProps["default"]; | ||||
| } | ||||
|  | ||||
| static void eraseOptionalNodes(JsonNode & node, const JsonNode & schema) | ||||
| { | ||||
| 	assert(schema["type"].String() == "object"); | ||||
|  | ||||
| 	std::set<std::string> foundEntries; | ||||
|  | ||||
| 	for(const auto & entry : schema["required"].Vector()) | ||||
| 		foundEntries.insert(entry.String()); | ||||
|  | ||||
| 	vstd::erase_if(node.Struct(), [&](const auto & node){ | ||||
| 		return !vstd::contains(foundEntries, node.first); | ||||
| 	}); | ||||
| } | ||||
|  | ||||
| static void minimizeNode(JsonNode & node, const JsonNode & schema) | ||||
| { | ||||
| 	if (schema["type"].String() != "object") | ||||
| 		return; | ||||
|  | ||||
| 	for(const auto & entry : schema["required"].Vector()) | ||||
| 	{ | ||||
| 		std::set<std::string> foundEntries; | ||||
| 		const std::string & name = entry.String(); | ||||
| 		minimizeNode(node[name], schema["properties"][name]); | ||||
|  | ||||
| 		for(const auto & entry : schema["required"].Vector()) | ||||
| 		{ | ||||
| 			const std::string & name = entry.String(); | ||||
| 			foundEntries.insert(name); | ||||
|  | ||||
| 			minimizeNode(node[name], schema["properties"][name]); | ||||
|  | ||||
| 			if (vstd::contains(node.Struct(), name) && | ||||
| 				node[name] == schema["properties"][name]["default"]) | ||||
| 			{ | ||||
| 				node.Struct().erase(name); | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		// erase all unhandled entries | ||||
| 		for (auto it = node.Struct().begin(); it != node.Struct().end();) | ||||
| 		{ | ||||
| 			if (!vstd::contains(foundEntries, it->first)) | ||||
| 				it = node.Struct().erase(it); | ||||
| 			else | ||||
| 				it++; | ||||
| 		} | ||||
| 		if (vstd::contains(node.Struct(), name) && node[name] == getDefaultValue(schema, name)) | ||||
| 			node.Struct().erase(name); | ||||
| 	} | ||||
| 	eraseOptionalNodes(node, schema); | ||||
| } | ||||
|  | ||||
| static void maximizeNode(JsonNode & node, const JsonNode & schema) | ||||
| { | ||||
| 	// "required" entry can only be found in object/struct | ||||
| 	if (schema["type"].String() != "object") | ||||
| 		return; | ||||
|  | ||||
| 	// check all required entries that have default version | ||||
| 	for(const auto & entry : schema["required"].Vector()) | ||||
| 	{ | ||||
| 		const std::string & name = entry.String(); | ||||
|  | ||||
| 		if (node[name].isNull() && !getDefaultValue(schema, name).isNull()) | ||||
| 			node[name] = getDefaultValue(schema, name); | ||||
|  | ||||
| 		maximizeNode(node[name], schema["properties"][name]); | ||||
| 	} | ||||
|  | ||||
| 	eraseOptionalNodes(node, schema); | ||||
| } | ||||
|  | ||||
| void JsonUtils::minimize(JsonNode & node, const std::string & schemaName) | ||||
| @@ -1171,39 +1207,6 @@ void JsonUtils::minimize(JsonNode & node, const std::string & schemaName) | ||||
| 	minimizeNode(node, getSchema(schemaName)); | ||||
| } | ||||
|  | ||||
| // FIXME: except for several lines function is identical to minimizeNode. Some way to reduce duplication? | ||||
| void maximizeNode(JsonNode & node, const JsonNode & schema) | ||||
| { | ||||
| 	// "required" entry can only be found in object/struct | ||||
| 	if (schema["type"].String() == "object") | ||||
| 	{ | ||||
| 		std::set<std::string> foundEntries; | ||||
|  | ||||
| 		// check all required entries that have default version | ||||
| 		for(const auto & entry : schema["required"].Vector()) | ||||
| 		{ | ||||
| 			const std::string & name = entry.String(); | ||||
| 			foundEntries.insert(name); | ||||
|  | ||||
| 			if (node[name].isNull() && | ||||
| 				!schema["properties"][name]["default"].isNull()) | ||||
| 			{ | ||||
| 				node[name] = schema["properties"][name]["default"]; | ||||
| 			} | ||||
| 			maximizeNode(node[name], schema["properties"][name]); | ||||
| 		} | ||||
|  | ||||
| 		// erase all unhandled entries | ||||
| 		for (auto it = node.Struct().begin(); it != node.Struct().end();) | ||||
| 		{ | ||||
| 			if (!vstd::contains(foundEntries, it->first)) | ||||
| 				it = node.Struct().erase(it); | ||||
| 			else | ||||
| 				it++; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void JsonUtils::maximize(JsonNode & node, const std::string & schemaName) | ||||
| { | ||||
| 	maximizeNode(node, getSchema(schemaName)); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user