Commit: c8b92ab50d180f1d6fd4c3097a9a2f7c94738d61
Parent: e09a9af4c87a8965737cb162ccb5b6f6d6f40b40
Author: Randy Palamar
Date: Wed, 31 Jul 2024 20:33:54 -0600
use a more correct metric for finding input cursor
Diffstat:
2 files changed, 15 insertions(+), 7 deletions(-)
diff --git a/colourpicker.c b/colourpicker.c
@@ -293,8 +293,8 @@ set_text_input_idx(ColourPickerCtx *ctx, enum input_indices idx, Rect r, v2 mous
return;
ASSERT(CheckCollisionPointRec(mouse.rv, r.rr));
- ctx->is.cursor_p = (mouse.x - r.pos.x) / r.size.w;
- CLAMP01(ctx->is.cursor_p);
+ ctx->is.cursor_hover_p = (mouse.x - r.pos.x) / r.size.w;
+ CLAMP01(ctx->is.cursor_hover_p);
}
static void
@@ -330,10 +330,18 @@ do_text_input(ColourPickerCtx *ctx, Rect r, Color colour)
/* NOTE: guess a cursor position */
if (ctx->is.cursor == -1) {
- f32 narrow_char_scale = 1.1;
- if (ctx->is.idx != INPUT_HEX) narrow_char_scale = 1.45;
- ctx->is.cursor = ctx->is.cursor_p * ctx->is.buf_len * narrow_char_scale;
- CLAMP(ctx->is.cursor, 0, ctx->is.buf_len);
+ /* NOTE: extra offset to help with putting a cursor at idx 0 */
+ #define TEXT_HALF_CHAR_WIDTH 10
+ f32 x_off = TEXT_HALF_CHAR_WIDTH, x_bounds = r.size.w * ctx->is.cursor_hover_p;
+ u32 i;
+ for (i = 0; i < ctx->is.buf_len && x_off < x_bounds; i++) {
+ u32 idx = GetGlyphIndex(ctx->font, ctx->is.buf[i]);
+ if (ctx->font.glyphs[idx].advanceX == 0)
+ x_off += ctx->font.recs[idx].width;
+ else
+ x_off += ctx->font.glyphs[idx].advanceX;
+ }
+ ctx->is.cursor = i;
}
/* NOTE: Braindead NULL termination stupidity */
diff --git a/util.c b/util.c
@@ -132,7 +132,7 @@ typedef struct {
typedef struct {
i32 idx;
i32 cursor;
- f32 cursor_p;
+ f32 cursor_hover_p;
f32 cursor_t;
f32 cursor_t_target;
i32 buf_len;