text_shader_editor.cpp 45 KB

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