text_shader_editor.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. /**************************************************************************/
  2. /* text_shader_editor.cpp */
  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. #include "text_shader_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/version_generated.gen.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/file_system/editor_file_system.h"
  36. #include "editor/settings/editor_settings.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "editor/themes/editor_theme_manager.h"
  39. #include "scene/gui/split_container.h"
  40. #include "servers/rendering/shader_preprocessor.h"
  41. #include "servers/rendering/shader_types.h"
  42. /*** SHADER SYNTAX HIGHLIGHTER ****/
  43. Dictionary GDShaderSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_line) {
  44. Dictionary color_map;
  45. for (const Point2i &region : disabled_branch_regions) {
  46. if (p_line >= region.x && p_line <= region.y) {
  47. // When "color_regions[0].p_start_key.length() > 2",
  48. // disabled_branch_region causes color_region to break.
  49. // This should be seen as a temporary solution.
  50. CodeHighlighter::_get_line_syntax_highlighting_impl(p_line);
  51. Dictionary highlighter_info;
  52. highlighter_info["color"] = disabled_branch_color;
  53. color_map[0] = highlighter_info;
  54. return color_map;
  55. }
  56. }
  57. return CodeHighlighter::_get_line_syntax_highlighting_impl(p_line);
  58. }
  59. void GDShaderSyntaxHighlighter::add_disabled_branch_region(const Point2i &p_region) {
  60. ERR_FAIL_COND(p_region.x < 0);
  61. ERR_FAIL_COND(p_region.y < 0);
  62. for (int i = 0; i < disabled_branch_regions.size(); i++) {
  63. ERR_FAIL_COND_MSG(disabled_branch_regions[i].x == p_region.x, "Branch region with a start line '" + itos(p_region.x) + "' already exists.");
  64. }
  65. Point2i disabled_branch_region;
  66. disabled_branch_region.x = p_region.x;
  67. disabled_branch_region.y = p_region.y;
  68. disabled_branch_regions.push_back(disabled_branch_region);
  69. clear_highlighting_cache();
  70. }
  71. void GDShaderSyntaxHighlighter::clear_disabled_branch_regions() {
  72. disabled_branch_regions.clear();
  73. clear_highlighting_cache();
  74. }
  75. void GDShaderSyntaxHighlighter::set_disabled_branch_color(const Color &p_color) {
  76. disabled_branch_color = p_color;
  77. clear_highlighting_cache();
  78. }
  79. /*** SHADER SCRIPT EDITOR ****/
  80. static bool saved_warnings_enabled = false;
  81. static bool saved_treat_warning_as_errors = false;
  82. static HashMap<ShaderWarning::Code, bool> saved_warnings;
  83. static uint32_t saved_warning_flags = 0U;
  84. void ShaderTextEditor::_notification(int p_what) {
  85. switch (p_what) {
  86. case NOTIFICATION_THEME_CHANGED: {
  87. if (is_visible_in_tree()) {
  88. _load_theme_settings();
  89. if (warnings.size() > 0 && last_compile_result == OK) {
  90. warnings_panel->clear();
  91. _update_warning_panel();
  92. }
  93. }
  94. } break;
  95. }
  96. }
  97. Ref<Shader> ShaderTextEditor::get_edited_shader() const {
  98. return shader;
  99. }
  100. Ref<ShaderInclude> ShaderTextEditor::get_edited_shader_include() const {
  101. return shader_inc;
  102. }
  103. void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader) {
  104. set_edited_shader(p_shader, p_shader->get_code());
  105. }
  106. void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader, const String &p_code) {
  107. if (shader == p_shader) {
  108. return;
  109. }
  110. if (shader.is_valid()) {
  111. shader->disconnect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
  112. }
  113. shader = p_shader;
  114. shader_inc = Ref<ShaderInclude>();
  115. set_edited_code(p_code);
  116. if (shader.is_valid()) {
  117. shader->connect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
  118. }
  119. }
  120. void ShaderTextEditor::set_edited_shader_include(const Ref<ShaderInclude> &p_shader_inc) {
  121. set_edited_shader_include(p_shader_inc, p_shader_inc->get_code());
  122. }
  123. void ShaderTextEditor::_shader_changed() {
  124. // This function is used for dependencies (include changing changes main shader and forces it to revalidate)
  125. if (block_shader_changed) {
  126. return;
  127. }
  128. dependencies_version++;
  129. _validate_script();
  130. }
  131. void ShaderTextEditor::set_edited_shader_include(const Ref<ShaderInclude> &p_shader_inc, const String &p_code) {
  132. if (shader_inc == p_shader_inc) {
  133. return;
  134. }
  135. if (shader_inc.is_valid()) {
  136. shader_inc->disconnect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
  137. }
  138. shader_inc = p_shader_inc;
  139. shader = Ref<Shader>();
  140. set_edited_code(p_code);
  141. if (shader_inc.is_valid()) {
  142. shader_inc->connect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
  143. }
  144. }
  145. void ShaderTextEditor::set_edited_code(const String &p_code) {
  146. _load_theme_settings();
  147. get_text_editor()->set_text(p_code);
  148. get_text_editor()->clear_undo_history();
  149. callable_mp((TextEdit *)get_text_editor(), &TextEdit::set_h_scroll).call_deferred(0);
  150. callable_mp((TextEdit *)get_text_editor(), &TextEdit::set_v_scroll).call_deferred(0);
  151. get_text_editor()->tag_saved_version();
  152. _validate_script();
  153. _line_col_changed();
  154. }
  155. void ShaderTextEditor::reload_text() {
  156. ERR_FAIL_COND(shader.is_null() && shader_inc.is_null());
  157. String code;
  158. if (shader.is_valid()) {
  159. code = shader->get_code();
  160. } else {
  161. code = shader_inc->get_code();
  162. }
  163. CodeEdit *te = get_text_editor();
  164. int column = te->get_caret_column();
  165. int row = te->get_caret_line();
  166. int h = te->get_h_scroll();
  167. int v = te->get_v_scroll();
  168. te->set_text(code);
  169. te->set_caret_line(row);
  170. te->set_caret_column(column);
  171. te->set_h_scroll(h);
  172. te->set_v_scroll(v);
  173. te->tag_saved_version();
  174. update_line_and_column();
  175. }
  176. void ShaderTextEditor::set_warnings_panel(RichTextLabel *p_warnings_panel) {
  177. warnings_panel = p_warnings_panel;
  178. }
  179. void ShaderTextEditor::_load_theme_settings() {
  180. CodeEdit *te = get_text_editor();
  181. Color updated_marked_line_color = EDITOR_GET("text_editor/theme/highlighting/mark_color");
  182. if (updated_marked_line_color != marked_line_color) {
  183. for (int i = 0; i < te->get_line_count(); i++) {
  184. if (te->get_line_background_color(i) == marked_line_color) {
  185. te->set_line_background_color(i, updated_marked_line_color);
  186. }
  187. }
  188. marked_line_color = updated_marked_line_color;
  189. }
  190. syntax_highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color"));
  191. syntax_highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color"));
  192. syntax_highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/function_color"));
  193. syntax_highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/member_variable_color"));
  194. syntax_highlighter->clear_keyword_colors();
  195. const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
  196. const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
  197. List<String> keywords;
  198. ShaderLanguage::get_keyword_list(&keywords);
  199. for (const String &E : keywords) {
  200. if (ShaderLanguage::is_control_flow_keyword(E)) {
  201. syntax_highlighter->add_keyword_color(E, control_flow_keyword_color);
  202. } else {
  203. syntax_highlighter->add_keyword_color(E, keyword_color);
  204. }
  205. }
  206. List<String> pp_keywords;
  207. ShaderPreprocessor::get_keyword_list(&pp_keywords, false);
  208. for (const String &E : pp_keywords) {
  209. syntax_highlighter->add_keyword_color(E, control_flow_keyword_color);
  210. }
  211. // Colorize built-ins like `COLOR` differently to make them easier
  212. // to distinguish from keywords at a quick glance.
  213. List<String> built_ins;
  214. if (shader_inc.is_valid()) {
  215. for (int i = 0; i < RenderingServer::SHADER_MAX; i++) {
  216. for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(i))) {
  217. for (const KeyValue<StringName, ShaderLanguage::BuiltInInfo> &F : E.value.built_ins) {
  218. built_ins.push_back(F.key);
  219. }
  220. }
  221. {
  222. const Vector<ShaderLanguage::ModeInfo> &render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(i));
  223. for (const ShaderLanguage::ModeInfo &mode_info : render_modes) {
  224. if (!mode_info.options.is_empty()) {
  225. for (const StringName &option : mode_info.options) {
  226. built_ins.push_back(String(mode_info.name) + "_" + String(option));
  227. }
  228. } else {
  229. built_ins.push_back(String(mode_info.name));
  230. }
  231. }
  232. }
  233. {
  234. const Vector<ShaderLanguage::ModeInfo> &stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(i));
  235. for (const ShaderLanguage::ModeInfo &mode_info : stencil_modes) {
  236. if (!mode_info.options.is_empty()) {
  237. for (const StringName &option : mode_info.options) {
  238. built_ins.push_back(String(mode_info.name) + "_" + String(option));
  239. }
  240. } else {
  241. built_ins.push_back(String(mode_info.name));
  242. }
  243. }
  244. }
  245. }
  246. } else if (shader.is_valid()) {
  247. for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(shader->get_mode()))) {
  248. for (const KeyValue<StringName, ShaderLanguage::BuiltInInfo> &F : E.value.built_ins) {
  249. built_ins.push_back(F.key);
  250. }
  251. }
  252. {
  253. const Vector<ShaderLanguage::ModeInfo> &shader_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader->get_mode()));
  254. for (const ShaderLanguage::ModeInfo &mode_info : shader_modes) {
  255. if (!mode_info.options.is_empty()) {
  256. for (const StringName &option : mode_info.options) {
  257. built_ins.push_back(String(mode_info.name) + "_" + String(option));
  258. }
  259. } else {
  260. built_ins.push_back(String(mode_info.name));
  261. }
  262. }
  263. }
  264. {
  265. const Vector<ShaderLanguage::ModeInfo> &stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(shader->get_mode()));
  266. for (const ShaderLanguage::ModeInfo &mode_info : stencil_modes) {
  267. if (!mode_info.options.is_empty()) {
  268. for (const StringName &option : mode_info.options) {
  269. built_ins.push_back(String(mode_info.name) + "_" + String(option));
  270. }
  271. } else {
  272. built_ins.push_back(String(mode_info.name));
  273. }
  274. }
  275. }
  276. }
  277. const Color user_type_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
  278. for (const String &E : built_ins) {
  279. syntax_highlighter->add_keyword_color(E, user_type_color);
  280. }
  281. // Colorize comments.
  282. const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  283. syntax_highlighter->clear_color_regions();
  284. syntax_highlighter->add_color_region("/*", "*/", comment_color, false);
  285. syntax_highlighter->add_color_region("//", "", comment_color, true);
  286. const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
  287. syntax_highlighter->add_color_region("/**", "*/", doc_comment_color, false);
  288. // "/**/" will be treated as the start of the "/**" region, this line is guaranteed to end the color_region.
  289. syntax_highlighter->add_color_region("/**/", "", comment_color, true);
  290. // Disabled preprocessor branches use translucent text color to be easier to distinguish from comments.
  291. syntax_highlighter->set_disabled_branch_color(Color(EDITOR_GET("text_editor/theme/highlighting/text_color")) * Color(1, 1, 1, 0.5));
  292. te->clear_comment_delimiters();
  293. te->add_comment_delimiter("/*", "*/", false);
  294. te->add_comment_delimiter("//", "", true);
  295. if (!te->has_auto_brace_completion_open_key("/*")) {
  296. te->add_auto_brace_completion_pair("/*", "*/");
  297. }
  298. // Colorize preprocessor include strings.
  299. const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  300. syntax_highlighter->add_color_region("\"", "\"", string_color, false);
  301. syntax_highlighter->set_uint_suffix_enabled(true);
  302. }
  303. void ShaderTextEditor::_check_shader_mode() {
  304. String type = ShaderLanguage::get_shader_type(get_text_editor()->get_text());
  305. Shader::Mode mode;
  306. if (type == "canvas_item") {
  307. mode = Shader::MODE_CANVAS_ITEM;
  308. } else if (type == "particles") {
  309. mode = Shader::MODE_PARTICLES;
  310. } else if (type == "sky") {
  311. mode = Shader::MODE_SKY;
  312. } else if (type == "fog") {
  313. mode = Shader::MODE_FOG;
  314. } else {
  315. mode = Shader::MODE_SPATIAL;
  316. }
  317. if (shader->get_mode() != mode) {
  318. set_block_shader_changed(true);
  319. shader->set_code(get_text_editor()->get_text());
  320. set_block_shader_changed(false);
  321. _load_theme_settings();
  322. }
  323. }
  324. static ShaderLanguage::DataType _get_global_shader_uniform_type(const StringName &p_variable) {
  325. RS::GlobalShaderParameterType gvt = RS::get_singleton()->global_shader_parameter_get_type(p_variable);
  326. return (ShaderLanguage::DataType)RS::global_shader_uniform_type_get_shader_datatype(gvt);
  327. }
  328. static String complete_from_path;
  329. static void _complete_include_paths_search(EditorFileSystemDirectory *p_efsd, List<ScriptLanguage::CodeCompletionOption> *r_options) {
  330. if (!p_efsd) {
  331. return;
  332. }
  333. for (int i = 0; i < p_efsd->get_file_count(); i++) {
  334. if (p_efsd->get_file_type(i) == SNAME("ShaderInclude")) {
  335. String path = p_efsd->get_file_path(i);
  336. if (path.begins_with(complete_from_path)) {
  337. path = path.replace_first(complete_from_path, "");
  338. }
  339. r_options->push_back(ScriptLanguage::CodeCompletionOption(path, ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH));
  340. }
  341. }
  342. for (int j = 0; j < p_efsd->get_subdir_count(); j++) {
  343. _complete_include_paths_search(p_efsd->get_subdir(j), r_options);
  344. }
  345. }
  346. static void _complete_include_paths(List<ScriptLanguage::CodeCompletionOption> *r_options) {
  347. _complete_include_paths_search(EditorFileSystem::get_singleton()->get_filesystem(), r_options);
  348. }
  349. void ShaderTextEditor::_code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) {
  350. List<ScriptLanguage::CodeCompletionOption> pp_options;
  351. List<ScriptLanguage::CodeCompletionOption> pp_defines;
  352. ShaderPreprocessor preprocessor;
  353. String code;
  354. String resource_path = (shader.is_valid() ? shader->get_path() : shader_inc->get_path());
  355. complete_from_path = resource_path.get_base_dir();
  356. if (!complete_from_path.ends_with("/")) {
  357. complete_from_path += "/";
  358. }
  359. preprocessor.preprocess(p_code, resource_path, code, nullptr, nullptr, nullptr, nullptr, &pp_options, &pp_defines, _complete_include_paths);
  360. complete_from_path = String();
  361. if (pp_options.size()) {
  362. for (const ScriptLanguage::CodeCompletionOption &E : pp_options) {
  363. r_options->push_back(E);
  364. }
  365. return;
  366. }
  367. for (const ScriptLanguage::CodeCompletionOption &E : pp_defines) {
  368. r_options->push_back(E);
  369. }
  370. ShaderLanguage sl;
  371. String calltip;
  372. ShaderLanguage::ShaderCompileInfo comp_info;
  373. comp_info.global_shader_uniform_type_func = _get_global_shader_uniform_type;
  374. if (shader.is_null()) {
  375. comp_info.is_include = true;
  376. sl.complete(code, comp_info, r_options, calltip);
  377. get_text_editor()->set_code_hint(calltip);
  378. return;
  379. }
  380. _check_shader_mode();
  381. comp_info.functions = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(shader->get_mode()));
  382. comp_info.render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader->get_mode()));
  383. comp_info.stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(shader->get_mode()));
  384. comp_info.shader_types = ShaderTypes::get_singleton()->get_types();
  385. sl.complete(code, comp_info, r_options, calltip);
  386. get_text_editor()->set_code_hint(calltip);
  387. }
  388. void ShaderTextEditor::_validate_script() {
  389. emit_signal(CoreStringName(script_changed)); // Ensure to notify that it changed, so it is applied
  390. String code;
  391. if (shader.is_valid()) {
  392. _check_shader_mode();
  393. code = shader->get_code();
  394. } else {
  395. code = shader_inc->get_code();
  396. }
  397. ShaderPreprocessor preprocessor;
  398. String code_pp;
  399. String error_pp;
  400. List<ShaderPreprocessor::FilePosition> err_positions;
  401. List<ShaderPreprocessor::Region> regions;
  402. String filename;
  403. if (shader.is_valid()) {
  404. filename = shader->get_path();
  405. } else if (shader_inc.is_valid()) {
  406. filename = shader_inc->get_path();
  407. }
  408. last_compile_result = preprocessor.preprocess(code, filename, code_pp, &error_pp, &err_positions, &regions);
  409. for (int i = 0; i < get_text_editor()->get_line_count(); i++) {
  410. get_text_editor()->set_line_background_color(i, Color(0, 0, 0, 0));
  411. }
  412. syntax_highlighter->clear_disabled_branch_regions();
  413. for (const ShaderPreprocessor::Region &region : regions) {
  414. if (!region.enabled) {
  415. if (filename != region.file) {
  416. continue;
  417. }
  418. syntax_highlighter->add_disabled_branch_region(Point2i(region.from_line, region.to_line));
  419. }
  420. }
  421. set_error("");
  422. set_error_count(0);
  423. if (last_compile_result != OK) {
  424. // Preprocessor error.
  425. ERR_FAIL_COND(err_positions.is_empty());
  426. String err_text;
  427. const int err_line = err_positions.front()->get().line;
  428. if (err_positions.size() == 1) {
  429. // Error in the main file.
  430. const String message = error_pp.replace("[", "[lb]");
  431. err_text = vformat(TTR("Error at line %d:"), err_line) + " " + message;
  432. } else {
  433. // Error in an included file.
  434. const String inc_file = err_positions.back()->get().file.get_file();
  435. const int inc_line = err_positions.back()->get().line;
  436. const String message = error_pp.replace("[", "[lb]");
  437. err_text = vformat(TTR("Error at line %d in include %s:%d:"), err_line, inc_file, inc_line) + " " + message;
  438. set_error_count(err_positions.size() - 1);
  439. }
  440. set_error(err_text);
  441. set_error_pos(err_line - 1, 0);
  442. for (int i = 0; i < get_text_editor()->get_line_count(); i++) {
  443. get_text_editor()->set_line_background_color(i, Color(0, 0, 0, 0));
  444. }
  445. get_text_editor()->set_line_background_color(err_line - 1, marked_line_color);
  446. set_warning_count(0);
  447. } else {
  448. ShaderLanguage sl;
  449. sl.enable_warning_checking(saved_warnings_enabled);
  450. uint32_t flags = saved_warning_flags;
  451. if (shader.is_null()) {
  452. if (flags & ShaderWarning::UNUSED_CONSTANT) {
  453. flags &= ~(ShaderWarning::UNUSED_CONSTANT);
  454. }
  455. if (flags & ShaderWarning::UNUSED_FUNCTION) {
  456. flags &= ~(ShaderWarning::UNUSED_FUNCTION);
  457. }
  458. if (flags & ShaderWarning::UNUSED_STRUCT) {
  459. flags &= ~(ShaderWarning::UNUSED_STRUCT);
  460. }
  461. if (flags & ShaderWarning::UNUSED_UNIFORM) {
  462. flags &= ~(ShaderWarning::UNUSED_UNIFORM);
  463. }
  464. if (flags & ShaderWarning::UNUSED_VARYING) {
  465. flags &= ~(ShaderWarning::UNUSED_VARYING);
  466. }
  467. }
  468. sl.set_warning_flags(flags);
  469. ShaderLanguage::ShaderCompileInfo comp_info;
  470. comp_info.global_shader_uniform_type_func = _get_global_shader_uniform_type;
  471. if (shader.is_null()) {
  472. comp_info.is_include = true;
  473. } else {
  474. Shader::Mode mode = shader->get_mode();
  475. comp_info.functions = ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(mode));
  476. comp_info.render_modes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(mode));
  477. comp_info.stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RenderingServer::ShaderMode(mode));
  478. comp_info.shader_types = ShaderTypes::get_singleton()->get_types();
  479. }
  480. code = code_pp;
  481. //compiler error
  482. last_compile_result = sl.compile(code, comp_info);
  483. if (last_compile_result != OK) {
  484. Vector<ShaderLanguage::FilePosition> include_positions = sl.get_include_positions();
  485. String err_text;
  486. int err_line;
  487. if (include_positions.size() > 1) {
  488. // Error in an included file.
  489. err_line = include_positions[0].line;
  490. const String inc_file = include_positions[include_positions.size() - 1].file;
  491. const int inc_line = include_positions[include_positions.size() - 1].line;
  492. const String message = sl.get_error_text().replace("[", "[lb]");
  493. err_text = vformat(TTR("Error at line %d in include %s:%d:"), err_line, inc_file, inc_line) + " " + message;
  494. set_error_count(include_positions.size() - 1);
  495. } else {
  496. // Error in the main file.
  497. err_line = sl.get_error_line();
  498. const String message = sl.get_error_text().replace("[", "[lb]");
  499. err_text = vformat(TTR("Error at line %d:"), err_line) + " " + message;
  500. set_error_count(0);
  501. }
  502. set_error(err_text);
  503. set_error_pos(err_line - 1, 0);
  504. get_text_editor()->set_line_background_color(err_line - 1, marked_line_color);
  505. } else {
  506. set_error("");
  507. }
  508. if (warnings.size() > 0 || last_compile_result != OK) {
  509. warnings_panel->clear();
  510. }
  511. warnings.clear();
  512. for (List<ShaderWarning>::Element *E = sl.get_warnings_ptr(); E; E = E->next()) {
  513. warnings.push_back(E->get());
  514. }
  515. if (warnings.size() > 0 && last_compile_result == OK) {
  516. warnings.sort_custom<WarningsComparator>();
  517. _update_warning_panel();
  518. } else {
  519. set_warning_count(0);
  520. }
  521. }
  522. emit_signal(SNAME("script_validated"), last_compile_result == OK); // Notify that validation finished, to update the list of scripts
  523. }
  524. void ShaderTextEditor::_update_warning_panel() {
  525. int warning_count = 0;
  526. warnings_panel->push_table(2);
  527. for (const ShaderWarning &w : warnings) {
  528. if (warning_count == 0) {
  529. if (saved_treat_warning_as_errors) {
  530. const String message = (w.get_message() + " " + TTR("Warnings should be fixed to prevent errors.")).replace("[", "[lb]");
  531. const String error_text = vformat(TTR("Error at line %d:"), w.get_line()) + " " + message;
  532. set_error(error_text);
  533. set_error_pos(w.get_line() - 1, 0);
  534. get_text_editor()->set_line_background_color(w.get_line() - 1, marked_line_color);
  535. }
  536. }
  537. warning_count++;
  538. int line = w.get_line();
  539. // First cell.
  540. warnings_panel->push_cell();
  541. warnings_panel->push_color(warnings_panel->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  542. if (line != -1) {
  543. warnings_panel->push_meta(line - 1);
  544. warnings_panel->add_text(vformat(TTR("Line %d (%s):"), line, w.get_name()));
  545. warnings_panel->pop(); // Meta goto.
  546. } else {
  547. warnings_panel->add_text(w.get_name() + ":");
  548. }
  549. warnings_panel->pop(); // Color.
  550. warnings_panel->pop(); // Cell.
  551. // Second cell.
  552. warnings_panel->push_cell();
  553. warnings_panel->add_text(w.get_message());
  554. warnings_panel->pop(); // Cell.
  555. }
  556. warnings_panel->pop(); // Table.
  557. set_warning_count(warning_count);
  558. }
  559. void ShaderTextEditor::_bind_methods() {
  560. ADD_SIGNAL(MethodInfo("script_validated", PropertyInfo(Variant::BOOL, "valid")));
  561. }
  562. ShaderTextEditor::ShaderTextEditor() {
  563. syntax_highlighter.instantiate();
  564. get_text_editor()->set_syntax_highlighter(syntax_highlighter);
  565. }
  566. /*** SCRIPT EDITOR ******/
  567. void TextShaderEditor::_menu_option(int p_option) {
  568. code_editor->get_text_editor()->apply_ime();
  569. switch (p_option) {
  570. case EDIT_UNDO: {
  571. code_editor->get_text_editor()->undo();
  572. } break;
  573. case EDIT_REDO: {
  574. code_editor->get_text_editor()->redo();
  575. } break;
  576. case EDIT_CUT: {
  577. code_editor->get_text_editor()->cut();
  578. } break;
  579. case EDIT_COPY: {
  580. code_editor->get_text_editor()->copy();
  581. } break;
  582. case EDIT_PASTE: {
  583. code_editor->get_text_editor()->paste();
  584. } break;
  585. case EDIT_SELECT_ALL: {
  586. code_editor->get_text_editor()->select_all();
  587. } break;
  588. case EDIT_MOVE_LINE_UP: {
  589. code_editor->get_text_editor()->move_lines_up();
  590. } break;
  591. case EDIT_MOVE_LINE_DOWN: {
  592. code_editor->get_text_editor()->move_lines_down();
  593. } break;
  594. case EDIT_INDENT: {
  595. if (shader.is_null() && shader_inc.is_null()) {
  596. return;
  597. }
  598. code_editor->get_text_editor()->indent_lines();
  599. } break;
  600. case EDIT_UNINDENT: {
  601. if (shader.is_null() && shader_inc.is_null()) {
  602. return;
  603. }
  604. code_editor->get_text_editor()->unindent_lines();
  605. } break;
  606. case EDIT_DELETE_LINE: {
  607. code_editor->get_text_editor()->delete_lines();
  608. } break;
  609. case EDIT_DUPLICATE_SELECTION: {
  610. code_editor->get_text_editor()->duplicate_selection();
  611. } break;
  612. case EDIT_DUPLICATE_LINES: {
  613. code_editor->get_text_editor()->duplicate_lines();
  614. } break;
  615. case EDIT_TOGGLE_WORD_WRAP: {
  616. TextEdit::LineWrappingMode wrap = code_editor->get_text_editor()->get_line_wrapping_mode();
  617. code_editor->get_text_editor()->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY);
  618. } break;
  619. case EDIT_TOGGLE_COMMENT: {
  620. if (shader.is_null() && shader_inc.is_null()) {
  621. return;
  622. }
  623. code_editor->toggle_inline_comment("//");
  624. } break;
  625. case EDIT_COMPLETE: {
  626. code_editor->get_text_editor()->request_code_completion();
  627. } break;
  628. case SEARCH_FIND: {
  629. code_editor->get_find_replace_bar()->popup_search();
  630. } break;
  631. case SEARCH_FIND_NEXT: {
  632. code_editor->get_find_replace_bar()->search_next();
  633. } break;
  634. case SEARCH_FIND_PREV: {
  635. code_editor->get_find_replace_bar()->search_prev();
  636. } break;
  637. case SEARCH_REPLACE: {
  638. code_editor->get_find_replace_bar()->popup_replace();
  639. } break;
  640. case SEARCH_GOTO_LINE: {
  641. goto_line_popup->popup_find_line(code_editor);
  642. } break;
  643. case BOOKMARK_TOGGLE: {
  644. code_editor->toggle_bookmark();
  645. } break;
  646. case BOOKMARK_GOTO_NEXT: {
  647. code_editor->goto_next_bookmark();
  648. } break;
  649. case BOOKMARK_GOTO_PREV: {
  650. code_editor->goto_prev_bookmark();
  651. } break;
  652. case BOOKMARK_REMOVE_ALL: {
  653. code_editor->remove_all_bookmarks();
  654. } break;
  655. case HELP_DOCS: {
  656. OS::get_singleton()->shell_open(vformat("%s/tutorials/shaders/shader_reference/index.html", GODOT_VERSION_DOCS_URL));
  657. } break;
  658. case EDIT_EMOJI_AND_SYMBOL: {
  659. code_editor->get_text_editor()->show_emoji_and_symbol_picker();
  660. } break;
  661. }
  662. if (p_option != SEARCH_FIND && p_option != SEARCH_REPLACE && p_option != SEARCH_GOTO_LINE) {
  663. callable_mp((Control *)code_editor->get_text_editor(), &Control::grab_focus).call_deferred(false);
  664. }
  665. }
  666. void TextShaderEditor::_prepare_edit_menu() {
  667. const CodeEdit *tx = code_editor->get_text_editor();
  668. PopupMenu *popup = edit_menu->get_popup();
  669. popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
  670. popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
  671. }
  672. void TextShaderEditor::_notification(int p_what) {
  673. switch (p_what) {
  674. case NOTIFICATION_THEME_CHANGED: {
  675. site_search->set_button_icon(get_editor_theme_icon(SNAME("ExternalLink")));
  676. } break;
  677. case NOTIFICATION_APPLICATION_FOCUS_IN: {
  678. _check_for_external_edit();
  679. } break;
  680. }
  681. }
  682. void TextShaderEditor::_editor_settings_changed() {
  683. if (!EditorThemeManager::is_generated_theme_outdated() &&
  684. !EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor") &&
  685. !EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor")) {
  686. return;
  687. }
  688. _apply_editor_settings();
  689. }
  690. void TextShaderEditor::_apply_editor_settings() {
  691. code_editor->update_editor_settings();
  692. trim_trailing_whitespace_on_save = EDITOR_GET("text_editor/behavior/files/trim_trailing_whitespace_on_save");
  693. trim_final_newlines_on_save = EDITOR_GET("text_editor/behavior/files/trim_final_newlines_on_save");
  694. }
  695. void TextShaderEditor::_show_warnings_panel(bool p_show) {
  696. warnings_panel->set_visible(p_show);
  697. }
  698. void TextShaderEditor::_warning_clicked(const Variant &p_line) {
  699. if (p_line.get_type() == Variant::INT) {
  700. code_editor->goto_line_centered(p_line.operator int64_t());
  701. }
  702. }
  703. void TextShaderEditor::_bind_methods() {
  704. ClassDB::bind_method("_show_warnings_panel", &TextShaderEditor::_show_warnings_panel);
  705. ClassDB::bind_method("_warning_clicked", &TextShaderEditor::_warning_clicked);
  706. ADD_SIGNAL(MethodInfo("validation_changed"));
  707. }
  708. void TextShaderEditor::ensure_select_current() {
  709. }
  710. void TextShaderEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  711. code_editor->goto_line_selection(p_line, p_begin, p_end);
  712. }
  713. void TextShaderEditor::_project_settings_changed() {
  714. _update_warnings(true);
  715. }
  716. void TextShaderEditor::_update_warnings(bool p_validate) {
  717. bool changed = false;
  718. bool warnings_enabled = GLOBAL_GET("debug/shader_language/warnings/enable").booleanize();
  719. if (warnings_enabled != saved_warnings_enabled) {
  720. saved_warnings_enabled = warnings_enabled;
  721. changed = true;
  722. }
  723. bool treat_warning_as_errors = GLOBAL_GET("debug/shader_language/warnings/treat_warnings_as_errors").booleanize();
  724. if (treat_warning_as_errors != saved_treat_warning_as_errors) {
  725. saved_treat_warning_as_errors = treat_warning_as_errors;
  726. changed = true;
  727. }
  728. bool update_flags = false;
  729. for (int i = 0; i < ShaderWarning::WARNING_MAX; i++) {
  730. ShaderWarning::Code code = (ShaderWarning::Code)i;
  731. bool value = GLOBAL_GET("debug/shader_language/warnings/" + ShaderWarning::get_name_from_code(code).to_lower());
  732. if (saved_warnings[code] != value) {
  733. saved_warnings[code] = value;
  734. update_flags = true;
  735. changed = true;
  736. }
  737. }
  738. if (update_flags) {
  739. saved_warning_flags = (uint32_t)ShaderWarning::get_flags_from_codemap(saved_warnings);
  740. }
  741. if (p_validate && changed && code_editor && code_editor->get_edited_shader().is_valid()) {
  742. code_editor->validate_script();
  743. }
  744. }
  745. void TextShaderEditor::_check_for_external_edit() {
  746. bool use_autoreload = bool(EDITOR_GET("text_editor/behavior/files/auto_reload_scripts_on_external_change"));
  747. if (shader_inc.is_valid()) {
  748. if (shader_inc->get_last_modified_time() != FileAccess::get_modified_time(shader_inc->get_path())) {
  749. if (use_autoreload) {
  750. _reload_shader_include_from_disk();
  751. } else {
  752. callable_mp((Window *)disk_changed, &Window::popup_centered).call_deferred(Size2i());
  753. }
  754. }
  755. return;
  756. }
  757. if (shader.is_null() || shader->is_built_in()) {
  758. return;
  759. }
  760. if (shader->get_last_modified_time() != FileAccess::get_modified_time(shader->get_path())) {
  761. if (use_autoreload) {
  762. _reload_shader_from_disk();
  763. } else {
  764. callable_mp((Window *)disk_changed, &Window::popup_centered).call_deferred(Size2i());
  765. }
  766. }
  767. }
  768. void TextShaderEditor::_reload_shader_from_disk() {
  769. Ref<Shader> rel_shader = ResourceLoader::load(shader->get_path(), shader->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
  770. ERR_FAIL_COND(rel_shader.is_null());
  771. code_editor->set_block_shader_changed(true);
  772. shader->set_code(rel_shader->get_code());
  773. code_editor->set_block_shader_changed(false);
  774. shader->set_last_modified_time(rel_shader->get_last_modified_time());
  775. code_editor->reload_text();
  776. }
  777. void TextShaderEditor::_reload_shader_include_from_disk() {
  778. Ref<ShaderInclude> rel_shader_include = ResourceLoader::load(shader_inc->get_path(), shader_inc->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
  779. ERR_FAIL_COND(rel_shader_include.is_null());
  780. code_editor->set_block_shader_changed(true);
  781. shader_inc->set_code(rel_shader_include->get_code());
  782. code_editor->set_block_shader_changed(false);
  783. shader_inc->set_last_modified_time(rel_shader_include->get_last_modified_time());
  784. code_editor->reload_text();
  785. }
  786. void TextShaderEditor::_reload() {
  787. if (shader.is_valid()) {
  788. _reload_shader_from_disk();
  789. } else if (shader_inc.is_valid()) {
  790. _reload_shader_include_from_disk();
  791. }
  792. }
  793. void TextShaderEditor::edit_shader(const Ref<Shader> &p_shader) {
  794. if (p_shader.is_null() || !p_shader->is_text_shader()) {
  795. return;
  796. }
  797. if (shader == p_shader) {
  798. return;
  799. }
  800. shader = p_shader;
  801. shader_inc = Ref<ShaderInclude>();
  802. code_editor->set_edited_shader(shader);
  803. }
  804. void TextShaderEditor::edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) {
  805. if (p_shader_inc.is_null()) {
  806. return;
  807. }
  808. if (shader_inc == p_shader_inc) {
  809. return;
  810. }
  811. shader_inc = p_shader_inc;
  812. shader = Ref<Shader>();
  813. code_editor->set_edited_shader_include(p_shader_inc);
  814. }
  815. void TextShaderEditor::use_menu_bar(MenuButton *p_file_menu) {
  816. p_file_menu->set_switch_on_hover(true);
  817. menu_bar_hbox->add_child(p_file_menu);
  818. menu_bar_hbox->move_child(p_file_menu, 0);
  819. }
  820. void TextShaderEditor::save_external_data(const String &p_str) {
  821. if (shader.is_null() && shader_inc.is_null()) {
  822. disk_changed->hide();
  823. return;
  824. }
  825. if (trim_trailing_whitespace_on_save) {
  826. trim_trailing_whitespace();
  827. }
  828. if (trim_final_newlines_on_save) {
  829. trim_final_newlines();
  830. }
  831. apply_shaders();
  832. Ref<Shader> edited_shader = code_editor->get_edited_shader();
  833. if (edited_shader.is_valid()) {
  834. ResourceSaver::save(edited_shader);
  835. }
  836. if (shader.is_valid() && shader != edited_shader) {
  837. ResourceSaver::save(shader);
  838. }
  839. Ref<ShaderInclude> edited_shader_inc = code_editor->get_edited_shader_include();
  840. if (edited_shader_inc.is_valid()) {
  841. ResourceSaver::save(edited_shader_inc);
  842. }
  843. if (shader_inc.is_valid() && shader_inc != edited_shader_inc) {
  844. ResourceSaver::save(shader_inc);
  845. }
  846. code_editor->get_text_editor()->tag_saved_version();
  847. disk_changed->hide();
  848. }
  849. void TextShaderEditor::trim_trailing_whitespace() {
  850. code_editor->trim_trailing_whitespace();
  851. }
  852. void TextShaderEditor::trim_final_newlines() {
  853. code_editor->trim_final_newlines();
  854. }
  855. void TextShaderEditor::set_toggle_list_control(Control *p_toggle_list_control) {
  856. code_editor->set_toggle_list_control(p_toggle_list_control);
  857. }
  858. void TextShaderEditor::update_toggle_files_button() {
  859. code_editor->update_toggle_files_button();
  860. }
  861. void TextShaderEditor::validate_script() {
  862. code_editor->_validate_script();
  863. }
  864. bool TextShaderEditor::is_unsaved() const {
  865. return code_editor->get_text_editor()->get_saved_version() != code_editor->get_text_editor()->get_version();
  866. }
  867. void TextShaderEditor::tag_saved_version() {
  868. code_editor->get_text_editor()->tag_saved_version();
  869. }
  870. void TextShaderEditor::apply_shaders() {
  871. String editor_code = code_editor->get_text_editor()->get_text();
  872. if (shader.is_valid()) {
  873. String shader_code = shader->get_code();
  874. if (shader_code != editor_code || dependencies_version != code_editor->get_dependencies_version()) {
  875. code_editor->set_block_shader_changed(true);
  876. shader->set_code(editor_code);
  877. code_editor->set_block_shader_changed(false);
  878. shader->set_edited(true);
  879. }
  880. }
  881. if (shader_inc.is_valid()) {
  882. String shader_inc_code = shader_inc->get_code();
  883. if (shader_inc_code != editor_code || dependencies_version != code_editor->get_dependencies_version()) {
  884. code_editor->set_block_shader_changed(true);
  885. shader_inc->set_code(editor_code);
  886. code_editor->set_block_shader_changed(false);
  887. shader_inc->set_edited(true);
  888. }
  889. }
  890. dependencies_version = code_editor->get_dependencies_version();
  891. }
  892. void TextShaderEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  893. Ref<InputEventMouseButton> mb = ev;
  894. if (mb.is_valid()) {
  895. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  896. CodeEdit *tx = code_editor->get_text_editor();
  897. tx->apply_ime();
  898. Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());
  899. int row = pos.y;
  900. int col = pos.x;
  901. tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));
  902. if (tx->is_move_caret_on_right_click_enabled()) {
  903. tx->remove_secondary_carets();
  904. if (tx->has_selection()) {
  905. int from_line = tx->get_selection_from_line();
  906. int to_line = tx->get_selection_to_line();
  907. int from_column = tx->get_selection_from_column();
  908. int to_column = tx->get_selection_to_column();
  909. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  910. // Right click is outside the selected text
  911. tx->deselect();
  912. }
  913. }
  914. if (!tx->has_selection()) {
  915. tx->set_caret_line(row, true, false, -1);
  916. tx->set_caret_column(col);
  917. }
  918. }
  919. _make_context_menu(tx->has_selection(), get_local_mouse_position());
  920. }
  921. }
  922. Ref<InputEventKey> k = ev;
  923. if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) {
  924. CodeEdit *tx = code_editor->get_text_editor();
  925. tx->adjust_viewport_to_caret();
  926. _make_context_menu(tx->has_selection(), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->get_caret_draw_pos()));
  927. context_menu->grab_focus();
  928. }
  929. }
  930. void TextShaderEditor::_update_bookmark_list() {
  931. bookmarks_menu->clear();
  932. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  933. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
  934. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
  935. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
  936. PackedInt32Array bookmark_list = code_editor->get_text_editor()->get_bookmarked_lines();
  937. if (bookmark_list.is_empty()) {
  938. return;
  939. }
  940. bookmarks_menu->add_separator();
  941. for (int i = 0; i < bookmark_list.size(); i++) {
  942. String line = code_editor->get_text_editor()->get_line(bookmark_list[i]).strip_edges();
  943. // Limit the size of the line if too big.
  944. if (line.length() > 50) {
  945. line = line.substr(0, 50);
  946. }
  947. bookmarks_menu->add_item(String::num_int64(bookmark_list[i] + 1) + " - \"" + line + "\"");
  948. bookmarks_menu->set_item_metadata(-1, bookmark_list[i]);
  949. }
  950. }
  951. void TextShaderEditor::_bookmark_item_pressed(int p_idx) {
  952. if (p_idx < 4) { // Any item before the separator.
  953. _menu_option(bookmarks_menu->get_item_id(p_idx));
  954. } else {
  955. code_editor->goto_line(bookmarks_menu->get_item_metadata(p_idx));
  956. }
  957. }
  958. void TextShaderEditor::_make_context_menu(bool p_selection, Vector2 p_position) {
  959. context_menu->clear();
  960. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
  961. context_menu->add_item(TTR("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL);
  962. context_menu->add_separator();
  963. }
  964. if (p_selection) {
  965. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  966. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  967. }
  968. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  969. context_menu->add_separator();
  970. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  971. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  972. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  973. context_menu->add_separator();
  974. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
  975. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
  976. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  977. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  978. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !code_editor->get_text_editor()->has_undo());
  979. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !code_editor->get_text_editor()->has_redo());
  980. context_menu->set_position(get_screen_position() + p_position);
  981. context_menu->reset_size();
  982. context_menu->popup();
  983. }
  984. TextShaderEditor::TextShaderEditor() {
  985. _update_warnings(false);
  986. code_editor = memnew(ShaderTextEditor);
  987. code_editor->connect("script_validated", callable_mp(this, &TextShaderEditor::_script_validated));
  988. code_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  989. code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  990. code_editor->connect("show_warnings_panel", callable_mp(this, &TextShaderEditor::_show_warnings_panel));
  991. code_editor->connect(CoreStringName(script_changed), callable_mp(this, &TextShaderEditor::apply_shaders));
  992. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &TextShaderEditor::_editor_settings_changed));
  993. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &TextShaderEditor::_project_settings_changed));
  994. code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);
  995. code_editor->get_text_editor()->set_context_menu_enabled(false);
  996. code_editor->get_text_editor()->set_draw_breakpoints_gutter(false);
  997. code_editor->get_text_editor()->set_draw_executing_lines_gutter(false);
  998. code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &TextShaderEditor::_text_edit_gui_input));
  999. code_editor->update_editor_settings();
  1000. context_menu = memnew(PopupMenu);
  1001. add_child(context_menu);
  1002. context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));
  1003. VBoxContainer *main_container = memnew(VBoxContainer);
  1004. main_container->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1005. menu_bar_hbox = memnew(HBoxContainer);
  1006. edit_menu = memnew(MenuButton);
  1007. edit_menu->set_flat(false);
  1008. edit_menu->set_theme_type_variation("FlatMenuButton");
  1009. edit_menu->set_shortcut_context(this);
  1010. edit_menu->set_text(TTR("Edit"));
  1011. edit_menu->set_switch_on_hover(true);
  1012. edit_menu->connect("about_to_popup", callable_mp(this, &TextShaderEditor::_prepare_edit_menu));
  1013. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  1014. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  1015. edit_menu->get_popup()->add_separator();
  1016. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  1017. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  1018. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  1019. edit_menu->get_popup()->add_separator();
  1020. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  1021. edit_menu->get_popup()->add_separator();
  1022. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  1023. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  1024. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
  1025. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
  1026. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  1027. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1028. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
  1029. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);
  1030. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP);
  1031. edit_menu->get_popup()->add_separator();
  1032. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);
  1033. edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));
  1034. search_menu = memnew(MenuButton);
  1035. search_menu->set_flat(false);
  1036. search_menu->set_theme_type_variation("FlatMenuButton");
  1037. search_menu->set_shortcut_context(this);
  1038. search_menu->set_text(TTR("Search"));
  1039. search_menu->set_switch_on_hover(true);
  1040. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  1041. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  1042. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  1043. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  1044. search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));
  1045. MenuButton *goto_menu = memnew(MenuButton);
  1046. goto_menu->set_flat(false);
  1047. goto_menu->set_theme_type_variation("FlatMenuButton");
  1048. goto_menu->set_shortcut_context(this);
  1049. goto_menu->set_text(TTR("Go To"));
  1050. goto_menu->set_switch_on_hover(true);
  1051. goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));
  1052. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  1053. goto_menu->get_popup()->add_separator();
  1054. bookmarks_menu = memnew(PopupMenu);
  1055. goto_menu->get_popup()->add_submenu_node_item(TTR("Bookmarks"), bookmarks_menu);
  1056. _update_bookmark_list();
  1057. bookmarks_menu->connect("about_to_popup", callable_mp(this, &TextShaderEditor::_update_bookmark_list));
  1058. bookmarks_menu->connect("index_pressed", callable_mp(this, &TextShaderEditor::_bookmark_item_pressed));
  1059. add_child(main_container);
  1060. main_container->add_child(menu_bar_hbox);
  1061. menu_bar_hbox->add_child(edit_menu);
  1062. menu_bar_hbox->add_child(search_menu);
  1063. menu_bar_hbox->add_child(goto_menu);
  1064. menu_bar_hbox->add_spacer();
  1065. site_search = memnew(Button);
  1066. site_search->set_theme_type_variation(SceneStringName(FlatButton));
  1067. site_search->connect(SceneStringName(pressed), callable_mp(this, &TextShaderEditor::_menu_option).bind(HELP_DOCS));
  1068. site_search->set_text(TTR("Online Docs"));
  1069. site_search->set_tooltip_text(TTR("Open Godot online documentation."));
  1070. menu_bar_hbox->add_child(site_search);
  1071. menu_bar_hbox->add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("ScriptEditorPanel"), EditorStringName(EditorStyles)));
  1072. VSplitContainer *editor_box = memnew(VSplitContainer);
  1073. main_container->add_child(editor_box);
  1074. editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1075. editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1076. editor_box->add_child(code_editor);
  1077. FindReplaceBar *bar = memnew(FindReplaceBar);
  1078. main_container->add_child(bar);
  1079. bar->hide();
  1080. code_editor->set_find_replace_bar(bar);
  1081. warnings_panel = memnew(RichTextLabel);
  1082. warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
  1083. warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  1084. warnings_panel->set_meta_underline(true);
  1085. warnings_panel->set_selection_enabled(true);
  1086. warnings_panel->set_context_menu_enabled(true);
  1087. warnings_panel->set_focus_mode(FOCUS_CLICK);
  1088. warnings_panel->hide();
  1089. warnings_panel->connect("meta_clicked", callable_mp(this, &TextShaderEditor::_warning_clicked));
  1090. editor_box->add_child(warnings_panel);
  1091. code_editor->set_warnings_panel(warnings_panel);
  1092. goto_line_popup = memnew(GotoLinePopup);
  1093. add_child(goto_line_popup);
  1094. disk_changed = memnew(ConfirmationDialog);
  1095. VBoxContainer *vbc = memnew(VBoxContainer);
  1096. disk_changed->add_child(vbc);
  1097. Label *dl = memnew(Label);
  1098. dl->set_focus_mode(FOCUS_ACCESSIBILITY);
  1099. dl->set_text(TTR("This shader has been modified on disk.\nWhat action should be taken?"));
  1100. vbc->add_child(dl);
  1101. disk_changed->connect(SceneStringName(confirmed), callable_mp(this, &TextShaderEditor::_reload));
  1102. disk_changed->set_ok_button_text(TTR("Reload"));
  1103. disk_changed->add_button(TTR("Resave"), !DisplayServer::get_singleton()->get_swap_cancel_ok(), "resave");
  1104. disk_changed->connect("custom_action", callable_mp(this, &TextShaderEditor::save_external_data));
  1105. add_child(disk_changed);
  1106. _editor_settings_changed();
  1107. code_editor->show_toggle_files_button(); // TODO: Disabled for now, because it doesn't work properly.
  1108. }