Ver código fonte

Merge pull request #5657 from Paulb23/block_caret

Added block caret to TextEdit
Rémi Verschelde 9 anos atrás
pai
commit
3183834c5d

+ 16 - 0
doc/base/classes.xml

@@ -39234,6 +39234,20 @@
 				Set the text editor caret blink speed. Cannot be less then or equal to 0.
 			</description>
 		</method>
+		<method name="cursor_set_block_mode">
+			<argument index="0" name="enable" type="bool">
+			</argument>
+			<description>
+				Set the text editor caret to block mode.
+			</description>
+		</method>
+		<method name="cursor_is_block_mode" qualifiers="const">
+			<return type="bool">
+			</return>
+			<description>
+				Gets whether the text editor caret is in block mode.
+			</description>
+		</method>
 		<method name="cursor_set_column">
 			<argument index="0" name="column" type="int">
 			</argument>
@@ -39524,6 +39538,8 @@
 		</theme_item>
 		<theme_item name="breakpoint_color" type="Color">
 		</theme_item>
+		<theme_item name="caret_background_color" type="Color">
+		</theme_item>
 		<theme_item name="caret_color" type="Color">
 		</theme_item>
 		<theme_item name="completion" type="StyleBox">

+ 38 - 17
scene/gui/text_edit.cpp

@@ -999,34 +999,38 @@ void TextEdit::_notification(int p_what) {
 						}
 					}
 
-
-					if (str[j]>=32)
-						cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),str[j],str[j+1],in_selection?cache.font_selected_color:color);
-
-					else if (draw_tabs && str[j]=='\t') {
-						int yofs= (get_row_height() - cache.tab_icon->get_height())/2;
-						cache.tab_icon->draw(ci, Point2(char_ofs+char_margin,ofs_y+yofs),in_selection?cache.font_selected_color:color);
-					}
-
-
 					if (cursor.column==j && cursor.line==line) {
 
 						cursor_pos = Point2i( char_ofs+char_margin, ofs_y );
 
 						if (insert_mode) {
-							cursor_pos.y += get_row_height();
+							cursor_pos.y += (get_row_height() - 3);
 						}
 
 						if (draw_caret) {
 							if (insert_mode) {
-								VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,1)),cache.caret_color);
+								int caret_h = (block_caret) ? 4 : 1;
+								VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,caret_h)),cache.caret_color);
 							} else {
-								VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color);
+								int caret_w = (block_caret) ? char_w : 1;
+								VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(caret_w,get_row_height())),cache.caret_color);
 							}
 						}
 					}
-					char_ofs+=char_w;
 
+					if (cursor.column==j && cursor.line==line && block_caret && draw_caret && !insert_mode) {
+						color = cache.caret_background_color;
+					}
+
+					if (str[j]>=32)
+						cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),str[j],str[j+1],in_selection?cache.font_selected_color:color);
+
+					else if (draw_tabs && str[j]=='\t') {
+						int yofs= (get_row_height() - cache.tab_icon->get_height())/2;
+						cache.tab_icon->draw(ci, Point2(char_ofs+char_margin,ofs_y+yofs),in_selection?cache.font_selected_color:color);
+					}
+
+					char_ofs+=char_w;
 				}
 
 				if (cursor.column==str.length() && cursor.line==line && (char_ofs+char_margin)>=xmargin_beg) {
@@ -1034,15 +1038,18 @@ void TextEdit::_notification(int p_what) {
 					cursor_pos=Point2i( char_ofs+char_margin, ofs_y );
 
 					if (insert_mode) {
-						cursor_pos.y += get_row_height();
+						cursor_pos.y += (get_row_height() - 3);
 					}
 
 					if (draw_caret) {
 						if (insert_mode) {
 							int char_w = cache.font->get_char_size(' ').width;
-							VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,1)),cache.caret_color);
+							int caret_h = (block_caret) ? 4 : 1;
+							VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,caret_h)),cache.caret_color);
 						} else {
-							VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color);
+							int char_w = cache.font->get_char_size(' ').width;
+							int caret_w = (block_caret) ? char_w : 1;
+							VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(caret_w,get_row_height())),cache.caret_color);
 						}
 					}
 				}
@@ -3074,6 +3081,15 @@ void TextEdit::cursor_set_blink_speed(const float p_speed) {
 	caret_blink_timer->set_wait_time(p_speed);
 }
 
