audio_stream_editor_plugin.cpp 10.0 KB

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