Merge pull request #2364 from neikeq/textedit_select

Some TextEdit selection improvements
This commit is contained in:
Juan Linietsky 2015-11-19 00:30:39 -03:00
commit b6084a8a62
2 changed files with 64 additions and 63 deletions

View file

@ -1094,16 +1094,16 @@ void TextEdit::backspace_at_cursor() {
}
bool TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const {
void TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const {
int row=p_mouse.y;
row-=cache.style_normal->get_margin(MARGIN_TOP);
row/=get_row_height();
if (row<0 || row>=get_visible_rows())
return false;
row+=cursor.line_ofs;
float rows=p_mouse.y;
rows-=cache.style_normal->get_margin(MARGIN_TOP);
rows/=get_row_height();
int row=cursor.line_ofs+rows;
if (row<0)
row=0;
int col=0;
if (row>=text.size()) {
@ -1119,7 +1119,6 @@ bool TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) co
r_row=row;
r_col=col;
return true;
}
void TextEdit::_input_event(const InputEvent& p_input_event) {
@ -1177,8 +1176,7 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (mb.button_index==BUTTON_LEFT) {
int row,col;
if (!_get_mouse_pos(Point2i(mb.x,mb.y), row,col))
return;
_get_mouse_pos(Point2i(mb.x,mb.y), row,col);
int prev_col=cursor.column;
int prev_line=cursor.line;
@ -1210,27 +1208,30 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
update();
} else {
if (cursor.line<selection.from_line || (cursor.line==selection.from_line && cursor.column<=selection.from_column)) {
if (cursor.line<selection.selecting_line || (cursor.line==selection.selecting_line && cursor.column<selection.selecting_column)) {
if (selection.shiftclick_left) {
SWAP(selection.from_column,selection.to_column);
SWAP(selection.from_line,selection.to_line);
selection.shiftclick_left = !selection.shiftclick_left;
}
selection.from_column=cursor.column;
selection.from_line=cursor.line;
} else if (cursor.line>selection.to_line || (cursor.line==selection.to_line && cursor.column>=selection.to_column)) {
} else if (cursor.line>selection.selecting_line || (cursor.line==selection.selecting_line && cursor.column>selection.selecting_column)) {
if (!selection.shiftclick_left) {
SWAP(selection.from_column,selection.to_column);
SWAP(selection.from_line,selection.to_line);
selection.shiftclick_left = !selection.shiftclick_left;
}
selection.to_column=cursor.column;
selection.to_line=cursor.line;
} else if (!selection.shiftclick_left) {
selection.from_column=cursor.column;
selection.from_line=cursor.line;
} else {
selection.to_column=cursor.column;
selection.to_line=cursor.line;
selection.active=false;
}
if (selection.from_line>selection.to_line || (selection.from_line==selection.to_line && selection.from_column>selection.to_column)) {
SWAP(selection.from_column,selection.to_column);
SWAP(selection.from_line,selection.to_line);
}
update();
}
@ -1255,6 +1256,7 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (!mb.doubleclick && (OS::get_singleton()->get_ticks_msec()-last_dblclk)<600 && cursor.line==prev_line) {
//tripleclick select line
select(cursor.line,0,cursor.line,text[cursor.line].length());
selection.selecting_column=0;
last_dblclk=0;
} else if (mb.doubleclick && text[cursor.line].length()) {
@ -1279,6 +1281,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
end+=1;
select(cursor.line,beg,cursor.line,end);
selection.selecting_column=beg;
}
last_dblclk = OS::get_singleton()->get_ticks_msec();
@ -1289,7 +1293,6 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
}
} else {
selection.selecting_mode=Selection::MODE_NONE;
// notify to show soft keyboard
notification(NOTIFICATION_FOCUS_ENTER);
}
@ -1302,10 +1305,9 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (mm.button_mask&BUTTON_MASK_LEFT) {
int row,col;
if (!_get_mouse_pos(Point2i(mm.x,mm.y), row,col))
return;
_get_mouse_pos(Point2i(mm.x,mm.y), row,col);
if (selection.selecting_mode==Selection::MODE_POINTER) {
if (selection.selecting_mode!=Selection::MODE_NONE) {
select(selection.selecting_line,selection.selecting_column,row,col);
@ -1585,7 +1587,7 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
break;
}
selection.selecting_test=false;
selection.selecting_text=false;
bool scancode_handled=true;
@ -2008,15 +2010,7 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
break;
}
if (text.size()==1 && text[0].length()==0)
break;
selection.active=true;
selection.from_line=0;
selection.from_column=0;
selection.to_line=text.size()-1;
selection.to_column=text[selection.to_line].length();
selection.selecting_mode=Selection::MODE_NONE;
update();
select_all();
} break;
case KEY_X: {
@ -2201,12 +2195,6 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
}
}
if (!selection.selecting_test) {
selection.selecting_mode=Selection::MODE_NONE;
}
return;
} break;
@ -2218,13 +2206,14 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
void TextEdit::_pre_shift_selection() {
if (!selection.active || selection.selecting_mode!=Selection::MODE_SHIFT) {
if (!selection.active || selection.selecting_mode==Selection::MODE_NONE) {
selection.selecting_line=cursor.line;
selection.selecting_column=cursor.column;
selection.active=true;
selection.selecting_mode=Selection::MODE_SHIFT;
}
selection.selecting_mode=Selection::MODE_SHIFT;
}
void TextEdit::_post_shift_selection() {
@ -2237,7 +2226,7 @@ void TextEdit::_post_shift_selection() {
}
selection.selecting_test=true;
selection.selecting_text=true;
}
/**** TEXT EDIT CORE API ****/
@ -2533,7 +2522,7 @@ void TextEdit::adjust_viewport_to_cursor() {
}
void TextEdit::cursor_set_column(int p_col) {
void TextEdit::cursor_set_column(int p_col, bool p_adjust_viewport) {
if (p_col<0)
p_col=0;
@ -2544,7 +2533,8 @@ void TextEdit::cursor_set_column(int p_col) {
cursor.last_fit_x=get_column_x_offset(cursor.column,get_line(cursor.line));
adjust_viewport_to_cursor();
if (p_adjust_viewport)
adjust_viewport_to_cursor();
if (!cursor_changed_dirty) {
if (is_inside_tree())
@ -2555,7 +2545,7 @@ void TextEdit::cursor_set_column(int p_col) {
}
void TextEdit::cursor_set_line(int p_row) {
void TextEdit::cursor_set_line(int p_row, bool p_adjust_viewport) {
if (setting_row)
return;
@ -2571,8 +2561,8 @@ void TextEdit::cursor_set_line(int p_row) {
cursor.line=p_row;
cursor.column=get_char_pos_for( cursor.last_fit_x, get_line( cursor.line) );
adjust_viewport_to_cursor();
if (p_adjust_viewport)
adjust_viewport_to_cursor();
setting_row=false;
@ -2937,9 +2927,14 @@ void TextEdit::select_all() {
selection.active=true;
selection.from_line=0;
selection.from_column=0;
selection.selecting_line=0;
selection.selecting_column=0;
selection.to_line=text.size()-1;
selection.to_column=text[selection.to_line].length();
selection.selecting_mode=Selection::MODE_NONE;
selection.selecting_mode=Selection::MODE_SHIFT;
selection.shiftclick_left=true;
cursor_set_line( selection.to_line, false );
cursor_set_column( selection.to_column, false );
update();
}
@ -2978,12 +2973,20 @@ void TextEdit::select(int p_from_line,int p_from_column,int p_to_line,int p_to_c
} else if (selection.from_column>selection.to_column) {
selection.shiftclick_left = false;
SWAP( selection.from_column, selection.to_column );
} else {
selection.shiftclick_left = true;
}
} else if (selection.from_line>selection.to_line) {
selection.shiftclick_left = false;
SWAP( selection.from_line, selection.to_line );
SWAP( selection.from_column, selection.to_column );
} else {
selection.shiftclick_left = true;
}
@ -3607,10 +3610,8 @@ String TextEdit::get_tooltip(const Point2& p_pos) const {
if (!tooltip_obj)
return Control::get_tooltip(p_pos);
int row,col;
if (!_get_mouse_pos(p_pos, row,col)) {
return Control::get_tooltip(p_pos);
}
_get_mouse_pos(p_pos, row, col);
String s = text[row];
if (s.length()==0)
return Control::get_tooltip(p_pos);
@ -3789,7 +3790,7 @@ TextEdit::TextEdit() {
selection.selecting_mode=Selection::MODE_NONE;
selection.selecting_line=0;
selection.selecting_column=0;
selection.selecting_test=false;
selection.selecting_text=false;
selection.active=false;
syntax_coloring=false;

View file

@ -55,7 +55,7 @@ class TextEdit : public Control {
Mode selecting_mode;
int selecting_line,selecting_column;
bool selecting_test;
bool selecting_text;
bool active;
@ -270,7 +270,7 @@ class TextEdit : public Control {
void _confirm_completion();
void _update_completion_candidates();
bool _get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const;
void _get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const;
protected:
@ -324,8 +324,8 @@ public:
update();
}
void cursor_set_column(int p_col);
void cursor_set_line(int p_row);
void cursor_set_column(int p_col, bool p_adjust_viewport=true);
void cursor_set_line(int p_row, bool p_adjust_viewport=true);
int cursor_get_column() const;
int cursor_get_line() const;