visual_shader_editor_plugin.cpp 42 KB

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