Skip to content

Commit

Permalink
feat: adds items pool table
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Feb 8, 2024
1 parent 32b516c commit 6d51d77
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 82 deletions.
1 change: 1 addition & 0 deletions project/.ggs/1.0.0/equipment/weapon_1.tres
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
[resource]
slot_name = &"weapon_1"
accepts_items_with_tags = PackedStringArray("type.weapon.1h", "type.weapon.2h")
denies_items_with_tags = PackedStringArray("type.weapon.shield")
resource_name = "weapon_1.tres"
2 changes: 1 addition & 1 deletion project/.ggs/1.0.0/equipment/weapon_2.tres
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

[resource]
slot_name = &"weapon_2"
accepts_items_with_tags = PackedStringArray("type.weapon.1h")
accepts_items_with_tags = PackedStringArray("type.weapon.1h", "type.weapon.shield")
resource_name = "weapon_2.tres"
8 changes: 8 additions & 0 deletions project/.ggs/1.0.0/items/common_drop_table.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="ItemsPool" load_steps=2 format=3 uid="uid://cq5poyahoqrmu"]

[ext_resource type="Item" uid="uid://cmuoe6r8lr7g" path="res://demos/item_test_ui/items/test_item_000.tres" id="1_a8emk"]

[resource]
items = Array[Item]([ExtResource("1_a8emk"), ExtResource("1_a8emk")])
pool_name = &"items_pool_001"
resource_name = "common_drop_table"
5 changes: 0 additions & 5 deletions project/.ggs/1.0.0/items/items_pool_000.tres

This file was deleted.

8 changes: 8 additions & 0 deletions project/.ggs/1.0.0/items/items_pool_001.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="ItemsPool" load_steps=2 format=3 uid="uid://b2a7tiunflpnh"]

[ext_resource type="Item" uid="uid://cmuoe6r8lr7g" path="res://demos/item_test_ui/items/test_item_000.tres" id="1_ocre2"]

[resource]
items = Array[Item]([ExtResource("1_ocre2")])
pool_name = &"items_pool_002"
resource_name = "items_pool_001"
2 changes: 1 addition & 1 deletion project/.ggs/1.0.0/tags/items.tres
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[gd_resource type="TagDictionary" format=3 uid="uid://dqcwj5qs8prk0"]

[resource]
tags = PackedStringArray("rarity.common", "rarity.magic", "rarity.legendary", "type.weapon.1h", "type.weapon.2h", "type.armor.helm", "type.armor.torso", "type.armor.hands", "type.armor.ring")
tags = PackedStringArray("rarity.common", "rarity.magic", "rarity.legendary", "type.weapon.1h", "type.weapon.2h", "type.armor.helm", "type.armor.torso", "type.armor.hands", "type.armor.ring", "type.weapon.shield")
resource_name = "items.tres"
6 changes: 0 additions & 6 deletions project/demos/item_test_ui/items/test_item_001.tres

This file was deleted.

2 changes: 1 addition & 1 deletion src/editor_plugin/ggs_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void GGSEditorPlugin::_enter_tree()

ggs_tag_main_scene->set_visible(false);

GGSResourceManager::get_singleton()->ensure_directories();
ResourceManager::get_singleton()->ensure_directories();
}

void GGSEditorPlugin::_exit_tree()
Expand Down
4 changes: 2 additions & 2 deletions src/editor_plugin/main_scene/ggs_new_resource_modal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void NewResourceModal::_handle_create_button_pressed()

void NewResourceModal::_handle_line_edit_text_changed(String text)
{
resource_name_label->set_text(GGSResourceManager::get_resource_name_from_name(text));
resource_name_label->set_text(ResourceManager::get_resource_name_from_name(text));
}

void NewResourceModal::_ready()
Expand All @@ -59,7 +59,7 @@ void NewResourceModal::_ready()
HBoxContainer *h_box_container = memnew(HBoxContainer);
VBoxContainer *v_box_container = memnew(VBoxContainer);

