shader_editor_plugin.cpp 22 KB

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