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) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 "core/io/resource_loader.h"
34 #include "core/project_settings.h"
35 #include "editor/audio_stream_preview.h"
36 #include "editor/editor_scale.h"
37 #include "editor/editor_settings.h"
38 
_notification(int p_what)39 void AudioStreamEditor::_notification(int p_what) {
40 
41 	if (p_what == NOTIFICATION_READY) {
42 		AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", this, "_preview_changed");
43 	}
44 
45 	if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_ENTER_TREE) {
46 		_play_button->set_icon(get_icon("MainPlay", "EditorIcons"));
47 		_stop_button->set_icon(get_icon("Stop", "EditorIcons"));
48 		_preview->set_frame_color(get_color("dark_color_2", "Editor"));
49 		set_frame_color(get_color("dark_color_1", "Editor"));
50 
51 		_indicator->update();
52 		_preview->update();
53 	}
54 
55 	if (p_what == NOTIFICATION_PROCESS) {
56 		_current = _player->get_playback_position();
57 		_indicator->update();
58 	}
59 
60 	if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
61 		if (!is_visible_in_tree()) {
62 			_stop();
63 		}
64 	}
65 }
66 
_draw_preview()67 void AudioStreamEditor::_draw_preview() {
68 	Rect2 rect = _preview->get_rect();
69 	Size2 size = get_size();
70 
71 	Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
72 	float preview_len = preview->get_length();
73 
74 	Vector<Vector2> lines;
75 	lines.resize(size.width * 2);
76 
77 	for (int i = 0; i < size.width; i++) {
78 
79 		float ofs = i * preview_len / size.width;
80 		float ofs_n = (i + 1) * preview_len / size.width;
81 		float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
82 		float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
83 
84 		int idx = i;
85 		lines.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);
86 		lines.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);
87 	}
88 
89 	Vector<Color> color;
90 	color.push_back(get_color("contrast_color_2", "Editor"));
91 
92 	VS::get_singleton()->canvas_item_add_multiline(_preview->get_canvas_item(), lines, color);
93 }
94 
_preview_changed(ObjectID p_which)95 void AudioStreamEditor::_preview_changed(ObjectID p_which) {
96 
97 	if (stream.is_valid() && stream->get_instance_id() == p_which) {
98 		_preview->update();
99 	}
100 }
101 
_changed_callback(Object * p_changed,const char * p_prop)102 void AudioStreamEditor::_changed_callback(Object *p_changed, const char *p_prop) {
103 
104 	if (!is_visible())
105 		return;
106 	update();
107 }
108 
_play()109 void AudioStreamEditor::_play() {
110 
111 	if (_player->is_playing()) {
112 		_player->stop();
113 		_play_button->set_icon(get_icon("MainPlay", "EditorIcons"));
114 		set_process(false);
115 	} else {
116 		_player->play(_current);
117 		_play_button->set_icon(get_icon("Pause", "EditorIcons"));
118 		set_process(true);
119 	}
120 }
121 
_stop()122 void AudioStreamEditor::_stop() {
123 
124 	_player->stop();
125 	_play_button->set_icon(get_icon("MainPlay", "EditorIcons"));
126 	_current = 0;
127 	_indicator->update();
128 	set_process(false);
129 }
130 
_on_finished()131 void AudioStreamEditor::_on_finished() {
132 
133 	_play_button->set_icon(get_icon("MainPlay", "EditorIcons"));
134 	if (_current == _player->get_stream()->get_length()) {
135 		_current = 0;
136 		_indicator->update();
137 	}
138 }
139 
_draw_indicator()140 void AudioStreamEditor::_draw_indicator() {
141 
142 	if (!stream.is_valid()) {
143 		return;
144 	}
145 
146 	Rect2 rect = _preview->get_rect();
147 	float len = stream->get_length();
148 	float ofs_x = _current / len * rect.size.width;
149 	_indicator->draw_line(Point2(ofs_x, 0), Point2(ofs_x, rect.size.height), get_color("accent_color", "Editor"), 1);
150 
151 	_current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /");
152 }
153 
_on_input_indicator(Ref<InputEvent> p_event)154 void AudioStreamEditor::_on_input_indicator(Ref<InputEvent> p_event) {
155 	Ref<InputEventMouseButton> mb = p_event;
156 
157 	if (mb.is_valid()) {
158 		if (mb->is_pressed()) {
159 			_seek_to(mb->get_position().x);
160 		}
161 		_dragging = mb->is_pressed();
162 	}
163 
164 	Ref<InputEventMouseMotion> mm = p_event;
165 
166 	if (mm.is_valid()) {
167 		if (_dragging) {
168 			_seek_to(mm->get_position().x);
169 		}
170 	}
171 }
172 
_seek_to(real_t p_x)173 void AudioStreamEditor::_seek_to(real_t p_x) {
174 	_current = p_x / _preview->get_rect().size.x * stream->get_length();
175 	_current = CLAMP(_current, 0, stream->get_length());
176 	_player->seek(_current);
177 	_indicator->update();
178 }
179 
edit(Ref<AudioStream> p_stream)180 void AudioStreamEditor::edit(Ref<AudioStream> p_stream) {
181 
182 	if (!stream.is_null())
183 		stream->remove_change_receptor(this);
184 
185 	stream = p_stream;
186 	_player->set_stream(stream);
187 	_current = 0;
188 	String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s";
189 	_duration_label->set_text(text);
190 
191 	if (!stream.is_null()) {
192 		stream->add_change_receptor(this);
193 		update();
194 	} else {
195 		hide();
196 	}
197 }
198 
_bind_methods()199 void AudioStreamEditor::_bind_methods() {
200 
201 	ClassDB::bind_method(D_METHOD("_preview_changed"), &AudioStreamEditor::_preview_changed);
202 	ClassDB::bind_method(D_METHOD("_play"), &AudioStreamEditor::_play);
203 	ClassDB::bind_method(D_METHOD("_stop"), &AudioStreamEditor::_stop);
204 	ClassDB::bind_method(D_METHOD("_on_finished"), &AudioStreamEditor::_on_finished);
205 	ClassDB::bind_method(D_METHOD("_draw_preview"), &AudioStreamEditor::_draw_preview);
206 	ClassDB::bind_method(D_METHOD("_draw_indicator"), &AudioStreamEditor::_draw_indicator);
207 	ClassDB::bind_method(D_METHOD("_on_input_indicator"), &AudioStreamEditor::_on_input_indicator);
208 }
209 
AudioStreamEditor()210 AudioStreamEditor::AudioStreamEditor() {
211 
212 	set_custom_minimum_size(Size2(1, 100) * EDSCALE);
213 	_current = 0;
214 	_dragging = false;
215 
216 	_player = memnew(AudioStreamPlayer);
217 	_player->connect("finished", this, "_on_finished");
218 	add_child(_player);
219 
220 	VBoxContainer *vbox = memnew(VBoxContainer);
221 	vbox->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_MINSIZE, 0);
222 	add_child(vbox);
223 
224 	_preview = memnew(ColorRect);
225 	_preview->set_v_size_flags(SIZE_EXPAND_FILL);
226 	_preview->connect("draw", this, "_draw_preview");
227 	vbox->add_child(_preview);
228 
229 	_indicator = memnew(Control);
230 	_indicator->set_anchors_and_margins_preset(PRESET_WIDE);
231 	_indicator->connect("draw", this, "_draw_indicator");
232 	_indicator->connect("gui_input", this, "_on_input_indicator");
233 	_preview->add_child(_indicator);
234 
235 	HBoxContainer *hbox = memnew(HBoxContainer);
236 	hbox->add_constant_override("separation", 0);
237 	vbox->add_child(hbox);
238 
239 	_play_button = memnew(ToolButton);
240 	hbox->add_child(_play_button);
241 	_play_button->set_focus_mode(Control::FOCUS_NONE);
242 	_play_button->connect("pressed", this, "_play");
243 
244 	_stop_button = memnew(ToolButton);
245 	hbox->add_child(_stop_button);
246 	_stop_button->set_focus_mode(Control::FOCUS_NONE);
247 	_stop_button->connect("pressed", this, "_stop");
248 
249 	_current_label = memnew(Label);
250 	_current_label->set_align(Label::ALIGN_RIGHT);
251 	_current_label->set_h_size_flags(SIZE_EXPAND_FILL);
252 	_current_label->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
253 	_current_label->set_modulate(Color(1, 1, 1, 0.5));
254 	hbox->add_child(_current_label);
255 
256 	_duration_label = memnew(Label);
257 	_duration_label->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
258 	hbox->add_child(_duration_label);
259 }
260 
edit(Object * p_object)261 void AudioStreamEditorPlugin::edit(Object *p_object) {
262 
263 	AudioStream *s = Object::cast_to<AudioStream>(p_object);
264 	if (!s)
265 		return;
266 
267 	audio_editor->edit(Ref<AudioStream>(s));
268 }
269 
handles(Object * p_object) const270 bool AudioStreamEditorPlugin::handles(Object *p_object) const {
271 
272 	return p_object->is_class("AudioStream");
273 }
274 
make_visible(bool p_visible)275 void AudioStreamEditorPlugin::make_visible(bool p_visible) {
276 
277 	audio_editor->set_visible(p_visible);
278 }
279 
AudioStreamEditorPlugin(EditorNode * p_node)280 AudioStreamEditorPlugin::AudioStreamEditorPlugin(EditorNode *p_node) {
281 
282 	editor = p_node;
283 	audio_editor = memnew(AudioStreamEditor);
284 	add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, audio_editor);
285 	audio_editor->hide();
286 }
287 
~AudioStreamEditorPlugin()288 AudioStreamEditorPlugin::~AudioStreamEditorPlugin() {
289 }
290