shader_editor_plugin.cpp 30 KB

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