audio_stream_editor_plugin.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #include "audio_stream_editor_plugin.h"
  31. #include "core/core_string_names.h"
  32. #include "editor/audio_stream_preview.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/editor_settings.h"
  35. #include "scene/resources/audio_stream_wav.h"
  36. // AudioStreamEditor
  37. void AudioStreamEditor::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_READY: {
  40. AudioStreamPreviewGenerator::get_singleton()->connect(SNAME("preview_updated"), callable_mp(this, &AudioStreamEditor::_preview_changed));
  41. } break;
  42. case NOTIFICATION_THEME_CHANGED:
  43. case NOTIFICATION_ENTER_TREE: {
  44. Ref<Font> font = get_theme_font(SNAME("status_source"), SNAME("EditorFonts"));
  45. _current_label->add_theme_font_override(SNAME("font"), font);
  46. _duration_label->add_theme_font_override(SNAME("font"), font);
  47. _play_button->set_icon(get_theme_icon(SNAME("MainPlay"), SNAME("EditorIcons")));
  48. _stop_button->set_icon(get_theme_icon(SNAME("Stop"), SNAME("EditorIcons")));
  49. _preview->set_color(get_theme_color(SNAME("dark_color_2"), SNAME("Editor")));
  50. set_color(get_theme_color(SNAME("dark_color_1"), SNAME("Editor")));
  51. _indicator->queue_redraw();
  52. _preview->queue_redraw();
  53. } break;
  54. case NOTIFICATION_PROCESS: {
  55. _current = _player->get_playback_position();
  56. _indicator->queue_redraw();
  57. } break;
  58. case NOTIFICATION_VISIBILITY_CHANGED: {
  59. if (!is_visible_in_tree()) {
  60. _stop();
  61. }
  62. } break;
  63. default: {
  64. } break;
  65. }
  66. }
  67. void AudioStreamEditor::_draw_preview() {
  68. Rect2 rect = _preview->get_rect();
  69. Size2 size = get_size();
  70. int width = size.width;
  71. if (width <= 0) {
  72. return; // No points to draw.
  73. }
  74. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  75. float preview_len = preview->get_length();
  76. Vector<Vector2> lines;
  77. lines.resize(width * 2);
  78. for (int i = 0; i < width; i++) {
  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. int idx = i;
  84. lines.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);
  85. lines.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);
  86. }
  87. RS::get_singleton()->canvas_item_add_multiline(_preview->get_canvas_item(), lines, { get_theme_color(SNAME("contrast_color_2"), SNAME("Editor")) });
  88. }
  89. void AudioStreamEditor::_preview_changed(ObjectID p_which) {
  90. if (stream.is_valid() && stream->get_instance_id() == p_which) {
  91. _preview->queue_redraw();
  92. }
  93. }
  94. void AudioStreamEditor::_stream_changed() {
  95. if (!is_visible()) {
  96. return;
  97. }
  98. queue_redraw();
  99. }
  100. void AudioStreamEditor::_play() {
  101. if (_player->is_playing()) {
  102. _pausing = true;
  103. _player->stop();
  104. _play_button->set_icon(get_theme_icon(SNAME("MainPlay"), SNAME("EditorIcons")));
  105. set_process(false);
  106. } else {
  107. _pausing = false;
  108. _player->play(_current);
  109. _play_button->set_icon(get_theme_icon(SNAME("Pause"), SNAME("EditorIcons")));
  110. set_process(true);
  111. }
  112. }
  113. void AudioStreamEditor::_stop() {
  114. _player->stop();
  115. _play_button->set_icon(get_theme_icon(SNAME("MainPlay"), SNAME("EditorIcons")));
  116. _current = 0;
  117. _indicator->queue_redraw();
  118. set_process(false);
  119. }
  120. void AudioStreamEditor::_on_finished() {
  121. _play_button->set_icon(get_theme_icon(SNAME("MainPlay"), SNAME("EditorIcons")));
  122. if (!_pausing) {
  123. _current = 0;
  124. _indicator->queue_redraw();
  125. } else {
  126. _pausing = false;
  127. }
  128. set_process(false);
  129. }
  130. void AudioStreamEditor::_draw_indicator() {
  131. if (stream.is_null()) {
  132. return;
  133. }
  134. Rect2 rect = _preview->get_rect();
  135. float len = stream->get_length();
  136. float ofs_x = _current / len * rect.size.width;
  137. const Color col = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  138. Ref<Texture2D> icon = get_theme_icon(SNAME("TimelineIndicator"), SNAME("EditorIcons"));
  139. _indicator->draw_line(Point2(ofs_x, 0), Point2(ofs_x, rect.size.height), col, Math::round(2 * EDSCALE));
  140. _indicator->draw_texture(
  141. icon,
  142. Point2(ofs_x - icon->get_width() * 0.5, 0),
  143. col);
  144. _current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /");
  145. }
  146. void AudioStreamEditor::_on_input_indicator(Ref<InputEvent> p_event) {
  147. const Ref<InputEventMouseButton> mb = p_event;
  148. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  149. if (mb->is_pressed()) {
  150. _seek_to(mb->get_position().x);
  151. }
  152. _dragging = mb->is_pressed();
  153. }
  154. const Ref<InputEventMouseMotion> mm = p_event;
  155. if (mm.is_valid()) {
  156. if (_dragging) {
  157. _seek_to(mm->get_position().x);
  158. }
  159. }
  160. }
  161. void AudioStreamEditor::_seek_to(real_t p_x) {
  162. _current = p_x / _preview->get_rect().size.x * stream->get_length();
  163. _current = CLAMP(_current, 0, stream->get_length());
  164. _player->seek(_current);
  165. _indicator->queue_redraw();
  166. }
  167. void AudioStreamEditor::set_stream(const Ref<AudioStream> &p_stream) {
  168. if (stream.is_valid()) {
  169. stream->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &AudioStreamEditor::_stream_changed));
  170. }
  171. stream = p_stream;
  172. if (stream.is_null()) {
  173. hide();
  174. return;
  175. }
  176. stream->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &AudioStreamEditor::_stream_changed));
  177. _player->set_stream(stream);
  178. _current = 0;
  179. String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s";
  180. _duration_label->set_text(text);
  181. queue_redraw();
  182. }
  183. AudioStreamEditor::AudioStreamEditor() {
  184. set_custom_minimum_size(Size2(1, 100) * EDSCALE);
  185. _player = memnew(AudioStreamPlayer);
  186. _player->connect(SNAME("finished"), callable_mp(this, &AudioStreamEditor::_on_finished));
  187. add_child(_player);
  188. VBoxContainer *vbox = memnew(VBoxContainer);
  189. vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  190. add_child(vbox);
  191. _preview = memnew(ColorRect);
  192. _preview->set_v_size_flags(SIZE_EXPAND_FILL);
  193. _preview->connect(SNAME("draw"), callable_mp(this, &AudioStreamEditor::_draw_preview));
  194. vbox->add_child(_preview);
  195. _indicator = memnew(Control);
  196. _indicator->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  197. _indicator->connect(SNAME("draw"), callable_mp(this, &AudioStreamEditor::_draw_indicator));
  198. _indicator->connect(SNAME("gui_input"), callable_mp(this, &AudioStreamEditor::_on_input_indicator));
  199. _preview->add_child(_indicator);
  200. HBoxContainer *hbox = memnew(HBoxContainer);
  201. hbox->add_theme_constant_override("separation", 0);
  202. vbox->add_child(hbox);
  203. _play_button = memnew(Button);
  204. hbox->add_child(_play_button);
  205. _play_button->set_flat(true);
  206. _play_button->set_focus_mode(Control::FOCUS_NONE);
  207. _play_button->connect(SNAME("pressed"), callable_mp(this, &AudioStreamEditor::_play));
  208. _play_button->set_shortcut(ED_SHORTCUT("audio_stream_editor/audio_preview_play_pause", TTR("Audio Preview Play/Pause"), Key::SPACE));
  209. _stop_button = memnew(Button);
  210. hbox->add_child(_stop_button);
  211. _stop_button->set_flat(true);
  212. _stop_button->set_focus_mode(Control::FOCUS_NONE);
  213. _stop_button->connect(SNAME("pressed"), callable_mp(this, &AudioStreamEditor::_stop));
  214. _current_label = memnew(Label);
  215. _current_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  216. _current_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  217. _current_label->set_modulate(Color(1, 1, 1, 0.5));
  218. hbox->add_child(_current_label);
  219. _duration_label = memnew(Label);
  220. hbox->add_child(_duration_label);
  221. }
  222. // EditorInspectorPluginAudioStream
  223. bool EditorInspectorPluginAudioStream::can_handle(Object *p_object) {
  224. return Object::cast_to<AudioStreamWAV>(p_object) != nullptr;
  225. }
  226. void EditorInspectorPluginAudioStream::parse_begin(Object *p_object) {
  227. AudioStream *stream = Object::cast_to<AudioStream>(p_object);
  228. editor = memnew(AudioStreamEditor);
  229. editor->set_stream(Ref<AudioStream>(stream));
  230. add_custom_control(editor);
  231. }
  232. // AudioStreamEditorPlugin
  233. AudioStreamEditorPlugin::AudioStreamEditorPlugin() {
  234. Ref<EditorInspectorPluginAudioStream> plugin;
  235. plugin.instantiate();
  236. add_inspector_plugin(plugin);
  237. }