patch up things from text changes
[fishladder.git] / vg / vg_ui.h
index 70d30aa64677e985150df3fd2e6b7775b15d6093..b8f826c297dc1a07ac51cd216f118707c3eac1a2 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" })
@@ -93,7 +103,7 @@ struct ui_ctx
        struct ui_vert
        {
                ui_px co[2];    //32 4
-               i16 uv[2];              //32 4
+               u8 uv[2];               //16 2
                u32 colour;             //32 4
                ui_rect clip;   //64 8
        }
@@ -134,25 +144,12 @@ struct ui_ctx
        int image_count;
 };
 
-struct ui_sdf_char 
-{
-  int x, y, width, height, originX, originY, advance;
-};
-
-struct ui_sdf_font 
-{
-  const char *name;
-  int size, width, height;
-  struct ui_sdf_char *characters;
-};
-
 // Globals
 // ===========================================================================================================
 
 // Opengl
-int ui_glyph_override = 0;
-ui_px ui_glyph_spacing_x = 6;
-GLuint ui_glyph_texture = 0;
+static ui_px ui_glyph_spacing_x = 9;
+static GLuint ui_glyph_texture = 0;
 
 ui_colourset ui_default_colours = {
        .main = 0xff00ff00,
@@ -197,7 +194,7 @@ static void ui_init_context( ui_ctx *ctx, int index_buffer_size )
                glEnableVertexAttribArray( 0 );
                
                // UV
-               glVertexAttribPointer( 1, 2, GL_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, uv ) );
+               glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE, stride, (void *)offsetof( struct ui_vert, uv ) );
                glEnableVertexAttribArray( 1 );
                
                // COLOUR
@@ -226,53 +223,40 @@ static void ui_context_free( ui_ctx *ctx )
        free( ctx->indices );
 }
 
-static void ui_override_font( GLuint new_tex, ui_px space_x )
-{
-       if( ui_glyph_texture )
-               glDeleteTextures( 1, &ui_glyph_texture );
-       
-       ui_glyph_texture = new_tex;
-       ui_glyph_override = 1;
-       ui_glyph_spacing_x = space_x;
-}
-
 static void ui_default_init(void)
 {
        // Load default font
-       if( !ui_glyph_override )
-       {
-               u32 compressed[] = {
-                       #include "fonts/weiholmir.h"
-               };
-       
-               u32 pixels = 0, total = 72*72, data = 0;
-               u8 *image = malloc( total );
-               
-               while( pixels < total )
-               {
-                       for( int b = 31; b >= 0; b-- )
-                       {
-                               image[ pixels ++ ] = (compressed[data] & (0x1 << b))? 0xff: 0x00;
-                               
-                               if( pixels >= total )
-                               {
-                                       total = 0;
-                                       break;
-                               }
-                       }
-                       data++;
-               }
-               
-               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 );
-               
-               vg_tex2d_clamp();
-               vg_tex2d_nearest();
-               
-               free( image );
-       }
+   u32 compressed[] = {
+      #include "vg/vg_pxfont.h"
+   };
+
+   u32 pixels = 0, total = 256*256, data = 0;
+   u8 *image = malloc( total );
+   
+   while( pixels < total )
+   {
+      for( int b = 31; b >= 0; b-- )
+      {
+         image[ pixels ++ ] = (compressed[data] & (0x1 << b))? 0xff: 0x00;
+         
+         if( pixels >= total )
+         {
+            total = 0;
+            break;
+         }
+      }
+      data++;
+   }
+   
+   glGenTextures( 1, &ui_glyph_texture );
+   glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
+   
+   glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0, GL_RED, GL_UNSIGNED_BYTE, image );
+   
+   vg_tex2d_clamp();
+   vg_tex2d_nearest();
+   
+   free( image );
        
        // Setup OpenGL memory
        SHADER_INIT( shader_ui );
@@ -281,8 +265,7 @@ static void ui_default_init(void)
 
 static void ui_default_free(void)
 {
-       if( !ui_glyph_override )        
-               glDeleteTextures( 1, &ui_glyph_texture );
+   glDeleteTextures( 1, &ui_glyph_texture );
        
        ui_context_free( &ui_global_ctx );
 }
@@ -569,54 +552,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;
-}
-
-static void ui_text_use_paragraph( ui_ctx *ctx )
-{
-       ctx->glyph_base = 6;
+       return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 4,4, 4,4 } );
 }
 
-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;
-
-       u32 current_colour = ctx->override_colour;
+       text_cursor[2] = 8*scale;
+       text_cursor[3] = 14*scale;
 
-       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;
@@ -624,27 +572,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' )
                {
@@ -660,14 +602,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;
@@ -681,13 +623,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
@@ -819,7 +758,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;
@@ -833,7 +772,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 ) )