shader_editor_plugin.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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_node.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "editor/filesystem_dock.h"
  35. #include "editor/inspector_dock.h"
  36. #include "editor/plugins/text_shader_editor.h"
  37. #include "editor/plugins/visual_shader_editor_plugin.h"
  38. #include "editor/shader_create_dialog.h"
  39. #include "scene/gui/item_list.h"
  40. #include "scene/gui/texture_rect.h"
  41. void ShaderEditorPlugin::_update_shader_list() {
  42. shader_list->clear();
  43. for (EditedShader &edited_shader : edited_shaders) {
  44. Ref<Resource> shader = edited_shader.shader;
  45. if (shader.is_null()) {
  46. shader = edited_shader.shader_inc;
  47. }
  48. String path = shader->get_path();
  49. String text = path.get_file();
  50. if (text.is_empty()) {
  51. // This appears for newly created built-in shaders before saving the scene.
  52. text = TTR("[unsaved]");
  53. } else if (shader->is_built_in()) {
  54. const String &shader_name = shader->get_name();
  55. if (!shader_name.is_empty()) {
  56. text = vformat("%s (%s)", shader_name, text.get_slice("::", 0));
  57. }
  58. }
  59. bool unsaved = false;
  60. if (edited_shader.shader_editor) {
  61. unsaved = edited_shader.shader_editor->is_unsaved();
  62. }
  63. // TODO: Handle visual shaders too.
  64. if (unsaved) {
  65. text += "(*)";
  66. }
  67. String _class = shader->get_class();
  68. if (!shader_list->has_theme_icon(_class, SNAME("EditorIcons"))) {
  69. _class = "TextFile";
  70. }
  71. Ref<Texture2D> icon = shader_list->get_theme_icon(_class, SNAME("EditorIcons"));
  72. shader_list->add_item(text, icon);
  73. shader_list->set_item_tooltip(shader_list->get_item_count() - 1, path);
  74. }
  75. if (shader_tabs->get_tab_count()) {
  76. shader_list->select(shader_tabs->get_current_tab());
  77. }
  78. for (int i = FILE_SAVE; i < FILE_MAX; i++) {
  79. file_menu->get_popup()->set_item_disabled(file_menu->get_popup()->get_item_index(i), edited_shaders.is_empty());
  80. }
  81. _update_shader_list_status();
  82. }
  83. void ShaderEditorPlugin::_update_shader_list_status() {
  84. for (int i = 0; i < shader_list->get_item_count(); i++) {
  85. TextShaderEditor *se = Object::cast_to<TextShaderEditor>(shader_tabs->get_tab_control(i));
  86. if (se) {
  87. if (se->was_compilation_successful()) {
  88. shader_list->set_item_tag_icon(i, Ref<Texture2D>());
  89. } else {
  90. shader_list->set_item_tag_icon(i, shader_list->get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  91. }
  92. }
  93. }
  94. }
  95. void ShaderEditorPlugin::_move_shader_tab(int p_from, int p_to) {
  96. if (p_from == p_to) {
  97. return;
  98. }
  99. EditedShader es = edited_shaders[p_from];
  100. edited_shaders.remove_at(p_from);
  101. edited_shaders.insert(p_to, es);
  102. shader_tabs->move_child(shader_tabs->get_tab_control(p_from), p_to);
  103. _update_shader_list();
  104. }
  105. void ShaderEditorPlugin::edit(Object *p_object) {
  106. if (!p_object) {
  107. return;
  108. }
  109. EditedShader es;
  110. ShaderInclude *si = Object::cast_to<ShaderInclude>(p_object);
  111. if (si != nullptr) {
  112. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  113. if (edited_shaders[i].shader_inc.ptr() == si) {
  114. shader_tabs->set_current_tab(i);
  115. shader_list->select(i);
  116. return;
  117. }
  118. }
  119. es.shader_inc = Ref<ShaderInclude>(si);
  120. es.shader_editor = memnew(TextShaderEditor);
  121. es.shader_editor->edit(si);
  122. shader_tabs->add_child(es.shader_editor);
  123. es.shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
  124. } else {
  125. Shader *s = Object::cast_to<Shader>(p_object);
  126. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  127. if (edited_shaders[i].shader.ptr() == s) {
  128. shader_tabs->set_current_tab(i);
  129. shader_list->select(i);
  130. return;
  131. }
  132. }
  133. es.shader = Ref<Shader>(s);
  134. Ref<VisualShader> vs = es.shader;
  135. if (vs.is_valid()) {
  136. es.visual_shader_editor = memnew(VisualShaderEditor);
  137. shader_tabs->add_child(es.visual_shader_editor);
  138. es.visual_shader_editor->edit(vs.ptr());
  139. } else {
  140. es.shader_editor = memnew(TextShaderEditor);
  141. shader_tabs->add_child(es.shader_editor);
  142. es.shader_editor->edit(s);
  143. es.shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
  144. }
  145. }
  146. shader_tabs->set_current_tab(shader_tabs->get_tab_count() - 1);
  147. edited_shaders.push_back(es);
  148. _update_shader_list();
  149. }
  150. bool ShaderEditorPlugin::handles(Object *p_object) const {
  151. return Object::cast_to<Shader>(p_object) != nullptr || Object::cast_to<ShaderInclude>(p_object) != nullptr;
  152. }
  153. void ShaderEditorPlugin::make_visible(bool p_visible) {
  154. if (p_visible) {
  155. EditorNode::get_singleton()->make_bottom_panel_item_visible(main_split);
  156. }
  157. }
  158. void ShaderEditorPlugin::selected_notify() {
  159. }
  160. TextShaderEditor *ShaderEditorPlugin::get_shader_editor(const Ref<Shader> &p_for_shader) {
  161. for (EditedShader &edited_shader : edited_shaders) {
  162. if (edited_shader.shader == p_for_shader) {
  163. return edited_shader.shader_editor;
  164. }
  165. }
  166. return nullptr;
  167. }
  168. VisualShaderEditor *ShaderEditorPlugin::get_visual_shader_editor(const Ref<Shader> &p_for_shader) {
  169. for (EditedShader &edited_shader : edited_shaders) {
  170. if (edited_shader.shader == p_for_shader) {
  171. return edited_shader.visual_shader_editor;
  172. }
  173. }
  174. return nullptr;
  175. }
  176. void ShaderEditorPlugin::save_external_data() {
  177. for (EditedShader &edited_shader : edited_shaders) {
  178. if (edited_shader.shader_editor) {
  179. edited_shader.shader_editor->save_external_data();
  180. }
  181. }
  182. _update_shader_list();
  183. }
  184. void ShaderEditorPlugin::apply_changes() {
  185. for (EditedShader &edited_shader : edited_shaders) {
  186. if (edited_shader.shader_editor) {
  187. edited_shader.shader_editor->apply_shaders();
  188. }
  189. }
  190. }
  191. void ShaderEditorPlugin::_shader_selected(int p_index) {
  192. if (edited_shaders[p_index].shader_editor) {
  193. edited_shaders[p_index].shader_editor->validate_script();
  194. }
  195. shader_tabs->set_current_tab(p_index);
  196. shader_list->select(p_index);
  197. }
  198. void ShaderEditorPlugin::_shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) {
  199. if (p_mouse_button_index == MouseButton::MIDDLE) {
  200. _close_shader(p_item);
  201. }
  202. }
  203. void ShaderEditorPlugin::_close_shader(int p_index) {
  204. ERR_FAIL_INDEX(p_index, shader_tabs->get_tab_count());
  205. Control *c = shader_tabs->get_tab_control(p_index);
  206. memdelete(c);
  207. edited_shaders.remove_at(p_index);
  208. _update_shader_list();
  209. EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.
  210. }
  211. void ShaderEditorPlugin::_close_builtin_shaders_from_scene(const String &p_scene) {
  212. for (uint32_t i = 0; i < edited_shaders.size();) {
  213. Ref<Shader> &shader = edited_shaders[i].shader;
  214. if (shader.is_valid()) {
  215. if (shader->is_built_in() && shader->get_path().begins_with(p_scene)) {
  216. _close_shader(i);
  217. continue;
  218. }
  219. }
  220. Ref<ShaderInclude> &include = edited_shaders[i].shader_inc;
  221. if (include.is_valid()) {
  222. if (include->is_built_in() && include->get_path().begins_with(p_scene)) {
  223. _close_shader(i);
  224. continue;
  225. }
  226. }
  227. i++;
  228. }
  229. }
  230. void ShaderEditorPlugin::_resource_saved(Object *obj) {
  231. // May have been renamed on save.
  232. for (EditedShader &edited_shader : edited_shaders) {
  233. if (edited_shader.shader.ptr() == obj) {
  234. _update_shader_list();
  235. return;
  236. }
  237. }
  238. }
  239. void ShaderEditorPlugin::_menu_item_pressed(int p_index) {
  240. switch (p_index) {
  241. case FILE_NEW: {
  242. String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
  243. shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 0);
  244. shader_create_dialog->popup_centered();
  245. } break;
  246. case FILE_NEW_INCLUDE: {
  247. String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
  248. shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 2);
  249. shader_create_dialog->popup_centered();
  250. } break;
  251. case FILE_OPEN: {
  252. InspectorDock::get_singleton()->open_resource("Shader");
  253. } break;
  254. case FILE_OPEN_INCLUDE: {
  255. InspectorDock::get_singleton()->open_resource("ShaderInclude");
  256. } break;
  257. case FILE_SAVE: {
  258. int index = shader_tabs->get_current_tab();
  259. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  260. if (edited_shaders[index].shader.is_valid()) {
  261. EditorNode::get_singleton()->save_resource(edited_shaders[index].shader);
  262. } else {
  263. EditorNode::get_singleton()->save_resource(edited_shaders[index].shader_inc);
  264. }
  265. if (edited_shaders[index].shader_editor) {
  266. edited_shaders[index].shader_editor->tag_saved_version();
  267. }
  268. } break;
  269. case FILE_SAVE_AS: {
  270. int index = shader_tabs->get_current_tab();
  271. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  272. String path;
  273. if (edited_shaders[index].shader.is_valid()) {
  274. path = edited_shaders[index].shader->get_path();
  275. if (!path.is_resource_file()) {
  276. path = "";
  277. }
  278. EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader, path);
  279. } else {
  280. path = edited_shaders[index].shader_inc->get_path();
  281. if (!path.is_resource_file()) {
  282. path = "";
  283. }
  284. EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader_inc, path);
  285. }
  286. if (edited_shaders[index].shader_editor) {
  287. edited_shaders[index].shader_editor->tag_saved_version();
  288. }
  289. } break;
  290. case FILE_INSPECT: {
  291. int index = shader_tabs->get_current_tab();
  292. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  293. if (edited_shaders[index].shader.is_valid()) {
  294. EditorNode::get_singleton()->push_item(edited_shaders[index].shader.ptr());
  295. } else {
  296. EditorNode::get_singleton()->push_item(edited_shaders[index].shader_inc.ptr());
  297. }
  298. } break;
  299. case FILE_CLOSE: {
  300. _close_shader(shader_tabs->get_current_tab());
  301. } break;
  302. }
  303. }
  304. void ShaderEditorPlugin::_shader_created(Ref<Shader> p_shader) {
  305. EditorNode::get_singleton()->push_item(p_shader.ptr());
  306. }
  307. void ShaderEditorPlugin::_shader_include_created(Ref<ShaderInclude> p_shader_inc) {
  308. EditorNode::get_singleton()->push_item(p_shader_inc.ptr());
  309. }
  310. Variant ShaderEditorPlugin::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  311. if (shader_list->get_item_count() == 0) {
  312. return Variant();
  313. }
  314. int idx = shader_list->get_item_at_position(p_point);
  315. if (idx < 0) {
  316. return Variant();
  317. }
  318. HBoxContainer *drag_preview = memnew(HBoxContainer);
  319. String preview_name = shader_list->get_item_text(idx);
  320. Ref<Texture2D> preview_icon = shader_list->get_item_icon(idx);
  321. if (!preview_icon.is_null()) {
  322. TextureRect *tf = memnew(TextureRect);
  323. tf->set_texture(preview_icon);
  324. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  325. drag_preview->add_child(tf);
  326. }
  327. Label *label = memnew(Label(preview_name));
  328. drag_preview->add_child(label);
  329. main_split->set_drag_preview(drag_preview);
  330. Dictionary drag_data;
  331. drag_data["type"] = "shader_list_element";
  332. drag_data["shader_list_element"] = idx;
  333. return drag_data;
  334. }
  335. bool ShaderEditorPlugin::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  336. Dictionary d = p_data;
  337. if (!d.has("type")) {
  338. return false;
  339. }
  340. if (String(d["type"]) == "shader_list_element") {
  341. return true;
  342. }
  343. if (String(d["type"]) == "files") {
  344. Vector<String> files = d["files"];
  345. if (files.size() == 0) {
  346. return false;
  347. }
  348. for (int i = 0; i < files.size(); i++) {
  349. String file = files[i];
  350. if (ResourceLoader::exists(file, "Shader")) {
  351. Ref<Shader> shader = ResourceLoader::load(file);
  352. if (shader.is_valid()) {
  353. return true;
  354. }
  355. }
  356. if (ResourceLoader::exists(file, "ShaderInclude")) {
  357. Ref<ShaderInclude> sinclude = ResourceLoader::load(file);
  358. if (sinclude.is_valid()) {
  359. return true;
  360. }
  361. }
  362. }
  363. return false;
  364. }
  365. return false;
  366. }
  367. void ShaderEditorPlugin::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  368. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  369. return;
  370. }
  371. Dictionary d = p_data;
  372. if (!d.has("type")) {
  373. return;
  374. }
  375. if (String(d["type"]) == "shader_list_element") {
  376. int idx = d["shader_list_element"];
  377. int new_idx = shader_list->get_item_at_position(p_point);
  378. _move_shader_tab(idx, new_idx);
  379. return;
  380. }
  381. if (String(d["type"]) == "files") {
  382. Vector<String> files = d["files"];
  383. for (int i = 0; i < files.size(); i++) {
  384. String file = files[i];
  385. Ref<Resource> res;
  386. if (ResourceLoader::exists(file, "Shader") || ResourceLoader::exists(file, "ShaderInclude")) {
  387. res = ResourceLoader::load(file);
  388. }
  389. if (res.is_valid()) {
  390. edit(res.ptr());
  391. }
  392. }
  393. }
  394. }
  395. void ShaderEditorPlugin::_notification(int p_what) {
  396. switch (p_what) {
  397. case NOTIFICATION_READY: {
  398. EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ShaderEditorPlugin::_close_builtin_shaders_from_scene));
  399. } break;
  400. }
  401. }
  402. ShaderEditorPlugin::ShaderEditorPlugin() {
  403. main_split = memnew(HSplitContainer);
  404. VBoxContainer *vb = memnew(VBoxContainer);
  405. HBoxContainer *file_hb = memnew(HBoxContainer);
  406. vb->add_child(file_hb);
  407. file_menu = memnew(MenuButton);
  408. file_menu->set_text(TTR("File"));
  409. file_menu->get_popup()->add_item(TTR("New Shader"), FILE_NEW);
  410. file_menu->get_popup()->add_item(TTR("New Shader Include"), FILE_NEW_INCLUDE);
  411. file_menu->get_popup()->add_separator();
  412. file_menu->get_popup()->add_item(TTR("Load Shader File"), FILE_OPEN);
  413. file_menu->get_popup()->add_item(TTR("Load Shader Include File"), FILE_OPEN_INCLUDE);
  414. file_menu->get_popup()->add_item(TTR("Save File"), FILE_SAVE);
  415. file_menu->get_popup()->add_item(TTR("Save File As"), FILE_SAVE_AS);
  416. file_menu->get_popup()->add_separator();
  417. file_menu->get_popup()->add_item(TTR("Open File in Inspector"), FILE_INSPECT);
  418. file_menu->get_popup()->add_separator();
  419. file_menu->get_popup()->add_item(TTR("Close File"), FILE_CLOSE);
  420. file_menu->get_popup()->connect("id_pressed", callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
  421. file_hb->add_child(file_menu);
  422. for (int i = FILE_SAVE; i < FILE_MAX; i++) {
  423. file_menu->get_popup()->set_item_disabled(file_menu->get_popup()->get_item_index(i), true);
  424. }
  425. shader_list = memnew(ItemList);
  426. shader_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  427. vb->add_child(shader_list);
  428. shader_list->connect("item_selected", callable_mp(this, &ShaderEditorPlugin::_shader_selected));
  429. shader_list->connect("item_clicked", callable_mp(this, &ShaderEditorPlugin::_shader_list_clicked));
  430. SET_DRAG_FORWARDING_GCD(shader_list, ShaderEditorPlugin);
  431. main_split->add_child(vb);
  432. vb->set_custom_minimum_size(Size2(200, 300) * EDSCALE);
  433. shader_tabs = memnew(TabContainer);
  434. shader_tabs->set_tabs_visible(false);
  435. shader_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  436. main_split->add_child(shader_tabs);
  437. Ref<StyleBoxEmpty> empty;
  438. empty.instantiate();
  439. shader_tabs->add_theme_style_override("panel", empty);
  440. button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Shader Editor"), main_split);
  441. // Defer connect because Editor class is not in the binding system yet.
  442. EditorNode::get_singleton()->call_deferred("connect", "resource_saved", callable_mp(this, &ShaderEditorPlugin::_resource_saved), CONNECT_DEFERRED);
  443. shader_create_dialog = memnew(ShaderCreateDialog);
  444. vb->add_child(shader_create_dialog);
  445. shader_create_dialog->connect("shader_created", callable_mp(this, &ShaderEditorPlugin::_shader_created));
  446. shader_create_dialog->connect("shader_include_created", callable_mp(this, &ShaderEditorPlugin::_shader_include_created));
  447. }
  448. ShaderEditorPlugin::~ShaderEditorPlugin() {
  449. }