Editor: Validate var name in BlackboardPlanEditor

This commit is contained in:
Serhii Snitsaruk 2024-01-28 20:14:52 +01:00
parent 7ac1bc2dec
commit 174810fb34
4 changed files with 37 additions and 3 deletions

View File

@ -158,9 +158,16 @@ String BlackboardPlan::get_var_name(const BBVariable &p_var) const {
return String(); 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) { void BlackboardPlan::rename_var(const String &p_name, const String &p_new_name) {
ERR_FAIL_COND(p_new_name.is_empty()); if (p_name == p_new_name) {
ERR_FAIL_COND(var_map.has(p_new_name)); return;
}
ERR_FAIL_COND(!is_valid_var_name(p_new_name));
ERR_FAIL_COND(!var_map.has(p_name)); ERR_FAIL_COND(!var_map.has(p_name));
BBVariable var = var_map[p_name]; BBVariable var = var_map[p_name];

View File

@ -59,6 +59,7 @@ public:
PackedStringArray list_vars() const; PackedStringArray list_vars() const;
String get_var_name(const BBVariable &p_var) 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 rename_var(const String &p_name, const String &p_new_name);
void move_var(int p_index, int p_new_index); void move_var(int p_index, int p_new_index);

View File

@ -40,6 +40,10 @@ using namespace godot;
BlackboardPlanEditor *BlackboardPlanEditor::singleton = nullptr; BlackboardPlanEditor *BlackboardPlanEditor::singleton = nullptr;
LineEdit *BlackboardPlanEditor::_get_name_edit(int p_row_index) const {
return Object::cast_to<LineEdit>(rows_vbox->get_child(p_row_index)->get_child(0)->get_child(1));
}
void BlackboardPlanEditor::_add_var() { void BlackboardPlanEditor::_add_var() {
ERR_FAIL_NULL(plan); 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) { void BlackboardPlanEditor::_rename_var(const String &p_new_name, int p_index) {
ERR_FAIL_NULL(plan); ERR_FAIL_NULL(plan);
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); 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) { void BlackboardPlanEditor::_change_var_type(Variant::Type p_new_type, int p_index) {

View File

@ -24,6 +24,7 @@
#ifdef LIMBOAI_GDEXTENSION #ifdef LIMBOAI_GDEXTENSION
#include <godot_cpp/classes/accept_dialog.hpp> #include <godot_cpp/classes/accept_dialog.hpp>
#include <godot_cpp/classes/editor_inspector_plugin.hpp> #include <godot_cpp/classes/editor_inspector_plugin.hpp>
#include <godot_cpp/classes/line_edit.hpp>
#include <godot_cpp/classes/panel_container.hpp> #include <godot_cpp/classes/panel_container.hpp>
#include <godot_cpp/classes/popup_menu.hpp> #include <godot_cpp/classes/popup_menu.hpp>
#include <godot_cpp/classes/scroll_container.hpp> #include <godot_cpp/classes/scroll_container.hpp>
@ -63,6 +64,8 @@ private:
PopupMenu *type_menu; PopupMenu *type_menu;
PopupMenu *hint_menu; PopupMenu *hint_menu;
LineEdit *_get_name_edit(int p_row_index) const;
void _add_var(); void _add_var();
void _trash_var(int p_index); void _trash_var(int p_index);
void _rename_var(const String &p_new_name, int p_index); void _rename_var(const String &p_new_name, int p_index);