shader_editor_plugin.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*************************************************************************/
  2. /* shader_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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_settings.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/property_editor.h"
  34. #include "io/resource_loader.h"
  35. #include "io/resource_saver.h"
  36. #include "os/keyboard.h"
  37. #include "os/os.h"
  38. #include "scene/resources/shader_graph.h"
  39. #include "spatial_editor_plugin.h"
  40. /*** SETTINGS EDITOR ****/
  41. /*** SCRIPT EDITOR ****/
  42. Ref<Shader> ShaderTextEditor::get_edited_shader() const {
  43. return shader;
  44. }
  45. void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader, ShaderLanguage::ShaderType p_type) {
  46. shader = p_shader;
  47. type = p_type;
  48. _load_theme_settings();
  49. if (p_type == ShaderLanguage::SHADER_MATERIAL_LIGHT || p_type == ShaderLanguage::SHADER_CANVAS_ITEM_LIGHT)
  50. get_text_edit()->set_text(shader->get_light_code());
  51. else if (p_type == ShaderLanguage::SHADER_MATERIAL_VERTEX || p_type == ShaderLanguage::SHADER_CANVAS_ITEM_VERTEX)
  52. get_text_edit()->set_text(shader->get_vertex_code());
  53. else
  54. get_text_edit()->set_text(shader->get_fragment_code());
  55. _line_col_changed();
  56. }
  57. void ShaderTextEditor::_load_theme_settings() {
  58. get_text_edit()->clear_colors();
  59. /* keyword color */
  60. get_text_edit()->set_custom_bg_color(EDITOR_DEF("text_editor/background_color", Color(0, 0, 0, 0)));
  61. get_text_edit()->add_color_override("completion_background_color", EDITOR_DEF("text_editor/completion_background_color", Color(0, 0, 0, 0)));
  62. get_text_edit()->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/completion_selected_color", Color::html("434244")));
  63. get_text_edit()->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/completion_existing_color", Color::html("21dfdfdf")));
  64. get_text_edit()->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/completion_scroll_color", Color::html("ffffff")));
  65. get_text_edit()->add_color_override("completion_font_color", EDITOR_DEF("text_editor/completion_font_color", Color::html("aaaaaa")));
  66. get_text_edit()->add_color_override("font_color", EDITOR_DEF("text_editor/text_color", Color(0, 0, 0)));
  67. get_text_edit()->add_color_override("line_number_color", EDITOR_DEF("text_editor/line_number_color", Color(0, 0, 0)));
  68. get_text_edit()->add_color_override("caret_color", EDITOR_DEF("text_editor/caret_color", Color(0, 0, 0)));
  69. get_text_edit()->add_color_override("caret_background_color", EDITOR_DEF("text_editor/caret_background_color", Color(0, 0, 0)));
  70. get_text_edit()->add_color_override("font_selected_color", EDITOR_DEF("text_editor/text_selected_color", Color(1, 1, 1)));
  71. get_text_edit()->add_color_override("selection_color", EDITOR_DEF("text_editor/selection_color", Color(0.2, 0.2, 1)));
  72. get_text_edit()->add_color_override("brace_mismatch_color", EDITOR_DEF("text_editor/brace_mismatch_color", Color(1, 0.2, 0.2)));
  73. get_text_edit()->add_color_override("current_line_color", EDITOR_DEF("text_editor/current_line_color", Color(0.3, 0.5, 0.8, 0.15)));
  74. get_text_edit()->add_color_override("word_highlighted_color", EDITOR_DEF("text_editor/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15)));
  75. get_text_edit()->add_color_override("number_color", EDITOR_DEF("text_editor/number_color", Color(0.9, 0.6, 0.0, 2)));
  76. get_text_edit()->add_color_override("function_color", EDITOR_DEF("text_editor/function_color", Color(0.4, 0.6, 0.8)));
  77. get_text_edit()->add_color_override("member_variable_color", EDITOR_DEF("text_editor/member_variable_color", Color(0.9, 0.3, 0.3)));
  78. get_text_edit()->add_color_override("mark_color", EDITOR_DEF("text_editor/mark_color", Color(1.0, 0.4, 0.4, 0.4)));
  79. get_text_edit()->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2)));
  80. get_text_edit()->add_color_override("search_result_color", EDITOR_DEF("text_editor/search_result_color", Color(0.05, 0.25, 0.05, 1)));
  81. get_text_edit()->add_color_override("search_result_border_color", EDITOR_DEF("text_editor/search_result_border_color", Color(0.1, 0.45, 0.1, 1)));
  82. Color keyword_color = EDITOR_DEF("text_editor/keyword_color", Color(0.5, 0.0, 0.2));
  83. List<String> keywords;
  84. ShaderLanguage::get_keyword_list(type, &keywords);
  85. for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
  86. get_text_edit()->add_keyword_color(E->get(), keyword_color);
  87. }
  88. //colorize core types
  89. // Color basetype_color= EDITOR_DEF("text_editor/base_type_color",Color(0.3,0.3,0.0));
  90. //colorize comments
  91. Color comment_color = EDITOR_DEF("text_editor/comment_color", Color::hex(0x797e7eff));
  92. get_text_edit()->add_color_region("/*", "*/", comment_color, false);
  93. get_text_edit()->add_color_region("//", "", comment_color, false);
  94. /*//colorize strings
  95. Color string_color = EDITOR_DEF("text_editor/string_color",Color::hex(0x6b6f00ff));
  96. List<String> strings;
  97. shader->get_shader_mode()->get_string_delimiters(&strings);
  98. for (List<String>::Element *E=strings.front();E;E=E->next()) {
  99. String string = E->get();
  100. String beg = string.get_slice(" ",0);
  101. String end = string.get_slice_count(" ")>1?string.get_slice(" ",1):String();
  102. get_text_edit()->add_color_region(beg,end,string_color,end=="");
  103. }*/
  104. //colorize symbols
  105. Color symbol_color = EDITOR_DEF("text_editor/symbol_color", Color::hex(0x005291ff));
  106. get_text_edit()->set_symbol_color(symbol_color);
  107. }
  108. void ShaderTextEditor::_validate_script() {
  109. String errortxt;
  110. int line, col;
  111. String code = get_text_edit()->get_text();
  112. //List<StringName> params;
  113. //shader->get_param_list(&params);
  114. Error err = ShaderLanguage::compile(code, type, NULL, NULL, &errortxt, &line, &col);
  115. if (err != OK) {
  116. String error_text = "error(" + itos(line + 1) + "," + itos(col + 1) + "): " + errortxt;
  117. set_error(error_text);
  118. get_text_edit()->set_line_as_marked(line, true);
  119. } else {
  120. for (int i = 0; i < get_text_edit()->get_line_count(); i++)
  121. get_text_edit()->set_line_as_marked(i, false);
  122. set_error("");
  123. }
  124. emit_signal("script_changed");
  125. }
  126. void ShaderTextEditor::_bind_methods() {
  127. //ADD_SIGNAL( MethodInfo("script_changed") );
  128. }
  129. ShaderTextEditor::ShaderTextEditor() {
  130. }
  131. /*** SCRIPT EDITOR ******/
  132. void ShaderEditor::_menu_option(int p_option) {
  133. ShaderTextEditor *current = tab_container->get_current_tab_control()->cast_to<ShaderTextEditor>();
  134. if (!current)
  135. return;
  136. switch (p_option) {
  137. case EDIT_UNDO: {
  138. current->get_text_edit()->undo();
  139. } break;
  140. case EDIT_REDO: {
  141. current->get_text_edit()->redo();
  142. } break;
  143. case EDIT_CUT: {
  144. current->get_text_edit()->cut();
  145. } break;
  146. case EDIT_COPY: {
  147. current->get_text_edit()->copy();
  148. } break;
  149. case EDIT_PASTE: {
  150. current->get_text_edit()->paste();
  151. } break;
  152. case EDIT_SELECT_ALL: {
  153. current->get_text_edit()->select_all();
  154. } break;
  155. case SEARCH_FIND: {
  156. current->get_find_replace_bar()->popup_search();
  157. } break;
  158. case SEARCH_FIND_NEXT: {
  159. current->get_find_replace_bar()->search_next();
  160. } break;
  161. case SEARCH_FIND_PREV: {
  162. current->get_find_replace_bar()->search_prev();
  163. } break;
  164. case SEARCH_REPLACE: {
  165. current->get_find_replace_bar()->popup_replace();
  166. } break;
  167. // case SEARCH_LOCATE_SYMBOL: {
  168. // } break;
  169. case SEARCH_GOTO_LINE: {
  170. goto_line_dialog->popup_find_line(current->get_text_edit());
  171. } break;
  172. }
  173. }
  174. void ShaderEditor::_tab_changed(int p_which) {
  175. ShaderTextEditor *shader_editor = tab_container->get_tab_control(p_which)->cast_to<ShaderTextEditor>();
  176. if (shader_editor && is_inside_tree())
  177. shader_editor->get_text_edit()->grab_focus();
  178. ensure_select_current();
  179. }
  180. void ShaderEditor::_notification(int p_what) {
  181. if (p_what == NOTIFICATION_ENTER_TREE) {
  182. close->set_normal_texture(get_icon("Close", "EditorIcons"));
  183. close->set_hover_texture(get_icon("CloseHover", "EditorIcons"));
  184. close->set_pressed_texture(get_icon("Close", "EditorIcons"));
  185. close->connect("pressed", this, "_close_callback");
  186. }
  187. if (p_what == NOTIFICATION_DRAW) {
  188. RID ci = get_canvas_item();
  189. Ref<StyleBox> style = get_stylebox("panel", "Panel");
  190. style->draw(ci, Rect2(Point2(), get_size()));
  191. }
  192. }
  193. Dictionary ShaderEditor::get_state() const {
  194. #if 0
  195. apply_shaders();
  196. Dictionary state;
  197. Array paths;
  198. int open=-1;
  199. for(int i=0;i<tab_container->get_child_count();i++) {
  200. ShaderTextEditor *ste = tab_container->get_child(i)->cast_to<ShaderTextEditor>();
  201. if (!ste)
  202. continue;
  203. Ref<Shader> shader = ste->get_edited_shader();
  204. if (shader->get_path()!="" && shader->get_path().find("local://")==-1 && shader->get_path().find("::")==-1) {
  205. paths.push_back(shader->get_path());
  206. } else {
  207. const Node *owner = _find_node_with_shader(get_root_node(),shader.get_ref_ptr());
  208. if (owner)
  209. paths.push_back(owner->get_path());
  210. }
  211. if (i==tab_container->get_current_tab())
  212. open=i;
  213. }
  214. if (paths.size())
  215. state["sources"]=paths;
  216. if (open!=-1)
  217. state["current"]=open;
  218. return state;
  219. #endif
  220. return Dictionary();
  221. }
  222. void ShaderEditor::set_state(const Dictionary &p_state) {
  223. #if 0
  224. print_line("setting state..");
  225. if (!p_state.has("sources"))
  226. return; //bleh
  227. Array sources = p_state["sources"];
  228. for(int i=0;i<sources.size();i++) {
  229. Variant source=sources[i];
  230. Ref<Shader> shader;
  231. if (source.get_type()==Variant::NODE_PATH) {
  232. print_line("cain find owner at path "+String(source));
  233. Node *owner=get_root_node()->get_node(source);
  234. if (!owner)
  235. continue;
  236. shader = owner->get_shader();
  237. } else if (source.get_type()==Variant::STRING) {
  238. print_line("loading at path "+String(source));
  239. shader = ResourceLoader::load(source,"Shader");
  240. }
  241. print_line("found shader at "+String(source)+"? - "+itos(shader.is_null()));
  242. if (shader.is_null()) //ah well..
  243. continue;
  244. get_scene()->get_root_node()->call("_resource_selected",shader);
  245. }
  246. if (p_state.has("current"))
  247. tab_container->set_current_tab(p_state["current"]);
  248. #endif
  249. }
  250. void ShaderEditor::clear() {
  251. }
  252. void ShaderEditor::_params_changed() {
  253. fragment_editor->_validate_script();
  254. vertex_editor->_validate_script();
  255. light_editor->_validate_script();
  256. }
  257. void ShaderEditor::_editor_settings_changed() {
  258. vertex_editor->update_editor_settings();
  259. fragment_editor->update_editor_settings();
  260. light_editor->update_editor_settings();
  261. }
  262. void ShaderEditor::_bind_methods() {
  263. ObjectTypeDB::bind_method("_editor_settings_changed", &ShaderEditor::_editor_settings_changed);
  264. ObjectTypeDB::bind_method("_tab_changed", &ShaderEditor::_tab_changed);
  265. ObjectTypeDB::bind_method("_menu_option", &ShaderEditor::_menu_option);
  266. ObjectTypeDB::bind_method("_params_changed", &ShaderEditor::_params_changed);
  267. ObjectTypeDB::bind_method("_close_callback", &ShaderEditor::_close_callback);
  268. ObjectTypeDB::bind_method("apply_shaders", &ShaderEditor::apply_shaders);
  269. // ObjectTypeDB::bind_method("_close_current_tab",&ShaderEditor::_close_current_tab);
  270. }
  271. void ShaderEditor::ensure_select_current() {
  272. /*
  273. if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
  274. ShaderTextEditor *ste = tab_container->get_child(tab_container->get_current_tab())->cast_to<ShaderTextEditor>();
  275. if (!ste)
  276. return;
  277. Ref<Shader> shader = ste->get_edited_shader();
  278. get_scene()->get_root_node()->call("_resource_selected",shader);
  279. }*/
  280. }
  281. void ShaderEditor::edit(const Ref<Shader> &p_shader) {
  282. if (p_shader.is_null())
  283. return;
  284. shader = p_shader;
  285. if (shader->get_mode() == Shader::MODE_MATERIAL) {
  286. vertex_editor->set_edited_shader(p_shader, ShaderLanguage::SHADER_MATERIAL_VERTEX);
  287. fragment_editor->set_edited_shader(p_shader, ShaderLanguage::SHADER_MATERIAL_FRAGMENT);
  288. light_editor->set_edited_shader(shader, ShaderLanguage::SHADER_MATERIAL_LIGHT);
  289. } else if (shader->get_mode() == Shader::MODE_CANVAS_ITEM) {
  290. vertex_editor->set_edited_shader(p_shader, ShaderLanguage::SHADER_CANVAS_ITEM_VERTEX);
  291. fragment_editor->set_edited_shader(p_shader, ShaderLanguage::SHADER_CANVAS_ITEM_FRAGMENT);
  292. light_editor->set_edited_shader(shader, ShaderLanguage::SHADER_CANVAS_ITEM_LIGHT);
  293. }
  294. //vertex_editor->set_edited_shader(shader,ShaderLanguage::SHADER_MATERIAL_VERTEX);
  295. // see if already has it
  296. }
  297. void ShaderEditor::save_external_data() {
  298. if (shader.is_null())
  299. return;
  300. apply_shaders();
  301. if (shader->get_path() != "" && shader->get_path().find("local://") == -1 && shader->get_path().find("::") == -1) {
  302. //external shader, save it
  303. ResourceSaver::save(shader->get_path(), shader);
  304. }
  305. }
  306. void ShaderEditor::apply_shaders() {
  307. if (shader.is_valid()) {
  308. shader->set_code(vertex_editor->get_text_edit()->get_text(), fragment_editor->get_text_edit()->get_text(), light_editor->get_text_edit()->get_text(), 0, 0);
  309. shader->set_edited(true);
  310. }
  311. }
  312. void ShaderEditor::_close_callback() {
  313. hide();
  314. }
  315. ShaderEditor::ShaderEditor() {
  316. tab_container = memnew(TabContainer);
  317. add_child(tab_container);
  318. tab_container->set_area_as_parent_rect();
  319. tab_container->set_begin(Point2(0, 0));
  320. //tab_container->set_begin(Point2(0,0));
  321. close = memnew(TextureButton);
  322. close->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, 20);
  323. close->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 4);
  324. close->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 2);
  325. add_child(close);
  326. edit_menu = memnew(MenuButton);
  327. add_child(edit_menu);
  328. edit_menu->set_pos(Point2(5, -1));
  329. edit_menu->set_text(TTR("Edit"));
  330. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/undo", TTR("Undo"), KEY_MASK_CMD | KEY_Z), EDIT_UNDO);
  331. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/redo", TTR("Redo"), KEY_MASK_CMD | KEY_Y), EDIT_REDO);
  332. edit_menu->get_popup()->add_separator();
  333. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/cut", TTR("Cut"), KEY_MASK_CMD | KEY_X), EDIT_CUT);
  334. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy", TTR("Copy"), KEY_MASK_CMD | KEY_C), EDIT_COPY);
  335. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/paste", TTR("Paste"), KEY_MASK_CMD | KEY_V), EDIT_PASTE);
  336. edit_menu->get_popup()->add_separator();
  337. edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/select_all", TTR("Select All"), KEY_MASK_CMD | KEY_A), EDIT_SELECT_ALL);
  338. edit_menu->get_popup()->connect("item_pressed", this, "_menu_option");
  339. search_menu = memnew(MenuButton);
  340. add_child(search_menu);
  341. search_menu->set_pos(Point2(38, -1));
  342. search_menu->set_text(TTR("Search"));
  343. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F), SEARCH_FIND);
  344. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), SEARCH_FIND_NEXT);
  345. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT | KEY_F3), SEARCH_FIND_PREV);
  346. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace", TTR("Replace.."), KEY_MASK_CMD | KEY_R), SEARCH_REPLACE);
  347. search_menu->get_popup()->add_separator();
  348. // search_menu->get_popup()->add_item("Locate Symbol..",SEARCH_LOCATE_SYMBOL,KEY_MASK_CMD|KEY_K);
  349. search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD | KEY_L), SEARCH_GOTO_LINE);
  350. search_menu->get_popup()->connect("item_pressed", this, "_menu_option");
  351. tab_container->connect("tab_changed", this, "_tab_changed");
  352. erase_tab_confirm = memnew(ConfirmationDialog);
  353. add_child(erase_tab_confirm);
  354. erase_tab_confirm->connect("confirmed", this, "_close_current_tab");
  355. goto_line_dialog = memnew(GotoLineDialog);
  356. add_child(goto_line_dialog);
  357. vertex_editor = memnew(ShaderTextEditor);
  358. tab_container->add_child(vertex_editor);
  359. vertex_editor->set_name(TTR("Vertex"));
  360. fragment_editor = memnew(ShaderTextEditor);
  361. tab_container->add_child(fragment_editor);
  362. fragment_editor->set_name(TTR("Fragment"));
  363. light_editor = memnew(ShaderTextEditor);
  364. tab_container->add_child(light_editor);
  365. light_editor->set_name(TTR("Lighting"));
  366. tab_container->set_current_tab(1);
  367. vertex_editor->connect("script_changed", this, "apply_shaders");
  368. fragment_editor->connect("script_changed", this, "apply_shaders");
  369. light_editor->connect("script_changed", this, "apply_shaders");
  370. EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
  371. _editor_settings_changed();
  372. }
  373. void ShaderEditorPlugin::edit(Object *p_object) {
  374. Shader *s = p_object->cast_to<Shader>();
  375. if (!s || s->cast_to<ShaderGraph>()) {
  376. shader_editor->hide(); //Dont edit ShaderGraph
  377. return;
  378. }
  379. if (_2d && s->get_mode() == Shader::MODE_CANVAS_ITEM)
  380. shader_editor->edit(s);
  381. else if (!_2d && s->get_mode() == Shader::MODE_MATERIAL)
  382. shader_editor->edit(s);
  383. }
  384. bool ShaderEditorPlugin::handles(Object *p_object) const {
  385. bool handles = true;
  386. Shader *shader = p_object->cast_to<Shader>();
  387. if (!shader || shader->cast_to<ShaderGraph>()) // Dont handle ShaderGraph's
  388. handles = false;
  389. if (handles && _2d)
  390. handles = shader->get_mode() == Shader::MODE_CANVAS_ITEM;
  391. else if (handles && !_2d)
  392. return shader->get_mode() == Shader::MODE_MATERIAL;
  393. if (!handles)
  394. shader_editor->hide();
  395. return handles;
  396. }
  397. void ShaderEditorPlugin::make_visible(bool p_visible) {
  398. if (p_visible) {
  399. shader_editor->show();
  400. } else {
  401. shader_editor->apply_shaders();
  402. }
  403. }
  404. void ShaderEditorPlugin::selected_notify() {
  405. shader_editor->ensure_select_current();
  406. }
  407. Dictionary ShaderEditorPlugin::get_state() const {
  408. return shader_editor->get_state();
  409. }
  410. void ShaderEditorPlugin::set_state(const Dictionary &p_state) {
  411. shader_editor->set_state(p_state);
  412. }
  413. void ShaderEditorPlugin::clear() {
  414. shader_editor->clear();
  415. }
  416. void ShaderEditorPlugin::save_external_data() {
  417. shader_editor->save_external_data();
  418. }
  419. void ShaderEditorPlugin::apply_changes() {
  420. shader_editor->apply_shaders();
  421. }
  422. ShaderEditorPlugin::ShaderEditorPlugin(EditorNode *p_node, bool p_2d) {
  423. editor = p_node;
  424. shader_editor = memnew(ShaderEditor);
  425. _2d = p_2d;
  426. if (p_2d)
  427. add_control_to_container(CONTAINER_CANVAS_EDITOR_BOTTOM, shader_editor);
  428. else
  429. add_control_to_container(CONTAINER_SPATIAL_EDITOR_BOTTOM, shader_editor);
  430. // editor->get_viewport()->add_child(shader_editor);
  431. // shader_editor->set_area_as_parent_rect();
  432. shader_editor->hide();
  433. }
  434. ShaderEditorPlugin::~ShaderEditorPlugin() {
  435. }