]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Modules/StyleEditor/Components/style_browser.gd
Squashed commit of the following:
[wolf-seeking-sheep.git] / addons / dialogic / Modules / StyleEditor / Components / style_browser.gd
1 @tool
2 extends Control
3
4 var ListItem := load("res://addons/dialogic/Editor/Common/BrowserItem.tscn")
5 enum Types {ALL, STYLES, LAYER, LAYOUT_BASE}
6
7 var current_type := Types.ALL
8 var style_part_info := []
9 var premade_scenes_reference := {}
10
11 signal activate_part(part_info:Dictionary)
12
13 var current_info := {}
14
15 # Called when the node enters the scene tree for the first time.
16 func _ready() -> void:
17         %Search.right_icon = get_theme_icon("Search", "EditorIcons")
18         %CloseButton.icon = get_theme_icon("Close", "EditorIcons")
19         collect_style_parts()
20
21
22 func collect_style_parts() -> void:
23         for indexer in DialogicUtil.get_indexers():
24                 for layout_part in indexer._get_layout_parts():
25                         style_part_info.append(layout_part)
26                         if not layout_part.get('path', '').is_empty():
27                                 premade_scenes_reference[layout_part['path']] = layout_part
28
29
30 func is_premade_style_part(scene_path:String) -> bool:
31         return scene_path in premade_scenes_reference
32
33
34 func load_parts() -> void:
35         for i in %PartGrid.get_children():
36                 i.queue_free()
37
38         %Search.placeholder_text = "Search for "
39         %Search.text = ""
40         match current_type:
41                 Types.STYLES:
42                         %Search.placeholder_text += "premade styles"
43                 Types.LAYER:
44                         %Search.placeholder_text += "layer scenes"
45                 Types.LAYOUT_BASE:
46                         %Search.placeholder_text += "layout base scenes"
47                 Types.ALL:
48                         %Search.placeholder_text += "styles or layout scenes"
49
50         for info in style_part_info:
51                 var type: String = info.get('type', '_')
52                 match current_type:
53                         Types.STYLES:
54                                 if type != "Style":
55                                         continue
56                         Types.LAYER:
57                                 if type != "Layer":
58                                         continue
59                         Types.LAYOUT_BASE:
60                                 if type != "Layout Base":
61                                         continue
62
63                 var style_item: Node = ListItem.instantiate()
64                 style_item.load_info(info)
65                 %PartGrid.add_child(style_item)
66                 style_item.set_meta('info', info)
67                 style_item.clicked.connect(_on_style_item_clicked.bind(style_item, info))
68                 style_item.focused.connect(_on_style_item_clicked.bind(style_item, info))
69                 style_item.double_clicked.connect(emit_signal.bind('activate_part', info))
70
71         await get_tree().process_frame
72
73         if %PartGrid.get_child_count() > 0:
74                 %PartGrid.get_child(0).clicked.emit()
75                 %PartGrid.get_child(0).grab_focus()
76
77
78 func _on_style_item_clicked(item:Node, info:Dictionary) -> void:
79         load_part_info(info)
80
81
82 func load_part_info(info:Dictionary) -> void:
83         current_info = info
84         %PartTitle.text = info.get('name', 'Unknown Part')
85         %PartAuthor.text = "by "+info.get('author', 'Anonymus')
86         %PartDescription.text = info.get('description', '')
87
88         if info.get('preview_image', null):
89                 %PreviewImage.texture = load(info.get('preview_image')[0])
90                 %PreviewImage.show()
91         else:
92                 %PreviewImage.hide()
93
94
95 func _on_activate_button_pressed() -> void:
96         activate_part.emit(current_info)
97
98
99 func _on_search_text_changed(new_text: String) -> void:
100         for item in %PartGrid.get_children():
101                 if new_text.is_empty():
102                         item.show()
103                         continue
104
105                 if new_text.to_lower() in item.get_meta('info').name.to_lower():
106                         item.show()
107                         continue
108
109                 item.hide()
110
111
112 func _on_close_button_pressed() -> void:
113         get_parent().hide()