Skip to content

Commit

Permalink
GH-605 Improve numeric default value widget validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Jul 28, 2024
1 parent 01d9c03 commit b416fcd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 45 deletions.
74 changes: 42 additions & 32 deletions src/editor/graph/pins/graph_node_pin_numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,65 @@
//
#include "graph_node_pin_numeric.h"

#include <godot_cpp/classes/line_edit.hpp>

OrchestratorGraphNodePinNumeric::OrchestratorGraphNodePinNumeric(OrchestratorGraphNode* p_node, const Ref<OScriptNodePin>& p_pin)
: OrchestratorGraphNodePin(p_node, p_pin)
{
}

void OrchestratorGraphNodePinNumeric::_bind_methods()
{
}

bool OrchestratorGraphNodePinNumeric::_set_default_value(const String& p_value)
{
switch (_pin->get_type())
if (_pin->get_type() == Variant::INT)
{
case Variant::INT:
// We allow float to coerce to int
if (p_value.is_valid_int() || p_value.is_valid_float())
{
_pin->set_default_value(p_value.to_int());
_line_edit->set_text(_pin->get_effective_default_value());
return true;
}
}

case Variant::FLOAT:
if (_pin->get_type() == Variant::FLOAT)
{
if (p_value.is_valid_float())
{
_pin->set_default_value(p_value.to_float());
_line_edit->set_text(_pin->get_effective_default_value());
return true;

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

_line_edit->set_text(_pin->get_effective_default_value());
_line_edit->call_deferred("grab_focus");
_line_edit->call_deferred("select_all");
return false;
}

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

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

Control* OrchestratorGraphNodePinNumeric::_get_default_value_widget()
{
LineEdit* line_edit = memnew(LineEdit);
line_edit->set_expand_to_text_length_enabled(true);
line_edit->set_h_size_flags(Control::SIZE_EXPAND);
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_text_submitted).bind(line_edit));
line_edit->connect("focus_exited", callable_mp(this, &OrchestratorGraphNodePinNumeric::_on_focus_lost).bind(line_edit));
return line_edit;
_line_edit = memnew(LineEdit);
_line_edit->set_expand_to_text_length_enabled(true);
_line_edit->set_h_size_flags(SIZE_EXPAND);
_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_text_submitted));
_line_edit->connect("focus_exited", callable_mp(this, &OrchestratorGraphNodePinNumeric::_on_focus_lost));
return _line_edit;
}

void OrchestratorGraphNodePinNumeric::_bind_methods()
{
}

OrchestratorGraphNodePinNumeric::OrchestratorGraphNodePinNumeric(OrchestratorGraphNode* p_node, const Ref<OScriptNodePin>& p_pin)
: OrchestratorGraphNodePin(p_node, p_pin)
{
}

26 changes: 13 additions & 13 deletions src/editor/graph/pins/graph_node_pin_numeric.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

#include "editor/graph/graph_node_pin.h"

namespace godot
{
class LineEdit;
}
#include <godot_cpp/classes/line_edit.hpp>

/// An implementation of OrchestratorGraphNodePin for types that want to represent their default values
/// using a string-based text field for numeric data entry.
Expand All @@ -33,7 +30,11 @@ class OrchestratorGraphNodePinNumeric : public OrchestratorGraphNodePin
static void _bind_methods();

protected:
OrchestratorGraphNodePinNumeric() = default;
LineEdit* _line_edit{ nullptr }; //! The line edit

//~ Begin OrchestratorGraphNodePin Interface
Control* _get_default_value_widget() override;
//~ End OrchestratorGraphNodePin Interface

/// Sets the default value.
/// @param p_value the new default value
Expand All @@ -42,20 +43,19 @@ class OrchestratorGraphNodePinNumeric : public OrchestratorGraphNodePin

/// 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);
void _on_text_submitted(const String& p_value);

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

//~ Begin OrchestratorGraphNodePin Interface
Control* _get_default_value_widget() override;
//~ End OrchestratorGraphNodePin Interface
/// Default constructor
OrchestratorGraphNodePinNumeric() = default;

public:
/// Construct the numeric pin
/// @param p_node the graph node
/// @param p_pin the script pin
OrchestratorGraphNodePinNumeric(OrchestratorGraphNode* p_node, const Ref<OScriptNodePin>& p_pin);

};

#endif // ORCHESTRATOR_GRAPH_NODE_PIN_NUMERIC_H

0 comments on commit b416fcd

Please sign in to comment.