Skip to content

Commit

Permalink
🎉 feat: sublist in lists is now possible
Browse files Browse the repository at this point in the history
  • Loading branch information
maktoobgar committed Jan 12, 2023
1 parent 8f6d912 commit d54908a
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
UNRELEASED
----------

* 🎉 feat: sublist in lists is now possible
* 🎉 feat: added sub section basic functionality but not full functionality
* 🎉 feat: basics of sub_section added

Expand Down
12 changes: 7 additions & 5 deletions addons/scene_manager/item_setting.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ class_name ItemSetting

var visibility: bool = true
var categorized: bool = false
var subsection: String = ""

func _init(visibility = true, categorized = false) -> void:
func _init(visibility = true, categorized = false, subsection = "") -> void:
self.visibility = visibility
self.categorized = categorized
self.subsection = subsection

func as_dictionary() -> Dictionary:
return {
"visibility": self.visibility,
"categorized": self.categorized
"subsection": self.subsection,
}

static func dictionary_to_item_setting(input: Dictionary) -> ItemSetting:
var visibility = input["visibility"] if input.has("visibility") else true
var categorized = input["categorized"] if input.has("categorized") else false
return ItemSetting.new(visibility, categorized)
var subsection = input["subsection"] if input.has("subsection") else ""
return ItemSetting.new(visibility, false, subsection)

static func default() -> ItemSetting:
return ItemSetting.new()

