limboai/editor/task_tree.h

121 lines
3.3 KiB
C
Raw Normal View History

2023-08-26 08:24:08 +00:00
/**
* task_tree.h
* =============================================================================
2024-03-21 20:38:57 +00:00
* Copyright 2021-2024 Serhii Snitsaruk
2023-08-26 08:24:08 +00:00
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
2024-09-19 21:35:14 +00:00
#ifndef TASK_TREE_H
#define TASK_TREE_H
#ifdef TOOLS_ENABLED
2024-01-07 20:04:18 +00:00
#include "../bt/behavior_tree.h"
2024-09-19 21:35:14 +00:00
#include "tree_search.h"
2023-08-26 08:24:08 +00:00
2024-01-07 20:04:18 +00:00
#ifdef LIMBOAI_MODULE
2023-08-26 08:24:08 +00:00
#include "scene/gui/control.h"
#include "scene/gui/tree.h"
2023-10-28 11:07:11 +00:00
#include "scene/resources/style_box_flat.h"
2023-08-26 08:24:08 +00:00
2024-01-07 20:04:18 +00:00
#define RECT_CACHE_KEY ObjectID
#endif // LIMBOAI_MODULE
#ifdef LIMBOAI_GDEXTENSION
#include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/font.hpp>
#include <godot_cpp/classes/style_box_flat.hpp>
#include <godot_cpp/classes/texture2d.hpp>
#include <godot_cpp/classes/tree.hpp>
#include <godot_cpp/templates/hash_map.hpp>
#define RECT_CACHE_KEY uint64_t
#endif // LIMBOAI_GDEXTENSION
2023-08-26 08:24:08 +00:00
class TaskTree : public Control {
GDCLASS(TaskTree, Control);
private:
Tree *tree;
Ref<BehaviorTree> bt;
Ref<BTTask> last_selected;
bool editable;
bool updating_tree;
2024-01-07 20:04:18 +00:00
HashMap<RECT_CACHE_KEY, Rect2> probability_rect_cache;
2024-09-25 17:34:44 +00:00
Ref<TreeSearch> tree_search;
TreeSearchPanel *tree_search_panel;
2023-08-26 08:24:08 +00:00
2023-08-29 09:07:18 +00:00
struct ThemeCache {
Ref<Font> comment_font;
Ref<Font> name_font;
2023-08-29 09:07:18 +00:00
Ref<Font> custom_name_font;
Ref<Font> normal_name_font;
Ref<Font> probability_font;
double name_font_size = 18.0;
double probability_font_size = 16.0;
2023-08-29 09:07:18 +00:00
Ref<Texture2D> task_warning_icon;
Color comment_color;
Color probability_font_color;
Ref<StyleBoxFlat> probability_bg;
2023-08-29 09:07:18 +00:00
} theme_cache;
2023-08-26 08:24:08 +00:00
TreeItem *_create_tree(const Ref<BTTask> &p_task, TreeItem *p_parent, int p_idx = -1);
void _update_item(TreeItem *p_item);
void _update_tree();
TreeItem *_find_item(const Ref<BTTask> &p_task) const;
void _on_item_selected();
void _on_item_activated();
void _on_item_collapsed(Object *p_obj);
void _on_item_mouse_selected(const Vector2 &p_pos, MouseButton p_button_index);
2023-08-26 08:24:08 +00:00
void _on_task_changed();
Variant _get_drag_data_fw(const Point2 &p_point);
bool _can_drop_data_fw(const Point2 &p_point, const Variant &p_data) const;
void _drop_data_fw(const Point2 &p_point, const Variant &p_data);
2024-09-13 17:24:52 +00:00
void _normalize_drop(TreeItem *item, int type, int &to_pos, Ref<BTTask> &to_task) const;
2023-08-26 08:24:08 +00:00
void _draw_probability(Object *item_obj, Rect2 rect);
2023-08-26 08:24:08 +00:00
protected:
2024-01-07 20:04:18 +00:00
virtual void _do_update_theme_item_cache();
2023-08-26 08:24:08 +00:00
void _notification(int p_what);
2023-08-29 09:07:18 +00:00
static void _bind_methods();
2023-08-26 08:24:08 +00:00
public:
void load_bt(const Ref<BehaviorTree> &p_behavior_tree);
void unload();
Ref<BehaviorTree> get_bt() const { return bt; }
void update_tree() { _update_tree(); }
void update_task(const Ref<BTTask> &p_task);
2024-09-11 03:38:05 +00:00
void add_selection(const Ref<BTTask> &p_task);
void remove_selection(const Ref<BTTask> &p_task);
2023-08-26 08:24:08 +00:00
Ref<BTTask> get_selected() const;
2024-09-11 03:38:05 +00:00
Vector<Ref<BTTask>> get_selected_tasks() const;
void clear_selection();
2024-09-25 15:32:03 +00:00
void tree_search_show_and_focus();
2023-08-26 08:24:08 +00:00
Rect2 get_selected_probability_rect() const;
double get_selected_probability_weight() const;
double get_selected_probability_percent() const;
bool selected_has_probability() const;
2023-08-26 08:24:08 +00:00
virtual bool editor_can_reload_from_file() { return false; }
TaskTree();
~TaskTree();
};
#endif // ! TOOLS_ENABLED
2024-09-19 21:35:14 +00:00
#endif // ! TASK_TREE_H