Skip to content

Commit

Permalink
Merge pull request #2 from OctoD:feat/tag_manager-additional-methods
Browse files Browse the repository at this point in the history
feat: adds new TagManager methods
  • Loading branch information
OctoD committed May 28, 2024
2 parents 084bc55 + dfb5690 commit b773926
Show file tree
Hide file tree
Showing 8 changed files with 1,275 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ This singleton, has several methods to help you manage the tags of your nodes:
|--------|-------------|
| `add_tag(node: Node, tag: StringName) -> void` | Adds a single tag to a node. |
| `add_tags(node: Node, tags: PackedStringArray) -> void` | Adds multiple tags to a node. |
| `get_nodes_in_tag_path(target: Node, path: String) -> Array[Node]` | Gets all the nodes that are in a specific tag path. |
| `get_tagged_nodes(target: Node) -> Array[Node]` | Gets all the nodes that have tags assigned under the target node. |
| `get_tags(target: Node) -> PackedStringArray` | Gets all the tags assigned to a node. |
| `has_all_tags(tags: PackedStringArray, target: Node) -> bool` | Checks if a node has all the tags assigned. |
| `has_some_tags(tags: PackedStringArray, target: Node) -> bool` | Checks if a node has at least one of the tags assigned. |
| `has_none_tags(tags: PackedStringArray, target: Node) -> bool` | Checks if a node has none of the tags assigned. |
| `is_in_path(target: Node, path: String) -> bool` | Checks if a node is in a specific tag path. |
| `remove_tag(tag: GString, target: Node)` | Removes a single tag from a node. |
| `remove_tags(tags: PackedStringArray, target: Node)` | Removes multiple tags from a node. |
| `set_tags(tags: PackedStringArray, target: Node)` | Sets the tags of a node, removing the previous ones. |
1 change: 1 addition & 0 deletions godot/examples/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var example_scenes: Array[String] = [
"res://examples/get_tag_dictionary_tree_dictionary/get_tag_dictionary_tree_dictionary.tscn",
"res://examples/list_all_tag_dictionaries/list_all_tag_dictionaries.tscn",
"res://examples/tree_tags_querying/tree_tags_querying.tscn",
"res://examples/tag_manager_tagpath_querying/tag_manager_tagpath_querying.tscn",
]

@onready var back_to_menu_button: Button = %BackToMenuButton
Expand Down
267 changes: 267 additions & 0 deletions godot/examples/tag_manager_tagpath_querying/inventory_item.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
### HUGE DISCLAIMER
### DO NOT EVER DO SOMETHING LIKE THIS
### This code is just for the sake of the example, doing this on a real game will hurt your feelings and will make your cat to cry

@tool

extends TextureRect


@export var shared_tag_dictionary: TagDictionary


var armor: Array[String] = [
"helm",
"ring",
"boots",
"chestplate",
"amulet",
"shield"
]


var weapon: Array[String] = [
"axe",
"knife",
"sword",
"wand",
]

var item_prefix: Array[String] = [
"shiny",
"rusty",
"broken",
"enchanted",
"mysterious",
"ancient",
"heavy",
"light",
"sharp",
"blunt",
"magical",
"normal",
"common",
"uncommon",
"rare",
"epic",
"legendary",
"mythical",
"godly",
"divine",
"unholy",
"evil",
"good",
"neutral",
"chaotic",
"lawful",
"fire",
"water",
"earth",
"air",
"lightning",
"ice",
"poison",
"acid",
"dark",
"light",
"void",
"spirit",
"mind",
"body",
"heart",
"soul",
"blood",
"bone",
"metal",
"wood",
"stone",
"glass",
"cloth",
"leather",
"chain",
"plate",
"scale",
"feather",
"fur",
"hair",
"claw",
"fang",
"horn",
"tusk",
"eye",
"tooth",
"tongue",
"tail",
"wing",
"hand",
"foot",
"head",
"neck",
"shoulder",
"chest",
"back",
"arm",
"leg",
"waist",
"hip",
"thigh",
"calf",
"ankle",
"elbow",
"wrist",
"finger",
"toe",
"ear",
"nose",
"mouth",
"beard",
"hair",
"scar",
"tattoo",
"mark",
"symbol",
"rune",
"sigil",
"seal",
"charm",
"amulet",
"pendant",
"necklace",
"ring",
"bracelet",
"anklet",
"belt",
"bag",
"pouch",
"backpack",
"scroll",
"book",
"tome",
"grimoire",
"codex",
"tablet"
]

