limboai/editor/limbo_ai_editor_plugin.h

257 lines
7.4 KiB
C
Raw Normal View History

/**
* limbo_ai_editor_plugin.h
* =============================================================================
* Copyright 2021-2023 Serhii Snitsaruk
*
* 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.
* =============================================================================
*/
2022-09-02 12:25:03 +00:00
#ifdef TOOLS_ENABLED
2024-01-09 12:34:24 +00:00
#ifndef LIMBO_AI_EDITOR_PLUGIN_H
#define LIMBO_AI_EDITOR_PLUGIN_H
2024-01-09 12:34:24 +00:00
#include "../bt/behavior_tree.h"
#include "../bt/tasks/bt_task.h"
2023-08-26 08:08:01 +00:00
#include "task_palette.h"
2023-08-26 08:24:08 +00:00
#include "task_tree.h"
2023-07-20 16:35:36 +00:00
2024-01-09 12:34:24 +00:00
#ifdef LIMBOAI_MODULE
#include "core/object/class_db.h"
#include "core/object/object.h"
#include "core/templates/hash_set.h"
#include "editor/editor_node.h"
#include "editor/editor_plugin.h"
#include "editor/gui/editor_spin_slider.h"
2022-09-02 12:25:03 +00:00
#include "scene/gui/box_container.h"
#include "scene/gui/control.h"
#include "scene/gui/file_dialog.h"
2022-09-02 12:25:03 +00:00
#include "scene/gui/flow_container.h"
#include "scene/gui/line_edit.h"
#include "scene/gui/margin_container.h"
#include "scene/gui/panel_container.h"
#include "scene/gui/popup.h"
#include "scene/gui/popup_menu.h"
#include "scene/gui/split_container.h"
#include "scene/gui/tree.h"
#include "scene/resources/texture.h"
2024-01-09 12:34:24 +00:00
#define GET_UNDO_REDO() EditorUndoRedoManager::get_singleton()
2024-01-13 16:10:42 +00:00
#endif // LIMBOAI_MODULE
2024-01-09 12:34:24 +00:00
#ifdef LIMBOAI_GDEXTENSION
#include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/editor_plugin.hpp>
#include <godot_cpp/classes/editor_spin_slider.hpp>
#include <godot_cpp/classes/file_dialog.hpp>
#include <godot_cpp/classes/h_box_container.hpp>
#include <godot_cpp/classes/h_split_container.hpp>
#include <godot_cpp/classes/input_event.hpp>
#include <godot_cpp/classes/menu_button.hpp>
#include <godot_cpp/classes/panel.hpp>
#include <godot_cpp/classes/popup_menu.hpp>
#include <godot_cpp/classes/texture2d.hpp>
#include <godot_cpp/variant/packed_string_array.hpp>
#include <godot_cpp/variant/variant.hpp>
using namespace godot;
#define GET_UNDO_REDO() plugin->get_undo_redo()
2024-01-13 16:10:42 +00:00
#endif // LIMBOAI_GDEXTENSION
2024-01-09 12:34:24 +00:00
class LimboAIEditor : public Control {
GDCLASS(LimboAIEditor, Control);
private:
enum Action {
ACTION_EDIT_PROBABILITY,
ACTION_RENAME,
ACTION_CHANGE_TYPE,
ACTION_EDIT_SCRIPT,
ACTION_OPEN_DOC,
ACTION_MOVE_UP,
ACTION_MOVE_DOWN,
ACTION_DUPLICATE,
ACTION_MAKE_ROOT,
ACTION_EXTRACT_SUBTREE,
ACTION_REMOVE,
};
enum MiscMenu {
MISC_INTRODUCTION,
2024-01-13 21:40:27 +00:00
MISC_ONLINE_DOCUMENTATION,
MISC_OPEN_DEBUGGER,
MISC_PROJECT_SETTINGS,
MISC_CREATE_SCRIPT_TEMPLATE,
};
2023-08-29 09:47:48 +00:00
struct ThemeCache {
Ref<Texture2D> duplicate_task_icon;
Ref<Texture2D> edit_script_icon;
Ref<Texture2D> make_root_icon;
Ref<Texture2D> move_task_down_icon;
Ref<Texture2D> move_task_up_icon;
Ref<Texture2D> open_debugger_icon;
2024-01-13 21:40:27 +00:00
Ref<Texture2D> doc_icon;
Ref<Texture2D> introduction_icon;
2023-09-26 14:12:10 +00:00
Ref<Texture2D> percent_icon;
2023-08-29 09:47:48 +00:00
Ref<Texture2D> remove_task_icon;
Ref<Texture2D> rename_task_icon;
Ref<Texture2D> change_type_icon;
Ref<Texture2D> extract_subtree_icon;
2024-01-09 12:34:24 +00:00
Ref<Texture2D> behavior_tree_icon;
2023-08-29 09:47:48 +00:00
} theme_cache;
2024-01-09 12:34:24 +00:00
EditorPlugin *plugin;
2022-09-02 13:43:54 +00:00
Vector<Ref<BehaviorTree>> history;
int idx_history;
HashSet<Ref<BehaviorTree>> dirty;
2022-09-02 13:43:54 +00:00
VBoxContainer *vbox;
Button *header;
HSplitContainer *hsc;
TaskTree *task_tree;
VBoxContainer *banners;
Panel *usage_hint;
PopupMenu *menu;
HBoxContainer *fav_tasks_hbox;
TaskPalette *task_palette;
PopupPanel *probability_popup;
EditorSpinSlider *probability_edit;
Button *weight_mode;
Button *percent_mode;
PopupPanel *change_type_popup;
TaskPalette *change_type_palette;
FileDialog *save_dialog;
FileDialog *load_dialog;
FileDialog *extract_dialog;
2022-09-02 13:43:54 +00:00
Button *history_back;
Button *history_forward;
Button *new_btn;
Button *load_btn;
Button *save_btn;
Button *new_script_btn;
MenuButton *misc_btn;
2022-11-23 15:25:32 +00:00
ConfirmationDialog *rename_dialog;
LineEdit *rename_edit;
ConfirmationDialog *disk_changed;
Tree *disk_changed_list;
HashSet<String> disk_changed_files;
2022-09-03 12:11:47 +00:00
void _add_task(const Ref<BTTask> &p_task);
Ref<BTTask> _create_task_by_class_or_path(const String &p_class_or_path) const;
void _add_task_by_class_or_path(const String &p_class_or_path);
2022-12-22 12:19:39 +00:00
void _remove_task(const Ref<BTTask> &p_task);
2022-09-03 12:11:47 +00:00
_FORCE_INLINE_ void _add_task_with_prototype(const Ref<BTTask> &p_prototype) { _add_task(p_prototype->clone()); }
2022-11-22 21:58:53 +00:00
void _update_header() const;
2022-09-02 13:43:54 +00:00
void _update_history_buttons();
void _update_favorite_tasks();
void _update_misc_menu();
void _update_banners();
void _new_bt();
void _save_bt(String p_path);
void _load_bt(String p_path);
2022-09-03 12:01:13 +00:00
void _mark_as_dirty(bool p_dirty);
void _create_user_task_dir();
void _remove_task_from_favorite(const String &p_task);
void _extract_subtree(const String &p_path);
2024-01-09 12:34:24 +00:00
void _replace_task(const Ref<BTTask> &p_task, const Ref<BTTask> &p_by_task);
void _reload_modified();
void _resave_modified(String _str = "");
2024-01-09 12:34:24 +00:00
void _popup_file_dialog(FileDialog *p_dialog) { p_dialog->popup_centered_clamped(Size2i(700, 500), 0.8f); }
2022-12-17 12:39:37 +00:00
void _rename_task_confirmed();
void _on_tree_rmb(const Vector2 &p_menu_pos);
void _action_selected(int p_id);
void _misc_option_selected(int p_id);
void _on_probability_edited(double p_value);
void _update_probability_edit();
void _probability_popup_closed();
void _on_tree_task_selected(const Ref<BTTask> &p_task);
void _on_visibility_changed();
void _on_header_pressed();
void _on_save_pressed();
2022-09-02 13:43:54 +00:00
void _on_history_back();
void _on_history_forward();
void _on_task_dragged(Ref<BTTask> p_task, Ref<BTTask> p_to_task, int p_type);
2024-01-09 12:34:24 +00:00
void _on_resources_reload(const PackedStringArray &p_resources);
void _task_type_selected(const String &p_class_or_path);
2024-01-09 12:34:24 +00:00
void _edit_project_settings();
void _process_shortcut_input(const Ref<InputEvent> &p_event);
#ifdef LIMBOAI_MODULE
virtual void shortcut_input(const Ref<InputEvent> &p_event) override { _process_shortcut_input(p_event); }
#endif // LIMBOAI_MODULE
protected:
2024-01-09 12:34:24 +00:00
virtual void _do_update_theme_item_cache();
void _notification(int p_what);
2023-08-29 09:47:48 +00:00
static void _bind_methods();
public:
2024-01-09 12:34:24 +00:00
void set_plugin(EditorPlugin *p_plugin) { plugin = p_plugin; };
void edit_bt(Ref<BehaviorTree> p_behavior_tree, bool p_force_refresh = false);
2022-09-03 12:01:13 +00:00
void apply_changes();
2024-01-09 12:34:24 +00:00
#ifdef LIMBOAI_GDEXTENSION
virtual void _shortcut_input(const Ref<InputEvent> &p_event) override { _process_shortcut_input(p_event); }
2024-01-13 16:10:42 +00:00
#endif
2024-01-09 12:34:24 +00:00
LimboAIEditor();
~LimboAIEditor();
};
class LimboAIEditorPlugin : public EditorPlugin {
GDCLASS(LimboAIEditorPlugin, EditorPlugin);
private:
LimboAIEditor *limbo_ai_editor;
protected:
2024-01-09 20:47:22 +00:00
static void _bind_methods();
void _notification(int p_notification);
public:
2024-01-09 12:34:24 +00:00
#ifdef LIMBOAI_MODULE
bool has_main_screen() const override { return true; }
2024-01-09 12:34:24 +00:00
virtual String get_name() const override { return "LimboAI"; }
virtual void make_visible(bool p_visible) override;
virtual void apply_changes() override;
virtual void edit(Object *p_object) override;
virtual bool handles(Object *p_object) const override;
2024-01-09 12:34:24 +00:00
2024-01-13 16:10:42 +00:00
#elif LIMBOAI_GDEXTENSION
2024-01-09 12:34:24 +00:00
bool _has_main_screen() const override { return true; }
virtual String _get_plugin_name() const override { return "LimboAI"; }
virtual void _make_visible(bool p_visible) override;
virtual void _apply_changes() override;
virtual void _edit(Object *p_object) override;
virtual bool _handles(Object *p_object) const override;
2024-01-10 00:17:09 +00:00
virtual Ref<Texture2D> _get_plugin_icon() const override;
2024-01-13 16:10:42 +00:00
#endif // LIMBOAI_MODULE & LIMBOAI_GDEXTENSION
LimboAIEditorPlugin();
~LimboAIEditorPlugin();
};
2024-01-13 16:10:42 +00:00
#endif // LIMBO_AI_EDITOR_PLUGIN_H
#endif // ! TOOLS_ENABLED