shader_editor_plugin.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /**************************************************************************/
  2. /* shader_editor_plugin.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 "shader_editor_plugin.h"
  31. #include "editor/editor_command_palette.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "editor/filesystem_dock.h"
  36. #include "editor/gui/editor_bottom_panel.h"
  37. #include "editor/inspector_dock.h"
  38. #include "editor/plugins/text_shader_editor.h"
  39. #include "editor/plugins/visual_shader_editor_plugin.h"
  40. #include "editor/shader_create_dialog.h"
  41. #include "editor/themes/editor_scale.h"
  42. #include "editor/window_wrapper.h"
  43. #include "scene/gui/item_list.h"
  44. #include "scene/gui/tab_container.h"
  45. #include "scene/gui/texture_rect.h"
  46. Ref<Resource> ShaderEditorPlugin::_get_current_shader() {
  47. int index = shader_tabs->get_current_tab();
  48. ERR_FAIL_INDEX_V(index, shader_tabs->get_tab_count(), Ref<Resource>());
  49. if (edited_shaders[index].shader.is_valid()) {
  50. return edited_shaders[index].shader;
  51. } else {
  52. return edited_shaders[index].shader_inc;
  53. }
  54. }
  55. void ShaderEditorPlugin::_update_shader_list() {
  56. shader_list->clear();
  57. for (EditedShader &edited_shader : edited_shaders) {
  58. Ref<Resource> shader = edited_shader.shader;
  59. if (shader.is_null()) {
  60. shader = edited_shader.shader_inc;
  61. }
  62. String path = shader->get_path();
  63. String text = path.get_file();
  64. if (text.is_empty()) {
  65. // This appears for newly created built-in shaders before saving the scene.
  66. text = TTR("[unsaved]");
  67. } else if (shader->is_built_in()) {
  68. const String &shader_name = shader->get_name();
  69. if (!shader_name.is_empty()) {
  70. text = vformat("%s (%s)", shader_name, text.get_slice("::", 0));
  71. }
  72. }
  73. // When shader is deleted in filesystem dock, need this to correctly close shader editor.
  74. edited_shader.path = path;
  75. bool unsaved = false;
  76. if (edited_shader.shader_editor) {
  77. unsaved = edited_shader.shader_editor->is_unsaved();
  78. }
  79. // TODO: Handle visual shaders too.
  80. if (unsaved) {
  81. text += "(*)";
  82. }
  83. String _class = shader->get_class();
  84. if (!shader_list->has_theme_icon(_class, EditorStringName(EditorIcons))) {
  85. _class = "TextFile";
  86. }
  87. Ref<Texture2D> icon = shader_list->get_editor_theme_icon(_class);
  88. shader_list->add_item(text, icon);
  89. shader_list->set_item_tooltip(-1, path);
  90. edited_shader.name = text;
  91. }
  92. if (shader_tabs->get_tab_count()) {
  93. shader_list->select(shader_tabs->get_current_tab());
  94. }
  95. _set_file_specific_items_disabled(edited_shaders.is_empty());
  96. _update_shader_list_status();
  97. }
  98. void ShaderEditorPlugin::_update_shader_list_status() {
  99. for (int i = 0; i < shader_list->get_item_count(); i++) {
  100. TextShaderEditor *se = Object::cast_to<TextShaderEditor>(shader_tabs->get_tab_control(i));
  101. if (se) {
  102. if (se->was_compilation_successful()) {
  103. shader_list->set_item_tag_icon(i, Ref<Texture2D>());
  104. } else {
  105. shader_list->set_item_tag_icon(i, shader_list->get_editor_theme_icon(SNAME("Error")));
  106. }
  107. }
  108. }
  109. }
  110. void ShaderEditorPlugin::_move_shader_tab(int p_from, int p_to) {
  111. if (p_from == p_to) {
  112. return;
  113. }
  114. EditedShader es = edited_shaders[p_from];
  115. edited_shaders.remove_at(p_from);
  116. edited_shaders.insert(p_to, es);
  117. shader_tabs->move_child(shader_tabs->get_tab_control(p_from), p_to);
  118. _update_shader_list();
  119. }
  120. void ShaderEditorPlugin::edit(Object *p_object) {
  121. if (!p_object) {
  122. return;
  123. }
  124. EditedShader es;
  125. ShaderInclude *si = Object::cast_to<ShaderInclude>(p_object);
  126. if (si != nullptr) {
  127. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  128. if (edited_shaders[i].shader_inc.ptr() == si) {
  129. shader_tabs->set_current_tab(i);
  130. shader_list->select(i);
  131. _switch_to_editor(edited_shaders[i].shader_editor);
  132. return;
  133. }
  134. }
  135. es.shader_inc = Ref<ShaderInclude>(si);
  136. TextShaderEditor *text_shader = memnew(TextShaderEditor);
  137. text_shader->get_code_editor()->set_toggle_list_control(shader_list);
  138. es.shader_editor = text_shader;
  139. es.shader_editor->edit_shader_include(si);
  140. shader_tabs->add_child(es.shader_editor);
  141. } else {
  142. Shader *s = Object::cast_to<Shader>(p_object);
  143. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  144. if (edited_shaders[i].shader.ptr() == s) {
  145. shader_tabs->set_current_tab(i);
  146. shader_list->select(i);
  147. _switch_to_editor(edited_shaders[i].shader_editor);
  148. return;
  149. }
  150. }
  151. es.shader = Ref<Shader>(s);
  152. Ref<VisualShader> vs = es.shader;
  153. if (vs.is_valid()) {
  154. VisualShaderEditor *vs_editor = memnew(VisualShaderEditor);
  155. vs_editor->set_toggle_list_control(shader_list);
  156. es.shader_editor = vs_editor;
  157. } else {
  158. TextShaderEditor *text_shader = memnew(TextShaderEditor);
  159. text_shader->get_code_editor()->set_toggle_list_control(shader_list);
  160. es.shader_editor = text_shader;
  161. }
  162. shader_tabs->add_child(es.shader_editor);
  163. es.shader_editor->edit_shader(es.shader);
  164. }
  165. TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(es.shader_editor);
  166. if (text_shader_editor) {
  167. text_shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
  168. CodeTextEditor *cte = text_shader_editor->get_code_editor();
  169. if (cte) {
  170. cte->set_zoom_factor(text_shader_zoom_factor);
  171. cte->connect("zoomed", callable_mp(this, &ShaderEditorPlugin::_set_text_shader_zoom_factor));
  172. cte->connect(SceneStringName(visibility_changed), callable_mp(this, &ShaderEditorPlugin::_update_shader_editor_zoom_factor).bind(cte));
  173. }
  174. if (text_shader_editor->get_top_bar()) {
  175. text_shader_editor->get_top_bar()->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  176. menu_hb->add_child(text_shader_editor->get_top_bar());
  177. menu_hb->move_child(text_shader_editor->get_top_bar(), 1);
  178. }
  179. }
  180. shader_tabs->set_current_tab(shader_tabs->get_tab_count() - 1);
  181. edited_shaders.push_back(es);
  182. _update_shader_list();
  183. _switch_to_editor(es.shader_editor);
  184. }
  185. bool ShaderEditorPlugin::handles(Object *p_object) const {
  186. return Object::cast_to<Shader>(p_object) != nullptr || Object::cast_to<ShaderInclude>(p_object) != nullptr;
  187. }
  188. void ShaderEditorPlugin::make_visible(bool p_visible) {
  189. if (p_visible) {
  190. EditorNode::get_bottom_panel()->make_item_visible(window_wrapper);
  191. }
  192. }
  193. void ShaderEditorPlugin::selected_notify() {
  194. }
  195. ShaderEditor *ShaderEditorPlugin::get_shader_editor(const Ref<Shader> &p_for_shader) {
  196. for (EditedShader &edited_shader : edited_shaders) {
  197. if (edited_shader.shader == p_for_shader) {
  198. return edited_shader.shader_editor;
  199. }
  200. }
  201. return nullptr;
  202. }
  203. void ShaderEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  204. if (EDITOR_GET("interface/multi_window/restore_windows_on_load") && window_wrapper->is_window_available() && p_layout->has_section_key("ShaderEditor", "window_rect")) {
  205. window_wrapper->restore_window_from_saved_position(
  206. p_layout->get_value("ShaderEditor", "window_rect", Rect2i()),
  207. p_layout->get_value("ShaderEditor", "window_screen", -1),
  208. p_layout->get_value("ShaderEditor", "window_screen_rect", Rect2i()));
  209. } else {
  210. window_wrapper->set_window_enabled(false);
  211. }
  212. if (!bool(EDITOR_GET("editors/shader_editor/behavior/files/restore_shaders_on_load"))) {
  213. return;
  214. }
  215. if (!p_layout->has_section("ShaderEditor")) {
  216. return;
  217. }
  218. if (!p_layout->has_section_key("ShaderEditor", "open_shaders") ||
  219. !p_layout->has_section_key("ShaderEditor", "selected_shader")) {
  220. return;
  221. }
  222. Array shaders = p_layout->get_value("ShaderEditor", "open_shaders");
  223. int selected_shader_idx = 0;
  224. String selected_shader = p_layout->get_value("ShaderEditor", "selected_shader");
  225. for (int i = 0; i < shaders.size(); i++) {
  226. String path = shaders[i];
  227. Ref<Resource> res = ResourceLoader::load(path);
  228. if (res.is_valid()) {
  229. edit(res.ptr());
  230. }
  231. if (selected_shader == path) {
  232. selected_shader_idx = i;
  233. }
  234. }
  235. if (p_layout->has_section_key("ShaderEditor", "split_offset")) {
  236. files_split->set_split_offset(p_layout->get_value("ShaderEditor", "split_offset"));
  237. }
  238. _update_shader_list();
  239. _shader_selected(selected_shader_idx);
  240. _set_text_shader_zoom_factor(p_layout->get_value("ShaderEditor", "text_shader_zoom_factor", 1.0f));
  241. }
  242. void ShaderEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
  243. if (window_wrapper->get_window_enabled()) {
  244. p_layout->set_value("ShaderEditor", "window_rect", window_wrapper->get_window_rect());
  245. int screen = window_wrapper->get_window_screen();
  246. p_layout->set_value("ShaderEditor", "window_screen", screen);
  247. p_layout->set_value("ShaderEditor", "window_screen_rect", DisplayServer::get_singleton()->screen_get_usable_rect(screen));
  248. } else {
  249. if (p_layout->has_section_key("ShaderEditor", "window_rect")) {
  250. p_layout->erase_section_key("ShaderEditor", "window_rect");
  251. }
  252. if (p_layout->has_section_key("ShaderEditor", "window_screen")) {
  253. p_layout->erase_section_key("ShaderEditor", "window_screen");
  254. }
  255. if (p_layout->has_section_key("ShaderEditor", "window_screen_rect")) {
  256. p_layout->erase_section_key("ShaderEditor", "window_screen_rect");
  257. }
  258. }
  259. Array shaders;
  260. String selected_shader;
  261. for (int i = 0; i < shader_tabs->get_tab_count(); i++) {
  262. EditedShader edited_shader = edited_shaders[i];
  263. if (edited_shader.shader_editor) {
  264. String shader_path;
  265. if (edited_shader.shader.is_valid()) {
  266. shader_path = edited_shader.shader->get_path();
  267. } else {
  268. DEV_ASSERT(edited_shader.shader_inc.is_valid());
  269. shader_path = edited_shader.shader_inc->get_path();
  270. }
  271. shaders.push_back(shader_path);
  272. ShaderEditor *shader_editor = Object::cast_to<ShaderEditor>(shader_tabs->get_current_tab_control());
  273. if (shader_editor && edited_shader.shader_editor == shader_editor) {
  274. selected_shader = shader_path;
  275. }
  276. }
  277. }
  278. p_layout->set_value("ShaderEditor", "open_shaders", shaders);
  279. p_layout->set_value("ShaderEditor", "split_offset", files_split->get_split_offset());
  280. p_layout->set_value("ShaderEditor", "selected_shader", selected_shader);
  281. p_layout->set_value("ShaderEditor", "text_shader_zoom_factor", text_shader_zoom_factor);
  282. }
  283. String ShaderEditorPlugin::get_unsaved_status(const String &p_for_scene) const {
  284. // TODO: This should also include visual shaders and shader includes, but save_external_data() doesn't seem to save them...
  285. PackedStringArray unsaved_shaders;
  286. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  287. if (edited_shaders[i].shader_editor) {
  288. if (edited_shaders[i].shader_editor->is_unsaved()) {
  289. if (unsaved_shaders.is_empty()) {
  290. unsaved_shaders.append(TTR("Save changes to the following shaders(s) before quitting?"));
  291. }
  292. unsaved_shaders.append(edited_shaders[i].name.trim_suffix("(*)"));
  293. }
  294. }
  295. }
  296. if (!p_for_scene.is_empty()) {
  297. PackedStringArray unsaved_built_in_shaders;
  298. const String scene_file = p_for_scene.get_file();
  299. for (const String &E : unsaved_shaders) {
  300. if (!E.is_resource_file() && E.contains(scene_file)) {
  301. if (unsaved_built_in_shaders.is_empty()) {
  302. unsaved_built_in_shaders.append(TTR("There are unsaved changes in the following built-in shaders(s):"));
  303. }
  304. unsaved_built_in_shaders.append(E);
  305. }
  306. }
  307. if (!unsaved_built_in_shaders.is_empty()) {
  308. return String("\n").join(unsaved_built_in_shaders);
  309. }
  310. return String();
  311. }
  312. return String("\n").join(unsaved_shaders);
  313. }
  314. void ShaderEditorPlugin::save_external_data() {
  315. for (EditedShader &edited_shader : edited_shaders) {
  316. if (edited_shader.shader_editor && edited_shader.shader_editor->is_unsaved()) {
  317. edited_shader.shader_editor->save_external_data();
  318. }
  319. }
  320. _update_shader_list();
  321. }
  322. void ShaderEditorPlugin::apply_changes() {
  323. for (EditedShader &edited_shader : edited_shaders) {
  324. if (edited_shader.shader_editor) {
  325. edited_shader.shader_editor->apply_shaders();
  326. }
  327. }
  328. }
  329. void ShaderEditorPlugin::_shader_selected(int p_index) {
  330. if (p_index >= (int)edited_shaders.size()) {
  331. return;
  332. }
  333. if (edited_shaders[p_index].shader_editor) {
  334. _switch_to_editor(edited_shaders[p_index].shader_editor);
  335. edited_shaders[p_index].shader_editor->validate_script();
  336. }
  337. shader_tabs->set_current_tab(p_index);
  338. shader_list->select(p_index);
  339. // Avoid `Shader` being edited when editing `ShaderInclude` due to inspector refreshing.
  340. if (edited_shaders[p_index].shader.is_valid()) {
  341. EditorNode::get_singleton()->push_item_no_inspector(edited_shaders[p_index].shader.ptr());
  342. } else {
  343. EditorNode::get_singleton()->push_item_no_inspector(edited_shaders[p_index].shader_inc.ptr());
  344. }
  345. }
  346. void ShaderEditorPlugin::_shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) {
  347. if (p_mouse_button_index == MouseButton::MIDDLE) {
  348. _close_shader(p_item);
  349. }
  350. if (p_mouse_button_index == MouseButton::RIGHT) {
  351. _make_script_list_context_menu();
  352. }
  353. }
  354. void ShaderEditorPlugin::_setup_popup_menu(PopupMenuType p_type, PopupMenu *p_menu) {
  355. if (p_type == FILE) {
  356. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/new", TTRC("New Shader..."), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_MENU_NEW);
  357. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/new_include", TTRC("New Shader Include..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N), FILE_MENU_NEW_INCLUDE);
  358. p_menu->add_separator();
  359. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/open", TTRC("Load Shader File..."), KeyModifierMask::CMD_OR_CTRL | Key::O), FILE_MENU_OPEN);
  360. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/open_include", TTRC("Load Shader Include File..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::O), FILE_MENU_OPEN_INCLUDE);
  361. }
  362. if (p_type == FILE || p_type == CONTEXT_VALID_ITEM) {
  363. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save", TTRC("Save File"), KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::S), FILE_MENU_SAVE);
  364. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save_as", TTRC("Save File As...")), FILE_MENU_SAVE_AS);
  365. }
  366. if (p_type == FILE) {
  367. p_menu->add_separator();
  368. p_menu->add_item(TTR("Open File in Inspector"), FILE_MENU_INSPECT);
  369. p_menu->add_item(TTR("Inspect Native Shader Code..."), FILE_MENU_INSPECT_NATIVE_SHADER_CODE);
  370. p_menu->add_separator();
  371. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTRC("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_MENU_CLOSE);
  372. p_menu->add_separator();
  373. p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_files_panel"), FILE_MENU_TOGGLE_FILES_PANEL);
  374. } else {
  375. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTRC("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_MENU_CLOSE);
  376. p_menu->add_item(TTR("Close All"), FILE_MENU_CLOSE_ALL);
  377. p_menu->add_item(TTR("Close Other Tabs"), FILE_MENU_CLOSE_OTHER_TABS);
  378. if (p_type == CONTEXT_VALID_ITEM) {
  379. p_menu->add_separator();
  380. p_menu->add_item(TTR("Copy Script Path"), FILE_MENU_COPY_PATH);
  381. p_menu->add_item(TTR("Show in FileSystem"), FILE_MENU_SHOW_IN_FILE_SYSTEM);
  382. }
  383. }
  384. }
  385. void ShaderEditorPlugin::_make_script_list_context_menu() {
  386. context_menu->clear();
  387. int selected = shader_tabs->get_current_tab();
  388. if (selected < 0 || selected >= shader_tabs->get_tab_count()) {
  389. return;
  390. }
  391. Control *control = shader_tabs->get_tab_control(selected);
  392. bool is_valid_editor_control = Object::cast_to<TextShaderEditor>(control) || Object::cast_to<VisualShaderEditor>(control);
  393. _setup_popup_menu(is_valid_editor_control ? CONTEXT_VALID_ITEM : CONTEXT, context_menu);
  394. context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_ALL), shader_tabs->get_tab_count() <= 0);
  395. context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_OTHER_TABS), shader_tabs->get_tab_count() <= 1);
  396. context_menu->set_position(files_split->get_screen_position() + files_split->get_local_mouse_position());
  397. context_menu->reset_size();
  398. context_menu->popup();
  399. }
  400. void ShaderEditorPlugin::_close_shader(int p_index) {
  401. ERR_FAIL_INDEX(p_index, shader_tabs->get_tab_count());
  402. Control *c = shader_tabs->get_tab_control(p_index);
  403. VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(c);
  404. if (vs_editor) {
  405. vs_editor->save_editor_layout();
  406. file_menu->get_parent()->remove_child(file_menu);
  407. menu_hb->add_child(file_menu);
  408. menu_hb->move_child(file_menu, 0);
  409. make_floating->get_parent()->remove_child(make_floating);
  410. menu_hb->add_child(make_floating);
  411. } else {
  412. memdelete(edited_shaders[p_index].shader_editor->get_top_bar());
  413. }
  414. memdelete(c);
  415. edited_shaders.remove_at(p_index);
  416. _update_shader_list();
  417. EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.
  418. if (shader_tabs->get_tab_count() == 0) {
  419. shader_list->show(); // Make sure the panel is visible, because it can't be toggled without open shaders.
  420. menu_spacer->show();
  421. } else {
  422. _switch_to_editor(edited_shaders[shader_tabs->get_current_tab()].shader_editor);
  423. }
  424. }
  425. void ShaderEditorPlugin::_close_builtin_shaders_from_scene(const String &p_scene) {
  426. for (uint32_t i = 0; i < edited_shaders.size();) {
  427. Ref<Shader> &shader = edited_shaders[i].shader;
  428. if (shader.is_valid()) {
  429. if (shader->is_built_in() && shader->get_path().begins_with(p_scene)) {
  430. _close_shader(i);
  431. continue;
  432. }
  433. }
  434. Ref<ShaderInclude> &include = edited_shaders[i].shader_inc;
  435. if (include.is_valid()) {
  436. if (include->is_built_in() && include->get_path().begins_with(p_scene)) {
  437. _close_shader(i);
  438. continue;
  439. }
  440. }
  441. i++;
  442. }
  443. }
  444. void ShaderEditorPlugin::_resource_saved(Object *obj) {
  445. // May have been renamed on save.
  446. for (EditedShader &edited_shader : edited_shaders) {
  447. if (edited_shader.shader.ptr() == obj || edited_shader.shader_inc.ptr() == obj) {
  448. _update_shader_list();
  449. return;
  450. }
  451. }
  452. }
  453. void ShaderEditorPlugin::_menu_item_pressed(int p_index) {
  454. switch (p_index) {
  455. case FILE_MENU_NEW: {
  456. String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
  457. shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 0);
  458. shader_create_dialog->popup_centered();
  459. } break;
  460. case FILE_MENU_NEW_INCLUDE: {
  461. String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
  462. shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 2);
  463. shader_create_dialog->popup_centered();
  464. } break;
  465. case FILE_MENU_OPEN: {
  466. InspectorDock::get_singleton()->open_resource("Shader");
  467. } break;
  468. case FILE_MENU_OPEN_INCLUDE: {
  469. InspectorDock::get_singleton()->open_resource("ShaderInclude");
  470. } break;
  471. case FILE_MENU_SAVE: {
  472. int index = shader_tabs->get_current_tab();
  473. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  474. TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
  475. if (editor) {
  476. if (editor->get_trim_trailing_whitespace_on_save()) {
  477. editor->trim_trailing_whitespace();
  478. }
  479. if (editor->get_trim_final_newlines_on_save()) {
  480. editor->trim_final_newlines();
  481. }
  482. }
  483. if (edited_shaders[index].shader.is_valid()) {
  484. EditorNode::get_singleton()->save_resource(edited_shaders[index].shader);
  485. } else {
  486. EditorNode::get_singleton()->save_resource(edited_shaders[index].shader_inc);
  487. }
  488. if (editor) {
  489. editor->tag_saved_version();
  490. }
  491. } break;
  492. case FILE_MENU_SAVE_AS: {
  493. int index = shader_tabs->get_current_tab();
  494. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  495. TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
  496. if (editor) {
  497. if (editor->get_trim_trailing_whitespace_on_save()) {
  498. editor->trim_trailing_whitespace();
  499. }
  500. if (editor->get_trim_final_newlines_on_save()) {
  501. editor->trim_final_newlines();
  502. }
  503. }
  504. String path;
  505. if (edited_shaders[index].shader.is_valid()) {
  506. path = edited_shaders[index].shader->get_path();
  507. if (!path.is_resource_file()) {
  508. path = "";
  509. }
  510. EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader, path);
  511. } else {
  512. path = edited_shaders[index].shader_inc->get_path();
  513. if (!path.is_resource_file()) {
  514. path = "";
  515. }
  516. EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader_inc, path);
  517. }
  518. if (editor) {
  519. editor->tag_saved_version();
  520. }
  521. } break;
  522. case FILE_MENU_INSPECT: {
  523. int index = shader_tabs->get_current_tab();
  524. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  525. if (edited_shaders[index].shader.is_valid()) {
  526. EditorNode::get_singleton()->push_item(edited_shaders[index].shader.ptr());
  527. } else {
  528. EditorNode::get_singleton()->push_item(edited_shaders[index].shader_inc.ptr());
  529. }
  530. } break;
  531. case FILE_MENU_INSPECT_NATIVE_SHADER_CODE: {
  532. int index = shader_tabs->get_current_tab();
  533. if (edited_shaders[index].shader.is_valid()) {
  534. edited_shaders[index].shader->inspect_native_shader_code();
  535. }
  536. } break;
  537. case FILE_MENU_CLOSE: {
  538. _close_shader(shader_tabs->get_current_tab());
  539. } break;
  540. case FILE_MENU_CLOSE_ALL: {
  541. while (shader_tabs->get_tab_count() > 0) {
  542. _close_shader(0);
  543. }
  544. } break;
  545. case FILE_MENU_CLOSE_OTHER_TABS: {
  546. int index = shader_tabs->get_current_tab();
  547. for (int i = 0; i < index; i++) {
  548. _close_shader(0);
  549. }
  550. while (shader_tabs->get_tab_count() > 1) {
  551. _close_shader(1);
  552. }
  553. } break;
  554. case FILE_MENU_SHOW_IN_FILE_SYSTEM: {
  555. Ref<Resource> shader = _get_current_shader();
  556. String path = shader->get_path();
  557. if (!path.is_empty()) {
  558. FileSystemDock::get_singleton()->navigate_to_path(path);
  559. }
  560. } break;
  561. case FILE_MENU_COPY_PATH: {
  562. Ref<Resource> shader = _get_current_shader();
  563. DisplayServer::get_singleton()->clipboard_set(shader->get_path());
  564. } break;
  565. case FILE_MENU_TOGGLE_FILES_PANEL: {
  566. shader_list->set_visible(!shader_list->is_visible());
  567. int index = shader_tabs->get_current_tab();
  568. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  569. TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
  570. if (editor) {
  571. editor->get_code_editor()->update_toggle_files_button();
  572. } else {
  573. VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(edited_shaders[index].shader_editor);
  574. if (vs_editor) {
  575. vs_editor->update_toggle_files_button();
  576. }
  577. }
  578. } break;
  579. }
  580. }
  581. void ShaderEditorPlugin::_shader_created(Ref<Shader> p_shader) {
  582. EditorNode::get_singleton()->push_item(p_shader.ptr());
  583. }
  584. void ShaderEditorPlugin::_shader_include_created(Ref<ShaderInclude> p_shader_inc) {
  585. EditorNode::get_singleton()->push_item(p_shader_inc.ptr());
  586. }
  587. Variant ShaderEditorPlugin::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  588. if (shader_list->get_item_count() == 0) {
  589. return Variant();
  590. }
  591. int idx = 0;
  592. if (p_point == Vector2(Math::INF, Math::INF)) {
  593. if (shader_list->is_anything_selected()) {
  594. idx = shader_list->get_selected_items()[0];
  595. }
  596. } else {
  597. idx = shader_list->get_item_at_position(p_point);
  598. }
  599. if (idx < 0) {
  600. return Variant();
  601. }
  602. HBoxContainer *drag_preview = memnew(HBoxContainer);
  603. String preview_name = shader_list->get_item_text(idx);
  604. Ref<Texture2D> preview_icon = shader_list->get_item_icon(idx);
  605. if (preview_icon.is_valid()) {
  606. TextureRect *tf = memnew(TextureRect);
  607. tf->set_texture(preview_icon);
  608. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  609. drag_preview->add_child(tf);
  610. }
  611. Label *label = memnew(Label(preview_name));
  612. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate script names.
  613. drag_preview->add_child(label);
  614. files_split->set_drag_preview(drag_preview);
  615. Dictionary drag_data;
  616. drag_data["type"] = "shader_list_element";
  617. drag_data["shader_list_element"] = idx;
  618. return drag_data;
  619. }
  620. bool ShaderEditorPlugin::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  621. Dictionary d = p_data;
  622. if (!d.has("type")) {
  623. return false;
  624. }
  625. if (String(d["type"]) == "shader_list_element") {
  626. return true;
  627. }
  628. if (String(d["type"]) == "files") {
  629. Vector<String> files = d["files"];
  630. if (files.is_empty()) {
  631. return false;
  632. }
  633. for (int i = 0; i < files.size(); i++) {
  634. const String &file = files[i];
  635. if (ResourceLoader::exists(file, "Shader")) {
  636. Ref<Shader> shader = ResourceLoader::load(file);
  637. if (shader.is_valid()) {
  638. return true;
  639. }
  640. }
  641. if (ResourceLoader::exists(file, "ShaderInclude")) {
  642. Ref<ShaderInclude> sinclude = ResourceLoader::load(file);
  643. if (sinclude.is_valid()) {
  644. return true;
  645. }
  646. }
  647. }
  648. return false;
  649. }
  650. return false;
  651. }
  652. void ShaderEditorPlugin::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  653. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  654. return;
  655. }
  656. Dictionary d = p_data;
  657. if (!d.has("type")) {
  658. return;
  659. }
  660. if (String(d["type"]) == "shader_list_element") {
  661. int idx = d["shader_list_element"];
  662. int new_idx = 0;
  663. if (p_point == Vector2(Math::INF, Math::INF)) {
  664. if (shader_list->is_anything_selected()) {
  665. new_idx = shader_list->get_selected_items()[0];
  666. }
  667. } else {
  668. new_idx = shader_list->get_item_at_position(p_point);
  669. }
  670. _move_shader_tab(idx, new_idx);
  671. return;
  672. }
  673. if (String(d["type"]) == "files") {
  674. Vector<String> files = d["files"];
  675. for (int i = 0; i < files.size(); i++) {
  676. const String &file = files[i];
  677. Ref<Resource> res;
  678. if (ResourceLoader::exists(file, "Shader") || ResourceLoader::exists(file, "ShaderInclude")) {
  679. res = ResourceLoader::load(file);
  680. }
  681. if (res.is_valid()) {
  682. edit(res.ptr());
  683. }
  684. }
  685. }
  686. }
  687. void ShaderEditorPlugin::_window_changed(bool p_visible) {
  688. make_floating->set_visible(!p_visible);
  689. }
  690. void ShaderEditorPlugin::_set_text_shader_zoom_factor(float p_zoom_factor) {
  691. if (text_shader_zoom_factor == p_zoom_factor) {
  692. return;
  693. }
  694. text_shader_zoom_factor = p_zoom_factor;
  695. }
  696. void ShaderEditorPlugin::_update_shader_editor_zoom_factor(CodeTextEditor *p_shader_editor) const {
  697. if (p_shader_editor && p_shader_editor->is_visible_in_tree() && text_shader_zoom_factor != p_shader_editor->get_zoom_factor()) {
  698. p_shader_editor->set_zoom_factor(text_shader_zoom_factor);
  699. }
  700. }
  701. void ShaderEditorPlugin::_switch_to_editor(ShaderEditor *p_editor) {
  702. Control *bar = p_editor->get_top_bar();
  703. VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(p_editor);
  704. if (vs_editor) {
  705. file_menu->get_parent()->remove_child(file_menu);
  706. bar->add_child(file_menu);
  707. bar->move_child(file_menu, 2); // Toggle Files Panel button + separator.
  708. make_floating->get_parent()->remove_child(make_floating);
  709. bar->add_child(make_floating);
  710. } else {
  711. if (menu_spacer->is_visible()) {
  712. menu_spacer->hide();
  713. }
  714. // Just swapped from a visual shader editor.
  715. if (file_menu->get_parent() != menu_hb) {
  716. file_menu->get_parent()->remove_child(file_menu);
  717. menu_hb->add_child(file_menu);
  718. menu_hb->move_child(file_menu, 0);
  719. make_floating->get_parent()->remove_child(make_floating);
  720. menu_hb->add_child(make_floating);
  721. }
  722. }
  723. for (int i = 0; i < shader_tabs->get_tab_count(); i++) {
  724. ShaderEditor *se = Object::cast_to<ShaderEditor>(shader_tabs->get_tab_control(i));
  725. if (se && se->get_top_bar()) {
  726. if (se == p_editor) {
  727. se->get_top_bar()->show();
  728. } else {
  729. se->get_top_bar()->hide();
  730. }
  731. }
  732. }
  733. }
  734. void ShaderEditorPlugin::_file_removed(const String &p_removed_file) {
  735. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  736. if (edited_shaders[i].path == p_removed_file) {
  737. _close_shader(i);
  738. break;
  739. }
  740. }
  741. }
  742. void ShaderEditorPlugin::_res_saved_callback(const Ref<Resource> &p_res) {
  743. if (p_res.is_null()) {
  744. return;
  745. }
  746. const String &path = p_res->get_path();
  747. for (EditedShader &edited : edited_shaders) {
  748. Ref<Resource> shader_res = edited.shader;
  749. if (shader_res.is_null()) {
  750. shader_res = edited.shader_inc;
  751. }
  752. ERR_FAIL_COND(shader_res.is_null());
  753. TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(edited.shader_editor);
  754. if (!text_shader_editor || !shader_res->is_built_in()) {
  755. continue;
  756. }
  757. if (shader_res->get_path().get_slice("::", 0) == path) {
  758. text_shader_editor->tag_saved_version();
  759. _update_shader_list();
  760. }
  761. }
  762. }
  763. void ShaderEditorPlugin::_set_file_specific_items_disabled(bool p_disabled) {
  764. PopupMenu *file_popup_menu = file_menu->get_popup();
  765. file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_SAVE), p_disabled);
  766. file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_SAVE_AS), p_disabled);
  767. file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_INSPECT), p_disabled);
  768. file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_INSPECT_NATIVE_SHADER_CODE), p_disabled);
  769. file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_CLOSE), p_disabled);
  770. }
  771. void ShaderEditorPlugin::_notification(int p_what) {
  772. switch (p_what) {
  773. case NOTIFICATION_READY: {
  774. EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ShaderEditorPlugin::_resource_saved), CONNECT_DEFERRED);
  775. EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ShaderEditorPlugin::_close_builtin_shaders_from_scene));
  776. FileSystemDock::get_singleton()->connect("file_removed", callable_mp(this, &ShaderEditorPlugin::_file_removed));
  777. EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ShaderEditorPlugin::_res_saved_callback));
  778. } break;
  779. }
  780. }
  781. ShaderEditorPlugin::ShaderEditorPlugin() {
  782. window_wrapper = memnew(WindowWrapper);
  783. window_wrapper->set_window_title(vformat(TTR("%s - Godot Engine"), TTR("Shader Editor")));
  784. window_wrapper->set_margins_enabled(true);
  785. main_container = memnew(VBoxContainer);
  786. Ref<Shortcut> make_floating_shortcut = ED_SHORTCUT_AND_COMMAND("shader_editor/make_floating", TTRC("Make Floating"));
  787. window_wrapper->set_wrapped_control(main_container, make_floating_shortcut);
  788. files_split = memnew(HSplitContainer);
  789. files_split->set_split_offset(200 * EDSCALE);
  790. files_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  791. menu_hb = memnew(HBoxContainer);
  792. main_container->add_child(menu_hb);
  793. file_menu = memnew(MenuButton);
  794. file_menu->set_text(TTR("File"));
  795. file_menu->set_shortcut_context(files_split);
  796. _setup_popup_menu(FILE, file_menu->get_popup());
  797. file_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
  798. menu_hb->add_child(file_menu);
  799. _set_file_specific_items_disabled(true);
  800. context_menu = memnew(PopupMenu);
  801. add_child(context_menu);
  802. context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
  803. menu_spacer = menu_hb->add_spacer();
  804. make_floating = memnew(ScreenSelect);
  805. make_floating->set_flat(true);
  806. make_floating->connect("request_open_in_screen", callable_mp(window_wrapper, &WindowWrapper::enable_window_on_screen).bind(true));
  807. if (!make_floating->is_disabled()) {
  808. // Override default ScreenSelect tooltip if multi-window support is available.
  809. make_floating->set_tooltip_text(TTR("Make the shader editor floating.") + "\n" + TTR("Right-click to open the screen selector."));
  810. }
  811. menu_hb->add_child(make_floating);
  812. window_wrapper->connect("window_visibility_changed", callable_mp(this, &ShaderEditorPlugin::_window_changed));
  813. shader_list = memnew(ItemList);
  814. shader_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  815. shader_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  816. shader_list->set_theme_type_variation("ItemListSecondary");
  817. files_split->add_child(shader_list);
  818. shader_list->connect(SceneStringName(item_selected), callable_mp(this, &ShaderEditorPlugin::_shader_selected));
  819. shader_list->connect("item_clicked", callable_mp(this, &ShaderEditorPlugin::_shader_list_clicked));
  820. shader_list->set_allow_rmb_select(true);
  821. SET_DRAG_FORWARDING_GCD(shader_list, ShaderEditorPlugin);
  822. main_container->add_child(files_split);
  823. main_container->set_custom_minimum_size(Size2(100, 300) * EDSCALE);
  824. shader_tabs = memnew(TabContainer);
  825. shader_tabs->set_custom_minimum_size(Size2(460, 300) * EDSCALE);
  826. shader_tabs->set_tabs_visible(false);
  827. shader_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  828. files_split->add_child(shader_tabs);
  829. Ref<StyleBoxEmpty> empty;
  830. empty.instantiate();
  831. shader_tabs->add_theme_style_override(SceneStringName(panel), empty);
  832. button = EditorNode::get_bottom_panel()->add_item(TTRC("Shader Editor"), window_wrapper, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_shader_editor_bottom_panel", TTRC("Toggle Shader Editor Bottom Panel"), KeyModifierMask::ALT | Key::S));
  833. shader_create_dialog = memnew(ShaderCreateDialog);
  834. files_split->add_child(shader_create_dialog);
  835. shader_create_dialog->connect("shader_created", callable_mp(this, &ShaderEditorPlugin::_shader_created));
  836. shader_create_dialog->connect("shader_include_created", callable_mp(this, &ShaderEditorPlugin::_shader_include_created));
  837. }