Skip to content

Commit

Permalink
feat: adds create/delete of ItemsPool resources
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Feb 5, 2024
1 parent 051bd59 commit 32b516c
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 13 deletions.
5 changes: 5 additions & 0 deletions project/.ggs/1.0.0/items/items_pool_000.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[gd_resource type="ItemsPool" format=3 uid="uid://bu65750smiusn"]

[resource]
pool_name = &"items_pool_000"
resource_name = "items_pool_000"
15 changes: 8 additions & 7 deletions src/editor_plugin/main_scene/item/ggs_item_main_scene.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ggs_item_main_scene.h"

#include "ggs_equipment_slot_scene.h"
#include "ggs_item_pool_scene.h"

#include <godot_cpp/classes/button.hpp>
#include <godot_cpp/classes/label.hpp>
Expand Down Expand Up @@ -46,8 +47,8 @@ void ItemMainScene::_ready()
Button *items_button = memnew(Button);
VBoxContainer *tab_buttons = memnew(VBoxContainer);
Panel *tabs_panel = memnew(Panel);
Panel *items_panel = memnew(Panel);
EquipmentSlotScene *equipment_slot_scene = memnew(EquipmentSlotScene);
ItemPoolScene *items_pool_scene = memnew(ItemPoolScene);

equipment_slot_scene->hide();

Expand All @@ -59,19 +60,19 @@ void ItemMainScene::_ready()
items_button->connect("pressed", Callable(this, "_handle_tab_pressed").bind(1));
items_button->set_text(tr("Items"));

items_panel->set_anchors_and_offsets_preset(PRESET_FULL_RECT);

tab_buttons->add_child(equipment_button);
tab_buttons->add_child(items_button);

tabs_container->add_child(equipment_slot_scene);
tabs_container->add_child(items_panel);
tabs_container->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
tabs_container->set_h_size_flags(SIZE_EXPAND_FILL);
tabs_container->set_v_size_flags(SIZE_EXPAND_FILL);
tabs_container->add_child(equipment_slot_scene);
tabs_container->add_child(items_pool_scene);

tabs_panel->add_child(tabs_container);
tabs_panel->set_v_size_flags(SIZE_EXPAND_FILL);
tabs_panel->set_h_size_flags(SIZE_EXPAND_FILL);
tabs_panel->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
tabs_panel->set_h_size_flags(SIZE_EXPAND_FILL);
tabs_panel->set_v_size_flags(SIZE_EXPAND_FILL);

add_child(tab_buttons);

Expand Down
139 changes: 139 additions & 0 deletions src/editor_plugin/main_scene/item/ggs_item_pool_scene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include "ggs_item_pool_scene.h"

#include "resource_manager/resource_manager.h"
#include "system/item/items_pool.h"

#include <godot_cpp/classes/h_box_container.hpp>
#include <godot_cpp/classes/button.hpp>
#include <godot_cpp/classes/label.hpp>
#include <godot_cpp/classes/scroll_container.hpp>

using namespace ggs;
using namespace ggs::editor_plugin;

void ItemPoolScene::_handle_add_button_pressed()
{
new_resource_modal->set_size(get_size() / 2);
new_resource_modal->popup_centered();
}

void ItemPoolScene::_handle_create_requested(String p_resource_name)
{
GGSResourceManager *resource_manager = GGSResourceManager::get_singleton();

resource_manager->create_items_pool_resource(p_resource_name);
resource_manager->save_resource(resource_manager->create_items_pool_resource(p_resource_name));

render_tree();

new_resource_modal->hide();
}

void ItemPoolScene::_handle_tree_button_pressed(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index)
{
if (p_id == TreeButtonId::DELETE)
{
GGSResourceManager *resource_manager = GGSResourceManager::get_singleton();
TypedArray<ItemsPool> pools = resource_manager->get_items_pool_resources();

ItemsPool *pool = nullptr;

for (int i = 0; i < pools.size(); i++)
{
Variant pool_variant = pools[i];
ItemsPool *pool_ptr = cast_to<ItemsPool>(pool_variant);

if (pool_ptr && pool_ptr->get_pool_name() == p_item->get_text(0))
{
pool = pool_ptr;
break;
}
}

if (pool)
{
resource_manager->remove_resource(pool);
render_tree();
}
}
}