GGSResourceManager *resource_manager = GGSResourceManager::get_singleton();
ResourceManager *resource_manager = ResourceManager::get_singleton();

add_child(v_box_container);

Expand Down
17 changes: 8 additions & 9 deletions src/editor_plugin/main_scene/item/ggs_equipment_slot_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void EquipmentSlotScene::_handle_slot_create_requested(String p_name)
return;
}

GGSResourceManager *resource_manager = GGSResourceManager::get_singleton();
ResourceManager *resource_manager = ResourceManager::get_singleton();
Ref<EquipmentSlot> slot = resource_manager->create_equipment_slot_resource(p_name);

resource_manager->save_resource(slot);
Expand All @@ -37,7 +37,7 @@ void EquipmentSlotScene::_handle_slot_remove_confirmed()
return;
}

Error result = GGSResourceManager::get_singleton()->remove_resource(selected_equipment_slot);
Error result = ResourceManager::get_singleton()->remove_resource(selected_equipment_slot);

if (result == OK)
{
Expand Down Expand Up @@ -67,7 +67,7 @@ void EquipmentSlotScene::_handle_item_edited()
if (slot)
{
slot->set_slot_name(slot_name);
GGSResourceManager::get_singleton()->save_resource(slot);
ResourceManager::get_singleton()->save_resource(slot);
}
}

Expand Down Expand Up @@ -133,8 +133,7 @@ void EquipmentSlotScene::_handle_slot_item_name_edited(String p_new_text)
return;
}

Variant slot_variant = EquipmentManager::get_singleton()->slots[selected->get_index()];
EquipmentSlot *slot = cast_to<EquipmentSlot>(slot_variant);
EquipmentSlot *slot = cast_to<EquipmentSlot>(EquipmentManager::get_singleton()->slots[selected->get_index()]);

if (slot == nullptr)
{
Expand All @@ -149,7 +148,7 @@ void EquipmentSlotScene::_handle_slot_item_name_edited(String p_new_text)
slot->set_slot_name(p_new_text);
selected->set_text(0, p_new_text);

GGSResourceManager::get_singleton()->save_resource(slot);
ResourceManager::get_singleton()->save_resource(slot);
}

void EquipmentSlotScene::_handle_tags_changed()
Expand Down Expand Up @@ -192,7 +191,7 @@ void EquipmentSlotScene::_handle_tags_deselected(PackedStringArray p_tags)

slots_tree->get_selected()->set_text(tag_selection_mode, new_text);

GGSResourceManager::get_singleton()->save_resource(slot);
ResourceManager::get_singleton()->save_resource(slot);
}
}

Expand All @@ -218,15 +217,15 @@ void EquipmentSlotScene::_handle_tags_selected(PackedStringArray p_tags)

slots_tree->get_selected()->set_text(tag_selection_mode, new_text);

GGSResourceManager::get_singleton()->save_resource(slot);
ResourceManager::get_singleton()->save_resource(slot);
}
}

void EquipmentSlotScene::_handle_tag_selection_window_closed()
{
selected_equipment_slot = nullptr;
selected_slot_tag_selection_tree->deselect_all();
selected_slot_tag_selection_window->hide();
selected_equipment_slot = nullptr;
}

void EquipmentSlotScene::_render_slots_tree()
Expand Down
80 changes: 71 additions & 9 deletions src/editor_plugin/main_scene/item/ggs_item_pool_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

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

Expand All @@ -17,9 +19,33 @@ void ItemPoolScene::_handle_add_button_pressed()
new_resource_modal->popup_centered();
}

