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