Browse Source

Merge pull request #74237 from AThousandShips/convert_keycode

Add keycode project conversion
Rémi Verschelde 2 years ago
parent
commit
743c86768a
2 changed files with 65 additions and 0 deletions
  1. 62 0
      editor/project_converter_3_to_4.cpp
  2. 3 0
      editor/project_converter_3_to_4.h

+ 62 - 0
editor/project_converter_3_to_4.cpp

@@ -139,6 +139,9 @@ public:
 	LocalVector<RegEx *> class_gd_regexes;
 	LocalVector<RegEx *> class_shader_regexes;
 
+	// Keycode.
+	RegEx input_map_keycode = RegEx("\\b,\"((physical_)?)scancode\":(\\d+)\\b");
+
 	LocalVector<RegEx *> class_regexes;
 
 	RegEx class_temp_tscn = RegEx("\\bTEMP_RENAMED_CLASS.tscn\\b");
@@ -415,6 +418,7 @@ bool ProjectConverter3To4::convert() {
 			} else if (file_name.ends_with("project.godot")) {
 				rename_common(RenamesMap3To4::project_godot_renames, reg_container.project_godot_regexes, lines);
 				rename_common(RenamesMap3To4::builtin_types_renames, reg_container.builtin_types_regexes, lines);
+				rename_input_map_scancode(lines, reg_container);
 				rename_common(RenamesMap3To4::input_map_renames, reg_container.input_map_regexes, lines);
 			} else if (file_name.ends_with(".csproj")) {
 				// TODO
@@ -587,6 +591,7 @@ bool ProjectConverter3To4::validate_conversion() {
 			} else if (file_name.ends_with("project.godot")) {
 				changed_elements.append_array(check_for_rename_common(RenamesMap3To4::project_godot_renames, reg_container.project_godot_regexes, lines));
 				changed_elements.append_array(check_for_rename_common(RenamesMap3To4::builtin_types_renames, reg_container.builtin_types_regexes, lines));
+				changed_elements.append_array(check_for_rename_input_map_scancode(lines, reg_container));
 				changed_elements.append_array(check_for_rename_common(RenamesMap3To4::input_map_renames, reg_container.input_map_regexes, lines));
 			} else if (file_name.ends_with(".csproj")) {
 				// TODO
@@ -913,6 +918,10 @@ bool ProjectConverter3To4::test_conversion(RegExContainer &reg_container) {
 
 	valid = valid && test_conversion_with_regex("AAA Color.white AF", "AAA Color.WHITE AF", &ProjectConverter3To4::rename_colors, "custom rename", reg_container);
 
+	// Note: Do not change to *scancode*, it is applied before that conversion.
+	valid = valid && test_conversion_with_regex("\"device\":-1,\"scancode\":16777231,\"physical_scancode\":16777232", "\"device\":-1,\"scancode\":4194319,\"physical_scancode\":4194320", &ProjectConverter3To4::rename_input_map_scancode, "custom rename", reg_container);
+	valid = valid && test_conversion_with_regex("\"device\":-1,\"scancode\":65,\"physical_scancode\":66", "\"device\":-1,\"scancode\":65,\"physical_scancode\":66", &ProjectConverter3To4::rename_input_map_scancode, "custom rename", reg_container);
+
 	// Custom rule conversion
 	{
 		String from = "instance";
@@ -2495,6 +2504,59 @@ Vector<String> ProjectConverter3To4::check_for_rename_gdscript_keywords(Vector<S
 	return found_renames;
 }
 
+void ProjectConverter3To4::rename_input_map_scancode(Vector<String> &lines, const RegExContainer &reg_container) {
+	// The old Special Key, now colliding with CMD_OR_CTRL.
+	const int old_spkey = (1 << 24);
+
+	for (String &line : lines) {
+		if (uint64_t(line.length()) <= maximum_line_length) {
+			TypedArray<RegExMatch> reg_match = reg_container.input_map_keycode.search_all(line);
+
+			for (int i = 0; i < reg_match.size(); ++i) {
+				Ref<RegExMatch> match = reg_match[i];
+				PackedStringArray strings = match->get_strings();
+				int key = strings[3].to_int();
+
+				if (key & old_spkey) {
+					// Create new key, clearing old Special Key and setting new one.
+					key = (key & ~old_spkey) | (int)Key::SPECIAL;
+
+					line = line.replace(strings[0], String(",\"") + strings[1] + "scancode\":" + String::num_int64(key));
+				}
+			}
+		}
+	}
+}
+
+Vector<String> ProjectConverter3To4::check_for_rename_input_map_scancode(Vector<String> &lines, const RegExContainer &reg_container) {
+	Vector<String> found_renames;
+
+	// The old Special Key, now colliding with CMD_OR_CTRL.
+	const int old_spkey = (1 << 24);
+
+	int current_line = 1;
+	for (String &line : lines) {
+		if (uint64_t(line.length()) <= maximum_line_length) {
+			TypedArray<RegExMatch> reg_match = reg_container.input_map_keycode.search_all(line);
+
+			for (int i = 0; i < reg_match.size(); ++i) {
+				Ref<RegExMatch> match = reg_match[i];
+				PackedStringArray strings = match->get_strings();
+				int key = strings[3].to_int();
+
+				if (key & old_spkey) {
+					// Create new key, clearing old Special Key and setting new one.
+					key = (key & ~old_spkey) | (int)Key::SPECIAL;
+
+					found_renames.append(line_formatter(current_line, strings[3], String::num_int64(key), line));
+				}
+			}
+		}
+		current_line++;
+	}
+	return found_renames;
+}
+
 void ProjectConverter3To4::custom_rename(Vector<String> &lines, String from, String to) {
 	RegEx reg = RegEx(String("\\b") + from + "\\b");
 	CRASH_COND(!reg.is_valid());

+ 3 - 0
editor/project_converter_3_to_4.h

@@ -86,6 +86,9 @@ class ProjectConverter3To4 {
 	void rename_gdscript_keywords(Vector<String> &lines, const RegExContainer &reg_container);
 	Vector<String> check_for_rename_gdscript_keywords(Vector<String> &lines, const RegExContainer &reg_container);
 
+	void rename_input_map_scancode(Vector<String> &lines, const RegExContainer &reg_container);
+	Vector<String> check_for_rename_input_map_scancode(Vector<String> &lines, const RegExContainer &reg_container);
+
 	void custom_rename(Vector<String> &lines, String from, String to);
 	Vector<String> check_for_custom_rename(Vector<String> &lines, String from, String to);