Add support for variant param editing and fix bug with ModeSwitchButton

This commit is contained in:
Serhii Snitsaruk 2023-12-10 14:35:28 +01:00
parent 7bce63fbcb
commit 106abe0b5a
5 changed files with 63 additions and 12 deletions

View File

@ -25,10 +25,10 @@ private:
protected:
static void _bind_methods();
public:
virtual Variant::Type get_type() const override;
void set_type(Variant::Type p_type);
public:
BBVariant();
};

View File

@ -11,11 +11,14 @@
#include "editor_property_bb_param.h"
#include "core/variant/variant.h"
#include "modules/limboai/blackboard/bb_param/bb_param.h"
#include "modules/limboai/blackboard/bb_param/bb_variant.h"
#include "modules/limboai/editor/mode_switch_button.h"
#include "core/error/error_macros.h"
#include "core/object/class_db.h"
#include "core/object/ref_counted.h"
#include "core/os/memory.h"
#include "core/string/print_string.h"
#include "editor/editor_inspector.h"
@ -27,6 +30,7 @@
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/line_edit.h"
#include "scene/gui/menu_button.h"
Ref<BBParam> EditorPropertyBBParam::_get_edited_param() {
Ref<BBParam> param = get_edited_property_value();
@ -47,6 +51,9 @@ void EditorPropertyBBParam::_create_value_editor(Variant::Type p_type) {
}
switch (p_type) {
case Variant::NIL: {
value_editor = memnew(EditorPropertyNil);
} break;
case Variant::BOOL: {
value_editor = memnew(EditorPropertyCheck);
} break;
@ -186,7 +193,7 @@ void EditorPropertyBBParam::_create_value_editor(Variant::Type p_type) {
}
}
value_editor->set_name_split_ratio(0.0);
hbox->add_child(value_editor);
editor_hbox->add_child(value_editor);
value_editor->set_h_size_flags(SIZE_EXPAND_FILL);
value_editor->set_meta(SNAME("_param_type"), p_type);
value_editor->connect(SNAME("property_changed"), callable_mp(this, &EditorPropertyBBParam::_value_edited));
@ -194,7 +201,7 @@ void EditorPropertyBBParam::_create_value_editor(Variant::Type p_type) {
void EditorPropertyBBParam::_remove_value_editor() {
if (value_editor) {
hbox->remove_child(value_editor);
editor_hbox->remove_child(value_editor);
value_editor->queue_free();
value_editor = nullptr;
}
@ -209,6 +216,16 @@ void EditorPropertyBBParam::_mode_changed() {
update_property();
}
void EditorPropertyBBParam::_type_selected(int p_index) {
Ref<BBVariant> param = _get_edited_param();
ERR_FAIL_COND(param.is_null());
Variant::Type t = Variant::Type(p_index);
param->set_type(t);
String type_name = Variant::get_type_name(t);
type_choice->set_icon(get_editor_theme_icon(type_name));
update_property();
}
void EditorPropertyBBParam::_variable_edited(const String &p_text) {
_get_edited_param()->set_variable(p_text);
}
@ -240,12 +257,32 @@ void EditorPropertyBBParam::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE:
case NOTIFICATION_THEME_CHANGED: {
int id = mode_button->get_mode();
{
String type = Variant::get_type_name(_get_edited_param()->get_type());
type_choice->set_icon(get_editor_theme_icon(type));
}
// Initialize mode button.
mode_button->clear();
mode_button->add_mode(Mode::SPECIFY_VALUE, get_editor_theme_icon(SNAME("LimboSpecifyValue")), TTR("Mode: Specify value.\nClick to switch mode."));
mode_button->add_mode(Mode::BIND_VAR, get_editor_theme_icon(SNAME("BTSetVar")), TTR("Mode: Bind blackboard variable.\nClick to switch mode."));
if (id >= 0) {
mode_button->set_mode(id);
mode_button->set_mode(_get_edited_param()->get_value_source() == BBParam::BLACKBOARD_VAR ? Mode::BIND_VAR : Mode::SPECIFY_VALUE);
Ref<BBVariant> variant_param = _get_edited_param();
bool is_variant_param = variant_param.is_valid();
if (is_variant_param) {
// Initialize type choice.
PopupMenu *type_menu = type_choice->get_popup();
type_menu->clear();
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
if (i == Variant::RID || i == Variant::CALLABLE || i == Variant::OBJECT || i == Variant::SIGNAL) {
continue;
}
String type = Variant::get_type_name(Variant::Type(i));
type_menu->add_icon_item(get_editor_theme_icon(type), type, i);
}
} else { // Not a variant param.
type_choice->hide();
}
} break;
}
@ -261,12 +298,21 @@ EditorPropertyBBParam::EditorPropertyBBParam() {
mode_button->set_focus_mode(FOCUS_NONE);
mode_button->connect(SNAME("mode_changed"), callable_mp(this, &EditorPropertyBBParam::_mode_changed));
editor_hbox = memnew(HBoxContainer);
hbox->add_child(editor_hbox);
editor_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
editor_hbox->add_theme_constant_override(SNAME("separation"), 0);
variable_edit = memnew(LineEdit);
hbox->add_child(variable_edit);
editor_hbox->add_child(variable_edit);
variable_edit->set_placeholder(TTR("Variable"));
variable_edit->set_h_size_flags(SIZE_EXPAND_FILL);
variable_edit->connect(SNAME("text_changed"), callable_mp(this, &EditorPropertyBBParam::_variable_edited));
type_choice = memnew(MenuButton);
hbox->add_child(type_choice);
type_choice->get_popup()->connect(SNAME("id_pressed"), callable_mp(this, &EditorPropertyBBParam::_type_selected));
param_type = SNAME("BBString");
}

View File

@ -12,12 +12,14 @@
#ifndef EDITOR_PROPERTY_BB_PARAM_H
#define EDITOR_PROPERTY_BB_PARAM_H
#include "core/object/object.h"
#include "editor/editor_inspector.h"
#include "modules/limboai/blackboard/bb_param/bb_param.h"
#include "modules/limboai/editor/mode_switch_button.h"
#include "scene/gui/box_container.h"
#include "scene/gui/menu_button.h"
class EditorPropertyBBParam : public EditorProperty {
GDCLASS(EditorPropertyBBParam, EditorProperty);
@ -32,9 +34,11 @@ private:
Mode mode = Mode::SPECIFY_VALUE;
HBoxContainer *hbox = nullptr;
HBoxContainer *editor_hbox = nullptr;
ModeSwitchButton *mode_button = nullptr;
EditorProperty *value_editor = nullptr;
LineEdit *variable_edit = nullptr;
MenuButton *type_choice = nullptr;
Ref<BBParam> _get_edited_param();
@ -44,6 +48,7 @@ private:
void _value_edited(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
void _variable_edited(const String &p_text);
void _mode_changed();
void _type_selected(int p_index);
protected:
void _notification(int p_what);

View File

@ -32,7 +32,7 @@ void ModeSwitchButton::add_mode(int p_id, const Ref<Texture2D> &p_icon, const St
modes.append(mode);
if (current_mode_index == -1) {
_set_mode(0);
_set_mode_by_index(0);
}
}
@ -48,7 +48,7 @@ void ModeSwitchButton::set_mode(int p_id, bool p_no_signal) {
}
ERR_FAIL_COND_MSG(idx == -1, "Button state not found with such id: " + itos(p_id));
_set_mode(p_id);
_set_mode_by_index(idx);
if (!p_no_signal) {
emit_signal(SNAME("mode_changed"));
}

View File

@ -30,8 +30,8 @@ private:
Vector<Mode> modes;
_FORCE_INLINE_ void _set_mode(int p_id) {
current_mode_index = p_id;
_FORCE_INLINE_ void _set_mode_by_index(int p_index) {
current_mode_index = p_index;
set_icon(modes[current_mode_index].icon);
if (!modes[current_mode_index].tooltip.is_empty()) {
set_tooltip_text(modes[current_mode_index].tooltip);