func duplicate() -> ItemSetting:
return new(self.visibility, self.categorized)
return new(self.visibility, self.categorized, self.subsection)
4 changes: 2 additions & 2 deletions addons/scene_manager/manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func _add_scene_to_list(list_name: String, scene_name: String, scene_address: St

# Adds an item to a list
#
# This function is used in `scene_item.gd` script and plus doing what is supposed
# This function is used in `scene_item.gd` script and plus doing what it is supposed
# to do, removes and again adds the item in `All` section so that it can be placed
# in currect place in currect section
func add_scene_to_list(list_name: String, scene_name: String, scene_address: String, setting :ItemSetting) -> void:
Expand Down Expand Up @@ -254,8 +254,8 @@ func _reload_scenes() -> void:

# Add scenes that are new and are not into `Scenes` script
var data_values: Array = []
var data_dics = data.values()
if data:
var data_dics = data.values()
for i in range(len(data_dics)):
data_values.append(data_dics[i]["value"])
for key in scenes:
Expand Down
6 changes: 6 additions & 0 deletions addons/scene_manager/popup_button.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@tool
extends Button

# Get and return drag data
func _get_drag_data(at_position: Vector2) -> Variant:
return get_parent()._get_drag_data(at_position)
17 changes: 14 additions & 3 deletions addons/scene_manager/scene_item.gd
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func get_setting() -> ItemSetting:
func set_setting(setting: ItemSetting) -> void:
_setting = setting

# Sets subsection for current item
func set_subsection(node: Control) -> void:
_sub_section = node

# Sets passed theme to normal theme of `key` LineEdit
func custom_set_theme(theme: StyleBox) -> void:
get_key_node().add_theme_stylebox_override("normal", theme)
Expand Down Expand Up @@ -142,8 +146,15 @@ func _on_key_gui_input(event: InputEvent) -> void:

# When added
func _on_tree_entered():
get_parent().get_parent().child_entered()
_sub_section.child_entered()

# When deleted
func _on_tree_exiting():
get_parent().get_parent().child_exiting()
func _on_tree_exited():
_sub_section.child_exited()

# Returns grab data
func _get_drag_data(at_position: Vector2) -> Variant:
return {
"node": self,
"parent": _sub_section,
}
6 changes: 4 additions & 2 deletions addons/scene_manager/scene_item.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[gd_scene load_steps=3 format=3 uid="uid://hh0sw1g7upfc"]
[gd_scene load_steps=4 format=3 uid="uid://hh0sw1g7upfc"]

[ext_resource type="Script" path="res://addons/scene_manager/scene_item.gd" id="2"]
[ext_resource type="Texture2D" uid="uid://bjmufqgg61f7j" path="res://addons/scene_manager/icons/GuiTabMenuHl.svg" id="3"]
[ext_resource type="Script" path="res://addons/scene_manager/popup_button.gd" id="3_nwus0"]

[node name="item" type="HBoxContainer"]
offset_right = 280.0
Expand All @@ -13,6 +14,7 @@ script = ExtResource("2")
custom_minimum_size = Vector2(28, 28)
layout_mode = 2
icon = ExtResource("3")
script = ExtResource("3_nwus0")

[node name="key" type="LineEdit" parent="."]
layout_mode = 2
Expand All @@ -29,7 +31,7 @@ visible = true
hide_on_item_selection = false

[connection signal="tree_entered" from="." to="." method="_on_tree_entered"]
[connection signal="tree_exiting" from="." to="." method="_on_tree_exiting"]
[connection signal="tree_exited" from="." to="." method="_on_tree_exited"]
[connection signal="button_up" from="popup_button" to="." method="_on_popup_button_button_up"]
[connection signal="gui_input" from="key" to="." method="_on_key_gui_input"]
[connection signal="index_pressed" from="popup_menu" to="." method="_on_popup_menu_index_pressed"]
17 changes: 14 additions & 3 deletions addons/scene_manager/scene_list.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ func _ready() -> void:
sub.name = "Uncategorized"
_container.add_child(sub)
sub.open()
sub.hide_delete_button()
_main_subsection = sub

var sub2 = _sub_section.instantiate()
sub2.name = "Categorized"
_container.add_child(sub2)
sub2.hide_delete_button()
_secondary_subsection = sub2
else:
var sub = _sub_section.instantiate()
sub.name = "All"
sub.visible = false
_container.add_child(sub)
sub.open()
sub.hide_delete_button()
_main_subsection = sub
while true:
if _root != null && _root.name == "Scene Manager" || _root.name == "menu":
Expand All @@ -65,7 +69,14 @@ func add_item(key: String, value: String, setting: ItemSetting) -> void:
else:
_secondary_subsection.add_item(item)
else:
_main_subsection.add_item(item)
if setting.subsection != "":
var subsection = find_subsection(setting.subsection)
if subsection:
subsection.add_item(item)
else:
add_subsection(setting.subsection).add_item(item)
else:
_main_subsection.add_item(item)

# Finds and returns a sub_section in the list
func find_subsection(key: String) -> Node:
Expand Down Expand Up @@ -109,7 +120,6 @@ func clear_list() -> void:
# Input example:
# {"scene_key": "scene_address", "scene_key": "scene_address", ...}
func append_scenes(nodes: Dictionary) -> void:
print(_root.call("has_sections", "scene3"))
if name == "All":
for key in nodes:
add_item(key, nodes[key], ItemSetting.new(true, _root.has_sections(nodes[key])))
Expand Down Expand Up @@ -197,10 +207,11 @@ func get_all_sublists() -> Array:
return arr

# Adds a subsection
func add_subsection(text: String) -> void:
func add_subsection(text: String) -> Control:
var sub = _sub_section.instantiate()
sub.name = text.capitalize()
_container.add_child(sub)
return sub

# List deletion
func _on_delete_list_button_up() -> void:
Expand Down
1 change: 0 additions & 1 deletion addons/scene_manager/scene_list.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[ext_resource type="Texture2D" uid="uid://bwd8fj0tm23qi" path="res://addons/scene_manager/icons/eye_open.png" id="4_u7g41"]

[node name="scenes" type="ScrollContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
Expand Down
2 changes: 1 addition & 1 deletion addons/scene_manager/scenes.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
#
extends Node

var scenes: Dictionary = {"_ignore_list":["res://addons"],"_sections":["Character","Menu"],"loading":{"sections":[],"settings":{"All":{"categorized":false,"visibility":true}},"value":"res://demo/loading.tscn"},"scene1":{"sections":[],"settings":{"All":{"categorized":false,"visibility":false}},"value":"res://demo/scene1.tscn"},"scene2":{"sections":["Menu"],"settings":{"All":{"categorized":true,"visibility":true},"Menu":{"categorized":false,"visibility":true}},"value":"res://demo/scene2.tscn"},"scene3":{"sections":["Character","Menu"],"settings":{"All":{"categorized":true,"visibility":true},"Character":{"categorized":false,"visibility":true},"Menu":{"categorized":false,"visibility":true}},"value":"res://demo/scene3.tscn"}}
var scenes: Dictionary = {"_ignore_list":["res://addons"],"_sections":["Character","Menu"],"loading":{"sections":[],"settings":{"All":{"subsection":"","visibility":true}},"value":"res://demo/loading.tscn"},"scene1":{"sections":["Menu"],"settings":{"All":{"subsection":"","visibility":false},"Menu":{"subsection":"Subcategory","visibility":false}},"value":"res://demo/scene1.tscn"},"scene2":{"sections":["Menu"],"settings":{"All":{"subsection":"","visibility":true},"Menu":{"subsection":"","visibility":true}},"value":"res://demo/scene2.tscn"},"scene3":{"sections":["Character","Menu"],"settings":{"All":{"subsection":"","visibility":true},"Character":{"subsection":"Eewew","visibility":true},"Menu":{"subsection":"","visibility":true}},"value":"res://demo/scene3.tscn"}}
68 changes: 59 additions & 9 deletions addons/scene_manager/sub_section.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ extends Control

# Nodes
@onready var button: Button = find_child("Button")
@onready var delete_button: Button = find_child("Delete")
@onready var list: VBoxContainer = find_child("List")
# Open close icons
const _open = preload("res://addons/scene_manager/icons/GuiOptionArrowDown.svg")
const _close = preload("res://addons/scene_manager/icons/GuiOptionArrowRight.png")
# Instances
const _scene_item = preload("res://addons/scene_manager/scene_item.tscn")

# If it is "All" subsection, open it
func _ready() -> void:
Expand All @@ -19,6 +22,9 @@ func add_item(item: Node) -> void:
item._sub_section = self
list.add_child(item)

func remove_item(item: Node) -> void:
list.remove_child(item)

# Open list
func open() -> void:
list.visible = true
Expand All @@ -40,16 +46,60 @@ func _on_button_up():
else:
open()

func _check_count():
if list.get_child_count() == 0:
if name == "All":
visible = false
else:
enable_delete_button()
else:
if name == "All":
visible = true
else:
disable_delete_button()

# When a node adds
func child_entered():
if name == "All" && get_child_count() == 0:
visible = false
else:
visible = true
_check_count()

# When a node removes
func child_exiting():
if name == "All" && get_child_count() - 2 == 0:
visible = false
else:
visible = true
func child_exited():
_check_count()

# Hides delete button of subsection
func hide_delete_button():
delete_button.visible = false

# Disables delete button
func disable_delete_button():
delete_button.disabled = true

# Enables delete button
func enable_delete_button():
delete_button.disabled = false

# Returns if we can drop here or not
func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
if !(data is Dictionary):
return false
data = data as Dictionary
return data.has("node") && data.has("parent")

# Function to actually do the dropping
func _drop_data(at_position: Vector2, data: Variant) -> void:
data = data as Dictionary
var parent = data["parent"] as Node
var node = data["node"] as Node
var setting = node.get_setting() as ItemSetting
if parent == self:
return
parent.remove_item(node)
setting.subsection = name
node.set_setting(setting)
node.set_subsection(self)
add_item(node)
open()

# Button Delete
func _on_delete_button_up():
queue_free()
37 changes: 26 additions & 11 deletions addons/scene_manager/sub_section.tscn
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
[gd_scene load_steps=7 format=3 uid="uid://b4edho3whn67t"]
[gd_scene load_steps=9 format=3 uid="uid://b4edho3whn67t"]

[ext_resource type="Script" path="res://addons/scene_manager/sub_section.gd" id="1_kgwwp"]
[ext_resource type="Texture2D" uid="uid://dme1lg33nbnnh" path="res://addons/scene_manager/icons/GuiOptionArrowRight.png" id="1_yyg5g"]
[ext_resource type="Script" path="res://addons/scene_manager/sub_section_button.gd" id="3_smgrs"]
[ext_resource type="Texture2D" uid="uid://bhejhahjk1yaa" path="res://addons/scene_manager/icons/ImportFail.svg" id="4_chjuj"]

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_7q1ek"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_vsivh"]
bg_color = Color(0.156863, 0.176471, 0.207843, 1)

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_kdkml"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_ngg5t"]
bg_color = Color(0.219608, 0.239216, 0.266667, 1)

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_6o5y8"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_joycj"]
bg_color = Color(0.129412, 0.14902, 0.180392, 1)

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_bin8o"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_3mngj"]
bg_color = Color(0.6, 0.6, 0.6, 0)

