shader_editor_plugin.cpp 24 KB

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