]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Editor/TimelineEditor/timeline_editor.gd
Squashed commit of the following:
[wolf-seeking-sheep.git] / addons / dialogic / Editor / TimelineEditor / timeline_editor.gd
1 @tool
2 extends DialogicEditor
3
4 ## Editor that holds both the visual and the text timeline editors.
5
6 # references
7 enum EditorMode {VISUAL, TEXT}
8
9 var current_editor_mode := EditorMode.VISUAL
10 var play_timeline_button: Button = null
11
12
13 ## Overwrite. Register to the editor manager in here.
14 func _register() -> void:
15         resource_unsaved.connect(_on_resource_unsaved)
16         resource_saved.connect(_on_resource_saved)
17
18         # register editor
19         editors_manager.register_resource_editor('dtl', self)
20         # add timeline button
21         var add_timeline_button: Button = editors_manager.add_icon_button(
22                 load("res://addons/dialogic/Editor/Images/Toolbar/add-timeline.svg"),
23                 "Add Timeline",
24                 self)
25         add_timeline_button.pressed.connect(_on_create_timeline_button_pressed)
26         add_timeline_button.shortcut = Shortcut.new()
27         add_timeline_button.shortcut.events.append(InputEventKey.new())
28         add_timeline_button.shortcut.events[0].keycode = KEY_1
29         add_timeline_button.shortcut.events[0].ctrl_pressed = true
30         # play timeline button
31         play_timeline_button = editors_manager.add_custom_button(
32                 "Play Timeline",
33                 get_theme_icon("PlayScene", "EditorIcons"),
34                 self)
35         play_timeline_button.pressed.connect(play_timeline)
36         play_timeline_button.tooltip_text = "Play the current timeline (CTRL+F5)"
37         if OS.get_name() == "macOS":
38                 play_timeline_button.tooltip_text = "Play the current timeline (CTRL+B)"
39
40         %VisualEditor.load_event_buttons()
41
42         current_editor_mode = DialogicUtil.get_editor_setting('timeline_editor_mode', 0)
43
44         match current_editor_mode:
45                 EditorMode.VISUAL:
46                         %VisualEditor.show()
47                         %TextEditor.hide()
48                         %SwitchEditorMode.text = "Text Editor"
49                 EditorMode.TEXT:
50                         %VisualEditor.hide()
51                         %TextEditor.show()
52                         %SwitchEditorMode.text = "Visual Editor"
53
54         $NoTimelineScreen.show()
55         play_timeline_button.disabled = true
56
57
58 func _get_title() -> String:
59         return "Timeline"
60
61
62 func _get_icon() -> Texture:
63         return get_theme_icon("TripleBar", "EditorIcons")
64
65
66 ## If this editor supports editing resources, load them here (overwrite in subclass)
67 func _open_resource(resource:Resource) -> void:
68         current_resource = resource
69         current_resource_state = ResourceStates.SAVED
70         match current_editor_mode:
71                 EditorMode.VISUAL:
72                         %VisualEditor.load_timeline(current_resource)
73                 EditorMode.TEXT:
74                         %TextEditor.load_timeline(current_resource)
75         $NoTimelineScreen.hide()
76         %TimelineName.text = DialogicResourceUtil.get_unique_identifier(current_resource.resource_path)
77         play_timeline_button.disabled = false
78
79
80 ## If this editor supports editing resources, save them here (overwrite in subclass)
81 func _save() -> void:
82         match current_editor_mode:
83                 EditorMode.VISUAL:
84                         %VisualEditor.save_timeline()
85                 EditorMode.TEXT:
86                         %TextEditor.save_timeline()
87
88
89 func _input(event: InputEvent) -> void:
90         if event is InputEventKey:
91                 var keycode := KEY_F5
92                 if OS.get_name() == "macOS":
93                         keycode = KEY_B
94                 if event.keycode == keycode and event.pressed:
95                         if Input.is_key_pressed(KEY_CTRL):
96                                 play_timeline()
97
98                 if event.keycode == KEY_F and event.pressed:
99                         if Input.is_key_pressed(KEY_CTRL):
100                                 if is_ancestor_of(get_viewport().gui_get_focus_owner()):
101                                         search_timeline()
102
103
104 ## Method to play the current timeline. Connected to the button in the sidebar.
105 func play_timeline() -> void:
106         _save()
107
108         var dialogic_plugin := DialogicUtil.get_dialogic_plugin()
109
110         # Save the current opened timeline
111         DialogicUtil.set_editor_setting('current_timeline_path', current_resource.resource_path)
112
113         DialogicUtil.get_dialogic_plugin().get_editor_interface().play_custom_scene("res://addons/dialogic/Editor/TimelineEditor/test_timeline_scene.tscn")
114
115
116 ## Method to switch from visual to text editor (and vice versa). Connected to the button in the sidebar.
117 func toggle_editor_mode() -> void:
118         match current_editor_mode:
119                 EditorMode.VISUAL:
120                         current_editor_mode = EditorMode.TEXT
121                         %VisualEditor.save_timeline()
122                         %VisualEditor.hide()
123                         %TextEditor.show()
124                         %TextEditor.load_timeline(current_resource)
125                         %SwitchEditorMode.text = "Visual Editor"
126                 EditorMode.TEXT:
127                         current_editor_mode = EditorMode.VISUAL
128                         %TextEditor.save_timeline()
129                         %TextEditor.hide()
130                         %VisualEditor.load_timeline(current_resource)
131                         %VisualEditor.show()
132                         %SwitchEditorMode.text = "Text Editor"
133         _on_search_text_changed(%Search.text)
134         DialogicUtil.set_editor_setting('timeline_editor_mode', current_editor_mode)
135
136
137 func _on_resource_unsaved() -> void:
138         if current_resource:
139                 current_resource.set_meta("timeline_not_saved", true)
140
141
142 func _on_resource_saved() -> void:
143         if current_resource:
144                 current_resource.set_meta("timeline_not_saved", false)
145
146
147 func new_timeline(path:String) -> void:
148         _save()
149         var new_timeline := DialogicTimeline.new()
150         new_timeline.resource_path = path
151         new_timeline.set_meta('timeline_not_saved', true)
152         var err := ResourceSaver.save(new_timeline)
153         EditorInterface.get_resource_filesystem().update_file(new_timeline.resource_path)
154         DialogicResourceUtil.update_directory('dtl')
155         editors_manager.edit_resource(new_timeline)
156
157
158 func _ready() -> void:
159         $NoTimelineScreen.add_theme_stylebox_override("panel", get_theme_stylebox("Background", "EditorStyles"))
160
161         # switch editor mode button
162         %SwitchEditorMode.text = "Text editor"
163         %SwitchEditorMode.icon = get_theme_icon("ArrowRight", "EditorIcons")
164         %SwitchEditorMode.pressed.connect(toggle_editor_mode)
165         %SwitchEditorMode.custom_minimum_size.x = 200 * DialogicUtil.get_editor_scale()
166
167         %SearchClose.icon = get_theme_icon("Close", "EditorIcons")
168         %SearchUp.icon = get_theme_icon("MoveUp", "EditorIcons")
169         %SearchDown.icon = get_theme_icon("MoveDown", "EditorIcons")
170
171
172
173 func _on_create_timeline_button_pressed() -> void:
174         editors_manager.show_add_resource_dialog(
175                         new_timeline,
176                         '*.dtl; DialogicTimeline',
177                         'Create new timeline',
178                         'timeline',
179                         )
180
181
182 func _clear() -> void:
183         current_resource = null
184         current_resource_state = ResourceStates.SAVED
185         match current_editor_mode:
186                 EditorMode.VISUAL:
187                         %VisualEditor.clear_timeline_nodes()
188                 EditorMode.TEXT:
189                         %TextEditor.clear_timeline()
190         $NoTimelineScreen.show()
191         play_timeline_button.disabled = true
192
193
194 func get_current_editor() -> Node:
195         if current_editor_mode == 1:
196                 return %TextEditor
197         return %VisualEditor
198
199 #region SEARCH
200
201 func search_timeline() -> void:
202         %SearchSection.show()
203         if get_viewport().gui_get_focus_owner() is TextEdit:
204                 %Search.text = get_viewport().gui_get_focus_owner().get_selected_text()
205                 _on_search_text_changed(%Search.text)
206         else:
207                 %Search.text = ""
208         %Search.grab_focus()
209
210
211 func _on_close_search_pressed() -> void:
212         %SearchSection.hide()
213         %Search.text = ""
214         _on_search_text_changed('')
215
216
217 func _on_search_text_changed(new_text: String) -> void:
218         var editor: Node = null
219         var anything_found: bool = get_current_editor()._search_timeline(new_text)
220         if anything_found or new_text.is_empty():
221                 %SearchLabel.hide()
222                 %Search.add_theme_color_override("font_color", get_theme_color("font_color", "Editor"))
223         else:
224                 %SearchLabel.show()
225                 %SearchLabel.add_theme_color_override("font_color", get_theme_color("error_color", "Editor"))
226                 %Search.add_theme_color_override("font_color", get_theme_color("error_color", "Editor"))
227                 %SearchLabel.text = "No Match"
228
229
230 func _on_search_down_pressed() -> void:
231         get_current_editor()._search_navigate_down()
232
233
234 func _on_search_up_pressed() -> void:
235         get_current_editor()._search_navigate_up()
236
237 #endregion
238
239