[node name="All" type="VBoxContainer"]
offset_right = 1024.0
offset_bottom = 23.0
script = ExtResource("1_kgwwp")

[node name="Button" type="Button" parent="."]
[node name="HBoxContainer" type="HBoxContainer" parent="."]
layout_mode = 2
theme_override_styles/normal = SubResource("StyleBoxFlat_7q1ek")
theme_override_styles/hover = SubResource("StyleBoxFlat_kdkml")
theme_override_styles/pressed = SubResource("StyleBoxFlat_6o5y8")
theme_override_styles/focus = SubResource("StyleBoxFlat_bin8o")

[node name="Button" type="Button" parent="HBoxContainer"]
custom_minimum_size = Vector2(0, 28)
layout_mode = 2
size_flags_horizontal = 3
theme_override_styles/normal = SubResource("StyleBoxFlat_vsivh")
theme_override_styles/hover = SubResource("StyleBoxFlat_ngg5t")
theme_override_styles/pressed = SubResource("StyleBoxFlat_joycj")
theme_override_styles/focus = SubResource("StyleBoxFlat_3mngj")
text = "All"
icon = ExtResource("1_yyg5g")
alignment = 0
script = ExtResource("3_smgrs")

[node name="Delete" type="Button" parent="HBoxContainer"]
custom_minimum_size = Vector2(28, 28)
layout_mode = 2
icon = ExtResource("4_chjuj")
alignment = 0

[node name="List" type="VBoxContainer" parent="."]
visible = false
layout_mode = 2

[connection signal="button_up" from="Button" to="." method="_on_button_up"]
[connection signal="button_up" from="HBoxContainer/Button" to="." method="_on_button_up"]
[connection signal="button_up" from="HBoxContainer/Delete" to="." method="_on_delete_button_up"]
10 changes: 10 additions & 0 deletions addons/scene_manager/sub_section_button.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@tool
extends Button

# Can drop here
func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
return get_parent().get_parent()._can_drop_data(at_position, data)

# Drop here
func _drop_data(at_position: Vector2, data: Variant) -> void:
get_parent().get_parent()._drop_data(at_position, data)

0 comments on commit d54908a

Please sign in to comment.