picky changes to mouse cursor text selection behavior, previously as soon as the...
authorCampbell Barton <ideasman42@gmail.com>
Fri, 9 Mar 2012 23:10:07 +0000 (23:10 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 9 Mar 2012 23:10:07 +0000 (23:10 +0000)
now find the closest point inbetween both chars.

source/blender/blenkernel/intern/modifiers_bmesh.c
source/blender/editors/interface/interface_handlers.c
source/blender/editors/space_text/text_ops.c

index 496533c..8fe8c9c 100644 (file)
@@ -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);
                }
 
index fbc1964..cae6f61 100644 (file)
@@ -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) {
index 9fed3db..1785727 100644 (file)
 
 /************************ 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);