diff --git a/examples/licenses/display_licenses.gd b/examples/licenses/display_licenses.gd new file mode 100644 index 0000000..a2069cd --- /dev/null +++ b/examples/licenses/display_licenses.gd @@ -0,0 +1,81 @@ +extends PanelContainer + +const Component := preload("res://addons/licenses/component.gd") +const Licenses := preload("res://addons/licenses/licenses.gd") +const LicenseContainer := preload("res://examples/licenses/license_container.gd") + +## First example with tree. +@export var _tree: Tree +## Second example with item list. +@export var _item_list: ItemList + +## Container to display the license details. +@export var _license_container: LicenseContainer + +@export var _example_selector: TabContainer + +var licenses: Array[Component] = [] + +func _ready() -> void: + # load licenses + var res: Licenses.LoadResult = Licenses.load(Licenses.get_license_data_filepath()) + if res.err_msg != "": + return + self.licenses = res.components + # add engine licenses + self.licenses.append_array(Licenses.get_required_engine_components()) + # sort licenses by name ascending + self.licenses.sort_custom(Licenses.new().compare_components_ascending) + + # first example + self._populate_tree() + # second example + self._populate_item_list() + +## first example with tree +func _populate_tree() -> void: + # if an item in the tree is selected, set the license container's component to the selected component + self._tree.item_selected.connect(self._on_tree_item_selected) + + # populate the tree node + # create a category cache to easily access already created category items + var category_cache: Dictionary = {} + var root: TreeItem = self._tree.create_item(null) + category_cache[""] = root + + # add each component to the tree + for idx: int in range(len(self.licenses)): + var component: Component = self.licenses[idx] + var parent: TreeItem = self._create_category_item(category_cache, component.category, root) + var item: TreeItem = self._tree.create_item(parent) + item.set_text(0, component.name) + item.set_meta("idx", idx) + +## create a category item in the tree +func _create_category_item(category_cache: Dictionary, category: String, root: TreeItem) -> TreeItem: + if category in category_cache: + return category_cache[category] + var category_item: TreeItem + category_item = self._tree.create_item(root) + category_item.set_text(0, category) + category_item.set_selectable(0, false) + category_cache[category] = category_item + return category_item + +## second example with item list +func _populate_item_list() -> void: + # if an item in the item list is selected, set the license container's component to the selected component + self._item_list.item_selected.connect(self._on_item_list_selected) + + # add each component to the item list + for idx: int in range(len(self.licenses)): + var component: Component = self.licenses[idx] + self._item_list.add_item(component.name) + self._item_list.set_item_metadata(idx, idx) + +## update the license display container to show the selected license +func _on_tree_item_selected() -> void: + self._license_container.set_component(self.licenses[self._tree.get_selected().get_meta("idx")]) + +func _on_item_list_selected(idx: int) -> void: + self._license_container.set_component(self.licenses[self._item_list.get_item_metadata(idx)]) diff --git a/examples/licenses/license_selector.gd b/examples/licenses/license_selector.gd deleted file mode 100644 index 30a8097..0000000 --- a/examples/licenses/license_selector.gd +++ /dev/null @@ -1,52 +0,0 @@ -extends Tree - -const Component := preload("res://addons/licenses/component.gd") -const Licenses := preload("res://addons/licenses/licenses.gd") -const LicenseContainer := preload("res://examples/licenses/license_container.gd") - -@export var _license_container: LicenseContainer - -var licenses: Array[Component] = [] - -func _ready() -> void: - self.item_selected.connect(self._on_item_selected) - var res: Licenses.LoadResult = Licenses.load(Licenses.get_license_data_filepath()) - if res.err_msg != "": - return - self.licenses = res.components - self.licenses.append_array(Licenses.get_required_engine_components()) - self.licenses.sort_custom(Licenses.new().compare_components_ascending) - - # create items - var category_cache: Dictionary = {} - var root: TreeItem = self.create_item(null) - category_cache[""] = root - var idx: int = 0 - - while idx < len(self.licenses): - var component: Component = self.licenses[idx] - self._add_component(component, category_cache, root, idx) - idx = idx + 1 - -func _create_category_item(category_cache: Dictionary, category: String, root: TreeItem) -> TreeItem: - if category in category_cache: - return category_cache[category] - var category_item: TreeItem - category_item = self.create_item(root) - category_item.set_text(0, category) - category_item.set_selectable(0, false) - category_cache[category] = category_item - return category_item - -func _add_tree_item(component: Component, idx: int, parent: TreeItem) -> TreeItem: - var item: TreeItem = self.create_item(parent) - item.set_text(0, component.name) - item.set_meta("idx", idx) - return item - -func _add_component(component: Component, category_cache: Dictionary, root: TreeItem, idx: int) -> TreeItem: - var parent: TreeItem = self._create_category_item(category_cache, component.category, root) - return self._add_tree_item(component, idx, parent) - -func _on_item_selected() -> void: - self._license_container.set_component(self.licenses[self.get_selected().get_meta("idx")]) diff --git a/examples/licenses/main.tscn b/examples/licenses/main.tscn index 35e2751..857e722 100644 --- a/examples/licenses/main.tscn +++ b/examples/licenses/main.tscn @@ -1,31 +1,55 @@ [gd_scene load_steps=3 format=3 uid="uid://chr26ux2vsrxq"] -[ext_resource type="Script" path="res://examples/licenses/license_selector.gd" id="1_bba6e"] +[ext_resource type="Script" path="res://examples/licenses/display_licenses.gd" id="1_bba6e"] [ext_resource type="Script" path="res://examples/licenses/license_container.gd" id="2_0yh67"] -[node name="main" type="PanelContainer"] +[node name="main" type="PanelContainer" node_paths=PackedStringArray("_tree", "_item_list", "_license_container", "_example_selector")] anchors_preset = 15 anchor_right = 1.0 anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 +script = ExtResource("1_bba6e") +_tree = NodePath("MarginContainer/HBoxContainer/TabContainer/Tree") +_item_list = NodePath("MarginContainer/HBoxContainer/TabContainer/ItemList") +_license_container = NodePath("MarginContainer/HBoxContainer/PanelContainer/MarginContainer") +_example_selector = NodePath("MarginContainer/HBoxContainer/TabContainer") + +[node name="MarginContainer" type="MarginContainer" parent="."] +layout_mode = 2 +theme_override_constants/margin_left = 8 +theme_override_constants/margin_top = 8 +theme_override_constants/margin_right = 8 +theme_override_constants/margin_bottom = 8 -[node name="HBoxContainer" type="HBoxContainer" parent="."] +[node name="HBoxContainer" type="HBoxContainer" parent="MarginContainer"] layout_mode = 2 -[node name="Tree" type="Tree" parent="HBoxContainer" node_paths=PackedStringArray("_license_container")] +[node name="TabContainer" type="TabContainer" parent="MarginContainer/HBoxContainer"] layout_mode = 2 size_flags_horizontal = 3 size_flags_stretch_ratio = 0.3 +tab_alignment = 1 +current_tab = 1 +use_hidden_tabs_for_min_size = true + +[node name="Tree" type="Tree" parent="MarginContainer/HBoxContainer/TabContainer"] +visible = false +layout_mode = 2 +size_flags_horizontal = 3 hide_root = true -script = ExtResource("1_bba6e") -_license_container = NodePath("../PanelContainer/MarginContainer") +metadata/_tab_index = 0 + +[node name="ItemList" type="ItemList" parent="MarginContainer/HBoxContainer/TabContainer"] +layout_mode = 2 +size_flags_horizontal = 3 +metadata/_tab_index = 1 -[node name="PanelContainer" type="PanelContainer" parent="HBoxContainer"] +[node name="PanelContainer" type="PanelContainer" parent="MarginContainer/HBoxContainer"] layout_mode = 2 size_flags_horizontal = 3 -[node name="MarginContainer" type="MarginContainer" parent="HBoxContainer/PanelContainer" node_paths=PackedStringArray("_name", "_version", "_description", "_contact", "_web", "_license", "_license_text")] +[node name="MarginContainer" type="MarginContainer" parent="MarginContainer/HBoxContainer/PanelContainer" node_paths=PackedStringArray("_name", "_version", "_description", "_contact", "_web", "_license", "_license_text")] layout_mode = 2 theme_override_constants/margin_left = 12 theme_override_constants/margin_top = 12 @@ -40,68 +64,68 @@ _web = NodePath("VBoxContainer/HBoxContainer/web") _license = NodePath("VBoxContainer/HBoxContainer/license") _license_text = NodePath("VBoxContainer/license_text") -[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer/PanelContainer/MarginContainer"] +[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer"] layout_mode = 2 -[node name="name" type="Label" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] +[node name="name" type="Label" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] layout_mode = 2 theme_override_font_sizes/font_size = 24 -[node name="version" type="Label" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] +[node name="version" type="Label" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] layout_mode = 2 theme_override_font_sizes/font_size = 14 -[node name="HSeparator" type="HSeparator" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] +[node name="HSeparator" type="HSeparator" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] layout_mode = 2 -[node name="HBoxContainer" type="GridContainer" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] +[node name="HBoxContainer" type="GridContainer" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] layout_mode = 2 columns = 2 -[node name="label" type="Label" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] +[node name="label" type="Label" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] layout_mode = 2 theme_override_font_sizes/font_size = 16 text = "Description" -[node name="description" type="RichTextLabel" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] +[node name="description" type="RichTextLabel" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] layout_mode = 2 size_flags_horizontal = 3 theme_override_font_sizes/normal_font_size = 16 -[node name="label2" type="Label" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] +[node name="label2" type="Label" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] layout_mode = 2 theme_override_font_sizes/font_size = 16 text = "Contact" -[node name="contact" type="RichTextLabel" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] +[node name="contact" type="RichTextLabel" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] layout_mode = 2 size_flags_horizontal = 3 theme_override_font_sizes/normal_font_size = 16 -[node name="label3" type="Label" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] +[node name="label3" type="Label" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] layout_mode = 2 theme_override_font_sizes/font_size = 16 text = "Web" -[node name="web" type="RichTextLabel" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] +[node name="web" type="RichTextLabel" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] layout_mode = 2 size_flags_horizontal = 3 theme_override_font_sizes/normal_font_size = 16 -[node name="label4" type="Label" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] +[node name="label4" type="Label" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] layout_mode = 2 theme_override_font_sizes/font_size = 16 text = "License" -[node name="license" type="RichTextLabel" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] +[node name="license" type="RichTextLabel" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] layout_mode = 2 size_flags_horizontal = 3 theme_override_font_sizes/normal_font_size = 16 -[node name="HSeparator2" type="HSeparator" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] +[node name="HSeparator2" type="HSeparator" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] layout_mode = 2 -[node name="license_text" type="RichTextLabel" parent="HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] +[node name="license_text" type="RichTextLabel" parent="MarginContainer/HBoxContainer/PanelContainer/MarginContainer/VBoxContainer"] layout_mode = 2 size_flags_vertical = 3 theme_override_font_sizes/normal_font_size = 16