sample_player_editor_plugin.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*************************************************************************/
  2. /* sample_player_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 "sample_player_editor_plugin.h"
  31. #include "scene/resources/sample_library.h"
  32. void SamplePlayerEditor::_notification(int p_what) {
  33. if (p_what == NOTIFICATION_ENTER_TREE) {
  34. play->set_icon(get_icon("Play", "EditorIcons"));
  35. stop->set_icon(get_icon("Stop", "EditorIcons"));
  36. }
  37. }
  38. void SamplePlayerEditor::_node_removed(Node *p_node) {
  39. if (p_node == node) {
  40. node = NULL;
  41. hide();
  42. }
  43. }
  44. void SamplePlayerEditor::_bind_methods() {
  45. ObjectTypeDB::bind_method(_MD("_play"), &SamplePlayerEditor::_play);
  46. ObjectTypeDB::bind_method(_MD("_stop"), &SamplePlayerEditor::_stop);
  47. }
  48. void SamplePlayerEditor::_play() {
  49. if (!node)
  50. return;
  51. if (samples->get_item_count() <= 0)
  52. return;
  53. node->call("play", samples->get_item_text(samples->get_selected()));
  54. stop->set_pressed(false);
  55. play->set_pressed(true);
  56. }
  57. void SamplePlayerEditor::_stop() {
  58. if (!node)
  59. return;
  60. if (samples->get_item_count() <= 0)
  61. return;
  62. node->call("stop_all");
  63. print_line("STOP ALL!!");
  64. stop->set_pressed(true);
  65. play->set_pressed(false);
  66. }
  67. void SamplePlayerEditor::_update_sample_library() {
  68. samples->clear();
  69. Ref<SampleLibrary> sl = node->call("get_sample_library");
  70. if (sl.is_null()) {
  71. samples->add_item("<NO SAMPLE LIBRARY>");
  72. return; //no sample library;
  73. }
  74. List<StringName> samplenames;
  75. sl->get_sample_list(&samplenames);
  76. samplenames.sort_custom<StringName::AlphCompare>();
  77. for (List<StringName>::Element *E = samplenames.front(); E; E = E->next()) {
  78. samples->add_item(E->get());
  79. }
  80. }
  81. void SamplePlayerEditor::edit(Node *p_sample_player) {
  82. node = p_sample_player;
  83. if (node) {
  84. _update_sample_library();
  85. }
  86. }
  87. SamplePlayerEditor::SamplePlayerEditor() {
  88. play = memnew(Button);
  89. play->set_pos(Point2(5, 5));
  90. play->set_toggle_mode(true);
  91. play->set_anchor_and_margin(MARGIN_LEFT, Control::ANCHOR_END, 250);
  92. play->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, 230);
  93. play->set_anchor_and_margin(MARGIN_TOP, Control::ANCHOR_BEGIN, 0);
  94. play->set_anchor_and_margin(MARGIN_BOTTOM, Control::ANCHOR_BEGIN, 0);
  95. add_child(play);
  96. stop = memnew(Button);
  97. stop->set_pos(Point2(35, 5));
  98. stop->set_toggle_mode(true);
  99. stop->set_anchor_and_margin(MARGIN_LEFT, Control::ANCHOR_END, 220);
  100. stop->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, 200);
  101. stop->set_anchor_and_margin(MARGIN_TOP, Control::ANCHOR_BEGIN, 0);
  102. stop->set_anchor_and_margin(MARGIN_BOTTOM, Control::ANCHOR_BEGIN, 0);
  103. add_child(stop);
  104. samples = memnew(OptionButton);
  105. samples->set_anchor_and_margin(MARGIN_LEFT, Control::ANCHOR_END, 190);
  106. samples->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, 5);
  107. samples->set_anchor_and_margin(MARGIN_TOP, Control::ANCHOR_BEGIN, 0);
  108. samples->set_anchor_and_margin(MARGIN_BOTTOM, Control::ANCHOR_BEGIN, 0);
  109. add_child(samples);
  110. play->connect("pressed", this, "_play");
  111. stop->connect("pressed", this, "_stop");
  112. }
  113. void SamplePlayerEditorPlugin::edit(Object *p_object) {
  114. sample_player_editor->edit(p_object->cast_to<Node>());
  115. }
  116. bool SamplePlayerEditorPlugin::handles(Object *p_object) const {
  117. return p_object->is_type("SamplePlayer2D") || p_object->is_type("SamplePlayer") || p_object->is_type("SpatialSamplePlayer");
  118. }
  119. void SamplePlayerEditorPlugin::make_visible(bool p_visible) {
  120. if (p_visible) {
  121. sample_player_editor->show();
  122. sample_player_editor->set_fixed_process(true);
  123. } else {
  124. sample_player_editor->hide();
  125. sample_player_editor->set_fixed_process(false);
  126. sample_player_editor->edit(NULL);
  127. }
  128. }
  129. SamplePlayerEditorPlugin::SamplePlayerEditorPlugin(EditorNode *p_node) {
  130. editor = p_node;
  131. sample_player_editor = memnew(SamplePlayerEditor);
  132. editor->get_viewport()->add_child(sample_player_editor);
  133. sample_player_editor->set_anchor(MARGIN_LEFT, Control::ANCHOR_END);
  134. sample_player_editor->set_anchor(MARGIN_RIGHT, Control::ANCHOR_END);
  135. sample_player_editor->set_margin(MARGIN_LEFT, 250);
  136. sample_player_editor->set_margin(MARGIN_RIGHT, 0);
  137. sample_player_editor->set_margin(MARGIN_TOP, 0);
  138. sample_player_editor->set_margin(MARGIN_BOTTOM, 10);
  139. sample_player_editor->hide();
  140. }
  141. SamplePlayerEditorPlugin::~SamplePlayerEditorPlugin() {
  142. }