text_shader_editor.cpp 42 KB

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