Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/shader/text_shader_editor.h
9896 views
1
/**************************************************************************/
2
/* text_shader_editor.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 "editor/shader/shader_editor.h"
35
#include "scene/gui/menu_button.h"
36
#include "scene/gui/rich_text_label.h"
37
#include "servers/rendering/shader_warnings.h"
38
39
class GDShaderSyntaxHighlighter : public CodeHighlighter {
40
GDCLASS(GDShaderSyntaxHighlighter, CodeHighlighter)
41
42
private:
43
Vector<Point2i> disabled_branch_regions;
44
Color disabled_branch_color;
45
46
public:
47
virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override;
48
49
void add_disabled_branch_region(const Point2i &p_region);
50
void clear_disabled_branch_regions();
51
void set_disabled_branch_color(const Color &p_color);
52
};
53
54
class ShaderTextEditor : public CodeTextEditor {
55
GDCLASS(ShaderTextEditor, CodeTextEditor);
56
57
Color marked_line_color = Color(1, 1, 1);
58
59
struct WarningsComparator {
60
_ALWAYS_INLINE_ bool operator()(const ShaderWarning &p_a, const ShaderWarning &p_b) const { return (p_a.get_line() < p_b.get_line()); }
61
};
62
63
Ref<GDShaderSyntaxHighlighter> syntax_highlighter;
64
RichTextLabel *warnings_panel = nullptr;
65
Ref<Shader> shader;
66
Ref<ShaderInclude> shader_inc;
67
List<ShaderWarning> warnings;
68
Error last_compile_result = Error::OK;
69
70
void _check_shader_mode();
71
void _update_warning_panel();
72
73
bool block_shader_changed = false;
74
void _shader_changed();
75
76
uint32_t dependencies_version = 0; // Incremented if deps changed
77
78
protected:
79
void _notification(int p_what);
80
static void _bind_methods();
81
virtual void _load_theme_settings() override;
82
83
virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) override;
84
85
public:
86
void set_block_shader_changed(bool p_block) { block_shader_changed = p_block; }
87
uint32_t get_dependencies_version() const { return dependencies_version; }
88
89
virtual void _validate_script() override;
90
91
void reload_text();
92
void set_warnings_panel(RichTextLabel *p_warnings_panel);
93
94
Ref<Shader> get_edited_shader() const;
95
Ref<ShaderInclude> get_edited_shader_include() const;
96
97
void set_edited_shader(const Ref<Shader> &p_shader);
98
void set_edited_shader(const Ref<Shader> &p_shader, const String &p_code);
99
void set_edited_shader_include(const Ref<ShaderInclude> &p_include);
100
void set_edited_shader_include(const Ref<ShaderInclude> &p_include, const String &p_code);
101
void set_edited_code(const String &p_code);
102
103
ShaderTextEditor();
104
};
105
106
class TextShaderEditor : public ShaderEditor {
107
GDCLASS(TextShaderEditor, ShaderEditor);
108
109
enum {
110
EDIT_UNDO,
111
EDIT_REDO,
112
EDIT_CUT,
113
EDIT_COPY,
114
EDIT_PASTE,
115
EDIT_SELECT_ALL,
116
EDIT_MOVE_LINE_UP,
117
EDIT_MOVE_LINE_DOWN,
118
EDIT_INDENT,
119
EDIT_UNINDENT,
120
EDIT_DELETE_LINE,
121
EDIT_DUPLICATE_SELECTION,
122
EDIT_DUPLICATE_LINES,
123
EDIT_TOGGLE_WORD_WRAP,
124
EDIT_TOGGLE_COMMENT,
125
EDIT_COMPLETE,
126
SEARCH_FIND,
127
SEARCH_FIND_NEXT,
128
SEARCH_FIND_PREV,
129
SEARCH_REPLACE,
130
SEARCH_GOTO_LINE,
131
BOOKMARK_TOGGLE,
132
BOOKMARK_GOTO_NEXT,
133
BOOKMARK_GOTO_PREV,
134
BOOKMARK_REMOVE_ALL,
135
HELP_DOCS,
136
EDIT_EMOJI_AND_SYMBOL,
137
};
138
139
HBoxContainer *menu_bar_hbox = nullptr;
140
MenuButton *edit_menu = nullptr;
141
MenuButton *search_menu = nullptr;
142
PopupMenu *bookmarks_menu = nullptr;
143
Button *site_search = nullptr;
144
PopupMenu *context_menu = nullptr;
145
RichTextLabel *warnings_panel = nullptr;
146
uint64_t idle = 0;
147
148
GotoLinePopup *goto_line_popup = nullptr;
149
ConfirmationDialog *erase_tab_confirm = nullptr;
150
ConfirmationDialog *disk_changed = nullptr;
151
152
ShaderTextEditor *code_editor = nullptr;
153
bool compilation_success = true;
154
155
void _menu_option(int p_option);
156
void _prepare_edit_menu();
157
mutable Ref<Shader> shader;
158
mutable Ref<ShaderInclude> shader_inc;
159
160
void _editor_settings_changed();
161
void _apply_editor_settings();
162
void _project_settings_changed();
163
164
void _check_for_external_edit();
165
void _reload_shader_from_disk();
166
void _reload_shader_include_from_disk();
167
void _reload();
168
void _show_warnings_panel(bool p_show);
169
void _warning_clicked(const Variant &p_line);
170
void _update_warnings(bool p_validate);
171
172
void _script_validated(bool p_valid) {
173
compilation_success = p_valid;
174
emit_signal(SNAME("validation_changed"));
175
}
176
177
uint32_t dependencies_version = 0xFFFFFFFF;
178
179
bool trim_trailing_whitespace_on_save;
180
bool trim_final_newlines_on_save;
181
182
protected:
183
void _notification(int p_what);
184
static void _bind_methods();
185
void _make_context_menu(bool p_selection, Vector2 p_position);
186
void _text_edit_gui_input(const Ref<InputEvent> &p_ev);
187
188
void _update_bookmark_list();
189
void _bookmark_item_pressed(int p_idx);
190
191
public:
192
virtual void edit_shader(const Ref<Shader> &p_shader) override;
193
virtual void edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) override;
194
virtual void use_menu_bar_items(MenuButton *p_file_menu, Button *p_make_floating) override;
195
196
virtual void apply_shaders() override;
197
virtual bool is_unsaved() const override;
198
virtual void save_external_data(const String &p_str = "") override;
199
virtual void validate_script() override;
200
201
bool was_compilation_successful() const { return compilation_success; }
202
bool get_trim_trailing_whitespace_on_save() const { return trim_trailing_whitespace_on_save; }
203
bool get_trim_final_newlines_on_save() const { return trim_final_newlines_on_save; }
204
void ensure_select_current();
205
void goto_line_selection(int p_line, int p_begin, int p_end);
206
void trim_trailing_whitespace();
207
void trim_final_newlines();
208
void tag_saved_version();
209
ShaderTextEditor *get_code_editor() { return code_editor; }
210
211
virtual Size2 get_minimum_size() const override { return Size2(0, 200); }
212
213
TextShaderEditor();
214
};
215
216