Debian Codegraph @

editor/audio/audio_stream_editor_plugin.cpp

raw symbols SCIP
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/audio_stream_preview.h"
34 #include "editor/editor_string_names.h"
35 #include "editor/settings/editor_settings.h"
36 #include "editor/themes/editor_scale.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 Ref<Font> font = get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts));
48
49 _current_label->add_theme_font_override(SceneStringName(font), font);
50 _duration_label->add_theme_font_override(SceneStringName(font), font);
51
52 _play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
53 _stop_button->set_button_icon(get_editor_theme_icon(SNAME("Stop")));
54 _preview->set_color(get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor)));
55
56 set_color(get_theme_color(SNAME("dark_color_1"), EditorStringName(Editor)));
57
58 _indicator->queue_redraw();
59 _preview->queue_redraw();
60 } break;
61 case NOTIFICATION_PROCESS: {
62 _current = _player->get_playback_position();
63 _indicator->queue_redraw();
64 } break;
65 case NOTIFICATION_VISIBILITY_CHANGED: {
66 if (!is_visible_in_tree()) {
67 _stop();
68 }
69 } break;
70 default: {
71 } break;
72 }
73 }
74
75 void AudioStreamEditor::_draw_preview() {
76 Size2 size = get_size();
77 int width = size.width;
78 if (width <= 0) {
79 return; // No points to draw.
80 }
81
82 Rect2 rect = _preview->get_rect();
83
84 Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
85 float preview_len = preview->get_length();
86
87 Vector<Vector2> points;
88 points.resize(width * 2);
89
90 for (int i = 0; i < width; i++) {
91 float ofs = i * preview_len / size.width;
92 float ofs_n = (i + 1) * preview_len / size.width;
93 float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
94 float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
95
96 int idx = i;
97 points.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);
98 points.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);
99 }
100
101 Vector<Color> colors = { get_theme_color(SNAME("contrast_color_2"), EditorStringName(Editor)) };
102
103 RS::get_singleton()->canvas_item_add_multiline(_preview->get_canvas_item(), points, colors);
104 }
105
106 void AudioStreamEditor::_preview_changed(ObjectID p_which) {
107 if (stream.is_valid() && stream->get_instance_id() == p_which) {
108 _preview->queue_redraw();
109 }
110 }
111
112 void AudioStreamEditor::_stream_changed() {
113 if (!is_visible()) {
114 return;
115 }
116 queue_redraw();
117 }
118
119 void AudioStreamEditor::_play() {
120 if (_player->is_playing()) {
121 _pausing = true;
122 _player->stop();
123 _play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
124 set_process(false);
125 } else {
126 _pausing = false;
127 _player->play(_current);
128 _play_button->set_button_icon(get_editor_theme_icon(SNAME("Pause")));
129 set_process(true);
130 }
131 }
132
133 void AudioStreamEditor::_stop() {
134 _player->stop();
135 _play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
136 _current = 0;
137 _indicator->queue_redraw();
138 set_process(false);
139 }
140
141 void AudioStreamEditor::_on_finished() {
142 _play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
143 if (!_pausing) {
144 _current = 0;
145 _indicator->queue_redraw();
146 } else {
147 _pausing = false;
148 }
149 set_process(false);
150 }
151
152 void AudioStreamEditor::_draw_indicator() {
153 if (stream.is_null()) {
154 return;
155 }
156
157 Rect2 rect = _preview->get_rect();
158 float len = stream->get_length();
159 float ofs_x = _current / len * rect.size.width;
160 const Color col = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
161 Ref<Texture2D> icon = get_editor_theme_icon(SNAME("TimelineIndicator"));
162 _indicator->draw_line(Point2(ofs_x, 0), Point2(ofs_x, rect.size.height), col, Math::round(2 * EDSCALE));
163 _indicator->draw_texture(
164 icon,
165 Point2(ofs_x - icon->get_width() * 0.5, 0),
166 col);
167
168 _current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /");
169 }
170
171 void AudioStreamEditor::_on_input_indicator(Ref<InputEvent> p_event) {
172 const Ref<InputEventMouseButton> mb = p_event;
173 if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
174 if (mb->is_pressed()) {
175 _seek_to(mb->get_position().x);
176 }
177 _dragging = mb->is_pressed();
178 }
179
180 const Ref<InputEventMouseMotion> mm = p_event;
181 if (mm.is_valid()) {
182 if (_dragging) {
183 _seek_to(mm->get_position().x);
184 }
185 }
186 }
187
188 void AudioStreamEditor::_seek_to(real_t p_x) {
189 _current = p_x / _preview->get_rect().size.x * stream->get_length();
190 _current = CLAMP(_current, 0, stream->get_length());
191 _player->seek(_current);
192 _indicator->queue_redraw();
193 }
194
195 void AudioStreamEditor::set_stream(const Ref<AudioStream> &p_stream) {
196 if (stream.is_valid()) {
197 stream->disconnect_changed(callable_mp(this, &AudioStreamEditor::_stream_changed));
198 }
199
200 stream = p_stream;
201 if (stream.is_null()) {
202 hide();
203 return;
204 }
205 stream->connect_changed(callable_mp(this, &AudioStreamEditor::_stream_changed));
206
207 _player->set_stream(stream);
208 _current = 0;
209
210 String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s";
211 _duration_label->set_text(text);
212
213 queue_redraw();
214 }
215
216 AudioStreamEditor::AudioStreamEditor() {
217 set_custom_minimum_size(Size2(1, 100) * EDSCALE);
218
219 _player = memnew(AudioStreamPlayer);
220 _player->connect(SceneStringName(finished), callable_mp(this, &AudioStreamEditor::_on_finished));
221 add_child(_player);
222
223 VBoxContainer *vbox = memnew(VBoxContainer);
224 vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
225 add_child(vbox);
226
227 _preview = memnew(ColorRect);
228 _preview->set_v_size_flags(SIZE_EXPAND_FILL);
229 _preview->connect(SceneStringName(draw), callable_mp(this, &AudioStreamEditor::_draw_preview));
230 vbox->add_child(_preview);
231
232 _indicator = memnew(Control);
233 _indicator->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
234 _indicator->connect(SceneStringName(draw), callable_mp(this, &AudioStreamEditor::_draw_indicator));
235 _indicator->connect(SceneStringName(gui_input), callable_mp(this, &AudioStreamEditor::_on_input_indicator));
236 _preview->add_child(_indicator);
237
238 HBoxContainer *hbox = memnew(HBoxContainer);
239 hbox->add_theme_constant_override("separation", 0);
240 vbox->add_child(hbox);
241
242 _play_button = memnew(Button);
243 hbox->add_child(_play_button);
244 _play_button->set_flat(true);
245 _play_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
246 _play_button->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamEditor::_play));
247 _play_button->set_shortcut(ED_SHORTCUT("audio_stream_editor/audio_preview_play_pause", TTRC("Audio Preview Play/Pause"), Key::SPACE));
248 _play_button->set_accessibility_name(TTRC("Play"));
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_ACCESSIBILITY);
254 _stop_button->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamEditor::_stop));
255 _stop_button->set_accessibility_name(TTRC("Stop"));
256
257 _current_label = memnew(Label);
258 _current_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
259 _current_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
260 _current_label->set_modulate(Color(1, 1, 1, 0.5));
261 hbox->add_child(_current_label);
262
263 _duration_label = memnew(Label);
264 hbox->add_child(_duration_label);
265 }
266
267 // EditorInspectorPluginAudioStream
268
269 bool EditorInspectorPluginAudioStream::can_handle(Object *p_object) {
270 return Object::cast_to<AudioStreamWAV>(p_object) != nullptr;
271 }
272
273 void EditorInspectorPluginAudioStream::parse_begin(Object *p_object) {
274 AudioStream *stream = Object::cast_to<AudioStream>(p_object);
275
276 editor = memnew(AudioStreamEditor);
277 editor->set_stream(Ref<AudioStream>(stream));
278
279 add_custom_control(editor);
280 }
281
282 // AudioStreamEditorPlugin
283
284 AudioStreamEditorPlugin::AudioStreamEditorPlugin() {
285 Ref<EditorInspectorPluginAudioStream> plugin;
286 plugin.instantiate();
287 add_inspector_plugin(plugin);
288 }