cross compile build script
[fishladder.git] / vg / vg_ui.h
index 3e4ae5e3717d272c90af5d9832368b5f5bb032c5..f6fa8955333ac76f19987b477038c3c2dac6d70b 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,17 +37,24 @@ 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" })
 )
 
-#define UI_AUTO_FILL 0
-//#define UI_DEBUG
-
 // Types
 // ===========================================================================================================
 
@@ -57,6 +64,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
@@ -78,8 +93,6 @@ struct ui_colourset
 
 struct ui_ctx
 {
-       ui_px padding;
-
        struct ui_qnode
        {
                ui_rect                 rect;
@@ -100,8 +113,6 @@ struct ui_ctx
        *verts;
        #pragma pack(pop)
        
-       u32 override_colour;
-       
        u32 num_verts;
        u16 *indices;
        u32 num_indices;
@@ -118,8 +129,7 @@ struct ui_ctx
        ui_px mouse[2];
        int click_state;        // 0: released, 1: on down, 2: pressed, 3: on release
        
-       ui_colourset *colours_main;
-       ui_colourset *colours_current;
+       ui_colourset *colours;
        
        GLuint vao;
        GLuint vbo;
@@ -137,30 +147,22 @@ struct ui_ctx
 // Globals
 // ===========================================================================================================
 
-// Opengl
-int ui_glyph_override = 0;
-ui_px ui_glyph_spacing_x = 6;
-GLuint ui_glyph_texture = 0;
+#define UI_GLYPH_SPACING_X 9
 
-ui_colourset ui_default_colours = {
-       .main = 0xff00ff00,
-       .hover = 0xffff00ff,
-       .active = 0xffff0000
-};
-ui_ctx ui_global_ctx = { 
-       .padding = 8, 
-       .colours_main = &ui_default_colours
+static GLuint ui_glyph_texture = 0;
+static ui_colourset ui_default_colours = {
+       .main = 0xff807373,
+       .hover = 0xff918484,
+       .active = 0xffad9f9e
 };
 
+static ui_ctx ui_global_ctx;
 
 // Initialization
 // ===========================================================================================================
 
-static void ui_reset_colours( ui_ctx *ctx );
 static void ui_init_context( ui_ctx *ctx, int index_buffer_size )
 {
-       ui_reset_colours( ctx );
-       
        u32 vertex_buffer_size = (index_buffer_size+(index_buffer_size/2));
        
        // Generate the buffer we are gonna be drawing to
@@ -201,6 +203,9 @@ static void ui_init_context( ui_ctx *ctx, int index_buffer_size )
        {
                ctx->verts = (struct ui_vert *)malloc( vertex_buffer_size * sizeof(struct ui_vert) );
                ctx->indices = (u16*)malloc( index_buffer_size * sizeof(u16) );
+
+      if( !ctx->colours )
+         ctx->colours = &ui_default_colours;
        }
 }
 
@@ -214,53 +219,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 );
@@ -269,9 +261,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 );
 }
 
@@ -347,24 +337,6 @@ static void ui_rect_pad( ui_rect rect, ui_px pad )
        rect[3] -= pad*2;
 }
 
-static void ui_vis_rect( ui_rect rect, u32 colour )
-{
-       #ifdef UI_DEBUG
-       v2f p0;
-       v2f p1;
-       
-       p0[0] = rect[0];
-       p0[1] = rect[1];
-       p1[0] = rect[0]+rect[2];
-       p1[1] = rect[1]+rect[3];
-
-       vg_line( p0, (v2f){p1[0],p0[1]}, colour );
-       vg_line( (v2f){p1[0],p0[1]}, p1, colour );
-       vg_line( p1, (v2f){p0[0],p1[1]}, colour );
-       vg_line( (v2f){p0[0],p1[1]}, p0, colour );
-       #endif
-}
-
 // Stack control
 // ===========================================================================================================
 
@@ -384,8 +356,8 @@ static void ui_new_node( ui_ctx *ctx )
        
        if( parent->mouse_over )
        {
-               if( ctx->mouse[0] >= node->rect[0] && ctx->mouse[0] <= node->rect[0]+node->rect[2] &&
-                        ctx->mouse[1] >= node->rect[1] && ctx->mouse[1] <= node->rect[1]+node->rect[3] )
+               if( ctx->mouse[0] >= node->rect[0] && ctx->mouse[0] < node->rect[0]+node->rect[2] &&
+                        ctx->mouse[1] >= node->rect[1] && ctx->mouse[1] < node->rect[1]+node->rect[3] )
                        node->mouse_over = 1;
                else
                        node->mouse_over = 0;
@@ -405,10 +377,7 @@ static int ui_hasmouse( ui_ctx *ctx )
 static void ui_end( ui_ctx *ctx )
 {
        struct ui_qnode *node = &ctx->stack[ --ctx->stack_count ];
-
        ui_rect_copy( node->rect, ctx->cursor );
-       ui_vis_rect( ctx->cursor, 
-               (node->mouse_over && (node->capture_id == ctx->capture_mouse_id))? 0xffff0000: 0xff0000ff );
 }
 
 static void ui_end_down( ui_ctx *ctx )
@@ -557,63 +526,62 @@ 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 } );
+       return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 4,4, 4,4 } );
 }
 
-static void ui_text_use_title( ui_ctx *ctx )
+static ui_px ui_text_line_offset( const char *str, ui_px scale, enum ui_text_align align )
 {
-       ctx->glyph_base = 0;
-}
+   if( align == k_text_align_left )
+      return 0;
 
