audio_stream_randomizer_editor_plugin.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*************************************************************************/
  2. /* audio_stream_randomizer_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_randomizer_editor_plugin.h"
  31. void AudioStreamRandomizerEditorPlugin::edit(Object *p_object) {
  32. }
  33. bool AudioStreamRandomizerEditorPlugin::handles(Object *p_object) const {
  34. return false;
  35. }
  36. void AudioStreamRandomizerEditorPlugin::make_visible(bool p_visible) {
  37. }
  38. void AudioStreamRandomizerEditorPlugin::_move_stream_array_element(Object *p_undo_redo, Object *p_edited, String p_array_prefix, int p_from_index, int p_to_pos) {
  39. UndoRedo *undo_redo = Object::cast_to<UndoRedo>(p_undo_redo);
  40. ERR_FAIL_COND(!undo_redo);
  41. AudioStreamRandomizer *randomizer = Object::cast_to<AudioStreamRandomizer>(p_edited);
  42. if (!randomizer) {
  43. return;
  44. }
  45. // Compute the array indices to save.
  46. int begin = 0;
  47. int end;
  48. if (p_array_prefix == "stream_") {
  49. end = randomizer->get_streams_count();
  50. } else {
  51. ERR_FAIL_MSG("Invalid array prefix for AudioStreamRandomizer.");
  52. }
  53. if (p_from_index < 0) {
  54. // Adding new.
  55. if (p_to_pos >= 0) {
  56. begin = p_to_pos;
  57. } else {
  58. end = 0; // Nothing to save when adding at the end.
  59. }
  60. } else if (p_to_pos < 0) {
  61. // Removing.
  62. begin = p_from_index;
  63. } else {
  64. // Moving.
  65. begin = MIN(p_from_index, p_to_pos);
  66. end = MIN(MAX(p_from_index, p_to_pos) + 1, end);
  67. }
  68. #define ADD_UNDO(obj, property) undo_redo->add_undo_property(obj, property, obj->get(property));
  69. // Save layers' properties.
  70. if (p_from_index < 0) {
  71. undo_redo->add_undo_method(randomizer, "remove_stream", p_to_pos < 0 ? randomizer->get_streams_count() : p_to_pos);
  72. } else if (p_to_pos < 0) {
  73. undo_redo->add_undo_method(randomizer, "add_stream", p_from_index);
  74. }
  75. List<PropertyInfo> properties;
  76. randomizer->get_property_list(&properties);
  77. for (PropertyInfo pi : properties) {
  78. if (pi.name.begins_with(p_array_prefix)) {
  79. String str = pi.name.trim_prefix(p_array_prefix);
  80. int to_char_index = 0;
  81. while (to_char_index < str.length()) {
  82. if (str[to_char_index] < '0' || str[to_char_index] > '9') {
  83. break;
  84. }
  85. to_char_index++;
  86. }
  87. if (to_char_index > 0) {
  88. int array_index = str.left(to_char_index).to_int();
  89. if (array_index >= begin && array_index < end) {
  90. ADD_UNDO(randomizer, pi.name);
  91. }
  92. }
  93. }
  94. }
  95. #undef ADD_UNDO
  96. if (p_from_index < 0) {
  97. undo_redo->add_do_method(randomizer, "add_stream", p_to_pos);
  98. } else if (p_to_pos < 0) {
  99. undo_redo->add_do_method(randomizer, "remove_stream", p_from_index);
  100. } else {
  101. undo_redo->add_do_method(randomizer, "move_stream", p_from_index, p_to_pos);
  102. }
  103. }
  104. AudioStreamRandomizerEditorPlugin::AudioStreamRandomizerEditorPlugin(EditorNode *p_node) {
  105. EditorNode::get_singleton()->get_editor_data().add_move_array_element_function(SNAME("AudioStreamRandomizer"), callable_mp(this, &AudioStreamRandomizerEditorPlugin::_move_stream_array_element));
  106. }
  107. AudioStreamRandomizerEditorPlugin::~AudioStreamRandomizerEditorPlugin() {}