shader_editor_plugin.cpp 30 KB

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