void GDTokenizerText::_advance() { if (error_flag) { //parser broke _make_error(last_error); return; } if (code_pos>=len) { _make_token(TK_EOF); return; } #define GETCHAR(m_ofs) ((m_ofs+code_pos)>=len?0:_code[m_ofs+code_pos]) #define INCPOS(m_amount) { code_pos+=m_amount; column+=m_amount; } while (true) { bool is_node_path = false; StringMode string_mode=STRING_DOUBLE_QUOTE; switch(GETCHAR(0)) { case 0: _make_token(TK_EOF); break; case '\\': INCPOS(1); if (GETCHAR(0)=='\r') { INCPOS(1); } if (GETCHAR(0)!='\n') { _make_error("Expected newline after '\\'."); return; } INCPOS(1); while(GETCHAR(0)==' ' || GETCHAR(0)=='\t') { INCPOS(1); } continue; case '\t': case '\r': case ' ': INCPOS(1); continue; case '\n': { line++; INCPOS(1); column=0; int i=0; while(GETCHAR(i)==' ' || GETCHAR(i)=='\t') { i++; } _make_newline(i); return; } #if 1 //py style tokenizer case '#': { // line comment skip while(GETCHAR(0)!='\n') { code_pos++; if (GETCHAR(0)==0) { //end of file //_make_error("Unterminated Comment"); _make_token(TK_EOF); return; } } INCPOS(1); column=0; line++; int i=0; while(GETCHAR(i)==' ' || GETCHAR(i)=='\t') { i++; } _make_newline(i); return; } break; #endif case '/': { switch(GETCHAR(1)) { #if 0 // c style tokenizer case '*': { // block comment int pos = code_pos+2; int new_line=line; int new_col=column+2; while(true) { if (_code[pos]=='0') { _make_error("Unterminated Comment"); code_pos=pos; return; } if (_code[pos]=='*' && _code[pos+1]=='/') { new_col+=2; pos+=2; //compensate break; } else if (_code[pos]=='\n') { new_line++; new_col=0; } else { new_col++; } pos++; } column=new_col; line=new_line; code_pos=pos; continue; } break; case '/': { // line comment skip while(GETCHAR(0)!='\n') { code_pos++; if (GETCHAR(0)==0) { //end of file _make_error("Unterminated Comment"); return; } } INCPOS(1); column=0; line++; continue; } break; #endif case '=': { // diveq _make_token(TK_OP_ASSIGN_DIV); INCPOS(1); } break; default: _make_token(TK_OP_DIV); } } break; case '=': { if (GETCHAR(1)=='=') { _make_token(TK_OP_EQUAL); INCPOS(1); } else _make_token(TK_OP_ASSIGN); } break; case '<': { if (GETCHAR(1)=='=') { _make_token(TK_OP_LESS_EQUAL); INCPOS(1); } else if (GETCHAR(1)=='<') { if (GETCHAR(2)=='=') { _make_token(TK_OP_ASSIGN_SHIFT_LEFT); INCPOS(1); } else { _make_token(TK_OP_SHIFT_LEFT); } INCPOS(1); } else _make_token(TK_OP_LESS); } break; case '>': { if (GETCHAR(1)=='=') { _make_token(TK_OP_GREATER_EQUAL); INCPOS(1); } else if (GETCHAR(1)=='>') { if (GETCHAR(2)=='=') { _make_token(TK_OP_ASSIGN_SHIFT_RIGHT); INCPOS(1); } else { _make_token(TK_OP_SHIFT_RIGHT); } INCPOS(1); } else { _make_token(TK_OP_GREATER); } } break; case '!': { if (GETCHAR(1)=='=') { _make_token(TK_OP_NOT_EQUAL); INCPOS(1); } else { _make_token(TK_OP_NOT); } } break; //case '"' //string - no strings in shader //case '\'' //string - no strings in shader case '{': _make_token(TK_CURLY_BRACKET_OPEN); break; case '}': _make_token(TK_CURLY_BRACKET_CLOSE); break; case '[': _make_token(TK_BRACKET_OPEN); break; case ']': _make_token(TK_BRACKET_CLOSE); break; case '(': _make_token(TK_PARENTHESIS_OPEN); break; case ')': _make_token(TK_PARENTHESIS_CLOSE); break; case ',': _make_token(TK_COMMA); break; case ';': _make_token(TK_SEMICOLON); break; case '?': _make_token(TK_QUESTION_MARK); break; case ':': _make_token(TK_COLON); //for methods maybe but now useless. break; case '^': { if (GETCHAR(1)=='=') { _make_token(TK_OP_ASSIGN_BIT_XOR); INCPOS(1); } else { _make_token(TK_OP_BIT_XOR); } } break; case '~': _make_token(TK_OP_BIT_INVERT); break; case '&': { if (GETCHAR(1)=='&') { _make_token(TK_OP_AND); INCPOS(1); } else if (GETCHAR(1)=='=') { _make_token(TK_OP_ASSIGN_BIT_AND); INCPOS(1); } else { _make_token(TK_OP_BIT_AND); } } break; case '|': { if (GETCHAR(1)=='|') { _make_token(TK_OP_OR); INCPOS(1); } else if (GETCHAR(1)=='=') { _make_token(TK_OP_ASSIGN_BIT_OR); INCPOS(1); } else { _make_token(TK_OP_BIT_OR); } } break; case '*': { if (GETCHAR(1)=='=') { _make_token(TK_OP_ASSIGN_MUL); INCPOS(1); } else { _make_token(TK_OP_MUL); } } break; case '+': { if (GETCHAR(1)=='=') { _make_token(TK_OP_ASSIGN_ADD); INCPOS(1); //} else if (GETCHAR(1)=='+') { // _make_token(TK_OP_PLUS_PLUS); // INCPOS(1); } else { _make_token(TK_OP_ADD); } } break; case '-': { if (GETCHAR(1)=='=') { _make_token(TK_OP_ASSIGN_SUB); INCPOS(1); //} else if (GETCHAR(1)=='-') { // _make_token(TK_OP_MINUS_MINUS); // INCPOS(1); } else { _make_token(TK_OP_SUB); } } break; case '%': { if (GETCHAR(1)=='=') { _make_token(TK_OP_ASSIGN_MOD); INCPOS(1); } else { _make_token(TK_OP_MOD); } } break; case '@': if( CharType(GETCHAR(1))!='"' && CharType(GETCHAR(1))!='\'' ) { _make_error("Unexpected '@'"); return; } INCPOS(1); is_node_path=true; case '\'': case '"': { if (GETCHAR(0)=='\'') string_mode=STRING_SINGLE_QUOTE; int i=1; if (string_mode==STRING_DOUBLE_QUOTE && GETCHAR(i)=='"' && GETCHAR(i+1)=='"') { i+=2; string_mode=STRING_MULTILINE; } String str; while(true) { if (CharType(GETCHAR(i))==0) { _make_error("Unterminated String"); return; } else if( string_mode==STRING_DOUBLE_QUOTE && CharType(GETCHAR(i))=='"' ) { break; } else if( string_mode==STRING_SINGLE_QUOTE && CharType(GETCHAR(i))=='\'' ) { break; } else if( string_mode==STRING_MULTILINE && CharType(GETCHAR(i))=='\"' && CharType(GETCHAR(i+1))=='\"' && CharType(GETCHAR(i+2))=='\"') { i+=2; break; } else if( string_mode!=STRING_MULTILINE && CharType(GETCHAR(i))=='\n') { _make_error("Unexpected EOL at String."); return; } else if (CharType(GETCHAR(i))=='\\') { //escaped characters... i++; CharType next = GETCHAR(i); if (next==0) { _make_error("Unterminated String"); return; } CharType res=0; switch(next) { case 'a': res=7; break; case 'b': res=8; break; case 't': res=9; break; case 'n': res=10; break; case 'v': res=11; break; case 'f': res=12; break; case 'r': res=13; break; case '\'': res='\''; break; case '\"': res='\"'; break; case '\\': res='\\'; break; case '/': res='/'; break; //wtf case 'u': { //hexnumbarh - oct is deprecated i+=1; for(int j=0;j<4;j++) { CharType c = GETCHAR(i+j); if (c==0) { _make_error("Unterminated String"); return; } if (!((c>='0' && c<='9') || (c>='a' && c<='f') || (c>='A' && c<='F'))) { _make_error("Malformed hex constant in string"); return; } CharType v; if (c>='0' && c<='9') { v=c-'0'; } else if (c>='a' && c<='f') { v=c-'a'; v+=10; } else if (c>='A' && c<='F') { v=c-'A'; v+=10; } else { ERR_PRINT("BUG"); v=0; } res<<=4; res|=v; } i+=3; } break; default: { _make_error("Invalid escape sequence"); return; } break; } str+=res; } else { str+=CharType(GETCHAR(i)); } i++; } INCPOS(i); if (is_node_path) { _make_constant(NodePath(str)); } else { _make_constant(str); } } break; case 0xFFFF: { _make_token(TK_CURSOR); } break; default: { if (_is_number(GETCHAR(0)) || (GETCHAR(0)=='.' && _is_number(GETCHAR(1)))) { // parse number bool period_found=false; bool exponent_found=false; bool hexa_found=false; bool sign_found=false; String str; int i=0; while(true) { if (GETCHAR(i)=='.') { if (period_found || exponent_found) { _make_error("Invalid numeric constant at '.'"); return; } period_found=true; } else if (GETCHAR(i)=='x') { if (hexa_found || str.length()!=1 || !( (i==1 && str[0]=='0') || (i==2 && str[1]=='0' && str[0]=='-') ) ) { _make_error("Invalid numeric constant at 'x'"); return; } hexa_found=true; } else if (!hexa_found && GETCHAR(i)=='e') { if (hexa_found || exponent_found) { _make_error("Invalid numeric constant at 'e'"); return; } exponent_found=true; } else if (_is_number(GETCHAR(i))) { //all ok } else if (hexa_found && _is_hex(GETCHAR(i))) { } else if ((GETCHAR(i)=='-' || GETCHAR(i)=='+') && exponent_found) { if (sign_found) { _make_error("Invalid numeric constant at '-'"); return; } sign_found=true; } else break; str+=CharType(GETCHAR(i)); i++; } if (!( _is_number(str[str.length()-1]) || (hexa_found && _is_hex(str[str.length()-1])))) { _make_error("Invalid numeric constant: "+str); return; } INCPOS(str.length()); if (hexa_found) { int val = str.hex_to_int(); _make_constant(val); } else if (period_found) { real_t val = str.to_double(); //print_line("*%*%*%*% to convert: "+str+" result: "+rtos(val)); _make_constant(val); } else { int val = str.to_int(); _make_constant(val); } return; } if (GETCHAR(0)=='.') { //parse period _make_token(TK_PERIOD); break; } if (_is_text_char(GETCHAR(0))) { // parse identifier String str; str+=CharType(GETCHAR(0)); int i=1; while(_is_text_char(GETCHAR(i))) { str+=CharType(GETCHAR(i)); i++; } bool identifier=false; if (str=="null") { _make_constant(Variant()); } else if (str=="true") { _make_constant(true); } else if (str=="false") { _make_constant(false); } else { bool found=false; struct _bit { Variant::Type type; const char *text;}; //built in types static const _bit type_list[]={ //types {Variant::BOOL,"bool"}, {Variant::INT,"int"}, {Variant::REAL,"float"}, {Variant::STRING,"String"}, {Variant::VECTOR2,"vec2"}, {Variant::VECTOR2,"Vector2"}, {Variant::RECT2,"Rect2"}, {Variant::MATRIX32,"Matrix32"}, {Variant::MATRIX32,"mat32"}, {Variant::VECTOR3,"vec3"}, {Variant::VECTOR3,"Vector3"}, {Variant::_AABB,"AABB"}, {Variant::_AABB,"Rect3"}, {Variant::PLANE,"Plane"}, {Variant::QUAT,"Quat"}, {Variant::MATRIX3,"mat3"}, {Variant::MATRIX3,"Matrix3"}, {Variant::TRANSFORM,"trn"}, {Variant::TRANSFORM,"Transform"}, {Variant::COLOR,"Color"}, {Variant::IMAGE,"Image"}, {Variant::_RID,"RID"}, {Variant::OBJECT,"Object"}, {Variant::INPUT_EVENT,"InputEvent"}, {Variant::NODE_PATH,"NodePath"}, {Variant::DICTIONARY,"dict"}, {Variant::DICTIONARY,"Dictionary"}, {Variant::ARRAY,"Array"}, {Variant::RAW_ARRAY,"RawArray"}, {Variant::INT_ARRAY,"IntArray"}, {Variant::REAL_ARRAY,"FloatArray"}, {Variant::STRING_ARRAY,"StringArray"}, {Variant::VECTOR2_ARRAY,"Vector2Array"}, {Variant::VECTOR3_ARRAY,"Vector3Array"}, {Variant::COLOR_ARRAY,"ColorArray"}, {Variant::VARIANT_MAX,NULL}, }; { int idx=0; while(type_list[idx].text) { if (str==type_list[idx].text) { _make_type(type_list[idx].type); found=true; break; } idx++; } } if (!found) { //built in func? for(int i=0;i<GDFunctions::FUNC_MAX;i++) { if (str==GDFunctions::get_func_name(GDFunctions::Function(i))) { _make_built_in_func(GDFunctions::Function(i)); found=true; break; } } //keywor } if (!found) { struct _kws { Token token; const char *text;}; static const _kws keyword_list[]={ //ops {TK_OP_IN,"in"}, {TK_OP_NOT,"not"}, {TK_OP_OR,"or"}, {TK_OP_AND,"and"}, //func {TK_PR_FUNCTION,"func"}, {TK_PR_FUNCTION,"function"}, {TK_PR_CLASS,"class"}, {TK_PR_EXTENDS,"extends"}, {TK_PR_TOOL,"tool"}, {TK_PR_STATIC,"static"}, {TK_PR_EXPORT,"export"}, {TK_PR_SETGET,"setget"}, {TK_PR_VAR,"var"}, {TK_PR_PRELOAD,"preload"}, {TK_PR_ASSERT,"assert"}, {TK_PR_YIELD,"yield"}, {TK_PR_CONST,"const"}, //controlflow {TK_CF_IF,"if"}, {TK_CF_ELIF,"elif"}, {TK_CF_ELSE,"else"}, {TK_CF_FOR,"for"}, {TK_CF_WHILE,"while"}, {TK_CF_DO,"do"}, {TK_CF_SWITCH,"switch"}, {TK_CF_BREAK,"break"}, {TK_CF_CONTINUE,"continue"}, {TK_CF_RETURN,"return"}, {TK_CF_PASS,"pass"}, {TK_SELF,"self"}, {TK_ERROR,NULL} }; int idx=0; found=false; while(keyword_list[idx].text) { if (str==keyword_list[idx].text) { _make_token(keyword_list[idx].token); found=true; break; } idx++; } } if (!found) identifier=true; } if (identifier) { _make_identifier(str); } INCPOS(str.length()); return; } _make_error("Unknown character"); return; } break; } INCPOS(1); break; } }
Ref<Texture> EditorScriptPreviewPlugin::generate(const RES& p_from) { Ref<Script> scr = p_from; if (scr.is_null()) return Ref<Texture>(); String code = scr->get_source_code().strip_edges(); if (code=="") return Ref<Texture>(); List<String> kwors; scr->get_language()->get_reserved_words(&kwors); Set<String> keywords; for(List<String>::Element *E=kwors.front();E;E=E->next()) { keywords.insert(E->get()); } int line = 0; int col=0; int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); thumbnail_size*=EDSCALE; Image img(thumbnail_size,thumbnail_size,0,Image::FORMAT_RGBA8); Color bg_color = EditorSettings::get_singleton()->get("text_editor/highlighting/background_color"); bg_color.a=1.0; Color keyword_color = EditorSettings::get_singleton()->get("text_editor/highlighting/keyword_color"); Color text_color = EditorSettings::get_singleton()->get("text_editor/highlighting/text_color"); Color symbol_color = EditorSettings::get_singleton()->get("text_editor/highlighting/symbol_color"); for(int i=0;i<thumbnail_size;i++) { for(int j=0;j<thumbnail_size;j++) { img.put_pixel(i,j,bg_color); } } bool prev_is_text=false; bool in_keyword=false; for(int i=0;i<code.length();i++) { CharType c = code[i]; if (c>32) { if (col<thumbnail_size) { Color color = text_color; if (c!='_' && ((c>='!' && c<='/') || (c>=':' && c<='@') || (c>='[' && c<='`') || (c>='{' && c<='~') || c=='\t')) { //make symbol a little visible color=symbol_color; in_keyword=false; } else if (!prev_is_text && _is_text_char(c)) { int pos = i; while(_is_text_char(code[pos])) { pos++; } ///print_line("from "+itos(i)+" to "+itos(pos)); String word = code.substr(i,pos-i); //print_line("found word: "+word); if (keywords.has(word)) in_keyword=true; } else if (!_is_text_char(c)) { in_keyword=false; } if (in_keyword) color=keyword_color; Color ul=color; ul.a*=0.5; img.put_pixel(col,line*2,bg_color.blend(ul)); img.put_pixel(col,line*2+1,color); prev_is_text=_is_text_char(c); } } else { prev_is_text=false; in_keyword=false; if (c=='\n') { col=0; line++; if (line>=thumbnail_size/2) break; } else if (c=='\t') { col+=3; } } col++; } Ref<ImageTexture> ptex = Ref<ImageTexture>( memnew( ImageTexture)); ptex->create_from_image(img,0); return ptex; }
void LineEdit::_input_event(InputEvent p_event) { switch(p_event.type) { case InputEvent::MOUSE_BUTTON: { const InputEventMouseButton &b = p_event.mouse_button; if (b.pressed && b.button_index==BUTTON_RIGHT) { menu->set_pos(get_global_transform().xform(get_local_mouse_pos())); menu->set_size(Vector2(1,1)); menu->popup(); grab_focus(); return; } if (b.button_index!=BUTTON_LEFT) break; _reset_caret_blink_timer(); if (b.pressed) { shift_selection_check_pre(b.mod.shift); set_cursor_at_pixel_pos(b.x); if (b.mod.shift) { selection_fill_at_cursor(); selection.creating=true; } else { if (b.doubleclick) { selection.enabled=true; selection.begin=0; selection.end=text.length(); selection.doubleclick=true; } selection.drag_attempt=false; if ((cursor_pos<selection.begin) || (cursor_pos>selection.end) || !selection.enabled) { selection_clear(); selection.cursor_start=cursor_pos; selection.creating=true; } else if (selection.enabled) { selection.drag_attempt=true; } } // if (!editable) // non_editable_clicked_signal.call(); update(); } else { if ( (!selection.creating) && (!selection.doubleclick)) { selection_clear(); } selection.creating=false; selection.doubleclick=false; if (OS::get_singleton()->has_virtual_keyboard()) OS::get_singleton()->show_virtual_keyboard(text,get_global_rect()); } update(); } break; case InputEvent::MOUSE_MOTION: { const InputEventMouseMotion& m=p_event.mouse_motion; if (m.button_mask&BUTTON_LEFT) { if (selection.creating) { set_cursor_at_pixel_pos(m.x); selection_fill_at_cursor(); } } } break; case InputEvent::KEY: { const InputEventKey &k =p_event.key; if (!k.pressed) return; unsigned int code = k.scancode; if (k.mod.command) { bool handled=true; switch (code) { case (KEY_X): { // CUT if(editable) { cut_text(); } } break; case (KEY_C): { // COPY copy_text(); } break; case (KEY_V): { // PASTE if(editable) { paste_text(); } } break; case (KEY_Z): { // Simple One level undo if(editable) { undo(); } } break; case (KEY_U): { // Delete from start to cursor if(editable) { selection_clear(); undo_text = text; text = text.substr(cursor_pos,text.length()-cursor_pos); Ref<Font> font = get_font("font"); cached_width = 0; if (font != NULL) { for (int i = 0; i < text.length(); i++) cached_width += font->get_char_size(text[i]).width; } set_cursor_pos(0); _text_changed(); } } break; case (KEY_Y): { // PASTE (Yank for unix users) if(editable) { paste_text(); } } break; case (KEY_K): { // Delete from cursor_pos to end if(editable) { selection_clear(); undo_text = text; text = text.substr(0,cursor_pos); _text_changed(); } } break; case (KEY_A): { //Select All select(); } break; default: { handled=false; } } if (handled) { accept_event(); return; } } _reset_caret_blink_timer(); if (!k.mod.meta) { bool handled=true; switch (code) { case KEY_ENTER: case KEY_RETURN: { emit_signal( "text_entered",text ); if (OS::get_singleton()->has_virtual_keyboard()) OS::get_singleton()->hide_virtual_keyboard(); return; } break; case KEY_BACKSPACE: { if (!editable) break; if (selection.enabled) { undo_text=text; selection_delete(); break; } #ifdef APPLE_STYLE_KEYS if (k.mod.alt) { #else if (k.mod.alt) { handled=false; break; } else if (k.mod.command) { #endif int cc=cursor_pos; bool prev_char=false; while (cc>0) { bool ischar=_is_text_char(text[cc-1]); if (prev_char && !ischar) break; prev_char=ischar; cc--; } delete_text(cc, cursor_pos); set_cursor_pos(cc); } else { undo_text=text; delete_char(); } } break; case KEY_KP_4: { if (k.unicode != 0) { handled = false; break; } // numlock disabled. fallthrough to key_left } case KEY_LEFT: { #ifndef APPLE_STYLE_KEYS if (!k.mod.alt) #endif shift_selection_check_pre(k.mod.shift); #ifdef APPLE_STYLE_KEYS if (k.mod.command) { set_cursor_pos(0); } else if (k.mod.alt) { #else if (k.mod.alt) { handled=false; break; } else if (k.mod.command) { #endif bool prev_char=false; int cc=cursor_pos; while (cc>0) { bool ischar=_is_text_char(text[cc-1]); if (prev_char && !ischar) break; prev_char=ischar; cc--; } set_cursor_pos(cc); } else { set_cursor_pos(get_cursor_pos()-1); } shift_selection_check_post(k.mod.shift); } break; case KEY_KP_6: { if (k.unicode != 0) { handled = false; break; } // numlock disabled. fallthrough to key_right } case KEY_RIGHT: { shift_selection_check_pre(k.mod.shift); #ifdef APPLE_STYLE_KEYS if (k.mod.command) { set_cursor_pos(text.length()); } else if (k.mod.alt) { #else if (k.mod.alt) { handled=false; break; } else if (k.mod.command) { #endif bool prev_char=false; int cc=cursor_pos; while (cc<text.length()) { bool ischar=_is_text_char(text[cc]); if (prev_char && !ischar) break; prev_char=ischar; cc++; } set_cursor_pos(cc); } else { set_cursor_pos(get_cursor_pos()+1); } shift_selection_check_post(k.mod.shift); } break; case KEY_DELETE: { if (!editable) break; if (k.mod.shift && !k.mod.command && !k.mod.alt) { cut_text(); break; } if (selection.enabled) { undo_text=text; selection_delete(); break; } int text_len = text.length(); if (cursor_pos==text_len) break; // nothing to do #ifdef APPLE_STYLE_KEYS if (k.mod.alt) { #else if (k.mod.alt) { handled=false; break; } else if (k.mod.command) { #endif int cc=cursor_pos; bool prev_char=false; while (cc<text.length()) { bool ischar=_is_text_char(text[cc]); if (prev_char && !ischar) break; prev_char=ischar; cc++; } delete_text(cursor_pos,cc); } else { undo_text=text; set_cursor_pos(cursor_pos+1); delete_char(); } } break; case KEY_KP_7: { if (k.unicode != 0) { handled = false; break; } // numlock disabled. fallthrough to key_home } case KEY_HOME: { shift_selection_check_pre(k.mod.shift); set_cursor_pos(0); shift_selection_check_post(k.mod.shift); } break; case KEY_KP_1: { if (k.unicode != 0) { handled = false; break; } // numlock disabled. fallthrough to key_end } case KEY_END: { shift_selection_check_pre(k.mod.shift); set_cursor_pos(text.length()); shift_selection_check_post(k.mod.shift); } break; default: { handled=false; } break; } if (handled) { accept_event(); } else if (!k.mod.alt && !k.mod.command) { if (k.unicode>=32 && k.scancode!=KEY_DELETE) { if (editable) { selection_delete(); CharType ucodestr[2]= {(CharType)k.unicode,0}; append_at_cursor(ucodestr); _text_changed(); accept_event(); } } else { return; } } update(); } return; } break; } } void LineEdit::set_align(Align p_align) { ERR_FAIL_INDEX(p_align, 4); align = p_align; update(); } LineEdit::Align LineEdit::get_align() const { return align; } Variant LineEdit::get_drag_data(const Point2& p_point) { if (selection.drag_attempt && selection.enabled) { String t = text.substr(selection.begin, selection.end - selection.begin); Label *l = memnew( Label ); l->set_text(t); set_drag_preview(l); return t; } return Variant(); } bool LineEdit::can_drop_data(const Point2& p_point,const Variant& p_data) const { return p_data.get_type()==Variant::STRING; } void LineEdit::drop_data(const Point2& p_point,const Variant& p_data) { if (p_data.get_type()==Variant::STRING) { set_cursor_at_pixel_pos(p_point.x); int selected = selection.end - selection.begin; Ref<Font> font = get_font("font"); if (font != NULL) { for (int i = selection.begin; i < selection.end; i++) cached_width -= font->get_char_size(text[i]).width; } text.erase(selection.begin, selected); append_at_cursor(p_data); selection.begin = cursor_pos-selected; selection.end = cursor_pos; } } 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(); width=size.width; height=size.height; RID ci = get_canvas_item(); Ref<StyleBox> style = get_stylebox("normal"); if (!is_editable()) style=get_stylebox("read_only"); Ref<Font> font=get_font("font"); style->draw( ci, Rect2( Point2(), size ) ); if (has_focus()) { get_stylebox("focus")->draw( ci, Rect2( Point2(), size ) ); } int x_ofs=0; switch (align) { case ALIGN_FILL: case ALIGN_LEFT: { x_ofs=style->get_offset().x; } break; case ALIGN_CENTER: { x_ofs=int(size.width-(cached_width))/2; } break; case ALIGN_RIGHT: { x_ofs=int(size.width-style->get_offset().x-(cached_width)); } break; } int ofs_max=width-style->get_minimum_size().width; int char_ofs=window_pos; int y_area=height-style->get_minimum_size().height; int y_ofs=style->get_offset().y; int font_ascent=font->get_ascent(); Color selection_color=get_color("selection_color"); Color font_color=get_color("font_color"); 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; int caret_height = font->get_height() > y_area ? y_area : font->get_height(); while(true) { //end of string, break! if (char_ofs>=t.length()) break; 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! if ((x_ofs + char_width) > ofs_max) break; bool selected=selection.enabled && char_ofs>=selection.begin && char_ofs<selection.end; if (selected) VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(char_width, caret_height)), selection_color); font->draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, selected ? font_color_selected : font_color); if (char_ofs==cursor_pos && draw_caret) { VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2( Point2( x_ofs , y_ofs ), Size2( 1, caret_height ) ), cursor_color ); } x_ofs+=char_width; char_ofs++; } 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, caret_height ) ), 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(text,get_global_rect()); } break; case NOTIFICATION_FOCUS_EXIT: { if (OS::get_singleton()->has_virtual_keyboard()) OS::get_singleton()->hide_virtual_keyboard(); } break; } } void LineEdit::copy_text() { if(selection.enabled) { OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin)); } } void LineEdit::cut_text() { if(selection.enabled) { undo_text = text; OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin)); selection_delete(); } } void LineEdit::paste_text() { String paste_buffer = OS::get_singleton()->get_clipboard(); if(paste_buffer != "") { if(selection.enabled) selection_delete(); append_at_cursor(paste_buffer); _text_changed(); } } void LineEdit::undo() { int old_cursor_pos = cursor_pos; text = undo_text; Ref<Font> font = get_font("font"); cached_width = 0; for (int i = 0; i<text.length(); i++) cached_width += font->get_char_size(text[i]).width; if(old_cursor_pos > text.length()) { set_cursor_pos(text.length()); } else { set_cursor_pos(old_cursor_pos); } _text_changed(); }