script_editor_plugin.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**************************************************************************/
  2. /* script_editor_plugin.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 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. REPLACE_IN_FILES,
  142. SEARCH_HELP,
  143. SEARCH_WEBSITE,
  144. HELP_SEARCH_FIND,
  145. HELP_SEARCH_FIND_NEXT,
  146. HELP_SEARCH_FIND_PREVIOUS,
  147. WINDOW_MOVE_UP,
  148. WINDOW_MOVE_DOWN,
  149. WINDOW_NEXT,
  150. WINDOW_PREV,
  151. WINDOW_SORT,
  152. WINDOW_SELECT_BASE = 100
  153. };
  154. enum {
  155. THEME_IMPORT,
  156. THEME_RELOAD,
  157. THEME_SAVE,
  158. THEME_SAVE_AS
  159. };
  160. enum ScriptSortBy {
  161. SORT_BY_NAME,
  162. SORT_BY_PATH,
  163. SORT_BY_NONE
  164. };
  165. enum ScriptListName {
  166. DISPLAY_NAME,
  167. DISPLAY_DIR_AND_NAME,
  168. DISPLAY_FULL_PATH,
  169. };
  170. HBoxContainer *menu_hb;
  171. MenuButton *file_menu;
  172. MenuButton *edit_menu;
  173. MenuButton *script_search_menu;
  174. MenuButton *debug_menu;
  175. PopupMenu *context_menu;
  176. Timer *autosave_timer;
  177. uint64_t idle;
  178. PopupMenu *recent_scripts;
  179. PopupMenu *theme_submenu;
  180. Button *help_search;
  181. Button *site_search;
  182. EditorHelpSearch *help_search_dialog;
  183. ItemList *script_list;
  184. HSplitContainer *script_split;
  185. ItemList *members_overview;
  186. LineEdit *filter_scripts;
  187. LineEdit *filter_methods;
  188. VBoxContainer *scripts_vbox;
  189. VBoxContainer *overview_vbox;
  190. HBoxContainer *buttons_hbox;
  191. Label *filename;
  192. ToolButton *members_overview_alphabeta_sort_button;
  193. bool members_overview_enabled;
  194. ItemList *help_overview;
  195. bool help_overview_enabled;
  196. VSplitContainer *list_split;
  197. TabContainer *tab_container;
  198. EditorFileDialog *file_dialog;
  199. AcceptDialog *error_dialog;
  200. ConfirmationDialog *erase_tab_confirm;
  201. ScriptCreateDialog *script_create_dialog;
  202. ScriptEditorDebugger *debugger;
  203. ToolButton *scripts_visible;
  204. String current_theme;
  205. TextureRect *script_icon;
  206. Label *script_name_label;
  207. ToolButton *script_back;
  208. ToolButton *script_forward;
  209. FindInFilesDialog *find_in_files_dialog;
  210. FindInFilesPanel *find_in_files;
  211. Button *find_in_files_button;
  212. enum {
  213. SCRIPT_EDITOR_FUNC_MAX = 32,
  214. SYNTAX_HIGHLIGHTER_FUNC_MAX = 32
  215. };
  216. static int script_editor_func_count;
  217. static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX];
  218. static int syntax_highlighters_func_count;
  219. static CreateSyntaxHighlighterFunc syntax_highlighters_funcs[SYNTAX_HIGHLIGHTER_FUNC_MAX];
  220. struct ScriptHistory {
  221. Control *control;
  222. Variant state;
  223. };
  224. Vector<ScriptHistory> history;
  225. int history_pos;
  226. List<String> previous_scripts;
  227. List<int> script_close_queue;
  228. void _tab_changed(int p_which);
  229. void _menu_option(int p_option);
  230. void _update_debug_options();
  231. void _theme_option(int p_option);
  232. void _show_save_theme_as_dialog();
  233. bool _has_docs_tab() const;
  234. bool _has_script_tab() const;
  235. void _prepare_file_menu();
  236. Tree *disk_changed_list;
  237. ConfirmationDialog *disk_changed;
  238. bool restoring_layout;
  239. String _get_debug_tooltip(const String &p_text, Node *_se);
  240. void _resave_scripts(const String &p_str);
  241. bool _test_script_times_on_disk(RES p_for_script = Ref<Resource>());
  242. void _add_recent_script(String p_path);
  243. void _update_recent_scripts();
  244. void _open_recent_script(int p_idx);
  245. void _show_error_dialog(String p_path);
  246. void _close_tab(int p_idx, bool p_save = true, bool p_history_back = true);
  247. void _close_current_tab(bool p_save = true);
  248. void _close_discard_current_tab(const String &p_str);
  249. void _close_docs_tab();
  250. void _close_other_tabs();
  251. void _close_all_tabs();
  252. void _queue_close_tabs();
  253. void _copy_script_path();
  254. void _ask_close_current_unsaved_tab(ScriptEditorBase *current);
  255. bool grab_focus_block;
  256. bool pending_auto_reload;
  257. bool auto_reload_running_scripts;
  258. void _trigger_live_script_reload();
  259. void _live_auto_reload_running_scripts();
  260. void _update_selected_editor_menu();
  261. EditorScriptCodeCompletionCache *completion_cache;
  262. void _editor_play();
  263. void _editor_pause();
  264. void _editor_stop();
  265. int edit_pass;
  266. void _add_callback(Object *p_obj, const String &p_function, const PoolStringArray &p_args);
  267. void _res_saved_callback(const Ref<Resource> &p_res);
  268. void _scene_saved_callback(const String &p_path);
  269. bool trim_trailing_whitespace_on_save;
  270. bool use_space_indentation;
  271. bool convert_indent_on_save;
  272. void _goto_script_line2(int p_line);
  273. void _goto_script_line(REF p_script, int p_line);
  274. void _set_execution(REF p_script, int p_line);
  275. void _clear_execution(REF p_script);
  276. void _breaked(bool p_breaked, bool p_can_debug);
  277. void _show_debugger(bool p_show);
  278. void _script_created(Ref<Script> p_script);
  279. ScriptEditorBase *_get_current_editor() const;
  280. Control *_get_base_editor() const;
  281. void _save_layout();
  282. void _editor_settings_changed();
  283. void _autosave_scripts();
  284. void _update_autosave_timer();
  285. void _update_members_overview_visibility();
  286. void _update_members_overview();
  287. void _toggle_members_overview_alpha_sort(bool p_alphabetic_sort);
  288. void _filter_scripts_text_changed(const String &p_newtext);
  289. void _filter_methods_text_changed(const String &p_newtext);
  290. void _update_script_names();
  291. void _update_script_connections();
  292. bool _sort_list_on_update;
  293. void _members_overview_selected(int p_idx);
  294. void _script_selected(int p_idx);
  295. void _update_help_overview_visibility();
  296. void _update_help_overview();
  297. void _help_overview_selected(int p_idx);
  298. void _find_scripts(Node *p_base, Node *p_current, Set<Ref<Script>> &used);
  299. void _tree_changed();
  300. void _script_split_dragged(float);
  301. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  302. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  303. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  304. void _input(const Ref<InputEvent> &p_event);
  305. void _unhandled_input(const Ref<InputEvent> &p_event);
  306. void _script_list_gui_input(const Ref<InputEvent> &ev);
  307. void _make_script_list_context_menu();
  308. void _help_search(String p_text);
  309. void _history_forward();
  310. void _history_back();
  311. bool waiting_update_names;
  312. void _help_class_open(const String &p_class);
  313. void _help_class_goto(const String &p_desc);
  314. void _update_history_arrows();
  315. void _save_history();
  316. void _go_to_tab(int p_idx);
  317. void _update_history_pos(int p_new_pos);
  318. void _update_script_colors();
  319. void _update_modified_scripts_for_external_editor(Ref<Script> p_for_script = Ref<Script>());
  320. void _script_changed();
  321. int file_dialog_option;
  322. void _file_dialog_action(String p_file);
  323. Ref<Script> _get_current_script();
  324. Array _get_open_scripts() const;
  325. Ref<TextFile> _load_text_file(const String &p_path, Error *r_error);
  326. Error _save_text_file(Ref<TextFile> p_text_file, const String &p_path);
  327. void _on_find_in_files_requested(String text);
  328. void _on_replace_in_files_requested(String text);
  329. void _on_find_in_files_result_selected(String fpath, int line_number, int begin, int end);
  330. void _start_find_in_files(bool with_replace);
  331. void _on_find_in_files_modified_files(PoolStringArray paths);
  332. static void _open_script_request(const String &p_path);
  333. static ScriptEditor *script_editor;
  334. protected:
  335. void _notification(int p_what);
  336. static void _bind_methods();
  337. public:
  338. static ScriptEditor *get_singleton() { return script_editor; }
  339. bool toggle_scripts_panel();
  340. bool is_scripts_panel_toggled();
  341. void ensure_focus_current();
  342. void apply_scripts() const;
  343. void open_script_create_dialog(const String &p_base_name, const String &p_base_path);
  344. void reload_scripts();
  345. void ensure_select_current();
  346. _FORCE_INLINE_ bool edit(const RES &p_resource, bool p_grab_focus = true) { return edit(p_resource, -1, 0, p_grab_focus); }
  347. bool edit(const RES &p_resource, int p_line, int p_col, bool p_grab_focus = true);
  348. void get_breakpoints(List<String> *p_breakpoints);
  349. void save_current_script();
  350. void save_all_scripts();
  351. void set_window_layout(Ref<ConfigFile> p_layout);
  352. void get_window_layout(Ref<ConfigFile> p_layout);
  353. void set_scene_root_script(Ref<Script> p_script);
  354. Vector<Ref<Script>> get_open_scripts() const;
  355. bool script_goto_method(Ref<Script> p_script, const String &p_method);
  356. virtual void edited_scene_changed();
  357. void notify_script_close(const Ref<Script> &p_script);
  358. void notify_script_changed(const Ref<Script> &p_script);
  359. void close_builtin_scripts_from_scene(const String &p_scene);
  360. void goto_help(const String &p_desc) { _help_class_goto(p_desc); }
  361. bool can_take_away_focus() const;
  362. VSplitContainer *get_left_list_split() { return list_split; }
  363. ScriptEditorDebugger *get_debugger() { return debugger; }
  364. void set_live_auto_reload_running_scripts(bool p_enabled);
  365. static void register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func);
  366. static void register_create_script_editor_function(CreateScriptEditorFunc p_func);
  367. ScriptEditor(EditorNode *p_editor);
  368. ~ScriptEditor();
  369. };
  370. class ScriptEditorPlugin : public EditorPlugin {
  371. GDCLASS(ScriptEditorPlugin, EditorPlugin);
  372. ScriptEditor *script_editor;
  373. EditorNode *editor;
  374. public:
  375. virtual String get_name() const { return "Script"; }
  376. bool has_main_screen() const { return true; }
  377. virtual void edit(Object *p_object);
  378. virtual bool handles(Object *p_object) const;
  379. virtual void make_visible(bool p_visible);
  380. virtual void selected_notify();
  381. virtual void save_external_data();
  382. virtual void apply_changes();
  383. virtual void restore_global_state();
  384. virtual void save_global_state();
  385. virtual void set_window_layout(Ref<ConfigFile> p_layout);
  386. virtual void get_window_layout(Ref<ConfigFile> p_layout);
  387. virtual void get_breakpoints(List<String> *p_breakpoints);
  388. virtual void edited_scene_changed();
  389. ScriptEditorPlugin(EditorNode *p_node);
  390. ~ScriptEditorPlugin();
  391. };
  392. #endif // SCRIPT_EDITOR_PLUGIN_H