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