-static void ui_text_use_paragraph( ui_ctx *ctx )
-{
-       ctx->glyph_base = 6;
-}
+   int length = 0;
+   const char *_c = str;
+   char c;
 
-enum text_alignment
-{
-       k_text_alignment_left = 0,
-       k_text_alignment_center
-};
+   while( (c = *(_c ++)) )
+      if( c >= 32 && c <= 126 )
+         length ++;
+      else if( c == '\n' )
+         break;
 
-static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_alignment alignment )
+   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;
+       u8 c;
 
-       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[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;
 
-       const char *_c = str;
-       char c;
        while( (c = *(_c ++)) )
        {
                if( c == '\n' )
                {
-                       text_cursor[1] += (7*scale)/2;
-                       text_cursor[0] = ctx->cursor[0];
+                       text_cursor[1] += 14*scale;
+                       text_cursor[0] = pos[0] + ui_text_line_offset( _c, scale, align );
                        continue;
                }
-               else if( c >= 33 && c <= 126 )
+               else if( c >= 33 )
                {
                        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' )
                {
@@ -629,14 +597,14 @@ static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_alignm
                                                
                                                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;
@@ -650,10 +618,16 @@ static void ui_text( ui_ctx *ctx, const char *str, ui_px scale, enum text_alignm
                                        break;
                                }
                        }
-                       continue;
+
+         continue;
                }
+      else if( c == '\t' )
+      {
+         text_cursor[0] += UI_GLYPH_SPACING_X*scale*4;
+         continue;
+      }
                
-               text_cursor[0] += (ui_glyph_spacing_x*scale)/4;
+       text_cursor[0] += UI_GLYPH_SPACING_X*scale;
        }
 }
 
@@ -733,7 +707,7 @@ static int ui_button( ui_ctx *ctx, u32 id )
                
                if( ui_hasmouse(ctx) )
                {
-                       ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->hover );
+                       ui_fill_rect( ctx, ctx->cursor, ctx->colours->hover );
                        
                        if( ctx->click_state == 1 )
                        {
@@ -746,7 +720,7 @@ static int ui_button( ui_ctx *ctx, u32 id )
                                return k_button_hold;
                }
                else
-                       ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->main );
+                       ui_fill_rect( ctx, ctx->cursor, ctx->colours->main );
        }
        
        return k_button_released;
@@ -786,7 +760,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, ctx->cursor, window->title, 2, 0 );
                        
                        // Close button
                        ctx->cursor[3] = 25;
@@ -800,7 +774,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, ctx->cursor, "x", 2, 0 );
                        ui_end( ctx );
                        
                        if( ui_hasmouse( ctx ) )
@@ -851,7 +825,7 @@ static void ui_scrollbar( ui_ctx *ctx, struct ui_scrollbar *scrollbar, u32 id )
        
        ui_new_node( ctx );
        {
-               ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->background );
+               ui_fill_rect( ctx, ctx->cursor, ctx->colours->background );
                ui_capture_mouse( ctx, id );
                
                ctx->cursor[1] += scrollbar->py;
@@ -860,14 +834,14 @@ static void ui_scrollbar( ui_ctx *ctx, struct ui_scrollbar *scrollbar, u32 id )
                ui_new_node( ctx );
                {
                        ui_capture_mouse( ctx, __COUNTER__ );
-                       struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, ctx->colours_current->bar );
+                       struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, ctx->colours->bar );
 
                        if( ui_hasmouse( ctx ) || scrollbar->drag )
                        {
-                               drag_bar[0].colour = ctx->colours_current->bar_hover;
-                               drag_bar[1].colour = ctx->colours_current->bar_hover;
-                               drag_bar[2].colour = ctx->colours_current->bar_hover;
-                               drag_bar[3].colour = ctx->colours_current->bar_hover;
+                               drag_bar[0].colour = ctx->colours->bar_hover;
+                               drag_bar[1].colour = ctx->colours->bar_hover;
+                               drag_bar[2].colour = ctx->colours->bar_hover;
+                               drag_bar[3].colour = ctx->colours->bar_hover;
                        
                                // start drag
                                if( ctx->click_state == 1 )
@@ -890,17 +864,6 @@ static ui_px ui_calculate_content_scroll( struct ui_scrollbar *scrollbar, ui_px
        return ((float)scrollbar->py / range) * overlap;
 }
 
-static void ui_override_colours( ui_ctx *ctx, ui_colourset *set )
-{
-       ctx->colours_current = set;
-}
-
-static void ui_reset_colours( ui_ctx *ctx )
-{
-       ctx->colours_current = ctx->colours_main;
-       ctx->override_colour = 0xffffffff;
-}
-
 static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image )
 {
        struct ui_image *img = &ctx->images[ ctx->image_count ++ ];
@@ -938,5 +901,4 @@ static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image )
 #define gui_want_mouse() ui_want_mouse( &ui_global_ctx )
 #define gui_push_image(...) ui_push_image( &ui_global_ctx, __VA_ARGS__ )
 #define gui_scrollbar(...) ui_scrollbar( &ui_global_ctx, __VA_ARGS__)
-#define gui_override_colours(...) ui_override_colours( &ui_global_ctx, __VA_ARGS__)
 #define gui_reset_colours(...) ui_reset_colours( &ui_global_ctx )