Skip to content

Commit

Permalink
[3.x] Add hints and default values to the uniform nodes in visual shader
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaosus committed Jan 3, 2022
1 parent ccbfe85 commit 5b0fe78
Show file tree
Hide file tree
Showing 10 changed files with 577 additions and 30 deletions.
8 changes: 8 additions & 0 deletions doc/classes/VisualShaderNodeBooleanUniform.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
</tutorials>
<methods>
</methods>
<members>
<member name="default_value" type="bool" setter="set_default_value" getter="get_default_value" default="false">
A default value to be assigned within the shader.
</member>
<member name="default_value_enabled" type="bool" setter="set_default_value_enabled" getter="is_default_value_enabled" default="false">
Enables usage of the [member default_value].
</member>
</members>
<constants>
</constants>
</class>
8 changes: 8 additions & 0 deletions doc/classes/VisualShaderNodeColorUniform.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
</tutorials>
<methods>
</methods>
<members>
<member name="default_value" type="Color" setter="set_default_value" getter="get_default_value" default="Color( 1, 1, 1, 1 )">
A default value to be assigned within the shader.
</member>
<member name="default_value_enabled" type="bool" setter="set_default_value_enabled" getter="is_default_value_enabled" default="false">
Enables usage of the [member default_value].
</member>
</members>
<constants>
</constants>
</class>
32 changes: 32 additions & 0 deletions doc/classes/VisualShaderNodeScalarUniform.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@
</tutorials>
<methods>
</methods>
<members>
<member name="default_value" type="float" setter="set_default_value" getter="get_default_value" default="0.0">
A default value to be assigned within the shader.
</member>
<member name="default_value_enabled" type="bool" setter="set_default_value_enabled" getter="is_default_value_enabled" default="false">
Enables usage of the [member default_value].
</member>
<member name="hint" type="int" setter="set_hint" getter="get_hint" enum="VisualShaderNodeScalarUniform.Hint" default="0">
A hint applied to the uniform, which controls the values it can take when set through the inspector.
</member>
<member name="max" type="float" setter="set_max" getter="get_max" default="1.0">
Minimum value for range hints. Used if [member hint] is set to [constant HINT_RANGE] or [constant HINT_RANGE_STEP].
</member>
<member name="min" type="float" setter="set_min" getter="get_min" default="0.0">
Maximum value for range hints. Used if [member hint] is set to [constant HINT_RANGE] or [constant HINT_RANGE_STEP].
</member>
<member name="step" type="float" setter="set_step" getter="get_step" default="0.1">
Step (increment) value for the range hint with step. Used if [member hint] is set to [constant HINT_RANGE_STEP].
</member>
</members>
<constants>
<constant name="HINT_NONE" value="0" enum="Hint">
No hint used.
</constant>
<constant name="HINT_RANGE" value="1" enum="Hint">
A range hint for scalar value, which limits possible input values between [member min] and [member max]. Translated to [code]hint_range(min, max)[/code] in shader code.
</constant>
<constant name="HINT_RANGE_STEP" value="2" enum="Hint">
A range hint for scalar value with step, which limits possible input values between [member min] and [member max], with a step (increment) of [member step]). Translated to [code]hint_range(min, max, step)[/code] in shader code.
</constant>
<constant name="HINT_MAX" value="3" enum="Hint">
Represents the size of the [enum Hint] enum.
</constant>
</constants>
</class>
8 changes: 8 additions & 0 deletions doc/classes/VisualShaderNodeTransformUniform.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
</tutorials>
<methods>
</methods>
<members>
<member name="default_value" type="Transform" setter="set_default_value" getter="get_default_value" default="Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 )">
A default value to be assigned within the shader.
</member>
<member name="default_value_enabled" type="bool" setter="set_default_value_enabled" getter="is_default_value_enabled" default="false">
Enables usage of the [member default_value].
</member>
</members>
<constants>
</constants>
</class>
8 changes: 8 additions & 0 deletions doc/classes/VisualShaderNodeVec3Uniform.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
</tutorials>
<methods>
</methods>
<members>
<member name="default_value" type="Vector3" setter="set_default_value" getter="get_default_value" default="Vector3( 0, 0, 0 )">
A default value to be assigned within the shader.
</member>
<member name="default_value_enabled" type="bool" setter="set_default_value_enabled" getter="is_default_value_enabled" default="false">
Enables usage of the [member default_value].
</member>
</members>
<constants>
</constants>
</class>
79 changes: 54 additions & 25 deletions editor/plugins/visual_shader_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,21 +589,25 @@ void VisualShaderEditor::_update_graph() {
}

Ref<VisualShaderNodeUniform> uniform = vsnode;
HBoxContainer *uniform_hbox = nullptr;

