Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-182 The ENTER key releases node input focus #186

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/editor/graph/pins/graph_node_pin_enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ void OrchestratorGraphNodePinEnum::_bind_methods()
{
}

void OrchestratorGraphNodePinEnum::_on_item_selected(int p_index)
void OrchestratorGraphNodePinEnum::_on_item_selected(int p_index, OptionButton* p_button)
{
const String enum_class = _pin->get_target_class();
if (ExtensionDB::get_global_enum_names().has(enum_class))
{
const EnumInfo& ei = ExtensionDB::get_global_enum(enum_class);
_pin->set_default_value(ei.values[p_index].value);
p_button->release_focus();
}
}

Control* OrchestratorGraphNodePinEnum::_get_default_value_widget()
{
OptionButton* button = memnew(OptionButton);
button->connect("item_selected", callable_mp(this, &OrchestratorGraphNodePinEnum::_on_item_selected));
button->connect("item_selected", callable_mp(this, &OrchestratorGraphNodePinEnum::_on_item_selected).bind(button));

const String enum_class = _pin->get_target_class();
if (!enum_class.is_empty() && ExtensionDB::get_global_enum_names().has(enum_class))
Expand Down
9 changes: 8 additions & 1 deletion src/editor/graph/pins/graph_node_pin_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@

#include "editor/graph/graph_node_pin.h"

/// Forward declarations
namespace godot
{
class OptionButton;
}

/// An implementation of OrchestratorGraphNodePin for enum pin types, which renders a
/// drop down selection box for choices.
class OrchestratorGraphNodePinEnum : public OrchestratorGraphNodePin
Expand All @@ -32,7 +38,8 @@ class OrchestratorGraphNodePinEnum : public OrchestratorGraphNodePin

/// Dispatched when the user makes a selection.
/// @param p_index the choice index that was selected
void _on_item_selected(int p_index);
/// @param p_button the button widget
void _on_item_selected(int p_index, OptionButton* p_button);

//~ Begin OrchestratorGraphNodePin Interface
Control* _get_default_value_widget() override;
Expand Down
26 changes: 17 additions & 9 deletions src/editor/graph/pins/graph_node_pin_numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,33 @@ void OrchestratorGraphNodePinNumeric::_bind_methods()
{
}

void OrchestratorGraphNodePinNumeric::_on_default_value_changed(const String& p_new_value)
bool OrchestratorGraphNodePinNumeric::_set_default_value(const String& p_value)
{
switch (_pin->get_type())
{
case Variant::INT:
_pin->set_default_value(p_new_value.to_int());
break;
_pin->set_default_value(p_value.to_int());
return true;

case Variant::FLOAT:
_pin->set_default_value(p_new_value.to_float());
break;
_pin->set_default_value(p_value.to_float());
return true;

default:
ERR_PRINT("Cannot set default value for an unknown numeric pin type");
break;
return false;
}
}

void OrchestratorGraphNodePinNumeric::_on_focus_lost(LineEdit* p_line_edit)
void OrchestratorGraphNodePinNumeric::_on_text_submitted(const String& p_value, LineEdit* p_line_edit)
{
if (_set_default_value(p_value) && p_line_edit)
p_line_edit->release_focus();
}

void OrchestratorGraphNodePinNumeric::_on_focus_lost(const LineEdit* p_line_edit)
{
_on_default_value_changed(p_line_edit->get_text());
_set_default_value(p_line_edit->get_text());
}

Control* OrchestratorGraphNodePinNumeric::_get_default_value_widget()
Expand All @@ -56,7 +64,7 @@ Control* OrchestratorGraphNodePinNumeric::_get_default_value_widget()
line_edit->set_text(_pin->get_effective_default_value());
line_edit->add_theme_constant_override("minimum_character_width", 0);
line_edit->set_select_all_on_focus(true);
line_edit->connect("text_submitted", callable_mp(this, &OrchestratorGraphNodePinNumeric::_on_default_value_changed));
line_edit->connect("text_submitted", callable_mp(this, &OrchestratorGraphNodePinNumeric::_on_text_submitted).bind(line_edit));
line_edit->connect("focus_exited", callable_mp(this, &OrchestratorGraphNodePinNumeric::_on_focus_lost).bind(line_edit));
return line_edit;
}
14 changes: 10 additions & 4 deletions src/editor/graph/pins/graph_node_pin_numeric.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ class OrchestratorGraphNodePinNumeric : public OrchestratorGraphNodePin
protected:
OrchestratorGraphNodePinNumeric() = default;

