Skip to content

Commit

Permalink
Some more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jowan-Spooner committed Sep 22, 2024
1 parent ab91322 commit 5819582
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 133 deletions.
1 change: 0 additions & 1 deletion addons/dialogic/Editor/Common/side_bar.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,4 @@ size = Vector2i(164, 100)
[connection signal="pressed" from="VBoxPrimary/Margin/MainVSplit/VBox/HBoxSearchSort/Options" to="." method="_on_options_pressed"]
[connection signal="toggled" from="VBoxPrimary/Margin/MainVSplit/VBox/HBoxSearchSort/Options/OptionsPopup/OptionsPanel/VBox/FolderColors" to="." method="_on_folder_colors_toggled"]
[connection signal="toggled" from="VBoxPrimary/Margin/MainVSplit/VBox/HBoxSearchSort/Options/OptionsPopup/OptionsPanel/VBox/TrimFolderPaths" to="." method="_on_trim_folder_paths_toggled"]
[connection signal="pressed" from="VBoxPrimary/CurrentVersion" to="." method="_on_current_version_pressed"]
[connection signal="id_pressed" from="RightClickMenu" to="." method="_on_right_click_menu_id_pressed"]
255 changes: 123 additions & 132 deletions addons/dialogic/Editor/Common/sidebar.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class_name DialogicSidebar extends Control

## Script that handles the editor sidebar.

signal file_activated(file_path)
signal content_item_activated(item_name)
signal show_sidebar(show: bool)

Expand Down Expand Up @@ -39,8 +38,8 @@ func _ready() -> void:
func(idx: int): content_item_activated.emit(%ContentList.get_item_text(idx))
)

(%OpenButton as Button).pressed.connect(_show_sidebar)
(%CloseButton as Button).pressed.connect(_hide_sidebar)
%OpenButton.pressed.connect(_show_sidebar)
%CloseButton.pressed.connect(_hide_sidebar)

var editor_scale := DialogicUtil.get_editor_scale()

Expand Down Expand Up @@ -129,11 +128,11 @@ func _hide_sidebar() -> void:
################################################################################


func _on_editors_resource_opened(resource: Resource) -> void:
func _on_editors_resource_opened(_resource: Resource) -> void:
update_resource_list()


func _on_editors_editor_changed(previous: DialogicEditor, current: DialogicEditor) -> void:
func _on_editors_editor_changed(_previous: DialogicEditor, current: DialogicEditor) -> void:
%ContentListSection.visible = current.current_resource is DialogicTimeline
update_resource_list()

Expand Down Expand Up @@ -243,11 +242,12 @@ func update_resource_list(resources_list: PackedStringArray = []) -> void:

# Sort the folder names by their folder name (not by the full path)
var sorted_dir_keys := unique_folder_names.keys()
print(sorted_dir_keys)
sorted_dir_keys.sort_custom(
func(x, y):
return x.get_slice("/", x.get_slice_count("/")-1) < y.get_slice("/", y.get_slice_count("/")-1)
)
var folder_colors := ProjectSettings.get_setting("file_customization/folder_colors", {})
var folder_colors: Dictionary = ProjectSettings.get_setting("file_customization/folder_colors", {})

for dir in sorted_dir_keys:
var display_name: String = dir
Expand All @@ -265,7 +265,7 @@ func update_resource_list(resources_list: PackedStringArray = []) -> void:
var dir_item := add_folder_item(display_name, root, dir_color, dir_path)

for item in dirs[dir_path]:
var tree_item := add_item(item, dir_item, current_file)
add_item(item, dir_item, current_file)


