proper cursor rendering

This commit is contained in:
2026-04-22 12:03:24 -04:00
parent 1dabb59a65
commit fb3bcb4904

115
edit.jai
View File

@@ -77,24 +77,47 @@ main :: () {
}
render_edit_buffer :: () {
num_lines_in_screen := window_height / line_height - 1;
viewable_lines := array_view(lines, first_line, num_lines_in_screen);
for line_buffer: viewable_lines {
visible_lines := get_visible_lines();
for line_buffer: visible_lines {
Simp.set_shader_for_text();
line := builder_to_line(*line_buffer);
line_str := builder_to_string(*line_buffer, do_reset = false,, temp);
Simp.prepare_text(my_font, line);
top_of_line := window_height - (it_index + 1) * line_height;
Simp.draw_prepared_text(my_font, 5, top_of_line, White);
prefix: string;
suffix: string;
if it_index == cursor_y {
Simp.set_shader_for_color();
Simp.immediate_quad(
5., cast(float) top_of_line + line_height,
6., cast(float) top_of_line,
White);
has_cursor_on_line := it_index == cursor_y;
if has_cursor_on_line {
prefix, suffix = string_to_line(line_str, cursor_x);
} else {
prefix, suffix = string_to_line(line_str);
}
prefix_width := Simp.prepare_text(my_font, prefix);
top_of_line := window_height - (it_index + 1) * line_height;
Left_Max :: 5;
Simp.draw_prepared_text(my_font, Left_Max, top_of_line, White);
if has_cursor_on_line {
Simp.set_shader_for_color();
start := ifx suffix.count == 0 then Left_Max else prefix_width + Left_Max;
Simp.immediate_quad(
cast(float) start, cast(float) top_of_line + line_height,
cast(float) start + 1, cast(float) top_of_line,
White);
print("----------------\n");
print("cursor_x: %\n", cursor_x);
print("Prefix width: %\n", prefix_width);
print("Suffix: %\n", suffix);
Simp.set_shader_for_text();
Simp.prepare_text(my_font, suffix);
Simp.draw_prepared_text(my_font, start, top_of_line, White);
}
}
}
@@ -115,10 +138,9 @@ handle_window_resizes :: (window: Window_Type) {
}
handle_arrow :: (key_code: Input.Key_Code) {
num_lines_in_screen := window_height / line_height - 1;
viewable_lines := array_view(lines, first_line, num_lines_in_screen);
visible_lines := get_visible_lines();
max_chars := 30;
max_chars :: 100;
if key_code == {
case .ARROW_UP;
@@ -126,39 +148,72 @@ handle_arrow :: (key_code: Input.Key_Code) {
then 0
else cursor_y - 1;
case .ARROW_DOWN;
cursor_y = ifx cursor_y == viewable_lines.count
then viewable_lines.count
cursor_y = ifx cursor_y == visible_lines.count - 1
then visible_lines.count - 1
else cursor_y + 1;
case .ARROW_LEFT;
cursor_x = ifx cursor_x == 0
then 0
else cursor_x - 1;
case .ARROW_RIGHT;
cursor_y = ifx cursor_x == max_chars
cursor_x = ifx cursor_x == max_chars
then max_chars
else cursor_x + 1;
}
}
builder_to_line :: (using builder: *String_Builder) -> string {
result_builder: String_Builder;
result_builder.allocator = temporary_allocator;
get_visible_lines :: () -> []String_Builder {
num_lines_in_screen := window_height / line_height - 1;
return array_view(lines, first_line, num_lines_in_screen);
}
builder_str := builder_to_string(builder, do_reset = false,, temp);
string_to_line :: (str: string, cursor_char_index := -1) -> string, string {
prefix_builder: String_Builder;
prefix_builder.allocator = temporary_allocator;
suffix_builder: String_Builder;
suffix_builder.allocator = temporary_allocator;
init_string_builder(*prefix_builder);
init_string_builder(*suffix_builder);
start := 0;
for c: builder_str {
char_index := 0;
cursor_index := 0;
current_builder := *prefix_builder;
while char_index < str.count {
defer char_index += 1;
c := str.data[char_index];
if c == #char "\t" {
s := string.{data=builder_str.data + start, count=(it_index + 1) - start};
append(*result_builder, s);
start = it_index + 1;
// s := string.{data=str.data + start, count=(char_index + 1) - start};
// append(current_builder, s);
// append(current_builder, " ");
// start = char_index + 1;
// cursor_index += 4;
} else {
cursor_index += 1;
}
if cursor_index == cursor_char_index {
s := string.{data=str.data + start, count=(char_index + 1) - start};
append(current_builder, s);
current_builder = *suffix_builder;
start = char_index + 1;
}
}
s := string.{data=builder_str.data + start, count=builder_str.count - start};
append(*result_builder, s);
return builder_to_string(*result_builder,, temp);
s := string.{data=str.data + start, count=str.count - start};
append(current_builder, s);
prefix := builder_to_string(*prefix_builder,, temp);
suffix := builder_to_string(*suffix_builder,, temp);
return prefix, suffix;
}
get_time :: () -> s64 {