new font
[fishladder.git] / vg / vg_ui.h
index 11728509ae0d9ac7fa03feae4fd5a83c39332cd3..d4fb20bab148ddb058bbef6a1aa756d6255dfb3f 100644 (file)
@@ -4,7 +4,7 @@ SHADER_DEFINE( shader_ui,
 
        // VERTEX
        "layout (location=0) in vec2 a_co;"                     // i16, i16, .. ?
-       "layout (location=1) in vec2 a_uv;"                     // i8, i8
+       "layout (location=1) in vec2 a_uv;"                     // i16, i16
        "layout (location=2) in vec4 a_colour;" // u32
        "layout (location=3) in vec4 a_clip;"           // i16, i16, i16, i16
        "uniform mat3 uPv;"
@@ -37,9 +37,19 @@ SHADER_DEFINE( shader_ui,
        "void main()"
        "{"
                "float clip_blend = step( aWsp.x, aClip.z ) * step( aWsp.y, aClip.w ) * step( aClip.x, aWsp.x ) * step( aClip.y, aWsp.y );"
-       
-               "vec4 glyph = texture( uTexGlyphs, aTexCoords );"
-               "FragColor = vec4( aColour.rgb * glyph.rgb, aColour.a*glyph.a*clip_blend );"
+               "vec4 glyph = vec4(1.0,1.0,1.0,1.0);"
+               
+               "if( aColour.a == 0.0 )"
+               "{"
+                       "glyph = texture( uTexGlyphs, aTexCoords );"
+                       "glyph.a = smoothstep( 0.47, 0.53, glyph.r );"
+               "}"
+               "else"
+               "{"
+                       "glyph.a = aColour.a;"
+               "}"
+               
+               "FragColor = vec4( aColour.rgb, glyph.a*clip_blend );"
        "}"
        ,
        UNIFORMS({ "uPv", "uTexGlyphs" })
@@ -139,7 +149,7 @@ struct ui_ctx
 
 // Opengl
 int ui_glyph_override = 0;
-ui_px ui_glyph_spacing_x = 6;
+ui_px ui_glyph_spacing_x = 9;
 GLuint ui_glyph_texture = 0;
 
 ui_colourset ui_default_colours = {
@@ -230,10 +240,10 @@ static void ui_default_init(void)
        if( !ui_glyph_override )
        {
                u32 compressed[] = {
-                       #include "fonts/weiholmir.h"
+                       #include "vg/vg_pxfont.h"
                };
        
-               u32 pixels = 0, total = 72*72, data = 0;
+               u32 pixels = 0, total = 256*256, data = 0;
                u8 *image = malloc( total );
                
                while( pixels < total )
@@ -254,7 +264,7 @@ static void ui_default_init(void)
                glGenTextures( 1, &ui_glyph_texture );
                glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
                
-               glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 72, 72, 0, GL_RED, GL_UNSIGNED_BYTE, image );
+               glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0, GL_RED, GL_UNSIGNED_BYTE, image );
                
                vg_tex2d_clamp();
                vg_tex2d_nearest();
@@ -557,54 +567,19 @@ static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, u32 colour, u
 
 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,124,4,124 } );
-}
-
-static void ui_text_use_title( ui_ctx *ctx )
-{
-       ctx->glyph_base = 0;
+       return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 4,4, 4,4 } );
 }
 