if %CurrentResource.text != "No Resource":
Expand Down Expand Up @@ -307,9 +307,56 @@ func add_folder_item(label: String, parent:TreeItem, color:= Color.BLACK, toolti
folder_item.set_custom_bg_color(0, color)
return folder_item


func get_directory_items(directory:Dictionary, filter:String, icon:Texture2D, resources_list:Array) -> Array:
var items := []
for item_name in directory:
if (directory[item_name] in resources_list) and (filter.is_empty() or filter.to_lower() in item_name.to_lower()):
var item := ResourceListItem.new()
item.text = item_name
item.icon = icon
item.metadata = directory[item_name]
item.tooltip = directory[item_name]
items.append(item)
return items


class ResourceListItem:
extends Object

var text: String
var index: int = -1
var icon: Texture
var metadata: String
var tooltip: String

func _to_string() -> String:
return JSON.stringify(
{
"text": text,
"index": index,
"icon": icon.resource_path,
"metadata": metadata,
"tooltip": tooltip,
"parent_dir": get_parent_directory()
},
"\t",
false
)

func get_parent_directory() -> String:
return (metadata.get_base_dir() as String).split("/")[-1]


func _sort_by_item_text(a: ResourceListItem, b: ResourceListItem) -> bool:
return a.text < b.text

#endregion


#region INTERACTING WITH RESOURCES


func _on_resources_tree_item_activated() -> void:
if resource_tree.get_selected() == null:
return
Expand All @@ -320,38 +367,75 @@ func _on_resources_tree_item_activated() -> void:


func _on_resources_tree_item_clicked(_pos: Vector2, mouse_button_index: int) -> void:
if mouse_button_index == MOUSE_BUTTON_LEFT:
var selected_item := resource_tree.get_selected()
if selected_item == null:
return
if selected_item.get_metadata(0) == null:
return
var resource_item := load(selected_item.get_metadata(0))
call_deferred("edit_resource", resource_item)
return
if mouse_button_index == MOUSE_BUTTON_MIDDLE:
remove_item_from_list(resource_tree.get_selected())
return
if mouse_button_index == MOUSE_BUTTON_RIGHT:
if resource_tree.get_selected().get_metadata(0):
%RightClickMenu.popup_on_parent(Rect2(get_global_mouse_position(), Vector2()))
(%RightClickMenu as PopupMenu).set_meta("item_clicked", resource_tree.get_selected())
return
match mouse_button_index:
MOUSE_BUTTON_LEFT:
var selected_item := resource_tree.get_selected()
if selected_item == null:
return
if selected_item.get_metadata(0) == null:
return
var resource_item := load(selected_item.get_metadata(0))
call_deferred("edit_resource", resource_item)

MOUSE_BUTTON_MIDDLE:
remove_item_from_list(resource_tree.get_selected())

MOUSE_BUTTON_RIGHT:
if resource_tree.get_selected().get_metadata(0):
%RightClickMenu.popup_on_parent(Rect2(get_global_mouse_position(), Vector2()))
%RightClickMenu.set_meta("item_clicked", resource_tree.get_selected())


func edit_resource(resource_item: Variant) -> void:
if resource_item is Resource:
editors_manager.edit_resource(resource_item)
else:
editors_manager.edit_resource(load(resource_item))


func remove_item_from_list(item: TreeItem) -> void:
var new_list := []
for entry in DialogicUtil.get_editor_setting("last_resources", []):
if entry != item.get_metadata(0):
new_list.append(entry)
DialogicUtil.set_editor_setting("last_resources", new_list)
update_resource_list(new_list)


func _on_right_click_menu_id_pressed(id: int) -> void:
match id:
1: # REMOVE ITEM FROM LIST
remove_item_from_list(%RightClickMenu.get_meta("item_clicked"))
2: # OPEN IN FILESYSTEM
EditorInterface.get_file_system_dock().navigate_to_path(
%RightClickMenu.get_meta("item_clicked").get_metadata(0)
)
3: # OPEN IN EXTERNAL EDITOR
OS.shell_open(
ProjectSettings.globalize_path(
%RightClickMenu.get_meta("item_clicked").get_metadata(0)
)
)
4: # COPY IDENTIFIER
DisplayServer.clipboard_set(
DialogicResourceUtil.get_unique_identifier(
%RightClickMenu.get_meta("item_clicked").get_metadata(0)
)
)
#endregion


func _on_search_text_changed(new_text: String) -> void:
#region FILTERING

func _on_search_text_changed(_new_text: String) -> void:
update_resource_list()
var tree_root := resource_tree.get_root()
var tree_items := tree_root.get_children()
if tree_items.size() == 0:
return
for item in tree_items:
for item in resource_tree.get_root().get_children():
if item.get_children().size() > 0:
resource_tree.set_selected(item.get_child(0), 0)
break


func _on_search_text_submitted(new_text: String) -> void:
func _on_search_text_submitted(_new_text: String) -> void:
if resource_tree.get_selected() == null:
return
var item := resource_tree.get_selected()
Expand All @@ -360,6 +444,7 @@ func _on_search_text_submitted(new_text: String) -> void:
edit_resource(item.get_metadata(0))
%Search.clear()

#endregion


#region CONTENT LIST
Expand Down Expand Up @@ -396,119 +481,23 @@ func update_content_list(list: PackedStringArray) -> void:
#endregion


func remove_item_from_list(item: TreeItem) -> void:
var new_list := []
for entry in DialogicUtil.get_editor_setting("last_resources", []):
if entry != item.get_metadata(0):
new_list.append(entry)
DialogicUtil.set_editor_setting("last_resources", new_list)
update_resource_list(new_list)

#region RESOURCE LIST OPTIONS

func _on_right_click_menu_id_pressed(id: int) -> void:
match id:
1: # REMOVE ITEM FROM LIST
remove_item_from_list(%RightClickMenu.get_meta("item_clicked"))
2: # OPEN IN FILESYSTEM
EditorInterface.get_file_system_dock().navigate_to_path(
%RightClickMenu.get_meta("item_clicked").get_metadata(0)
)
3: # OPEN IN EXTERNAL EDITOR
OS.shell_open(
ProjectSettings.globalize_path(
%RightClickMenu.get_meta("item_clicked").get_metadata(0)
)
)
4: # COPY IDENTIFIER
DisplayServer.clipboard_set(
DialogicResourceUtil.get_unique_identifier(
%RightClickMenu.get_meta("item_clicked").get_metadata(0)
)
)
func _on_options_pressed() -> void:
%OptionsPopup.popup_on_parent(Rect2(%Options.global_position+%Options.size*Vector2(0,1), Vector2()))


func _on_grouping_changed(idx: int) -> void:
var id : int = %GroupingOptions.get_item_id(idx)
var id: int = %GroupingOptions.get_item_id(idx)
if (GroupMode as Dictionary).values().has(id):
group_mode = id
group_mode = (id as GroupMode)
DialogicUtil.set_editor_setting("sidebar_group_mode", id)
update_resource_list()
else:
group_mode = GroupMode.TYPE
print("Invalid sort mode: ", id)

%FolderColors.disabled = group_mode != GroupMode.PATH
%TrimFolderPaths.disabled = group_mode != GroupMode.PATH


func _sort_by_item_text(a: ResourceListItem, b: ResourceListItem) -> bool:
return a.text < b.text


func edit_resource(resource_item: Variant) -> void:
if resource_item is Resource:
editors_manager.edit_resource(resource_item)
else:
editors_manager.edit_resource(load(resource_item))


func get_directory_items(directory:Dictionary, filter:String, icon:Texture2D, resources_list:Array) -> Array:
var items := []
for item_name in directory:
if (directory[item_name] in resources_list) and (filter.is_empty() or filter.to_lower() in item_name.to_lower()):
var item := ResourceListItem.new()
item.text = item_name
item.icon = icon
item.metadata = directory[item_name]
item.tooltip = directory[item_name]
items.append(item)
return items


class ResourceListItem:
extends Object

var text: String
var index: int = -1
var icon: Texture
var metadata: String
var tooltip: String

func _to_string() -> String:
return JSON.stringify(
{
"text": text,
"index": index,
"icon": icon.resource_path,
"metadata": metadata,
"tooltip": tooltip,
"parent_dir": get_parent_directory()
},
"\t",
false
)

func add_to_item_list(item_list: ItemList, current_file: String) -> void:
item_list.add_item(text, icon)
item_list.set_item_metadata(item_list.item_count - 1, metadata)
item_list.set_item_tooltip(item_list.item_count - 1, tooltip)

func get_parent_directory() -> String:
return (metadata.get_base_dir() as String).split("/")[-1]

func current_file(sidebar: Control, resource_list: ItemList, current_file: String) -> void:
if metadata == current_file:
resource_list.select(index)
resource_list.set_item_custom_fg_color(
index, resource_list.get_theme_color("accent_color", "Editor")
)
sidebar.find_child("CurrentResource").text = metadata.get_file()


func _on_options_pressed() -> void:
%OptionsPopup.popup_on_parent(Rect2(%Options.global_position+%Options.size*Vector2(0,1), Vector2()))


func _on_folder_colors_toggled(toggled_on: bool) -> void:
DialogicUtil.set_editor_setting("sidebar_use_folder_colors", toggled_on)
update_resource_list()
Expand All @@ -518,6 +507,8 @@ func _on_trim_folder_paths_toggled(toggled_on: bool) -> void:
DialogicUtil.set_editor_setting("sidebar_trim_folder_paths", toggled_on)
update_resource_list()

#endregion


func _on_main_v_split_dragged(offset: int) -> void:
DialogicUtil.set_editor_setting("sidebar_v_split", offset)

0 comments on commit 5819582

Please sign in to comment.