2
0

script_editor_plugin.h 15 KB

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