editor_resource_picker.h 7.0 KB

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