shader_editor_plugin.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. EditedShader es;
  107. ShaderInclude *si = Object::cast_to<ShaderInclude>(p_object);
  108. if (si != nullptr) {
  109. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  110. if (edited_shaders[i].shader_inc.ptr() == si) {
  111. shader_tabs->set_current_tab(i);
  112. shader_list->select(i);
  113. return;
  114. }
  115. }
  116. es.shader_inc = Ref<ShaderInclude>(si);
  117. es.shader_editor = memnew(TextShaderEditor);
  118. es.shader_editor->edit(si);
  119. shader_tabs->add_child(es.shader_editor);
  120. es.shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
  121. } else {
  122. Shader *s = Object::cast_to<Shader>(p_object);
  123. for (uint32_t i = 0; i < edited_shaders.size(); i++) {
  124. if (edited_shaders[i].shader.ptr() == s) {
  125. shader_tabs->set_current_tab(i);
  126. shader_list->select(i);
  127. return;
  128. }
  129. }
  130. es.shader = Ref<Shader>(s);
  131. Ref<VisualShader> vs = es.shader;
  132. if (vs.is_valid()) {
  133. es.visual_shader_editor = memnew(VisualShaderEditor);
  134. shader_tabs->add_child(es.visual_shader_editor);
  135. es.visual_shader_editor->edit(vs.ptr());
  136. } else {
  137. es.shader_editor = memnew(TextShaderEditor);
  138. shader_tabs->add_child(es.shader_editor);
  139. es.shader_editor->edit(s);
  140. es.shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
  141. }
  142. }
  143. shader_tabs->set_current_tab(shader_tabs->get_tab_count() - 1);
  144. edited_shaders.push_back(es);
  145. _update_shader_list();
  146. }
  147. bool ShaderEditorPlugin::handles(Object *p_object) const {
  148. return Object::cast_to<Shader>(p_object) != nullptr || Object::cast_to<ShaderInclude>(p_object) != nullptr;
  149. }
  150. void ShaderEditorPlugin::make_visible(bool p_visible) {
  151. if (p_visible) {
  152. EditorNode::get_singleton()->make_bottom_panel_item_visible(main_split);
  153. }
  154. }
  155. void ShaderEditorPlugin::selected_notify() {
  156. }
  157. TextShaderEditor *ShaderEditorPlugin::get_shader_editor(const Ref<Shader> &p_for_shader) {
  158. for (EditedShader &edited_shader : edited_shaders) {
  159. if (edited_shader.shader == p_for_shader) {
  160. return edited_shader.shader_editor;
  161. }
  162. }
  163. return nullptr;
  164. }
  165. VisualShaderEditor *ShaderEditorPlugin::get_visual_shader_editor(const Ref<Shader> &p_for_shader) {
  166. for (EditedShader &edited_shader : edited_shaders) {
  167. if (edited_shader.shader == p_for_shader) {
  168. return edited_shader.visual_shader_editor;
  169. }
  170. }
  171. return nullptr;
  172. }
  173. void ShaderEditorPlugin::save_external_data() {
  174. for (EditedShader &edited_shader : edited_shaders) {
  175. if (edited_shader.shader_editor) {
  176. edited_shader.shader_editor->save_external_data();
  177. }
  178. }
  179. _update_shader_list();
  180. }
  181. void ShaderEditorPlugin::apply_changes() {
  182. for (EditedShader &edited_shader : edited_shaders) {
  183. if (edited_shader.shader_editor) {
  184. edited_shader.shader_editor->apply_shaders();
  185. }
  186. }
  187. }
  188. void ShaderEditorPlugin::_shader_selected(int p_index) {
  189. if (edited_shaders[p_index].shader_editor) {
  190. edited_shaders[p_index].shader_editor->validate_script();
  191. }
  192. shader_tabs->set_current_tab(p_index);
  193. shader_list->select(p_index);
  194. }
  195. void ShaderEditorPlugin::_shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) {
  196. if (p_mouse_button_index == MouseButton::MIDDLE) {
  197. _close_shader(p_item);
  198. }
  199. }
  200. void ShaderEditorPlugin::_close_shader(int p_index) {
  201. ERR_FAIL_INDEX(p_index, shader_tabs->get_tab_count());
  202. Control *c = shader_tabs->get_tab_control(p_index);
  203. memdelete(c);
  204. edited_shaders.remove_at(p_index);
  205. _update_shader_list();
  206. EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.
  207. }
  208. void ShaderEditorPlugin::_resource_saved(Object *obj) {
  209. // May have been renamed on save.
  210. for (EditedShader &edited_shader : edited_shaders) {
  211. if (edited_shader.shader.ptr() == obj) {
  212. _update_shader_list();
  213. return;
  214. }
  215. }
  216. }
  217. void ShaderEditorPlugin::_menu_item_pressed(int p_index) {
  218. switch (p_index) {
  219. case FILE_NEW: {
  220. String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
  221. shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 0);
  222. shader_create_dialog->popup_centered();
  223. } break;
  224. case FILE_NEW_INCLUDE: {
  225. String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
  226. shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 2);
  227. shader_create_dialog->popup_centered();
  228. } break;
  229. case FILE_OPEN: {
  230. InspectorDock::get_singleton()->open_resource("Shader");
  231. } break;
  232. case FILE_OPEN_INCLUDE: {
  233. InspectorDock::get_singleton()->open_resource("ShaderInclude");
  234. } break;
  235. case FILE_SAVE: {
  236. int index = shader_tabs->get_current_tab();
  237. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  238. if (edited_shaders[index].shader.is_valid()) {
  239. EditorNode::get_singleton()->save_resource(edited_shaders[index].shader);
  240. } else {
  241. EditorNode::get_singleton()->save_resource(edited_shaders[index].shader_inc);
  242. }
  243. if (edited_shaders[index].shader_editor) {
  244. edited_shaders[index].shader_editor->tag_saved_version();
  245. }
  246. } break;
  247. case FILE_SAVE_AS: {
  248. int index = shader_tabs->get_current_tab();
  249. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  250. String path;
  251. if (edited_shaders[index].shader.is_valid()) {
  252. path = edited_shaders[index].shader->get_path();
  253. if (!path.is_resource_file()) {
  254. path = "";
  255. }
  256. EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader, path);
  257. } else {
  258. path = edited_shaders[index].shader_inc->get_path();
  259. if (!path.is_resource_file()) {
  260. path = "";
  261. }
  262. EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader_inc, path);
  263. }
  264. if (edited_shaders[index].shader_editor) {
  265. edited_shaders[index].shader_editor->tag_saved_version();
  266. }
  267. } break;
  268. case FILE_INSPECT: {
  269. int index = shader_tabs->get_current_tab();
  270. ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
  271. if (edited_shaders[index].shader.is_valid()) {
  272. EditorNode::get_singleton()->push_item(edited_shaders[index].shader.ptr());
  273. } else {
  274. EditorNode::get_singleton()->push_item(edited_shaders[index].shader_inc.ptr());
  275. }
  276. } break;
  277. case FILE_CLOSE: {
  278. _close_shader(shader_tabs->get_current_tab());
  279. } break;
  280. }
  281. }
  282. void ShaderEditorPlugin::_shader_created(Ref<Shader> p_shader) {
  283. EditorNode::get_singleton()->push_item(p_shader.ptr());
  284. }
  285. void ShaderEditorPlugin::_shader_include_created(Ref<ShaderInclude> p_shader_inc) {
  286. EditorNode::get_singleton()->push_item(p_shader_inc.ptr());
  287. }
  288. Variant ShaderEditorPlugin::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  289. if (shader_list->get_item_count() == 0) {
  290. return Variant();
  291. }
  292. int idx = shader_list->get_item_at_position(p_point);
  293. if (idx < 0) {
  294. return Variant();
  295. }
  296. HBoxContainer *drag_preview = memnew(HBoxContainer);
  297. String preview_name = shader_list->get_item_text(idx);
  298. Ref<Texture2D> preview_icon = shader_list->get_item_icon(idx);
  299. if (!preview_icon.is_null()) {
  300. TextureRect *tf = memnew(TextureRect);
  301. tf->set_texture(preview_icon);
  302. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  303. drag_preview->add_child(tf);
  304. }
  305. Label *label = memnew(Label(preview_name));
  306. drag_preview->add_child(label);
  307. main_split->set_drag_preview(drag_preview);
  308. Dictionary drag_data;
  309. drag_data["type"] = "shader_list_element";
  310. drag_data["shader_list_element"] = idx;
  311. return drag_data;
  312. }
  313. bool ShaderEditorPlugin::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  314. Dictionary d = p_data;
  315. if (!d.has("type")) {
  316. return false;
  317. }
  318. if (String(d["type"]) == "shader_list_element") {
  319. return true;
  320. }
  321. if (String(d["type"]) == "files") {
  322. Vector<String> files = d["files"];
  323. if (files.size() == 0) {
  324. return false;
  325. }
  326. for (int i = 0; i < files.size(); i++) {
  327. String file = files[i];
  328. if (ResourceLoader::exists(file, "Shader")) {
  329. Ref<Shader> shader = ResourceLoader::load(file);
  330. if (shader.is_valid()) {
  331. return true;
  332. }
  333. }
  334. }
  335. return false;
  336. }
  337. return false;
  338. }
  339. void ShaderEditorPlugin::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  340. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  341. return;
  342. }
  343. Dictionary d = p_data;
  344. if (!d.has("type")) {
  345. return;
  346. }
  347. if (String(d["type"]) == "shader_list_element") {
  348. int idx = d["shader_list_element"];
  349. int new_idx = shader_list->get_item_at_position(p_point);
  350. _move_shader_tab(idx, new_idx);
  351. return;
  352. }
  353. if (String(d["type"]) == "files") {
  354. Vector<String> files = d["files"];
  355. for (int i = 0; i < files.size(); i++) {
  356. String file = files[i];
  357. if (!ResourceLoader::exists(file, "Shader")) {
  358. continue;
  359. }
  360. Ref<Resource> res = ResourceLoader::load(file);
  361. if (res.is_valid()) {
  362. edit(res.ptr());
  363. }
  364. }
  365. }
  366. }
  367. ShaderEditorPlugin::ShaderEditorPlugin() {
  368. main_split = memnew(HSplitContainer);
  369. VBoxContainer *vb = memnew(VBoxContainer);
  370. HBoxContainer *file_hb = memnew(HBoxContainer);
  371. vb->add_child(file_hb);
  372. file_menu = memnew(MenuButton);
  373. file_menu->set_text(TTR("File"));
  374. file_menu->get_popup()->add_item(TTR("New Shader"), FILE_NEW);
  375. file_menu->get_popup()->add_item(TTR("New Shader Include"), FILE_NEW_INCLUDE);
  376. file_menu->get_popup()->add_separator();
  377. file_menu->get_popup()->add_item(TTR("Load Shader File"), FILE_OPEN);
  378. file_menu->get_popup()->add_item(TTR("Load Shader Include File"), FILE_OPEN_INCLUDE);
  379. file_menu->get_popup()->add_item(TTR("Save File"), FILE_SAVE);
  380. file_menu->get_popup()->add_item(TTR("Save File As"), FILE_SAVE_AS);
  381. file_menu->get_popup()->add_separator();
  382. file_menu->get_popup()->add_item(TTR("Open File in Inspector"), FILE_INSPECT);
  383. file_menu->get_popup()->add_separator();
  384. file_menu->get_popup()->add_item(TTR("Close File"), FILE_CLOSE);
  385. file_menu->get_popup()->connect("id_pressed", callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
  386. file_hb->add_child(file_menu);
  387. for (int i = FILE_SAVE; i < FILE_MAX; i++) {
  388. file_menu->get_popup()->set_item_disabled(file_menu->get_popup()->get_item_index(i), true);
  389. }
  390. shader_list = memnew(ItemList);
  391. shader_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  392. vb->add_child(shader_list);
  393. shader_list->connect("item_selected", callable_mp(this, &ShaderEditorPlugin::_shader_selected));
  394. shader_list->connect("item_clicked", callable_mp(this, &ShaderEditorPlugin::_shader_list_clicked));
  395. SET_DRAG_FORWARDING_GCD(shader_list, ShaderEditorPlugin);
  396. main_split->add_child(vb);
  397. vb->set_custom_minimum_size(Size2(200, 300) * EDSCALE);
  398. shader_tabs = memnew(TabContainer);
  399. shader_tabs->set_tabs_visible(false);
  400. shader_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  401. main_split->add_child(shader_tabs);
  402. Ref<StyleBoxEmpty> empty;
  403. empty.instantiate();
  404. shader_tabs->add_theme_style_override("panel", empty);
  405. button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Shader Editor"), main_split);
  406. // Defer connect because Editor class is not in the binding system yet.
  407. EditorNode::get_singleton()->call_deferred("connect", "resource_saved", callable_mp(this, &ShaderEditorPlugin::_resource_saved), CONNECT_DEFERRED);
  408. shader_create_dialog = memnew(ShaderCreateDialog);
  409. vb->add_child(shader_create_dialog);
  410. shader_create_dialog->connect("shader_created", callable_mp(this, &ShaderEditorPlugin::_shader_created));
  411. shader_create_dialog->connect("shader_include_created", callable_mp(this, &ShaderEditorPlugin::_shader_include_created));
  412. }
  413. ShaderEditorPlugin::~ShaderEditorPlugin() {
  414. }