+void TextEdit::cursor_set_block_mode(const bool p_enable){
+	block_caret = p_enable;
+	update();
+}
+
+bool TextEdit::cursor_is_block_mode() const {
+	return block_caret;
+}
+
 
 void TextEdit::_scroll_moved(double p_to_val) {
 
@@ -3315,6 +3331,7 @@ void TextEdit::_update_caches() {
 	cache.completion_font_color=get_color("completion_font_color");
 	cache.font=get_font("font");
 	cache.caret_color=get_color("caret_color");
+	cache.caret_background_color=get_color("caret_background_color");
 	cache.line_number_color=get_color("line_number_color");
 	cache.font_color=get_color("font_color");
 	cache.font_selected_color=get_color("font_selected_color");
@@ -4417,6 +4434,8 @@ void TextEdit::_bind_methods() {
 	ObjectTypeDB::bind_method(_MD("cursor_get_blink_enabled"),&TextEdit::cursor_get_blink_enabled);
 	ObjectTypeDB::bind_method(_MD("cursor_set_blink_speed", "blink_speed"),&TextEdit::cursor_set_blink_speed);
 	ObjectTypeDB::bind_method(_MD("cursor_get_blink_speed"),&TextEdit::cursor_get_blink_speed);
+	ObjectTypeDB::bind_method(_MD("cursor_set_block_mode", "enable"), &TextEdit::cursor_set_block_mode);
+	ObjectTypeDB::bind_method(_MD("cursor_is_block_mode"), &TextEdit::cursor_is_block_mode);
 
 	ObjectTypeDB::bind_method(_MD("set_readonly","enable"),&TextEdit::set_readonly);
 	ObjectTypeDB::bind_method(_MD("set_wrap","enable"),&TextEdit::set_wrap);
@@ -4462,6 +4481,7 @@ void TextEdit::_bind_methods() {
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_line_numbers"), _SCS("set_show_line_numbers"), _SCS("is_show_line_numbers_enabled"));
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), _SCS("set_highlight_all_occurrences"), _SCS("is_highlight_all_occurrences_enabled"));
 
+	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret/block_caret"), _SCS("cursor_set_block_mode"), _SCS("cursor_is_block_mode"));
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret/caret_blink"), _SCS("cursor_set_blink_enabled"), _SCS("cursor_get_blink_enabled"));
 	ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret/caret_blink_speed",PROPERTY_HINT_RANGE,"0.1,10,0.1"), _SCS("cursor_set_blink_speed"),_SCS("cursor_get_blink_speed") );
 
@@ -4527,6 +4547,7 @@ TextEdit::TextEdit()  {
 	selection.active=false;
 	syntax_coloring=false;
 
+	block_caret=false;
 	caret_blink_enabled=false;
 	caret_blink_timer = memnew(Timer);
 	add_child(caret_blink_timer);

+ 5 - 0
scene/gui/text_edit.h

@@ -79,6 +79,7 @@ class TextEdit : public Control  {
 		Color completion_existing_color;
 		Color completion_font_color;
 		Color caret_color;
+		Color caret_background_color;
 		Color line_number_color;
 		Color font_color;
 		Color font_selected_color;
@@ -222,6 +223,7 @@ class TextEdit : public Control  {
 	bool caret_blink_enabled;
 	bool draw_caret;
 	bool window_has_focus;
+	bool block_caret;
 
 	bool setting_row;
 	bool wrap;
@@ -406,6 +408,9 @@ public:
 	float cursor_get_blink_speed() const;
 	void cursor_set_blink_speed(const float p_speed);
 
+	void cursor_set_block_mode(const bool p_enable);
+	bool cursor_is_block_mode() const;
+
 	void set_readonly(bool p_readonly);
 
 	void set_max_chars(int p_max_chars);

+ 1 - 0
scene/resources/default_theme/default_theme.cpp

@@ -481,6 +481,7 @@ void fill_default_theme(Ref<Theme>& t,const Ref<Font> & default_font,const Ref<F
 	t->set_color("breakpoint_color","TextEdit", Color(0.8,0.8,0.4,0.2) );
 	t->set_color("current_line_color","TextEdit", Color(0.25,0.25,0.26,0.8) );
 	t->set_color("caret_color","TextEdit", control_font_color );
+	t->set_color("caret_background_color", "TextEdit", Color::html("000000"));
 	t->set_color("symbol_color","TextEdit", control_font_color_hover );
 	t->set_color("brace_mismatch_color","TextEdit", Color(1,0.2,0.2) );
 	t->set_color("line_number_color","TextEdit",Color::html("66aaaaaa"));

+ 3 - 0
tools/editor/editor_settings.cpp

@@ -554,6 +554,7 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
 	set("text_editor/create_signal_callbacks",true);
 	set("text_editor/autosave_interval_secs",0);
 
+	set("text_editor/block_caret", false);
 	set("text_editor/caret_blink", false);
 	set("text_editor/caret_blink_speed", 0.65);
 	hints["text_editor/caret_blink_speed"]=PropertyInfo(Variant::REAL,"text_editor/caret_blink_speed",PROPERTY_HINT_RANGE,"0.1, 10, 0.1");
@@ -685,6 +686,7 @@ void EditorSettings::_load_default_text_editor_theme() {
 	set("text_editor/completion_scroll_color", Color::html("ffffff"));
 	set("text_editor/completion_font_color", Color::html("aaaaaa"));
 	set("text_editor/caret_color",Color::html("aaaaaa"));
+	set("text_editor/caret_background_color", Color::html("000000"));
 	set("text_editor/line_number_color",Color::html("66aaaaaa"));
 	set("text_editor/text_color",Color::html("aaaaaa"));
 	set("text_editor/text_selected_color",Color::html("000000"));
@@ -922,6 +924,7 @@ bool EditorSettings::_save_text_editor_theme(String p_file) {
 	cf->set_value(theme_section, "completion_scroll_color", ((Color)get("text_editor/completion_scroll_color")).to_html());
 	cf->set_value(theme_section, "completion_font_color", ((Color)get("text_editor/completion_font_color")).to_html());
 	cf->set_value(theme_section, "caret_color", ((Color)get("text_editor/caret_color")).to_html());
+	cf->set_value(theme_section, "caret_background_color", ((Color)get("text_editor/caret_background_color")).to_html());
 	cf->set_value(theme_section, "line_number_color", ((Color)get("text_editor/line_number_color")).to_html());
 	cf->set_value(theme_section, "text_color", ((Color)get("text_editor/text_color")).to_html());
 	cf->set_value(theme_section, "text_selected_color", ((Color)get("text_editor/text_selected_color")).to_html());

+ 3 - 0
tools/editor/plugins/script_editor_plugin.cpp

@@ -317,6 +317,7 @@ void ScriptTextEditor::_load_theme_settings() {
 	get_text_edit()->add_color_override("font_color",EDITOR_DEF("text_editor/text_color",Color(0,0,0)));
 	get_text_edit()->add_color_override("line_number_color",EDITOR_DEF("text_editor/line_number_color",Color(0,0,0)));
 	get_text_edit()->add_color_override("caret_color",EDITOR_DEF("text_editor/caret_color",Color(0,0,0)));
+	get_text_edit()->add_color_override("caret_background_color",EDITOR_DEF("text_editor/caret_background_color",Color(0,0,0)));
 	get_text_edit()->add_color_override("font_selected_color",EDITOR_DEF("text_editor/text_selected_color",Color(1,1,1)));
 	get_text_edit()->add_color_override("selection_color",EDITOR_DEF("text_editor/selection_color",Color(0.2,0.2,1)));
 	get_text_edit()->add_color_override("brace_mismatch_color",EDITOR_DEF("text_editor/brace_mismatch_color",Color(1,0.2,0.2)));
@@ -2195,6 +2196,7 @@ void ScriptEditor::edit(const Ref<Script>& p_script) {
 	ste->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
 	ste->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
 	ste->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
+	ste->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
 	ste->get_text_edit()->set_callhint_settings(
 		EditorSettings::get_singleton()->get("text_editor/put_callhint_tooltip_below_current_line"),
 		EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset"));
@@ -2362,6 +2364,7 @@ void ScriptEditor::_editor_settings_changed() {
 		ste->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
 		ste->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
 		ste->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
+		ste->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
 	}
 
 	ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/auto_reload_and_parse_scripts_on_save",true));

+ 4 - 0
tools/editor/plugins/shader_editor_plugin.cpp

@@ -86,6 +86,7 @@ void ShaderTextEditor::_load_theme_settings() {
 	get_text_edit()->add_color_override("font_color",EDITOR_DEF("text_editor/text_color",Color(0,0,0)));
 	get_text_edit()->add_color_override("line_number_color",EDITOR_DEF("text_editor/line_number_color",Color(0,0,0)));
 	get_text_edit()->add_color_override("caret_color",EDITOR_DEF("text_editor/caret_color",Color(0,0,0)));
+	get_text_edit()->add_color_override("caret_background_color",EDITOR_DEF("text_editor/caret_background_color",Color(0,0,0)));
 	get_text_edit()->add_color_override("font_selected_color",EDITOR_DEF("text_editor/text_selected_color",Color(1,1,1)));
 	get_text_edit()->add_color_override("selection_color",EDITOR_DEF("text_editor/selection_color",Color(0.2,0.2,1)));
 	get_text_edit()->add_color_override("brace_mismatch_color",EDITOR_DEF("text_editor/brace_mismatch_color",Color(1,0.2,0.2)));
@@ -381,6 +382,7 @@ void ShaderEditor::_editor_settings_changed() {
 		vertex_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
 		vertex_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
 		vertex_editor->get_text_edit()->add_constant_override("line_spacing", EditorSettings::get_singleton()->get("text_editor/line_spacing"));
+		vertex_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
 
 		fragment_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
 		fragment_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
@@ -392,6 +394,7 @@ void ShaderEditor::_editor_settings_changed() {
 		fragment_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
 		fragment_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
 		fragment_editor->get_text_edit()->add_constant_override("line_spacing", EditorSettings::get_singleton()->get("text_editor/line_spacing"));
+		fragment_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
 
 		light_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
 		light_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
@@ -403,6 +406,7 @@ void ShaderEditor::_editor_settings_changed() {
 		light_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
 		light_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
 		light_editor->get_text_edit()->add_constant_override("line_spacing", EditorSettings::get_singleton()->get("text_editor/line_spacing"));
+		light_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
 }
 
 void ShaderEditor::_bind_methods() {