Path: blob/master/editor/audio/audio_stream_editor_plugin.cpp
9897 views
/**************************************************************************/1/* audio_stream_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "audio_stream_editor_plugin.h"3132#include "editor/audio/audio_stream_preview.h"33#include "editor/editor_string_names.h"34#include "editor/settings/editor_settings.h"35#include "editor/themes/editor_scale.h"36#include "scene/resources/audio_stream_wav.h"3738// AudioStreamEditor3940void AudioStreamEditor::_notification(int p_what) {41switch (p_what) {42case NOTIFICATION_READY: {43AudioStreamPreviewGenerator::get_singleton()->connect(SNAME("preview_updated"), callable_mp(this, &AudioStreamEditor::_preview_changed));44} break;45case NOTIFICATION_THEME_CHANGED: {46Ref<Font> font = get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts));4748_current_label->add_theme_font_override(SceneStringName(font), font);49_duration_label->add_theme_font_override(SceneStringName(font), font);5051_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));52_stop_button->set_button_icon(get_editor_theme_icon(SNAME("Stop")));53_preview->set_color(get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor)));5455set_color(get_theme_color(SNAME("dark_color_1"), EditorStringName(Editor)));5657_indicator->queue_redraw();58_preview->queue_redraw();59} break;60case NOTIFICATION_PROCESS: {61_current = _player->get_playback_position();62_indicator->queue_redraw();63} break;64case NOTIFICATION_VISIBILITY_CHANGED: {65if (!is_visible_in_tree()) {66_stop();67}68} break;69default: {70} break;71}72}7374void AudioStreamEditor::_draw_preview() {75Size2 size = get_size();76int width = size.width;77if (width <= 0) {78return; // No points to draw.79}8081Rect2 rect = _preview->get_rect();8283Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);84float preview_len = preview->get_length();8586Vector<Vector2> points;87points.resize(width * 2);8889for (int i = 0; i < width; i++) {90float ofs = i * preview_len / size.width;91float ofs_n = (i + 1) * preview_len / size.width;92float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;93float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;9495int idx = i;96points.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);97points.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);98}99100Vector<Color> colors = { get_theme_color(SNAME("contrast_color_2"), EditorStringName(Editor)) };101102RS::get_singleton()->canvas_item_add_multiline(_preview->get_canvas_item(), points, colors);103}104105void AudioStreamEditor::_preview_changed(ObjectID p_which) {106if (stream.is_valid() && stream->get_instance_id() == p_which) {107_preview->queue_redraw();108}109}110111void AudioStreamEditor::_stream_changed() {112if (!is_visible()) {113return;114}115queue_redraw();116}117118void AudioStreamEditor::_play() {119if (_player->is_playing()) {120_pausing = true;121_player->stop();122_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));123set_process(false);124} else {125_pausing = false;126_player->play(_current);127_play_button->set_button_icon(get_editor_theme_icon(SNAME("Pause")));128set_process(true);129}130}131132void AudioStreamEditor::_stop() {133_player->stop();134_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));135_current = 0;136_indicator->queue_redraw();137set_process(false);138}139140void AudioStreamEditor::_on_finished() {141_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));142if (!_pausing) {143_current = 0;144_indicator->queue_redraw();145} else {146_pausing = false;147}148set_process(false);149}150151void AudioStreamEditor::_draw_indicator() {152if (stream.is_null()) {153return;154}155156Rect2 rect = _preview->get_rect();157float len = stream->get_length();158float ofs_x = _current / len * rect.size.width;159const Color col = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));160Ref<Texture2D> icon = get_editor_theme_icon(SNAME("TimelineIndicator"));161_indicator->draw_line(Point2(ofs_x, 0), Point2(ofs_x, rect.size.height), col, Math::round(2 * EDSCALE));162_indicator->draw_texture(163icon,164Point2(ofs_x - icon->get_width() * 0.5, 0),165col);166167_current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /");168}169170void AudioStreamEditor::_on_input_indicator(Ref<InputEvent> p_event) {171const Ref<InputEventMouseButton> mb = p_event;172if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {173if (mb->is_pressed()) {174_seek_to(mb->get_position().x);175}176_dragging = mb->is_pressed();177}178179const Ref<InputEventMouseMotion> mm = p_event;180if (mm.is_valid()) {181if (_dragging) {182_seek_to(mm->get_position().x);183}184}185}186187void AudioStreamEditor::_seek_to(real_t p_x) {188_current = p_x / _preview->get_rect().size.x * stream->get_length();189_current = CLAMP(_current, 0, stream->get_length());190_player->seek(_current);191_indicator->queue_redraw();192}193194void AudioStreamEditor::set_stream(const Ref<AudioStream> &p_stream) {195if (stream.is_valid()) {196stream->disconnect_changed(callable_mp(this, &AudioStreamEditor::_stream_changed));197}198199stream = p_stream;200if (stream.is_null()) {201hide();202return;203}204stream->connect_changed(callable_mp(this, &AudioStreamEditor::_stream_changed));205206_player->set_stream(stream);207_current = 0;208209String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s";210_duration_label->set_text(text);211212queue_redraw();213}214215AudioStreamEditor::AudioStreamEditor() {216set_custom_minimum_size(Size2(1, 100) * EDSCALE);217218_player = memnew(AudioStreamPlayer);219_player->connect(SceneStringName(finished), callable_mp(this, &AudioStreamEditor::_on_finished));220add_child(_player);221222VBoxContainer *vbox = memnew(VBoxContainer);223vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);224add_child(vbox);225226_preview = memnew(ColorRect);227_preview->set_v_size_flags(SIZE_EXPAND_FILL);228_preview->connect(SceneStringName(draw), callable_mp(this, &AudioStreamEditor::_draw_preview));229vbox->add_child(_preview);230231_indicator = memnew(Control);232_indicator->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);233_indicator->connect(SceneStringName(draw), callable_mp(this, &AudioStreamEditor::_draw_indicator));234_indicator->connect(SceneStringName(gui_input), callable_mp(this, &AudioStreamEditor::_on_input_indicator));235_preview->add_child(_indicator);236237HBoxContainer *hbox = memnew(HBoxContainer);238hbox->add_theme_constant_override("separation", 0);239vbox->add_child(hbox);240241_play_button = memnew(Button);242hbox->add_child(_play_button);243_play_button->set_flat(true);244_play_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);245_play_button->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamEditor::_play));246_play_button->set_shortcut(ED_SHORTCUT("audio_stream_editor/audio_preview_play_pause", TTRC("Audio Preview Play/Pause"), Key::SPACE));247_play_button->set_accessibility_name(TTRC("Play"));248249_stop_button = memnew(Button);250hbox->add_child(_stop_button);251_stop_button->set_flat(true);252_stop_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);253_stop_button->connect(SceneStringName(pressed), callable_mp(this, &AudioStreamEditor::_stop));254_stop_button->set_accessibility_name(TTRC("Stop"));255256_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));260hbox->add_child(_current_label);261262_duration_label = memnew(Label);263hbox->add_child(_duration_label);264}265266// EditorInspectorPluginAudioStream267268bool EditorInspectorPluginAudioStream::can_handle(Object *p_object) {269return Object::cast_to<AudioStreamWAV>(p_object) != nullptr;270}271272void EditorInspectorPluginAudioStream::parse_begin(Object *p_object) {273AudioStream *stream = Object::cast_to<AudioStream>(p_object);274275editor = memnew(AudioStreamEditor);276editor->set_stream(Ref<AudioStream>(stream));277278add_custom_control(editor);279}280281// AudioStreamEditorPlugin282283AudioStreamEditorPlugin::AudioStreamEditorPlugin() {284Ref<EditorInspectorPluginAudioStream> plugin;285plugin.instantiate();286add_inspector_plugin(plugin);287}288289290