From 702e85ef849a2ad0a34ef0bd2d9f021e4a6e773f Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 9 Mar 2012 23:10:07 +0000 Subject: picky changes to mouse cursor text selection behavior, previously as soon as the mouse was before a character it would select the previous, even if the cursor was closer to the space between the next 2 chars. now find the closest point inbetween both chars. --- source/blender/blenkernel/intern/modifiers_bmesh.c | 3 +- .../blender/editors/interface/interface_handlers.c | 49 ++++++++++++++++------ source/blender/editors/space_text/text_ops.c | 9 +++- 3 files changed, 46 insertions(+), 15 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/intern/modifiers_bmesh.c b/source/blender/blenkernel/intern/modifiers_bmesh.c index 496533cacd7..8fe8c9cbaed 100644 --- a/source/blender/blenkernel/intern/modifiers_bmesh.c +++ b/source/blender/blenkernel/intern/modifiers_bmesh.c @@ -120,9 +120,8 @@ void DM_to_bmesh_ex(DerivedMesh *dm, BMesh *bm) f->mat_nr = mp->mat_nr; l = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, f); - k = mp->loopstart; - for (j = 0; l; l = BM_iter_step(&liter), k++) { + for (k = mp->loopstart; l; l = BM_iter_step(&liter), k++) { CustomData_to_bmesh_block(&dm->loopData, &bm->ldata, k, &l->head.data); } diff --git a/source/blender/editors/interface/interface_handlers.c b/source/blender/editors/interface/interface_handlers.c index fbc196467aa..cae6f61d269 100644 --- a/source/blender/editors/interface/interface_handlers.c +++ b/source/blender/editors/interface/interface_handlers.c @@ -1282,13 +1282,33 @@ static void ui_textedit_set_cursor_pos(uiBut *but, uiHandleButtonData *data, sho } /* mouse inside the widget */ else if (x >= startx) { + int pos_i; + + /* keep track of previous distance from the cursor to the char */ + float cdist, cdist_prev = 0.0f; + short pos_prev; + const float aspect_sqrt= sqrtf(but->block->aspect); - but->pos= strlen(origstr)-but->ofs; - - /* XXX does not take zoom level into account */ - while (startx + aspect_sqrt * BLF_width(fstyle->uifont_id, origstr+but->ofs) > x) { - int pos_i = but->pos; + but->pos = pos_prev = strlen(origstr) - but->ofs; + + while (TRUE) { + /* XXX does not take zoom level into account */ + cdist = startx + aspect_sqrt * BLF_width(fstyle->uifont_id, origstr + but->ofs); + + /* check if position is found */ + if (cdist < x) { + /* check is previous location was infact closer */ + if (((float)x - cdist) > (cdist_prev - (float)x)) { + but->pos = pos_prev; + } + break; + } + cdist_prev = cdist; + pos_prev = but->pos; + /* done with tricky distance checks */ + + pos_i = but->pos; if (but->pos <= 0) break; if (BLI_str_cursor_step_prev_utf8(origstr, but->ofs, &pos_i)) { but->pos = pos_i; @@ -1580,6 +1600,8 @@ static int ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, int paste static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data) { + int len; + if(data->str) { MEM_freeN(data->str); data->str= NULL; @@ -1594,15 +1616,18 @@ static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data) ui_convert_to_unit_alt_name(but, data->str, data->maxlen); } - data->origstr= BLI_strdup(data->str); - data->selextend= 0; - data->selstartx= 0; + /* won't change from now on */ + len = strlen(data->str); + + data->origstr = BLI_strdupn(data->str, len); + data->selextend = 0; + data->selstartx = 0; /* set cursor pos to the end of the text */ - but->editstr= data->str; - but->pos= strlen(data->str); - but->selsta= 0; - but->selend= strlen(data->str); + but->editstr = data->str; + but->pos = len; + but->selsta = 0; + but->selend = len; /* optional searchbox */ if(but->type==SEARCH_MENU) { diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c index 9fed3db7f16..17857275d36 100644 --- a/source/blender/editors/space_text/text_ops.c +++ b/source/blender/editors/space_text/text_ops.c @@ -73,6 +73,13 @@ /************************ poll ***************************/ + +BM_INLINE int text_pixel_x_to_index(SpaceText *st, const int x) +{ + /* add half the char width so mouse cursor selection is inbetween letters */ + return (x + (st->cwidth / 2)) / st->cwidth; +} + static int text_new_poll(bContext *UNUSED(C)) { return 1; @@ -2530,7 +2537,7 @@ static void text_cursor_set_to_pos(SpaceText *st, ARegion *ar, int x, int y, int else x-= TXT_OFFSET; if(x<0) x= 0; - x = (x/st->cwidth) + st->left; + x = text_pixel_x_to_index(st, x) + st->left; if(st->wordwrap) { text_cursor_set_to_pos_wrapped(st, ar, x, y, sel); -- cgit v1.2.3