editor_resource_picker.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*************************************************************************/
  2. /* editor_resource_picker.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifndef EDITOR_RESOURCE_PICKER_H
  31. #define EDITOR_RESOURCE_PICKER_H
  32. #include "scene/gui/box_container.h"
  33. #include "scene/gui/button.h"
  34. #include "scene/gui/popup_menu.h"
  35. #include "scene/gui/texture_rect.h"
  36. class EditorFileDialog;
  37. class EditorQuickOpen;
  38. class EditorResourcePicker : public HBoxContainer {
  39. GDCLASS(EditorResourcePicker, HBoxContainer);
  40. static HashMap<StringName, List<StringName>> allowed_types_cache;
  41. String base_type;
  42. RES edited_resource;
  43. bool editable = true;
  44. bool dropping = false;
  45. Vector<String> inheritors_array;
  46. Button *assign_button = nullptr;
  47. TextureRect *preview_rect = nullptr;
  48. Button *edit_button = nullptr;
  49. EditorFileDialog *file_dialog = nullptr;
  50. EditorQuickOpen *quick_open = nullptr;
  51. enum MenuOption {
  52. OBJ_MENU_LOAD,
  53. OBJ_MENU_QUICKLOAD,
  54. OBJ_MENU_EDIT,
  55. OBJ_MENU_CLEAR,
  56. OBJ_MENU_MAKE_UNIQUE,
  57. OBJ_MENU_SAVE,
  58. OBJ_MENU_COPY,
  59. OBJ_MENU_PASTE,
  60. OBJ_MENU_SHOW_IN_FILE_SYSTEM,
  61. TYPE_BASE_ID = 100,
  62. CONVERT_BASE_ID = 1000,
  63. };
  64. PopupMenu *edit_menu = nullptr;
  65. void _update_resource();
  66. void _update_resource_preview(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, ObjectID p_obj);
  67. void _resource_selected();
  68. void _file_quick_selected();
  69. void _file_selected(const String &p_path);
  70. void _update_menu();
  71. void _update_menu_items();
  72. void _edit_menu_cbk(int p_which);
  73. void _button_draw();
  74. void _button_input(const Ref<InputEvent> &p_event);
  75. void _get_allowed_types(bool p_with_convert, Set<String> *p_vector) const;
  76. bool _is_drop_valid(const Dictionary &p_drag_data) const;
  77. bool _is_type_valid(const String p_type_name, Set<String> p_allowed_types) const;
  78. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  79. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  80. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  81. void _ensure_resource_menu();
  82. protected:
  83. static void _bind_methods();
  84. void _notification(int p_what);
  85. GDVIRTUAL1(_set_create_options, Object *)
  86. GDVIRTUAL1R(bool, _handle_menu_selected, int)
  87. public:
  88. static void clear_caches();
  89. void set_base_type(const String &p_base_type);
  90. String get_base_type() const;
  91. Vector<String> get_allowed_types() const;
  92. void set_edited_resource(RES p_resource);
  93. RES get_edited_resource();
  94. void set_toggle_mode(bool p_enable);
  95. bool is_toggle_mode() const;
  96. void set_toggle_pressed(bool p_pressed);
  97. void set_editable(bool p_editable);
  98. bool is_editable() const;
  99. virtual void set_create_options(Object *p_menu_node);
  100. virtual bool handle_menu_selected(int p_which);
  101. EditorResourcePicker();
  102. };
  103. class EditorScriptPicker : public EditorResourcePicker {
  104. GDCLASS(EditorScriptPicker, EditorResourcePicker);
  105. enum ExtraMenuOption {
  106. OBJ_MENU_NEW_SCRIPT = 10,
  107. OBJ_MENU_EXTEND_SCRIPT = 11
  108. };
  109. Node *script_owner = nullptr;
  110. protected:
  111. static void _bind_methods();
  112. public:
  113. virtual void set_create_options(Object *p_menu_node) override;
  114. virtual bool handle_menu_selected(int p_which) override;
  115. void set_script_owner(Node *p_owner);
  116. Node *get_script_owner() const;
  117. EditorScriptPicker();
  118. };
  119. class EditorShaderPicker : public EditorResourcePicker {
  120. GDCLASS(EditorShaderPicker, EditorResourcePicker);
  121. enum ExtraMenuOption {
  122. OBJ_MENU_NEW_SHADER = 10,
  123. };
  124. ShaderMaterial *edited_material = nullptr;
  125. int preferred_mode = -1;
  126. public:
  127. virtual void set_create_options(Object *p_menu_node) override;
  128. virtual bool handle_menu_selected(int p_which) override;
  129. void set_edited_material(ShaderMaterial *p_material);
  130. ShaderMaterial *get_edited_material() const;
  131. void set_preferred_mode(int p_preferred_mode);
  132. EditorShaderPicker();
  133. };
  134. #endif // EDITOR_RESOURCE_PICKER_H