Commit: b10c7301ef0db4d89ea9493b2950c4393d55e316
Parent: 690f0f9eb6ea4b9cd0148c52ab4f221658bbad69
Author: Randy Palamar
Date: Tue, 30 Jul 2024 12:52:37 -0600
properly calculate initial cursor position
Diffstat:
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/beamformer.c b/beamformer.c
@@ -217,9 +217,16 @@ do_text_input(BeamformerCtx *ctx, i32 max_chars, Rect r, Color colour)
/* NOTE: guess a cursor position */
if (ctx->is.cursor == -1) {
- f32 narrow_char_scale = 1.45;
- ctx->is.cursor = ctx->is.cursor_hover_p * ctx->is.buf_len * narrow_char_scale;
- CLAMP(ctx->is.cursor, 0, ctx->is.buf_len);
+ f32 x_off = TEXT_BOX_EXTRA_X, 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 */
@@ -377,7 +384,7 @@ draw_settings_ui(BeamformerCtx *ctx, Arena arena, Rect r, v2 mouse)
Rect edit_rect = {
.pos = {.x = pos.x + max_prefix_len + 15, .y = pos.y},
- .size = {.x = txt_s.w + 10, .y = txt_s.h}
+ .size = {.x = txt_s.w + TEXT_BOX_EXTRA_X, .y = txt_s.h}
};
b32 collides = CheckCollisionPointRec(mouse.rl, edit_rect.rl);
diff --git a/beamformer.h b/beamformer.h
@@ -17,6 +17,9 @@
#define FOCUSED_COLOUR (v4){.r = 0.86, .g = 0.28, .b = 0.21, .a = 1.0}
#define HOVERED_COLOUR (v4){.r = 0.11, .g = 0.50, .b = 0.59, .a = 1.0}
+/* NOTE: extra space used for allowing mouse clicks after end of text */
+#define TEXT_BOX_EXTRA_X 10.0f
+
#define TEXT_HOVER_SPEED 5.0f
typedef union {