editor_file_dialog.h 12 KB

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