if (uniform.is_valid()) {
graph->add_child(node);
_update_created_node(node);

LineEdit *uniform_name = memnew(LineEdit);
uniform_name->set_h_size_flags(SIZE_EXPAND_FILL);
uniform_name->set_text(uniform->get_uniform_name());
node->add_child(uniform_name);
uniform_name->connect("text_entered", this, "_line_edit_changed", varray(uniform_name, nodes[n_i]));
uniform_name->connect("focus_exited", this, "_line_edit_focus_out", varray(uniform_name, nodes[n_i]));

if (vsnode->get_input_port_count() == 0 && vsnode->get_output_port_count() == 1 && vsnode->get_output_port_name(0) == "") {
//shortcut
VisualShaderNode::PortType port_right = vsnode->get_output_port_type(0);
node->set_slot(0, false, VisualShaderNode::PORT_TYPE_SCALAR, Color(), true, port_right, type_color[port_right]);
continue;
if (vsnode->get_output_port_count() == 1 && vsnode->get_output_port_name(0) == "") {
uniform_hbox = memnew(HBoxContainer);
uniform_hbox->add_constant_override("separation", 7 * EDSCALE);
uniform_hbox->add_child(uniform_name);
node->add_child(uniform_hbox);
} else {
node->add_child(uniform_name);
}
port_offset++;
}
Expand All @@ -615,12 +619,14 @@ void VisualShaderEditor::_update_graph() {
}
}

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) == "")) {
//will be embedded in first port
} else if (custom_editor) {
port_offset++;
node->add_child(custom_editor);
custom_editor = nullptr;
if (custom_editor) {
if (uniform_hbox == nullptr && vsnode->get_output_port_count() > 0 && vsnode->get_output_port_name(0) == "" && (vsnode->get_input_port_count() == 0 || vsnode->get_input_port_name(0) == "")) {
//will be embedded in first port
} else {
port_offset++;
node->add_child(custom_editor);
custom_editor = nullptr;
}
}

if (is_group) {
Expand Down Expand Up @@ -675,8 +681,15 @@ void VisualShaderEditor::_update_graph() {
port_right = vsnode->get_output_port_type(i);
}

HBoxContainer *hb = memnew(HBoxContainer);
hb->add_constant_override("separation", 7 * EDSCALE);
HBoxContainer *hb = nullptr;
bool is_uniform_hbox = false;
if (i == 0 && uniform_hbox != nullptr) {
hb = uniform_hbox;
is_uniform_hbox = true;
} else {
hb = memnew(HBoxContainer);
hb->add_constant_override("separation", 7 * EDSCALE);
}

Variant default_value;

Expand Down Expand Up @@ -756,7 +769,7 @@ void VisualShaderEditor::_update_graph() {
}
}

if (!is_group) {
if (!is_group && !is_uniform_hbox) {
hb->add_spacer();
}

Expand Down Expand Up @@ -786,10 +799,12 @@ void VisualShaderEditor::_update_graph() {
type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
type_box->connect("item_selected", this, "_change_output_port_type", varray(nodes[n_i], i), CONNECT_DEFERRED);
} else {
Label *label = memnew(Label);
label->set_text(name_right);
label->add_style_override("normal", label_style); //more compact
hb->add_child(label);
if (name_right != "") {
Label *label = memnew(Label);
label->set_text(name_right);
label->add_style_override("normal", label_style); //more compact
hb->add_child(label);
}
}
}
}
Expand All @@ -816,9 +831,12 @@ void VisualShaderEditor::_update_graph() {
port_offset++;
}

node->add_child(hb);

node->set_slot(i + port_offset, valid_left, port_left, type_color[port_left], valid_right, port_right, type_color[port_right]);
int idx = 0;
if (!is_uniform_hbox) {
node->add_child(hb);
idx = i + port_offset;
}
node->set_slot(idx, valid_left, port_left, type_color[port_left], valid_right, port_right, type_color[port_right]);
}

if (vsnode->get_output_port_for_preview() >= 0) {
Expand Down Expand Up @@ -3046,9 +3064,9 @@ class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
} else {
undo_redo->add_undo_method(this, "_open_inspector", (RES)parent_resource.ptr());
}
undo_redo->add_do_method(this, "_refresh_request");
undo_redo->add_undo_method(this, "_refresh_request");
}
undo_redo->add_do_method(this, "_refresh_request");
undo_redo->add_undo_method(this, "_refresh_request");
undo_redo->commit_action();

updating = false;
Expand Down Expand Up @@ -3086,7 +3104,18 @@ class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
properties = p_properties;

for (int i = 0; i < p_properties.size(); i++) {
add_child(p_properties[i]);
HBoxContainer *hbox = memnew(HBoxContainer);
hbox->set_h_size_flags(SIZE_EXPAND_FILL);
add_child(hbox);

if (p_node->is_show_prop_names()) {
Label *prop_name = memnew(Label);
prop_name->set_text(String(p_names[i]).capitalize() + ":");
hbox->add_child(prop_name);
}

p_properties[i]->set_h_size_flags(SIZE_EXPAND_FILL);
hbox->add_child(p_properties[i]);

bool res_prop = Object::cast_to<EditorPropertyResource>(p_properties[i]);
if (res_prop) {
Expand Down
8 changes: 8 additions & 0 deletions scene/resources/visual_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ bool VisualShaderNode::is_code_generated() const {
return true;
}

bool VisualShaderNode::is_show_prop_names() const {
return false;
}

Vector<VisualShader::DefaultTextureParam> VisualShaderNode::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
return Vector<VisualShader::DefaultTextureParam>();
}
Expand Down Expand Up @@ -2334,6 +2338,10 @@ void VisualShaderNodeUniform::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::STRING, "uniform_name"), "set_uniform_name", "get_uniform_name");
}

bool VisualShaderNodeUniform::is_show_prop_names() const {
return true;
}

VisualShaderNodeUniform::VisualShaderNodeUniform() {
}

Expand Down
3 changes: 3 additions & 0 deletions scene/resources/visual_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class VisualShaderNode : public Resource {
virtual bool is_generate_input_var(int p_port) const;

virtual bool is_code_generated() const;
virtual bool is_show_prop_names() const;

virtual Vector<StringName> get_editable_properties() const;

Expand Down Expand Up @@ -388,6 +389,8 @@ class VisualShaderNodeUniform : public VisualShaderNode {
void set_global_code_generated(bool p_enabled);
bool is_global_code_generated() const;

virtual bool is_show_prop_names() const;

void set_uniform_name(const String &p_name);
String get_uniform_name() const;

Expand Down
Loading

0 comments on commit 5b0fe78

Please sign in to comment.