Merge pull request #63907 from heppocogne/Fix-code-editor-colorpicker

Fix code editor's ColorPicker issue
This commit is contained in:
Rémi Verschelde 2023-02-11 14:34:39 +01:00
commit 9e4d378b39
No known key found for this signature in database
GPG Key ID: C3336907360768E1

View File

@ -1820,30 +1820,72 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
color_position.x = row;
color_position.y = col;
int begin = 0;
int end = 0;
bool valid = false;
int begin = -1;
int end = -1;
enum EXPRESSION_PATTERNS {
NOT_PARSED,
RGBA_PARAMETER, // Color(float,float,float) or Color(float,float,float,float)
COLOR_NAME, // Color.COLOR_NAME
} expression_pattern = NOT_PARSED;
for (int i = col; i < line.length(); i++) {
if (line[i] == '(') {
begin = i;
continue;
} else if (line[i] == ')') {
if (expression_pattern == NOT_PARSED) {
begin = i;
expression_pattern = RGBA_PARAMETER;
} else {
// Method call or '(' appearing twice.
expression_pattern = NOT_PARSED;
break;
}
} else if (expression_pattern == RGBA_PARAMETER && line[i] == ')' && end < 0) {
end = i + 1;
valid = true;
break;
} else if (expression_pattern == NOT_PARSED && line[i] == '.') {
begin = i;
expression_pattern = COLOR_NAME;
} else if (expression_pattern == COLOR_NAME && end < 0 && (line[i] == ' ' || line[i] == '\t')) {
// Including '.' and spaces.
continue;
} else if (expression_pattern == COLOR_NAME && !(line[i] == '_' || ('A' <= line[i] && line[i] <= 'Z'))) {
end = i;
break;
}
}
if (valid) {
color_args = line.substr(begin, end - begin);
String stripped = color_args.replace(" ", "").replace("(", "").replace(")", "");
PackedFloat64Array color = stripped.split_floats(",");
if (color.size() > 2) {
float alpha = color.size() > 3 ? color[3] : 1.0f;
color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));
}
switch (expression_pattern) {
case RGBA_PARAMETER: {
color_args = line.substr(begin, end - begin);
String stripped = color_args.replace(" ", "").replace("\t", "").replace("(", "").replace(")", "");
PackedFloat64Array color = stripped.split_floats(",");
if (color.size() > 2) {
float alpha = color.size() > 3 ? color[3] : 1.0f;
color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));
}
} break;
case COLOR_NAME: {
if (end < 0) {
end = line.length();
}
color_args = line.substr(begin, end - begin);
const String color_name = color_args.replace(" ", "").replace("\t", "").replace(".", "");
const int color_index = Color::find_named_color(color_name);
if (0 <= color_index) {
const Color color_constant = Color::get_named_color(color_index);
color_picker->set_pick_color(color_constant);
} else {
has_color = false;
}
} break;
default:
has_color = false;
break;
}
if (has_color) {
color_panel->set_position(get_screen_position() + local_pos);
} else {
has_color = false;
}
}
_make_context_menu(tx->has_selection(), has_color, foldable, open_docs, goto_definition, local_pos);