2018-07-16 15:30:27 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_stream_editor_plugin.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2020-01-01 18:16:22 +08:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-07-16 15:30:27 +08:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "audio_stream_editor_plugin.h"
|
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/config/project_settings.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/io/resource_loader.h"
|
2019-12-24 15:17:23 +08:00
|
|
|
#include "editor/audio_stream_preview.h"
|
2020-01-12 15:24:15 +08:00
|
|
|
#include "editor/editor_scale.h"
|
2018-07-16 15:30:27 +08:00
|
|
|
#include "editor/editor_settings.h"
|
|
|
|
|
|
|
|
void AudioStreamEditor::_notification(int p_what) {
|
|
|
|
if (p_what == NOTIFICATION_READY) {
|
2020-02-22 01:28:45 +08:00
|
|
|
AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", callable_mp(this, &AudioStreamEditor::_preview_changed));
|
2018-07-16 15:30:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_ENTER_TREE) {
|
2020-03-12 20:37:40 +08:00
|
|
|
_play_button->set_icon(get_theme_icon("MainPlay", "EditorIcons"));
|
|
|
|
_stop_button->set_icon(get_theme_icon("Stop", "EditorIcons"));
|
2020-10-20 00:34:35 +08:00
|
|
|
_preview->set_color(get_theme_color("dark_color_2", "Editor"));
|
|
|
|
set_color(get_theme_color("dark_color_1", "Editor"));
|
2018-07-16 15:30:27 +08:00
|
|
|
|
|
|
|
_indicator->update();
|
|
|
|
_preview->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_what == NOTIFICATION_PROCESS) {
|
|
|
|
_current = _player->get_playback_position();
|
|
|
|
_indicator->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
|
|
|
|
if (!is_visible_in_tree()) {
|
|
|
|
_stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditor::_draw_preview() {
|
|
|
|
Rect2 rect = _preview->get_rect();
|
|
|
|
Size2 size = get_size();
|
|
|
|
|
|
|
|
Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
|
|
|
|
float preview_len = preview->get_length();
|
|
|
|
|
|
|
|
Vector<Vector2> lines;
|
|
|
|
lines.resize(size.width * 2);
|
|
|
|
|
|
|
|
for (int i = 0; i < size.width; i++) {
|
|
|
|
float ofs = i * preview_len / size.width;
|
|
|
|
float ofs_n = (i + 1) * preview_len / size.width;
|
|
|
|
float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
|
|
|
|
float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
|
|
|
|
|
|
|
|
int idx = i;
|
2018-07-25 09:11:03 +08:00
|
|
|
lines.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);
|
|
|
|
lines.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);
|
2018-07-16 15:30:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Color> color;
|
2020-03-12 20:37:40 +08:00
|
|
|
color.push_back(get_theme_color("contrast_color_2", "Editor"));
|
2018-07-16 15:30:27 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->canvas_item_add_multiline(_preview->get_canvas_item(), lines, color);
|
2018-07-16 15:30:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditor::_preview_changed(ObjectID p_which) {
|
|
|
|
if (stream.is_valid() && stream->get_instance_id() == p_which) {
|
|
|
|
_preview->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditor::_changed_callback(Object *p_changed, const char *p_prop) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!is_visible()) {
|
2018-07-16 15:30:27 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-07-16 15:30:27 +08:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditor::_play() {
|
|
|
|
if (_player->is_playing()) {
|
|
|
|
_player->stop();
|
2020-03-12 20:37:40 +08:00
|
|
|
_play_button->set_icon(get_theme_icon("MainPlay", "EditorIcons"));
|
2018-07-16 15:30:27 +08:00
|
|
|
set_process(false);
|
|
|
|
} else {
|
|
|
|
_player->play(_current);
|
2020-03-12 20:37:40 +08:00
|
|
|
_play_button->set_icon(get_theme_icon("Pause", "EditorIcons"));
|
2018-07-16 15:30:27 +08:00
|
|
|
set_process(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditor::_stop() {
|
|
|
|
_player->stop();
|
2020-03-12 20:37:40 +08:00
|
|
|
_play_button->set_icon(get_theme_icon("MainPlay", "EditorIcons"));
|
2019-06-03 09:40:28 +08:00
|
|
|
_current = 0;
|
|
|
|
_indicator->update();
|
|
|
|
set_process(false);
|
2018-07-16 15:30:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditor::_on_finished() {
|
2020-03-12 20:37:40 +08:00
|
|
|
_play_button->set_icon(get_theme_icon("MainPlay", "EditorIcons"));
|
2019-06-03 09:40:28 +08:00
|
|
|
if (_current == _player->get_stream()->get_length()) {
|
|
|
|
_current = 0;
|
|
|
|
_indicator->update();
|
|
|
|
}
|
2018-07-16 15:30:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditor::_draw_indicator() {
|
|
|
|
if (!stream.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect2 rect = _preview->get_rect();
|
|
|
|
float len = stream->get_length();
|
|
|
|
float ofs_x = _current / len * rect.size.width;
|
2020-03-12 20:37:40 +08:00
|
|
|
_indicator->draw_line(Point2(ofs_x, 0), Point2(ofs_x, rect.size.height), get_theme_color("accent_color", "Editor"), 1);
|
2018-07-16 15:30:27 +08:00
|
|
|
|
|
|
|
_current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditor::_on_input_indicator(Ref<InputEvent> p_event) {
|
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
|
|
|
|
|
|
|
if (mb.is_valid()) {
|
|
|
|
if (mb->is_pressed()) {
|
|
|
|
_seek_to(mb->get_position().x);
|
|
|
|
}
|
|
|
|
_dragging = mb->is_pressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
|
|
|
|
|
|
|
if (mm.is_valid()) {
|
|
|
|
if (_dragging) {
|
|
|
|
_seek_to(mm->get_position().x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditor::_seek_to(real_t p_x) {
|
|
|
|
_current = p_x / _preview->get_rect().size.x * stream->get_length();
|
|
|
|
_current = CLAMP(_current, 0, stream->get_length());
|
|
|
|
_player->seek(_current);
|
|
|
|
_indicator->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditor::edit(Ref<AudioStream> p_stream) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!stream.is_null()) {
|
2018-07-16 15:30:27 +08:00
|
|
|
stream->remove_change_receptor(this);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-07-16 15:30:27 +08:00
|
|
|
|
|
|
|
stream = p_stream;
|
|
|
|
_player->set_stream(stream);
|
|
|
|
_current = 0;
|
|
|
|
String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s";
|
|
|
|
_duration_label->set_text(text);
|
|
|
|
|
|
|
|
if (!stream.is_null()) {
|
|
|
|
stream->add_change_receptor(this);
|
|
|
|
update();
|
|
|
|
} else {
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditor::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioStreamEditor::AudioStreamEditor() {
|
2020-01-12 15:24:15 +08:00
|
|
|
set_custom_minimum_size(Size2(1, 100) * EDSCALE);
|
2018-07-16 15:30:27 +08:00
|
|
|
_current = 0;
|
|
|
|
_dragging = false;
|
|
|
|
|
|
|
|
_player = memnew(AudioStreamPlayer);
|
2020-02-22 01:28:45 +08:00
|
|
|
_player->connect("finished", callable_mp(this, &AudioStreamEditor::_on_finished));
|
2018-07-16 15:30:27 +08:00
|
|
|
add_child(_player);
|
|
|
|
|
|
|
|
VBoxContainer *vbox = memnew(VBoxContainer);
|
|
|
|
vbox->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_MINSIZE, 0);
|
|
|
|
add_child(vbox);
|
|
|
|
|
|
|
|
_preview = memnew(ColorRect);
|
|
|
|
_preview->set_v_size_flags(SIZE_EXPAND_FILL);
|
2020-02-22 01:28:45 +08:00
|
|
|
_preview->connect("draw", callable_mp(this, &AudioStreamEditor::_draw_preview));
|
2018-07-16 15:30:27 +08:00
|
|
|
vbox->add_child(_preview);
|
|
|
|
|
|
|
|
_indicator = memnew(Control);
|
|
|
|
_indicator->set_anchors_and_margins_preset(PRESET_WIDE);
|
2020-02-22 01:28:45 +08:00
|
|
|
_indicator->connect("draw", callable_mp(this, &AudioStreamEditor::_draw_indicator));
|
|
|
|
_indicator->connect("gui_input", callable_mp(this, &AudioStreamEditor::_on_input_indicator));
|
2018-07-16 15:30:27 +08:00
|
|
|
_preview->add_child(_indicator);
|
|
|
|
|
|
|
|
HBoxContainer *hbox = memnew(HBoxContainer);
|
2020-03-12 20:37:40 +08:00
|
|
|
hbox->add_theme_constant_override("separation", 0);
|
2018-07-16 15:30:27 +08:00
|
|
|
vbox->add_child(hbox);
|
|
|
|
|
2020-06-20 02:49:04 +08:00
|
|
|
_play_button = memnew(Button);
|
|
|
|
_play_button->set_flat(true);
|
2018-07-16 15:30:27 +08:00
|
|
|
hbox->add_child(_play_button);
|
|
|
|
_play_button->set_focus_mode(Control::FOCUS_NONE);
|
2020-02-22 01:28:45 +08:00
|
|
|
_play_button->connect("pressed", callable_mp(this, &AudioStreamEditor::_play));
|
2018-07-16 15:30:27 +08:00
|
|
|
|
2020-06-20 02:49:04 +08:00
|
|
|
_stop_button = memnew(Button);
|
|
|
|
_stop_button->set_flat(true);
|
2018-07-16 15:30:27 +08:00
|
|
|
hbox->add_child(_stop_button);
|
|
|
|
_stop_button->set_focus_mode(Control::FOCUS_NONE);
|
2020-02-22 01:28:45 +08:00
|
|
|
_stop_button->connect("pressed", callable_mp(this, &AudioStreamEditor::_stop));
|
2018-07-16 15:30:27 +08:00
|
|
|
|
|
|
|
_current_label = memnew(Label);
|
|
|
|
_current_label->set_align(Label::ALIGN_RIGHT);
|
|
|
|
_current_label->set_h_size_flags(SIZE_EXPAND_FILL);
|
2020-03-12 20:37:40 +08:00
|
|
|
_current_label->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts"));
|
2020-09-03 19:22:16 +08:00
|
|
|
_current_label->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts"));
|
2018-07-16 15:30:27 +08:00
|
|
|
_current_label->set_modulate(Color(1, 1, 1, 0.5));
|
|
|
|
hbox->add_child(_current_label);
|
|
|
|
|
|
|
|
_duration_label = memnew(Label);
|
2020-03-12 20:37:40 +08:00
|
|
|
_duration_label->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts"));
|
2020-09-03 19:22:16 +08:00
|
|
|
_duration_label->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts"));
|
2018-07-16 15:30:27 +08:00
|
|
|
hbox->add_child(_duration_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditorPlugin::edit(Object *p_object) {
|
|
|
|
AudioStream *s = Object::cast_to<AudioStream>(p_object);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!s) {
|
2018-07-16 15:30:27 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-07-16 15:30:27 +08:00
|
|
|
|
|
|
|
audio_editor->edit(Ref<AudioStream>(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioStreamEditorPlugin::handles(Object *p_object) const {
|
|
|
|
return p_object->is_class("AudioStream");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioStreamEditorPlugin::make_visible(bool p_visible) {
|
|
|
|
audio_editor->set_visible(p_visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioStreamEditorPlugin::AudioStreamEditorPlugin(EditorNode *p_node) {
|
|
|
|
editor = p_node;
|
|
|
|
audio_editor = memnew(AudioStreamEditor);
|
|
|
|
add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, audio_editor);
|
|
|
|
audio_editor->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioStreamEditorPlugin::~AudioStreamEditorPlugin() {
|
|
|
|
}
|