shader_editor_plugin.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. }
  340. void ShaderEditorPlugin::_shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) {
  341. if (p_mouse_button_index == MouseButton::MIDDLE) {
  342. _close_shader(p_item);
  343. }
  344. if (p_mouse_button_index == MouseButton::RIGHT) {
  345. _make_script_list_context_menu();
  346. }
  347. }
  348. void ShaderEditorPlugin::_setup_popup_menu(PopupMenuType p_type, PopupMenu *p_menu) {
  349. if (p_type == FILE) {
  350. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/new", TTRC("New Shader..."), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_MENU_NEW);
  351. 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);
  352. p_menu->add_separator();
  353. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/open", TTRC("Load Shader File..."), KeyModifierMask::CMD_OR_CTRL | Key::O), FILE_MENU_OPEN);
  354. 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);
  355. }
  356. if (p_type == FILE || p_type == CONTEXT_VALID_ITEM) {
  357. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save", TTRC("Save File"), KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::S), FILE_MENU_SAVE);
  358. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save_as", TTRC("Save File As...")), FILE_MENU_SAVE_AS);
  359. }
  360. if (p_type == FILE) {
  361. p_menu->add_separator();
  362. p_menu->add_item(TTR("Open File in Inspector"), FILE_MENU_INSPECT);
  363. p_menu->add_item(TTR("Inspect Native Shader Code..."), FILE_MENU_INSPECT_NATIVE_SHADER_CODE);
  364. p_menu->add_separator();
  365. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTRC("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_MENU_CLOSE);
  366. p_menu->add_separator();
  367. p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_files_panel"), FILE_MENU_TOGGLE_FILES_PANEL);
  368. } else {
  369. p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTRC("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_MENU_CLOSE);
  370. p_menu->add_item(TTR("Close All"), FILE_MENU_CLOSE_ALL);
  371. p_menu->add_item(TTR("Close Other Tabs"), FILE_MENU_CLOSE_OTHER_TABS);
  372. if (p_type == CONTEXT_VALID_ITEM) {
  373. p_menu->add_separator();
  374. p_menu->add_item(TTR("Copy Script Path"), FILE_MENU_COPY_PATH);
  375. p_menu->add_item(TTR("Show in FileSystem"), FILE_MENU_SHOW_IN_FILE_SYSTEM);
  376. }
  377. }
  378. }
  379. void ShaderEditorPlugin::_make_script_list_context_menu() {
  380. context_menu->clear();
  381. int selected = shader_tabs->get_current_tab();
  382. if (selected < 0 || selected >= shader_tabs->get_tab_count()) {
  383. return;
  384. }
  385. Control *control = shader_tabs->get_tab_control(selected);
  386. bool is_valid_editor_control = Object::cast_to<TextShaderEditor>(control) || Object::cast_to<VisualShaderEditor>(control);
  387. _setup_popup_menu(is_valid_editor_control ? CONTEXT_VALID_ITEM : CONTEXT, context_menu);
  388. context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_ALL), shader_tabs->get_tab_count() <= 0);
  389. context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_OTHER_TABS), shader_tabs->get_tab_count() <= 1);
  390. context_menu->set_position(files_split->get_screen_position() + files_split->get_local_mouse_position());
  391. context_menu->reset_size();
  392. context_menu->popup();
  393. }
  394. void ShaderEditorPlugin::_close_shader(int p_index) {
  395. ERR_FAIL_INDEX(p_index, shader_tabs->get_tab_count());
  396. Control *c = shader_tabs->get_tab_control(p_index);
  397. VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(c);
  398. if (vs_editor) {
  399. file_menu->get_parent()->remove_child(file_menu);
  400. menu_hb->add_child(file_menu);
  401. menu_hb->move_child(file_menu, 0);
  402. make_floating->get_parent()->remove_child(make_floating);
  403. menu_hb->add_child(make_floating);
  404. } else {
  405. memdelete(edited_shaders[p_index].shader_editor->get_top_bar());
  406. }
  407. memdelete(c);
  408. edited_shaders.remove_at(p_index);
  409. _update_shader_list();
  410. EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.
  411. if (shader_tabs->get_tab_count() == 0) {
  412. shader_list->show(); // Make sure the panel is visible, because it can't be toggled without open shaders.
  413. menu_spacer->show();
  414. } else {
  415. _switch_to_editor(edited_shaders[shader_tabs->get_current_tab()].shader_editor);
  416. }
  417. }
  418. void ShaderEditorPlugin::_close_builtin_shaders_from_scene(const String &p_scene) {
  419. for (uint32_t i = 0; i < edited_shaders.size();) {
  420. Ref<Shader> &shader = edited_shaders[i].shader;
  421. if (shader.is_valid()) {
  422. if (shader->is_built_in() && shader->get_path().begins_with(p_scene)) {
  423. _close_shader(i);
  424. continue;
  425. }
  426. }
  427. Ref<ShaderInclude> &include = edited_shaders[i].shader_inc;
  428. if (include.is_valid()) {
  429. if (include->is_built_in() && include->get_path().begins_with(p_scene)) {
  430. _close_shader(i);
  431. continue;
  432. }
  433. }
  434. i++;
  435. }
  436. }
  437. void ShaderEditorPlugin::_resource_saved(Object *obj) {
  438. // May have been renamed on save.
  439. for (EditedShader &edited_shader : edited_shaders) {
  440. if (edited_shader.shader.ptr() == obj || edited_shader.shader_inc.ptr() == obj) {
  441. _update_shader_list();
  442. return;
  443. }
  444. }
  445. }
  446. void ShaderEditorPlugin::_menu_item_pressed(int p_index) {
  447. switch (p_index) {
  448. case FILE_MENU_NEW: {
  449. String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
  450. shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 0);
  451. shader_create_dialog->popup_centered();
  452. } break;
  453. case FILE_MENU_NEW_INCLUDE: {
  454. String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
  455. shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 2);
  456. shader_create_dialog->popup_centered();
  457. } break;
  458. case FILE_MENU_OPEN: {
  459. InspectorDock::get_singleton()->open_resource("Shader");
  460. } break;
  461. case FILE_MENU_OPEN_INCLUDE: {
  462. InspectorDock::get_singleton()->open_resource("ShaderInclude");
  463. } break;
  464. case FILE_MENU_SAVE: {
  465. int index = shader_tabs->get_current_tab();
  466. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  467. TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
  468. if (editor) {
  469. if (editor->get_trim_trailing_whitespace_on_save()) {
  470. editor->trim_trailing_whitespace();
  471. }
  472. if (editor->get_trim_final_newlines_on_save()) {
  473. editor->trim_final_newlines();
  474. }
  475. }
  476. if (edited_shaders[index].shader.is_valid()) {
  477. EditorNode::get_singleton()->save_resource(edited_shaders[index].shader);
  478. } else {
  479. EditorNode::get_singleton()->save_resource(edited_shaders[index].shader_inc);
  480. }
  481. if (editor) {
  482. editor->tag_saved_version();
  483. }
  484. } break;
  485. case FILE_MENU_SAVE_AS: {
  486. int index = shader_tabs->get_current_tab();
  487. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  488. TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
  489. if (editor) {
  490. if (editor->get_trim_trailing_whitespace_on_save()) {
  491. editor->trim_trailing_whitespace();
  492. }
  493. if (editor->get_trim_final_newlines_on_save()) {
  494. editor->trim_final_newlines();
  495. }
  496. }
  497. String path;
  498. if (edited_shaders[index].shader.is_valid()) {
  499. path = edited_shaders[index].shader->get_path();
  500. if (!path.is_resource_file()) {
  501. path = "";
  502. }
  503. EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader, path);
  504. } else {
  505. path = edited_shaders[index].shader_inc->get_path();
  506. if (!path.is_resource_file()) {
  507. path = "";
  508. }
  509. EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader_inc, path);
  510. }
  511. if (editor) {
  512. editor->tag_saved_version();
  513. }
  514. } break;
  515. case FILE_MENU_INSPECT: {
  516. int index = shader_tabs->get_current_tab();
  517. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  518. if (edited_shaders[index].shader.is_valid()) {
  519. EditorNode::get_singleton()->push_item(edited_shaders[index].shader.ptr());
  520. } else {
  521. EditorNode::get_singleton()->push_item(edited_shaders[index].shader_inc.ptr());
  522. }
  523. } break;
  524. case FILE_MENU_INSPECT_NATIVE_SHADER_CODE: {
  525. int index = shader_tabs->get_current_tab();
  526. if (edited_shaders[index].shader.is_valid()) {
  527. edited_shaders[index].shader->inspect_native_shader_code();
  528. }
  529. } break;
  530. case FILE_MENU_CLOSE: {
  531. _close_shader(shader_tabs->get_current_tab());
  532. } break;
  533. case FILE_MENU_CLOSE_ALL: {
  534. while (shader_tabs->get_tab_count() > 0) {
  535. _close_shader(0);
  536. }
  537. } break;
  538. case FILE_MENU_CLOSE_OTHER_TABS: {
  539. int index = shader_tabs->get_current_tab();
  540. for (int i = 0; i < index; i++) {
  541. _close_shader(0);
  542. }
  543. while (shader_tabs->get_tab_count() > 1) {
  544. _close_shader(1);
  545. }
  546. } break;
  547. case FILE_MENU_SHOW_IN_FILE_SYSTEM: {
  548. Ref<Resource> shader = _get_current_shader();
  549. String path = shader->get_path();
  550. if (!path.is_empty()) {
  551. FileSystemDock::get_singleton()->navigate_to_path(path);
  552. }
  553. } break;
  554. case FILE_MENU_COPY_PATH: {
  555. Ref<Resource> shader = _get_current_shader();
  556. DisplayServer::get_singleton()->clipboard_set(shader->get_path());
  557. } break;
  558. case FILE_MENU_TOGGLE_FILES_PANEL: {
  559. shader_list->set_visible(!shader_list->is_visible());
  560. int index = shader_tabs->get_current_tab();
  561. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  562. TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
  563. if (editor) {
  564. editor->get_code_editor()->update_toggle_files_button();
  565. } else {
  566. VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(edited_shaders[index].shader_editor);
  567. if (vs_editor) {
  568. vs_editor->update_toggle_files_button();
  569. }
  570. }
  571. } break;
  572. }
  573. }
  574. void ShaderEditorPlugin::_shader_created(Ref<Shader> p_shader) {
  575. EditorNode::get_singleton()->push_item(p_shader.ptr());
  576. }
  577. void ShaderEditorPlugin::_shader_include_created(Ref<ShaderInclude> p_shader_inc) {
  578. EditorNode::get_singleton()->push_item(p_shader_inc.ptr());
  579. }
  580. Variant ShaderEditorPlugin::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  581. if (shader_list->get_item_count() == 0) {
  582. return Variant();
  583. }
  584. int idx = 0;
  585. if (p_point == Vector2(Math::INF, Math::INF)) {
  586. if (shader_list->is_anything_selected()) {
  587. idx = shader_list->get_selected_items()[0];
  588. }
  589. } else {
  590. idx = shader_list->get_item_at_position(p_point);
  591. }
  592. if (idx < 0) {
  593. return Variant();
  594. }
  595. HBoxContainer *drag_preview = memnew(HBoxContainer);
  596. String preview_name = shader_list->get_item_text(idx);
  597. Ref<Texture2D> preview_icon = shader_list->get_item_icon(idx);
  598. if (preview_icon.is_valid()) {
  599. TextureRect *tf = memnew(TextureRect);
  600. tf->set_texture(preview_icon);
  601. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  602. drag_preview->add_child(tf);
  603. }
  604. Label *label = memnew(Label(preview_name));
  605. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate script names.
  606. drag_preview->add_child(label);
  607. files_split->set_drag_preview(drag_preview);
  608. Dictionary drag_data;
  609. drag_data["type"] = "shader_list_element";
  610. drag_data["shader_list_element"] = idx;
  611. return drag_data;
  612. }
  613. bool ShaderEditorPlugin::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  614. Dictionary d = p_data;
  615. if (!d.has("type")) {
  616. return false;
  617. }
  618. if (String(d["type"]) == "shader_list_element") {
  619. return true;
  620. }
  621. if (String(d["type"]) == "files") {
  622. Vector<String> files = d["files"];
  623. if (files.is_empty()) {
  624. return false;
  625. }
  626. for (int i = 0; i < files.size(); i++) {
  627. const String &file = files[i];
  628. if (ResourceLoader::exists(file, "Shader")) {
  629. Ref<Shader> shader = ResourceLoader::load(file);
  630. if (shader.is_valid()) {
  631. return true;
  632. }
  633. }
  634. if (ResourceLoader::exists(file, "ShaderInclude")) {
  635. Ref<ShaderInclude> sinclude = ResourceLoader::load(file);
  636. if (sinclude.is_valid()) {
  637. return true;
  638. }
  639. }
  640. }
  641. return false;
  642. }
  643. return false;
  644. }
  645. void ShaderEditorPlugin::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  646. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  647. return;
  648. }
  649. Dictionary d = p_data;
  650. if (!d.has("type")) {
  651. return;
  652. }
  653. if (String(d["type"]) == "shader_list_element") {
  654. int idx = d["shader_list_element"];
  655. int new_idx = 0;
  656. if (p_point == Vector2(Math::INF, Math::INF)) {
  657. if (shader_list->is_anything_selected()) {
  658. new_idx = shader_list->get_selected_items()[0];
  659. }
  660. } else {
  661. new_idx = shader_list->get_item_at_position(p_point);
  662. }
  663. _move_shader_tab(idx, new_idx);
  664. return;
  665. }
  666. if (String(d["type"]) == "files") {
  667. Vector<String> files = d["files"];
  668. for (int i = 0; i < files.size(); i++) {
  669. const String &file = files[i];
  670. Ref<Resource> res;
  671. if (ResourceLoader::exists(file, "Shader") || ResourceLoader::exists(file, "ShaderInclude")) {
  672. res = ResourceLoader::load(file);
  673. }
  674. if (res.is_valid()) {
  675. edit(res.ptr());
  676. }
  677. }
  678. }
  679. }
  680. void ShaderEditorPlugin::_window_changed(bool p_visible) {
  681. make_floating->set_visible(!p_visible);
  682. }
  683. void ShaderEditorPlugin::_set_text_shader_zoom_factor(float p_zoom_factor) {
  684. if (text_shader_zoom_factor == p_zoom_factor) {
  685. return;
  686. }
  687. text_shader_zoom_factor = p_zoom_factor;
  688. }
  689. void ShaderEditorPlugin::_update_shader_editor_zoom_factor(CodeTextEditor *p_shader_editor) const {
  690. if (p_shader_editor && p_shader_editor->is_visible_in_tree() && text_shader_zoom_factor != p_shader_editor->get_zoom_factor()) {
  691. p_shader_editor->set_zoom_factor(text_shader_zoom_factor);
  692. }
  693. }
  694. void ShaderEditorPlugin::_switch_to_editor(ShaderEditor *p_editor) {
  695. Control *bar = p_editor->get_top_bar();
  696. VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(p_editor);
  697. if (vs_editor) {
  698. file_menu->get_parent()->remove_child(file_menu);
  699. bar->add_child(file_menu);
  700. bar->move_child(file_menu, 2); // Toggle Files Panel button + separator.
  701. make_floating->get_parent()->remove_child(make_floating);
  702. bar->add_child(make_floating);
  703. } else {
  704. if (menu_spacer->is_visible()) {
  705. menu_spacer->hide();
  706. }
  707. // Just swapped from a visual shader editor.
  708. if (file_menu->get_parent() != menu_hb) {
  709. file_menu->get_parent()->remove_child(file_menu);
  710. menu_hb->add_child(file_menu);
  711. menu_hb->move_child(file_menu, 0);
  712. make_floating->get_parent()->remove_child(make_floating);
  713. menu_hb->add_child(make_floating);
  714. }
  715. }
  716. for (int i = 0; i < shader_tabs->get_tab_count(); i++) {
  717. ShaderEditor *se = Object::cast_to<ShaderEditor>(shader_tabs->get_tab_control(i));
  718. if (se && se->get_top_bar()) {
  719. if (se == p_editor) {
  720. se->get_top_bar()->show();
  721. } else {
  722. se->get_top_bar()->hide();
  723. }
  724. }
  725. }
  726. }
  727. void ShaderEditorPlugin::_file_removed(const String &p_removed_file) {
  728. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  729. if (edited_shaders[i].path == p_removed_file) {
  730. _close_shader(i);
  731. break;
  732. }
  733. }
  734. }
  735. void ShaderEditorPlugin::_res_saved_callback(const Ref<Resource> &p_res) {
  736. if (p_res.is_null()) {
  737. return;
  738. }
  739. const String &path = p_res->get_path();
  740. for (EditedShader &edited : edited_shaders) {
  741. Ref<Resource> shader_res = edited.shader;
  742. if (shader_res.is_null()) {
  743. shader_res = edited.shader_inc;
  744. }
  745. ERR_FAIL_COND(shader_res.is_null());
  746. TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(edited.shader_editor);
  747. if (!text_shader_editor || !shader_res->is_built_in()) {
  748. continue;
  749. }
  750. if (shader_res->get_path().get_slice("::", 0) == path) {
  751. text_shader_editor->tag_saved_version();
  752. _update_shader_list();
  753. }
  754. }
  755. }
  756. void ShaderEditorPlugin::_set_file_specific_items_disabled(bool p_disabled) {
  757. PopupMenu *file_popup_menu = file_menu->get_popup();
  758. file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_SAVE), p_disabled);
  759. file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_SAVE_AS), p_disabled);
  760. file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_INSPECT), p_disabled);
  761. file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_INSPECT_NATIVE_SHADER_CODE), p_disabled);
  762. file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_CLOSE), p_disabled);
  763. }
  764. void ShaderEditorPlugin::_notification(int p_what) {
  765. switch (p_what) {
  766. case NOTIFICATION_READY: {
  767. EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ShaderEditorPlugin::_resource_saved), CONNECT_DEFERRED);
  768. EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ShaderEditorPlugin::_close_builtin_shaders_from_scene));
  769. FileSystemDock::get_singleton()->connect("file_removed", callable_mp(this, &ShaderEditorPlugin::_file_removed));
  770. EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ShaderEditorPlugin::_res_saved_callback));
  771. } break;
  772. }
  773. }
  774. ShaderEditorPlugin::ShaderEditorPlugin() {
  775. window_wrapper = memnew(WindowWrapper);
  776. window_wrapper->set_window_title(vformat(TTR("%s - Godot Engine"), TTR("Shader Editor")));
  777. window_wrapper->set_margins_enabled(true);
  778. main_container = memnew(VBoxContainer);
  779. Ref<Shortcut> make_floating_shortcut = ED_SHORTCUT_AND_COMMAND("shader_editor/make_floating", TTRC("Make Floating"));
  780. window_wrapper->set_wrapped_control(main_container, make_floating_shortcut);
  781. files_split = memnew(HSplitContainer);
  782. files_split->set_split_offset(200 * EDSCALE);
  783. files_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  784. menu_hb = memnew(HBoxContainer);
  785. main_container->add_child(menu_hb);
  786. file_menu = memnew(MenuButton);
  787. file_menu->set_text(TTR("File"));
  788. file_menu->set_shortcut_context(files_split);
  789. _setup_popup_menu(FILE, file_menu->get_popup());
  790. file_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
  791. menu_hb->add_child(file_menu);
  792. _set_file_specific_items_disabled(true);
  793. context_menu = memnew(PopupMenu);
  794. add_child(context_menu);
  795. context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
  796. menu_spacer = menu_hb->add_spacer();
  797. make_floating = memnew(ScreenSelect);
  798. make_floating->set_flat(true);
  799. make_floating->connect("request_open_in_screen", callable_mp(window_wrapper, &WindowWrapper::enable_window_on_screen).bind(true));
  800. if (!make_floating->is_disabled()) {
  801. // Override default ScreenSelect tooltip if multi-window support is available.
  802. make_floating->set_tooltip_text(TTR("Make the shader editor floating.") + "\n" + TTR("Right-click to open the screen selector."));
  803. }
  804. menu_hb->add_child(make_floating);
  805. window_wrapper->connect("window_visibility_changed", callable_mp(this, &ShaderEditorPlugin::_window_changed));
  806. shader_list = memnew(ItemList);
  807. shader_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  808. shader_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  809. shader_list->set_theme_type_variation("ItemListSecondary");
  810. files_split->add_child(shader_list);
  811. shader_list->connect(SceneStringName(item_selected), callable_mp(this, &ShaderEditorPlugin::_shader_selected));
  812. shader_list->connect("item_clicked", callable_mp(this, &ShaderEditorPlugin::_shader_list_clicked));
  813. shader_list->set_allow_rmb_select(true);
  814. SET_DRAG_FORWARDING_GCD(shader_list, ShaderEditorPlugin);
  815. main_container->add_child(files_split);
  816. main_container->set_custom_minimum_size(Size2(100, 300) * EDSCALE);
  817. shader_tabs = memnew(TabContainer);
  818. shader_tabs->set_custom_minimum_size(Size2(460, 300) * EDSCALE);
  819. shader_tabs->set_tabs_visible(false);
  820. shader_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  821. files_split->add_child(shader_tabs);
  822. Ref<StyleBoxEmpty> empty;
  823. empty.instantiate();
  824. shader_tabs->add_theme_style_override(SceneStringName(panel), empty);
  825. button = EditorNode::get_bottom_panel()->add_item(TTR("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));
  826. shader_create_dialog = memnew(ShaderCreateDialog);
  827. files_split->add_child(shader_create_dialog);
  828. shader_create_dialog->connect("shader_created", callable_mp(this, &ShaderEditorPlugin::_shader_created));
  829. shader_create_dialog->connect("shader_include_created", callable_mp(this, &ShaderEditorPlugin::_shader_include_created));
  830. }