void ItemPoolScene::_handle_cell_selected()
{
TreeItem *selected_item = item_pool_tree->get_selected();
ResourceManager *resource_manager = ResourceManager::get_singleton();
TypedArray<ItemsPool> pools = resource_manager->get_items_pool_resources();
int index = selected_item->get_index();

if (index < 0 || index > pools.size())
{
return;
}

Variant v = pools[index];
Ref<Resource> pool = cast_to<Resource>(v);
EditorInterface::get_singleton()->edit_resource(pool);

Callable callable = Callable(this, "_handle_resource_pool_changed").bind(index);

if (!pool->is_connected("changed", callable))
{
pool->connect("changed", callable, ConnectFlags::CONNECT_DEFERRED);
}
}

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

resource_manager->create_items_pool_resource(p_resource_name);
resource_manager->save_resource(resource_manager->create_items_pool_resource(p_resource_name));
Expand All @@ -29,19 +55,43 @@ void ItemPoolScene::_handle_create_requested(String p_resource_name)
new_resource_modal->hide();
}

void ItemPoolScene::_handle_resource_pool_changed(int p_item_pool_index)
{
TreeItem *item = item_pool_tree->get_selected();

if (p_item_pool_index < 0 || p_item_pool_index >= item->get_index())
{
return;
}

TypedArray<ItemsPool> pools = ResourceManager::get_singleton()->get_items_pool_resources();

if (p_item_pool_index < 0 || p_item_pool_index >= pools.size())
{
return;
}

Variant v = pools[p_item_pool_index];
ItemsPool *pool = cast_to<ItemsPool>(v);

if (pool)
{
item->set_text(1, String::num_int64(pool->get_items().size()));
}
}

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();
ResourceManager *resource_manager = ResourceManager::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);
ItemsPool *pool_ptr = cast_to<ItemsPool>(pools[i]);

if (pool_ptr && pool_ptr->get_pool_name() == p_item->get_text(0))
{
Expand All @@ -61,21 +111,23 @@ void ItemPoolScene::_handle_tree_button_pressed(TreeItem *p_item, int p_column,
void ItemPoolScene::_bind_methods()
{
ClassDB::bind_method(D_METHOD("_handle_add_button_pressed"), &ItemPoolScene::_handle_add_button_pressed);
ClassDB::bind_method(D_METHOD("_handle_cell_selected"), &ItemPoolScene::_handle_cell_selected);
ClassDB::bind_method(D_METHOD("_handle_create_requested", "p_resource_name"), &ItemPoolScene::_handle_create_requested);
ClassDB::bind_method(D_METHOD("_handle_resource_pool_changed", "p_item_pool_index"), &ItemPoolScene::_handle_resource_pool_changed);
ClassDB::bind_method(D_METHOD("_handle_tree_button_pressed", "p_item", "p_column", "p_id", "p_mouse_button_index"), &ItemPoolScene::_handle_tree_button_pressed);
ClassDB::bind_method(D_METHOD("render_tree"), &ItemPoolScene::render_tree);
}

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

TypedArray<ItemsPool> pools = GGSResourceManager::get_singleton()->get_items_pool_resources();
TypedArray<ItemsPool> pools = ResourceManager::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);
ItemsPool *pool_ptr = cast_to<ItemsPool>(pools[i]);
TreeItem *item = root->create_child();

item->set_text(0, pool_ptr->get_pool_name());
Expand All @@ -93,12 +145,12 @@ void ItemPoolScene::_ready()

item_pool_tree = memnew(Tree);
item_pool_tree->connect("button_clicked", Callable(this, "_handle_tree_button_pressed"));
item_pool_tree->connect("cell_selected", Callable(this, "_handle_cell_selected"));
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);
Expand All @@ -109,13 +161,23 @@ void ItemPoolScene::_ready()
item_pool_label->set_h_size_flags(SIZE_EXPAND_FILL);
item_pool_label->set_text("Items pools");

Button *refresh_button = memnew(Button);
refresh_button->connect("pressed", Callable(this, "render_tree"));
refresh_button->set_button_icon(get_theme_icon("Reload", "EditorIcons"));
refresh_button->set_text(tr("Refresh"));

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");
add_button->set_text(tr("Add items pool"));

