script_editor_plugin.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor/code_editor.h"
  33. #include "editor/editor_help.h"
  34. #include "editor/editor_plugin.h"
  35. #include "editor/script_create_dialog.h"
  36. #include "scene/gui/item_list.h"
  37. #include "scene/gui/menu_button.h"
  38. #include "scene/gui/split_container.h"
  39. #include "scene/gui/tab_container.h"
  40. #include "scene/gui/text_edit.h"
  41. #include "scene/gui/tool_button.h"
  42. #include "scene/gui/tree.h"
  43. #include "scene/main/timer.h"
  44. #include "script_language.h"
  45. class ScriptEditorQuickOpen : public ConfirmationDialog {
  46. GDCLASS(ScriptEditorQuickOpen, ConfirmationDialog)
  47. LineEdit *search_box;
  48. Tree *search_options;
  49. String function;
  50. void _update_search();
  51. void _sbox_input(const Ref<InputEvent> &p_ie);
  52. Vector<String> functions;
  53. void _confirmed();
  54. void _text_changed(const String &p_newtext);
  55. protected:
  56. void _notification(int p_what);
  57. static void _bind_methods();
  58. public:
  59. void popup(const Vector<String> &p_functions, bool p_dontclear = false);
  60. ScriptEditorQuickOpen();
  61. };
  62. class ScriptEditorDebugger;
  63. class ScriptEditorBase : public Control {
  64. GDCLASS(ScriptEditorBase, Control);
  65. protected:
  66. static void _bind_methods();
  67. public:
  68. virtual void apply_code() = 0;
  69. virtual Ref<Script> get_edited_script() const = 0;
  70. virtual Vector<String> get_functions() = 0;
  71. virtual void set_edited_script(const Ref<Script> &p_script) = 0;
  72. virtual void reload_text() = 0;
  73. virtual String get_name() = 0;
  74. virtual Ref<Texture> get_icon() = 0;
  75. virtual bool is_unsaved() = 0;
  76. virtual Variant get_edit_state() = 0;
  77. virtual void set_edit_state(const Variant &p_state) = 0;
  78. virtual void goto_line(int p_line, bool p_with_error = false) = 0;
  79. virtual void trim_trailing_whitespace() = 0;
  80. virtual void convert_indent_to_spaces() = 0;
  81. virtual void convert_indent_to_tabs() = 0;
  82. virtual void ensure_focus() = 0;
  83. virtual void tag_saved_version() = 0;
  84. virtual void reload(bool p_soft) = 0;
  85. virtual void get_breakpoints(List<int> *p_breakpoints) = 0;
  86. virtual void add_callback(const String &p_function, PoolStringArray p_args) = 0;
  87. virtual void update_settings() = 0;
  88. virtual void set_debugger_active(bool p_active) = 0;
  89. virtual bool can_lose_focus_on_node_selection() { return true; }
  90. virtual bool show_members_overview() = 0;
  91. virtual void set_tooltip_request_func(String p_method, Object *p_obj) = 0;
  92. virtual Control *get_edit_menu() = 0;
  93. virtual void clear_edit_menu() = 0;
  94. ScriptEditorBase() {}
  95. };
  96. typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const Ref<Script> &p_script);
  97. class EditorScriptCodeCompletionCache;
  98. class ScriptEditor : public PanelContainer {
  99. GDCLASS(ScriptEditor, PanelContainer);
  100. EditorNode *editor;
  101. enum {
  102. FILE_NEW,
  103. FILE_OPEN,
  104. FILE_OPEN_RECENT,
  105. FILE_SAVE,
  106. FILE_SAVE_AS,
  107. FILE_SAVE_ALL,
  108. FILE_IMPORT_THEME,
  109. FILE_RELOAD_THEME,
  110. FILE_SAVE_THEME,
  111. FILE_SAVE_THEME_AS,
  112. FILE_RUN,
  113. FILE_CLOSE,
  114. CLOSE_DOCS,
  115. CLOSE_ALL,
  116. TOGGLE_SCRIPTS_PANEL,
  117. FILE_TOOL_RELOAD,
  118. FILE_TOOL_RELOAD_SOFT,
  119. DEBUG_NEXT,
  120. DEBUG_STEP,
  121. DEBUG_BREAK,
  122. DEBUG_CONTINUE,
  123. DEBUG_SHOW,
  124. DEBUG_SHOW_KEEP_OPEN,
  125. DEBUG_WITH_EXTERNAL_EDITOR,
  126. SEARCH_HELP,
  127. SEARCH_CLASSES,
  128. SEARCH_WEBSITE,
  129. HELP_SEARCH_FIND,
  130. HELP_SEARCH_FIND_NEXT,
  131. WINDOW_MOVE_LEFT,
  132. WINDOW_MOVE_RIGHT,
  133. WINDOW_NEXT,
  134. WINDOW_PREV,
  135. WINDOW_SELECT_BASE = 100
  136. };
  137. enum ScriptSortBy {
  138. SORT_BY_NAME,
  139. SORT_BY_PATH,
  140. };
  141. enum ScriptListName {
  142. DISPLAY_NAME,
  143. DISPLAY_DIR_AND_NAME,
  144. DISPLAY_FULL_PATH,
  145. };
  146. HBoxContainer *menu_hb;
  147. MenuButton *file_menu;
  148. MenuButton *edit_menu;
  149. MenuButton *script_search_menu;
  150. MenuButton *debug_menu;
  151. Timer *autosave_timer;
  152. uint64_t idle;
  153. PopupMenu *recent_scripts;
  154. Button *help_search;
  155. Button *site_search;
  156. Button *class_search;
  157. EditorHelpSearch *help_search_dialog;
  158. ItemList *script_list;
  159. HSplitContainer *script_split;
  160. ItemList *members_overview;
  161. bool members_overview_enabled;
  162. ItemList *help_overview;
  163. bool help_overview_enabled;
  164. VSplitContainer *list_split;
  165. TabContainer *tab_container;
  166. EditorFileDialog *file_dialog;
  167. ConfirmationDialog *erase_tab_confirm;
  168. ScriptCreateDialog *script_create_dialog;
  169. ScriptEditorDebugger *debugger;
  170. ToolButton *scripts_visible;
  171. String current_theme;
  172. TextureRect *script_icon;
  173. Label *script_name_label;
  174. ToolButton *script_back;
  175. ToolButton *script_forward;
  176. enum {
  177. SCRIPT_EDITOR_FUNC_MAX = 32
  178. };
  179. static int script_editor_func_count;
  180. static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX];
  181. struct ScriptHistory {
  182. Control *control;
  183. Variant state;
  184. };
  185. Vector<ScriptHistory> history;
  186. int history_pos;
  187. Vector<String> previous_scripts;
  188. EditorHelpIndex *help_index;
  189. void _tab_changed(int p_which);
  190. void _menu_option(int p_option);
  191. Tree *disk_changed_list;
  192. ConfirmationDialog *disk_changed;
  193. bool restoring_layout;
  194. String _get_debug_tooltip(const String &p_text, Node *_se);
  195. void _resave_scripts(const String &p_str);
  196. void _reload_scripts();
  197. bool _test_script_times_on_disk(Ref<Script> p_for_script = Ref<Script>());
  198. void _add_recent_script(String p_path);
  199. void _update_recent_scripts();
  200. void _open_recent_script(int p_idx);
  201. void _close_tab(int p_idx, bool p_save = true);
  202. void _close_current_tab();
  203. void _close_discard_current_tab(const String &p_str);
  204. void _close_docs_tab();
  205. void _close_all_tabs();
  206. void _ask_close_current_unsaved_tab(ScriptEditorBase *current);
  207. bool grab_focus_block;
  208. bool pending_auto_reload;
  209. bool auto_reload_running_scripts;
  210. void _live_auto_reload_running_scripts();
  211. void _update_selected_editor_menu();
  212. EditorScriptCodeCompletionCache *completion_cache;
  213. void _editor_play();
  214. void _editor_pause();
  215. void _editor_stop();
  216. int edit_pass;
  217. void _add_callback(Object *p_obj, const String &p_function, const PoolStringArray &p_args);
  218. void _res_saved_callback(const Ref<Resource> &p_res);
  219. bool trim_trailing_whitespace_on_save;
  220. bool use_space_indentation;
  221. bool convert_indent_on_save;
  222. void _trim_trailing_whitespace(TextEdit *tx);
  223. void _goto_script_line2(int p_line);
  224. void _goto_script_line(REF p_script, int p_line);
  225. void _breaked(bool p_breaked, bool p_can_debug);
  226. void _show_debugger(bool p_show);
  227. void _update_window_menu();
  228. void _script_created(Ref<Script> p_script);
  229. void _save_layout();
  230. void _editor_settings_changed();
  231. void _autosave_scripts();
  232. void _update_members_overview_visibility();
  233. void _update_members_overview();
  234. void _update_script_names();
  235. void _members_overview_selected(int p_idx);
  236. void _script_selected(int p_idx);
  237. void _update_help_overview_visibility();
  238. void _update_help_overview();
  239. void _help_overview_selected(int p_idx);
  240. void _find_scripts(Node *p_base, Node *p_current, Set<Ref<Script> > &used);
  241. void _tree_changed();
  242. void _script_split_dragged(float);
  243. void _unhandled_input(const Ref<InputEvent> &p_event);
  244. void _help_search(String p_text);
  245. void _help_index(String p_text);
  246. void _history_forward();
  247. void _history_back();
  248. bool waiting_update_names;
  249. void _help_class_open(const String &p_class);
  250. void _help_class_goto(const String &p_desc);
  251. void _update_history_arrows();
  252. void _save_history();
  253. void _go_to_tab(int p_idx);
  254. void _update_history_pos(int p_new_pos);
  255. void _update_script_colors();
  256. void _update_modified_scripts_for_external_editor(Ref<Script> p_for_script = Ref<Script>());
  257. void _script_changed();
  258. int file_dialog_option;
  259. void _file_dialog_action(String p_file);
  260. Ref<Script> _get_current_script();
  261. Array _get_open_scripts() const;
  262. static void _open_script_request(const String &p_path);
  263. static ScriptEditor *script_editor;
  264. protected:
  265. void _notification(int p_what);
  266. static void _bind_methods();
  267. public:
  268. static ScriptEditor *get_singleton() { return script_editor; }
  269. void ensure_focus_current();
  270. void apply_scripts() const;
  271. void ensure_select_current();
  272. _FORCE_INLINE_ bool edit(const Ref<Script> &p_script, bool p_grab_focus = true) { return edit(p_script, -1, 0, p_grab_focus); }
  273. bool edit(const Ref<Script> &p_script, int p_line, int p_col, bool p_grab_focus = true);
  274. void get_breakpoints(List<String> *p_breakpoints);
  275. void save_all_scripts();
  276. void set_window_layout(Ref<ConfigFile> p_layout);
  277. void get_window_layout(Ref<ConfigFile> p_layout);
  278. void set_scene_root_script(Ref<Script> p_script);
  279. Vector<Ref<Script> > get_open_scripts() const;
  280. bool script_goto_method(Ref<Script> p_script, const String &p_method);
  281. virtual void edited_scene_changed();
  282. void notify_script_close(const Ref<Script> &p_script);
  283. void notify_script_changed(const Ref<Script> &p_script);
  284. void close_builtin_scripts_from_scene(const String &p_scene);
  285. void goto_help(const String &p_desc) { _help_class_goto(p_desc); }
  286. bool can_take_away_focus() const;
  287. VSplitContainer *get_left_list_split() { return list_split; }
  288. ScriptEditorDebugger *get_debugger() { return debugger; }
  289. void set_live_auto_reload_running_scripts(bool p_enabled);
  290. static void register_create_script_editor_function(CreateScriptEditorFunc p_func);
  291. ScriptEditor(EditorNode *p_editor);
  292. ~ScriptEditor();
  293. };
  294. class ScriptEditorPlugin : public EditorPlugin {
  295. GDCLASS(ScriptEditorPlugin, EditorPlugin);
  296. ScriptEditor *script_editor;
  297. EditorNode *editor;
  298. public:
  299. virtual String get_name() const { return "Script"; }
  300. bool has_main_screen() const { return true; }
  301. virtual void edit(Object *p_object);
  302. virtual bool handles(Object *p_object) const;
  303. virtual void make_visible(bool p_visible);
  304. virtual void selected_notify();
  305. virtual void save_external_data();
  306. virtual void apply_changes();
  307. virtual void restore_global_state();
  308. virtual void save_global_state();
  309. virtual void set_window_layout(Ref<ConfigFile> p_layout);
  310. virtual void get_window_layout(Ref<ConfigFile> p_layout);
  311. virtual void get_breakpoints(List<String> *p_breakpoints);
  312. virtual void edited_scene_changed();
  313. ScriptEditorPlugin(EditorNode *p_node);
  314. ~ScriptEditorPlugin();
  315. };
  316. #endif // SCRIPT_EDITOR_PLUGIN_H