/// Called when the default value is changed in the UI.
/// @param p_new_value the new default value
void _on_default_value_changed(const String& p_new_value);
/// Sets the default value.
/// @param p_value the new default value
/// @return true if the default value was set; false otherwise
bool _set_default_value(const String& p_value);

/// Called when the user hits "ENTER" in the line edit widget.
/// @param p_value the submitted value
/// @param p_line_edit the line edit widget
void _on_text_submitted(const String& p_value, LineEdit* p_line_edit);

/// Called when focus is lost on the line edit widget.
/// @param p_line_edit the line edit widget
void _on_focus_lost(LineEdit* p_line_edit);
void _on_focus_lost(const LineEdit* p_line_edit);

//~ Begin OrchestratorGraphNodePin Interface
Control* _get_default_value_widget() override;
Expand Down
49 changes: 29 additions & 20 deletions src/editor/graph/pins/graph_node_pin_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,30 @@ void OrchestratorGraphNodePinString::_bind_methods()
{
}

void OrchestratorGraphNodePinString::_on_default_value_changed(const String& p_new_value)
void OrchestratorGraphNodePinString::_set_default_value(const String& p_value)
{
_pin->set_default_value(p_new_value);
_pin->set_default_value(p_value);
}

void OrchestratorGraphNodePinString::_on_text_edit_changed(Control* p_control)
void OrchestratorGraphNodePinString::_on_text_changed(TextEdit* p_text_edit)
{
_pin->set_default_value(Object::cast_to<TextEdit>(p_control)->get_text());
if (p_text_edit)
_set_default_value(p_text_edit->get_text());
}

void OrchestratorGraphNodePinString::_on_text_submitted(const String& p_value, LineEdit* p_line_edit)
{
if (p_line_edit)
{
_set_default_value(p_line_edit->get_text());
p_line_edit->release_focus();
}
}

void OrchestratorGraphNodePinString::_on_focus_lost(LineEdit* p_line_edit)
{
_on_default_value_changed(p_line_edit->get_text());
if (p_line_edit)
_set_default_value(p_line_edit->get_text());
}

Control* OrchestratorGraphNodePinString::_get_default_value_widget()
Expand All @@ -58,23 +69,21 @@ Control* OrchestratorGraphNodePinString::_get_default_value_widget()
text_edit->set_line_wrapping_mode(TextEdit::LINE_WRAPPING_BOUNDARY);
text_edit->set_fit_content_height_enabled(true);
text_edit->connect("text_changed",
callable_mp(this, &OrchestratorGraphNodePinString::_on_text_edit_changed).bind(text_edit));
callable_mp(this, &OrchestratorGraphNodePinString::_on_text_changed).bind(text_edit));
return text_edit;
}
else
{
LineEdit* line_edit = memnew(LineEdit);
line_edit->set_custom_minimum_size(Vector2(30, 0));
line_edit->set_expand_to_text_length_enabled(true);
line_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
line_edit->set_text(_pin->get_effective_default_value());
line_edit->set_select_all_on_focus(true);
line_edit->connect("text_submitted",
callable_mp(this, &OrchestratorGraphNodePinString::_on_default_value_changed));
line_edit->connect("focus_exited",
callable_mp(this, &OrchestratorGraphNodePinString::_on_focus_lost).bind(line_edit));
return line_edit;
}

LineEdit* line_edit = memnew(LineEdit);
line_edit->set_custom_minimum_size(Vector2(30, 0));
line_edit->set_expand_to_text_length_enabled(true);
line_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
line_edit->set_text(_pin->get_effective_default_value());
line_edit->set_select_all_on_focus(true);
line_edit->connect("text_submitted",
callable_mp(this, &OrchestratorGraphNodePinString::_on_text_submitted).bind(line_edit));
line_edit->connect("focus_exited",
callable_mp(this, &OrchestratorGraphNodePinString::_on_focus_lost).bind(line_edit));
return line_edit;
}

