shader_editor_plugin.cpp 33 KB

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