Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

8.7 KB
Raw Blame
/**************************************************************************/
/*  script_text_editor.h                                                  */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#pragma once

#include "core/object/editor_language.h"
#include "editor/gui/code_editor.h"
#include "editor/script/script_editor_base.h"
#include "editor/script/script_editor_plugin.h"
#include "scene/gui/color_picker.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/option_button.h"
#include "scene/gui/tree.h"

class RichTextLabel;

class ConnectionInfoDialog : public AcceptDialog {
	GDCLASS(ConnectionInfoDialog, AcceptDialog);

	Label *method = nullptr;
	Tree *tree = nullptr;

	virtual void ok_pressed() override;

public:
	void popup_connections(const String &p_method, const Vector<Node *> &p_nodes);

	ConnectionInfoDialog();
};

class ScriptTextEditor : public CodeEditorBase {
	GDCLASS(ScriptTextEditor, CodeEditorBase);

	Variant pending_state;

	RichTextLabel *errors_panel = nullptr;
	Label *drag_info_label = nullptr;

	Vector<String> functions;
	List<EditorLanguage::Warning> warnings;
	List<EditorLanguage::ScriptError> errors;
	HashMap<String, List<EditorLanguage::ScriptError>> depended_errors;
	HashSet<int> safe_lines;

	List<Connection> missing_connections;

	int inline_color_line = -1;
	int inline_color_start = -1;
	int inline_color_end = -1;
	PopupPanel *inline_color_popup = nullptr;
	ColorPicker *inline_color_picker = nullptr;
	OptionButton *inline_color_options = nullptr;
	Ref<Texture2D> color_alpha_texture;

	ScriptEditorQuickOpen *quick_open = nullptr;
	ConnectionInfoDialog *connection_info_dialog = nullptr;

	int connection_gutter = -1;
	void _gutter_clicked(int p_line, int p_gutter);
	void _update_gutter_indexes();

	int line_number_gutter = -1;
	Color default_line_number_color = Color(1, 1, 1);
	Color safe_line_number_color = Color(1, 1, 1);

	Color marked_line_color = Color(1, 1, 1);
	Color warning_line_color = Color(1, 1, 1);
	Color folded_code_region_color = Color(1, 1, 1);

	Color warning_underline_color = Color(1, 1, 1);
	Color error_underline_color = Color(1, 1, 1);

	bool theme_loaded = false;

	enum {
		EDIT_AUTO_INDENT = CODE_ENUM_COUNT,
		EDIT_PICK_COLOR,
		EDIT_EVALUATE,
		EDIT_CREATE_CODE_REGION,

		SEARCH_LOCATE_FUNCTION,

		SHOW_TOOLTIP_AT_CARET,
		HELP_CONTEXTUAL,
		LOOKUP_SYMBOL,
		RENAME_SYMBOL,
	};

	class EditMenusScTE : public EditMenusCEB {
		GDCLASS(EditMenusScTE, EditMenusCEB);

	protected:
		void _update_breakpoint_list() override;

	public:
		virtual bool handles(ScriptEditorBase *p_seb) override { return Object::cast_to<ScriptTextEditor>(p_seb); }

		EditMenusScTE(DocumentEditorContainer *p_document_editor_container);
	};

	void _script_res_changed();
	void _enable_code_editor();

	struct DraggedExport {
		ObjectID obj_id;
		String variable_name;
		Variant value;
		String class_name;
	};

	LocalVector<DraggedExport> pending_dragged_exports;
	Vector<ObjectID> _get_objects_for_export_assignment() const;
	String _get_dropped_resource_as_exported_member(const Ref<Resource> &p_resource, const Vector<ObjectID> &p_script_instance_obj_ids);
	void _assign_dragged_export_variables();

	Timer *hover_tooltip_timer = nullptr;
	Point2i hover_tooltip_pos;
	void _on_hover_tooltip_timer_timeout();
	void _apply_project_settings();
	static bool _is_line_col_in_range(int p_line, int p_column, int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
		// TODO: Duplicate of logic from TextEdit::_is_line_col_in_range, minus the include_edges logic.
		// At some point, this function should probably be unified.
		return (p_line >= p_from_line && p_line <= p_to_line && (p_line > p_from_line || p_column >= p_from_column) && (p_line < p_to_line || p_column <= p_to_column));
	}

	static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource);

protected:
	virtual void _breakpoint_toggled(int p_row) override;

	void _on_caret_moved();

	void _update_warnings();
	void _update_errors();

	virtual void _code_complete_script(const String &p_code, List<EditorLanguage::CompletionOption> *r_options, bool &r_force) override;

	void _set_theme_for_script();
	void _show_errors_panel(bool p_show);
	void _error_clicked(const Variant &p_line);
	virtual bool _warning_clicked(const Variant &p_line) override;
	void _on_mouse_exited();

	bool _is_valid_color_info(const Dictionary &p_info);
	Array _inline_object_parse(const String &p_text);
	void _inline_object_draw(const Dictionary &p_info, const Rect2 &p_rect);
	void _inline_object_handle_click(const Dictionary &p_info, const Rect2 &p_rect);
	void _open_picker();
	void _picker_color_changed(const Color &p_color);
	void _update_color_constructor_options();
	void _update_background_color();
	void _update_color_text();

	void _notification(int p_what);

	void _edit_option_toggle_inline_comment();

	void _lookup_symbol(const String &p_symbol, int p_row, int p_column);
	void _validate_symbol(const String &p_symbol);

	void _show_symbol_tooltip(const String &p_symbol, int p_row, int p_column, bool p_shortcut = false);

	Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
	bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
	void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);

	void _set_drop_info_text(const Dictionary &p_info) const;

	String _get_absolute_path(const String &rel_path);

	void _goto_line(int p_line);

	void _make_ste_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_allow_rename, const Vector2 &p_pos);
	Dictionary _get_context_data() const;

	virtual void _text_edit_gui_input(const Ref<InputEvent> &p_ev) override;
	virtual bool _edit_option(int p_op) override;

	virtual void _load_theme_settings() override;
	virtual void _validate_script() override;

public:
	void _update_connected_methods();

	virtual void shortcut_input(const Ref<InputEvent> &p_event) override;

	virtual void apply_code() override;
	virtual void set_edited_resource(const Ref<Resource> &p_res) override;
	virtual void enable_editor() override;
	virtual Vector<String> get_functions() override;

	virtual EditMenusBase *create_edit_menu(DocumentEditorContainer *p_document_editor_container) override { return memnew(EditMenusScTE(p_document_editor_container)); }

	virtual Ref<Texture2D> get_theme_icon() override;

	virtual Variant get_edit_state() override;
	virtual void set_edit_state(const Variant &p_state, bool p_grab_focus = true) override;

	virtual PackedInt32Array get_breakpoints() override;
	virtual void clear_breakpoints() override;

	virtual void add_callback(const String &p_function, const PackedStringArray &p_args);
	virtual void update_settings() override;

	static void register_editor();

	Variant get_previous_state();
	void store_previous_state();

	ScriptTextEditor();
	~ScriptTextEditor();
};