void ItemPoolScene::_bind_methods()
{
ClassDB::bind_method(D_METHOD("_handle_add_button_pressed"), &ItemPoolScene::_handle_add_button_pressed);
ClassDB::bind_method(D_METHOD("_handle_create_requested", "p_resource_name"), &ItemPoolScene::_handle_create_requested);
ClassDB::bind_method(D_METHOD("_handle_tree_button_pressed", "p_item", "p_column", "p_id", "p_mouse_button_index"), &ItemPoolScene::_handle_tree_button_pressed);
}

void ItemPoolScene::render_tree()
{
item_pool_tree->clear();

TypedArray<ItemsPool> pools = GGSResourceManager::get_singleton()->get_items_pool_resources();
TreeItem *root = item_pool_tree->create_item();

for (int i = 0; i < pools.size(); i++)
{
Variant pool = pools[i];
ItemsPool *pool_ptr = cast_to<ItemsPool>(pool);
TreeItem *item = root->create_child();

item->set_text(0, pool_ptr->get_pool_name());
item->set_text(1, String::num_int64(pool_ptr->get_items().size()));
item->set_text_alignment(1, HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
item->add_button(2, get_theme_icon("Remove", "EditorIcons"), TreeButtonId::DELETE);
}
}

void ItemPoolScene::_ready()
{
set_anchors_and_offsets_preset(PRESET_FULL_RECT);
set_h_size_flags(SIZE_EXPAND_FILL);
set_v_size_flags(SIZE_EXPAND_FILL);

item_pool_tree = memnew(Tree);
item_pool_tree->connect("button_clicked", Callable(this, "_handle_tree_button_pressed"));
item_pool_tree->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
item_pool_tree->set_columns(3);
item_pool_tree->set_column_title(0, "Name");
item_pool_tree->set_column_title(1, "Items stored");
item_pool_tree->set_column_title_alignment(1, HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
item_pool_tree->set_column_title(2, "Actions");
item_pool_tree->set_column_titles_visible(true);
item_pool_tree->set_hide_root(true);
item_pool_tree->set_h_size_flags(SIZE_EXPAND_FILL);
item_pool_tree->set_v_size_flags(SIZE_EXPAND_FILL);

Label *item_pool_label = memnew(Label);
item_pool_label->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
item_pool_label->set_h_size_flags(SIZE_EXPAND_FILL);
item_pool_label->set_text("Items pools");

Button *add_button = memnew(Button);
add_button->connect("pressed", Callable(this, "_handle_add_button_pressed"));
add_button->set_button_icon(get_theme_icon("Add", "EditorIcons"));
add_button->set_text("Add items pool");

HBoxContainer *header = memnew(HBoxContainer);
header->add_child(item_pool_label);
header->add_child(add_button);
header->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
header->set_h_size_flags(SIZE_EXPAND_FILL);

add_child(header);

ScrollContainer *scroll_container = memnew(ScrollContainer);
scroll_container->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
scroll_container->set_h_size_flags(SIZE_EXPAND_FILL);
scroll_container->set_v_size_flags(SIZE_EXPAND_FILL);
scroll_container->add_child(item_pool_tree);

add_child(scroll_container);

render_tree();

new_resource_modal = memnew(NewResourceModal);
new_resource_modal->connect("create_requested", Callable(this, "_handle_create_requested"));

add_child(new_resource_modal);
}
41 changes: 41 additions & 0 deletions src/editor_plugin/main_scene/item/ggs_item_pool_scene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef GGS_ITEM_POOL_SCENE_H
#define GGS_ITEM_POOL_SCENE_H

#include <godot_cpp/classes/v_box_container.hpp>
#include <godot_cpp/classes/tree.hpp>

#include "editor_plugin/main_scene/ggs_new_resource_modal.h"
#include "system/item/items_pool.h"

using namespace godot;

namespace ggs::editor_plugin
{
class ItemPoolScene : public VBoxContainer
{
GDCLASS(ItemPoolScene, VBoxContainer);

private:
enum TreeButtonId
{
DELETE = 0,
};

void _handle_add_button_pressed();
void _handle_create_requested(String p_resource_name);
void _handle_tree_button_pressed(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index);

protected:
static void _bind_methods();

NewResourceModal *new_resource_modal;
Tree *item_pool_tree;

void render_tree();

public:
void _ready() override;
};
}

#endif // GGS_ITEM_POOL_SCENE_H
2 changes: 2 additions & 0 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "editor_plugin/main_scene/attribute/ggs_attribute_main_scene.h"
#include "editor_plugin/main_scene/item/ggs_equipment_slot_scene.h"
#include "editor_plugin/main_scene/item/ggs_item_main_scene.h"
#include "editor_plugin/main_scene/item/ggs_item_pool_scene.h"
#include "editor_plugin/main_scene/tag/ggs_tag_dictionary_item.h"
#include "editor_plugin/main_scene/tag/ggs_tag_main_scene.h"

Expand Down Expand Up @@ -118,6 +119,7 @@ void initialize_ggs_module(ModuleInitializationLevel p_level)
ClassDB::register_internal_class<ggs::editor_plugin::EquipmentSlotScene>();
ClassDB::register_internal_class<ggs::editor_plugin::GGSEditorPlugin>();
ClassDB::register_internal_class<ggs::editor_plugin::ItemMainScene>();
ClassDB::register_internal_class<ggs::editor_plugin::ItemPoolScene>();
ClassDB::register_internal_class<ggs::editor_plugin::MainScene>();
ClassDB::register_internal_class<ggs::editor_plugin::TagDictionaryItem>();
ClassDB::register_internal_class<ggs::editor_plugin::TagDocks>();
Expand Down
9 changes: 5 additions & 4 deletions src/resource_manager/resource_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ Ref<EquipmentSlot> GGSResourceManager::create_equipment_slot_resource(String p_f
return resource;
}

Ref<ItemsPool> GGSResourceManager::create_item_resource(String p_file_name) const
Ref<ItemsPool> GGSResourceManager::create_items_pool_resource(String p_file_name) const
{
Ref<ItemsPool> resource = memnew(ItemsPool);
resource->set_name(p_file_name);
resource->set_name(p_file_name.replace(".tres", ""));
resource->set_path(String(ITEMS_DIR) + "/" + p_file_name);
resource->set_pool_name(p_file_name.replace(".tres", ""));
return resource;
}

Ref<TagDictionary> GGSResourceManager::create_tag_resource(String p_file_name) const
{
Ref<TagDictionary> resource = memnew(TagDictionary);
resource->set_name(p_file_name);
resource->set_name(p_file_name.replace(".tres", ""));
resource->set_path(String(TAGS_DIR) + "/" + p_file_name);
return resource;
}
Expand Down Expand Up @@ -171,7 +172,7 @@ TypedArray<EquipmentSlot> GGSResourceManager::get_equipment_slot_resources() con
return get_resources(EQUIPMENT_DIR);
}

TypedArray<ItemsPool> GGSResourceManager::get_item_resources() const
TypedArray<ItemsPool> GGSResourceManager::get_items_pool_resources() const
{
return get_resources(ITEMS_DIR);
}
Expand Down
6 changes: 4 additions & 2 deletions src/resource_manager/resource_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace ggs
{
class EquipmentSlotScene;
class GGSEditorPlugin;
class ItemPoolScene;
class TagMainScene;
}

Expand All @@ -29,6 +30,7 @@ namespace ggs
protected:
friend class editor_plugin::EquipmentSlotScene;
friend class editor_plugin::GGSEditorPlugin;
friend class editor_plugin::ItemPoolScene;
friend class editor_plugin::TagMainScene;

/// @brief The resource manager's initialization method.
Expand All @@ -41,7 +43,7 @@ namespace ggs
Ref<EquipmentSlot> create_equipment_slot_resource(String p_file_name) const;
/// @brief Creates a new items pool resource.
/// @return The new items pool resource.
Ref<ItemsPool> create_item_resource(String p_file_name) const;
Ref<ItemsPool> create_items_pool_resource(String p_file_name) const;
/// @brief Creates a new tag dictionary resource.
/// @return The new tag dictionary resource.
Ref<TagDictionary> create_tag_resource(String p_file_name) const;
Expand Down Expand Up @@ -77,7 +79,7 @@ namespace ggs
TypedArray<EquipmentSlot> get_equipment_slot_resources() const;
/// @brief Gets all the resources in the items directory.
/// @return The resources in the items directory.
TypedArray<ItemsPool> get_item_resources() const;
TypedArray<ItemsPool> get_items_pool_resources() const;
/// @brief Gets all the resources in the tags directory.
/// @return The resources in the tags directory.
TypedArray<TagDictionary> get_tag_resources() const;
Expand Down

0 comments on commit 32b516c

Please sign in to comment.