1 | /**************************************************************************/ |
2 | /* audio_stream_editor_plugin.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "audio_stream_editor_plugin.h" |
32 | |
33 | #include "editor/audio_stream_preview.h" |
34 | #include "editor/editor_scale.h" |
35 | #include "editor/editor_settings.h" |
36 | #include "editor/editor_string_names.h" |
37 | #include "scene/resources/audio_stream_wav.h" |
38 | |
39 | // AudioStreamEditor |
40 | |
41 | void AudioStreamEditor::_notification(int p_what) { |
42 | switch (p_what) { |
43 | case NOTIFICATION_READY: { |
44 | AudioStreamPreviewGenerator::get_singleton()->connect(SNAME("preview_updated" ), callable_mp(this, &AudioStreamEditor::_preview_changed)); |
45 | } break; |
46 | case NOTIFICATION_THEME_CHANGED: |
47 | case NOTIFICATION_ENTER_TREE: { |
48 | Ref<Font> font = get_theme_font(SNAME("status_source" ), EditorStringName(EditorFonts)); |
49 | |
50 | _current_label->add_theme_font_override(SNAME("font" ), font); |
51 | _duration_label->add_theme_font_override(SNAME("font" ), font); |
52 | |
53 | _play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay" ))); |
54 | _stop_button->set_icon(get_editor_theme_icon(SNAME("Stop" ))); |
55 | _preview->set_color(get_theme_color(SNAME("dark_color_2" ), EditorStringName(Editor))); |
56 | |
57 | set_color(get_theme_color(SNAME("dark_color_1" ), EditorStringName(Editor))); |
58 | |
59 | _indicator->queue_redraw(); |
60 | _preview->queue_redraw(); |
61 | } break; |
62 | case NOTIFICATION_PROCESS: { |
63 | _current = _player->get_playback_position(); |
64 | _indicator->queue_redraw(); |
65 | } break; |
66 | case NOTIFICATION_VISIBILITY_CHANGED: { |
67 | if (!is_visible_in_tree()) { |
68 | _stop(); |
69 | } |
70 | } break; |
71 | default: { |
72 | } break; |
73 | } |
74 | } |
75 | |
76 | void AudioStreamEditor::_draw_preview() { |
77 | Size2 size = get_size(); |
78 | int width = size.width; |
79 | if (width <= 0) { |
80 | return; // No points to draw. |
81 | } |
82 | |
83 | Rect2 rect = _preview->get_rect(); |
84 | |
85 | Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream); |
86 | float preview_len = preview->get_length(); |
87 | |
88 | Vector<Vector2> points; |
89 | points.resize(width * 2); |
90 | |
91 | for (int i = 0; i < width; i++) { |
92 | float ofs = i * preview_len / size.width; |
93 | float ofs_n = (i + 1) * preview_len / size.width; |
94 | float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5; |
95 | float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5; |
96 | |
97 | int idx = i; |
98 | points.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y); |
99 | points.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y); |
100 | } |
101 | |
102 | Vector<Color> colors = { get_theme_color(SNAME("contrast_color_2" ), EditorStringName(Editor)) }; |
103 | |
104 | RS::get_singleton()->canvas_item_add_multiline(_preview->get_canvas_item(), points, colors); |
105 | } |
106 | |
107 | void AudioStreamEditor::_preview_changed(ObjectID p_which) { |
108 | if (stream.is_valid() && stream->get_instance_id() == p_which) { |
109 | _preview->queue_redraw(); |
110 | } |
111 | } |
112 | |
113 | void AudioStreamEditor::_stream_changed() { |
114 | if (!is_visible()) { |
115 | return; |
116 | } |
117 | queue_redraw(); |
118 | } |
119 | |
120 | void AudioStreamEditor::_play() { |
121 | if (_player->is_playing()) { |
122 | _pausing = true; |
123 | _player->stop(); |
124 | _play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay" ))); |
125 | set_process(false); |
126 | } else { |
127 | _pausing = false; |
128 | _player->play(_current); |
129 | _play_button->set_icon(get_editor_theme_icon(SNAME("Pause" ))); |
130 | set_process(true); |
131 | } |
132 | } |
133 | |
134 | void AudioStreamEditor::_stop() { |
135 | _player->stop(); |
136 | _play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay" ))); |
137 | _current = 0; |
138 | _indicator->queue_redraw(); |
139 | set_process(false); |
140 | } |
141 | |
142 | void AudioStreamEditor::_on_finished() { |
143 | _play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay" ))); |
144 | if (!_pausing) { |
145 | _current = 0; |
146 | _indicator->queue_redraw(); |
147 | } else { |
148 | _pausing = false; |
149 | } |
150 | set_process(false); |
151 | } |
152 | |
153 | void AudioStreamEditor::_draw_indicator() { |
154 | if (stream.is_null()) { |
155 | return; |
156 | } |
157 | |
158 | Rect2 rect = _preview->get_rect(); |
159 | float len = stream->get_length(); |
160 | float ofs_x = _current / len * rect.size.width; |
161 | const Color col = get_theme_color(SNAME("accent_color" ), EditorStringName(Editor)); |
162 | Ref<Texture2D> icon = get_editor_theme_icon(SNAME("TimelineIndicator" )); |
163 | _indicator->draw_line(Point2(ofs_x, 0), Point2(ofs_x, rect.size.height), col, Math::round(2 * EDSCALE)); |
164 | _indicator->draw_texture( |
165 | icon, |
166 | Point2(ofs_x - icon->get_width() * 0.5, 0), |
167 | col); |
168 | |
169 | _current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /" ); |
170 | } |
171 | |
172 | void AudioStreamEditor::_on_input_indicator(Ref<InputEvent> p_event) { |
173 | const Ref<InputEventMouseButton> mb = p_event; |
174 | if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) { |
175 | if (mb->is_pressed()) { |
176 | _seek_to(mb->get_position().x); |
177 | } |
178 | _dragging = mb->is_pressed(); |
179 | } |
180 | |
181 | const Ref<InputEventMouseMotion> mm = p_event; |
182 | if (mm.is_valid()) { |
183 | if (_dragging) { |
184 | _seek_to(mm->get_position().x); |
185 | } |
186 | } |
187 | } |
188 | |
189 | void AudioStreamEditor::_seek_to(real_t p_x) { |
190 | _current = p_x / _preview->get_rect().size.x * stream->get_length(); |
191 | _current = CLAMP(_current, 0, stream->get_length()); |
192 | _player->seek(_current); |
193 | _indicator->queue_redraw(); |
194 | } |
195 | |
196 | void AudioStreamEditor::set_stream(const Ref<AudioStream> &p_stream) { |
197 | if (stream.is_valid()) { |
198 | stream->disconnect_changed(callable_mp(this, &AudioStreamEditor::_stream_changed)); |
199 | } |
200 | |
201 | stream = p_stream; |
202 | if (stream.is_null()) { |
203 | hide(); |
204 | return; |
205 | } |
206 | stream->connect_changed(callable_mp(this, &AudioStreamEditor::_stream_changed)); |
207 | |
208 | _player->set_stream(stream); |
209 | _current = 0; |
210 | |
211 | String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s" ; |
212 | _duration_label->set_text(text); |
213 | |
214 | queue_redraw(); |
215 | } |
216 | |
217 | AudioStreamEditor::AudioStreamEditor() { |
218 | set_custom_minimum_size(Size2(1, 100) * EDSCALE); |
219 | |
220 | _player = memnew(AudioStreamPlayer); |
221 | _player->connect(SNAME("finished" ), callable_mp(this, &AudioStreamEditor::_on_finished)); |
222 | add_child(_player); |
223 | |
224 | VBoxContainer *vbox = memnew(VBoxContainer); |
225 | vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
226 | add_child(vbox); |
227 | |
228 | _preview = memnew(ColorRect); |
229 | _preview->set_v_size_flags(SIZE_EXPAND_FILL); |
230 | _preview->connect(SNAME("draw" ), callable_mp(this, &AudioStreamEditor::_draw_preview)); |
231 | vbox->add_child(_preview); |
232 | |
233 | _indicator = memnew(Control); |
234 | _indicator->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
235 | _indicator->connect(SNAME("draw" ), callable_mp(this, &AudioStreamEditor::_draw_indicator)); |
236 | _indicator->connect(SNAME("gui_input" ), callable_mp(this, &AudioStreamEditor::_on_input_indicator)); |
237 | _preview->add_child(_indicator); |
238 | |
239 | HBoxContainer *hbox = memnew(HBoxContainer); |
240 | hbox->add_theme_constant_override("separation" , 0); |
241 | vbox->add_child(hbox); |
242 | |
243 | _play_button = memnew(Button); |
244 | hbox->add_child(_play_button); |
245 | _play_button->set_flat(true); |
246 | _play_button->set_focus_mode(Control::FOCUS_NONE); |
247 | _play_button->connect(SNAME("pressed" ), callable_mp(this, &AudioStreamEditor::_play)); |
248 | _play_button->set_shortcut(ED_SHORTCUT("audio_stream_editor/audio_preview_play_pause" , TTR("Audio Preview Play/Pause" ), Key::SPACE)); |
249 | |
250 | _stop_button = memnew(Button); |
251 | hbox->add_child(_stop_button); |
252 | _stop_button->set_flat(true); |
253 | _stop_button->set_focus_mode(Control::FOCUS_NONE); |
254 | _stop_button->connect(SNAME("pressed" ), callable_mp(this, &AudioStreamEditor::_stop)); |
255 | |
256 | _current_label = memnew(Label); |
257 | _current_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
258 | _current_label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
259 | _current_label->set_modulate(Color(1, 1, 1, 0.5)); |
260 | hbox->add_child(_current_label); |
261 | |
262 | _duration_label = memnew(Label); |
263 | hbox->add_child(_duration_label); |
264 | } |
265 | |
266 | // EditorInspectorPluginAudioStream |
267 | |
268 | bool EditorInspectorPluginAudioStream::can_handle(Object *p_object) { |
269 | return Object::cast_to<AudioStreamWAV>(p_object) != nullptr; |
270 | } |
271 | |
272 | void EditorInspectorPluginAudioStream::parse_begin(Object *p_object) { |
273 | AudioStream *stream = Object::cast_to<AudioStream>(p_object); |
274 | |
275 | editor = memnew(AudioStreamEditor); |
276 | editor->set_stream(Ref<AudioStream>(stream)); |
277 | |
278 | add_custom_control(editor); |
279 | } |
280 | |
281 | // AudioStreamEditorPlugin |
282 | |
283 | AudioStreamEditorPlugin::AudioStreamEditorPlugin() { |
284 | Ref<EditorInspectorPluginAudioStream> plugin; |
285 | plugin.instantiate(); |
286 | add_inspector_plugin(plugin); |
287 | } |
288 | |