From c658ee19126c15e86eaff3c16fac0fec5c2b3def Mon Sep 17 00:00:00 2001 From: octod Date: Thu, 1 Feb 2024 16:10:22 +0100 Subject: [PATCH] feat: adds slot remove --- project/.ggs/1.0.0/equipment/head.tres | 6 --- .../item/ggs_equipment_slot_scene.cpp | 48 ++++++++++++++++++- .../item/ggs_equipment_slot_scene.h | 11 ++++- src/resource_manager/resource_manager.cpp | 10 ++++ src/resource_manager/resource_manager.h | 12 +++-- 5 files changed, 74 insertions(+), 13 deletions(-) delete mode 100644 project/.ggs/1.0.0/equipment/head.tres diff --git a/project/.ggs/1.0.0/equipment/head.tres b/project/.ggs/1.0.0/equipment/head.tres deleted file mode 100644 index 3a071c2..0000000 --- a/project/.ggs/1.0.0/equipment/head.tres +++ /dev/null @@ -1,6 +0,0 @@ -[gd_resource type="EquipmentSlot" format=3 uid="uid://2h6o2lbxds84"] - -[resource] -slot_name = &"head" -accepts_items_with_tags = PackedStringArray("type.armor.helm") -resource_name = "head.tres" diff --git a/src/editor_plugin/main_scene/item/ggs_equipment_slot_scene.cpp b/src/editor_plugin/main_scene/item/ggs_equipment_slot_scene.cpp index 0536c58..ff8071f 100644 --- a/src/editor_plugin/main_scene/item/ggs_equipment_slot_scene.cpp +++ b/src/editor_plugin/main_scene/item/ggs_equipment_slot_scene.cpp @@ -30,6 +30,27 @@ void EquipmentSlotScene::_handle_slot_create_requested(String p_name) _render_slots_tree(); } +void EquipmentSlotScene::_handle_slot_remove_confirmed() +{ + if (selected_equipment_slot == nullptr) + { + return; + } + + Error result = GGSResourceManager::get_singleton()->remove_resource(selected_equipment_slot); + + if (result == OK) + { + EquipmentManager::get_singleton()->remove_slot(selected_equipment_slot); + confirm_remove_slot_dialog->hide(); + _render_slots_tree(); + } + else + { + WARN_PRINT("Could not remove slot because of error " + String::num_int64(result) + "."); + } +} + void EquipmentSlotScene::_handle_item_edited() { TreeItem *selected = slots_tree->get_edited(); @@ -50,6 +71,19 @@ void EquipmentSlotScene::_handle_item_edited() } } +void EquipmentSlotScene::_handle_slot_tree_button_clicked(TreeItem *p_item, int p_column, int p_id, int mouse_button_index) +{ + Variant slot_variant = EquipmentManager::get_singleton()->slots[p_item->get_index()]; + selected_equipment_slot = cast_to(slot_variant); + + if (selected_equipment_slot == nullptr) + { + return; + } + + confirm_remove_slot_dialog->popup_centered(); +} + void EquipmentSlotScene::_handle_slot_item_selected() { TreeItem *selected = slots_tree->get_selected(); @@ -226,6 +260,8 @@ void EquipmentSlotScene::_bind_methods() ClassDB::bind_method(D_METHOD("_handle_slot_create_requested"), &EquipmentSlotScene::_handle_slot_create_requested); ClassDB::bind_method(D_METHOD("_handle_slot_item_name_edited"), &EquipmentSlotScene::_handle_slot_item_name_edited); ClassDB::bind_method(D_METHOD("_handle_slot_item_selected"), &EquipmentSlotScene::_handle_slot_item_selected); + ClassDB::bind_method(D_METHOD("_handle_slot_remove_confirmed"), &EquipmentSlotScene::_handle_slot_remove_confirmed); + ClassDB::bind_method(D_METHOD("_handle_slot_tree_button_clicked", "p_item", "p_column", "p_id", "mouse_button_index"), &EquipmentSlotScene::_handle_slot_tree_button_clicked); ClassDB::bind_method(D_METHOD("_handle_tag_selection_window_closed"), &EquipmentSlotScene::_handle_tag_selection_window_closed); ClassDB::bind_method(D_METHOD("_handle_tags_changed"), &EquipmentSlotScene::_handle_tags_changed); ClassDB::bind_method(D_METHOD("_handle_tags_deselected", "p_tag_path"), &EquipmentSlotScene::_handle_tags_deselected); @@ -275,6 +311,15 @@ void EquipmentSlotScene::_ready() slots_tree = memnew(Tree); selected_slot_tag_dictionary = memnew(TagDictionary); + confirm_remove_slot_dialog = memnew(AcceptDialog); + confirm_remove_slot_dialog->add_cancel_button(tr("Cancel")); + confirm_remove_slot_dialog->connect("confirmed", Callable(this, "_handle_slot_remove_confirmed")); + confirm_remove_slot_dialog->set_close_on_escape(true); + confirm_remove_slot_dialog->set_ok_button_text(tr("Yes, remove it")); + confirm_remove_slot_dialog->set_text(tr("Are you sure you want to remove this slot?")); + confirm_remove_slot_dialog->set_title(tr("Do you wish to remove this slot?")); + add_child(confirm_remove_slot_dialog); + new_resource_modal = memnew(NewResourceModal); new_resource_modal->connect("create_requested", Callable(this, "_handle_slot_create_requested")); @@ -296,8 +341,9 @@ void EquipmentSlotScene::_ready() header->set_anchors_and_offsets_preset(PRESET_TOP_WIDE); header->set_h_size_flags(SIZE_EXPAND_FILL); - slots_tree->connect("item_selected", Callable(this, "_handle_slot_item_selected")); + slots_tree->connect("button_clicked", Callable(this, "_handle_slot_tree_button_clicked")); slots_tree->connect("item_edited", Callable(this, "_handle_item_edited")); + slots_tree->connect("item_selected", Callable(this, "_handle_slot_item_selected")); slots_tree->set_columns(4); slots_tree->set_column_title(0, tr("Slot name")); slots_tree->set_column_title(1, tr("Accepts items with tags")); diff --git a/src/editor_plugin/main_scene/item/ggs_equipment_slot_scene.h b/src/editor_plugin/main_scene/item/ggs_equipment_slot_scene.h index 2180c32..a1c9a8d 100644 --- a/src/editor_plugin/main_scene/item/ggs_equipment_slot_scene.h +++ b/src/editor_plugin/main_scene/item/ggs_equipment_slot_scene.h @@ -2,6 +2,7 @@ #define GGS_EQUIPMENT_SLOT_SCENE_H #include +#include #include #include #include @@ -20,10 +21,14 @@ namespace ggs::editor_plugin GDCLASS(EquipmentSlotScene, VBoxContainer); private: - /// @brief Handles the add button pressed event. - void _handle_slot_create_requested(String p_name); /// @brief Handles the item edited event. void _handle_item_edited(); + /// @brief Handles the slot tree button clicked event. + void _handle_slot_tree_button_clicked(TreeItem *p_item, int p_column, int p_id, int mouse_button_index); + /// @brief Handles the add button pressed event. + void _handle_slot_create_requested(String p_name); + /// @brief Handles the remove button pressed event. + void _handle_slot_remove_confirmed(); /// @brief Handles the slot item selected event. void _handle_slot_item_selected(); /// @brief handles the slot item name edited event. @@ -50,6 +55,8 @@ namespace ggs::editor_plugin }; static void _bind_methods(); + /// @brief The confirm remove slot dialog. + AcceptDialog *confirm_remove_slot_dialog; /// @brief The new resource modal. NewResourceModal *new_resource_modal; /// @brief The selected equipment slot. diff --git a/src/resource_manager/resource_manager.cpp b/src/resource_manager/resource_manager.cpp index bc81f17..d449a0b 100644 --- a/src/resource_manager/resource_manager.cpp +++ b/src/resource_manager/resource_manager.cpp @@ -69,6 +69,16 @@ void GGSResourceManager::ensure_directories() const } } +Error GGSResourceManager::remove_resource(Ref p_ref) const +{ + if (Engine::get_singleton()->is_editor_hint()) + { + return DirAccess::remove_absolute(p_ref->get_path()); + } + + return Error::ERR_UNAVAILABLE; +} + bool GGSResourceManager::save_resource(Ref p_ref) const { return save_resource(String(EQUIPMENT_DIR), p_ref) == Error::OK; diff --git a/src/resource_manager/resource_manager.h b/src/resource_manager/resource_manager.h index ec820c9..588c5d6 100644 --- a/src/resource_manager/resource_manager.h +++ b/src/resource_manager/resource_manager.h @@ -53,10 +53,14 @@ namespace ggs void ensure_directory(const String &p_dir) const; /// @brief Ensures that the directories for the ggs resources exist, if not, they are created. void ensure_directories() const; - /// @brief Saves the given resource to the file system. - /// @param p_dirname The directory to save the resource to. - /// @param p_ref The resource to save. - Error save_resource(const String &p_dirname, Ref p_ref) const; + /// @brief Removes the given resource from the file system. + /// @param p_ref The resource to remove. + /// @return The error code. + Error remove_resource(Ref p_ref) const; + /// @brief Saves the given resource to the file system. + /// @param p_dirname The directory to save the resource to. + /// @param p_ref The resource to save. + Error save_resource(const String &p_dirname, Ref p_ref) const; public: GGSResourceManager();