From e019ccc64ba365b07f53553d872a0959993bb42c Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 18 Aug 2022 20:36:36 +0200 Subject: [PATCH] Now text files will show up in the editor if the text editor plugin is enabled. Also clicking them will open them in the text editor. --- modules/text_editor/SCsub | 3 + .../icons/icon_text_editor_file.svg | 1 + modules/text_editor/register_types.cpp | 10 ++- modules/text_editor/text_editor_file.cpp | 15 ++++ modules/text_editor/text_editor_file.h | 22 ++++++ .../text_editor/text_editor_format_loader.cpp | 71 +++++++++++++++++++ .../text_editor/text_editor_format_loader.h | 42 +++++++++++ modules/text_editor/text_editor_plugin.cpp | 14 ++++ modules/text_editor/text_editor_plugin.h | 3 + 9 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 modules/text_editor/icons/icon_text_editor_file.svg create mode 100644 modules/text_editor/text_editor_file.cpp create mode 100644 modules/text_editor/text_editor_file.h create mode 100644 modules/text_editor/text_editor_format_loader.cpp create mode 100644 modules/text_editor/text_editor_format_loader.h diff --git a/modules/text_editor/SCsub b/modules/text_editor/SCsub index 54c1f8eb2..b3ae309db 100644 --- a/modules/text_editor/SCsub +++ b/modules/text_editor/SCsub @@ -6,6 +6,9 @@ module_env = env.Clone() module_env.add_source_files(env.modules_sources,"register_types.cpp") +module_env.add_source_files(env.modules_sources,"text_editor_file.cpp") +module_env.add_source_files(env.modules_sources,"text_editor_format_loader.cpp") + if env["tools"]: module_env.add_source_files(env.modules_sources,"text_editor_plugin.cpp") module_env.add_source_files(env.modules_sources,"text_editor_settings.cpp") diff --git a/modules/text_editor/icons/icon_text_editor_file.svg b/modules/text_editor/icons/icon_text_editor_file.svg new file mode 100644 index 000000000..f480217dc --- /dev/null +++ b/modules/text_editor/icons/icon_text_editor_file.svg @@ -0,0 +1 @@ + diff --git a/modules/text_editor/register_types.cpp b/modules/text_editor/register_types.cpp index bc9e5a214..add01078e 100644 --- a/modules/text_editor/register_types.cpp +++ b/modules/text_editor/register_types.cpp @@ -22,12 +22,20 @@ SOFTWARE. #include "register_types.h" +#include "text_editor_file.h" +#include "text_editor_format_loader.h" + #ifdef TOOLS_ENABLED #include "text_editor_plugin.h" +#include "editor/editor_plugin.h" #endif void register_text_editor_types() { - //ClassDB::register_class(); + ClassDB::register_class(); + + Ref loader; + loader.instance(); + ResourceLoader::add_resource_format_loader(loader); #ifdef TOOLS_ENABLED EditorPlugins::add_by_type(); diff --git a/modules/text_editor/text_editor_file.cpp b/modules/text_editor/text_editor_file.cpp new file mode 100644 index 000000000..700f13650 --- /dev/null +++ b/modules/text_editor/text_editor_file.cpp @@ -0,0 +1,15 @@ +#include "text_editor_file.h" + +String TextEditorFile::get_file_path() { + return _file_path; +} + +void TextEditorFile::set_file_path(const String &file_path) { + _file_path = file_path; +} + +void TextEditorFile::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_file_path"), &TextEditorFile::get_file_path); + ClassDB::bind_method(D_METHOD("set_file_path", "file_path"), &TextEditorFile::set_file_path); + //ADD_PROPERTY(PropertyInfo(Variant::STRING, "file_path"), "set_file_path", "get_file_path"); +} diff --git a/modules/text_editor/text_editor_file.h b/modules/text_editor/text_editor_file.h new file mode 100644 index 000000000..6ca908a37 --- /dev/null +++ b/modules/text_editor/text_editor_file.h @@ -0,0 +1,22 @@ +#ifndef TEXT_EDITOR_FILE_H +#define TEXT_EDITOR_FILE_H + +#include "core/string/ustring.h" + +#include "core/object/resource.h" + +class TextEditorFile : public Resource { + GDCLASS(TextEditorFile, Resource); + +public: + String get_file_path(); + void set_file_path(const String &file_path); + +protected: + static void _bind_methods(); + +private: + String _file_path; +}; + +#endif diff --git a/modules/text_editor/text_editor_format_loader.cpp b/modules/text_editor/text_editor_format_loader.cpp new file mode 100644 index 000000000..3b74abb3a --- /dev/null +++ b/modules/text_editor/text_editor_format_loader.cpp @@ -0,0 +1,71 @@ +/* +Copyright (c) 2019-2022 Péter Magyar + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "text_editor_format_loader.h" + +#include "text_editor_file.h" + +RES TextEditorTextLoader::load(const String &p_path, const String &p_original_path, Error *r_error) { + if (r_error) { + *r_error = ERR_FILE_CANT_OPEN; + } + + Ref file; + file.instance(); + file->set_file_path(p_original_path); + + if (r_error) { + *r_error = OK; + } + + return file; +} + +void TextEditorTextLoader::get_recognized_extensions(List *p_extensions) const { + p_extensions->push_back("txt"); + p_extensions->push_back("md"); + p_extensions->push_back("xml"); + p_extensions->push_back("sql"); + p_extensions->push_back("csv"); + p_extensions->push_back("cfg"); + p_extensions->push_back("ini"); + p_extensions->push_back("log"); + p_extensions->push_back("json"); + p_extensions->push_back("yml"); + p_extensions->push_back("yaml"); + p_extensions->push_back("toml"); + p_extensions->push_back("html"); + p_extensions->push_back("js"); + p_extensions->push_back("css"); + p_extensions->push_back("htmpl"); +} + +bool TextEditorTextLoader::handles_type(const String &p_type) const { + return p_type == "TextEditorFile"; +} + +String TextEditorTextLoader::get_resource_type(const String &p_path) const { + return "TextEditorFile"; +} + +void TextEditorTextLoader::get_dependencies(const String &p_path, List *p_dependencies, bool p_add_types) { +} diff --git a/modules/text_editor/text_editor_format_loader.h b/modules/text_editor/text_editor_format_loader.h new file mode 100644 index 000000000..756a656b3 --- /dev/null +++ b/modules/text_editor/text_editor_format_loader.h @@ -0,0 +1,42 @@ +#ifndef TEXT_EDITOR_FORMAT_LOADER_H +#define TEXT_EDITOR_FORMAT_LOADER_H + +/* +Copyright (c) 2019-2022 Péter Magyar + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "core/io/resource_loader.h" + +class TextFileEditor; +class Texture; + +class TextEditorTextLoader : public ResourceFormatLoader { + GDCLASS(TextEditorTextLoader, ResourceFormatLoader); + +public: + virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr); + virtual void get_recognized_extensions(List *p_extensions) const; + virtual bool handles_type(const String &p_type) const; + virtual String get_resource_type(const String &p_path) const; + virtual void get_dependencies(const String &p_path, List *p_dependencies, bool p_add_types = false); +}; + +#endif diff --git a/modules/text_editor/text_editor_plugin.cpp b/modules/text_editor/text_editor_plugin.cpp index c566e4e9d..36c5baa31 100644 --- a/modules/text_editor/text_editor_plugin.cpp +++ b/modules/text_editor/text_editor_plugin.cpp @@ -22,6 +22,7 @@ SOFTWARE. #include "text_editor_plugin.h" +#include "text_editor_file.h" #include "text_file_editor.h" void TextEditorEditorPlugin::make_visible(const bool visible) { @@ -35,10 +36,23 @@ String TextEditorEditorPlugin::get_name() const { const Ref TextEditorEditorPlugin::get_icon() const { return _icon; } + bool TextEditorEditorPlugin::has_main_screen() const { return true; } +void TextEditorEditorPlugin::edit(Object *p_object) { + Ref f = Object::cast_to(p_object); + + if (f.is_valid()) { + window->open_file(f->get_path()); + } +} + +bool TextEditorEditorPlugin::handles(Object *p_object) const { + return p_object->is_class("TextEditorFile"); +} + TextEditorEditorPlugin::TextEditorEditorPlugin(EditorNode *p_node) { editor = p_node; diff --git a/modules/text_editor/text_editor_plugin.h b/modules/text_editor/text_editor_plugin.h index 76a0fcb8f..2191f79dd 100644 --- a/modules/text_editor/text_editor_plugin.h +++ b/modules/text_editor/text_editor_plugin.h @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include "core/io/resource_importer.h" #include "core/object/reference.h" #include "editor/editor_plugin.h" @@ -37,6 +38,8 @@ public: const Ref get_icon() const; bool has_main_screen() const; String get_name() const; + void edit(Object *p_object); + bool handles(Object *p_object) const; TextEditorEditorPlugin(EditorNode *p_node); ~TextEditorEditorPlugin();