|
| 1 | +/**************************************************************************/ |
| 2 | +/* audio_stream_effect_editor_plugin.cpp */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | + |
| 31 | +#include "audio_stream_effect_editor_plugin.h" |
| 32 | + |
| 33 | +#include "editor/editor_node.h" |
| 34 | +#include "editor/editor_undo_redo_manager.h" |
| 35 | +#include "scene/resources/audio_stream_effect.h" |
| 36 | + |
| 37 | +void AudioStreamEffectEditorPlugin::edit(Object *p_object) { |
| 38 | +} |
| 39 | + |
| 40 | +bool AudioStreamEffectEditorPlugin::handles(Object *p_object) const { |
| 41 | + return false; |
| 42 | +} |
| 43 | + |
| 44 | +void AudioStreamEffectEditorPlugin::make_visible(bool p_visible) { |
| 45 | +} |
| 46 | + |
| 47 | +void AudioStreamEffectEditorPlugin::_move_effect_array_element(Object *p_undo_redo, Object *p_edited, const String &p_array_prefix, int p_from_index, int p_to_pos) { |
| 48 | + EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo); |
| 49 | + ERR_FAIL_NULL(undo_redo_man); |
| 50 | + |
| 51 | + AudioStreamEffect *stream = Object::cast_to<AudioStreamEffect>(p_edited); |
| 52 | + if (!stream) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Compute the array indices to save. |
| 57 | + int begin = 0; |
| 58 | + int end; |
| 59 | + if (p_array_prefix == "effect_") { |
| 60 | + end = stream->get_effect_count(); |
| 61 | + } else { |
| 62 | + ERR_FAIL_MSG("Invalid array prefix for AudioStreamEffect."); |
| 63 | + } |
| 64 | + if (p_from_index < 0) { |
| 65 | + // Adding new. |
| 66 | + if (p_to_pos >= 0) { |
| 67 | + begin = p_to_pos; |
| 68 | + } else { |
| 69 | + end = 0; // Nothing to save when adding at the end. |
| 70 | + } |
| 71 | + } else if (p_to_pos < 0) { |
| 72 | + // Removing. |
| 73 | + begin = p_from_index; |
| 74 | + } else { |
| 75 | + // Moving. |
| 76 | + begin = MIN(p_from_index, p_to_pos); |
| 77 | + end = MIN(MAX(p_from_index, p_to_pos) + 1, end); |
| 78 | + } |
| 79 | + |
| 80 | +#define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property)); |
| 81 | + // Save layers' properties. |
| 82 | + if (p_from_index < 0) { |
| 83 | + undo_redo_man->add_undo_method(stream, "remove_effect", p_to_pos < 0 ? stream->get_effect_count() : p_to_pos); |
| 84 | + } else if (p_to_pos < 0) { |
| 85 | + undo_redo_man->add_undo_method(stream, "add_effect", p_from_index, Ref<AudioEffect>()); |
| 86 | + } |
| 87 | + |
| 88 | + List<PropertyInfo> properties; |
| 89 | + stream->get_property_list(&properties); |
| 90 | + for (PropertyInfo pi : properties) { |
| 91 | + if (pi.name.begins_with(p_array_prefix)) { |
| 92 | + String str = pi.name.trim_prefix(p_array_prefix); |
| 93 | + int to_char_index = 0; |
| 94 | + while (to_char_index < str.length()) { |
| 95 | + if (str[to_char_index] < '0' || str[to_char_index] > '9') { |
| 96 | + break; |
| 97 | + } |
| 98 | + to_char_index++; |
| 99 | + } |
| 100 | + if (to_char_index > 0) { |
| 101 | + int array_index = str.left(to_char_index).to_int(); |
| 102 | + if (array_index >= begin && array_index < end) { |
| 103 | + ADD_UNDO(stream, pi.name); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +#undef ADD_UNDO |
| 109 | + |
| 110 | + if (p_from_index < 0) { |
| 111 | + undo_redo_man->add_do_method(stream, "add_effect", p_to_pos, Ref<AudioEffect>(), false); |
| 112 | + } else if (p_to_pos < 0) { |
| 113 | + undo_redo_man->add_do_method(stream, "remove_effect", p_from_index); |
| 114 | + } else { |
| 115 | + undo_redo_man->add_do_method(stream, "move_effect", p_from_index, p_to_pos); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +AudioStreamEffectEditorPlugin::AudioStreamEffectEditorPlugin() { |
| 120 | + EditorNode::get_editor_data().add_move_array_element_function(SNAME("AudioStreamEffect"), callable_mp(this, &AudioStreamEffectEditorPlugin::_move_effect_array_element)); |
| 121 | +} |
0 commit comments