shader_editor_plugin.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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_scale.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/filesystem_dock.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/window_wrapper.h"
  42. #include "scene/gui/item_list.h"
  43. #include "scene/gui/texture_rect.h"
  44. void ShaderEditorPlugin::_update_shader_list() {
  45. shader_list->clear();
  46. for (EditedShader &edited_shader : edited_shaders) {
  47. Ref<Resource> shader = edited_shader.shader;
  48. if (shader.is_null()) {
  49. shader = edited_shader.shader_inc;
  50. }
  51. String path = shader->get_path();
  52. String text = path.get_file();
  53. if (text.is_empty()) {
  54. // This appears for newly created built-in shaders before saving the scene.
  55. text = TTR("[unsaved]");
  56. } else if (shader->is_built_in()) {
  57. const String &shader_name = shader->get_name();
  58. if (!shader_name.is_empty()) {
  59. text = vformat("%s (%s)", shader_name, text.get_slice("::", 0));
  60. }
  61. }
  62. // When shader is deleted in filesystem dock, need this to correctly close shader editor.
  63. edited_shader.path = path;
  64. bool unsaved = false;
  65. if (edited_shader.shader_editor) {
  66. unsaved = edited_shader.shader_editor->is_unsaved();
  67. }
  68. // TODO: Handle visual shaders too.
  69. if (unsaved) {
  70. text += "(*)";
  71. }
  72. String _class = shader->get_class();
  73. if (!shader_list->has_theme_icon(_class, EditorStringName(EditorIcons))) {
  74. _class = "TextFile";
  75. }
  76. Ref<Texture2D> icon = shader_list->get_editor_theme_icon(_class);
  77. shader_list->add_item(text, icon);
  78. shader_list->set_item_tooltip(shader_list->get_item_count() - 1, path);
  79. }
  80. if (shader_tabs->get_tab_count()) {
  81. shader_list->select(shader_tabs->get_current_tab());
  82. }
  83. for (int i = FILE_SAVE; i < FILE_MAX; i++) {
  84. file_menu->get_popup()->set_item_disabled(file_menu->get_popup()->get_item_index(i), edited_shaders.is_empty());
  85. }
  86. _update_shader_list_status();
  87. }
  88. void ShaderEditorPlugin::_update_shader_list_status() {
  89. for (int i = 0; i < shader_list->get_item_count(); i++) {
  90. TextShaderEditor *se = Object::cast_to<TextShaderEditor>(shader_tabs->get_tab_control(i));
  91. if (se) {
  92. if (se->was_compilation_successful()) {
  93. shader_list->set_item_tag_icon(i, Ref<Texture2D>());
  94. } else {
  95. shader_list->set_item_tag_icon(i, shader_list->get_editor_theme_icon(SNAME("Error")));
  96. }
  97. }
  98. }
  99. }
  100. void ShaderEditorPlugin::_move_shader_tab(int p_from, int p_to) {
  101. if (p_from == p_to) {
  102. return;
  103. }
  104. EditedShader es = edited_shaders[p_from];
  105. edited_shaders.remove_at(p_from);
  106. edited_shaders.insert(p_to, es);
  107. shader_tabs->move_child(shader_tabs->get_tab_control(p_from), p_to);
  108. _update_shader_list();
  109. }
  110. void ShaderEditorPlugin::edit(Object *p_object) {
  111. if (!p_object) {
  112. return;
  113. }
  114. EditedShader es;
  115. ShaderInclude *si = Object::cast_to<ShaderInclude>(p_object);
  116. if (si != nullptr) {
  117. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  118. if (edited_shaders[i].shader_inc.ptr() == si) {
  119. shader_tabs->set_current_tab(i);
  120. shader_list->select(i);
  121. return;
  122. }
  123. }
  124. es.shader_inc = Ref<ShaderInclude>(si);
  125. es.shader_editor = memnew(TextShaderEditor);
  126. es.shader_editor->edit(si);
  127. shader_tabs->add_child(es.shader_editor);
  128. es.shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
  129. } else {
  130. Shader *s = Object::cast_to<Shader>(p_object);
  131. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  132. if (edited_shaders[i].shader.ptr() == s) {
  133. shader_tabs->set_current_tab(i);
  134. shader_list->select(i);
  135. return;
  136. }
  137. }
  138. es.shader = Ref<Shader>(s);
  139. Ref<VisualShader> vs = es.shader;
  140. if (vs.is_valid()) {
  141. es.visual_shader_editor = memnew(VisualShaderEditor);
  142. shader_tabs->add_child(es.visual_shader_editor);
  143. es.visual_shader_editor->edit(vs.ptr());
  144. } else {
  145. es.shader_editor = memnew(TextShaderEditor);
  146. shader_tabs->add_child(es.shader_editor);
  147. es.shader_editor->edit(s);
  148. es.shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
  149. }
  150. }
  151. shader_tabs->set_current_tab(shader_tabs->get_tab_count() - 1);
  152. edited_shaders.push_back(es);
  153. _update_shader_list();
  154. }
  155. bool ShaderEditorPlugin::handles(Object *p_object) const {
  156. return Object::cast_to<Shader>(p_object) != nullptr || Object::cast_to<ShaderInclude>(p_object) != nullptr;
  157. }
  158. void ShaderEditorPlugin::make_visible(bool p_visible) {
  159. if (p_visible) {
  160. EditorNode::get_singleton()->make_bottom_panel_item_visible(window_wrapper);
  161. }
  162. }
  163. void ShaderEditorPlugin::selected_notify() {
  164. }
  165. TextShaderEditor *ShaderEditorPlugin::get_shader_editor(const Ref<Shader> &p_for_shader) {
  166. for (EditedShader &edited_shader : edited_shaders) {
  167. if (edited_shader.shader == p_for_shader) {
  168. return edited_shader.shader_editor;
  169. }
  170. }
  171. return nullptr;
  172. }
  173. VisualShaderEditor *ShaderEditorPlugin::get_visual_shader_editor(const Ref<Shader> &p_for_shader) {
  174. for (EditedShader &edited_shader : edited_shaders) {
  175. if (edited_shader.shader == p_for_shader) {
  176. return edited_shader.visual_shader_editor;
  177. }
  178. }
  179. return nullptr;
  180. }
  181. void ShaderEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  182. if (EDITOR_GET("interface/multi_window/restore_windows_on_load") && window_wrapper->is_window_available() && p_layout->has_section_key("ShaderEditor", "window_rect")) {
  183. window_wrapper->restore_window_from_saved_position(
  184. p_layout->get_value("ShaderEditor", "window_rect", Rect2i()),
  185. p_layout->get_value("ShaderEditor", "window_screen", -1),
  186. p_layout->get_value("ShaderEditor", "window_screen_rect", Rect2i()));
  187. } else {
  188. window_wrapper->set_window_enabled(false);
  189. }
  190. if (!bool(EDITOR_GET("editors/shader_editor/behavior/files/restore_shaders_on_load"))) {
  191. return;
  192. }
  193. if (!p_layout->has_section("ShaderEditor")) {
  194. return;
  195. }
  196. if (!p_layout->has_section_key("ShaderEditor", "open_shaders") ||
  197. !p_layout->has_section_key("ShaderEditor", "selected_shader")) {
  198. return;
  199. }
  200. Array shaders = p_layout->get_value("ShaderEditor", "open_shaders");
  201. int selected_shader_idx = 0;
  202. String selected_shader = p_layout->get_value("ShaderEditor", "selected_shader");
  203. for (int i = 0; i < shaders.size(); i++) {
  204. String path = shaders[i];
  205. Ref<Resource> res = ResourceLoader::load(path);
  206. if (res.is_valid()) {
  207. edit(res.ptr());
  208. }
  209. if (selected_shader == path) {
  210. selected_shader_idx = i;
  211. }
  212. }
  213. if (p_layout->has_section_key("ShaderEditor", "split_offset")) {
  214. main_split->set_split_offset(p_layout->get_value("ShaderEditor", "split_offset"));
  215. }
  216. _update_shader_list();
  217. _shader_selected(selected_shader_idx);
  218. }
  219. void ShaderEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
  220. if (window_wrapper->get_window_enabled()) {
  221. p_layout->set_value("ShaderEditor", "window_rect", window_wrapper->get_window_rect());
  222. int screen = window_wrapper->get_window_screen();
  223. p_layout->set_value("ShaderEditor", "window_screen", screen);
  224. p_layout->set_value("ShaderEditor", "window_screen_rect", DisplayServer::get_singleton()->screen_get_usable_rect(screen));
  225. } else {
  226. if (p_layout->has_section_key("ShaderEditor", "window_rect")) {
  227. p_layout->erase_section_key("ShaderEditor", "window_rect");
  228. }
  229. if (p_layout->has_section_key("ShaderEditor", "window_screen")) {
  230. p_layout->erase_section_key("ShaderEditor", "window_screen");
  231. }
  232. if (p_layout->has_section_key("ShaderEditor", "window_screen_rect")) {
  233. p_layout->erase_section_key("ShaderEditor", "window_screen_rect");
  234. }
  235. }
  236. Array shaders;
  237. String selected_shader;
  238. for (int i = 0; i < shader_tabs->get_tab_count(); i++) {
  239. EditedShader edited_shader = edited_shaders[i];
  240. if (edited_shader.shader_editor || edited_shader.visual_shader_editor) {
  241. String shader_path;
  242. if (edited_shader.shader.is_valid()) {
  243. shader_path = edited_shader.shader->get_path();
  244. } else {
  245. DEV_ASSERT(edited_shader.shader_inc.is_valid());
  246. shader_path = edited_shader.shader_inc->get_path();
  247. }
  248. shaders.push_back(shader_path);
  249. TextShaderEditor *shader_editor = Object::cast_to<TextShaderEditor>(shader_tabs->get_current_tab_control());
  250. VisualShaderEditor *visual_shader_editor = Object::cast_to<VisualShaderEditor>(shader_tabs->get_current_tab_control());
  251. if ((shader_editor && edited_shader.shader_editor == shader_editor) || (visual_shader_editor && edited_shader.visual_shader_editor == visual_shader_editor)) {
  252. selected_shader = shader_path;
  253. }
  254. }
  255. }
  256. p_layout->set_value("ShaderEditor", "open_shaders", shaders);
  257. p_layout->set_value("ShaderEditor", "split_offset", main_split->get_split_offset());
  258. p_layout->set_value("ShaderEditor", "selected_shader", selected_shader);
  259. }
  260. String ShaderEditorPlugin::get_unsaved_status(const String &p_for_scene) const {
  261. if (!p_for_scene.is_empty()) {
  262. // TODO: handle built-in shaders.
  263. return String();
  264. }
  265. // TODO: This should also include visual shaders and shader includes, but save_external_data() doesn't seem to save them...
  266. PackedStringArray unsaved_shaders;
  267. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  268. if (edited_shaders[i].shader_editor) {
  269. if (edited_shaders[i].shader_editor->is_unsaved()) {
  270. if (unsaved_shaders.is_empty()) {
  271. unsaved_shaders.append(TTR("Save changes to the following shaders(s) before quitting?"));
  272. }
  273. unsaved_shaders.append(edited_shaders[i].shader_editor->get_name());
  274. }
  275. }
  276. }
  277. return String("\n").join(unsaved_shaders);
  278. }
  279. void ShaderEditorPlugin::save_external_data() {
  280. for (EditedShader &edited_shader : edited_shaders) {
  281. if (edited_shader.shader_editor) {
  282. edited_shader.shader_editor->save_external_data();
  283. }
  284. }
  285. _update_shader_list();
  286. }
  287. void ShaderEditorPlugin::apply_changes() {
  288. for (EditedShader &edited_shader : edited_shaders) {
  289. if (edited_shader.shader_editor) {
  290. edited_shader.shader_editor->apply_shaders();
  291. }
  292. }
  293. }
  294. void ShaderEditorPlugin::_shader_selected(int p_index) {
  295. if (p_index >= (int)edited_shaders.size()) {
  296. return;
  297. }
  298. if (edited_shaders[p_index].shader_editor) {
  299. edited_shaders[p_index].shader_editor->validate_script();
  300. }
  301. shader_tabs->set_current_tab(p_index);
  302. shader_list->select(p_index);
  303. }
  304. void ShaderEditorPlugin::_shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) {
  305. if (p_mouse_button_index == MouseButton::MIDDLE) {
  306. _close_shader(p_item);
  307. }
  308. }
  309. void ShaderEditorPlugin::_close_shader(int p_index) {
  310. ERR_FAIL_INDEX(p_index, shader_tabs->get_tab_count());
  311. Control *c = shader_tabs->get_tab_control(p_index);
  312. memdelete(c);
  313. edited_shaders.remove_at(p_index);
  314. _update_shader_list();
  315. EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.
  316. }
  317. void ShaderEditorPlugin::_close_builtin_shaders_from_scene(const String &p_scene) {
  318. for (uint32_t i = 0; i < edited_shaders.size();) {
  319. Ref<Shader> &shader = edited_shaders[i].shader;
  320. if (shader.is_valid()) {
  321. if (shader->is_built_in() && shader->get_path().begins_with(p_scene)) {
  322. _close_shader(i);
  323. continue;
  324. }
  325. }
  326. Ref<ShaderInclude> &include = edited_shaders[i].shader_inc;
  327. if (include.is_valid()) {
  328. if (include->is_built_in() && include->get_path().begins_with(p_scene)) {
  329. _close_shader(i);
  330. continue;
  331. }
  332. }
  333. i++;
  334. }
  335. }
  336. void ShaderEditorPlugin::_resource_saved(Object *obj) {
  337. // May have been renamed on save.
  338. for (EditedShader &edited_shader : edited_shaders) {
  339. if (edited_shader.shader.ptr() == obj) {
  340. _update_shader_list();
  341. return;
  342. }
  343. }
  344. }
  345. void ShaderEditorPlugin::_menu_item_pressed(int p_index) {
  346. switch (p_index) {
  347. case FILE_NEW: {
  348. String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
  349. shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 0);
  350. shader_create_dialog->popup_centered();
  351. } break;
  352. case FILE_NEW_INCLUDE: {
  353. String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
  354. shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 2);
  355. shader_create_dialog->popup_centered();
  356. } break;
  357. case FILE_OPEN: {
  358. InspectorDock::get_singleton()->open_resource("Shader");
  359. } break;
  360. case FILE_OPEN_INCLUDE: {
  361. InspectorDock::get_singleton()->open_resource("ShaderInclude");
  362. } break;
  363. case FILE_SAVE: {
  364. int index = shader_tabs->get_current_tab();
  365. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  366. TextShaderEditor *editor = edited_shaders[index].shader_editor;
  367. if (editor && editor->get_trim_trailing_whitespace_on_save()) {
  368. editor->trim_trailing_whitespace();
  369. }
  370. if (edited_shaders[index].shader.is_valid()) {
  371. EditorNode::get_singleton()->save_resource(edited_shaders[index].shader);
  372. } else {
  373. EditorNode::get_singleton()->save_resource(edited_shaders[index].shader_inc);
  374. }
  375. if (editor) {
  376. editor->tag_saved_version();
  377. }
  378. } break;
  379. case FILE_SAVE_AS: {
  380. int index = shader_tabs->get_current_tab();
  381. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  382. TextShaderEditor *editor = edited_shaders[index].shader_editor;
  383. if (editor && editor->get_trim_trailing_whitespace_on_save()) {
  384. editor->trim_trailing_whitespace();
  385. }
  386. String path;
  387. if (edited_shaders[index].shader.is_valid()) {
  388. path = edited_shaders[index].shader->get_path();
  389. if (!path.is_resource_file()) {
  390. path = "";
  391. }
  392. EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader, path);
  393. } else {
  394. path = edited_shaders[index].shader_inc->get_path();
  395. if (!path.is_resource_file()) {
  396. path = "";
  397. }
  398. EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader_inc, path);
  399. }
  400. if (editor) {
  401. editor->tag_saved_version();
  402. }
  403. } break;
  404. case FILE_INSPECT: {
  405. int index = shader_tabs->get_current_tab();
  406. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  407. if (edited_shaders[index].shader.is_valid()) {
  408. EditorNode::get_singleton()->push_item(edited_shaders[index].shader.ptr());
  409. } else {
  410. EditorNode::get_singleton()->push_item(edited_shaders[index].shader_inc.ptr());
  411. }
  412. } break;
  413. case FILE_CLOSE: {
  414. _close_shader(shader_tabs->get_current_tab());
  415. } break;
  416. }
  417. }
  418. void ShaderEditorPlugin::_shader_created(Ref<Shader> p_shader) {
  419. EditorNode::get_singleton()->push_item(p_shader.ptr());
  420. }
  421. void ShaderEditorPlugin::_shader_include_created(Ref<ShaderInclude> p_shader_inc) {
  422. EditorNode::get_singleton()->push_item(p_shader_inc.ptr());
  423. }
  424. Variant ShaderEditorPlugin::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  425. if (shader_list->get_item_count() == 0) {
  426. return Variant();
  427. }
  428. int idx = shader_list->get_item_at_position(p_point);
  429. if (idx < 0) {
  430. return Variant();
  431. }
  432. HBoxContainer *drag_preview = memnew(HBoxContainer);
  433. String preview_name = shader_list->get_item_text(idx);
  434. Ref<Texture2D> preview_icon = shader_list->get_item_icon(idx);
  435. if (!preview_icon.is_null()) {
  436. TextureRect *tf = memnew(TextureRect);
  437. tf->set_texture(preview_icon);
  438. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  439. drag_preview->add_child(tf);
  440. }
  441. Label *label = memnew(Label(preview_name));
  442. drag_preview->add_child(label);
  443. main_split->set_drag_preview(drag_preview);
  444. Dictionary drag_data;
  445. drag_data["type"] = "shader_list_element";
  446. drag_data["shader_list_element"] = idx;
  447. return drag_data;
  448. }
  449. bool ShaderEditorPlugin::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  450. Dictionary d = p_data;
  451. if (!d.has("type")) {
  452. return false;
  453. }
  454. if (String(d["type"]) == "shader_list_element") {
  455. return true;
  456. }
  457. if (String(d["type"]) == "files") {
  458. Vector<String> files = d["files"];
  459. if (files.size() == 0) {
  460. return false;
  461. }
  462. for (int i = 0; i < files.size(); i++) {
  463. String file = files[i];
  464. if (ResourceLoader::exists(file, "Shader")) {
  465. Ref<Shader> shader = ResourceLoader::load(file);
  466. if (shader.is_valid()) {
  467. return true;
  468. }
  469. }
  470. if (ResourceLoader::exists(file, "ShaderInclude")) {
  471. Ref<ShaderInclude> sinclude = ResourceLoader::load(file);
  472. if (sinclude.is_valid()) {
  473. return true;
  474. }
  475. }
  476. }
  477. return false;
  478. }
  479. return false;
  480. }
  481. void ShaderEditorPlugin::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  482. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  483. return;
  484. }
  485. Dictionary d = p_data;
  486. if (!d.has("type")) {
  487. return;
  488. }
  489. if (String(d["type"]) == "shader_list_element") {
  490. int idx = d["shader_list_element"];
  491. int new_idx = shader_list->get_item_at_position(p_point);
  492. _move_shader_tab(idx, new_idx);
  493. return;
  494. }
  495. if (String(d["type"]) == "files") {
  496. Vector<String> files = d["files"];
  497. for (int i = 0; i < files.size(); i++) {
  498. String file = files[i];
  499. Ref<Resource> res;
  500. if (ResourceLoader::exists(file, "Shader") || ResourceLoader::exists(file, "ShaderInclude")) {
  501. res = ResourceLoader::load(file);
  502. }
  503. if (res.is_valid()) {
  504. edit(res.ptr());
  505. }
  506. }
  507. }
  508. }
  509. void ShaderEditorPlugin::_window_changed(bool p_visible) {
  510. make_floating->set_visible(!p_visible);
  511. }
  512. void ShaderEditorPlugin::_file_removed(const String &p_removed_file) {
  513. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  514. if (edited_shaders[i].path == p_removed_file) {
  515. _close_shader(i);
  516. break;
  517. }
  518. }
  519. }
  520. void ShaderEditorPlugin::_notification(int p_what) {
  521. switch (p_what) {
  522. case NOTIFICATION_READY: {
  523. EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ShaderEditorPlugin::_close_builtin_shaders_from_scene));
  524. FileSystemDock::get_singleton()->connect("file_removed", callable_mp(this, &ShaderEditorPlugin::_file_removed));
  525. } break;
  526. }
  527. }
  528. ShaderEditorPlugin::ShaderEditorPlugin() {
  529. window_wrapper = memnew(WindowWrapper);
  530. window_wrapper->set_window_title(vformat(TTR("%s - Godot Engine"), TTR("Shader Editor")));
  531. window_wrapper->set_margins_enabled(true);
  532. main_split = memnew(HSplitContainer);
  533. Ref<Shortcut> make_floating_shortcut = ED_SHORTCUT_AND_COMMAND("shader_editor/make_floating", TTR("Make Floating"));
  534. window_wrapper->set_wrapped_control(main_split, make_floating_shortcut);
  535. VBoxContainer *vb = memnew(VBoxContainer);
  536. HBoxContainer *menu_hb = memnew(HBoxContainer);
  537. vb->add_child(menu_hb);
  538. file_menu = memnew(MenuButton);
  539. file_menu->set_text(TTR("File"));
  540. file_menu->set_shortcut_context(main_split);
  541. file_menu->get_popup()->add_item(TTR("New Shader"), FILE_NEW);
  542. file_menu->get_popup()->add_item(TTR("New Shader Include"), FILE_NEW_INCLUDE);
  543. file_menu->get_popup()->add_separator();
  544. file_menu->get_popup()->add_item(TTR("Load Shader File"), FILE_OPEN);
  545. file_menu->get_popup()->add_item(TTR("Load Shader Include File"), FILE_OPEN_INCLUDE);
  546. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("shader_editor/save", TTR("Save File"), KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::S), FILE_SAVE);
  547. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("shader_editor/save_as", TTR("Save File As")), FILE_SAVE_AS);
  548. file_menu->get_popup()->add_separator();
  549. file_menu->get_popup()->add_item(TTR("Open File in Inspector"), FILE_INSPECT);
  550. file_menu->get_popup()->add_separator();
  551. file_menu->get_popup()->add_item(TTR("Close File"), FILE_CLOSE);
  552. file_menu->get_popup()->connect("id_pressed", callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
  553. menu_hb->add_child(file_menu);
  554. for (int i = FILE_SAVE; i < FILE_MAX; i++) {
  555. file_menu->get_popup()->set_item_disabled(file_menu->get_popup()->get_item_index(i), true);
  556. }
  557. if (window_wrapper->is_window_available()) {
  558. Control *padding = memnew(Control);
  559. padding->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  560. menu_hb->add_child(padding);
  561. make_floating = memnew(ScreenSelect);
  562. make_floating->set_flat(true);
  563. make_floating->set_tooltip_text(TTR("Make the shader editor floating."));
  564. make_floating->connect("request_open_in_screen", callable_mp(window_wrapper, &WindowWrapper::enable_window_on_screen).bind(true));
  565. menu_hb->add_child(make_floating);
  566. window_wrapper->connect("window_visibility_changed", callable_mp(this, &ShaderEditorPlugin::_window_changed));
  567. }
  568. shader_list = memnew(ItemList);
  569. shader_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  570. vb->add_child(shader_list);
  571. shader_list->connect("item_selected", callable_mp(this, &ShaderEditorPlugin::_shader_selected));
  572. shader_list->connect("item_clicked", callable_mp(this, &ShaderEditorPlugin::_shader_list_clicked));
  573. SET_DRAG_FORWARDING_GCD(shader_list, ShaderEditorPlugin);
  574. main_split->add_child(vb);
  575. vb->set_custom_minimum_size(Size2(200, 300) * EDSCALE);
  576. shader_tabs = memnew(TabContainer);
  577. shader_tabs->set_tabs_visible(false);
  578. shader_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  579. main_split->add_child(shader_tabs);
  580. Ref<StyleBoxEmpty> empty;
  581. empty.instantiate();
  582. shader_tabs->add_theme_style_override("panel", empty);
  583. button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Shader Editor"), window_wrapper);
  584. // Defer connect because Editor class is not in the binding system yet.
  585. EditorNode::get_singleton()->call_deferred("connect", "resource_saved", callable_mp(this, &ShaderEditorPlugin::_resource_saved), CONNECT_DEFERRED);
  586. shader_create_dialog = memnew(ShaderCreateDialog);
  587. vb->add_child(shader_create_dialog);
  588. shader_create_dialog->connect("shader_created", callable_mp(this, &ShaderEditorPlugin::_shader_created));
  589. shader_create_dialog->connect("shader_include_created", callable_mp(this, &ShaderEditorPlugin::_shader_include_created));
  590. }
  591. ShaderEditorPlugin::~ShaderEditorPlugin() {
  592. }