bool OrchestratorGraphNodePinString::_render_default_value_below_label() const
Expand Down
17 changes: 13 additions & 4 deletions src/editor/graph/pins/graph_node_pin_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace godot
{
class LineEdit;
class TextEdit;
}

/// An implementation of OrchestratorGraphNodePin for types that want to represent their default values
Expand All @@ -35,10 +36,18 @@ class OrchestratorGraphNodePinString : public OrchestratorGraphNodePin
protected:
OrchestratorGraphNodePinString() = default;

/// Called when the default value is changed in the UI.
/// @param p_new_value the new default value
void _on_default_value_changed(const String& p_new_value);
void _on_text_edit_changed(Control* p_control);
/// Sets the default value on the pin
/// @param p_value the default value to set
void _set_default_value(const String& p_value);

/// Called when the text edit's (multi-line text) text changes
/// @param p_text_edit the text edit widget
void _on_text_changed(TextEdit* p_text_edit);

/// Called when the line edit's text submitted handler.
/// @param p_value the text value submitted
/// @param p_line_edit the line edit widget
void _on_text_submitted(const String& p_value, LineEdit* p_line_edit);

/// Called when focus is lost on the line edit widget.
/// @param p_line_edit the line edit widget
Expand Down
17 changes: 14 additions & 3 deletions src/editor/graph/pins/graph_node_pin_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,18 @@ void OrchestratorGraphNodePinStruct::_on_focus_entered(int p_index)
_edits[p_index]->call_deferred("select_all");
}

void OrchestratorGraphNodePinStruct::_on_default_value_changed()
void OrchestratorGraphNodePinStruct::_on_focus_exited(int p_index)
{
if (p_index >= 0 && p_index < _edits.size())
_set_default_value_from_line_edits();
}

void OrchestratorGraphNodePinStruct::_on_text_submitted()
{
_set_default_value_from_line_edits();
}

void OrchestratorGraphNodePinStruct::_set_default_value_from_line_edits()
{
// this works if the sub-parts are variant based types, i.e. floats
// what about when there are sub-component types, i.e. basis or transform2d
Expand Down Expand Up @@ -184,8 +195,8 @@ Control* OrchestratorGraphNodePinStruct::_get_default_value_widget()
line_edit->set_expand_to_text_length_enabled(true);
line_edit->add_theme_constant_override("minimum_character_width", 0);
line_edit->connect("focus_entered", callable_mp(this, &OrchestratorGraphNodePinStruct::_on_focus_entered).bind(i));
line_edit->connect("focus_exited", callable_mp(this, &OrchestratorGraphNodePinStruct::_on_default_value_changed));
line_edit->connect("text_submitted", callable_mp(this, &OrchestratorGraphNodePinStruct::_on_default_value_changed));
line_edit->connect("focus_exited", callable_mp(this, &OrchestratorGraphNodePinStruct::_on_focus_exited).bind(i));
line_edit->connect("text_submitted", callable_mp(this, &OrchestratorGraphNodePinStruct::_on_text_submitted));
container->add_child(line_edit);

_edits.push_back(line_edit);
Expand Down
12 changes: 10 additions & 2 deletions src/editor/graph/pins/graph_node_pin_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,18 @@ class OrchestratorGraphNodePinStruct : public OrchestratorGraphNodePin
//~ End OrchestratorGraphNodePin Interface

/// Dispatched when the edit control receives focus
/// @param p_index the line edit control index
void _on_focus_entered(int p_index);

/// Dispatched when the default value changed
void _on_default_value_changed();
/// Dispatched when the edit control looses focus
/// @param p_index the line edit control index
void _on_focus_exited(int p_index);

/// Dispatched when the text is submitted in a field
void _on_text_submitted();

/// Sets the default value from the all collective line edit controls
void _set_default_value_from_line_edits();

public:
/// Constructs the pin object
Expand Down
Loading