[Editor] Fix font style matching issues.

Fix font selection when no style selected.
Fix style matching when fonts have different style sets.
Use SNAME for theme overrides.
This commit is contained in:
bruvzg 2022-02-07 22:40:28 +02:00
parent b6ddf4a629
commit 8e3245383a
No known key found for this signature in database
GPG Key ID: 7960FCF39844EC38
1 changed files with 69 additions and 79 deletions

View File

@ -110,7 +110,7 @@ void TextControlEditor::_update_styles_menu() {
for (Map<String, String>::Element *E = fonts[name].front(); E; E = E->next()) { for (Map<String, String>::Element *E = fonts[name].front(); E; E = E->next()) {
font_style_list->add_item(E->key()); font_style_list->add_item(E->key());
} }
} else { } else if (font_list->get_selected() >= 0) {
font_style_list->add_item("Default"); font_style_list->add_item("Default");
} }
@ -123,9 +123,9 @@ void TextControlEditor::_update_styles_menu() {
void TextControlEditor::_update_control() { void TextControlEditor::_update_control() {
if (!edited_controls.is_empty()) { if (!edited_controls.is_empty()) {
int font_selected = 0; String font_selected;
bool same_font = true; bool same_font = true;
int style_selected = 0; String style_selected;
bool same_style = true; bool same_style = true;
int font_size = 0; int font_size = 0;
bool same_font_size = true; bool same_font_size = true;
@ -136,26 +136,23 @@ void TextControlEditor::_update_control() {
Color outline_color = Color{ 1.0f, 1.0f, 1.0f }; Color outline_color = Color{ 1.0f, 1.0f, 1.0f };
bool same_outline_color = true; bool same_outline_color = true;
_update_fonts_menu();
_update_styles_menu();
int count = edited_controls.size(); int count = edited_controls.size();
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
Control *edited_control = edited_controls[i]; Control *edited_control = edited_controls[i];
String edited_color; StringName edited_color;
String edited_font; StringName edited_font;
String edited_font_size; StringName edited_font_size;
// Get override names. // Get override names.
if (edited_control->is_class("RichTextLabel")) { if (Object::cast_to<RichTextLabel>(edited_control)) {
edited_color = "default_color"; edited_color = SNAME("default_color");
edited_font = "normal_font"; edited_font = SNAME("normal_font");
edited_font_size = "normal_font_size"; edited_font_size = SNAME("normal_font_size");
} else { } else {
edited_color = "font_color"; edited_color = SNAME("font_color");
edited_font = "font"; edited_font = SNAME("font");
edited_font_size = "font_size"; edited_font_size = SNAME("font_size");
} }
// Get font override. // Get font override.
@ -166,57 +163,40 @@ void TextControlEditor::_update_control() {
if (font.is_valid()) { if (font.is_valid()) {
if (font->get_data_count() != 1) { if (font->get_data_count() != 1) {
custom_font = font;
if (i > 0) { if (i > 0) {
same_font = same_font && (font_selected == FONT_INFO_USER_CUSTOM); same_font = same_font && (custom_font == font);
same_style = same_style && (style_selected == 0);
} }
custom_font = font;
font_selected = FONT_INFO_USER_CUSTOM; font_selected = TTR("[Custom Font]");
style_selected = 0; same_style = false;
} else { } else {
String name = font->get_data(0)->get_font_name(); String name = font->get_data(0)->get_font_name();
String style = font->get_data(0)->get_font_style_name(); String style = font->get_data(0)->get_font_style_name();
if (fonts.has(name) && fonts[name].has(style)) { if (fonts.has(name) && fonts[name].has(style)) {
for (int j = 0; j < font_list->get_item_count(); j++) {
if (font_list->get_item_text(j) == name) {
if (i > 0) {
same_font = same_font && (j == font_selected);
}
font_selected = j;
break;
}
}
for (int j = 0; j < font_style_list->get_item_count(); j++) {
if (font_style_list->get_item_text(j) == style) {
if (i > 0) {
same_style = same_style && (j == style_selected);
}
style_selected = j;
break;
}
}
} else {
custom_font = font;
if (i > 0) { if (i > 0) {
same_font = same_font && (font_selected == FONT_INFO_USER_CUSTOM); same_font = same_font && (name == font_selected);
same_style = same_style && (style_selected == 0); same_style = same_style && (style == style_selected);
} }
font_selected = name;
style_selected = style;
} else {
if (i > 0) {
same_font = same_font && (custom_font == font);
}
custom_font = font;
font_selected = FONT_INFO_USER_CUSTOM; font_selected = TTR("[Custom Font]");
style_selected = 0; same_style = false;
} }
} }
} else { } else {
if (i > 0) { if (i > 0) {
same_font = same_font && (font_selected == FONT_INFO_THEME_DEFAULT); same_font = same_font && (font_selected == TTR("[Theme Default]"));
same_style = same_style && (style_selected == 0);
} }
font_selected = FONT_INFO_THEME_DEFAULT; font_selected = TTR("[Theme Default]");
style_selected = 0; same_style = false;
} }
int current_font_size = edited_control->get_theme_font_size(edited_font_size); int current_font_size = edited_control->get_theme_font_size(edited_font_size);
@ -235,19 +215,29 @@ void TextControlEditor::_update_control() {
font_color = current_font_color; font_color = current_font_color;
outline_color = current_outline_color; outline_color = current_outline_color;
} }
_update_fonts_menu(); _update_fonts_menu();
if (same_font) { if (same_font) {
font_list->select(font_selected); for (int j = 0; j < font_list->get_item_count(); j++) {
if (font_list->get_item_text(j) == font_selected) {
font_list->select(j);
break;
}
}
} else { } else {
custom_font = Ref<Font>();
font_list->select(-1); font_list->select(-1);
} }
_update_styles_menu(); _update_styles_menu();
if (same_style) { if (same_style) {
font_style_list->select(style_selected); for (int j = 0; j < font_style_list->get_item_count(); j++) {
if (font_style_list->get_item_text(j) == style_selected) {
font_style_list->select(j);
break;
}
}
} else { } else {
font_list->select(-1); font_style_list->select(-1);
} }
// Get other theme overrides. // Get other theme overrides.
@ -282,6 +272,7 @@ void TextControlEditor::_update_control() {
} }
void TextControlEditor::_font_selected(int p_id) { void TextControlEditor::_font_selected(int p_id) {
_update_styles_menu();
_set_font(); _set_font();
} }
@ -301,11 +292,11 @@ void TextControlEditor::_set_font() {
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
Control *edited_control = edited_controls[i]; Control *edited_control = edited_controls[i];
String edited_font; StringName edited_font;
if (edited_control->is_class("RichTextLabel")) { if (Object::cast_to<RichTextLabel>(edited_control)) {
edited_font = "normal_font"; edited_font = SNAME("normal_font");
} else { } else {
edited_font = "font"; edited_font = SNAME("font");
} }
if (font_list->get_selected_id() == FONT_INFO_THEME_DEFAULT) { if (font_list->get_selected_id() == FONT_INFO_THEME_DEFAULT) {
@ -314,14 +305,13 @@ void TextControlEditor::_set_font() {
} else if (font_list->get_selected_id() == FONT_INFO_USER_CUSTOM) { } else if (font_list->get_selected_id() == FONT_INFO_USER_CUSTOM) {
// Restore "custom_font". // Restore "custom_font".
ur->add_do_method(edited_control, "add_theme_font_override", edited_font, custom_font); ur->add_do_method(edited_control, "add_theme_font_override", edited_font, custom_font);
} else { } else if (font_list->get_selected() >= 0) {
// Load new font resource using selected name and style. // Load new font resource using selected name and style.
String name = font_list->get_item_text(font_list->get_selected()); String name = font_list->get_item_text(font_list->get_selected());
String style = font_style_list->get_item_text(font_style_list->get_selected()); String style = font_style_list->get_item_text(font_style_list->get_selected());
if (style.is_empty()) { if (style.is_empty()) {
style = "Default"; style = "Default";
} }
if (fonts.has(name)) { if (fonts.has(name)) {
Ref<FontData> fd = ResourceLoader::load(fonts[name][style]); Ref<FontData> fd = ResourceLoader::load(fonts[name][style]);
if (fd.is_valid()) { if (fd.is_valid()) {
@ -358,11 +348,11 @@ void TextControlEditor::_font_size_selected(double p_size) {
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
Control *edited_control = edited_controls[i]; Control *edited_control = edited_controls[i];
String edited_font_size; StringName edited_font_size;
if (edited_control->is_class("RichTextLabel")) { if (Object::cast_to<RichTextLabel>(edited_control)) {
edited_font_size = "normal_font_size"; edited_font_size = SNAME("normal_font_size");
} else { } else {
edited_font_size = "font_size"; edited_font_size = SNAME("font_size");
} }
ur->add_do_method(edited_control, "add_theme_font_size_override", edited_font_size, p_size); ur->add_do_method(edited_control, "add_theme_font_size_override", edited_font_size, p_size);
@ -417,11 +407,11 @@ void TextControlEditor::_font_color_changed(const Color &p_color) {
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
Control *edited_control = edited_controls[i]; Control *edited_control = edited_controls[i];
String edited_color; StringName edited_color;
if (edited_control->is_class("RichTextLabel")) { if (Object::cast_to<RichTextLabel>(edited_control)) {
edited_color = "default_color"; edited_color = SNAME("default_color");
} else { } else {
edited_color = "font_color"; edited_color = SNAME("font_color");
} }
ur->add_do_method(edited_control, "add_theme_color_override", edited_color, p_color); ur->add_do_method(edited_control, "add_theme_color_override", edited_color, p_color);
@ -476,19 +466,19 @@ void TextControlEditor::_clear_formatting() {
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
Control *edited_control = edited_controls[i]; Control *edited_control = edited_controls[i];
String edited_color; StringName edited_color;
String edited_font; StringName edited_font;
String edited_font_size; StringName edited_font_size;
// Get override names. // Get override names.
if (edited_control->is_class("RichTextLabel")) { if (Object::cast_to<RichTextLabel>(edited_control)) {
edited_color = "default_color"; edited_color = SNAME("default_color");
edited_font = "normal_font"; edited_font = SNAME("normal_font");
edited_font_size = "normal_font_size"; edited_font_size = SNAME("normal_font_size");
} else { } else {
edited_color = "font_color"; edited_color = SNAME("font_color");
edited_font = "font"; edited_font = SNAME("font");
edited_font_size = "font_size"; edited_font_size = SNAME("font_size");
} }
ur->add_do_method(edited_control, "begin_bulk_theme_override"); ur->add_do_method(edited_control, "begin_bulk_theme_override");