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