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