Răsfoiți Sursa

Allow defining default values of settings per platform

Ivan Savenko 2 ani în urmă
părinte
comite
8f602b2ef6

+ 2 - 9
client/gui/CursorHandler.cpp

@@ -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>();

+ 7 - 7
config/schemas/settings.json

@@ -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

+ 0 - 1
launcher/settingsView/csettingsview_moc.cpp

@@ -38,7 +38,6 @@ QString resolutionToString(const QSize & resolution)
 
 static const std::string cursorTypesList[] =
 {
-	"auto",
 	"hardware",
 	"software"
 };

+ 1 - 6
launcher/settingsView/csettingsview_moc.ui

@@ -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>

+ 54 - 51
lib/JsonNode.cpp

@@ -1135,73 +1135,76 @@ 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")
-	{
-		std::set<std::string> foundEntries;
+	const JsonNode & fieldProps = schema["properties"][fieldName];
 
-		for(const auto & entry : schema["required"].Vector())
-		{
-			const std::string & name = entry.String();
-			foundEntries.insert(name);
+#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"];
+}
 
-			minimizeNode(node[name], schema["properties"][name]);
+static void eraseOptionalNodes(JsonNode & node, const JsonNode & schema)
+{
+	assert(schema["type"].String() == "object");
 
-			if (vstd::contains(node.Struct(), name) &&
-				node[name] == schema["properties"][name]["default"])
-			{
-				node.Struct().erase(name);
-			}
-		}
+	std::set<std::string> foundEntries;
 
-		// 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++;
-		}
-	}
+	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);
+	});
 }
 
-void JsonUtils::minimize(JsonNode & node, const std::string & schemaName)
+static void minimizeNode(JsonNode & node, const JsonNode & schema)
 {
-	minimizeNode(node, getSchema(schemaName));
+	if (schema["type"].String() != "object")
+		return;
+
+	for(const auto & entry : schema["required"].Vector())
+	{
+		const std::string & name = entry.String();
+		minimizeNode(node[name], schema["properties"][name]);
+
+		if (vstd::contains(node.Struct(), name) && node[name] == getDefaultValue(schema, name))
+			node.Struct().erase(name);
+	}
+	eraseOptionalNodes(node, schema);
 }
 
-// FIXME: except for several lines function is identical to minimizeNode. Some way to reduce duplication?
-void maximizeNode(JsonNode & node, const JsonNode & schema)
+static void maximizeNode(JsonNode & node, const JsonNode & schema)
 {
 	// "required" entry can only be found in object/struct
-	if (schema["type"].String() == "object")
+	if (schema["type"].String() != "object")
+		return;
+
+	// check all required entries that have default version
+	for(const auto & entry : schema["required"].Vector())
 	{
-		std::set<std::string> foundEntries;
+		const std::string & name = entry.String();
 
-		// 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() && !getDefaultValue(schema, name).isNull())
+			node[name] = getDefaultValue(schema, 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++;
-		}
+		maximizeNode(node[name], schema["properties"][name]);
 	}
+
+	eraseOptionalNodes(node, schema);
+}
+
+void JsonUtils::minimize(JsonNode & node, const std::string & schemaName)
+{
+	minimizeNode(node, getSchema(schemaName));
 }
 
 void JsonUtils::maximize(JsonNode & node, const std::string & schemaName)