editor_resource_picker.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "editor_file_dialog.h"
  33. #include "editor_quick_open.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/button.h"
  36. #include "scene/gui/popup_menu.h"
  37. #include "scene/gui/texture_rect.h"
  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;
  47. TextureRect *preview_rect;
  48. Button *edit_button;
  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;
  65. void _update_resource();
  66. void _update_resource_preview(const String &p_path, const Ref<Texture> &p_preview, const Ref<Texture> &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. protected:
  82. static void _bind_methods();
  83. void _notification(int p_what);
  84. public:
  85. static void clear_caches();
  86. void set_base_type(const String &p_base_type);
  87. String get_base_type() const;
  88. Vector<String> get_allowed_types() const;
  89. void set_edited_resource(RES p_resource);
  90. RES get_edited_resource();
  91. void set_toggle_mode(bool p_enable);
  92. bool is_toggle_mode() const;
  93. void set_toggle_pressed(bool p_pressed);
  94. void set_editable(bool p_editable);
  95. bool is_editable() const;
  96. virtual void set_create_options(Object *p_menu_node);
  97. virtual bool handle_menu_selected(int p_which);
  98. EditorResourcePicker();
  99. };
  100. class EditorScriptPicker : public EditorResourcePicker {
  101. GDCLASS(EditorScriptPicker, EditorResourcePicker);
  102. enum ExtraMenuOption {
  103. OBJ_MENU_NEW_SCRIPT = 10,
  104. OBJ_MENU_EXTEND_SCRIPT = 11
  105. };
  106. Node *script_owner = nullptr;
  107. protected:
  108. static void _bind_methods();
  109. public:
  110. virtual void set_create_options(Object *p_menu_node);
  111. virtual bool handle_menu_selected(int p_which);
  112. void set_script_owner(Node *p_owner);
  113. Node *get_script_owner() const;
  114. EditorScriptPicker();
  115. };
  116. #endif // EDITOR_RESOURCE_PICKER_H