Skip to content

Commit

Permalink
Implemented clearing custom fonts in the editor's text editor module.…
Browse files Browse the repository at this point in the history
… Also small improvements.
  • Loading branch information
Relintai committed Jan 12, 2024
1 parent 2a0b2b6 commit 441c52a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
17 changes: 11 additions & 6 deletions editor_modules/text_editor/text_editor_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void TextEditorSettings::remove_opened_file(const int index, ItemList *fileconta
EditorSettings::get_singleton()->set_project_metadata("file_editor", "files", arr);

Dictionary fonts_dict = EditorSettings::get_singleton()->get_project_metadata("file_editor", "file_fonts", Dictionary());

if (fonts_dict.has(f)) {
fonts_dict.erase(f);
EditorSettings::get_singleton()->set_project_metadata("file_editor", "file_fonts", fonts_dict);
Expand All @@ -115,22 +115,23 @@ Array TextEditorSettings::load_opened_files() {
if (fonts_dict.has(a[1])) {
k.push_back(fonts_dict[a[1]]);
} else {
k.push_back("null");
k.push_back("");
}

keys.append(k);
}

ERR_PRINT(Variant(keys));

return keys;
}

void TextEditorSettings::store_editor_fonts(const String &file_path, const String &font_path) {
Dictionary fonts_dict = EditorSettings::get_singleton()->get_project_metadata("file_editor", "file_fonts", Dictionary());
fonts_dict[file_path] = font_path;

ERR_PRINT(Variant(fonts_dict));
if (!font_path.empty()) {
fonts_dict[file_path] = font_path;
} else {
fonts_dict.erase(file_path);
}

EditorSettings::get_singleton()->set_project_metadata("file_editor", "file_fonts", fonts_dict);
}
Expand All @@ -139,6 +140,10 @@ String TextEditorSettings::get_editor_font() {
return EditorSettings::get_singleton()->get_setting("interface/editor/code_font");
}

void TextEditorSettings::clear_editor_fonts() {
EditorSettings::get_singleton()->set_project_metadata("file_editor", "file_fonts", Dictionary());
}

TextEditorSettings::TextEditorSettings() {
}

Expand Down
2 changes: 2 additions & 0 deletions editor_modules/text_editor/text_editor_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class TextEditorSettings : public Reference {
void store_opened_files(ItemList *filecontainer);
void remove_opened_file(const int index, ItemList *filecontainer);
Array load_opened_files();

void store_editor_fonts(const String &file_name, const String &font_path);
String get_editor_font();
void clear_editor_fonts();

TextEditorSettings();
~TextEditorSettings();
Expand Down
7 changes: 6 additions & 1 deletion editor_modules/text_editor/text_editor_vanilla_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ void TextEditorVanillaEditor::set_search_flag(const int val) {
}

void TextEditorVanillaEditor::set_font(const String &font_path) {
if (font_path.empty()) {
text_editor->remove_theme_font_override("font");
return;
}

Ref<DynamicFont> dynamic_font;
Ref<DynamicFontData> dynamic_font_data;

Expand All @@ -83,7 +88,7 @@ void TextEditorVanillaEditor::set_font(const String &font_path) {

dynamic_font_data->set_font_path(font_path);
dynamic_font->set_font_data(dynamic_font_data);
text_editor->set("custom_fonts/font", dynamic_font);
text_editor->add_theme_font_override("font", dynamic_font);
}

void TextEditorVanillaEditor::load_default_font() {
Expand Down
27 changes: 25 additions & 2 deletions editor_modules/text_editor/text_file_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,27 @@ void TextFileEditor::_on_preview_btn_pressed(const int id) {
void TextFileEditor::_on_settings_btn_pressed(const int index) {
if (index == 0) {
select_font_dialog->popup();
} else if (index == 1) {
if (!current_file_path.empty()) {
_text_editor_settings->store_editor_fonts(current_file_path, "");
}

if (current_editor) {
current_editor->set_font("");
}
} else if (index == 2) {
_text_editor_settings->clear_editor_fonts();

for (int i = 0; i < _open_file_list->get_item_count(); ++i) {
Array selected_item_metadata = _open_file_list->get_item_metadata(i);

ObjectID editor = selected_item_metadata[0];
TextEditorVanillaEditor *e = Object::cast_to<TextEditorVanillaEditor>(ObjectDB::get_instance(editor));

if (e) {
e->set_font("");
}
}
}
}

Expand Down Expand Up @@ -250,7 +271,7 @@ void TextFileEditor::open_file(const String &path, const String &font) {
current_file_path = path;
TextEditorVanillaEditor *vanilla_editor = open_in_vanillaeditor(path);

if (font != "null") {
if (!font.empty()) {
vanilla_editor->set_font(font);
}

Expand Down Expand Up @@ -651,6 +672,8 @@ TextFileEditor::TextFileEditor() {

settings_btn_popup = settings_btn->get_popup();
settings_btn_popup->add_item("Change Font");
settings_btn_popup->add_item("Clear Font");
settings_btn_popup->add_item("Clear All Fonts");

//SplitContainer;
editor_container = memnew(HSplitContainer);
Expand Down Expand Up @@ -801,7 +824,7 @@ void TextFileEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_on_font_selected", "font_path"), &TextFileEditor::_on_font_selected);
ClassDB::bind_method(D_METHOD("_on_fileitem_pressed", "index"), &TextFileEditor::_on_fileitem_pressed);

ClassDB::bind_method(D_METHOD("open_file", "path", "font"), &TextFileEditor::open_file, "null");
ClassDB::bind_method(D_METHOD("open_file", "path", "font"), &TextFileEditor::open_file, "");
//ClassDB::bind_method(D_METHOD("generate_file_item", "path", "veditor"), &TextFileEditor::generate_file_item);
//ClassDB::bind_method(D_METHOD("open_in_vanillaeditor", "path"), &TextFileEditor::open_in_vanillaeditor);

Expand Down
2 changes: 1 addition & 1 deletion editor_modules/text_editor/text_file_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class TextFileEditor : public Control {
void _on_font_selected(const String &font_path);
void _on_fileitem_pressed(const int index);

void open_file(const String &path, const String &font = "null");
void open_file(const String &path, const String &font = "");
void generate_file_item(const String &path, Control *veditor);
TextEditorVanillaEditor *open_in_vanillaeditor(const String &path);
void close_file(const int index);
Expand Down

0 comments on commit 441c52a

Please sign in to comment.