shader_editor_plugin.cpp 34 KB

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