Merge pull request #4598 from Paulb23/caret_blink

Added caret blink to the text editor
This commit is contained in:
Rémi Verschelde 2016-05-11 09:33:12 +02:00
commit b9b423c1ee
5 changed files with 103 additions and 8 deletions

View file

@ -921,14 +921,18 @@ void TextEdit::_notification(int p_what) {
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();
VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,1)),cache.caret_color);
} else {
VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color);
}
if (draw_caret) {
if (insert_mode) {
VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,1)),cache.caret_color);
} else {
VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color);
}
}
}
char_ofs+=char_w;
@ -937,12 +941,18 @@ void TextEdit::_notification(int p_what) {
if (cursor.column==str.length() && cursor.line==line && (char_ofs+char_margin)>=xmargin_beg) {
cursor_pos=Point2i( char_ofs+char_margin, ofs_y );
if (insert_mode) {
cursor_pos.y += get_row_height();
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);
} else {
VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color);
}
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);
} else {
VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color);
}
}
}
}
@ -1390,6 +1400,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
}
if (mb.button_index==BUTTON_LEFT) {
_reset_caret_blink_timer();
int row,col;
_get_mouse_pos(Point2i(mb.x,mb.y), row,col);
@ -1524,6 +1536,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (selection.selecting_mode!=Selection::MODE_NONE) {
_reset_caret_blink_timer();
int row,col;
_get_mouse_pos(Point2i(mm.x,mm.y), row,col);
@ -1644,6 +1658,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (k.scancode==KEY_BACKSPACE) {
_reset_caret_blink_timer();
backspace_at_cursor();
_update_completion_candidates();
accept_event();
@ -1659,6 +1675,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (k.unicode>32) {
_reset_caret_blink_timer();
const CharType chr[2] = {(CharType)k.unicode, 0};
if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
_consume_pair_symbol(chr[0]);
@ -1705,6 +1723,9 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
k.mod.shift=false;
}
if (!k.mod.command) {
_reset_caret_blink_timer();
}
// save here for insert mode, just in case it is cleared in the following section
bool had_selection = selection.active;
@ -2902,6 +2923,30 @@ int TextEdit::cursor_get_line() const {
return cursor.line;
}
bool TextEdit::cursor_get_blink_enabled() const {
return caret_blink_enabled;
}
void TextEdit::cursor_set_blink_enabled(const bool p_enabled) {
caret_blink_enabled = p_enabled;
if (p_enabled) {
caret_blink_timer->start();
} else {
caret_blink_timer->stop();
}
draw_caret = true;
}
float TextEdit::cursor_get_blink_speed() const {
return caret_blink_timer->get_wait_time();
}
void TextEdit::cursor_set_blink_speed(const float p_speed) {
ERR_FAIL_COND(p_speed <= 0);
caret_blink_timer->set_wait_time(p_speed);
}
void TextEdit::_scroll_moved(double p_to_val) {
@ -3117,6 +3162,20 @@ void TextEdit::set_max_chars(int p_max_chars) {
max_chars=p_max_chars;
}
void TextEdit::_reset_caret_blink_timer() {
if (caret_blink_enabled) {
caret_blink_timer->stop();
caret_blink_timer->start();
draw_caret = true;
update();
}
}
void TextEdit::_toggle_draw_caret() {
draw_caret = !draw_caret;
update();
}
void TextEdit::_update_caches() {
cache.style_normal=get_stylebox("normal");
@ -4096,6 +4155,7 @@ void TextEdit::_bind_methods() {
ObjectTypeDB::bind_method(_MD("_text_changed_emit"),&TextEdit::_text_changed_emit);
ObjectTypeDB::bind_method(_MD("_push_current_op"),&TextEdit::_push_current_op);
ObjectTypeDB::bind_method(_MD("_click_selection_held"),&TextEdit::_click_selection_held);
ObjectTypeDB::bind_method(_MD("_toggle_draw_caret"),&TextEdit::_toggle_draw_caret);
BIND_CONSTANT( SEARCH_MATCH_CASE );
BIND_CONSTANT( SEARCH_WHOLE_WORDS );
@ -4165,6 +4225,7 @@ TextEdit::TextEdit() {
readonly=false;
setting_row=false;
draw_tabs=false;
draw_caret=true;
max_chars=0;
clear();
wrap=false;
@ -4204,6 +4265,13 @@ TextEdit::TextEdit() {
selection.active=false;
syntax_coloring=false;
caret_blink_enabled=false;
caret_blink_timer = memnew(Timer);
add_child(caret_blink_timer);
caret_blink_timer->set_wait_time(0.65);
caret_blink_timer->connect("timeout", this,"_toggle_draw_caret");
cursor_set_blink_enabled(false);
custom_bg_color=Color(0,0,0,0);
idle_detect = memnew( Timer );
add_child(idle_detect);

View file

@ -210,6 +210,10 @@ class TextEdit : public Control {
bool syntax_coloring;
int tab_size;
Timer *caret_blink_timer;
bool caret_blink_enabled;
bool draw_caret;
bool setting_row;
bool wrap;
bool draw_tabs;
@ -267,6 +271,9 @@ class TextEdit : public Control {
int get_row_height() const;
void _reset_caret_blink_timer();
void _toggle_draw_caret();
void _update_caches();
void _cursor_changed_emit();
void _text_changed_emit();
@ -364,6 +371,12 @@ public:
int cursor_get_column() const;
int cursor_get_line() const;
bool cursor_get_blink_enabled() const;
void cursor_set_blink_enabled(const bool p_enabled);
float cursor_get_blink_speed() const;
void cursor_set_blink_speed(const float p_speed);
void set_readonly(bool p_readonly);
void set_max_chars(int p_max_chars);

View file

@ -426,6 +426,10 @@ 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/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");
set("text_editor/font","");
hints["text_editor/font"]=PropertyInfo(Variant::STRING,"text_editor/font",PROPERTY_HINT_GLOBAL_FILE,"*.fnt");
set("text_editor/auto_brace_complete", false);

View file

@ -1967,6 +1967,8 @@ void ScriptEditor::edit(const Ref<Script>& p_script) {
ste->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
ste->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
ste->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
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_callhint_settings(
EditorSettings::get_singleton()->get("text_editor/put_callhint_tooltip_below_current_line"),
EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset"));
@ -2114,6 +2116,8 @@ void ScriptEditor::_editor_settings_changed() {
ste->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
ste->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
ste->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
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"));
}
}

View file

@ -368,6 +368,8 @@ void ShaderEditor::_editor_settings_changed() {
vertex_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
vertex_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
vertex_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
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"));
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"));
@ -376,6 +378,8 @@ void ShaderEditor::_editor_settings_changed() {
fragment_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
fragment_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
fragment_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
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"));
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"));
@ -384,6 +388,8 @@ void ShaderEditor::_editor_settings_changed() {
light_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
light_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
light_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
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"));
}
void ShaderEditor::_bind_methods() {