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) 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_randomizer_editor_plugin.h"
32
33#include "editor/editor_node.h"
34#include "editor/editor_undo_redo_manager.h"
35
36void AudioStreamRandomizerEditorPlugin::edit(Object *p_object) {
37}
38
39bool AudioStreamRandomizerEditorPlugin::handles(Object *p_object) const {
40 return false;
41}
42
43void AudioStreamRandomizerEditorPlugin::make_visible(bool p_visible) {
44}
45
46void AudioStreamRandomizerEditorPlugin::_move_stream_array_element(Object *p_undo_redo, Object *p_edited, String p_array_prefix, int p_from_index, int p_to_pos) {
47 EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
48 ERR_FAIL_NULL(undo_redo_man);
49
50 AudioStreamRandomizer *randomizer = Object::cast_to<AudioStreamRandomizer>(p_edited);
51 if (!randomizer) {
52 return;
53 }
54
55 // Compute the array indices to save.
56 int begin = 0;
57 int end;
58 if (p_array_prefix == "stream_") {
59 end = randomizer->get_streams_count();
60 } else {
61 ERR_FAIL_MSG("Invalid array prefix for AudioStreamRandomizer.");
62 }
63 if (p_from_index < 0) {
64 // Adding new.
65 if (p_to_pos >= 0) {
66 begin = p_to_pos;
67 } else {
68 end = 0; // Nothing to save when adding at the end.
69 }
70 } else if (p_to_pos < 0) {
71 // Removing.
72 begin = p_from_index;
73 } else {
74 // Moving.
75 begin = MIN(p_from_index, p_to_pos);
76 end = MIN(MAX(p_from_index, p_to_pos) + 1, end);
77 }
78
79#define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));
80 // Save layers' properties.
81 if (p_from_index < 0) {
82 undo_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) {
84 undo_redo_man->add_undo_method(randomizer, "add_stream", p_from_index, Ref<AudioStream>());
85 }
86
87 List<PropertyInfo> properties;
88 randomizer->get_property_list(&properties);
89 for (PropertyInfo pi : properties) {
90 if (pi.name.begins_with(p_array_prefix)) {
91 String str = pi.name.trim_prefix(p_array_prefix);
92 int to_char_index = 0;
93 while (to_char_index < str.length()) {
94 if (str[to_char_index] < '0' || str[to_char_index] > '9') {
95 break;
96 }
97 to_char_index++;
98 }
99 if (to_char_index > 0) {
100 int array_index = str.left(to_char_index).to_int();
101 if (array_index >= begin && array_index < end) {
102 ADD_UNDO(randomizer, pi.name);
103 }
104 }
105 }
106 }
107#undef ADD_UNDO
108
109 if (p_from_index < 0) {
110 undo_redo_man->add_do_method(randomizer, "add_stream", p_to_pos, Ref<AudioStream>());
111 } else if (p_to_pos < 0) {
112 undo_redo_man->add_do_method(randomizer, "remove_stream", p_from_index);
113 } else {
114 undo_redo_man->add_do_method(randomizer, "move_stream", p_from_index, p_to_pos);
115 }
116}
117
118AudioStreamRandomizerEditorPlugin::AudioStreamRandomizerEditorPlugin() {
119 EditorNode::get_editor_data().add_move_array_element_function(SNAME("AudioStreamRandomizer"), callable_mp(this, &AudioStreamRandomizerEditorPlugin::_move_stream_array_element));
120}
121
122AudioStreamRandomizerEditorPlugin::~AudioStreamRandomizerEditorPlugin() {}
123