diff options
Diffstat (limited to 'scene/gui/line_edit.cpp')
-rw-r--r-- | scene/gui/line_edit.cpp | 165 |
1 files changed, 149 insertions, 16 deletions
diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 52ef57cf1c..6c47072b33 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -31,6 +31,10 @@ #include "os/os.h" #include "print_string.h" #include "label.h" +#include "translation.h" +#ifdef TOOLS_ENABLED +#include "tools/editor/editor_settings.h" +#endif static bool _is_text_char(CharType c) { @@ -57,6 +61,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); @@ -104,7 +109,7 @@ void LineEdit::_input_event(InputEvent p_event) { selection.doubleclick=false; if (OS::get_singleton()->has_virtual_keyboard()) - OS::get_singleton()->show_virtual_keyboard(get_text(),get_global_rect()); + OS::get_singleton()->show_virtual_keyboard(text,get_global_rect()); } update(); @@ -227,7 +232,7 @@ void LineEdit::_input_event(InputEvent p_event) { } } - + _reset_caret_blink_timer(); if (!k.mod.meta) { bool handled=true; @@ -294,6 +299,9 @@ void LineEdit::_input_event(InputEvent p_event) { } case KEY_LEFT: { +#ifndef APPLE_STYLE_KEYS + if (!k.mod.alt) +#endif shift_selection_check_pre(k.mod.shift); #ifdef APPLE_STYLE_KEYS @@ -543,14 +551,39 @@ 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)); + + if (!EditorSettings::get_singleton()->is_connected("settings_changed",this,"_editor_settings_changed")) { + 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(); @@ -604,14 +637,19 @@ void LineEdit::_notification(int p_what) { Color font_color_selected=get_color("font_color_selected"); Color cursor_color=get_color("cursor_color"); + const String& t = text.empty() ? placeholder : text; + // draw placeholder color + if(text.empty()) + font_color.a *= placeholder_alpha; + while(true) { //end of string, break! - if (char_ofs>=text.length()) + if (char_ofs>=t.length()) break; - CharType cchar=pass?'*':text[char_ofs]; - CharType next=pass?'*':text[char_ofs+1]; + CharType cchar=pass?'*':t[char_ofs]; + CharType next=pass?'*':t[char_ofs+1]; int char_width=font->get_char_size( cchar,next ).width; // end of widget, break! @@ -627,23 +665,28 @@ 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()); + OS::get_singleton()->show_virtual_keyboard(text,get_global_rect()); } break; case NOTIFICATION_FOCUS_EXIT: { @@ -788,6 +831,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; @@ -864,6 +946,29 @@ String LineEdit::get_text() const { return text; } +void LineEdit::set_placeholder(String p_text) { + + placeholder = XL_MESSAGE(p_text); + update(); +} + +String LineEdit::get_placeholder() const { + + return placeholder; +} + + +void LineEdit::set_placeholder_alpha(float p_alpha) { + + placeholder_alpha = p_alpha; + update(); +} + +float LineEdit::get_placeholder_alpha() const { + + return placeholder_alpha; +} + void LineEdit::set_cursor_pos(int p_pos) { if (p_pos>(int)text.length()) @@ -876,10 +981,6 @@ void LineEdit::set_cursor_pos(int p_pos) { cursor_pos=p_pos; -// if (cursor_pos>(window_pos+get_window_length())) { -// set_window_pos(cursor_pos-get_window_lengt//h()); -// } - if (!is_inside_tree()) { window_pos=cursor_pos; @@ -898,7 +999,6 @@ void LineEdit::set_cursor_pos(int p_pos) { if (window_width<0) return; - int width_to_cursor=0; int wp=window_pos; if (font.is_valid()) { @@ -1126,8 +1226,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); @@ -1136,8 +1249,16 @@ void LineEdit::_bind_methods() { ObjectTypeDB::bind_method(_MD("select_all"),&LineEdit::select_all); ObjectTypeDB::bind_method(_MD("set_text","text"),&LineEdit::set_text); ObjectTypeDB::bind_method(_MD("get_text"),&LineEdit::get_text); + ObjectTypeDB::bind_method(_MD("set_placeholder","text"),&LineEdit::set_placeholder); + ObjectTypeDB::bind_method(_MD("get_placeholder"),&LineEdit::get_placeholder); + ObjectTypeDB::bind_method(_MD("set_placeholder_alpha","alpha"),&LineEdit::set_placeholder_alpha); + ObjectTypeDB::bind_method(_MD("get_placeholder_alpha"),&LineEdit::get_placeholder_alpha); 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); @@ -1166,12 +1287,15 @@ void LineEdit::_bind_methods() { BIND_CONSTANT( MENU_MAX ); ADD_PROPERTYNZ( PropertyInfo( Variant::STRING, "text" ), _SCS("set_text"),_SCS("get_text") ); + ADD_PROPERTYNZ( PropertyInfo( Variant::STRING, "placeholder/text" ), _SCS("set_placeholder"),_SCS("get_placeholder") ); + ADD_PROPERTYNZ( PropertyInfo( Variant::REAL, "placeholder/alpha",PROPERTY_HINT_RANGE,"0,1,0.001" ), _SCS("set_placeholder_alpha"),_SCS("get_placeholder_alpha") ); ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), _SCS("set_align"), _SCS("get_align")); ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "max_length" ), _SCS("set_max_length"),_SCS("get_max_length") ); 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,8 +1304,10 @@ LineEdit::LineEdit() { cached_width = 0; cursor_pos=0; window_pos=0; + window_has_focus=true; max_length = 0; pass=false; + placeholder_alpha=0.6; selection_clear(); set_focus_mode( FOCUS_ALL ); @@ -1189,6 +1315,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); |