the cursor seek is pretty cleaned up and ready for an interactive API

This commit is contained in:
Allen Webster 2016-09-22 22:50:02 -04:00
parent e2bb11ad8d
commit 6a4e401a56
1 changed files with 92 additions and 118 deletions

View File

@ -388,18 +388,43 @@ buffer_get_line_index_from_wrapped_y(f32 *wraps, f32 y, f32 font_height, i32 l_b
return(result);
}
typedef struct Seek_State{
Full_Cursor cursor;
Full_Cursor prev_cursor;
} Seek_State;
internal_4tech Full_Cursor
buffer_cursor_seek(Buffer_Type *buffer, Buffer_Seek seek, f32 max_width,
f32 font_height, f32 *adv, Full_Cursor cursor){
i32 xy_seek;
internal_4tech i32
cursor_seek_step(Seek_State *state, Buffer_Seek seek, i32 xy_seek, f32 max_width,
f32 font_height, f32 *adv, i32 size, u8 ch){
Full_Cursor cursor = state->cursor;
Full_Cursor prev_cursor = cursor;
switch(seek.type){
case buffer_seek_pos:
if (cursor.pos >= seek.pos) goto buffer_cursor_seek_end;
case buffer_seek_wrapped_xy:
if (seek.x == 0 && cursor.wrapped_y >= seek.y) goto buffer_cursor_seek_end;
case buffer_seek_unwrapped_xy:
if (seek.x == 0 && cursor.unwrapped_y >= seek.y) goto buffer_cursor_seek_end;
case buffer_seek_line_char:
if (cursor.line >= seek.line && cursor.character >= seek.character) goto buffer_cursor_seek_end;
}
if (adv){
Buffer_Stream_Type stream = {0};
i32 size = buffer_size(buffer);
i32 i = cursor.pos;
xy_seek = (seek.type == buffer_seek_wrapped_xy || seek.type == buffer_seek_unwrapped_xy);
i32 result = 1;
f32 x, px, y;
stream.use_termination_character = 1;
stream.terminator = 0;
if (buffer_stringify_loop(&stream, buffer, i, size)){
b32 still_looping = 0;
do{
for (; i < stream.end; ++i){
u8 ch = (u8)stream.data[i];
Full_Cursor prev_cursor = cursor;
switch (ch){
case '\n':
@ -435,7 +460,7 @@ cursor_seek_step(Seek_State *state, Buffer_Seek seek, i32 xy_seek, f32 max_width
goto cursor_seek_step_end;
}
x = y = px = 0;
f32 x = 0, px = 0, y = 0;
switch (seek.type){
case buffer_seek_pos:
@ -488,57 +513,6 @@ cursor_seek_step(Seek_State *state, Buffer_Seek seek, i32 xy_seek, f32 max_width
}
cursor_seek_step_end:;
state->cursor = cursor;
state->prev_cursor = prev_cursor;
return(result);
}
internal_4tech Full_Cursor
buffer_cursor_seek(Buffer_Type *buffer, Buffer_Seek seek, f32 max_width,
f32 font_height, f32 *adv, Full_Cursor cursor){
i32 result = 0;
Seek_State state;
i32 xy_seek;
state.cursor = cursor;
switch(seek.type){
case buffer_seek_pos:
if (cursor.pos >= seek.pos) goto buffer_cursor_seek_end;
break;
case buffer_seek_wrapped_xy:
if (seek.x == 0 && cursor.wrapped_y >= seek.y) goto buffer_cursor_seek_end;
break;
case buffer_seek_unwrapped_xy:
if (seek.x == 0 && cursor.unwrapped_y >= seek.y) goto buffer_cursor_seek_end;
break;
case buffer_seek_line_char:
if (cursor.line >= seek.line && cursor.character >= seek.character) goto buffer_cursor_seek_end;
break;
}
if (adv){
Buffer_Stream_Type stream = {0};
i32 size = buffer_size(buffer);
i32 i = cursor.pos;
xy_seek = (seek.type == buffer_seek_wrapped_xy || seek.type == buffer_seek_unwrapped_xy);
result = 1;
stream.use_termination_character = 1;
stream.terminator = 0;
if (buffer_stringify_loop(&stream, buffer, i, size)){
b32 still_looping = 0;
do{
for (; i < stream.end; ++i){
u8 ch = (u8)stream.data[i];
result = cursor_seek_step(&state, seek, xy_seek, max_width,
font_height, adv, size, ch);
if (!result){
goto buffer_cursor_seek_end;
}
@ -551,7 +525,7 @@ buffer_cursor_seek(Buffer_Type *buffer, Buffer_Seek seek, f32 max_width,
}
buffer_cursor_seek_end:;
return(state.cursor);
return(cursor);
}
internal_4tech Partial_Cursor