proper cursor rendering
This commit is contained in:
115
edit.jai
115
edit.jai
@@ -77,24 +77,47 @@ main :: () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render_edit_buffer :: () {
|
render_edit_buffer :: () {
|
||||||
num_lines_in_screen := window_height / line_height - 1;
|
visible_lines := get_visible_lines();
|
||||||
viewable_lines := array_view(lines, first_line, num_lines_in_screen);
|
for line_buffer: visible_lines {
|
||||||
for line_buffer: viewable_lines {
|
|
||||||
Simp.set_shader_for_text();
|
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);
|
prefix: string;
|
||||||
top_of_line := window_height - (it_index + 1) * line_height;
|
suffix: string;
|
||||||
Simp.draw_prepared_text(my_font, 5, top_of_line, White);
|
|
||||||
|
|
||||||
if it_index == cursor_y {
|
has_cursor_on_line := it_index == cursor_y;
|
||||||
Simp.set_shader_for_color();
|
|
||||||
Simp.immediate_quad(
|
if has_cursor_on_line {
|
||||||
5., cast(float) top_of_line + line_height,
|
prefix, suffix = string_to_line(line_str, cursor_x);
|
||||||
6., cast(float) top_of_line,
|
} else {
|
||||||
White);
|
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) {
|
handle_arrow :: (key_code: Input.Key_Code) {
|
||||||
num_lines_in_screen := window_height / line_height - 1;
|
visible_lines := get_visible_lines();
|
||||||
viewable_lines := array_view(lines, first_line, num_lines_in_screen);
|
|
||||||
|
|
||||||
max_chars := 30;
|
max_chars :: 100;
|
||||||
|
|
||||||
if key_code == {
|
if key_code == {
|
||||||
case .ARROW_UP;
|
case .ARROW_UP;
|
||||||
@@ -126,39 +148,72 @@ handle_arrow :: (key_code: Input.Key_Code) {
|
|||||||
then 0
|
then 0
|
||||||
else cursor_y - 1;
|
else cursor_y - 1;
|
||||||
case .ARROW_DOWN;
|
case .ARROW_DOWN;
|
||||||
cursor_y = ifx cursor_y == viewable_lines.count
|
cursor_y = ifx cursor_y == visible_lines.count - 1
|
||||||
then viewable_lines.count
|
then visible_lines.count - 1
|
||||||
else cursor_y + 1;
|
else cursor_y + 1;
|
||||||
|
|
||||||
case .ARROW_LEFT;
|
case .ARROW_LEFT;
|
||||||
cursor_x = ifx cursor_x == 0
|
cursor_x = ifx cursor_x == 0
|
||||||
then 0
|
then 0
|
||||||
else cursor_x - 1;
|
else cursor_x - 1;
|
||||||
case .ARROW_RIGHT;
|
case .ARROW_RIGHT;
|
||||||
cursor_y = ifx cursor_x == max_chars
|
cursor_x = ifx cursor_x == max_chars
|
||||||
then max_chars
|
then max_chars
|
||||||
else cursor_x + 1;
|
else cursor_x + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
builder_to_line :: (using builder: *String_Builder) -> string {
|
get_visible_lines :: () -> []String_Builder {
|
||||||
result_builder: String_Builder;
|
num_lines_in_screen := window_height / line_height - 1;
|
||||||
result_builder.allocator = temporary_allocator;
|
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;
|
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" {
|
if c == #char "\t" {
|
||||||
s := string.{data=builder_str.data + start, count=(it_index + 1) - start};
|
// s := string.{data=str.data + start, count=(char_index + 1) - start};
|
||||||
append(*result_builder, s);
|
// append(current_builder, s);
|
||||||
start = it_index + 1;
|
// 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 {
|
get_time :: () -> s64 {
|
||||||
|
|||||||
Reference in New Issue
Block a user