shader_editor_plugin.cpp 30 KB

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