From 174810fb340e57bfb68ffe32703fec65a171a6dd Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Sun, 28 Jan 2024 20:14:52 +0100 Subject: [PATCH] Editor: Validate var name in BlackboardPlanEditor --- blackboard/blackboard_plan.cpp | 11 +++++++++-- blackboard/blackboard_plan.h | 1 + editor/blackboard_plan_editor.cpp | 25 ++++++++++++++++++++++++- editor/blackboard_plan_editor.h | 3 +++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/blackboard/blackboard_plan.cpp b/blackboard/blackboard_plan.cpp index a52cb6c..ef84fe7 100644 --- a/blackboard/blackboard_plan.cpp +++ b/blackboard/blackboard_plan.cpp @@ -158,9 +158,16 @@ String BlackboardPlan::get_var_name(const BBVariable &p_var) const { return String(); } +bool BlackboardPlan::is_valid_var_name(const String &p_name) const { + return p_name.is_valid_identifier() && !var_map.has(p_name); +} + void BlackboardPlan::rename_var(const String &p_name, const String &p_new_name) { - ERR_FAIL_COND(p_new_name.is_empty()); - ERR_FAIL_COND(var_map.has(p_new_name)); + if (p_name == p_new_name) { + return; + } + + ERR_FAIL_COND(!is_valid_var_name(p_new_name)); ERR_FAIL_COND(!var_map.has(p_name)); BBVariable var = var_map[p_name]; diff --git a/blackboard/blackboard_plan.h b/blackboard/blackboard_plan.h index e5a315a..4b6e064 100644 --- a/blackboard/blackboard_plan.h +++ b/blackboard/blackboard_plan.h @@ -59,6 +59,7 @@ public: PackedStringArray list_vars() const; String get_var_name(const BBVariable &p_var) const; + bool is_valid_var_name(const String &p_name) const; void rename_var(const String &p_name, const String &p_new_name); void move_var(int p_index, int p_new_index); diff --git a/editor/blackboard_plan_editor.cpp b/editor/blackboard_plan_editor.cpp index dff457b..700ceaf 100644 --- a/editor/blackboard_plan_editor.cpp +++ b/editor/blackboard_plan_editor.cpp @@ -40,6 +40,10 @@ using namespace godot; BlackboardPlanEditor *BlackboardPlanEditor::singleton = nullptr; +LineEdit *BlackboardPlanEditor::_get_name_edit(int p_row_index) const { + return Object::cast_to(rows_vbox->get_child(p_row_index)->get_child(0)->get_child(1)); +} + void BlackboardPlanEditor::_add_var() { ERR_FAIL_NULL(plan); @@ -64,7 +68,26 @@ void BlackboardPlanEditor::_trash_var(int p_index) { void BlackboardPlanEditor::_rename_var(const String &p_new_name, int p_index) { ERR_FAIL_NULL(plan); - plan->rename_var(plan->get_var_by_index(p_index).first, p_new_name); + + LineEdit *name_edit = _get_name_edit(p_index); + ERR_FAIL_NULL(name_edit); + + bool is_valid_var_name = plan->is_valid_var_name(p_new_name); + if (is_valid_var_name) { + plan->rename_var(plan->get_var_by_index(p_index).first, p_new_name); + } + + if (is_valid_var_name || plan->get_var_by_index(p_index).first == p_new_name) { + if (name_edit->has_theme_color_override(LW_NAME(font_color))) { + name_edit->remove_theme_color_override(LW_NAME(font_color)); + name_edit->queue_redraw(); + } + } else { + if (!name_edit->has_theme_color_override(LW_NAME(font_color))) { + name_edit->add_theme_color_override(LW_NAME(font_color), Color(1, 0, 0)); + name_edit->queue_redraw(); + } + } } void BlackboardPlanEditor::_change_var_type(Variant::Type p_new_type, int p_index) { diff --git a/editor/blackboard_plan_editor.h b/editor/blackboard_plan_editor.h index 619906f..3c3672c 100644 --- a/editor/blackboard_plan_editor.h +++ b/editor/blackboard_plan_editor.h @@ -24,6 +24,7 @@ #ifdef LIMBOAI_GDEXTENSION #include #include +#include #include #include #include @@ -63,6 +64,8 @@ private: PopupMenu *type_menu; PopupMenu *hint_menu; + LineEdit *_get_name_edit(int p_row_index) const; + void _add_var(); void _trash_var(int p_index); void _rename_var(const String &p_new_name, int p_index);