visual_shader_editor_plugin.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*************************************************************************/
  2. /* visual_shader_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "visual_shader_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/os/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/project_settings.h"
  35. #include "editor/editor_properties.h"
  36. #include "scene/animation/animation_player.h"
  37. #include "scene/gui/menu_button.h"
  38. #include "scene/gui/panel.h"
  39. #include "scene/main/viewport.h"
  40. Control *VisualShaderNodePlugin::create_editor(const Ref<VisualShaderNode> &p_node) {
  41. if (get_script_instance()) {
  42. return get_script_instance()->call("create_editor", p_node);
  43. }
  44. return NULL;
  45. }
  46. void VisualShaderNodePlugin::_bind_methods() {
  47. BIND_VMETHOD(MethodInfo(Variant::OBJECT, "create_editor", PropertyInfo(Variant::OBJECT, "for_node", PROPERTY_HINT_RESOURCE_TYPE, "VisualShaderNode")));
  48. }
  49. ///////////////////
  50. void VisualShaderEditor::edit(VisualShader *p_visual_shader) {
  51. if (p_visual_shader) {
  52. visual_shader = Ref<VisualShader>(p_visual_shader);
  53. } else {
  54. visual_shader.unref();
  55. }
  56. if (visual_shader.is_null()) {
  57. hide();
  58. } else {
  59. _update_graph();
  60. }
  61. }
  62. void VisualShaderEditor::add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  63. if (plugins.find(p_plugin) != -1)
  64. return;
  65. plugins.push_back(p_plugin);
  66. }
  67. void VisualShaderEditor::remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  68. plugins.erase(p_plugin);
  69. }
  70. void VisualShaderEditor::add_custom_type(const String &p_name, const String &p_category, const Ref<Script> &p_script) {
  71. for (int i = 0; i < add_options.size(); i++) {
  72. ERR_FAIL_COND(add_options[i].script == p_script);
  73. }
  74. AddOption ao;
  75. ao.name = p_name;
  76. ao.script = p_script;
  77. ao.category = p_category;
  78. add_options.push_back(ao);
  79. _update_options_menu();
  80. }
  81. void VisualShaderEditor::remove_custom_type(const Ref<Script> &p_script) {
  82. for (int i = 0; i < add_options.size(); i++) {
  83. if (add_options[i].script == p_script) {
  84. add_options.remove(i);
  85. return;
  86. }
  87. }
  88. _update_options_menu();
  89. }
  90. void VisualShaderEditor::_update_options_menu() {
  91. String prev_category;
  92. add_node->get_popup()->clear();
  93. for (int i = 0; i < add_options.size(); i++) {
  94. if (prev_category != add_options[i].category) {
  95. add_node->get_popup()->add_separator(add_options[i].category);
  96. }
  97. add_node->get_popup()->add_item(add_options[i].name, i);
  98. prev_category = add_options[i].category;
  99. }
  100. }
  101. Size2 VisualShaderEditor::get_minimum_size() const {
  102. return Size2(10, 200);
  103. }
  104. void VisualShaderEditor::_draw_color_over_button(Object *obj, Color p_color) {
  105. Button *button = Object::cast_to<Button>(obj);
  106. if (!button)
  107. return;
  108. Ref<StyleBox> normal = get_stylebox("normal", "Button");
  109. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  110. }
  111. static Ref<StyleBoxEmpty> make_empty_stylebox(float p_margin_left = -1, float p_margin_top = -1, float p_margin_right = -1, float p_margin_bottom = -1) {
  112. Ref<StyleBoxEmpty> style(memnew(StyleBoxEmpty));
  113. style->set_default_margin(MARGIN_LEFT, p_margin_left * EDSCALE);
  114. style->set_default_margin(MARGIN_RIGHT, p_margin_right * EDSCALE);
  115. style->set_default_margin(MARGIN_BOTTOM, p_margin_bottom * EDSCALE);
  116. style->set_default_margin(MARGIN_TOP, p_margin_top * EDSCALE);
  117. return style;
  118. }
  119. void VisualShaderEditor::_update_graph() {
  120. if (updating)
  121. return;
  122. if (visual_shader.is_null())
  123. return;
  124. graph->set_scroll_ofs(visual_shader->get_graph_offset() * EDSCALE);
  125. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  126. graph->clear_connections();
  127. //erase all nodes
  128. for (int i = 0; i < graph->get_child_count(); i++) {
  129. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  130. memdelete(graph->get_child(i));
  131. i--;
  132. }
  133. }
  134. static const Color type_color[3] = {
  135. Color::html("#61daf4"),
  136. Color::html("#d67dee"),
  137. Color::html("#f6a86e")
  138. };
  139. List<VisualShader::Connection> connections;
  140. visual_shader->get_node_connections(type, &connections);
  141. Ref<StyleBoxEmpty> label_style = make_empty_stylebox(2, 1, 2, 1);
  142. Vector<int> nodes = visual_shader->get_node_list(type);
  143. for (int n_i = 0; n_i < nodes.size(); n_i++) {
  144. Vector2 position = visual_shader->get_node_position(type, nodes[n_i]);
  145. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, nodes[n_i]);
  146. GraphNode *node = memnew(GraphNode);
  147. graph->add_child(node);
  148. /*if (!vsnode->is_connected("changed", this, "_node_changed")) {
  149. vsnode->connect("changed", this, "_node_changed", varray(vsnode->get_instance_id()), CONNECT_DEFERRED);
  150. }*/
  151. node->set_offset(position);
  152. node->set_title(vsnode->get_caption());
  153. node->set_name(itos(nodes[n_i]));
  154. if (nodes[n_i] >= 2) {
  155. node->set_show_close_button(true);
  156. node->connect("close_request", this, "_delete_request", varray(nodes[n_i]), CONNECT_DEFERRED);
  157. }
  158. node->connect("dragged", this, "_node_dragged", varray(nodes[n_i]));
  159. Control *custom_editor = NULL;
  160. int port_offset = 0;
  161. Ref<VisualShaderNodeUniform> uniform = vsnode;
  162. if (uniform.is_valid()) {
  163. LineEdit *uniform_name = memnew(LineEdit);
  164. uniform_name->set_text(uniform->get_uniform_name());
  165. node->add_child(uniform_name);
  166. uniform_name->connect("text_entered", this, "_line_edit_changed", varray(uniform_name, nodes[n_i]));
  167. uniform_name->connect("focus_exited", this, "_line_edit_focus_out", varray(uniform_name, nodes[n_i]));
  168. if (vsnode->get_input_port_count() == 0 && vsnode->get_output_port_count() == 1 && vsnode->get_output_port_name(0) == "") {
  169. //shortcut
  170. VisualShaderNode::PortType port_right = vsnode->get_output_port_type(0);
  171. node->set_slot(0, false, VisualShaderNode::PORT_TYPE_SCALAR, Color(), true, port_right, type_color[port_right]);
  172. continue;
  173. }
  174. port_offset++;
  175. }
  176. for (int i = 0; i < plugins.size(); i++) {
  177. custom_editor = plugins.write[i]->create_editor(vsnode);
  178. if (custom_editor) {
  179. break;
  180. }
  181. }
  182. if (custom_editor && vsnode->get_output_port_count() > 0 && vsnode->get_output_port_name(0) == "" && (vsnode->get_input_port_count() == 0 || vsnode->get_input_port_name(0) == "")) {
  183. //will be embedded in first port
  184. } else if (custom_editor) {
  185. port_offset++;
  186. node->add_child(custom_editor);
  187. custom_editor = NULL;
  188. }
  189. for (int i = 0; i < MAX(vsnode->get_input_port_count(), vsnode->get_output_port_count()); i++) {
  190. if (vsnode->is_port_separator(i)) {
  191. node->add_child(memnew(HSeparator));
  192. port_offset++;
  193. }
  194. bool valid_left = i < vsnode->get_input_port_count();
  195. VisualShaderNode::PortType port_left = VisualShaderNode::PORT_TYPE_SCALAR;
  196. bool port_left_used = false;
  197. String name_left;
  198. if (valid_left) {
  199. name_left = vsnode->get_input_port_name(i);
  200. port_left = vsnode->get_input_port_type(i);
  201. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  202. if (E->get().to_node == nodes[n_i] && E->get().to_port == i) {
  203. port_left_used = true;
  204. }
  205. }
  206. }
  207. bool valid_right = i < vsnode->get_output_port_count();
  208. VisualShaderNode::PortType port_right = VisualShaderNode::PORT_TYPE_SCALAR;
  209. String name_right;
  210. if (valid_right) {
  211. name_right = vsnode->get_output_port_name(i);
  212. port_right = vsnode->get_output_port_type(i);
  213. }
  214. HBoxContainer *hb = memnew(HBoxContainer);
  215. Variant default_value;
  216. if (valid_left && !port_left_used) {
  217. default_value = vsnode->get_input_port_default_value(i);
  218. }
  219. if (default_value.get_type() != Variant::NIL) { // only a label
  220. Button *button = memnew(Button);
  221. hb->add_child(button);
  222. button->connect("pressed", this, "_edit_port_default_input", varray(button, nodes[n_i], i));
  223. switch (default_value.get_type()) {
  224. case Variant::COLOR: {
  225. button->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
  226. button->connect("draw", this, "_draw_color_over_button", varray(button, default_value));
  227. } break;
  228. case Variant::INT:
  229. case Variant::REAL: {
  230. button->set_text(String::num(default_value, 4));
  231. } break;
  232. case Variant::VECTOR3: {
  233. Vector3 v = default_value;
  234. button->set_text(String::num(v.x, 3) + "," + String::num(v.y, 3) + "," + String::num(v.z, 3));
  235. } break;
  236. default: {}
  237. }
  238. }
  239. if (i == 0 && custom_editor) {
  240. hb->add_child(custom_editor);
  241. custom_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  242. } else {
  243. if (valid_left) {
  244. Label *label = memnew(Label);
  245. label->set_text(name_left);
  246. label->add_style_override("normal", label_style); //more compact
  247. hb->add_child(label);
  248. }
  249. hb->add_spacer();
  250. if (valid_right) {
  251. Label *label = memnew(Label);
  252. label->set_text(name_right);
  253. label->set_align(Label::ALIGN_RIGHT);
  254. label->add_style_override("normal", label_style); //more compact
  255. hb->add_child(label);
  256. }
  257. }
  258. if (valid_right && edit_type->get_selected() == VisualShader::TYPE_FRAGMENT) {
  259. TextureButton *preview = memnew(TextureButton);
  260. preview->set_toggle_mode(true);
  261. preview->set_normal_texture(get_icon("GuiVisibilityHidden", "EditorIcons"));
  262. preview->set_pressed_texture(get_icon("GuiVisibilityVisible", "EditorIcons"));
  263. preview->set_v_size_flags(SIZE_SHRINK_CENTER);
  264. if (vsnode->get_output_port_for_preview() == i) {
  265. preview->set_pressed(true);
  266. }
  267. preview->connect("pressed", this, "_preview_select_port", varray(nodes[n_i], i), CONNECT_DEFERRED);
  268. hb->add_child(preview);
  269. }
  270. node->add_child(hb);
  271. node->set_slot(i + port_offset, valid_left, port_left, type_color[port_left], valid_right, port_right, type_color[port_right]);
  272. }
  273. if (vsnode->get_output_port_for_preview() >= 0) {
  274. VisualShaderNodePortPreview *port_preview = memnew(VisualShaderNodePortPreview);
  275. port_preview->setup(visual_shader, type, nodes[n_i], vsnode->get_output_port_for_preview());
  276. port_preview->set_h_size_flags(SIZE_SHRINK_CENTER);
  277. node->add_child(port_preview);
  278. }
  279. String error = vsnode->get_warning(visual_shader->get_mode(), type);
  280. if (error != String()) {
  281. Label *error_label = memnew(Label);
  282. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  283. error_label->set_text(error);
  284. node->add_child(error_label);
  285. }
  286. }
  287. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  288. int from = E->get().from_node;
  289. int from_idx = E->get().from_port;
  290. int to = E->get().to_node;
  291. int to_idx = E->get().to_port;
  292. graph->connect_node(itos(from), from_idx, itos(to), to_idx);
  293. }
  294. }
  295. void VisualShaderEditor::_preview_select_port(int p_node, int p_port) {
  296. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  297. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  298. if (node.is_null()) {
  299. return;
  300. }
  301. if (node->get_output_port_for_preview() == p_port) {
  302. p_port = -1; //toggle it
  303. }
  304. undo_redo->create_action("Set Uniform Name");
  305. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", p_port);
  306. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", node->get_output_port_for_preview());
  307. undo_redo->add_do_method(this, "_update_graph");
  308. undo_redo->add_undo_method(this, "_update_graph");
  309. undo_redo->commit_action();
  310. }
  311. void VisualShaderEditor::_line_edit_changed(const String &p_text, Object *line_edit, int p_node_id) {
  312. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  313. Ref<VisualShaderNodeUniform> node = visual_shader->get_node(type, p_node_id);
  314. ERR_FAIL_COND(!node.is_valid());
  315. String validated_name = visual_shader->validate_uniform_name(p_text, node);
  316. updating = true;
  317. undo_redo->create_action("Set Uniform Name");
  318. undo_redo->add_do_method(node.ptr(), "set_uniform_name", validated_name);
  319. undo_redo->add_undo_method(node.ptr(), "set_uniform_name", node->get_uniform_name());
  320. undo_redo->add_do_method(this, "_update_graph");
  321. undo_redo->add_undo_method(this, "_update_graph");
  322. undo_redo->commit_action();
  323. updating = false;
  324. Object::cast_to<LineEdit>(line_edit)->set_text(validated_name);
  325. }
  326. void VisualShaderEditor::_line_edit_focus_out(Object *line_edit, int p_node_id) {
  327. String text = Object::cast_to<LineEdit>(line_edit)->get_text();
  328. _line_edit_changed(text, line_edit, p_node_id);
  329. }
  330. void VisualShaderEditor::_port_edited() {
  331. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  332. Variant value = property_editor->get_variant();
  333. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, editing_node);
  334. ERR_FAIL_COND(!vsn.is_valid());
  335. undo_redo->create_action("Set Input Default Port");
  336. undo_redo->add_do_method(vsn.ptr(), "set_input_port_default_value", editing_port, value);
  337. undo_redo->add_undo_method(vsn.ptr(), "set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
  338. undo_redo->add_do_method(this, "_update_graph");
  339. undo_redo->add_undo_method(this, "_update_graph");
  340. undo_redo->commit_action();
  341. property_editor->hide();
  342. }
  343. void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node, int p_port) {
  344. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  345. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, p_node);
  346. Button *button = Object::cast_to<Button>(p_button);
  347. ERR_FAIL_COND(!button);
  348. Variant value = vsn->get_input_port_default_value(p_port);
  349. property_editor->set_global_position(button->get_global_position() + Vector2(0, button->get_size().height));
  350. property_editor->edit(NULL, "", value.get_type(), value, 0, "");
  351. property_editor->popup();
  352. editing_node = p_node;
  353. editing_port = p_port;
  354. }
  355. void VisualShaderEditor::_add_node(int p_idx) {
  356. ERR_FAIL_INDEX(p_idx, add_options.size());
  357. Ref<VisualShaderNode> vsnode;
  358. if (add_options[p_idx].type != String()) {
  359. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(add_options[p_idx].type));
  360. ERR_FAIL_COND(!vsn);
  361. vsnode = Ref<VisualShaderNode>(vsn);
  362. } else {
  363. ERR_FAIL_COND(add_options[p_idx].script.is_null());
  364. String base_type = add_options[p_idx].script->get_instance_base_type();
  365. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(base_type));
  366. ERR_FAIL_COND(!vsn);
  367. vsnode = Ref<VisualShaderNode>(vsn);
  368. vsnode->set_script(add_options[p_idx].script.get_ref_ptr());
  369. }
  370. Point2 position = (graph->get_scroll_ofs() + graph->get_size() * 0.5) / EDSCALE;
  371. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  372. int id_to_use = visual_shader->get_valid_node_id(type);
  373. undo_redo->create_action("Add Node to Visual Shader");
  374. undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, vsnode, position, id_to_use);
  375. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_to_use);
  376. undo_redo->add_do_method(this, "_update_graph");
  377. undo_redo->add_undo_method(this, "_update_graph");
  378. undo_redo->commit_action();
  379. }
  380. void VisualShaderEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node) {
  381. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  382. updating = true;
  383. undo_redo->create_action("Node Moved");
  384. undo_redo->add_do_method(visual_shader.ptr(), "set_node_position", type, p_node, p_to);
  385. undo_redo->add_undo_method(visual_shader.ptr(), "set_node_position", type, p_node, p_from);
  386. undo_redo->add_do_method(this, "_update_graph");
  387. undo_redo->add_undo_method(this, "_update_graph");
  388. undo_redo->commit_action();
  389. updating = false;
  390. }
  391. void VisualShaderEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  392. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  393. int from = p_from.to_int();
  394. int to = p_to.to_int();
  395. if (!visual_shader->can_connect_nodes(type, from, p_from_index, to, p_to_index)) {
  396. EditorNode::get_singleton()->show_warning(TTR("Unable to connect, port may be in use or connection may be invalid."));
  397. return;
  398. }
  399. undo_redo->create_action("Nodes Connected");
  400. List<VisualShader::Connection> conns;
  401. visual_shader->get_node_connections(type, &conns);
  402. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  403. if (E->get().to_node == to && E->get().to_port == p_to_index) {
  404. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  405. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  406. }
  407. }
  408. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  409. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  410. undo_redo->add_do_method(this, "_update_graph");
  411. undo_redo->add_undo_method(this, "_update_graph");
  412. undo_redo->commit_action();
  413. }
  414. void VisualShaderEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  415. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  416. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  417. int from = p_from.to_int();
  418. int to = p_to.to_int();
  419. //updating = true; seems graph edit can handle this, no need to protect
  420. undo_redo->create_action("Nodes Disconnected");
  421. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  422. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  423. undo_redo->add_do_method(this, "_update_graph");
  424. undo_redo->add_undo_method(this, "_update_graph");
  425. undo_redo->commit_action();
  426. //updating = false;
  427. }
  428. void VisualShaderEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  429. }
  430. void VisualShaderEditor::_delete_request(int which) {
  431. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  432. undo_redo->create_action("Delete Node");
  433. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, which);
  434. undo_redo->add_undo_method(visual_shader.ptr(), "add_node", type, visual_shader->get_node(type, which), visual_shader->get_node_position(type, which), which);
  435. List<VisualShader::Connection> conns;
  436. visual_shader->get_node_connections(type, &conns);
  437. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  438. if (E->get().from_node == which || E->get().to_node == which) {
  439. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  440. }
  441. }
  442. undo_redo->add_do_method(this, "_update_graph");
  443. undo_redo->add_undo_method(this, "_update_graph");
  444. undo_redo->commit_action();
  445. }
  446. void VisualShaderEditor::_node_selected(Object *p_node) {
  447. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  448. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  449. ERR_FAIL_COND(!gn);
  450. int id = String(gn->get_name()).to_int();
  451. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, id);
  452. ERR_FAIL_COND(!vsnode.is_valid());
  453. //do not rely on this, makes editor more complex
  454. //EditorNode::get_singleton()->push_item(vsnode.ptr(), "", true);
  455. }
  456. void VisualShaderEditor::_input(const Ref<InputEvent> p_event) {
  457. if (graph->has_focus()) {
  458. Ref<InputEventMouseButton> mb = p_event;
  459. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  460. add_node->get_popup()->set_position(get_viewport()->get_mouse_position());
  461. add_node->get_popup()->show_modal();
  462. }
  463. }
  464. }
  465. void VisualShaderEditor::_notification(int p_what) {
  466. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  467. error_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  468. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  469. }
  470. if (p_what == NOTIFICATION_PROCESS) {
  471. }
  472. }
  473. void VisualShaderEditor::_scroll_changed(const Vector2 &p_scroll) {
  474. if (updating)
  475. return;
  476. updating = true;
  477. visual_shader->set_graph_offset(p_scroll / EDSCALE);
  478. updating = false;
  479. }
  480. void VisualShaderEditor::_node_changed(int p_id) {
  481. if (updating)
  482. return;
  483. if (is_visible_in_tree()) {
  484. _update_graph();
  485. }
  486. }
  487. void VisualShaderEditor::_duplicate_nodes() {
  488. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  489. List<int> nodes;
  490. Set<int> excluded;
  491. for (int i = 0; i < graph->get_child_count(); i++) {
  492. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  493. if (gn) {
  494. int id = String(gn->get_name()).to_int();
  495. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  496. Ref<VisualShaderNodeOutput> output = node;
  497. if (output.is_valid()) { // can't duplicate output
  498. excluded.insert(id);
  499. continue;
  500. }
  501. if (node.is_valid() && gn->is_selected()) {
  502. nodes.push_back(id);
  503. }
  504. excluded.insert(id);
  505. }
  506. }
  507. if (nodes.empty())
  508. return;
  509. undo_redo->create_action("Duplicate Nodes");
  510. int base_id = visual_shader->get_valid_node_id(type);
  511. int id_from = base_id;
  512. Map<int, int> connection_remap;
  513. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  514. connection_remap[E->get()] = id_from;
  515. Ref<VisualShaderNode> node = visual_shader->get_node(type, E->get());
  516. Ref<VisualShaderNode> dupli = node->duplicate();
  517. undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, dupli, visual_shader->get_node_position(type, E->get()) + Vector2(10, 10) * EDSCALE, id_from);
  518. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_from);
  519. id_from++;
  520. }
  521. List<VisualShader::Connection> conns;
  522. visual_shader->get_node_connections(type, &conns);
  523. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  524. if (connection_remap.has(E->get().from_node) && connection_remap.has(E->get().to_node)) {
  525. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, connection_remap[E->get().from_node], E->get().from_port, connection_remap[E->get().to_node], E->get().to_port);
  526. }
  527. }
  528. undo_redo->add_do_method(this, "_update_graph");
  529. undo_redo->add_undo_method(this, "_update_graph");
  530. undo_redo->commit_action();
  531. // reselect duplicated nodes by excluding the other ones
  532. for (int i = 0; i < graph->get_child_count(); i++) {
  533. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  534. if (gn) {
  535. int id = String(gn->get_name()).to_int();
  536. if (!excluded.has(id)) {
  537. gn->set_selected(true);
  538. } else {
  539. gn->set_selected(false);
  540. }
  541. }
  542. }
  543. }
  544. void VisualShaderEditor::_mode_selected(int p_id) {
  545. _update_graph();
  546. }
  547. void VisualShaderEditor::_input_select_item(Ref<VisualShaderNodeInput> input, String name) {
  548. String prev_name = input->get_input_name();
  549. if (name == prev_name)
  550. return;
  551. bool type_changed = input->get_input_type_by_name(name) != input->get_input_type_by_name(prev_name);
  552. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  553. undo_redo->create_action("Visual Shader Input Type Changed");
  554. undo_redo->add_do_method(input.ptr(), "set_input_name", name);
  555. undo_redo->add_undo_method(input.ptr(), "set_input_name", prev_name);
  556. if (type_changed) {
  557. //restore connections if type changed
  558. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  559. int id = visual_shader->find_node_id(type, input);
  560. List<VisualShader::Connection> conns;
  561. visual_shader->get_node_connections(type, &conns);
  562. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  563. if (E->get().from_node == id) {
  564. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  565. }
  566. }
  567. }
  568. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  569. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  570. undo_redo->commit_action();
  571. }
  572. void VisualShaderEditor::_bind_methods() {
  573. ClassDB::bind_method("_update_graph", &VisualShaderEditor::_update_graph);
  574. ClassDB::bind_method("_add_node", &VisualShaderEditor::_add_node);
  575. ClassDB::bind_method("_node_dragged", &VisualShaderEditor::_node_dragged);
  576. ClassDB::bind_method("_connection_request", &VisualShaderEditor::_connection_request);
  577. ClassDB::bind_method("_disconnection_request", &VisualShaderEditor::_disconnection_request);
  578. ClassDB::bind_method("_node_selected", &VisualShaderEditor::_node_selected);
  579. ClassDB::bind_method("_scroll_changed", &VisualShaderEditor::_scroll_changed);
  580. ClassDB::bind_method("_delete_request", &VisualShaderEditor::_delete_request);
  581. ClassDB::bind_method("_node_changed", &VisualShaderEditor::_node_changed);
  582. ClassDB::bind_method("_edit_port_default_input", &VisualShaderEditor::_edit_port_default_input);
  583. ClassDB::bind_method("_port_edited", &VisualShaderEditor::_port_edited);
  584. ClassDB::bind_method("_connection_to_empty", &VisualShaderEditor::_connection_to_empty);
  585. ClassDB::bind_method("_line_edit_focus_out", &VisualShaderEditor::_line_edit_focus_out);
  586. ClassDB::bind_method("_line_edit_changed", &VisualShaderEditor::_line_edit_changed);
  587. ClassDB::bind_method("_duplicate_nodes", &VisualShaderEditor::_duplicate_nodes);
  588. ClassDB::bind_method("_mode_selected", &VisualShaderEditor::_mode_selected);
  589. ClassDB::bind_method("_input_select_item", &VisualShaderEditor::_input_select_item);
  590. ClassDB::bind_method("_preview_select_port", &VisualShaderEditor::_preview_select_port);
  591. ClassDB::bind_method("_input", &VisualShaderEditor::_input);
  592. }
  593. VisualShaderEditor *VisualShaderEditor::singleton = NULL;
  594. VisualShaderEditor::VisualShaderEditor() {
  595. singleton = this;
  596. updating = false;
  597. graph = memnew(GraphEdit);
  598. add_child(graph);
  599. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SCALAR);
  600. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_VECTOR);
  601. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_TRANSFORM);
  602. //graph->add_valid_left_disconnect_type(0);
  603. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  604. graph->connect("connection_request", this, "_connection_request", varray(), CONNECT_DEFERRED);
  605. graph->connect("disconnection_request", this, "_disconnection_request", varray(), CONNECT_DEFERRED);
  606. graph->connect("node_selected", this, "_node_selected");
  607. graph->connect("scroll_offset_changed", this, "_scroll_changed");
  608. graph->connect("duplicate_nodes_request", this, "_duplicate_nodes");
  609. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR);
  610. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_VECTOR);
  611. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_SCALAR);
  612. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR);
  613. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShaderNode::PORT_TYPE_TRANSFORM);
  614. VSeparator *vs = memnew(VSeparator);
  615. graph->get_zoom_hbox()->add_child(vs);
  616. graph->get_zoom_hbox()->move_child(vs, 0);
  617. edit_type = memnew(OptionButton);
  618. edit_type->add_item(TTR("Vertex"));
  619. edit_type->add_item(TTR("Fragment"));
  620. edit_type->add_item(TTR("Light"));
  621. edit_type->select(1);
  622. edit_type->connect("item_selected", this, "_mode_selected");
  623. graph->get_zoom_hbox()->add_child(edit_type);
  624. graph->get_zoom_hbox()->move_child(edit_type, 0);
  625. add_node = memnew(MenuButton);
  626. graph->get_zoom_hbox()->add_child(add_node);
  627. add_node->set_text(TTR("Add Node..."));
  628. graph->get_zoom_hbox()->move_child(add_node, 0);
  629. add_node->get_popup()->connect("id_pressed", this, "_add_node");
  630. add_options.push_back(AddOption("Scalar", "Constants", "VisualShaderNodeScalarConstant"));
  631. add_options.push_back(AddOption("Vector", "Constants", "VisualShaderNodeVec3Constant"));
  632. add_options.push_back(AddOption("Color", "Constants", "VisualShaderNodeColorConstant"));
  633. add_options.push_back(AddOption("Transform", "Constants", "VisualShaderNodeTransformConstant"));
  634. add_options.push_back(AddOption("Texture", "Constants", "VisualShaderNodeTexture"));
  635. add_options.push_back(AddOption("CubeMap", "Constants", "VisualShaderNodeCubeMap"));
  636. add_options.push_back(AddOption("ScalarOp", "Operators", "VisualShaderNodeScalarOp"));
  637. add_options.push_back(AddOption("VectorOp", "Operators", "VisualShaderNodeVectorOp"));
  638. add_options.push_back(AddOption("ColorOp", "Operators", "VisualShaderNodeColorOp"));
  639. add_options.push_back(AddOption("TransformMult", "Operators", "VisualShaderNodeTransformMult"));
  640. add_options.push_back(AddOption("TransformVectorMult", "Operators", "VisualShaderNodeTransformVecMult"));
  641. add_options.push_back(AddOption("ScalarFunc", "Functions", "VisualShaderNodeScalarFunc"));
  642. add_options.push_back(AddOption("VectorFunc", "Functions", "VisualShaderNodeVectorFunc"));
  643. add_options.push_back(AddOption("DotProduct", "Functions", "VisualShaderNodeDotProduct"));
  644. add_options.push_back(AddOption("VectorLen", "Functions", "VisualShaderNodeVectorLen"));
  645. add_options.push_back(AddOption("ScalarInterp", "Interpolation", "VisualShaderNodeScalarInterp"));
  646. add_options.push_back(AddOption("VectorInterp", "Interpolation", "VisualShaderNodeVectorInterp"));
  647. add_options.push_back(AddOption("VectorCompose", "Compose", "VisualShaderNodeVectorCompose"));
  648. add_options.push_back(AddOption("TransformCompose", "Compose", "VisualShaderNodeTransformCompose"));
  649. add_options.push_back(AddOption("VectorDecompose", "Decompose", "VisualShaderNodeVectorDecompose"));
  650. add_options.push_back(AddOption("TransformDecompose", "Decompose", "VisualShaderNodeTransformDecompose"));
  651. add_options.push_back(AddOption("Scalar", "Uniforms", "VisualShaderNodeScalarUniform"));
  652. add_options.push_back(AddOption("Vector", "Uniforms", "VisualShaderNodeVec3Uniform"));
  653. add_options.push_back(AddOption("Color", "Uniforms", "VisualShaderNodeColorUniform"));
  654. add_options.push_back(AddOption("Transform", "Uniforms", "VisualShaderNodeTransformUniform"));
  655. add_options.push_back(AddOption("Texture", "Uniforms", "VisualShaderNodeTextureUniform"));
  656. add_options.push_back(AddOption("CubeMap", "Uniforms", "VisualShaderNodeCubeMapUniform"));
  657. add_options.push_back(AddOption("Input", "Inputs", "VisualShaderNodeInput"));
  658. _update_options_menu();
  659. error_panel = memnew(PanelContainer);
  660. add_child(error_panel);
  661. error_label = memnew(Label);
  662. error_panel->add_child(error_label);
  663. error_label->set_text("eh");
  664. error_panel->hide();
  665. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  666. Ref<VisualShaderNodePluginDefault> default_plugin;
  667. default_plugin.instance();
  668. add_plugin(default_plugin);
  669. property_editor = memnew(CustomPropertyEditor);
  670. add_child(property_editor);
  671. property_editor->connect("variant_changed", this, "_port_edited");
  672. }
  673. void VisualShaderEditorPlugin::edit(Object *p_object) {
  674. visual_shader_editor->edit(Object::cast_to<VisualShader>(p_object));
  675. }
  676. bool VisualShaderEditorPlugin::handles(Object *p_object) const {
  677. return p_object->is_class("VisualShader");
  678. }
  679. void VisualShaderEditorPlugin::make_visible(bool p_visible) {
  680. if (p_visible) {
  681. //editor->hide_animation_player_editors();
  682. //editor->animation_panel_make_visible(true);
  683. button->show();
  684. editor->make_bottom_panel_item_visible(visual_shader_editor);
  685. visual_shader_editor->set_process_input(true);
  686. //visual_shader_editor->set_process(true);
  687. } else {
  688. if (visual_shader_editor->is_visible_in_tree())
  689. editor->hide_bottom_panel();
  690. button->hide();
  691. visual_shader_editor->set_process_input(false);
  692. //visual_shader_editor->set_process(false);
  693. }
  694. }
  695. VisualShaderEditorPlugin::VisualShaderEditorPlugin(EditorNode *p_node) {
  696. editor = p_node;
  697. visual_shader_editor = memnew(VisualShaderEditor);
  698. visual_shader_editor->set_custom_minimum_size(Size2(0, 300));
  699. button = editor->add_bottom_panel_item(TTR("VisualShader"), visual_shader_editor);
  700. button->hide();
  701. }
  702. VisualShaderEditorPlugin::~VisualShaderEditorPlugin() {
  703. }
  704. ////////////////
  705. class VisualShaderNodePluginInputEditor : public OptionButton {
  706. GDCLASS(VisualShaderNodePluginInputEditor, OptionButton)
  707. Ref<VisualShaderNodeInput> input;
  708. protected:
  709. static void _bind_methods() {
  710. ClassDB::bind_method("_item_selected", &VisualShaderNodePluginInputEditor::_item_selected);
  711. }
  712. public:
  713. void _notification(int p_what) {
  714. if (p_what == NOTIFICATION_READY) {
  715. connect("item_selected", this, "_item_selected");
  716. }
  717. }
  718. void _item_selected(int p_item) {
  719. VisualShaderEditor::get_singleton()->call_deferred("_input_select_item", input, get_item_text(p_item));
  720. }
  721. void setup(const Ref<VisualShaderNodeInput> &p_input) {
  722. input = p_input;
  723. Ref<Texture> type_icon[3] = {
  724. EditorNode::get_singleton()->get_gui_base()->get_icon("float", "EditorIcons"),
  725. EditorNode::get_singleton()->get_gui_base()->get_icon("Vector3", "EditorIcons"),
  726. EditorNode::get_singleton()->get_gui_base()->get_icon("Transform", "EditorIcons"),
  727. };
  728. add_item("[None]");
  729. int to_select = -1;
  730. for (int i = 0; i < input->get_input_index_count(); i++) {
  731. if (input->get_input_name() == input->get_input_index_name(i)) {
  732. to_select = i + 1;
  733. }
  734. add_icon_item(type_icon[input->get_input_index_type(i)], input->get_input_index_name(i));
  735. }
  736. if (to_select >= 0) {
  737. select(to_select);
  738. }
  739. }
  740. };
  741. class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
  742. GDCLASS(VisualShaderNodePluginDefaultEditor, VBoxContainer)
  743. public:
  744. void _property_changed(const String &prop, const Variant &p_value, bool p_changing = false) {
  745. if (p_changing)
  746. return;
  747. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  748. updating = true;
  749. undo_redo->create_action("Edit Visual Property: " + prop, UndoRedo::MERGE_ENDS);
  750. undo_redo->add_do_property(node.ptr(), prop, p_value);
  751. undo_redo->add_undo_property(node.ptr(), prop, node->get(prop));
  752. undo_redo->commit_action();
  753. updating = false;
  754. }
  755. void _node_changed() {
  756. if (updating)
  757. return;
  758. for (int i = 0; i < properties.size(); i++) {
  759. properties[i]->update_property();
  760. }
  761. }
  762. void _refresh_request() {
  763. VisualShaderEditor::get_singleton()->call_deferred("_update_graph");
  764. }
  765. bool updating;
  766. Ref<VisualShaderNode> node;
  767. Vector<EditorProperty *> properties;
  768. void setup(Vector<EditorProperty *> p_properties, const Vector<StringName> &p_names, Ref<VisualShaderNode> p_node) {
  769. updating = false;
  770. node = p_node;
  771. properties = p_properties;
  772. for (int i = 0; i < p_properties.size(); i++) {
  773. add_child(p_properties[i]);
  774. properties[i]->connect("property_changed", this, "_property_changed");
  775. properties[i]->set_object_and_property(node.ptr(), p_names[i]);
  776. properties[i]->update_property();
  777. properties[i]->set_name_split_ratio(0);
  778. }
  779. node->connect("changed", this, "_node_changed");
  780. node->connect("editor_refresh_request", this, "_refresh_request", varray(), CONNECT_DEFERRED);
  781. }
  782. static void _bind_methods() {
  783. ClassDB::bind_method("_property_changed", &VisualShaderNodePluginDefaultEditor::_property_changed, DEFVAL(false));
  784. ClassDB::bind_method("_node_changed", &VisualShaderNodePluginDefaultEditor::_node_changed);
  785. ClassDB::bind_method("_refresh_request", &VisualShaderNodePluginDefaultEditor::_refresh_request);
  786. }
  787. };
  788. Control *VisualShaderNodePluginDefault::create_editor(const Ref<VisualShaderNode> &p_node) {
  789. if (p_node->is_class("VisualShaderNodeInput")) {
  790. //create input
  791. VisualShaderNodePluginInputEditor *input_editor = memnew(VisualShaderNodePluginInputEditor);
  792. input_editor->setup(p_node);
  793. return input_editor;
  794. }
  795. Vector<StringName> properties = p_node->get_editable_properties();
  796. if (properties.size() == 0) {
  797. return NULL;
  798. }
  799. List<PropertyInfo> props;
  800. p_node->get_property_list(&props);
  801. Vector<PropertyInfo> pinfo;
  802. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  803. for (int i = 0; i < properties.size(); i++) {
  804. if (E->get().name == String(properties[i])) {
  805. pinfo.push_back(E->get());
  806. }
  807. }
  808. }
  809. if (pinfo.size() == 0)
  810. return NULL;
  811. properties.clear();
  812. Ref<VisualShaderNode> node = p_node;
  813. Vector<EditorProperty *> editors;
  814. for (int i = 0; i < pinfo.size(); i++) {
  815. EditorProperty *prop = EditorInspector::instantiate_property_editor(node.ptr(), pinfo[i].type, pinfo[i].name, pinfo[i].hint, pinfo[i].hint_string, pinfo[i].usage);
  816. if (!prop)
  817. return NULL;
  818. if (Object::cast_to<EditorPropertyResource>(prop)) {
  819. Object::cast_to<EditorPropertyResource>(prop)->set_use_sub_inspector(false);
  820. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  821. } else if (Object::cast_to<EditorPropertyTransform>(prop)) {
  822. prop->set_custom_minimum_size(Size2(250 * EDSCALE, 0));
  823. } else if (Object::cast_to<EditorPropertyFloat>(prop) || Object::cast_to<EditorPropertyVector3>(prop)) {
  824. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  825. } else if (Object::cast_to<EditorPropertyEnum>(prop)) {
  826. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  827. Object::cast_to<EditorPropertyEnum>(prop)->set_option_button_clip(false);
  828. }
  829. editors.push_back(prop);
  830. properties.push_back(pinfo[i].name);
  831. }
  832. VisualShaderNodePluginDefaultEditor *editor = memnew(VisualShaderNodePluginDefaultEditor);
  833. editor->setup(editors, properties, p_node);
  834. return editor;
  835. }
  836. void EditorPropertyShaderMode::_option_selected(int p_which) {
  837. //will not use this, instead will do all the logic setting manually
  838. //emit_signal("property_changed", get_edited_property(), p_which);
  839. Ref<VisualShader> visual_shader(Object::cast_to<VisualShader>(get_edited_object()));
  840. if (visual_shader->get_mode() == p_which)
  841. return;
  842. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  843. undo_redo->create_action("Visual Shader Mode Changed");
  844. //do is easy
  845. undo_redo->add_do_method(visual_shader.ptr(), "set_mode", p_which);
  846. undo_redo->add_undo_method(visual_shader.ptr(), "set_mode", visual_shader->get_mode());
  847. //now undo is hell
  848. //1. restore connections to output
  849. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  850. VisualShader::Type type = VisualShader::Type(i);
  851. List<VisualShader::Connection> conns;
  852. visual_shader->get_node_connections(type, &conns);
  853. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  854. if (E->get().to_node == VisualShader::NODE_ID_OUTPUT) {
  855. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  856. }
  857. }
  858. }
  859. //2. restore input indices
  860. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  861. VisualShader::Type type = VisualShader::Type(i);
  862. Vector<int> nodes = visual_shader->get_node_list(type);
  863. for (int i = 0; i < nodes.size(); i++) {
  864. Ref<VisualShaderNodeInput> input = visual_shader->get_node(type, nodes[i]);
  865. if (!input.is_valid()) {
  866. continue;
  867. }
  868. undo_redo->add_undo_method(input.ptr(), "set_input_name", input->get_input_name());
  869. }
  870. }
  871. //3. restore enums and flags
  872. List<PropertyInfo> props;
  873. visual_shader->get_property_list(&props);
  874. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  875. if (E->get().name.begins_with("flags/") || E->get().name.begins_with("modes/")) {
  876. undo_redo->add_undo_property(visual_shader.ptr(), E->get().name, visual_shader->get(E->get().name));
  877. }
  878. }
  879. //update graph
  880. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  881. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  882. undo_redo->commit_action();
  883. }
  884. void EditorPropertyShaderMode::update_property() {
  885. int which = get_edited_object()->get(get_edited_property());
  886. options->select(which);
  887. }
  888. void EditorPropertyShaderMode::setup(const Vector<String> &p_options) {
  889. for (int i = 0; i < p_options.size(); i++) {
  890. options->add_item(p_options[i], i);
  891. }
  892. }
  893. void EditorPropertyShaderMode::set_option_button_clip(bool p_enable) {
  894. options->set_clip_text(p_enable);
  895. }
  896. void EditorPropertyShaderMode::_bind_methods() {
  897. ClassDB::bind_method(D_METHOD("_option_selected"), &EditorPropertyShaderMode::_option_selected);
  898. }
  899. EditorPropertyShaderMode::EditorPropertyShaderMode() {
  900. options = memnew(OptionButton);
  901. options->set_clip_text(true);
  902. add_child(options);
  903. add_focusable(options);
  904. options->connect("item_selected", this, "_option_selected");
  905. }
  906. bool EditorInspectorShaderModePlugin::can_handle(Object *p_object) {
  907. return true; //can handle everything
  908. }
  909. void EditorInspectorShaderModePlugin::parse_begin(Object *p_object) {
  910. //do none
  911. }
  912. bool EditorInspectorShaderModePlugin::parse_property(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage) {
  913. if (p_path == "mode" && p_object->is_class("VisualShader") && p_type == Variant::INT) {
  914. EditorPropertyShaderMode *editor = memnew(EditorPropertyShaderMode);
  915. Vector<String> options = p_hint_text.split(",");
  916. editor->setup(options);
  917. add_property_editor(p_path, editor);
  918. return true;
  919. }
  920. return false; //can be overridden, although it will most likely be last anyway
  921. }
  922. void EditorInspectorShaderModePlugin::parse_end() {
  923. //do none
  924. }
  925. //////////////////////////////////
  926. void VisualShaderNodePortPreview::_shader_changed() {
  927. if (shader.is_null()) {
  928. return;
  929. }
  930. Vector<VisualShader::DefaultTextureParam> default_textures;
  931. String shader_code = shader->generate_preview_shader(type, node, port, default_textures);
  932. Ref<Shader> preview_shader;
  933. preview_shader.instance();
  934. preview_shader->set_code(shader_code);
  935. for (int i = 0; i < default_textures.size(); i++) {
  936. preview_shader->set_default_texture_param(default_textures[i].name, default_textures[i].param);
  937. }
  938. Ref<ShaderMaterial> material;
  939. material.instance();
  940. material->set_shader(preview_shader);
  941. //find if a material is also being edited and copy parameters to this one
  942. for (int i = EditorNode::get_singleton()->get_editor_history()->get_path_size() - 1; i >= 0; i--) {
  943. Object *object = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_path_object(i));
  944. if (!object)
  945. continue;
  946. ShaderMaterial *src_mat = Object::cast_to<ShaderMaterial>(object);
  947. if (src_mat && src_mat->get_shader().is_valid()) {
  948. List<PropertyInfo> params;
  949. src_mat->get_shader()->get_param_list(&params);
  950. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  951. material->set(E->get().name, src_mat->get(E->get().name));
  952. }
  953. }
  954. }
  955. set_material(material);
  956. }
  957. void VisualShaderNodePortPreview::setup(const Ref<VisualShader> &p_shader, VisualShader::Type p_type, int p_node, int p_port) {
  958. shader = p_shader;
  959. shader->connect("changed", this, "_shader_changed");
  960. type = p_type;
  961. port = p_port;
  962. node = p_node;
  963. update();
  964. _shader_changed();
  965. }
  966. Size2 VisualShaderNodePortPreview::get_minimum_size() const {
  967. return Size2(100, 100) * EDSCALE;
  968. }
  969. void VisualShaderNodePortPreview::_notification(int p_what) {
  970. if (p_what == NOTIFICATION_DRAW) {
  971. Vector<Vector2> points;
  972. Vector<Vector2> uvs;
  973. Vector<Color> colors;
  974. points.push_back(Vector2());
  975. uvs.push_back(Vector2(0, 0));
  976. colors.push_back(Color(1, 1, 1, 1));
  977. points.push_back(Vector2(get_size().width, 0));
  978. uvs.push_back(Vector2(1, 0));
  979. colors.push_back(Color(1, 1, 1, 1));
  980. points.push_back(get_size());
  981. uvs.push_back(Vector2(1, 1));
  982. colors.push_back(Color(1, 1, 1, 1));
  983. points.push_back(Vector2(0, get_size().height));
  984. uvs.push_back(Vector2(0, 1));
  985. colors.push_back(Color(1, 1, 1, 1));
  986. draw_primitive(points, colors, uvs);
  987. }
  988. }
  989. void VisualShaderNodePortPreview::_bind_methods() {
  990. ClassDB::bind_method("_shader_changed", &VisualShaderNodePortPreview::_shader_changed);
  991. }
  992. VisualShaderNodePortPreview::VisualShaderNodePortPreview() {
  993. }