script_editor_plugin.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*************************************************************************/
  2. /* script_editor_plugin.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 SCRIPT_EDITOR_PLUGIN_H
  31. #define SCRIPT_EDITOR_PLUGIN_H
  32. #include "core/script_language.h"
  33. #include "editor/code_editor.h"
  34. #include "editor/editor_help.h"
  35. #include "editor/editor_help_search.h"
  36. #include "editor/editor_plugin.h"
  37. #include "editor/script_create_dialog.h"
  38. #include "scene/gui/item_list.h"
  39. #include "scene/gui/menu_button.h"
  40. #include "scene/gui/split_container.h"
  41. #include "scene/gui/tab_container.h"
  42. #include "scene/gui/text_edit.h"
  43. #include "scene/gui/tool_button.h"
  44. #include "scene/gui/tree.h"
  45. #include "scene/main/timer.h"
  46. #include "scene/resources/text_file.h"
  47. class ScriptEditorQuickOpen : public ConfirmationDialog {
  48. GDCLASS(ScriptEditorQuickOpen, ConfirmationDialog)
  49. LineEdit *search_box;
  50. Tree *search_options;
  51. String function;
  52. void _update_search();
  53. void _sbox_input(const Ref<InputEvent> &p_ie);
  54. Vector<String> functions;
  55. void _confirmed();
  56. void _text_changed(const String &p_newtext);
  57. protected:
  58. void _notification(int p_what);
  59. static void _bind_methods();
  60. public:
  61. void popup_dialog(const Vector<String> &p_functions, bool p_dontclear = false);
  62. ScriptEditorQuickOpen();
  63. };
  64. class ScriptEditorDebugger;
  65. class ScriptEditorBase : public VBoxContainer {
  66. GDCLASS(ScriptEditorBase, VBoxContainer)
  67. protected:
  68. static void _bind_methods();
  69. public:
  70. virtual void add_syntax_highlighter(SyntaxHighlighter *p_highlighter) = 0;
  71. virtual void set_syntax_highlighter(SyntaxHighlighter *p_highlighter) = 0;
  72. virtual void apply_code() = 0;
  73. virtual RES get_edited_resource() const = 0;
  74. virtual Vector<String> get_functions() = 0;
  75. virtual void set_edited_resource(const RES &p_res) = 0;
  76. virtual void reload_text() = 0;
  77. virtual String get_name() = 0;
  78. virtual Ref<Texture> get_icon() = 0;
  79. virtual bool is_unsaved() = 0;
  80. virtual Variant get_edit_state() = 0;
  81. virtual void set_edit_state(const Variant &p_state) = 0;
  82. virtual void goto_line(int p_line, bool p_with_error = false) = 0;
  83. virtual void trim_trailing_whitespace() = 0;
  84. virtual void convert_indent_to_spaces() = 0;
  85. virtual void convert_indent_to_tabs() = 0;
  86. virtual void ensure_focus() = 0;
  87. virtual void tag_saved_version() = 0;
  88. virtual void reload(bool p_soft) {}
  89. virtual void get_breakpoints(List<int> *p_breakpoints) = 0;
  90. virtual void add_callback(const String &p_function, PoolStringArray p_args) = 0;
  91. virtual void update_settings() = 0;
  92. virtual void set_debugger_active(bool p_active) = 0;
  93. virtual bool can_lose_focus_on_node_selection() { return true; }
  94. virtual bool show_members_overview() = 0;
  95. virtual void set_tooltip_request_func(String p_method, Object *p_obj) = 0;
  96. virtual Control *get_edit_menu() = 0;
  97. virtual void clear_edit_menu() = 0;
  98. ScriptEditorBase() {}
  99. };
  100. typedef SyntaxHighlighter *(*CreateSyntaxHighlighterFunc)();
  101. typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const RES &p_resource);
  102. class EditorScriptCodeCompletionCache;
  103. class FindInFilesDialog;
  104. class FindInFilesPanel;
  105. class ScriptEditor : public PanelContainer {
  106. GDCLASS(ScriptEditor, PanelContainer);
  107. EditorNode *editor;
  108. enum {
  109. FILE_NEW,
  110. FILE_NEW_TEXTFILE,
  111. FILE_OPEN,
  112. FILE_OPEN_RECENT,
  113. FILE_SAVE,
  114. FILE_SAVE_AS,
  115. FILE_SAVE_ALL,
  116. FILE_THEME,
  117. FILE_RUN,
  118. FILE_CLOSE,
  119. CLOSE_DOCS,
  120. CLOSE_ALL,
  121. CLOSE_OTHER_TABS,
  122. TOGGLE_SCRIPTS_PANEL,
  123. SHOW_IN_FILE_SYSTEM,
  124. FILE_COPY_PATH,
  125. FILE_TOOL_RELOAD,
  126. FILE_TOOL_RELOAD_SOFT,
  127. DEBUG_NEXT,
  128. DEBUG_STEP,
  129. DEBUG_BREAK,
  130. DEBUG_CONTINUE,
  131. DEBUG_SHOW,
  132. DEBUG_SHOW_KEEP_OPEN,
  133. DEBUG_WITH_EXTERNAL_EDITOR,
  134. SEARCH_HELP,
  135. SEARCH_WEBSITE,
  136. HELP_SEARCH_FIND,
  137. HELP_SEARCH_FIND_NEXT,
  138. WINDOW_MOVE_UP,
  139. WINDOW_MOVE_DOWN,
  140. WINDOW_NEXT,
  141. WINDOW_PREV,
  142. WINDOW_SORT,
  143. WINDOW_SELECT_BASE = 100
  144. };
  145. enum {
  146. THEME_IMPORT,
  147. THEME_RELOAD,
  148. THEME_SAVE,
  149. THEME_SAVE_AS
  150. };
  151. enum ScriptSortBy {
  152. SORT_BY_NAME,
  153. SORT_BY_PATH,
  154. SORT_BY_NONE
  155. };
  156. enum ScriptListName {
  157. DISPLAY_NAME,
  158. DISPLAY_DIR_AND_NAME,
  159. DISPLAY_FULL_PATH,
  160. };
  161. HBoxContainer *menu_hb;
  162. MenuButton *file_menu;
  163. MenuButton *edit_menu;
  164. MenuButton *script_search_menu;
  165. MenuButton *debug_menu;
  166. PopupMenu *context_menu;
  167. Timer *autosave_timer;
  168. uint64_t idle;
  169. PopupMenu *recent_scripts;
  170. PopupMenu *theme_submenu;
  171. Button *help_search;
  172. Button *site_search;
  173. EditorHelpSearch *help_search_dialog;
  174. ItemList *script_list;
  175. HSplitContainer *script_split;
  176. ItemList *members_overview;
  177. VBoxContainer *overview_vbox;
  178. HBoxContainer *buttons_hbox;
  179. Label *filename;
  180. ToolButton *members_overview_alphabeta_sort_button;
  181. bool members_overview_enabled;
  182. ItemList *help_overview;
  183. bool help_overview_enabled;
  184. VSplitContainer *list_split;
  185. TabContainer *tab_container;
  186. EditorFileDialog *file_dialog;
  187. AcceptDialog *error_dialog;
  188. ConfirmationDialog *erase_tab_confirm;
  189. ScriptCreateDialog *script_create_dialog;
  190. ScriptEditorDebugger *debugger;
  191. ToolButton *scripts_visible;
  192. String current_theme;
  193. TextureRect *script_icon;
  194. Label *script_name_label;
  195. ToolButton *script_back;
  196. ToolButton *script_forward;
  197. FindInFilesDialog *find_in_files_dialog;
  198. FindInFilesPanel *find_in_files;
  199. Button *find_in_files_button;
  200. enum {
  201. SCRIPT_EDITOR_FUNC_MAX = 32,
  202. SYNTAX_HIGHLIGHTER_FUNC_MAX = 32
  203. };
  204. static int script_editor_func_count;
  205. static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX];
  206. static int syntax_highlighters_func_count;
  207. static CreateSyntaxHighlighterFunc syntax_highlighters_funcs[SYNTAX_HIGHLIGHTER_FUNC_MAX];
  208. struct ScriptHistory {
  209. Control *control;
  210. Variant state;
  211. };
  212. Vector<ScriptHistory> history;
  213. int history_pos;
  214. Vector<String> previous_scripts;
  215. void _tab_changed(int p_which);
  216. void _menu_option(int p_option);
  217. void _theme_option(int p_option);
  218. Tree *disk_changed_list;
  219. ConfirmationDialog *disk_changed;
  220. bool restoring_layout;
  221. String _get_debug_tooltip(const String &p_text, Node *_se);
  222. void _resave_scripts(const String &p_str);
  223. void _reload_scripts();
  224. bool _test_script_times_on_disk(RES p_for_script = Ref<Resource>());
  225. void _add_recent_script(String p_path);
  226. void _update_recent_scripts();
  227. void _open_recent_script(int p_idx);
  228. void _show_error_dialog(String p_path);
  229. void _close_tab(int p_idx, bool p_save = true, bool p_history_back = true);
  230. void _close_current_tab();
  231. void _close_discard_current_tab(const String &p_str);
  232. void _close_docs_tab();
  233. void _close_other_tabs();
  234. void _close_all_tabs();
  235. void _copy_script_path();
  236. void _ask_close_current_unsaved_tab(ScriptEditorBase *current);
  237. bool grab_focus_block;
  238. bool pending_auto_reload;
  239. bool auto_reload_running_scripts;
  240. void _live_auto_reload_running_scripts();
  241. void _update_selected_editor_menu();
  242. EditorScriptCodeCompletionCache *completion_cache;
  243. void _editor_play();
  244. void _editor_pause();
  245. void _editor_stop();
  246. int edit_pass;
  247. void _add_callback(Object *p_obj, const String &p_function, const PoolStringArray &p_args);
  248. void _res_saved_callback(const Ref<Resource> &p_res);
  249. bool trim_trailing_whitespace_on_save;
  250. bool use_space_indentation;
  251. bool convert_indent_on_save;
  252. void _trim_trailing_whitespace(TextEdit *tx);
  253. void _goto_script_line2(int p_line);
  254. void _goto_script_line(REF p_script, int p_line);
  255. void _breaked(bool p_breaked, bool p_can_debug);
  256. void _show_debugger(bool p_show);
  257. void _update_window_menu();
  258. void _script_created(Ref<Script> p_script);
  259. ScriptEditorBase *_get_current_editor() const;
  260. void _save_layout();
  261. void _editor_settings_changed();
  262. void _autosave_scripts();
  263. void _update_members_overview_visibility();
  264. void _update_members_overview();
  265. void _toggle_members_overview_alpha_sort(bool p_alphabetic_sort);
  266. void _update_script_names();
  267. bool _sort_list_on_update;
  268. void _members_overview_selected(int p_idx);
  269. void _script_selected(int p_idx);
  270. void _update_help_overview_visibility();
  271. void _update_help_overview();
  272. void _help_overview_selected(int p_idx);
  273. void _find_scripts(Node *p_base, Node *p_current, Set<Ref<Script> > &used);
  274. void _tree_changed();
  275. void _script_split_dragged(float);
  276. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  277. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  278. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  279. void _unhandled_input(const Ref<InputEvent> &p_event);
  280. void _script_list_gui_input(const Ref<InputEvent> &ev);
  281. void _make_script_list_context_menu();
  282. void _help_search(String p_text);
  283. void _help_index(String p_text);
  284. void _history_forward();
  285. void _history_back();
  286. bool waiting_update_names;
  287. void _help_class_open(const String &p_class);
  288. void _help_class_goto(const String &p_desc);
  289. void _update_history_arrows();
  290. void _save_history();
  291. void _go_to_tab(int p_idx);
  292. void _update_history_pos(int p_new_pos);
  293. void _update_script_colors();
  294. void _update_modified_scripts_for_external_editor(Ref<Script> p_for_script = Ref<Script>());
  295. void _script_changed();
  296. int file_dialog_option;
  297. void _file_dialog_action(String p_file);
  298. Ref<Script> _get_current_script();
  299. Array _get_open_scripts() const;
  300. Ref<TextFile> _load_text_file(const String &p_path, Error *r_error);
  301. Error _save_text_file(Ref<TextFile> p_text_file, const String &p_path);
  302. void _on_find_in_files_requested(String text);
  303. void _on_find_in_files_result_selected(String fpath, int line_number, int begin, int end);
  304. void _start_find_in_files(bool with_replace);
  305. void _on_find_in_files_modified_files(PoolStringArray paths);
  306. static void _open_script_request(const String &p_path);
  307. static ScriptEditor *script_editor;
  308. protected:
  309. void _notification(int p_what);
  310. static void _bind_methods();
  311. public:
  312. static ScriptEditor *get_singleton() { return script_editor; }
  313. void ensure_focus_current();
  314. void apply_scripts() const;
  315. void open_script_create_dialog(const String &p_base_name, const String &p_base_path);
  316. void ensure_select_current();
  317. _FORCE_INLINE_ bool edit(const RES &p_resource, bool p_grab_focus = true) { return edit(p_resource, -1, 0, p_grab_focus); }
  318. bool edit(const RES &p_resource, int p_line, int p_col, bool p_grab_focus = true);
  319. void get_breakpoints(List<String> *p_breakpoints);
  320. void save_all_scripts();
  321. void set_window_layout(Ref<ConfigFile> p_layout);
  322. void get_window_layout(Ref<ConfigFile> p_layout);
  323. void set_scene_root_script(Ref<Script> p_script);
  324. Vector<Ref<Script> > get_open_scripts() const;
  325. bool script_goto_method(Ref<Script> p_script, const String &p_method);
  326. virtual void edited_scene_changed();
  327. void notify_script_close(const Ref<Script> &p_script);
  328. void notify_script_changed(const Ref<Script> &p_script);
  329. void close_builtin_scripts_from_scene(const String &p_scene);
  330. void goto_help(const String &p_desc) { _help_class_goto(p_desc); }
  331. bool can_take_away_focus() const;
  332. VSplitContainer *get_left_list_split() { return list_split; }
  333. ScriptEditorDebugger *get_debugger() { return debugger; }
  334. void set_live_auto_reload_running_scripts(bool p_enabled);
  335. static void register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func);
  336. static void register_create_script_editor_function(CreateScriptEditorFunc p_func);
  337. ScriptEditor(EditorNode *p_editor);
  338. ~ScriptEditor();
  339. };
  340. class ScriptEditorPlugin : public EditorPlugin {
  341. GDCLASS(ScriptEditorPlugin, EditorPlugin);
  342. ScriptEditor *script_editor;
  343. EditorNode *editor;
  344. public:
  345. virtual String get_name() const { return "Script"; }
  346. bool has_main_screen() const { return true; }
  347. virtual void edit(Object *p_object);
  348. virtual bool handles(Object *p_object) const;
  349. virtual void make_visible(bool p_visible);
  350. virtual void selected_notify();
  351. virtual void save_external_data();
  352. virtual void apply_changes();
  353. virtual void restore_global_state();
  354. virtual void save_global_state();
  355. virtual void set_window_layout(Ref<ConfigFile> p_layout);
  356. virtual void get_window_layout(Ref<ConfigFile> p_layout);
  357. virtual void get_breakpoints(List<String> *p_breakpoints);
  358. virtual void edited_scene_changed();
  359. ScriptEditorPlugin(EditorNode *p_node);
  360. ~ScriptEditorPlugin();
  361. };
  362. #endif // SCRIPT_EDITOR_PLUGIN_H