-static void ui_text_use_paragraph( ui_ctx *ctx )
-{
-       ctx->glyph_base = 6;
-}
-
-enum text_alignment
-{
-       k_text_alignment_left = 0,
-       k_text_alignment_center,
-       k_text_alignment_right
-};
-
-static ui_px ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_alignment alignment )
+static void ui_text( ui_ctx *ctx, const char *str, ui_px scale )
 {
        ui_rect text_cursor;
 
        text_cursor[0] = ctx->cursor[0];
        text_cursor[1] = ctx->cursor[1];
-       text_cursor[2] = (scale*8)/2;
-       text_cursor[3] = (scale*8)/2;
+       text_cursor[2] = 8*scale;
+       text_cursor[3] = 14*scale;
 
-       u32 current_colour = ctx->override_colour;
-
-       ui_px offset = 0;
-       if( alignment != k_text_alignment_left )
-       {
-               const char *pch = str;
-               for(;;)
-               {
-                       offset += (ui_glyph_spacing_x*scale)/4;
-                       if( !(*pch) || *pch == '\n' )
-                               break;
-                       pch ++;
-               }
-               
-               if( alignment == k_text_alignment_right )
-                       text_cursor[0] = ctx->cursor[0]+ctx->cursor[2]-offset;
-               else
-                       text_cursor[0] = (ctx->cursor[0]+(ctx->cursor[2]/2))-(offset/2);
-       }
+       u32 current_colour = 0x00ffffff;
 
        const char *_c = str;
        char c;
@@ -612,27 +587,21 @@ static ui_px ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_align
        {
                if( c == '\n' )
                {
-                       text_cursor[1] += (7*scale)/2;
+                       text_cursor[1] += 14*scale;
                        text_cursor[0] = ctx->cursor[0];
                        continue;
                }
                else if( c >= 33 && c <= 126 )
                {
                        u8 glyph_base[2];
-                       u8 glyph_index = c - 32;
-                       glyph_base[0] = (glyph_index&0xf);
-                       glyph_base[1] = ctx->glyph_base + ((glyph_index-glyph_base[0])>>4);
-
+                       u8 glyph_index = c;
+                       glyph_base[0] = glyph_index & 0xf;
+                       glyph_base[1] = (glyph_index-glyph_base[0])>>4;
+                       
                        glyph_base[0] *= 8;
                        glyph_base[1] *= 8;
                        
-                       ui_fill_rect_uv( ctx, text_cursor, current_colour, 
-                               (ui_px[4]){
-                                       glyph_base[0],
-                                       128-glyph_base[1],
-                                       glyph_base[0]+8,
-                                       128-(glyph_base[1]+8)
-                               });
+                       ui_fill_rect_uv( ctx, text_cursor, current_colour, (ui_px[4]){glyph_base[0]+2,glyph_base[1]+1,glyph_base[0]+6,glyph_base[1]+8} );
                }
                else if( c == '\x1B' )
                {
@@ -648,14 +617,14 @@ static ui_px ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_align
                                                
                                                switch( colour_id )
                                                {
-                                                       case '0': current_colour = 0xffffffff; break;
-                                                       case '3'|'1'<<8: current_colour = 0xff201fee; break;
-                                                       case '3'|'2'<<8: current_colour = 0xff37e420; break;
-                                                       case '3'|'3'<<8: current_colour = 0xff0ed8e2; break;
-                                                       case '3'|'4'<<8: current_colour = 0xfff15010; break;
-                                                       case '3'|'5'<<8: current_colour = 0xffee20ee; break;
-                                                       case '3'|'6'<<8: current_colour = 0xffeeee20; break;
-                                                       case '3'|'7'<<8: current_colour = 0xffffffff; break;
+                                                       case '0': current_colour = 0x00ffffff; break;
+                                                       case '3'|'1'<<8: current_colour = 0x00201fee; break;
+                                                       case '3'|'2'<<8: current_colour = 0x0037e420; break;
+                                                       case '3'|'3'<<8: current_colour = 0x000ed8e2; break;
+                                                       case '3'|'4'<<8: current_colour = 0x00f15010; break;
+                                                       case '3'|'5'<<8: current_colour = 0x00ee20ee; break;
+                                                       case '3'|'6'<<8: current_colour = 0x00eeee20; break;
+                                                       case '3'|'7'<<8: current_colour = 0x00ffffff; break;
                                                }
                                                
                                                break;
@@ -669,13 +638,10 @@ static ui_px ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_align
                                        break;
                                }
                        }
-                       continue;
                }
                
-               text_cursor[0] += (ui_glyph_spacing_x*scale)/4;
+               text_cursor[0] += 9*scale;
        }
-       
-       return text_cursor[0];
 }
 
 // API control
@@ -807,7 +773,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, 0 );
+                       ui_text( ctx, window->title, 2 );
                        
                        // Close button
                        ctx->cursor[3] = 25;
@@ -821,7 +787,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, 0 );
+                       ui_text( ctx, "x", 2 );
                        ui_end( ctx );
                        
                        if( ui_hasmouse( ctx ) )