/**************************************************************************/ /* visual_shader_editor_plugin.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #include "visual_shader_editor_plugin.h" #include "core/config/project_settings.h" #include "core/io/resource_loader.h" #include "core/math/math_defs.h" #include "core/os/keyboard.h" #include "editor/editor_node.h" #include "editor/editor_properties.h" #include "editor/editor_properties_vector.h" #include "editor/editor_settings.h" #include "editor/editor_string_names.h" #include "editor/editor_undo_redo_manager.h" #include "editor/filesystem_dock.h" #include "editor/inspector_dock.h" #include "editor/plugins/curve_editor_plugin.h" #include "editor/plugins/shader_editor_plugin.h" #include "editor/themes/editor_scale.h" #include "scene/animation/tween.h" #include "scene/gui/button.h" #include "scene/gui/check_box.h" #include "scene/gui/code_edit.h" #include "scene/gui/color_picker.h" #include "scene/gui/graph_edit.h" #include "scene/gui/menu_button.h" #include "scene/gui/option_button.h" #include "scene/gui/popup.h" #include "scene/gui/rich_text_label.h" #include "scene/gui/separator.h" #include "scene/gui/tree.h" #include "scene/gui/view_panner.h" #include "scene/main/window.h" #include "scene/resources/curve_texture.h" #include "scene/resources/style_box_flat.h" #include "scene/resources/visual_shader_nodes.h" #include "scene/resources/visual_shader_particle_nodes.h" #include "servers/display_server.h" #include "servers/rendering/shader_preprocessor.h" #include "servers/rendering/shader_types.h" struct FloatConstantDef { String name; float value = 0; String desc; }; static FloatConstantDef float_constant_defs[] = { { "E", Math_E, TTR("E constant (2.718282). Represents the base of the natural logarithm.") }, { "Epsilon", CMP_EPSILON, TTR("Epsilon constant (0.00001). Smallest possible scalar number.") }, { "Phi", 1.618034f, TTR("Phi constant (1.618034). Golden ratio.") }, { "Pi/4", Math_PI / 4, TTR("Pi/4 constant (0.785398) or 45 degrees.") }, { "Pi/2", Math_PI / 2, TTR("Pi/2 constant (1.570796) or 90 degrees.") }, { "Pi", Math_PI, TTR("Pi constant (3.141593) or 180 degrees.") }, { "Tau", Math_TAU, TTR("Tau constant (6.283185) or 360 degrees.") }, { "Sqrt2", Math_SQRT2, TTR("Sqrt2 constant (1.414214). Square root of 2.") } }; const int MAX_FLOAT_CONST_DEFS = sizeof(float_constant_defs) / sizeof(FloatConstantDef); /////////////////// void VisualShaderNodePlugin::set_editor(VisualShaderEditor *p_editor) { vseditor = p_editor; } Control *VisualShaderNodePlugin::create_editor(const Ref &p_parent_resource, const Ref &p_node) { Object *ret = nullptr; GDVIRTUAL_CALL(_create_editor, p_parent_resource, p_node, ret); return Object::cast_to(ret); } void VisualShaderNodePlugin::_bind_methods() { GDVIRTUAL_BIND(_create_editor, "parent_resource", "visual_shader_node"); } /////////////////// void VSGraphNode::_draw_port(int p_slot_index, Point2i p_pos, bool p_left, const Color &p_color, const Color &p_rim_color) { Ref port_icon = p_left ? get_slot_custom_icon_left(p_slot_index) : get_slot_custom_icon_right(p_slot_index); Point2 icon_offset; if (!port_icon.is_valid()) { port_icon = get_theme_icon(SNAME("port"), SNAME("GraphNode")); } icon_offset = -port_icon->get_size() * 0.5; // Draw "shadow"/outline in the connection rim color. draw_texture_rect(port_icon, Rect2(p_pos + (icon_offset - Size2(2, 2)) * EDSCALE, (port_icon->get_size() + Size2(4, 4)) * EDSCALE), false, p_rim_color); draw_texture_rect(port_icon, Rect2(p_pos + icon_offset * EDSCALE, port_icon->get_size() * EDSCALE), false, p_color); } void VSGraphNode::draw_port(int p_slot_index, Point2i p_pos, bool p_left, const Color &p_color) { Color rim_color = get_theme_color(SNAME("connection_rim_color"), SNAME("GraphEdit")); _draw_port(p_slot_index, p_pos, p_left, p_color, rim_color); } /////////////////// void VSRerouteNode::_notification(int p_what) { switch (p_what) { case NOTIFICATION_READY: { connect(SceneStringName(mouse_entered), callable_mp(this, &VSRerouteNode::_on_mouse_entered)); connect(SceneStringName(mouse_exited), callable_mp(this, &VSRerouteNode::_on_mouse_exited)); } break; case NOTIFICATION_DRAW: { Vector2 offset = Vector2(0, -16 * EDSCALE); Color drag_bg_color = get_theme_color(SNAME("drag_background"), SNAME("VSRerouteNode")); draw_circle(get_size() * 0.5 + offset, 16 * EDSCALE, Color(drag_bg_color, selected ? 1 : icon_opacity), true, -1, true); Ref icon = get_editor_theme_icon(SNAME("ToolMove")); Point2 icon_offset = -icon->get_size() * 0.5 + get_size() * 0.5 + offset; draw_texture(icon, icon_offset, Color(1, 1, 1, selected ? 1 : icon_opacity)); } break; } } void VSRerouteNode::draw_port(int p_slot_index, Point2i p_pos, bool p_left, const Color &p_color) { Color rim_color = selected ? get_theme_color("selected_rim_color", "VSRerouteNode") : get_theme_color("connection_rim_color", "GraphEdit"); _draw_port(p_slot_index, p_pos, p_left, p_color, rim_color); } VSRerouteNode::VSRerouteNode() { Label *title_lbl = Object::cast_to