1/**************************************************************************/
2/* editor_resource_picker.h */
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#ifndef EDITOR_RESOURCE_PICKER_H
32#define EDITOR_RESOURCE_PICKER_H
33
34#include "scene/gui/box_container.h"
35
36class Button;
37class ConfirmationDialog;
38class EditorFileDialog;
39class EditorQuickOpen;
40class PopupMenu;
41class TextureRect;
42class Tree;
43class TreeItem;
44
45class EditorResourcePicker : public HBoxContainer {
46 GDCLASS(EditorResourcePicker, HBoxContainer);
47
48 String base_type;
49 Ref<Resource> edited_resource;
50
51 bool editable = true;
52 bool dropping = false;
53
54 Vector<String> inheritors_array;
55
56 Button *assign_button = nullptr;
57 TextureRect *preview_rect = nullptr;
58 Button *edit_button = nullptr;
59 EditorFileDialog *file_dialog = nullptr;
60 EditorQuickOpen *quick_open = nullptr;
61
62 ConfirmationDialog *duplicate_resources_dialog = nullptr;
63 Tree *duplicate_resources_tree = nullptr;
64
65 Size2i assign_button_min_size = Size2i(1, 1);
66
67 enum MenuOption {
68 OBJ_MENU_LOAD,
69 OBJ_MENU_QUICKLOAD,
70 OBJ_MENU_INSPECT,
71 OBJ_MENU_CLEAR,
72 OBJ_MENU_MAKE_UNIQUE,
73 OBJ_MENU_MAKE_UNIQUE_RECURSIVE,
74 OBJ_MENU_SAVE,
75 OBJ_MENU_COPY,
76 OBJ_MENU_PASTE,
77 OBJ_MENU_SHOW_IN_FILE_SYSTEM,
78
79 TYPE_BASE_ID = 100,
80 CONVERT_BASE_ID = 1000,
81 };
82
83 PopupMenu *edit_menu = nullptr;
84
85 void _update_resource_preview(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, ObjectID p_obj);
86
87 void _resource_selected();
88 void _file_quick_selected();
89 void _file_selected(const String &p_path);
90
91 void _update_menu();
92 void _update_menu_items();
93 void _edit_menu_cbk(int p_which);
94
95 void _button_draw();
96 void _button_input(const Ref<InputEvent> &p_event);
97
98 String _get_resource_type(const Ref<Resource> &p_resource) const;
99 void _get_allowed_types(bool p_with_convert, HashSet<StringName> *p_vector) const;
100 bool _is_drop_valid(const Dictionary &p_drag_data) const;
101 bool _is_type_valid(const String p_type_name, HashSet<StringName> p_allowed_types) const;
102
103 Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
104 bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
105 void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
106
107 void _ensure_resource_menu();
108 void _gather_resources_to_duplicate(const Ref<Resource> p_resource, TreeItem *p_item, const String &p_property_name = "") const;
109 void _duplicate_selected_resources();
110
111protected:
112 virtual void _update_resource();
113
114 Button *get_assign_button() { return assign_button; }
115 static void _bind_methods();
116 void _notification(int p_what);
117
118 void set_assign_button_min_size(const Size2i &p_size);
119
120 GDVIRTUAL1(_set_create_options, Object *)
121 GDVIRTUAL1R(bool, _handle_menu_selected, int)
122
123public:
124 void set_base_type(const String &p_base_type);
125 String get_base_type() const;
126 Vector<String> get_allowed_types() const;
127
128 void set_edited_resource(Ref<Resource> p_resource);
129 Ref<Resource> get_edited_resource();
130
131 void set_toggle_mode(bool p_enable);
132 bool is_toggle_mode() const;
133 void set_toggle_pressed(bool p_pressed);
134
135 void set_editable(bool p_editable);
136 bool is_editable() const;
137
138 virtual void set_create_options(Object *p_menu_node);
139 virtual bool handle_menu_selected(int p_which);
140
141 EditorResourcePicker(bool p_hide_assign_button_controls = false);
142};
143
144class EditorScriptPicker : public EditorResourcePicker {
145 GDCLASS(EditorScriptPicker, EditorResourcePicker);
146
147 enum ExtraMenuOption {
148 OBJ_MENU_NEW_SCRIPT = 10,
149 OBJ_MENU_EXTEND_SCRIPT = 11
150 };
151
152 Node *script_owner = nullptr;
153
154protected:
155 static void _bind_methods();
156
157public:
158 virtual void set_create_options(Object *p_menu_node) override;
159 virtual bool handle_menu_selected(int p_which) override;
160
161 void set_script_owner(Node *p_owner);
162 Node *get_script_owner() const;
163
164 EditorScriptPicker();
165};
166
167class EditorShaderPicker : public EditorResourcePicker {
168 GDCLASS(EditorShaderPicker, EditorResourcePicker);
169
170 enum ExtraMenuOption {
171 OBJ_MENU_NEW_SHADER = 10,
172 };
173
174 ShaderMaterial *edited_material = nullptr;
175 int preferred_mode = -1;
176
177public:
178 virtual void set_create_options(Object *p_menu_node) override;
179 virtual bool handle_menu_selected(int p_which) override;
180
181 void set_edited_material(ShaderMaterial *p_material);
182 ShaderMaterial *get_edited_material() const;
183 void set_preferred_mode(int p_preferred_mode);
184
185 EditorShaderPicker();
186};
187
188class EditorAudioStreamPicker : public EditorResourcePicker {
189 GDCLASS(EditorAudioStreamPicker, EditorResourcePicker);
190
191 uint64_t last_preview_version = 0;
192 Control *stream_preview_rect = nullptr;
193
194 enum {
195 MAX_TAGGED_FRAMES = 8
196 };
197 float tagged_frame_offsets[MAX_TAGGED_FRAMES];
198 uint32_t tagged_frame_offset_count = 0;
199
200 void _preview_draw();
201 virtual void _update_resource() override;
202
203protected:
204 void _notification(int p_what);
205
206public:
207 EditorAudioStreamPicker();
208};
209
210#endif // EDITOR_RESOURCE_PICKER_H
211