audio_stream_editor_plugin.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #include "audio_stream_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/project_settings.h"
  33. #include "editor/audio_stream_preview.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor/editor_settings.h"
  36. void AudioStreamEditor::_notification(int p_what) {
  37. if (p_what == NOTIFICATION_READY) {
  38. AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", callable_mp(this, &AudioStreamEditor::_preview_changed));
  39. }
  40. if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_ENTER_TREE) {
  41. _play_button->set_icon(get_theme_icon("MainPlay", "EditorIcons"));
  42. _stop_button->set_icon(get_theme_icon("Stop", "EditorIcons"));
  43. _preview->set_frame_color(get_theme_color("dark_color_2", "Editor"));
  44. set_frame_color(get_theme_color("dark_color_1", "Editor"));
  45. _indicator->update();
  46. _preview->update();
  47. }
  48. if (p_what == NOTIFICATION_PROCESS) {
  49. _current = _player->get_playback_position();
  50. _indicator->update();
  51. }
  52. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  53. if (!is_visible_in_tree()) {
  54. _stop();
  55. }
  56. }
  57. }
  58. void AudioStreamEditor::_draw_preview() {
  59. Rect2 rect = _preview->get_rect();
  60. Size2 size = get_size();
  61. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
  62. float preview_len = preview->get_length();
  63. Vector<Vector2> lines;
  64. lines.resize(size.width * 2);
  65. for (int i = 0; i < size.width; i++) {
  66. float ofs = i * preview_len / size.width;
  67. float ofs_n = (i + 1) * preview_len / size.width;
  68. float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
  69. float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
  70. int idx = i;
  71. lines.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);
  72. lines.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);
  73. }
  74. Vector<Color> color;
  75. color.push_back(get_theme_color("contrast_color_2", "Editor"));
  76. RS::get_singleton()->canvas_item_add_multiline(_preview->get_canvas_item(), lines, color);
  77. }
  78. void AudioStreamEditor::_preview_changed(ObjectID p_which) {
  79. if (stream.is_valid() && stream->get_instance_id() == p_which) {
  80. _preview->update();
  81. }
  82. }
  83. void AudioStreamEditor::_changed_callback(Object *p_changed, const char *p_prop) {
  84. if (!is_visible())
  85. return;
  86. update();
  87. }
  88. void AudioStreamEditor::_play() {
  89. if (_player->is_playing()) {
  90. _player->stop();
  91. _play_button->set_icon(get_theme_icon("MainPlay", "EditorIcons"));
  92. set_process(false);
  93. } else {
  94. _player->play(_current);
  95. _play_button->set_icon(get_theme_icon("Pause", "EditorIcons"));
  96. set_process(true);
  97. }
  98. }
  99. void AudioStreamEditor::_stop() {
  100. _player->stop();
  101. _play_button->set_icon(get_theme_icon("MainPlay", "EditorIcons"));
  102. _current = 0;
  103. _indicator->update();
  104. set_process(false);
  105. }
  106. void AudioStreamEditor::_on_finished() {
  107. _play_button->set_icon(get_theme_icon("MainPlay", "EditorIcons"));
  108. if (_current == _player->get_stream()->get_length()) {
  109. _current = 0;
  110. _indicator->update();
  111. }
  112. }
  113. void AudioStreamEditor::_draw_indicator() {
  114. if (!stream.is_valid()) {
  115. return;
  116. }
  117. Rect2 rect = _preview->get_rect();
  118. float len = stream->get_length();
  119. float ofs_x = _current / len * rect.size.width;
  120. _indicator->draw_line(Point2(ofs_x, 0), Point2(ofs_x, rect.size.height), get_theme_color("accent_color", "Editor"), 1);
  121. _current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /");
  122. }
  123. void AudioStreamEditor::_on_input_indicator(Ref<InputEvent> p_event) {
  124. Ref<InputEventMouseButton> mb = p_event;
  125. if (mb.is_valid()) {
  126. if (mb->is_pressed()) {
  127. _seek_to(mb->get_position().x);
  128. }
  129. _dragging = mb->is_pressed();
  130. }
  131. Ref<InputEventMouseMotion> mm = p_event;
  132. if (mm.is_valid()) {
  133. if (_dragging) {
  134. _seek_to(mm->get_position().x);
  135. }
  136. }
  137. }
  138. void AudioStreamEditor::_seek_to(real_t p_x) {
  139. _current = p_x / _preview->get_rect().size.x * stream->get_length();
  140. _current = CLAMP(_current, 0, stream->get_length());
  141. _player->seek(_current);
  142. _indicator->update();
  143. }
  144. void AudioStreamEditor::edit(Ref<AudioStream> p_stream) {
  145. if (!stream.is_null())
  146. stream->remove_change_receptor(this);
  147. stream = p_stream;
  148. _player->set_stream(stream);
  149. _current = 0;
  150. String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s";
  151. _duration_label->set_text(text);
  152. if (!stream.is_null()) {
  153. stream->add_change_receptor(this);
  154. update();
  155. } else {
  156. hide();
  157. }
  158. }
  159. void AudioStreamEditor::_bind_methods() {
  160. }
  161. AudioStreamEditor::AudioStreamEditor() {
  162. set_custom_minimum_size(Size2(1, 100) * EDSCALE);
  163. _current = 0;
  164. _dragging = false;
  165. _player = memnew(AudioStreamPlayer);
  166. _player->connect("finished", callable_mp(this, &AudioStreamEditor::_on_finished));
  167. add_child(_player);
  168. VBoxContainer *vbox = memnew(VBoxContainer);
  169. vbox->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_MINSIZE, 0);
  170. add_child(vbox);
  171. _preview = memnew(ColorRect);
  172. _preview->set_v_size_flags(SIZE_EXPAND_FILL);
  173. _preview->connect("draw", callable_mp(this, &AudioStreamEditor::_draw_preview));
  174. vbox->add_child(_preview);
  175. _indicator = memnew(Control);
  176. _indicator->set_anchors_and_margins_preset(PRESET_WIDE);
  177. _indicator->connect("draw", callable_mp(this, &AudioStreamEditor::_draw_indicator));
  178. _indicator->connect("gui_input", callable_mp(this, &AudioStreamEditor::_on_input_indicator));
  179. _preview->add_child(_indicator);
  180. HBoxContainer *hbox = memnew(HBoxContainer);
  181. hbox->add_theme_constant_override("separation", 0);
  182. vbox->add_child(hbox);
  183. _play_button = memnew(ToolButton);
  184. hbox->add_child(_play_button);
  185. _play_button->set_focus_mode(Control::FOCUS_NONE);
  186. _play_button->connect("pressed", callable_mp(this, &AudioStreamEditor::_play));
  187. _stop_button = memnew(ToolButton);
  188. hbox->add_child(_stop_button);
  189. _stop_button->set_focus_mode(Control::FOCUS_NONE);
  190. _stop_button->connect("pressed", callable_mp(this, &AudioStreamEditor::_stop));
  191. _current_label = memnew(Label);
  192. _current_label->set_align(Label::ALIGN_RIGHT);
  193. _current_label->set_h_size_flags(SIZE_EXPAND_FILL);
  194. _current_label->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts"));
  195. _current_label->set_modulate(Color(1, 1, 1, 0.5));
  196. hbox->add_child(_current_label);
  197. _duration_label = memnew(Label);
  198. _duration_label->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts"));
  199. hbox->add_child(_duration_label);
  200. }
  201. void AudioStreamEditorPlugin::edit(Object *p_object) {
  202. AudioStream *s = Object::cast_to<AudioStream>(p_object);
  203. if (!s)
  204. return;
  205. audio_editor->edit(Ref<AudioStream>(s));
  206. }
  207. bool AudioStreamEditorPlugin::handles(Object *p_object) const {
  208. return p_object->is_class("AudioStream");
  209. }
  210. void AudioStreamEditorPlugin::make_visible(bool p_visible) {
  211. audio_editor->set_visible(p_visible);
  212. }
  213. AudioStreamEditorPlugin::AudioStreamEditorPlugin(EditorNode *p_node) {
  214. editor = p_node;
  215. audio_editor = memnew(AudioStreamEditor);
  216. add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, audio_editor);
  217. audio_editor->hide();
  218. }
  219. AudioStreamEditorPlugin::~AudioStreamEditorPlugin() {
  220. }