1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Fix ScriptEditor inline colors float handling

This commit is contained in:
kit
2025-06-23 13:16:50 -04:00
parent 4d1f26e1fd
commit 9184256ae4

View File

@@ -411,38 +411,53 @@ Array ScriptTextEditor::_inline_object_parse(const String &p_text, int p_line) {
while (i_start != -1) { while (i_start != -1) {
// Ignore words that just have "Color" in them. // Ignore words that just have "Color" in them.
if (i_start == 0 || !("_" + p_text.substr(i_start - 1, 1)).is_valid_ascii_identifier()) { if (i_start != 0 && ('_' + p_text.substr(i_start - 1, 1)).is_valid_ascii_identifier()) {
int i_par_start = p_text.find_char('(', i_start + 5); i_end_previous = MAX(i_end_previous, i_start);
if (i_par_start != -1) { i_start = p_text.find("Color", i_start + 1);
int i_par_end = p_text.find_char(')', i_start + 5); continue;
if (i_par_end != -1) { }
const int i_par_start = p_text.find_char('(', i_start + 5);
const int i_par_end = p_text.find_char(')', i_start + 5);
if (i_par_start == -1 || i_par_end == -1) {
i_end_previous = MAX(i_end_previous, i_start);
i_start = p_text.find("Color", i_start + 1);
continue;
}
Dictionary color_info; Dictionary color_info;
color_info["line"] = p_line; color_info["line"] = p_line;
color_info["column"] = i_start; color_info["column"] = i_start;
color_info["width_ratio"] = 1.0; color_info["width_ratio"] = 1.0;
color_info["color_end"] = i_par_end; color_info["color_end"] = i_par_end;
String fn_name = p_text.substr(i_start + 5, i_par_start - i_start - 5); const String fn_name = p_text.substr(i_start + 5, i_par_start - i_start - 5);
String s_params = p_text.substr(i_par_start + 1, i_par_end - i_par_start - 1); const String s_params = p_text.substr(i_par_start + 1, i_par_end - i_par_start - 1);
bool has_added_color = false; bool has_added_color = false;
if (fn_name.is_empty()) { if (fn_name.is_empty()) {
String stripped = s_params.strip_edges(true, true); String stripped = s_params.strip_edges(true, true);
if (stripped.length() > 1 && (stripped[0] == '"' || stripped[0] == '\'')) {
// String constructor. // String constructor.
if (stripped.length() > 0 && (stripped[0] == '\"')) { const char32_t string_delimiter = stripped[0];
String color_string = stripped.substr(1, stripped.length() - 2); if (stripped[stripped.length() - 1] == string_delimiter) {
color_info["color"] = Color(color_string); const String color_string = stripped.substr(1, stripped.length() - 2);
if (!color_string.contains_char(string_delimiter)) {
color_info["color"] = Color::from_string(color_string, Color());
color_info["color_mode"] = MODE_STRING; color_info["color_mode"] = MODE_STRING;
has_added_color = true; has_added_color = true;
} }
}
} else if (stripped.length() == 10 && stripped.begins_with("0x")) {
// Hex constructor. // Hex constructor.
else if (stripped.length() == 10 && stripped.substr(0, 2) == "0x") { const String color_string = stripped.substr(2);
color_info["color"] = Color(stripped.substr(2, stripped.length() - 2)); if (color_string.is_valid_hex_number(false)) {
color_info["color"] = Color::from_string(color_string, Color());
color_info["color_mode"] = MODE_HEX; color_info["color_mode"] = MODE_HEX;
has_added_color = true; has_added_color = true;
} }
} else if (stripped.is_empty()) {
// Empty Color() constructor. // Empty Color() constructor.
else if (stripped.is_empty()) {
color_info["color"] = Color(); color_info["color"] = Color();
color_info["color_mode"] = MODE_RGB; color_info["color_mode"] = MODE_RGB;
has_added_color = true; has_added_color = true;
@@ -450,12 +465,21 @@ Array ScriptTextEditor::_inline_object_parse(const String &p_text, int p_line) {
} }
// Float & int parameters. // Float & int parameters.
if (!has_added_color && s_params.size() > 0) { if (!has_added_color && s_params.size() > 0) {
PackedFloat64Array params = s_params.split_floats(",", false); const PackedStringArray s_params_split = s_params.split(",", false, 4);
if (params.size() == 3) { PackedFloat64Array params;
params.resize(4); bool valid_floats = true;
params.set(3, 1.0); for (const String &s_param : s_params_split) {
// Only allow float literals, expressions won't be evaluated and could get replaced.
if (!s_param.strip_edges().is_valid_float()) {
valid_floats = false;
break;
} }
if (params.size() == 4) { params.push_back(s_param.to_float());
}
if (valid_floats && params.size() == 3) {
params.push_back(1.0);
}
if (valid_floats && params.size() == 4) {
has_added_color = true; has_added_color = true;
if (fn_name == ".from_ok_hsl") { if (fn_name == ".from_ok_hsl") {
color_info["color"] = Color::from_ok_hsl(params[0], params[1], params[2], params[3]); color_info["color"] = Color::from_ok_hsl(params[0], params[1], params[2], params[3]);
@@ -479,9 +503,6 @@ Array ScriptTextEditor::_inline_object_parse(const String &p_text, int p_line) {
result.push_back(color_info); result.push_back(color_info);
i_end_previous = i_par_end + 1; i_end_previous = i_par_end + 1;
} }
}
}
}
i_end_previous = MAX(i_end_previous, i_start); i_end_previous = MAX(i_end_previous, i_start);
i_start = p_text.find("Color", i_start + 1); i_start = p_text.find("Color", i_start + 1);
} }