HSeparator *separator = memnew(HSeparator);
separator->set_custom_minimum_size(Size2(4, 8));

HBoxContainer *header = memnew(HBoxContainer);
header->add_child(item_pool_label);
header->add_child(refresh_button);
header->add_child(separator);
header->add_child(add_button);
header->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
header->set_h_size_flags(SIZE_EXPAND_FILL);
Expand Down
7 changes: 5 additions & 2 deletions src/editor_plugin/main_scene/item/ggs_item_pool_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

#include "editor_plugin/main_scene/ggs_new_resource_modal.h"
#include "system/item/items_pool.h"
Expand All @@ -22,8 +23,10 @@ namespace ggs::editor_plugin
};

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);
void _handle_cell_selected();
void _handle_create_requested(String p_resource_name);
void _handle_resource_pool_changed(int p_item_pool_index);
void _handle_tree_button_pressed(TreeItem *p_item, int p_column, int p_id, int p_mouse_button_index);

protected:
static void _bind_methods();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void TagDictionaryItem::_handle_add_tag_button_pressed()

dictionary->add_tag(tag_name);

GGSResourceManager::get_singleton()->save_resource(dictionary);
ResourceManager::get_singleton()->save_resource(dictionary);

new_tag_line_edit->set_text("");
tag_tree->clear();
Expand Down
8 changes: 4 additions & 4 deletions src/editor_plugin/main_scene/tag/ggs_tag_main_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void TagMainScene::render_tag_dictionaries()

void TagMainScene::_handle_add_tag_dictionary_requested(String p_name)
{
GGSResourceManager *resource_manager = GGSResourceManager::get_singleton();
ResourceManager *resource_manager = ResourceManager::get_singleton();
resource_manager->save_resource(resource_manager->create_tag_resource(p_name));

TagManager::get_singleton()->load_dictionaries();
Expand Down Expand Up @@ -122,7 +122,7 @@ void TagMainScene::_handle_add_tag(TagDictionary *p_dictionary, LineEdit *p_tag_

p_dictionary->add_tag(tag_path);

GGSResourceManager::get_singleton()->save_resource(p_dictionary);
ResourceManager::get_singleton()->save_resource(p_dictionary);

render_tag_dictionaries();
}
Expand All @@ -137,7 +137,7 @@ void TagMainScene::_handle_edit_tag(TagDictionary *p_dictionary, LineEdit *p_tag

p_dictionary->replace_tag_path(old_path, tag_path);

GGSResourceManager::get_singleton()->save_resource(p_dictionary);
ResourceManager::get_singleton()->save_resource(p_dictionary);

render_tag_dictionaries();
}
Expand All @@ -146,7 +146,7 @@ void TagMainScene::_handle_delete_tag(TagDictionary *p_dictionary, String p_tag_
{
p_dictionary->remove_tag_path(p_tag_path);

GGSResourceManager::get_singleton()->save_resource(p_dictionary);
ResourceManager::get_singleton()->save_resource(p_dictionary);

render_tag_dictionaries();
}
Expand Down
4 changes: 2 additions & 2 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ void initialize_ggs_module(ModuleInitializationLevel p_level)
}
else if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR)
{
ClassDB::register_internal_class<ggs::GGSResourceManager>();
ClassDB::register_internal_class<ggs::ResourceManager>();

Engine::get_singleton()->register_singleton("GGSResourceManager", ggs::GGSResourceManager::get_singleton());
Engine::get_singleton()->register_singleton("GGSResourceManager", ggs::ResourceManager::get_singleton());

ClassDB::register_internal_class<ggs::editor_plugin::NewResourceModal>();
ClassDB::register_internal_class<ggs::editor_plugin::AbilityInspectorPlugin>();
Expand Down
Loading

0 comments on commit 6d51d77

Please sign in to comment.