From 33eb774c970500fe536d4a0aa926bc816e2eac86 Mon Sep 17 00:00:00 2001 From: Grant Horner Date: Tue, 9 Dec 2025 18:03:32 -0500 Subject: [PATCH] correctly display single line selection --- src/main.odin | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/src/main.odin b/src/main.odin index a00053c..e9bf329 100644 --- a/src/main.odin +++ b/src/main.odin @@ -168,9 +168,54 @@ main :: proc() { } raw_data(line)[len(line)] = 0 - cstr := strings.unsafe_string_to_cstring(string(line[:])) - pos := r.Vector2{0, font_size * f32(line_index)} - r.DrawTextEx(font, cstr, pos, font_size, 0, r.WHITE) + + if selection.active && + line_index == selection.start_line && + line_index == selection.end_line { + selection_begin := min(selection.start_char, selection.end_char) + selection_end := max(selection.start_char, selection.end_char) + prefix := line[:selection_begin] + suffix := line[selection_end + 1:] if selection_end + 1 < len(line) else []u8{} + // need to copy these into separate buffers for zero value + x: f32 = 0.0 + if len(prefix) != 0 { + cstr := strings.clone_to_cstring(string(prefix), context.temp_allocator) + measurements := r.MeasureTextEx(font, cstr, font_size, 0) + pos := r.Vector2{x, font_size * f32(line_index)} + r.DrawTextEx(font, cstr, pos, font_size, 0, r.WHITE) + x += measurements.x + } + + selected_str := string(line[selection_begin:selection_end + 1]) + cstr := + strings.clone_to_cstring(selected_str, context.temp_allocator) if len(suffix) != 0 else strings.unsafe_string_to_cstring(selected_str) + measurements := r.MeasureTextEx(font, cstr, font_size, 0) + pos := r.Vector2{x, font_size * f32(line_index)} + r.DrawRectangle( + i32(pos.x), + i32(pos.y), + i32(measurements.x), + i32(measurements.y), + r.WHITE, + ) + r.DrawTextEx(font, cstr, pos, font_size, 0, r.BLACK) + x += measurements.x + + if len(suffix) != 0 { + cstr := strings.unsafe_string_to_cstring(string(suffix)) + // cstr := strings.clone_to_cstring(string(suffix), context.temp_allocator) + pos := r.Vector2{x, font_size * f32(line_index)} + r.DrawTextEx(font, cstr, pos, font_size, 0, r.WHITE) + } + } else if selection.active && line_index == selection.start_line { + } else if selection.active && line_index == selection.end_line { + } else if selection.active && selection.start_line < line_index && line_index < selection.end_line { + } else { + // No selection active + cstr := strings.unsafe_string_to_cstring(string(line[:])) + pos := r.Vector2{0, font_size * f32(line_index)} + r.DrawTextEx(font, cstr, pos, font_size, 0, r.WHITE) + } } render_cursor(&cursor)