visual_shader_editor_plugin.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. if (EditorSettings::get_singleton()->get("interface/theme/use_graph_node_headers")) {
  273. Ref<StyleBoxFlat> sb = node->get_stylebox("frame", "GraphNode");
  274. Color c = sb->get_border_color(MARGIN_TOP);
  275. Color mono_color = ((c.r + c.g + c.b) / 3) < 0.7 ? Color(1.0, 1.0, 1.0) : Color(0.0, 0.0, 0.0);
  276. mono_color.a = 0.85;
  277. c = mono_color;
  278. node->add_color_override("title_color", c);
  279. c.a = 0.7;
  280. node->add_color_override("close_color", c);
  281. }
  282. }
  283. if (vsnode->get_output_port_for_preview() >= 0) {
  284. VisualShaderNodePortPreview *port_preview = memnew(VisualShaderNodePortPreview);
  285. port_preview->setup(visual_shader, type, nodes[n_i], vsnode->get_output_port_for_preview());
  286. port_preview->set_h_size_flags(SIZE_SHRINK_CENTER);
  287. node->add_child(port_preview);
  288. }
  289. String error = vsnode->get_warning(visual_shader->get_mode(), type);
  290. if (error != String()) {
  291. Label *error_label = memnew(Label);
  292. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  293. error_label->set_text(error);
  294. node->add_child(error_label);
  295. }
  296. }
  297. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  298. int from = E->get().from_node;
  299. int from_idx = E->get().from_port;
  300. int to = E->get().to_node;
  301. int to_idx = E->get().to_port;
  302. graph->connect_node(itos(from), from_idx, itos(to), to_idx);
  303. }
  304. }
  305. void VisualShaderEditor::_preview_select_port(int p_node, int p_port) {
  306. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  307. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  308. if (node.is_null()) {
  309. return;
  310. }
  311. if (node->get_output_port_for_preview() == p_port) {
  312. p_port = -1; //toggle it
  313. }
  314. undo_redo->create_action("Set Uniform Name");
  315. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", p_port);
  316. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", node->get_output_port_for_preview());
  317. undo_redo->add_do_method(this, "_update_graph");
  318. undo_redo->add_undo_method(this, "_update_graph");
  319. undo_redo->commit_action();
  320. }
  321. void VisualShaderEditor::_line_edit_changed(const String &p_text, Object *line_edit, int p_node_id) {
  322. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  323. Ref<VisualShaderNodeUniform> node = visual_shader->get_node(type, p_node_id);
  324. ERR_FAIL_COND(!node.is_valid());
  325. String validated_name = visual_shader->validate_uniform_name(p_text, node);
  326. updating = true;
  327. undo_redo->create_action("Set Uniform Name");
  328. undo_redo->add_do_method(node.ptr(), "set_uniform_name", validated_name);
  329. undo_redo->add_undo_method(node.ptr(), "set_uniform_name", node->get_uniform_name());
  330. undo_redo->add_do_method(this, "_update_graph");
  331. undo_redo->add_undo_method(this, "_update_graph");
  332. undo_redo->commit_action();
  333. updating = false;
  334. Object::cast_to<LineEdit>(line_edit)->set_text(validated_name);
  335. }
  336. void VisualShaderEditor::_line_edit_focus_out(Object *line_edit, int p_node_id) {
  337. String text = Object::cast_to<LineEdit>(line_edit)->get_text();
  338. _line_edit_changed(text, line_edit, p_node_id);
  339. }
  340. void VisualShaderEditor::_port_edited() {
  341. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  342. Variant value = property_editor->get_variant();
  343. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, editing_node);
  344. ERR_FAIL_COND(!vsn.is_valid());
  345. undo_redo->create_action("Set Input Default Port");
  346. undo_redo->add_do_method(vsn.ptr(), "set_input_port_default_value", editing_port, value);
  347. undo_redo->add_undo_method(vsn.ptr(), "set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
  348. undo_redo->add_do_method(this, "_update_graph");
  349. undo_redo->add_undo_method(this, "_update_graph");
  350. undo_redo->commit_action();
  351. property_editor->hide();
  352. }
  353. void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node, int p_port) {
  354. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  355. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, p_node);
  356. Button *button = Object::cast_to<Button>(p_button);
  357. ERR_FAIL_COND(!button);
  358. Variant value = vsn->get_input_port_default_value(p_port);
  359. property_editor->set_global_position(button->get_global_position() + Vector2(0, button->get_size().height));
  360. property_editor->edit(NULL, "", value.get_type(), value, 0, "");
  361. property_editor->popup();
  362. editing_node = p_node;
  363. editing_port = p_port;
  364. }
  365. void VisualShaderEditor::_add_node(int p_idx) {
  366. ERR_FAIL_INDEX(p_idx, add_options.size());
  367. Ref<VisualShaderNode> vsnode;
  368. if (add_options[p_idx].type != String()) {
  369. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(add_options[p_idx].type));
  370. ERR_FAIL_COND(!vsn);
  371. vsnode = Ref<VisualShaderNode>(vsn);
  372. } else {
  373. ERR_FAIL_COND(add_options[p_idx].script.is_null());
  374. String base_type = add_options[p_idx].script->get_instance_base_type();
  375. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(base_type));
  376. ERR_FAIL_COND(!vsn);
  377. vsnode = Ref<VisualShaderNode>(vsn);
  378. vsnode->set_script(add_options[p_idx].script.get_ref_ptr());
  379. }
  380. Point2 position = (graph->get_scroll_ofs() + graph->get_size() * 0.5) / EDSCALE;
  381. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  382. int id_to_use = visual_shader->get_valid_node_id(type);
  383. undo_redo->create_action("Add Node to Visual Shader");
  384. undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, vsnode, position, id_to_use);
  385. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_to_use);
  386. undo_redo->add_do_method(this, "_update_graph");
  387. undo_redo->add_undo_method(this, "_update_graph");
  388. undo_redo->commit_action();
  389. }
  390. void VisualShaderEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node) {
  391. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  392. updating = true;
  393. undo_redo->create_action("Node Moved");
  394. undo_redo->add_do_method(visual_shader.ptr(), "set_node_position", type, p_node, p_to);
  395. undo_redo->add_undo_method(visual_shader.ptr(), "set_node_position", type, p_node, p_from);
  396. undo_redo->add_do_method(this, "_update_graph");
  397. undo_redo->add_undo_method(this, "_update_graph");
  398. undo_redo->commit_action();
  399. updating = false;
  400. }
  401. void VisualShaderEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  402. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  403. int from = p_from.to_int();
  404. int to = p_to.to_int();
  405. if (!visual_shader->can_connect_nodes(type, from, p_from_index, to, p_to_index)) {
  406. EditorNode::get_singleton()->show_warning(TTR("Unable to connect, port may be in use or connection may be invalid."));
  407. return;
  408. }
  409. undo_redo->create_action("Nodes Connected");
  410. List<VisualShader::Connection> conns;
  411. visual_shader->get_node_connections(type, &conns);
  412. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  413. if (E->get().to_node == to && E->get().to_port == p_to_index) {
  414. 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);
  415. 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);
  416. }
  417. }
  418. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  419. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  420. undo_redo->add_do_method(this, "_update_graph");
  421. undo_redo->add_undo_method(this, "_update_graph");
  422. undo_redo->commit_action();
  423. }
  424. void VisualShaderEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  425. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  426. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  427. int from = p_from.to_int();
  428. int to = p_to.to_int();
  429. //updating = true; seems graph edit can handle this, no need to protect
  430. undo_redo->create_action("Nodes Disconnected");
  431. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  432. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  433. undo_redo->add_do_method(this, "_update_graph");
  434. undo_redo->add_undo_method(this, "_update_graph");
  435. undo_redo->commit_action();
  436. //updating = false;
  437. }
  438. void VisualShaderEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  439. }
  440. void VisualShaderEditor::_delete_request(int which) {
  441. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  442. undo_redo->create_action("Delete Node");
  443. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, which);
  444. 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);
  445. List<VisualShader::Connection> conns;
  446. visual_shader->get_node_connections(type, &conns);
  447. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  448. if (E->get().from_node == which || E->get().to_node == which) {
  449. 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);
  450. }
  451. }
  452. undo_redo->add_do_method(this, "_update_graph");
  453. undo_redo->add_undo_method(this, "_update_graph");
  454. undo_redo->commit_action();
  455. }
  456. void VisualShaderEditor::_node_selected(Object *p_node) {
  457. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  458. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  459. ERR_FAIL_COND(!gn);
  460. int id = String(gn->get_name()).to_int();
  461. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, id);
  462. ERR_FAIL_COND(!vsnode.is_valid());
  463. //do not rely on this, makes editor more complex
  464. //EditorNode::get_singleton()->push_item(vsnode.ptr(), "", true);
  465. }
  466. void VisualShaderEditor::_input(const Ref<InputEvent> p_event) {
  467. if (graph->has_focus()) {
  468. Ref<InputEventMouseButton> mb = p_event;
  469. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  470. add_node->get_popup()->set_position(get_viewport()->get_mouse_position());
  471. add_node->get_popup()->show_modal();
  472. }
  473. }
  474. }
  475. void VisualShaderEditor::_notification(int p_what) {
  476. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  477. error_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  478. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  479. if (p_what == NOTIFICATION_THEME_CHANGED && is_visible_in_tree())
  480. _update_graph();
  481. }
  482. if (p_what == NOTIFICATION_PROCESS) {
  483. }
  484. }
  485. void VisualShaderEditor::_scroll_changed(const Vector2 &p_scroll) {
  486. if (updating)
  487. return;
  488. updating = true;
  489. visual_shader->set_graph_offset(p_scroll / EDSCALE);
  490. updating = false;
  491. }
  492. void VisualShaderEditor::_node_changed(int p_id) {
  493. if (updating)
  494. return;
  495. if (is_visible_in_tree()) {
  496. _update_graph();
  497. }
  498. }
  499. void VisualShaderEditor::_duplicate_nodes() {
  500. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  501. List<int> nodes;
  502. Set<int> excluded;
  503. for (int i = 0; i < graph->get_child_count(); i++) {
  504. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  505. if (gn) {
  506. int id = String(gn->get_name()).to_int();
  507. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  508. Ref<VisualShaderNodeOutput> output = node;
  509. if (output.is_valid()) { // can't duplicate output
  510. excluded.insert(id);
  511. continue;
  512. }
  513. if (node.is_valid() && gn->is_selected()) {
  514. nodes.push_back(id);
  515. }
  516. excluded.insert(id);
  517. }
  518. }
  519. if (nodes.empty())
  520. return;
  521. undo_redo->create_action("Duplicate Nodes");
  522. int base_id = visual_shader->get_valid_node_id(type);
  523. int id_from = base_id;
  524. Map<int, int> connection_remap;
  525. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  526. connection_remap[E->get()] = id_from;
  527. Ref<VisualShaderNode> node = visual_shader->get_node(type, E->get());
  528. Ref<VisualShaderNode> dupli = node->duplicate();
  529. 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);
  530. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_from);
  531. id_from++;
  532. }
  533. List<VisualShader::Connection> conns;
  534. visual_shader->get_node_connections(type, &conns);
  535. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  536. if (connection_remap.has(E->get().from_node) && connection_remap.has(E->get().to_node)) {
  537. 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);
  538. }
  539. }
  540. undo_redo->add_do_method(this, "_update_graph");
  541. undo_redo->add_undo_method(this, "_update_graph");
  542. undo_redo->commit_action();
  543. // reselect duplicated nodes by excluding the other ones
  544. for (int i = 0; i < graph->get_child_count(); i++) {
  545. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  546. if (gn) {
  547. int id = String(gn->get_name()).to_int();
  548. if (!excluded.has(id)) {
  549. gn->set_selected(true);
  550. } else {
  551. gn->set_selected(false);
  552. }
  553. }
  554. }
  555. }
  556. void VisualShaderEditor::_mode_selected(int p_id) {
  557. _update_graph();
  558. }
  559. void VisualShaderEditor::_input_select_item(Ref<VisualShaderNodeInput> input, String name) {
  560. String prev_name = input->get_input_name();
  561. if (name == prev_name)
  562. return;
  563. bool type_changed = input->get_input_type_by_name(name) != input->get_input_type_by_name(prev_name);
  564. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  565. undo_redo->create_action("Visual Shader Input Type Changed");
  566. undo_redo->add_do_method(input.ptr(), "set_input_name", name);
  567. undo_redo->add_undo_method(input.ptr(), "set_input_name", prev_name);
  568. if (type_changed) {
  569. //restore connections if type changed
  570. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  571. int id = visual_shader->find_node_id(type, input);
  572. List<VisualShader::Connection> conns;
  573. visual_shader->get_node_connections(type, &conns);
  574. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  575. if (E->get().from_node == id) {
  576. 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);
  577. }
  578. }
  579. }
  580. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  581. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  582. undo_redo->commit_action();
  583. }
  584. void VisualShaderEditor::_bind_methods() {
  585. ClassDB::bind_method("_update_graph", &VisualShaderEditor::_update_graph);
  586. ClassDB::bind_method("_add_node", &VisualShaderEditor::_add_node);
  587. ClassDB::bind_method("_node_dragged", &VisualShaderEditor::_node_dragged);
  588. ClassDB::bind_method("_connection_request", &VisualShaderEditor::_connection_request);
  589. ClassDB::bind_method("_disconnection_request", &VisualShaderEditor::_disconnection_request);
  590. ClassDB::bind_method("_node_selected", &VisualShaderEditor::_node_selected);
  591. ClassDB::bind_method("_scroll_changed", &VisualShaderEditor::_scroll_changed);
  592. ClassDB::bind_method("_delete_request", &VisualShaderEditor::_delete_request);
  593. ClassDB::bind_method("_node_changed", &VisualShaderEditor::_node_changed);
  594. ClassDB::bind_method("_edit_port_default_input", &VisualShaderEditor::_edit_port_default_input);
  595. ClassDB::bind_method("_port_edited", &VisualShaderEditor::_port_edited);
  596. ClassDB::bind_method("_connection_to_empty", &VisualShaderEditor::_connection_to_empty);
  597. ClassDB::bind_method("_line_edit_focus_out", &VisualShaderEditor::_line_edit_focus_out);
  598. ClassDB::bind_method("_line_edit_changed", &VisualShaderEditor::_line_edit_changed);
  599. ClassDB::bind_method("_duplicate_nodes", &VisualShaderEditor::_duplicate_nodes);
  600. ClassDB::bind_method("_mode_selected", &VisualShaderEditor::_mode_selected);
  601. ClassDB::bind_method("_input_select_item", &VisualShaderEditor::_input_select_item);
  602. ClassDB::bind_method("_preview_select_port", &VisualShaderEditor::_preview_select_port);
  603. ClassDB::bind_method("_input", &VisualShaderEditor::_input);
  604. }
  605. VisualShaderEditor *VisualShaderEditor::singleton = NULL;
  606. VisualShaderEditor::VisualShaderEditor() {
  607. singleton = this;
  608. updating = false;
  609. graph = memnew(GraphEdit);
  610. add_child(graph);
  611. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SCALAR);
  612. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_VECTOR);
  613. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_TRANSFORM);
  614. //graph->add_valid_left_disconnect_type(0);
  615. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  616. graph->connect("connection_request", this, "_connection_request", varray(), CONNECT_DEFERRED);
  617. graph->connect("disconnection_request", this, "_disconnection_request", varray(), CONNECT_DEFERRED);
  618. graph->connect("node_selected", this, "_node_selected");
  619. graph->connect("scroll_offset_changed", this, "_scroll_changed");
  620. graph->connect("duplicate_nodes_request", this, "_duplicate_nodes");
  621. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR);
  622. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_VECTOR);
  623. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_SCALAR);
  624. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR);
  625. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShaderNode::PORT_TYPE_TRANSFORM);
  626. VSeparator *vs = memnew(VSeparator);
  627. graph->get_zoom_hbox()->add_child(vs);
  628. graph->get_zoom_hbox()->move_child(vs, 0);
  629. edit_type = memnew(OptionButton);
  630. edit_type->add_item(TTR("Vertex"));
  631. edit_type->add_item(TTR("Fragment"));
  632. edit_type->add_item(TTR("Light"));
  633. edit_type->select(1);
  634. edit_type->connect("item_selected", this, "_mode_selected");
  635. graph->get_zoom_hbox()->add_child(edit_type);
  636. graph->get_zoom_hbox()->move_child(edit_type, 0);
  637. add_node = memnew(MenuButton);
  638. graph->get_zoom_hbox()->add_child(add_node);
  639. add_node->set_text(TTR("Add Node..."));
  640. graph->get_zoom_hbox()->move_child(add_node, 0);
  641. add_node->get_popup()->connect("id_pressed", this, "_add_node");
  642. add_options.push_back(AddOption("Scalar", "Constants", "VisualShaderNodeScalarConstant"));
  643. add_options.push_back(AddOption("Vector", "Constants", "VisualShaderNodeVec3Constant"));
  644. add_options.push_back(AddOption("Color", "Constants", "VisualShaderNodeColorConstant"));
  645. add_options.push_back(AddOption("Transform", "Constants", "VisualShaderNodeTransformConstant"));
  646. add_options.push_back(AddOption("Texture", "Constants", "VisualShaderNodeTexture"));
  647. add_options.push_back(AddOption("CubeMap", "Constants", "VisualShaderNodeCubeMap"));
  648. add_options.push_back(AddOption("ScalarOp", "Operators", "VisualShaderNodeScalarOp"));
  649. add_options.push_back(AddOption("VectorOp", "Operators", "VisualShaderNodeVectorOp"));
  650. add_options.push_back(AddOption("ColorOp", "Operators", "VisualShaderNodeColorOp"));
  651. add_options.push_back(AddOption("TransformMult", "Operators", "VisualShaderNodeTransformMult"));
  652. add_options.push_back(AddOption("TransformVectorMult", "Operators", "VisualShaderNodeTransformVecMult"));
  653. add_options.push_back(AddOption("ScalarFunc", "Functions", "VisualShaderNodeScalarFunc"));
  654. add_options.push_back(AddOption("VectorFunc", "Functions", "VisualShaderNodeVectorFunc"));
  655. add_options.push_back(AddOption("DotProduct", "Functions", "VisualShaderNodeDotProduct"));
  656. add_options.push_back(AddOption("VectorLen", "Functions", "VisualShaderNodeVectorLen"));
  657. add_options.push_back(AddOption("ScalarInterp", "Interpolation", "VisualShaderNodeScalarInterp"));
  658. add_options.push_back(AddOption("VectorInterp", "Interpolation", "VisualShaderNodeVectorInterp"));
  659. add_options.push_back(AddOption("VectorCompose", "Compose", "VisualShaderNodeVectorCompose"));
  660. add_options.push_back(AddOption("TransformCompose", "Compose", "VisualShaderNodeTransformCompose"));
  661. add_options.push_back(AddOption("VectorDecompose", "Decompose", "VisualShaderNodeVectorDecompose"));
  662. add_options.push_back(AddOption("TransformDecompose", "Decompose", "VisualShaderNodeTransformDecompose"));
  663. add_options.push_back(AddOption("Scalar", "Uniforms", "VisualShaderNodeScalarUniform"));
  664. add_options.push_back(AddOption("Vector", "Uniforms", "VisualShaderNodeVec3Uniform"));
  665. add_options.push_back(AddOption("Color", "Uniforms", "VisualShaderNodeColorUniform"));
  666. add_options.push_back(AddOption("Transform", "Uniforms", "VisualShaderNodeTransformUniform"));
  667. add_options.push_back(AddOption("Texture", "Uniforms", "VisualShaderNodeTextureUniform"));
  668. add_options.push_back(AddOption("CubeMap", "Uniforms", "VisualShaderNodeCubeMapUniform"));
  669. add_options.push_back(AddOption("Input", "Inputs", "VisualShaderNodeInput"));
  670. _update_options_menu();
  671. error_panel = memnew(PanelContainer);
  672. add_child(error_panel);
  673. error_label = memnew(Label);
  674. error_panel->add_child(error_label);
  675. error_label->set_text("eh");
  676. error_panel->hide();
  677. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  678. Ref<VisualShaderNodePluginDefault> default_plugin;
  679. default_plugin.instance();
  680. add_plugin(default_plugin);
  681. property_editor = memnew(CustomPropertyEditor);
  682. add_child(property_editor);
  683. property_editor->connect("variant_changed", this, "_port_edited");
  684. }
  685. void VisualShaderEditorPlugin::edit(Object *p_object) {
  686. visual_shader_editor->edit(Object::cast_to<VisualShader>(p_object));
  687. }
  688. bool VisualShaderEditorPlugin::handles(Object *p_object) const {
  689. return p_object->is_class("VisualShader");
  690. }
  691. void VisualShaderEditorPlugin::make_visible(bool p_visible) {
  692. if (p_visible) {
  693. //editor->hide_animation_player_editors();
  694. //editor->animation_panel_make_visible(true);
  695. button->show();
  696. editor->make_bottom_panel_item_visible(visual_shader_editor);
  697. visual_shader_editor->set_process_input(true);
  698. //visual_shader_editor->set_process(true);
  699. } else {
  700. if (visual_shader_editor->is_visible_in_tree())
  701. editor->hide_bottom_panel();
  702. button->hide();
  703. visual_shader_editor->set_process_input(false);
  704. //visual_shader_editor->set_process(false);
  705. }
  706. }
  707. VisualShaderEditorPlugin::VisualShaderEditorPlugin(EditorNode *p_node) {
  708. editor = p_node;
  709. visual_shader_editor = memnew(VisualShaderEditor);
  710. visual_shader_editor->set_custom_minimum_size(Size2(0, 300));
  711. button = editor->add_bottom_panel_item(TTR("VisualShader"), visual_shader_editor);
  712. button->hide();
  713. }
  714. VisualShaderEditorPlugin::~VisualShaderEditorPlugin() {
  715. }
  716. ////////////////
  717. class VisualShaderNodePluginInputEditor : public OptionButton {
  718. GDCLASS(VisualShaderNodePluginInputEditor, OptionButton)
  719. Ref<VisualShaderNodeInput> input;
  720. protected:
  721. static void _bind_methods() {
  722. ClassDB::bind_method("_item_selected", &VisualShaderNodePluginInputEditor::_item_selected);
  723. }
  724. public:
  725. void _notification(int p_what) {
  726. if (p_what == NOTIFICATION_READY) {
  727. connect("item_selected", this, "_item_selected");
  728. }
  729. }
  730. void _item_selected(int p_item) {
  731. VisualShaderEditor::get_singleton()->call_deferred("_input_select_item", input, get_item_text(p_item));
  732. }
  733. void setup(const Ref<VisualShaderNodeInput> &p_input) {
  734. input = p_input;
  735. Ref<Texture> type_icon[3] = {
  736. EditorNode::get_singleton()->get_gui_base()->get_icon("float", "EditorIcons"),
  737. EditorNode::get_singleton()->get_gui_base()->get_icon("Vector3", "EditorIcons"),
  738. EditorNode::get_singleton()->get_gui_base()->get_icon("Transform", "EditorIcons"),
  739. };
  740. add_item("[None]");
  741. int to_select = -1;
  742. for (int i = 0; i < input->get_input_index_count(); i++) {
  743. if (input->get_input_name() == input->get_input_index_name(i)) {
  744. to_select = i + 1;
  745. }
  746. add_icon_item(type_icon[input->get_input_index_type(i)], input->get_input_index_name(i));
  747. }
  748. if (to_select >= 0) {
  749. select(to_select);
  750. }
  751. }
  752. };
  753. class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
  754. GDCLASS(VisualShaderNodePluginDefaultEditor, VBoxContainer)
  755. public:
  756. void _property_changed(const String &prop, const Variant &p_value, bool p_changing = false) {
  757. if (p_changing)
  758. return;
  759. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  760. updating = true;
  761. undo_redo->create_action("Edit Visual Property: " + prop, UndoRedo::MERGE_ENDS);
  762. undo_redo->add_do_property(node.ptr(), prop, p_value);
  763. undo_redo->add_undo_property(node.ptr(), prop, node->get(prop));
  764. undo_redo->commit_action();
  765. updating = false;
  766. }
  767. void _node_changed() {
  768. if (updating)
  769. return;
  770. for (int i = 0; i < properties.size(); i++) {
  771. properties[i]->update_property();
  772. }
  773. }
  774. void _refresh_request() {
  775. VisualShaderEditor::get_singleton()->call_deferred("_update_graph");
  776. }
  777. bool updating;
  778. Ref<VisualShaderNode> node;
  779. Vector<EditorProperty *> properties;
  780. void setup(Vector<EditorProperty *> p_properties, const Vector<StringName> &p_names, Ref<VisualShaderNode> p_node) {
  781. updating = false;
  782. node = p_node;
  783. properties = p_properties;
  784. for (int i = 0; i < p_properties.size(); i++) {
  785. add_child(p_properties[i]);
  786. properties[i]->connect("property_changed", this, "_property_changed");
  787. properties[i]->set_object_and_property(node.ptr(), p_names[i]);
  788. properties[i]->update_property();
  789. properties[i]->set_name_split_ratio(0);
  790. }
  791. node->connect("changed", this, "_node_changed");
  792. node->connect("editor_refresh_request", this, "_refresh_request", varray(), CONNECT_DEFERRED);
  793. }
  794. static void _bind_methods() {
  795. ClassDB::bind_method("_property_changed", &VisualShaderNodePluginDefaultEditor::_property_changed, DEFVAL(false));
  796. ClassDB::bind_method("_node_changed", &VisualShaderNodePluginDefaultEditor::_node_changed);
  797. ClassDB::bind_method("_refresh_request", &VisualShaderNodePluginDefaultEditor::_refresh_request);
  798. }
  799. };
  800. Control *VisualShaderNodePluginDefault::create_editor(const Ref<VisualShaderNode> &p_node) {
  801. if (p_node->is_class("VisualShaderNodeInput")) {
  802. //create input
  803. VisualShaderNodePluginInputEditor *input_editor = memnew(VisualShaderNodePluginInputEditor);
  804. input_editor->setup(p_node);
  805. return input_editor;
  806. }
  807. Vector<StringName> properties = p_node->get_editable_properties();
  808. if (properties.size() == 0) {
  809. return NULL;
  810. }
  811. List<PropertyInfo> props;
  812. p_node->get_property_list(&props);
  813. Vector<PropertyInfo> pinfo;
  814. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  815. for (int i = 0; i < properties.size(); i++) {
  816. if (E->get().name == String(properties[i])) {
  817. pinfo.push_back(E->get());
  818. }
  819. }
  820. }
  821. if (pinfo.size() == 0)
  822. return NULL;
  823. properties.clear();
  824. Ref<VisualShaderNode> node = p_node;
  825. Vector<EditorProperty *> editors;
  826. for (int i = 0; i < pinfo.size(); i++) {
  827. EditorProperty *prop = EditorInspector::instantiate_property_editor(node.ptr(), pinfo[i].type, pinfo[i].name, pinfo[i].hint, pinfo[i].hint_string, pinfo[i].usage);
  828. if (!prop)
  829. return NULL;
  830. if (Object::cast_to<EditorPropertyResource>(prop)) {
  831. Object::cast_to<EditorPropertyResource>(prop)->set_use_sub_inspector(false);
  832. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  833. } else if (Object::cast_to<EditorPropertyTransform>(prop)) {
  834. prop->set_custom_minimum_size(Size2(250 * EDSCALE, 0));
  835. } else if (Object::cast_to<EditorPropertyFloat>(prop) || Object::cast_to<EditorPropertyVector3>(prop)) {
  836. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  837. } else if (Object::cast_to<EditorPropertyEnum>(prop)) {
  838. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  839. Object::cast_to<EditorPropertyEnum>(prop)->set_option_button_clip(false);
  840. }
  841. editors.push_back(prop);
  842. properties.push_back(pinfo[i].name);
  843. }
  844. VisualShaderNodePluginDefaultEditor *editor = memnew(VisualShaderNodePluginDefaultEditor);
  845. editor->setup(editors, properties, p_node);
  846. return editor;
  847. }
  848. void EditorPropertyShaderMode::_option_selected(int p_which) {
  849. //will not use this, instead will do all the logic setting manually
  850. //emit_signal("property_changed", get_edited_property(), p_which);
  851. Ref<VisualShader> visual_shader(Object::cast_to<VisualShader>(get_edited_object()));
  852. if (visual_shader->get_mode() == p_which)
  853. return;
  854. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  855. undo_redo->create_action("Visual Shader Mode Changed");
  856. //do is easy
  857. undo_redo->add_do_method(visual_shader.ptr(), "set_mode", p_which);
  858. undo_redo->add_undo_method(visual_shader.ptr(), "set_mode", visual_shader->get_mode());
  859. //now undo is hell
  860. //1. restore connections to output
  861. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  862. VisualShader::Type type = VisualShader::Type(i);
  863. List<VisualShader::Connection> conns;
  864. visual_shader->get_node_connections(type, &conns);
  865. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  866. if (E->get().to_node == VisualShader::NODE_ID_OUTPUT) {
  867. 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);
  868. }
  869. }
  870. }
  871. //2. restore input indices
  872. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  873. VisualShader::Type type = VisualShader::Type(i);
  874. Vector<int> nodes = visual_shader->get_node_list(type);
  875. for (int i = 0; i < nodes.size(); i++) {
  876. Ref<VisualShaderNodeInput> input = visual_shader->get_node(type, nodes[i]);
  877. if (!input.is_valid()) {
  878. continue;
  879. }
  880. undo_redo->add_undo_method(input.ptr(), "set_input_name", input->get_input_name());
  881. }
  882. }
  883. //3. restore enums and flags
  884. List<PropertyInfo> props;
  885. visual_shader->get_property_list(&props);
  886. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  887. if (E->get().name.begins_with("flags/") || E->get().name.begins_with("modes/")) {
  888. undo_redo->add_undo_property(visual_shader.ptr(), E->get().name, visual_shader->get(E->get().name));
  889. }
  890. }
  891. //update graph
  892. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  893. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  894. undo_redo->commit_action();
  895. }
  896. void EditorPropertyShaderMode::update_property() {
  897. int which = get_edited_object()->get(get_edited_property());
  898. options->select(which);
  899. }
  900. void EditorPropertyShaderMode::setup(const Vector<String> &p_options) {
  901. for (int i = 0; i < p_options.size(); i++) {
  902. options->add_item(p_options[i], i);
  903. }
  904. }
  905. void EditorPropertyShaderMode::set_option_button_clip(bool p_enable) {
  906. options->set_clip_text(p_enable);
  907. }
  908. void EditorPropertyShaderMode::_bind_methods() {
  909. ClassDB::bind_method(D_METHOD("_option_selected"), &EditorPropertyShaderMode::_option_selected);
  910. }
  911. EditorPropertyShaderMode::EditorPropertyShaderMode() {
  912. options = memnew(OptionButton);
  913. options->set_clip_text(true);
  914. add_child(options);
  915. add_focusable(options);
  916. options->connect("item_selected", this, "_option_selected");
  917. }
  918. bool EditorInspectorShaderModePlugin::can_handle(Object *p_object) {
  919. return true; //can handle everything
  920. }
  921. void EditorInspectorShaderModePlugin::parse_begin(Object *p_object) {
  922. //do none
  923. }
  924. 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) {
  925. if (p_path == "mode" && p_object->is_class("VisualShader") && p_type == Variant::INT) {
  926. EditorPropertyShaderMode *editor = memnew(EditorPropertyShaderMode);
  927. Vector<String> options = p_hint_text.split(",");
  928. editor->setup(options);
  929. add_property_editor(p_path, editor);
  930. return true;
  931. }
  932. return false; //can be overridden, although it will most likely be last anyway
  933. }
  934. void EditorInspectorShaderModePlugin::parse_end() {
  935. //do none
  936. }
  937. //////////////////////////////////
  938. void VisualShaderNodePortPreview::_shader_changed() {
  939. if (shader.is_null()) {
  940. return;
  941. }
  942. Vector<VisualShader::DefaultTextureParam> default_textures;
  943. String shader_code = shader->generate_preview_shader(type, node, port, default_textures);
  944. Ref<Shader> preview_shader;
  945. preview_shader.instance();
  946. preview_shader->set_code(shader_code);
  947. for (int i = 0; i < default_textures.size(); i++) {
  948. preview_shader->set_default_texture_param(default_textures[i].name, default_textures[i].param);
  949. }
  950. Ref<ShaderMaterial> material;
  951. material.instance();
  952. material->set_shader(preview_shader);
  953. //find if a material is also being edited and copy parameters to this one
  954. for (int i = EditorNode::get_singleton()->get_editor_history()->get_path_size() - 1; i >= 0; i--) {
  955. Object *object = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_path_object(i));
  956. if (!object)
  957. continue;
  958. ShaderMaterial *src_mat = Object::cast_to<ShaderMaterial>(object);
  959. if (src_mat && src_mat->get_shader().is_valid()) {
  960. List<PropertyInfo> params;
  961. src_mat->get_shader()->get_param_list(&params);
  962. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  963. material->set(E->get().name, src_mat->get(E->get().name));
  964. }
  965. }
  966. }
  967. set_material(material);
  968. }
  969. void VisualShaderNodePortPreview::setup(const Ref<VisualShader> &p_shader, VisualShader::Type p_type, int p_node, int p_port) {
  970. shader = p_shader;
  971. shader->connect("changed", this, "_shader_changed");
  972. type = p_type;
  973. port = p_port;
  974. node = p_node;
  975. update();
  976. _shader_changed();
  977. }
  978. Size2 VisualShaderNodePortPreview::get_minimum_size() const {
  979. return Size2(100, 100) * EDSCALE;
  980. }
  981. void VisualShaderNodePortPreview::_notification(int p_what) {
  982. if (p_what == NOTIFICATION_DRAW) {
  983. Vector<Vector2> points;
  984. Vector<Vector2> uvs;
  985. Vector<Color> colors;
  986. points.push_back(Vector2());
  987. uvs.push_back(Vector2(0, 0));
  988. colors.push_back(Color(1, 1, 1, 1));
  989. points.push_back(Vector2(get_size().width, 0));
  990. uvs.push_back(Vector2(1, 0));
  991. colors.push_back(Color(1, 1, 1, 1));
  992. points.push_back(get_size());
  993. uvs.push_back(Vector2(1, 1));
  994. colors.push_back(Color(1, 1, 1, 1));
  995. points.push_back(Vector2(0, get_size().height));
  996. uvs.push_back(Vector2(0, 1));
  997. colors.push_back(Color(1, 1, 1, 1));
  998. draw_primitive(points, colors, uvs);
  999. }
  1000. }
  1001. void VisualShaderNodePortPreview::_bind_methods() {
  1002. ClassDB::bind_method("_shader_changed", &VisualShaderNodePortPreview::_shader_changed);
  1003. }
  1004. VisualShaderNodePortPreview::VisualShaderNodePortPreview() {
  1005. }