Skip to content

Commit

Permalink
feat: adds slot remove
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Feb 1, 2024
1 parent 4b09761 commit c658ee1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
6 changes: 0 additions & 6 deletions project/.ggs/1.0.0/equipment/head.tres

This file was deleted.

48 changes: 47 additions & 1 deletion src/editor_plugin/main_scene/item/ggs_equipment_slot_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<EquipmentSlot>(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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"));

Expand All @@ -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"));
Expand Down
11 changes: 9 additions & 2 deletions src/editor_plugin/main_scene/item/ggs_equipment_slot_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GGS_EQUIPMENT_SLOT_SCENE_H

#include <godot_cpp/classes/v_box_container.hpp>
#include <godot_cpp/classes/accept_dialog.hpp>
#include <godot_cpp/classes/button.hpp>
#include <godot_cpp/classes/tree.hpp>
#include <godot_cpp/classes/window.hpp>
Expand All @@ -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.
Expand All @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/resource_manager/resource_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ void GGSResourceManager::ensure_directories() const
}
}

Error GGSResourceManager::remove_resource(Ref<Resource> 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<EquipmentSlot> p_ref) const
{
return save_resource(String(EQUIPMENT_DIR), p_ref) == Error::OK;
Expand Down
12 changes: 8 additions & 4 deletions src/resource_manager/resource_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Resource> 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<Resource> 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<Resource> p_ref) const;

public:
GGSResourceManager();
Expand Down

0 comments on commit c658ee1

Please sign in to comment.