var item_suffix: Array[String] = [
"of power",
"of might",
"of strength",
"of endurance",
"of agility",
"of dexterity",
"of speed",
"of haste",
"of quickness",
"of swiftness",
"of accuracy",
"of precision",
"of skill",
"of mastery",
"of expertise",
"of proficiency",
"of knowledge",
"of wisdom",
"of intelligence",
"of insight",
"of perception",
"of awareness",
"of consciousness",
"of enlightenment",
"of clarity",
"of focus",
"of concentration",
"of meditation",
"of contemplation",
"of reflection",
"of thought",
"of memory",
"of recall",
"of recollection",
"of remembrance",
"of forgetfulness",
"of oblivion",
"of amnesia",
"of dementia",
"of madness",
"of insanity",
"of lunacy",
"of delirium",
"of confusion",
"of chaos",
"of disorder",
"of entropy",
"of decay",
"of corruption",
"of pollution",
"of contamination",
"of infection",
"of disease",
"of plague",
"of pestilence",
"of famine",
"of drought",
"of flood",
"of storm",
"of hurricane",
"of tornado",
"of earthquake",
"of volcano",
"of eruption",
"of explosion",
"of implosion",
"of collapse",
"of ruin",
"of destruction",
"of annihilation",
"of obliteration",
"of extermination",
]

var item_name = ""
var tag = ""


func _ready():
var prefix = _get_item_prefix()
var itemtype = _get_item_type()
var suffix = _get_item_suffix()

item_name = prefix +" "+ itemtype +" "+ suffix

%Label.text = item_name
self.tooltip_text = item_name

tag = "item"

if armor.has(itemtype):
tag += "." + "armor"
else:
tag += "." + "weapon"

tag += "." + itemtype

TagManager.remove_tags(TagManager.get_tags(self), self)
TagManager.add_tag(tag, self)

register_tag()


func _get_item_prefix() -> String:
return item_prefix[randi_range(0, item_prefix.size() - 1)]


func _get_item_type() -> String:
var types = armor.duplicate()

types.append_array(weapon)

return types[randi_range(0, types.size() - 1)]


func _get_item_suffix() -> String:
return item_suffix[randi_range(0, item_suffix.size() - 1)]


func register_tag() -> void:
if shared_tag_dictionary and tag:
shared_tag_dictionary.add_tag(tag)
38 changes: 38 additions & 0 deletions godot/examples/tag_manager_tagpath_querying/inventory_item.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[gd_scene load_steps=4 format=3 uid="uid://bqv5fslvvyqe0"]

[ext_resource type="Texture2D" uid="uid://c5xjgijg6uw1l" path="res://icon.svg" id="1_0l7ev"]
[ext_resource type="Script" path="res://examples/tag_manager_tagpath_querying/inventory_item.gd" id="1_hngm6"]

[sub_resource type="LabelSettings" id="LabelSettings_grb1m"]
font_size = 8

[node name="TextureRect" type="TextureRect" groups=["octod_ggs_tagged_node_group"]]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 4
size_flags_vertical = 4
tooltip_text = "divine chestplate of proficiency"
texture = ExtResource("1_0l7ev")
stretch_mode = 4
script = ExtResource("1_hngm6")
metadata/octod_ggs_tagged_node = PackedStringArray("item.armor.chestplate")

[node name="Label" type="Label" parent="."]
unique_name_in_owner = true
layout_mode = 1
anchors_preset = 12
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
offset_top = -12.0
grow_horizontal = 2
grow_vertical = 0
text = "divine chestplate of proficiency"
label_settings = SubResource("LabelSettings_grb1m")
horizontal_alignment = 1
vertical_alignment = 2
clip_text = true
text_overrun_behavior = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
@tool

extends Control


@onready var tag_tree: TagTree = %TagTree
@onready var tag_path_selection_container = $InventoryContainer/VBoxContainer/TagPathSelectionContainer


func _get_path(tag: String) -> String:
var chunks = tag.split(".")

chunks.remove_at(chunks.size() - 1)

return ".".join(PackedStringArray(chunks))


func _get_paths(tag_dictionary: TagDictionary) -> Array[String]:
var out: Array[String] = []

for tag in tag_dictionary.tags:
var path = _get_path(tag)

if !out.has(path):
out.append(path)

out.sort()

return out


func _ready():
var tag_dictionary = TagDictionary.new()

for child in %ItemList.get_children():
child.shared_tag_dictionary = tag_dictionary
child.register_tag()

tag_tree.hide_root = true
tag_tree.set_selectable(true)
tag_tree.set_tag_dictionary(tag_dictionary)
tag_tree.set_selected_tags(tag_dictionary.tags)
tag_tree.render_tree()
tag_tree.item_selected.connect(_on_tag_select)

for path in _get_paths(tag_dictionary):
var button = Button.new()
var tags = tag_dictionary.get_tags_from_path(path)

button.text = "Hide all " + path + " items"

button.pressed.connect(func ():
for node in TagManager.get_nodes_in_tag_path(self, path):
node.visible = false

tag_tree.deselect_many_tags(tags)
)

tag_path_selection_container.add_child(button)


func _on_tag_select() -> void:
var selected_tags = tag_tree.get_selected_tags()

for child in %ItemList.get_children():
if child is Control:
child.visible = TagManager.has_some_tags(selected_tags, child)
Loading

0 comments on commit b773926

Please sign in to comment.