Skip to content

Commit

Permalink
Usability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
limbonaut committed Jan 25, 2024
1 parent a22860b commit f912f0a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 15 deletions.
25 changes: 19 additions & 6 deletions blackboard/blackboard_plan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,27 @@ void BlackboardPlan::rename_var(const String &p_name, const String &p_new_name)
emit_changed();
}

void BlackboardPlan::swap_vars(int p_idx_a, int p_idx_b) {
ERR_FAIL_INDEX(p_idx_a, (int)var_map.size());
ERR_FAIL_INDEX(p_idx_b, (int)var_map.size());
void BlackboardPlan::move_var(int p_index, int p_new_index) {
ERR_FAIL_INDEX(p_index, (int)var_map.size());
ERR_FAIL_INDEX(p_new_index, (int)var_map.size());

Pair<String, BBVariable> a = var_list[p_idx_a];
Pair<String, BBVariable> b = var_list[p_idx_b];
if (p_index == p_new_index) {
return;
}

List<Pair<String, BBVariable>>::Element *E = var_list.front();
for (int i = 0; i < p_index; i++) {
E = E->next();
}
List<Pair<String, BBVariable>>::Element *E2 = var_list.front();
for (int i = 0; i < p_new_index; i++) {
E2 = E2->next();
}

var_list.swap(var_list.find(a), var_list.find(b));
var_list.move_before(E, E2);
if (p_new_index > p_index) {
var_list.move_before(E2, E);
}

notify_property_list_changed();
emit_changed();
Expand Down
2 changes: 1 addition & 1 deletion blackboard/blackboard_plan.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class BlackboardPlan : public Resource {
PackedStringArray list_vars() const;
String get_var_name(const BBVariable &p_var) const;
void rename_var(const String &p_name, const String &p_new_name);
void swap_vars(int idx_a, int idx_b);
void move_var(int p_index, int p_new_index);

void sync_with_base_plan();
bool is_derived() const { return base.is_valid(); }
Expand Down
17 changes: 10 additions & 7 deletions editor/blackboard_plan_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,16 @@ void BlackboardPlanEditor::_hint_chosen(int id) {

void BlackboardPlanEditor::_drag_button_down(Control *p_row) {
drag_index = p_row->get_index();
drag_start = drag_index;
drag_mouse_y_delta = 0.0;
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
}

void BlackboardPlanEditor::_drag_button_up() {
drag_index = -1;
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
plan->move_var(drag_start, drag_index);
drag_index = -1;
drag_start = -1;
_refresh();
}

Expand All @@ -140,13 +143,11 @@ void BlackboardPlanEditor::_drag_button_gui_input(const Ref<InputEvent> &p_event
return;
}

float required_distance = 20.0f * EDSCALE;
float required_distance = 30.0f * EDSCALE;
if (ABS(drag_mouse_y_delta) > required_distance) {
int drag_dir = drag_mouse_y_delta > 0.0f ? 1 : -1;
drag_mouse_y_delta -= required_distance * drag_dir;

plan->swap_vars(drag_index, drag_index + drag_dir);

Control *row = Object::cast_to<Control>(rows_vbox->get_child(drag_index));
Control *other_row = Object::cast_to<Control>(rows_vbox->get_child(drag_index + drag_dir));
ERR_FAIL_NULL(row);
Expand Down Expand Up @@ -412,21 +413,23 @@ void EditorInspectorPluginBBPlan::_parse_begin(Object *p_object) {

VBoxContainer *toolbar = memnew(VBoxContainer);
margin_container->add_child(toolbar);
toolbar->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
toolbar->set_h_size_flags(Control::SIZE_EXPAND_FILL);

if (plan->is_derived()) {
Button *goto_btn = memnew(Button);
toolbar->add_child(goto_btn);
goto_btn->set_text(TTR("Edit Base"));
goto_btn->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
goto_btn->set_custom_minimum_size(Size2(200.0, 0.0) * EDSCALE);
goto_btn->set_custom_minimum_size(Size2(150.0, 0.0) * EDSCALE);
BUTTON_SET_ICON(goto_btn, EditorInterface::get_singleton()->get_editor_theme()->get_icon(LW_NAME(Edit), LW_NAME(EditorIcons)));
goto_btn->connect(LW_NAME(pressed), callable_mp(this, &EditorInspectorPluginBBPlan::_open_base_plan).bind(plan));
} else {
Button *edit_btn = memnew(Button);
toolbar->add_child(edit_btn);
edit_btn->set_text(TTR("Manage..."));
edit_btn->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
edit_btn->set_custom_minimum_size(Size2(200.0, 0.0) * EDSCALE);
edit_btn->set_custom_minimum_size(Size2(150.0, 0.0) * EDSCALE);
BUTTON_SET_ICON(edit_btn, EditorInterface::get_singleton()->get_editor_theme()->get_icon(LW_NAME(EditAddRemove), LW_NAME(EditorIcons)));
edit_btn->connect(LW_NAME(pressed), callable_mp(this, &EditorInspectorPluginBBPlan::_edit_plan).bind(plan));
}

Expand Down
1 change: 1 addition & 0 deletions editor/blackboard_plan_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class BlackboardPlanEditor : public AcceptDialog {

int last_index = 0;
int drag_mouse_y_delta = 0;
int drag_start = -1;
int drag_index = -1;

Ref<BlackboardPlan> plan;
Expand Down
11 changes: 10 additions & 1 deletion editor/limbo_ai_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ void LimboAIEditor::_update_history_buttons() {
}

void LimboAIEditor::_new_bt() {
BehaviorTree *bt = memnew(BehaviorTree);
Ref<BehaviorTree> bt = memnew(BehaviorTree);
bt->set_root_task(memnew(BTSelector));
bt->set_blackboard_plan(memnew(BlackboardPlan));
EDIT_RESOURCE(bt);
}

Expand Down Expand Up @@ -222,6 +223,11 @@ void LimboAIEditor::edit_bt(Ref<BehaviorTree> p_behavior_tree, bool p_force_refr
return;
}

#ifdef LIMBOAI_MODULE
p_behavior_tree->editor_set_section_unfold("blackboard_plan", true);
p_behavior_tree->notify_property_list_changed();
#endif // LIMBOAI_MODULE

task_tree->load_bt(p_behavior_tree);

int idx = history.find(p_behavior_tree);
Expand Down Expand Up @@ -698,6 +704,9 @@ void LimboAIEditor::_on_visibility_changed() {
void LimboAIEditor::_on_header_pressed() {
_update_header();
task_tree->deselect();
#ifdef LIMBOAI_MODULE
task_tree->get_bt()->editor_set_section_unfold("blackboard_plan", true);
#endif // LIMBOAI_MODULE
EDIT_RESOURCE(task_tree->get_bt());
}

Expand Down
2 changes: 2 additions & 0 deletions util/limbo_string_names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ LimboStringNames::LimboStringNames() {
doc_italic = SN("doc_italic");
draw = SN("draw");
Duplicate = SN("Duplicate");
Edit = SN("Edit");
EditAddRemove = SN("EditAddRemove");
Editor = SN("Editor");
EditorFonts = SN("EditorFonts");
EditorIcons = SN("EditorIcons");
Expand Down
2 changes: 2 additions & 0 deletions util/limbo_string_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class LimboStringNames {
StringName doc_italic;
StringName draw;
StringName Duplicate;
StringName Edit;
StringName EditAddRemove;
StringName Editor;
StringName EditorFonts;
StringName EditorIcons;
Expand Down

0 comments on commit f912f0a

Please sign in to comment.