From: hgn Date: Thu, 20 Jan 2022 08:24:49 +0000 (+0000) Subject: text alignments X-Git-Url: https://harrygodden.com/git/?p=fishladder.git;a=commitdiff_plain;h=bf818249690c5ae1a1e4634714dd2e722fa35632 text alignments --- diff --git a/fishladder.c b/fishladder.c index deafa8e..11d65b0 100644 --- a/fishladder.c +++ b/fishladder.c @@ -3270,6 +3270,10 @@ void vg_render(void) void vg_ui(void) { + // Drawing world name + gui_text( (ui_px [2]){ vg_window_x / 2, 4 }, "THIS IS A WORLD NAME", 2, k_text_align_center ); + gui_text( (ui_px [2]){ vg_window_x / 2, 28 }, "And here is its cool description yo", 1, k_text_align_center ); + if( world.st.state == k_game_state_settings ) { gui_group_id( 35 ); @@ -3289,14 +3293,14 @@ void vg_ui(void) gui_new_node(); { - gui_text( "SETTINGS", 2 ); + gui_text( ui_global_ctx.cursor, "SETTINGS", 2, 0 ); } gui_end(); // Colour scheme selection ui_global_ctx.cursor[1] += 30; - gui_text( "Colour Scheme", 1 ); + gui_text( ui_global_ctx.cursor, "Colour Scheme", 1, 0 ); ui_global_ctx.cursor[1] += 25; gui_new_node(); @@ -3328,16 +3332,18 @@ void vg_ui(void) if( colour_set_id > 0 ) colour_set_id --; } - gui_text( "<", 1 ); + gui_text( ui_global_ctx.cursor, "<", 1, 0 ); gui_end_right(); ui_global_ctx.cursor[2] = 150; gui_new_node(); { gui_fill_rect( ui_global_ctx.cursor, 0x33ffffff ); - ui_global_ctx.cursor[0] += 45; - ui_global_ctx.cursor[1] += 6; - gui_text( (const char *[]){ "Normal", "Extra1", "Extra2" }[ colour_set_id ], 1 ); + gui_text( + (ui_px [2]){ ui_global_ctx.cursor[0] + 75, ui_global_ctx.cursor[1] + 6 }, + (const char *[]){ "Normal", "Extra1", "Extra2" }[ colour_set_id ], + 1, k_text_align_center + ); } gui_end_right(); @@ -3347,7 +3353,7 @@ void vg_ui(void) if( colour_set_id < vg_list_size( colour_sets )-1 ) colour_set_id ++; } - gui_text( ">", 1 ); + gui_text( ui_global_ctx.cursor, ">", 1, 0 ); gui_end_down(); } gui_end_down(); @@ -3356,7 +3362,7 @@ void vg_ui(void) // TODO: remove code dupe ui_global_ctx.cursor[1] += 16; - gui_text( "Tile Theme", 1 ); + gui_text( ui_global_ctx.cursor, "Tile Theme", 1, 0 ); ui_global_ctx.cursor[1] += 20; gui_new_node(); @@ -3367,16 +3373,17 @@ void vg_ui(void) if( world_theme_id > 0 ) world_theme_id --; } - gui_text( "<", 1 ); + gui_text( ui_global_ctx.cursor, "<", 1, 0 ); gui_end_right(); ui_global_ctx.cursor[2] = 150; gui_new_node(); { gui_fill_rect( ui_global_ctx.cursor, 0x33ffffff ); - ui_global_ctx.cursor[0] += 45; - ui_global_ctx.cursor[1] += 6; - gui_text( world_themes[ world_theme_id ].name, 1 ); + gui_text( + (ui_px [2]){ ui_global_ctx.cursor[0] + 75, ui_global_ctx.cursor[1] + 6 }, + world_themes[ world_theme_id ].name, 1, k_text_align_center + ); } gui_end_right(); @@ -3386,7 +3393,7 @@ void vg_ui(void) if( world_theme_id < vg_list_size( world_themes )-1 ) world_theme_id ++; } - gui_text( ">", 1 ); + gui_text( ui_global_ctx.cursor, ">", 1, 0 ); gui_end_down(); } gui_end_down(); diff --git a/vg/vg_console.h b/vg/vg_console.h index 42b94f2..8b2a0cd 100644 --- a/vg/vg_console.h +++ b/vg/vg_console.h @@ -122,7 +122,7 @@ static void vg_console_draw( void ) if( ptr < 0 ) ptr = vg_list_size( vg_console.lines )-1; - ui_text( &ui_global_ctx, vg_console.lines[ptr], vg_console.scale ); + ui_text( &ui_global_ctx, ui_global_ctx.cursor, vg_console.lines[ptr], vg_console.scale, 0 ); ui_global_ctx.cursor[1] -= fh*vg_console.scale; ptr --; @@ -137,7 +137,7 @@ static void vg_console_draw( void ) { ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77333333 ); - ui_text( &ui_global_ctx, vg_console.input, vg_console.scale ); + ui_text( &ui_global_ctx, ui_global_ctx.cursor, vg_console.input, vg_console.scale, 0 ); int start = VG_MIN( vg_console.cursor_pos, vg_console.cursor_user ), end = VG_MAX( vg_console.cursor_pos, vg_console.cursor_user ); diff --git a/vg/vg_debug.h b/vg/vg_debug.h index 16dbbe1..1360548 100644 --- a/vg/vg_debug.h +++ b/vg/vg_debug.h @@ -76,7 +76,7 @@ static void sfx_internal_debug_overlay(void) ui_global_ctx.cursor[0] = baseline + 2; ui_global_ctx.cursor[1] += 2; - ui_text( &ui_global_ctx, infos[i].name, 1 ); + ui_text( &ui_global_ctx, ui_global_ctx.cursor, infos[i].name, 1, 0 ); } ui_end_down( &ui_global_ctx ); ui_global_ctx.cursor[1] += 1; diff --git a/vg/vg_ui.h b/vg/vg_ui.h index b8f826c..eb8c84e 100644 --- a/vg/vg_ui.h +++ b/vg/vg_ui.h @@ -67,6 +67,14 @@ typedef ui_px ui_rect[4]; typedef struct ui_ctx ui_ctx; typedef struct ui_colourset ui_colourset; +// Relative to cursor p0 +enum ui_text_align +{ + k_text_align_left = 0, + k_text_align_right = 1, + k_text_align_center = 2 +}; + struct ui_colourset { union @@ -555,25 +563,46 @@ static struct ui_vert *ui_fill_rect( ui_ctx *ctx, ui_rect rect, u32 colour ) return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 4,4, 4,4 } ); } -static void ui_text( ui_ctx *ctx, const char *str, ui_px scale ) +static ui_px ui_text_line_offset( const char *str, ui_px scale, enum ui_text_align align ) +{ + if( align == k_text_align_left ) + return 0; + + int length = 0; + const char *_c = str; + char c; + + while( (c = *(_c ++)) ) + if( c >= 32 && c <= 126 ) + length ++; + else if( c == '\n' ) + break; + + if( align == k_text_align_right ) + return -length * scale*8; + else + return (-length * scale*8) / 2; +} + +static void ui_text( ui_ctx *ctx, ui_px pos[2], const char *str, ui_px scale, enum ui_text_align align ) { ui_rect text_cursor; + u32 current_colour = 0x00ffffff; + + const char *_c = str; + char c; - text_cursor[0] = ctx->cursor[0]; - text_cursor[1] = ctx->cursor[1]; + text_cursor[0] = pos[0] + ui_text_line_offset( str, scale, align ); + text_cursor[1] = pos[1]; text_cursor[2] = 8*scale; text_cursor[3] = 14*scale; - u32 current_colour = 0x00ffffff; - - const char *_c = str; - char c; while( (c = *(_c ++)) ) { if( c == '\n' ) { text_cursor[1] += 14*scale; - text_cursor[0] = ctx->cursor[0]; + text_cursor[0] = ctx->cursor[0] + ui_text_line_offset( _c, scale, align ); continue; } else if( c >= 33 && c <= 126 ) @@ -758,7 +787,7 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) // title.. ctx->cursor[0] += 2; ctx->cursor[1] += 2; - ui_text( ctx, window->title, 2 ); + ui_text( ctx, ctx->cursor, window->title, 2, 0 ); // Close button ctx->cursor[3] = 25; @@ -772,7 +801,7 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) vg_info( "Click clacked\n" ); } ctx->cursor[0] += 2; - ui_text( ctx, "x", 2 ); + ui_text( ctx, ctx->cursor, "x", 2, 0 ); ui_end( ctx ); if( ui_hasmouse( ctx ) )