Path: blob/master/editor/audio/audio_stream_randomizer_editor_plugin.cpp
9897 views
/**************************************************************************/1/* audio_stream_randomizer_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "audio_stream_randomizer_editor_plugin.h"3132#include "editor/editor_node.h"33#include "editor/editor_undo_redo_manager.h"34#include "servers/audio/audio_stream.h"3536void AudioStreamRandomizerEditorPlugin::edit(Object *p_object) {37}3839bool AudioStreamRandomizerEditorPlugin::handles(Object *p_object) const {40return false;41}4243void AudioStreamRandomizerEditorPlugin::make_visible(bool p_visible) {44}4546void AudioStreamRandomizerEditorPlugin::_move_stream_array_element(Object *p_undo_redo, Object *p_edited, const String &p_array_prefix, int p_from_index, int p_to_pos) {47EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);48ERR_FAIL_NULL(undo_redo_man);4950AudioStreamRandomizer *randomizer = Object::cast_to<AudioStreamRandomizer>(p_edited);51if (!randomizer) {52return;53}5455// Compute the array indices to save.56int begin = 0;57int end;58if (p_array_prefix == "stream_") {59end = randomizer->get_streams_count();60} else {61ERR_FAIL_MSG("Invalid array prefix for AudioStreamRandomizer.");62}63if (p_from_index < 0) {64// Adding new.65if (p_to_pos >= 0) {66begin = p_to_pos;67} else {68end = 0; // Nothing to save when adding at the end.69}70} else if (p_to_pos < 0) {71// Removing.72begin = p_from_index;73} else {74// Moving.75begin = MIN(p_from_index, p_to_pos);76end = MIN(MAX(p_from_index, p_to_pos) + 1, end);77}7879#define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));80// Save layers' properties.81if (p_from_index < 0) {82undo_redo_man->add_undo_method(randomizer, "remove_stream", p_to_pos < 0 ? randomizer->get_streams_count() : p_to_pos);83} else if (p_to_pos < 0) {84undo_redo_man->add_undo_method(randomizer, "add_stream", p_from_index, Ref<AudioStream>());85}8687List<PropertyInfo> properties;88randomizer->get_property_list(&properties);89for (PropertyInfo pi : properties) {90if (pi.name.begins_with(p_array_prefix)) {91String str = pi.name.trim_prefix(p_array_prefix);92int to_char_index = 0;93while (to_char_index < str.length()) {94if (str[to_char_index] < '0' || str[to_char_index] > '9') {95break;96}97to_char_index++;98}99if (to_char_index > 0) {100int array_index = str.left(to_char_index).to_int();101if (array_index >= begin && array_index < end) {102ADD_UNDO(randomizer, pi.name);103}104}105}106}107#undef ADD_UNDO108109if (p_from_index < 0) {110undo_redo_man->add_do_method(randomizer, "add_stream", p_to_pos, Ref<AudioStream>());111} else if (p_to_pos < 0) {112undo_redo_man->add_do_method(randomizer, "remove_stream", p_from_index);113} else {114undo_redo_man->add_do_method(randomizer, "move_stream", p_from_index, p_to_pos);115}116}117118AudioStreamRandomizerEditorPlugin::AudioStreamRandomizerEditorPlugin() {119EditorNode::get_editor_data().add_move_array_element_function(SNAME("AudioStreamRandomizer"), callable_mp(this, &AudioStreamRandomizerEditorPlugin::_move_stream_array_element));120}121122123