editor_file_dialog.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /**************************************************************************/
  2. /* editor_file_dialog.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_FILE_DIALOG_H
  31. #define EDITOR_FILE_DIALOG_H
  32. #include "core/io/dir_access.h"
  33. #include "editor/file_info.h"
  34. #include "scene/gui/dialogs.h"
  35. #include "scene/property_list_helper.h"
  36. class DependencyRemoveDialog;
  37. class GridContainer;
  38. class HSplitContainer;
  39. class HFlowContainer;
  40. class ItemList;
  41. class MenuButton;
  42. class OptionButton;
  43. class PopupMenu;
  44. class TextureRect;
  45. class VSeparator;
  46. class EditorFileDialog : public ConfirmationDialog {
  47. GDCLASS(EditorFileDialog, ConfirmationDialog);
  48. public:
  49. enum DisplayMode {
  50. DISPLAY_THUMBNAILS,
  51. DISPLAY_LIST
  52. };
  53. enum Access {
  54. ACCESS_RESOURCES,
  55. ACCESS_USERDATA,
  56. ACCESS_FILESYSTEM
  57. };
  58. enum FileMode {
  59. FILE_MODE_OPEN_FILE,
  60. FILE_MODE_OPEN_FILES,
  61. FILE_MODE_OPEN_DIR,
  62. FILE_MODE_OPEN_ANY,
  63. FILE_MODE_SAVE_FILE
  64. };
  65. typedef Ref<Texture2D> (*GetIconFunc)(const String &);
  66. typedef void (*RegisterFunc)(EditorFileDialog *);
  67. static GetIconFunc get_icon_func;
  68. static GetIconFunc get_thumbnail_func;
  69. static RegisterFunc register_func;
  70. static RegisterFunc unregister_func;
  71. private:
  72. enum ItemMenu {
  73. ITEM_MENU_COPY_PATH,
  74. ITEM_MENU_DELETE,
  75. ITEM_MENU_REFRESH,
  76. ITEM_MENU_NEW_FOLDER,
  77. ITEM_MENU_SHOW_IN_EXPLORER,
  78. ITEM_MENU_SHOW_BUNDLE_CONTENT,
  79. };
  80. ConfirmationDialog *makedialog = nullptr;
  81. LineEdit *makedirname = nullptr;
  82. VSeparator *makedir_sep = nullptr;
  83. Button *makedir = nullptr;
  84. Access access = ACCESS_RESOURCES;
  85. HFlowContainer *flow_checkbox_options = nullptr;
  86. GridContainer *grid_select_options = nullptr;
  87. VBoxContainer *vbox = nullptr;
  88. FileMode mode = FILE_MODE_SAVE_FILE;
  89. bool can_create_dir = false;
  90. LineEdit *dir = nullptr;
  91. Button *dir_prev = nullptr;
  92. Button *dir_next = nullptr;
  93. Button *dir_up = nullptr;
  94. HBoxContainer *drives_container = nullptr;
  95. HBoxContainer *shortcuts_container = nullptr;
  96. OptionButton *drives = nullptr;
  97. ItemList *item_list = nullptr;
  98. PopupMenu *item_menu = nullptr;
  99. TextureRect *preview = nullptr;
  100. VBoxContainer *preview_vb = nullptr;
  101. HSplitContainer *body_hsplit = nullptr;
  102. HSplitContainer *list_hb = nullptr;
  103. HBoxContainer *file_box = nullptr;
  104. LineEdit *file = nullptr;
  105. OptionButton *filter = nullptr;
  106. AcceptDialog *error_dialog = nullptr;
  107. Ref<DirAccess> dir_access;
  108. ConfirmationDialog *confirm_save = nullptr;
  109. DependencyRemoveDialog *dep_remove_dialog = nullptr;
  110. ConfirmationDialog *global_remove_dialog = nullptr;
  111. VBoxContainer *side_vbox = nullptr;
  112. VBoxContainer *vbc = nullptr;
  113. HBoxContainer *pathhb = nullptr;
  114. Button *mode_thumbnails = nullptr;
  115. Button *mode_list = nullptr;
  116. Button *refresh = nullptr;
  117. Button *favorite = nullptr;
  118. Button *show_hidden = nullptr;
  119. Button *show_search_filter_button = nullptr;
  120. String search_string;
  121. bool show_search_filter = false;
  122. HBoxContainer *filter_hb = nullptr;
  123. LineEdit *filter_box = nullptr;
  124. FileSortOption file_sort = FileSortOption::FILE_SORT_NAME;
  125. MenuButton *file_sort_button = nullptr;
  126. Button *fav_up = nullptr;
  127. Button *fav_down = nullptr;
  128. ItemList *favorites = nullptr;
  129. ItemList *recent = nullptr;
  130. Vector<String> local_history;
  131. int local_history_pos = 0;
  132. void _push_history();
  133. Vector<String> filters;
  134. Vector<String> processed_filters;
  135. bool previews_enabled = true;
  136. bool preview_waiting = false;
  137. int preview_wheel_index = 0;
  138. float preview_wheel_timeout = 0.0f;
  139. static bool default_show_hidden_files;
  140. static DisplayMode default_display_mode;
  141. bool show_hidden_files;
  142. DisplayMode display_mode;
  143. bool disable_overwrite_warning = false;
  144. bool is_invalidating = false;
  145. struct ThemeCache {
  146. Ref<Texture2D> parent_folder;
  147. Ref<Texture2D> forward_folder;
  148. Ref<Texture2D> back_folder;
  149. Ref<Texture2D> open_folder;
  150. Ref<Texture2D> reload;
  151. Ref<Texture2D> toggle_hidden;
  152. Ref<Texture2D> toggle_filename_filter;
  153. Ref<Texture2D> favorite;
  154. Ref<Texture2D> mode_thumbnails;
  155. Ref<Texture2D> mode_list;
  156. Ref<Texture2D> create_folder;
  157. Ref<Texture2D> favorites_up;
  158. Ref<Texture2D> favorites_down;
  159. Ref<Texture2D> filter_box;
  160. Ref<Texture2D> file_sort_button;
  161. Ref<Texture2D> folder;
  162. Color folder_icon_color;
  163. Ref<Texture2D> action_copy;
  164. Ref<Texture2D> action_delete;
  165. Ref<Texture2D> filesystem;
  166. Ref<Texture2D> folder_medium_thumbnail;
  167. Ref<Texture2D> file_medium_thumbnail;
  168. Ref<Texture2D> folder_big_thumbnail;
  169. Ref<Texture2D> file_big_thumbnail;
  170. Ref<Texture2D> progress[8]{};
  171. } theme_cache;
  172. struct Option {
  173. String name;
  174. Vector<String> values;
  175. int default_idx = 0;
  176. };
  177. static inline PropertyListHelper base_property_helper;
  178. PropertyListHelper property_helper;
  179. Vector<Option> options;
  180. Dictionary selected_options;
  181. bool options_dirty = false;
  182. String full_dir;
  183. void update_dir();
  184. void update_file_name();
  185. void update_file_list();
  186. void update_search_filter_gui();
  187. void update_filters();
  188. void _focus_file_text();
  189. void _update_favorites();
  190. void _favorite_pressed();
  191. void _favorite_selected(int p_idx);
  192. void _favorite_move_up();
  193. void _favorite_move_down();
  194. void _update_recent();
  195. void _recent_selected(int p_idx);
  196. void _item_selected(int p_item);
  197. void _multi_selected(int p_item, bool p_selected);
  198. void _items_clear_selection(const Vector2 &p_pos, MouseButton p_mouse_button_index);
  199. void _item_dc_selected(int p_item);
  200. void _item_list_item_rmb_clicked(int p_item, const Vector2 &p_pos, MouseButton p_mouse_button_index);
  201. void _item_list_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index);
  202. void _item_menu_id_pressed(int p_option);
  203. void _select_drive(int p_idx);
  204. void _dir_submitted(const String &p_dir);
  205. void _action_pressed();
  206. void _save_confirm_pressed();
  207. void _cancel_pressed();
  208. void _filter_selected(int);
  209. void _make_dir();
  210. void _make_dir_confirm();
  211. void _focus_filter_box();
  212. void _filter_changed(const String &p_text);
  213. void _search_filter_selected();
  214. void _file_sort_popup(int p_id);
  215. void _delete_items();
  216. void _delete_files_global();
  217. void _update_drives(bool p_select = true);
  218. void _update_icons();
  219. void _go_up();
  220. void _go_back();
  221. void _go_forward();
  222. void _invalidate();
  223. virtual void _post_popup() override;
  224. void _save_to_recent();
  225. // Callback function is callback(String p_path,Ref<Texture2D> preview,Variant udata) preview null if could not load.
  226. void _thumbnail_result(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata);
  227. void _thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata);
  228. void _request_single_thumbnail(const String &p_path);
  229. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  230. bool _is_open_should_be_disabled();
  231. void _update_side_menu_visibility(bool p_native_dlg);
  232. void _native_popup();
  233. void _native_dialog_cb(bool p_ok, const Vector<String> &p_files, int p_filter, const Dictionary &p_selected_options);
  234. TypedArray<Dictionary> _get_options() const;
  235. void _update_option_controls();
  236. void _option_changed_checkbox_toggled(bool p_pressed, const String &p_name);
  237. void _option_changed_item_selected(int p_idx, const String &p_name);
  238. protected:
  239. virtual void _update_theme_item_cache() override;
  240. void _notification(int p_what);
  241. bool _set(const StringName &p_name, const Variant &p_value) { return property_helper.property_set_value(p_name, p_value); }
  242. bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
  243. void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
  244. bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
  245. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
  246. static void _bind_methods();
  247. public:
  248. Color get_dir_icon_color(const String &p_dir_path);
  249. virtual void set_visible(bool p_visible) override;
  250. virtual void popup(const Rect2i &p_rect = Rect2i()) override;
  251. // Public for use with callable_mp.
  252. void _file_submitted(const String &p_file);
  253. void popup_file_dialog();
  254. void clear_filters();
  255. void add_filter(const String &p_filter, const String &p_description = "");
  256. void set_filters(const Vector<String> &p_filters);
  257. Vector<String> get_filters() const;
  258. void clear_search_filter();
  259. void set_search_filter(const String &p_search_filter);
  260. String get_search_filter() const;
  261. void set_enable_multiple_selection(bool p_enable);
  262. Vector<String> get_selected_files() const;
  263. String get_current_dir() const;
  264. String get_current_file() const;
  265. String get_current_path() const;
  266. void set_current_dir(const String &p_dir);
  267. void set_current_file(const String &p_file);
  268. void set_current_path(const String &p_path);
  269. String get_option_name(int p_option) const;
  270. Vector<String> get_option_values(int p_option) const;
  271. int get_option_default(int p_option) const;
  272. void set_option_name(int p_option, const String &p_name);
  273. void set_option_values(int p_option, const Vector<String> &p_values);
  274. void set_option_default(int p_option, int p_index);
  275. void add_option(const String &p_name, const Vector<String> &p_values, int p_index);
  276. void set_option_count(int p_count);
  277. int get_option_count() const;
  278. Dictionary get_selected_options() const;
  279. void set_display_mode(DisplayMode p_mode);
  280. DisplayMode get_display_mode() const;
  281. void set_file_mode(FileMode p_mode);
  282. FileMode get_file_mode() const;
  283. VBoxContainer *get_vbox();
  284. LineEdit *get_line_edit() { return file; }
  285. void set_access(Access p_access);
  286. Access get_access() const;
  287. static void set_default_show_hidden_files(bool p_show);
  288. static void set_default_display_mode(DisplayMode p_mode);
  289. void set_show_hidden_files(bool p_show);
  290. void set_show_search_filter(bool p_show);
  291. bool is_showing_hidden_files() const;
  292. void invalidate();
  293. void set_disable_overwrite_warning(bool p_disable);
  294. bool is_overwrite_warning_disabled() const;
  295. void set_previews_enabled(bool p_enabled);
  296. bool are_previews_enabled();
  297. void add_side_menu(Control *p_menu, const String &p_title = "");
  298. EditorFileDialog();
  299. ~EditorFileDialog();
  300. };
  301. VARIANT_ENUM_CAST(EditorFileDialog::FileMode);
  302. VARIANT_ENUM_CAST(EditorFileDialog::Access);
  303. VARIANT_ENUM_CAST(EditorFileDialog::DisplayMode);
  304. #endif // EDITOR_FILE_DIALOG_H