Skip to content
0.2.0-dev Download ZIP
303 lines ·10.7 KB Raw
1/**************************************************************************/
2/* script_editor_base.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#pragma once
32
33#include "editor/gui/code_editor.h"
34#include "scene/gui/box_container.h"
35
36class EditorSyntaxHighlighter;
37class MenuButton;
38class VSplitContainer;
39class DocumentEditorContainer;
40
41class ScriptEditorBase : public VBoxContainer {
42 GDCLASS(ScriptEditorBase, VBoxContainer);
43
44protected:
45 Ref<Resource> edited_res;
46
47 static void _bind_methods();
48
49public:
50 class EditMenusBase : public HBoxContainer {
51 GDCLASS(EditMenusBase, HBoxContainer);
52
53 protected:
54 DocumentEditorContainer *document_editor_container = nullptr;
55
56 public:
57 virtual bool handles(ScriptEditorBase *p_seb) = 0;
58 };
59
60 struct EditedFileData {
61 String path;
62 uint64_t last_modified_time = -1;
63 } edited_file_data;
64
65 String get_document_name() const;
66 virtual String get_doc_url_path() { return "/"; }
67 virtual Ref<Texture2D> get_theme_icon();
68
69 virtual bool show_members_overview() { return false; }
70
71 virtual void ensure_focus() = 0;
72
73 virtual void set_edited_resource(const Ref<Resource> &p_res) = 0;
74 virtual Ref<Resource> get_edited_resource() const { return edited_res; }
75
76 virtual void apply_code() = 0;
77 virtual void validate_script() = 0;
78 virtual bool is_unsaved() = 0;
79 virtual void tag_saved_version();
80
81 virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {}
82 virtual Control *get_base_editor() const { return nullptr; }
83};
84
85typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const Ref<Resource> &p_resource);
86
87class TextEditorBase : public ScriptEditorBase {
88 GDCLASS(TextEditorBase, ScriptEditorBase);
89
90protected:
91 enum {
92 EDIT_UNDO,
93 EDIT_REDO,
94 EDIT_CUT,
95 EDIT_COPY,
96 EDIT_PASTE,
97 EDIT_SELECT_ALL,
98 EDIT_TRIM_TRAILING_WHITESAPCE,
99 EDIT_TRIM_FINAL_NEWLINES,
100 EDIT_CONVERT_INDENT_TO_SPACES,
101 EDIT_CONVERT_INDENT_TO_TABS,
102 EDIT_MOVE_LINE_UP,
103 EDIT_MOVE_LINE_DOWN,
104 EDIT_INDENT,
105 EDIT_UNINDENT,
106 EDIT_DELETE_LINE,
107 EDIT_JOIN_LINES,
108 EDIT_DUPLICATE_SELECTION,
109 EDIT_DUPLICATE_LINES,
110 EDIT_TO_UPPERCASE,
111 EDIT_TO_LOWERCASE,
112 EDIT_CAPITALIZE,
113 EDIT_TOGGLE_FOLD_LINE,
114 EDIT_FOLD_ALL_LINES,
115 EDIT_TOGGLE_WORD_WRAP,
116 EDIT_UNFOLD_ALL_LINES,
117 EDIT_EMOJI_AND_SYMBOL,
118
119 SEARCH_FIND,
120 SEARCH_FIND_NEXT,
121 SEARCH_FIND_PREV,
122 SEARCH_REPLACE,
123 SEARCH_IN_FILES,
124 SEARCH_GOTO_LINE,
125
126 REPLACE_IN_FILES,
127
128 BOOKMARK_TOGGLE,
129 BOOKMARK_GOTO_NEXT,
130 BOOKMARK_GOTO_PREV,
131 BOOKMARK_REMOVE_ALL,
132
133 BASE_ENUM_COUNT,
134 };
135
136 class EditMenus : public EditMenusBase {
137 GDCLASS(EditMenus, EditMenusBase);
138
139 protected:
140 MenuButton *edit_menu = nullptr;
141 MenuButton *search_menu = nullptr;
142 MenuButton *goto_menu = nullptr;
143 PopupMenu *bookmarks_menu = nullptr;
144 PopupMenu *highlighter_menu = nullptr;
145
146 PopupMenu *edit_menu_line = nullptr;
147 PopupMenu *edit_menu_fold = nullptr;
148 PopupMenu *edit_menu_convert_indent = nullptr;
149 PopupMenu *edit_menu_convert = nullptr;
150
151 TextEditorBase *_get_active_editor();
152 void _edit_option(int p_op);
153 void _prepare_edit_menu();
154 void _update_highlighter_menu();
155 void _change_syntax_highlighter(int p_idx);
156 void _update_bookmark_list();
157 void _bookmark_item_pressed(int p_idx);
158
159 public:
160 EditMenus(DocumentEditorContainer *p_document_editor_container);
161 };
162
163 static void _popup_move_item(int p_target_id, PopupMenu *r_popup, bool p_move_after = true, int p_idx = -1) {
164 int target_idx = r_popup->get_item_index(p_target_id) + p_move_after;
165 if (target_idx >= 0 && target_idx < r_popup->get_item_count()) {
166 r_popup->set_item_index(p_idx, target_idx);
167 }
168 }
169
170 bool validation_success = true;
171 bool was_unsaved = false;
172
173 bool editor_enabled = false;
174 CodeTextEditor *code_editor = nullptr;
175
176 GotoLinePopup *goto_line_popup = nullptr;
177
178 LocalVector<Ref<EditorSyntaxHighlighter>> highlighters;
179
180 PopupMenu *context_menu = nullptr;
181
182 virtual void _make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position = Vector2(0, 0), bool p_show = true);
183 void _show_context_menu(const Vector2 &p_position);
184
185 virtual void _text_edit_gui_input(const Ref<InputEvent> &p_ev);
186 virtual bool _edit_option(int p_op);
187
188 virtual void _load_theme_settings();
189 virtual void _validate_script();
190 void _saved_update();
191
192 void _convert_case(CodeTextEditor::CaseStyle p_case) { code_editor->convert_case(p_case); }
193
194 int previous_history_line = -1;
195 void _emit_request_save_new_history();
196 void _emit_request_save_previous_state();
197
198public:
199 virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
200 virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter);
201
202 virtual void set_edited_resource(const Ref<Resource> &p_res) override;
203
204 virtual bool is_unsaved() override;
205 virtual void tag_saved_version() override;
206
207 virtual void reload_text();
208 virtual void enable_editor();
209
210 virtual EditMenusBase *create_edit_menu(DocumentEditorContainer *p_document_editor_container) = 0;
211
212 virtual Control *get_base_editor() const override { return code_editor->get_text_editor(); }
213 virtual CodeTextEditor *get_code_editor() const { return code_editor; }
214
215 virtual void set_tooltip_request_func(const Callable &p_toolip_callback);
216
217 virtual void ensure_focus() override { code_editor->get_text_editor()->grab_focus(); }
218 virtual void convert_indent() { code_editor->get_text_editor()->convert_indent(); }
219
220 virtual void trim_trailing_whitespace() { code_editor->trim_trailing_whitespace(); }
221 virtual void trim_final_newlines() { code_editor->trim_final_newlines(); }
222 virtual void insert_final_newline() { code_editor->insert_final_newline(); }
223
224 virtual void goto_line_without_history(int p_line, int p_column = 0) { code_editor->goto_line_without_history(p_line, p_column); }
225 virtual void goto_line(int p_line, int p_column = 0) { code_editor->goto_line(p_line, p_column); }
226 virtual void goto_line_selection(int p_line, int p_begin, int p_end) { code_editor->goto_line_selection(p_line, p_begin, p_end); }
227 virtual void goto_line_centered(int p_line, int p_column = 0) { code_editor->goto_line_centered(p_line, p_column); }
228 virtual void goto_line_and_center_if_necessary(int p_line, int p_column = 0) { code_editor->goto_line_and_center_if_necessary(p_line, p_column); }
229 virtual void trigger_history_save_on_navigate() { code_editor->trigger_history_save_on_navigate(); }
230 virtual void set_executing_line(int p_line) { code_editor->set_executing_line(p_line); }
231 virtual void clear_executing_line() { code_editor->clear_executing_line(); }
232
233 virtual Variant get_edit_state() { return code_editor->get_edit_state(); }
234 virtual void set_edit_state(const Variant &p_state, bool p_grab_focus = true);
235 virtual Variant get_navigation_state() { return code_editor->get_navigation_state(); }
236
237 virtual void update_settings() { code_editor->update_editor_settings(); }
238 virtual void set_find_replace_bar(FindReplaceBar *p_bar) { code_editor->set_find_replace_bar(p_bar); }
239
240 virtual void validate_script() override { code_editor->validate_script(); }
241 bool get_validation_success() { return validation_success; }
242
243 TextEditorBase();
244 ~TextEditorBase();
245};
246
247class CodeEditorBase : public TextEditorBase {
248 GDCLASS(CodeEditorBase, TextEditorBase);
249
250protected:
251 enum {
252 EDIT_COMPLETE = BASE_ENUM_COUNT,
253 EDIT_TOGGLE_COMMENT,
254
255 DEBUG_TOGGLE_BREAKPOINT,
256 DEBUG_REMOVE_ALL_BREAKPOINTS,
257 DEBUG_GOTO_NEXT_BREAKPOINT,
258 DEBUG_GOTO_PREV_BREAKPOINT,
259
260 CODE_ENUM_COUNT,
261 };
262
263 class EditMenusCEB : public EditMenus {
264 GDCLASS(EditMenusCEB, EditMenus);
265
266 protected:
267 PopupMenu *breakpoints_menu = nullptr;
268
269 virtual void _update_breakpoint_list();
270 void _breakpoint_item_pressed(int p_idx);
271
272 void _notification(int p_what);
273
274 public:
275 EditMenusCEB(DocumentEditorContainer *p_document_editor_container, String p_breakpoint_menu_name);
276 };
277
278 VSplitContainer *editor_box = nullptr;
279 RichTextLabel *warnings_panel = nullptr;
280
281 static void _code_complete_scripts(void *p_ud, const String &p_code, List<EditorLanguage::CompletionOption> *r_options, bool &r_force);
282 virtual void _code_complete_script(const String &p_code, List<EditorLanguage::CompletionOption> *r_options, bool &r_force) = 0;
283
284 void _show_warnings_panel(bool p_show);
285 virtual bool _warning_clicked(const Variant &p_line);
286
287 virtual void _make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position = Vector2(0, 0), bool p_show = true) override;
288
289 virtual bool _edit_option(int p_op) override;
290
291 virtual void _breakpoint_toggled(int p_row) = 0;
292
293public:
294 virtual bool show_members_overview() override { return true; }
295
296 virtual Vector<String> get_functions() { return Vector<String>(); }
297
298 virtual PackedInt32Array get_breakpoints() { return PackedInt32Array(); }
299 virtual void set_breakpoint(int p_line, bool p_enabled);
300 virtual void clear_breakpoints() {}
301
302 CodeEditorBase();
303};