audio_stream_editor_plugin.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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", this, "_preview_changed");
  39. }
  40. if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_ENTER_TREE) {
  41. _play_button->set_icon(get_icon("MainPlay", "EditorIcons"));
  42. _stop_button->set_icon(get_icon("Stop", "EditorIcons"));
  43. _preview->set_frame_color(get_color("dark_color_2", "Editor"));
  44. set_frame_color(get_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_color("contrast_color_2", "Editor"));
  76. VS::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. // '_pausing' variable indicates that we want to pause the audio player, not stop it. See '_on_finished()'.
  91. _pausing = true;
  92. _player->stop();
  93. _play_button->set_icon(get_icon("MainPlay", "EditorIcons"));
  94. set_process(false);
  95. } else {
  96. _player->play(_current);
  97. _play_button->set_icon(get_icon("Pause", "EditorIcons"));
  98. set_process(true);
  99. }
  100. }
  101. void AudioStreamEditor::_stop() {
  102. _player->stop();
  103. _play_button->set_icon(get_icon("MainPlay", "EditorIcons"));
  104. _current = 0;
  105. _indicator->update();
  106. set_process(false);
  107. }
  108. void AudioStreamEditor::_on_finished() {
  109. _play_button->set_icon(get_icon("MainPlay", "EditorIcons"));
  110. if (!_pausing) {
  111. _current = 0;
  112. _indicator->update();
  113. } else {
  114. _pausing = false;
  115. }
  116. set_process(false);
  117. }
  118. void AudioStreamEditor::_draw_indicator() {
  119. if (!stream.is_valid()) {
  120. return;
  121. }
  122. Rect2 rect = _preview->get_rect();
  123. float len = stream->get_length();
  124. float ofs_x = _current / len * rect.size.width;
  125. _indicator->draw_line(Point2(ofs_x, 0), Point2(ofs_x, rect.size.height), get_color("accent_color", "Editor"), 1);
  126. _current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /");
  127. }
  128. void AudioStreamEditor::_on_input_indicator(Ref<InputEvent> p_event) {
  129. Ref<InputEventMouseButton> mb = p_event;
  130. if (mb.is_valid()) {
  131. if (mb->is_pressed()) {
  132. _seek_to(mb->get_position().x);
  133. }
  134. _dragging = mb->is_pressed();
  135. }
  136. Ref<InputEventMouseMotion> mm = p_event;
  137. if (mm.is_valid()) {
  138. if (_dragging) {
  139. _seek_to(mm->get_position().x);
  140. }
  141. }
  142. }
  143. void AudioStreamEditor::_seek_to(real_t p_x) {
  144. _current = p_x / _preview->get_rect().size.x * stream->get_length();
  145. _current = CLAMP(_current, 0, stream->get_length());
  146. _player->seek(_current);
  147. _indicator->update();
  148. }
  149. void AudioStreamEditor::edit(Ref<AudioStream> p_stream) {
  150. if (!stream.is_null())
  151. stream->remove_change_receptor(this);
  152. stream = p_stream;
  153. _player->set_stream(stream);
  154. _current = 0;
  155. String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s";
  156. _duration_label->set_text(text);
  157. if (!stream.is_null()) {
  158. stream->add_change_receptor(this);
  159. update();
  160. } else {
  161. hide();
  162. }
  163. }
  164. void AudioStreamEditor::_bind_methods() {
  165. ClassDB::bind_method(D_METHOD("_preview_changed"), &AudioStreamEditor::_preview_changed);
  166. ClassDB::bind_method(D_METHOD("_play"), &AudioStreamEditor::_play);
  167. ClassDB::bind_method(D_METHOD("_stop"), &AudioStreamEditor::_stop);
  168. ClassDB::bind_method(D_METHOD("_on_finished"), &AudioStreamEditor::_on_finished);
  169. ClassDB::bind_method(D_METHOD("_draw_preview"), &AudioStreamEditor::_draw_preview);
  170. ClassDB::bind_method(D_METHOD("_draw_indicator"), &AudioStreamEditor::_draw_indicator);
  171. ClassDB::bind_method(D_METHOD("_on_input_indicator"), &AudioStreamEditor::_on_input_indicator);
  172. }
  173. AudioStreamEditor::AudioStreamEditor() {
  174. set_custom_minimum_size(Size2(1, 100) * EDSCALE);
  175. _player = memnew(AudioStreamPlayer);
  176. _player->connect("finished", this, "_on_finished");
  177. add_child(_player);
  178. VBoxContainer *vbox = memnew(VBoxContainer);
  179. vbox->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_MINSIZE, 0);
  180. add_child(vbox);
  181. _preview = memnew(ColorRect);
  182. _preview->set_v_size_flags(SIZE_EXPAND_FILL);
  183. _preview->connect("draw", this, "_draw_preview");
  184. vbox->add_child(_preview);
  185. _indicator = memnew(Control);
  186. _indicator->set_anchors_and_margins_preset(PRESET_WIDE);
  187. _indicator->connect("draw", this, "_draw_indicator");
  188. _indicator->connect("gui_input", this, "_on_input_indicator");
  189. _preview->add_child(_indicator);
  190. HBoxContainer *hbox = memnew(HBoxContainer);
  191. hbox->add_constant_override("separation", 0);
  192. vbox->add_child(hbox);
  193. _play_button = memnew(ToolButton);
  194. hbox->add_child(_play_button);
  195. _play_button->set_focus_mode(Control::FOCUS_NONE);
  196. _play_button->connect("pressed", this, "_play");
  197. _stop_button = memnew(ToolButton);
  198. hbox->add_child(_stop_button);
  199. _stop_button->set_focus_mode(Control::FOCUS_NONE);
  200. _stop_button->connect("pressed", this, "_stop");
  201. _current_label = memnew(Label);
  202. _current_label->set_align(Label::ALIGN_RIGHT);
  203. _current_label->set_h_size_flags(SIZE_EXPAND_FILL);
  204. _current_label->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  205. _current_label->set_modulate(Color(1, 1, 1, 0.5));
  206. hbox->add_child(_current_label);
  207. _duration_label = memnew(Label);
  208. _duration_label->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("status_source", "EditorFonts"));
  209. hbox->add_child(_duration_label);
  210. }
  211. void AudioStreamEditorPlugin::edit(Object *p_object) {
  212. AudioStream *s = Object::cast_to<AudioStream>(p_object);
  213. if (!s)
  214. return;
  215. audio_editor->edit(Ref<AudioStream>(s));
  216. }
  217. bool AudioStreamEditorPlugin::handles(Object *p_object) const {
  218. return p_object->is_class("AudioStream");
  219. }
  220. void AudioStreamEditorPlugin::make_visible(bool p_visible) {
  221. audio_editor->set_visible(p_visible);
  222. }
  223. AudioStreamEditorPlugin::AudioStreamEditorPlugin(EditorNode *p_node) {
  224. editor = p_node;
  225. audio_editor = memnew(AudioStreamEditor);
  226. add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, audio_editor);
  227. audio_editor->hide();
  228. }
  229. AudioStreamEditorPlugin::~AudioStreamEditorPlugin() {
  230. }