diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2016-06-24 21:13:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-24 21:13:26 +0200 |
commit | 118f54aeeb3c2f67e721d261edf4f96dce7cf0d4 (patch) | |
tree | 652962af8406a411d38c11c371bb316d19a17e9c | |
parent | 7c4221f62aba064718301e0de69c0edc3bb1ae3b (diff) | |
parent | 4f54e721895d44ded7b49fe6d6138a97d56d456b (diff) |
Merge pull request #5328 from Paulb23/line_edit_caret
Added caret blink to line edit
-rw-r--r-- | doc/base/classes.xml | 28 | ||||
-rw-r--r-- | scene/gui/line_edit.cpp | 107 | ||||
-rw-r--r-- | scene/gui/line_edit.h | 18 |
3 files changed, 147 insertions, 6 deletions
diff --git a/doc/base/classes.xml b/doc/base/classes.xml index 808bf20c25..7224ae8554 100644 --- a/doc/base/classes.xml +++ b/doc/base/classes.xml @@ -18959,6 +18959,34 @@ Example: (content-length:12), (Content-Type:application/json; charset=UTF-8) Set the cursor position inside the [LineEdit], causing it to scroll if needed. </description> </method> + <method name="cursor_set_blink_enabled"> + <argument index="0" name="enable" type="bool"> + </argument> + <description> + Set the line edit caret to blink. + </description> + </method> + <method name="cursor_get_blink_enabled" qualifiers="const"> + <return type="float"> + </return> + <description> + Gets whether the line edit caret is blinking. + </description> + </method> + <method name="cursor_set_blink_speed"> + <argument index="0" name="blink_speed" type="float"> + </argument> + <description> + Set the line edit caret blink speed. Cannot be less then or equal to 0. + </description> + </method> + <method name="cursor_get_blink_speed" qualifiers="const"> + <return type="float"> + </return> + <description> + Gets the line edit caret blink speed. + </description> + </method> <method name="set_editable"> <argument index="0" name="enabled" type="bool"> </argument> diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 52ef57cf1c..fc5e82d36a 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -31,6 +31,9 @@ #include "os/os.h" #include "print_string.h" #include "label.h" +#ifdef TOOLS_ENABLED +#include "tools/editor/editor_settings.h" +#endif static bool _is_text_char(CharType c) { @@ -57,6 +60,7 @@ void LineEdit::_input_event(InputEvent p_event) { if (b.button_index!=BUTTON_LEFT) break; + _reset_caret_blink_timer(); if (b.pressed) { shift_selection_check_pre(b.mod.shift); @@ -227,7 +231,7 @@ void LineEdit::_input_event(InputEvent p_event) { } } - + _reset_caret_blink_timer(); if (!k.mod.meta) { bool handled=true; @@ -543,14 +547,37 @@ void LineEdit::drop_data(const Point2& p_point,const Variant& p_data){ void LineEdit::_notification(int p_what) { switch(p_what) { +#ifdef TOOLS_ENABLED + case NOTIFICATION_ENTER_TREE: { + if (get_tree()->is_editor_hint()) { + cursor_set_blink_enabled(EDITOR_DEF("text_editor/caret_blink", false)); + cursor_set_blink_speed(EDITOR_DEF("text_editor/caret_blink_speed", 0.65)); + EditorSettings::get_singleton()->connect("settings_changed",this,"_editor_settings_changed"); + } + } break; +#endif case NOTIFICATION_RESIZED: { set_cursor_pos( get_cursor_pos() ); } break; + case MainLoop::NOTIFICATION_WM_FOCUS_IN: { + window_has_focus = true; + draw_caret = true; + update(); + } break; + case MainLoop::NOTIFICATION_WM_FOCUS_OUT: { + window_has_focus = false; + draw_caret = false; + update(); + } break; case NOTIFICATION_DRAW: { + if ((!has_focus() && !menu->has_focus()) || !window_has_focus) { + draw_caret = false; + } + int width,height; Size2 size=get_size(); @@ -627,21 +654,26 @@ void LineEdit::_notification(int p_what) { font->draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, selected ? font_color_selected : font_color); - if (char_ofs==cursor_pos && has_focus()) + if (char_ofs==cursor_pos && draw_caret) { VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2( Point2( x_ofs , y_ofs ), Size2( 1, y_area ) ), cursor_color ); + } x_ofs+=char_width; char_ofs++; } - if (char_ofs==cursor_pos && has_focus()) //may be at the end + if (char_ofs==cursor_pos && draw_caret) {//may be at the end VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2( Point2( x_ofs , y_ofs ), Size2( 1, y_area ) ), cursor_color ); - + } } break; case NOTIFICATION_FOCUS_ENTER: { + if (!caret_blink_enabled) { + draw_caret = true; + } + if (OS::get_singleton()->has_virtual_keyboard()) OS::get_singleton()->show_virtual_keyboard(get_text(),get_global_rect()); @@ -788,6 +820,45 @@ void LineEdit::set_cursor_at_pixel_pos(int p_x) { } +bool LineEdit::cursor_get_blink_enabled() const { + return caret_blink_enabled; +} + +void LineEdit::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 LineEdit::cursor_get_blink_speed() const { + return caret_blink_timer->get_wait_time(); +} + +void LineEdit::cursor_set_blink_speed(const float p_speed) { + ERR_FAIL_COND(p_speed <= 0); + caret_blink_timer->set_wait_time(p_speed); +} + +void LineEdit::_reset_caret_blink_timer() { + if (caret_blink_enabled) { + caret_blink_timer->stop(); + caret_blink_timer->start(); + draw_caret = true; + update(); + } + } + +void LineEdit::_toggle_draw_caret() { + draw_caret = !draw_caret; + if (is_visible()) { + update(); + } +} + void LineEdit::delete_char() { if ((text.length()<=0) || (cursor_pos==0)) return; @@ -1126,8 +1197,21 @@ PopupMenu *LineEdit::get_menu() const { return menu; } +#ifdef TOOLS_ENABLED + void LineEdit::_editor_settings_changed() { + cursor_set_blink_enabled(EDITOR_DEF("text_editor/caret_blink", false)); + cursor_set_blink_speed(EDITOR_DEF("text_editor/caret_blink_speed", 0.65)); + } +#endif + void LineEdit::_bind_methods() { + ObjectTypeDB::bind_method(_MD("_toggle_draw_caret"),&LineEdit::_toggle_draw_caret); + +#ifdef TOOLS_ENABLED + ObjectTypeDB::bind_method("_editor_settings_changed",&LineEdit::_editor_settings_changed); +#endif + ObjectTypeDB::bind_method(_MD("set_align", "align"), &LineEdit::set_align); ObjectTypeDB::bind_method(_MD("get_align"), &LineEdit::get_align); @@ -1138,6 +1222,10 @@ void LineEdit::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_text"),&LineEdit::get_text); ObjectTypeDB::bind_method(_MD("set_cursor_pos","pos"),&LineEdit::set_cursor_pos); ObjectTypeDB::bind_method(_MD("get_cursor_pos"),&LineEdit::get_cursor_pos); + ObjectTypeDB::bind_method(_MD("cursor_set_blink_enabled", "enable"),&LineEdit::cursor_set_blink_enabled); + ObjectTypeDB::bind_method(_MD("cursor_get_blink_enabled"),&LineEdit::cursor_get_blink_enabled); + ObjectTypeDB::bind_method(_MD("cursor_set_blink_speed", "blink_speed"),&LineEdit::cursor_set_blink_speed); + ObjectTypeDB::bind_method(_MD("cursor_get_blink_speed"),&LineEdit::cursor_get_blink_speed); ObjectTypeDB::bind_method(_MD("set_max_length","chars"),&LineEdit::set_max_length); ObjectTypeDB::bind_method(_MD("get_max_length"),&LineEdit::get_max_length); ObjectTypeDB::bind_method(_MD("append_at_cursor","text"),&LineEdit::append_at_cursor); @@ -1171,7 +1259,8 @@ void LineEdit::_bind_methods() { ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "editable" ), _SCS("set_editable"),_SCS("is_editable") ); ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "secret" ), _SCS("set_secret"),_SCS("is_secret") ); ADD_PROPERTY( PropertyInfo( Variant::INT,"focus_mode", PROPERTY_HINT_ENUM, "None,Click,All" ), _SCS("set_focus_mode"), _SCS("get_focus_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") ); } LineEdit::LineEdit() { @@ -1180,6 +1269,7 @@ LineEdit::LineEdit() { cached_width = 0; cursor_pos=0; window_pos=0; + window_has_focus=true; max_length = 0; pass=false; @@ -1189,6 +1279,13 @@ LineEdit::LineEdit() { set_default_cursor_shape(CURSOR_IBEAM); set_stop_mouse(true); + draw_caret=true; + 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); menu = memnew( PopupMenu ); add_child(menu); diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h index ce3958db02..e4da0f0b87 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -87,6 +87,11 @@ private: bool drag_attempt; } selection; + Timer *caret_blink_timer; + bool caret_blink_enabled; + bool draw_caret; + bool window_has_focus; + void shift_selection_check_pre(bool); void shift_selection_check_post(bool); @@ -97,10 +102,15 @@ private: void set_cursor_at_pixel_pos(int p_x); + void _reset_caret_blink_timer(); + void _toggle_draw_caret(); + void clear_internal(); void changed_internal(); - +#ifdef TOOLS_ENABLED + void _editor_settings_changed(); +#endif void _input_event(InputEvent p_event); void _notification(int p_what); @@ -132,6 +142,12 @@ public: void append_at_cursor(String p_text); void clear(); + 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 copy_text(); void cut_text(); void paste_text(); |