Platform and OS stability stuff
[vg.git] / src / vg / vg_ui.h
index f6fa8955333ac76f19987b477038c3c2dac6d70b..4e09338dc2985149ea84f711e596433754ba37f4 100644 (file)
@@ -1,12 +1,28 @@
-// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
+/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
 
-SHADER_DEFINE( shader_ui,
+/*
+ * TODO: Get rid of many context design
+ */
 
-       // VERTEX
-       "layout (location=0) in vec2 a_co;"                     // i16, i16, .. ?
-       "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
+#ifndef VG_UI_H
+#define VG_UI_H
+
+#include "vg/vg.h"
+#include "vg/vg_tex.h"
+#include "vg/vg_shader.h"
+
+static struct vg_shader _shader_ui =
+{
+   .name = "[vg] ui",
+   .link = NULL,
+   .vs = 
+   {
+      .orig_file = NULL,
+      .static_src = 
+       "layout (location=0) in vec2 a_co;"
+       "layout (location=1) in vec2 a_uv;"
+       "layout (location=2) in vec4 a_colour;"
+       "layout (location=3) in vec4 a_clip;"
        "uniform mat3 uPv;"
        ""
        "out vec2 aTexCoords;"
@@ -23,8 +39,11 @@ SHADER_DEFINE( shader_ui,
                "aWsp = a_co;"
                "aClip = a_clip;"
        "}",
-       
-       // FRAGMENT
+   },
+   .fs = 
+   {
+      .orig_file = NULL,
+      .static_src = 
        "uniform sampler2D uTexGlyphs;"
        "out vec4 FragColor;"
        ""
@@ -36,7 +55,11 @@ 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 );"
+               "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 = vec4(1.0,1.0,1.0,1.0);"
                
                "if( aColour.a == 0.0 )"
@@ -51,12 +74,8 @@ SHADER_DEFINE( shader_ui,
                
                "FragColor = vec4( aColour.rgb, glyph.a*clip_blend );"
        "}"
-       ,
-       UNIFORMS({ "uPv", "uTexGlyphs" })
-)
-
-// Types
-// ===========================================================================================================
+   }
+};
 
 typedef i16                            ui_px;
 typedef u32                            ui_colour;
@@ -64,7 +83,7 @@ typedef ui_px                                 ui_rect[4];
 typedef struct ui_ctx  ui_ctx;
 typedef struct ui_colourset ui_colourset;
 
-// Relative to cursor p0
+/* Relative to cursor p0 */
 enum ui_text_align
 {
    k_text_align_left = 0,
@@ -105,13 +124,15 @@ struct ui_ctx
        #pragma pack(push,1)
        struct ui_vert
        {
-               ui_px co[2];    //32 4
-               u8 uv[2];               //16 2
-               u32 colour;             //32 4
-               ui_rect clip;   //64 8
+               ui_px co[2];
+               u8 uv[2];
+               u32 colour;
+               ui_rect clip;
        }
        *verts;
        #pragma pack(pop)
+
+   u32 control_id;
        
        u32 num_verts;
        u16 *indices;
@@ -122,12 +143,11 @@ struct ui_ctx
        u32 stack_count;
        u32 capture_mouse_id;
        int capture_lock;
-       u32 id_base;
-       int glyph_base;
+
        
-       // User input
+       /* User input */
        ui_px mouse[2];
-       int click_state;        // 0: released, 1: on down, 2: pressed, 3: on release
+       int click_state;        /* 0: released, 1: on down, 2: pressed, 3: on release */
        
        ui_colourset *colours;
        
@@ -144,9 +164,6 @@ struct ui_ctx
        int image_count;
 };
 
-// Globals
-// ===========================================================================================================
-
 #define UI_GLYPH_SPACING_X 9
 
 static GLuint ui_glyph_texture = 0;
@@ -158,76 +175,101 @@ static ui_colourset ui_default_colours = {
 
 static ui_ctx ui_global_ctx;
 
-// Initialization
-// ===========================================================================================================
-
-static void ui_init_context( ui_ctx *ctx, int index_buffer_size )
+static void ui_context_vg_free( ui_ctx *ctx )
 {
-       u32 vertex_buffer_size = (index_buffer_size+(index_buffer_size/2));
-       
-       // Generate the buffer we are gonna be drawing to
-       {
-               glGenVertexArrays(1, &ctx->vao);
-               glGenBuffers( 1, &ctx->vbo );
-               glGenBuffers( 1, &ctx->ebo );
-               glBindVertexArray( ctx->vao );
-               
-               glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo );
-               
-               glBufferData( GL_ARRAY_BUFFER, vertex_buffer_size * sizeof( struct ui_vert ), NULL, GL_DYNAMIC_DRAW );
-               glBindVertexArray( ctx->vao );
-               
-               glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo );
-               glBufferData( GL_ELEMENT_ARRAY_BUFFER, index_buffer_size * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW );
-               
-               u32 const stride = sizeof( struct ui_vert );
-               
-               // XY
-               glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, co ) );
-               glEnableVertexAttribArray( 0 );
-               
-               // UV
-               glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE, stride, (void *)offsetof( struct ui_vert, uv ) );
-               glEnableVertexAttribArray( 1 );
-               
-               // COLOUR
-               glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (void *)offsetof( struct ui_vert, colour ) );
-               glEnableVertexAttribArray( 2 );
-               
-               // CLIPPING
-               glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, clip ) );
-               glEnableVertexAttribArray( 3 );
-       }
+       glDeleteVertexArrays( 1, &ctx->vao );
+       glDeleteBuffers( 1, &ctx->vbo );
+       glDeleteBuffers( 1, &ctx->ebo );
        
-       // Initialize default context
-       {
-               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;
-       }
+       vg_free( ctx->verts );
+       vg_free( ctx->indices );
 }
 
-static void ui_context_free( ui_ctx *ctx )
+static int ui_init_context( ui_ctx *ctx, int index_buffer_size )
 {
-       glDeleteVertexArrays( 1, &ctx->vao );
-       glDeleteBuffers( 1, &ctx->vbo );
-       glDeleteBuffers( 1, &ctx->ebo );
+       u32 vertex_buffer_size = (index_buffer_size+(index_buffer_size/2));
        
-       free( ctx->verts );
-       free( ctx->indices );
+       /* Generate the buffer we are gonna be drawing to */
+   glGenVertexArrays( 1, &ctx->vao );
+   glGenBuffers( 1, &ctx->vbo );
+   glGenBuffers( 1, &ctx->ebo );
+
+   glBindVertexArray( ctx->vao );
+   glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo );
+   
+   glBufferData( GL_ARRAY_BUFFER, 
+         vertex_buffer_size * sizeof( struct ui_vert ), 
+         NULL, GL_DYNAMIC_DRAW );
+   glBindVertexArray( ctx->vao );
+   
+   glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo );
+   glBufferData( GL_ELEMENT_ARRAY_BUFFER, 
+         index_buffer_size * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW );
+
+   VG_CHECK_GL_ERR();
+
+   /* Set pointers */
+   u32 const stride = sizeof( struct ui_vert );
+   
+   /* XY */
+   glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE, 
+         stride, (void *)offsetof( struct ui_vert, co ) );
+   glEnableVertexAttribArray( 0 );
+   
+   /* UV */
+   glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE, 
+         stride, (void *)offsetof( struct ui_vert, uv ) );
+   glEnableVertexAttribArray( 1 );
+   
+   /* COLOUR */
+   glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, 
+         (void *)offsetof( struct ui_vert, colour ) );
+   glEnableVertexAttribArray( 2 );
+   
+   /* CLIPPING */
+   glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride, 
+         (void *)offsetof( struct ui_vert, clip ) );
+   glEnableVertexAttribArray( 3 );
+   
+   VG_CHECK_GL_ERR();
+
+       /* Alloc RAM default context */
+   ctx->verts = (struct ui_vert *)vg_alloc( 
+         vertex_buffer_size * sizeof(struct ui_vert) );
+   if( !ctx->verts )
+      goto il_fail_alloc_verts;
+
+   ctx->indices = (u16*)vg_alloc( index_buffer_size * sizeof(u16) );
+   if( !ctx->indices )
+      goto il_fail_alloc_indices;
+
+   if( !ctx->colours )
+      ctx->colours = &ui_default_colours;
+
+   return 1;
+
+                           vg_free( ctx->indices );
+                           ctx->indices = NULL;
+il_fail_alloc_indices:
+                           vg_free( ctx->verts );
+                           ctx->verts = NULL;
+il_fail_alloc_verts:
+                           glDeleteBuffers( 1, &ctx->ebo );
+                           glDeleteBuffers( 1, &ctx->vbo );
+                           glDeleteVertexArrays( 1, &ctx->vao );
+                           VG_CHECK_GL_ERR();
+                           return 0;
 }
 
-static void ui_default_init(void)
+static int ui_default_init(void)
 {
-       // Load default font
+       /* Load default font */
    u32 compressed[] = {
       #include "vg/vg_pxfont.h"
    };
 
    u32 pixels = 0, total = 256*256, data = 0;
-   u8 *image = malloc( total );
+   u8 *image = vg_alloc( total );
    
    while( pixels < total )
    {
@@ -246,47 +288,64 @@ 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, 256, 256, 0, 
+         GL_RED, GL_UNSIGNED_BYTE, image );
    
-   glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0, GL_RED, GL_UNSIGNED_BYTE, image );
+   vg_free( image );
+
+   VG_CHECK_GL_ERR();
    
    vg_tex2d_clamp();
    vg_tex2d_nearest();
-   
-   free( image );
        
-       // Setup OpenGL memory
-       SHADER_INIT( shader_ui );
-       ui_init_context( &ui_global_ctx, 20000 );
+       if( !ui_init_context( &ui_global_ctx, 20000 ) ) goto il_generic_fail;
+   if( !vg_shader_compile( &_shader_ui ) ) goto il_shader_fail;
+
+   return 1;
+
+il_shader_fail:
+il_generic_fail:
+   glDeleteTextures( 1, &ui_glyph_texture );
+   vg_free( image );
+   return 0;
 }
 
 static void ui_default_free(void)
 {
+   vg_free_shader( &_shader_ui );
    glDeleteTextures( 1, &ui_glyph_texture );
-       ui_context_free( &ui_global_ctx );
+       ui_context_vg_free( &ui_global_ctx );
 }
 
-static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, u32 colour, ui_px uv[4] );
+static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, 
+      u32 colour, ui_px uv[4] );
+
 static void ui_draw( ui_ctx *ctx, m3x3f view_override )
 {
        u32 num_indices_normal = ctx->num_indices;
        
-       // Append images to back of buffer
+       /* Append images to back of buffer */
        for( int i = 0; i < ctx->image_count; i ++ )
-               ui_fill_rect_uv( ctx, ctx->images[i].rc, 0xffffffff, (ui_px[4]){0,0,128,128} );
+   {
+               ui_fill_rect_uv( ctx, ctx->images[i].rc, 
+            0xffffffff, (ui_px[4]){0,0,128,128} );
+   }
 
        glBindVertexArray( ctx->vao );
        
        glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo );
-       glBufferSubData( GL_ARRAY_BUFFER, 0, ctx->num_verts * sizeof( struct ui_vert ), ctx->verts );
+       glBufferSubData( GL_ARRAY_BUFFER, 0, 
+         ctx->num_verts * sizeof( struct ui_vert ), ctx->verts );
        
        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo );
-       glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, ctx->num_indices * sizeof( u16 ), ctx->indices );
+       glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, 
+         ctx->num_indices * sizeof( u16 ), ctx->indices );
        
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glBlendEquation(GL_FUNC_ADD);
        
-       SHADER_USE( shader_ui );
+   glUseProgram( _shader_ui.id );
        
        m3x3f view = M3X3_IDENTITY;
        
@@ -295,32 +354,37 @@ static void ui_draw( ui_ctx *ctx, m3x3f view_override )
                view_override = view;
        
                m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } );
-               m3x3_scale( view, (v3f){ 1.0f/((float)vg_window_x*0.5f), -1.0f/((float)vg_window_y*0.5f), 1.0f } );
+               m3x3_scale( view, (v3f){ 1.0f/((float)vg_window_x*0.5f), 
+                              -1.0f/((float)vg_window_y*0.5f), 1.0f } );
        }
        
-       glUniformMatrix3fv( SHADER_UNIFORM( shader_ui, "uPv" ), 1, GL_FALSE, (float *)view_override );
+   /* TODO? */
+       glUniformMatrix3fv( glGetUniformLocation( _shader_ui.id, "uPv" ), 1, 
+         GL_FALSE, (float *)view_override );
        
        glActiveTexture( GL_TEXTURE0 );
        glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
-       glUniform1i( SHADER_UNIFORM( shader_ui, "uTexGlyphs" ), 0 );
+       glUniform1i( glGetUniformLocation( _shader_ui.id, "uTexGlyphs" ), 0 );
        
-       glDrawElements( GL_TRIANGLES, num_indices_normal, GL_UNSIGNED_SHORT, (void*)(0) );
+       glDrawElements( GL_TRIANGLES, num_indices_normal, 
+         GL_UNSIGNED_SHORT, (void*)(0) );
        
-       // Draw image elements
+       /* Draw image elements */
        for( int i = 0; i < ctx->image_count; i ++ )
        {
                struct ui_image *img = &ctx->images[i];
                
                glBindTexture( GL_TEXTURE_2D, img->image );
-               glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)( (num_indices_normal + 6*i)*sizeof(u16) ) );
+               glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 
+            (void*)( (num_indices_normal + 6*i)*sizeof(u16) ) );
        }
        
        glDisable(GL_BLEND);
 }
 
-// Rect controls
-// ===========================================================================================================
-
+/
+ * Rect controls
+ */
 static void ui_rect_copy( ui_rect src, ui_rect dst )
 {
        dst[0] = src[0];
@@ -337,9 +401,9 @@ static void ui_rect_pad( ui_rect rect, ui_px pad )
        rect[3] -= pad*2;
 }
 
-// Stack control
-// ===========================================================================================================
-
+/*
+ * Stack control
+ */
 static struct ui_qnode *ui_current( ui_ctx *ctx )
 {
        return &ctx->stack[ ctx->stack_count-1 ];
@@ -348,7 +412,10 @@ static struct ui_qnode *ui_current( ui_ctx *ctx )
 static void ui_new_node( ui_ctx *ctx )
 {
        if( ctx->stack_count == vg_list_size( ctx->stack ) )
-               vg_exiterr( "[UI] Stack overflow while creating box!" );
+   {
+      vg_error( "[UI] Stack overflow while creating box!" );
+      return;
+   }
        
        struct ui_qnode *parent = &ctx->stack[ ctx->stack_count-1 ];
        struct ui_qnode *node = &ctx->stack[ ctx->stack_count++ ];
@@ -356,8 +423,10 @@ 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;
@@ -406,7 +475,6 @@ static void ui_fill_x( ui_ctx *ctx )
        ctx->cursor[2] = node->rect[2] - (ctx->cursor[0]-node->rect[0]);
 }
 
-// Alignment: | []   | -> |    []|
 static void ui_align_bottom( ui_ctx *ctx )
 {
        struct ui_qnode *node = ui_current( ctx );
@@ -437,21 +505,14 @@ static void ui_clamp_rect( ui_rect parent, ui_rect dest )
        dest[1] = vg_max( parent[1], dest[1] );
 }
 
-static u32 ui_group_id( ui_ctx *ctx, u32 lesser_unique )
-{
-       return ctx->id_base | lesser_unique;
-}
-
 static void ui_capture_mouse( ui_ctx *ctx, u32 id )
 {
-       u32 group_uid = ui_group_id(ctx,id);
-
        struct ui_qnode *node = &ctx->stack[ ctx->stack_count-1 ];
-       node->capture_id = group_uid;
+       node->capture_id = id;
        
        if( !ctx->capture_lock && node->mouse_over )
        {
-               ctx->capture_mouse_id = group_uid;
+               ctx->capture_mouse_id = id;
        }
 }
 
@@ -476,10 +537,8 @@ static void ui_release_clip( ui_ctx *ctx )
        ctx->clipping[3] =  32000;
 }
 
-// Drawing
-// ===========================================================================================================
-
-static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, u32 colour, ui_px uv[4] )
+static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, 
+      u32 colour, ui_px uv[4] )
 {
        struct ui_vert *vertices = &ctx->verts[ ctx->num_verts ];
        vertices[0].co[0] = rect[0];
@@ -529,7 +588,8 @@ 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 ui_px ui_text_line_offset( const char *str, ui_px scale, enum ui_text_align align )
+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;
@@ -550,7 +610,8 @@ static ui_px ui_text_line_offset( const char *str, ui_px scale, enum ui_text_ali
       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 )
+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;
@@ -581,7 +642,13 @@ static void ui_text( ui_ctx *ctx, ui_px pos[2], const char *str, ui_px scale, en
                        glyph_base[0] *= 8;
                        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} );
+                       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' )
                {
@@ -631,9 +698,9 @@ static void ui_text( ui_ctx *ctx, ui_px pos[2], const char *str, ui_px scale, en
        }
 }
 
-// API control
-// ====================================================================
-
+/*
+ * API Control
+ */
 static void ui_begin( ui_ctx *ctx, ui_px res_x, ui_px res_y )
 {
        ctx->cursor[0] = 0;
@@ -660,7 +727,10 @@ static void ui_begin( ui_ctx *ctx, ui_px res_x, ui_px res_y )
 static void ui_resolve( ui_ctx *ctx )
 {
        if( ctx->stack_count-1 )
-               vg_exiterr( "[UI] Mismatched node create/drestroy!" );
+   {
+      vg_error( "[UI] Mismatched node create/drestroy!" );
+      return;
+   }
        
        if( ctx->click_state == 3 || ctx->click_state == 0 )
        {
@@ -668,9 +738,6 @@ static void ui_resolve( ui_ctx *ctx )
        }
 }
 
-// User Input piping
-// ====================================================================
-
 static void ui_set_mouse( ui_ctx *ctx, int x, int y, int click_state )
 {
        ctx->mouse[0] = x;
@@ -679,9 +746,9 @@ static void ui_set_mouse( ui_ctx *ctx, int x, int y, int click_state )
        ctx->click_state = click_state;
 }
 
-// High level controls
-// ====================================================================
-
+/*
+ * High level controls
+ */
 struct ui_window
 {
        const char *title;
@@ -699,11 +766,13 @@ enum button_state
        k_button_hold
 };
 
-static int ui_button( ui_ctx *ctx, u32 id )
+static int ui_button( ui_ctx *ctx )
 {
+   u32 uid = ctx->control_id ++;
+
        ui_new_node( ctx );
        {
-               ui_capture_mouse( ctx, id );
+               ui_capture_mouse( ctx, uid );
                
                if( ui_hasmouse(ctx) )
                {
@@ -718,6 +787,8 @@ static int ui_button( ui_ctx *ctx, u32 id )
                                return k_button_click;
                        else if( ctx->capture_lock && ctx->click_state == 2 )
                                return k_button_hold;
+
+         return k_button_click;
                }
                else
                        ui_fill_rect( ctx, ctx->cursor, ctx->colours->main );
@@ -728,8 +799,6 @@ static int ui_button( ui_ctx *ctx, u32 id )
 
 static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group )
 {
-       ctx->id_base = control_group << 16;
-
        if( window->drag )
        {
                window->transform[0] = ctx->mouse[0]+window->drag_offset[0];
@@ -747,29 +816,29 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group )
        
        ui_new_node( ctx );
        {
-               ui_capture_mouse( ctx, __COUNTER__ );
+               ui_capture_mouse( ctx, ctx->control_id ++ );
                
-               // Drag bar
+               /* Drag bar */
                ctx->cursor[3] = 25;            
                ui_new_node( ctx );
                {
-                       ui_capture_mouse( ctx, __COUNTER__ );
+                       ui_capture_mouse( ctx, ctx->control_id ++ );
                        
                        struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, 0xff555555 );
                        
-                       // title..
+                       /* title.. */
                        ctx->cursor[0] += 2;
                        ctx->cursor[1] += 2;
                        ui_text( ctx, ctx->cursor, window->title, 2, 0 );
                        
-                       // Close button
+                       /* Close button */
                        ctx->cursor[3] = 25;
                        ctx->cursor[2] = 25;
                        ui_align_right( ctx );
                        ui_align_top( ctx );
                        ui_rect_pad( ctx->cursor, 4 );
                        
-                       if( ui_button( ctx, __COUNTER__ ) )
+                       if( ui_button( ctx ) )
                        {
                                vg_info( "Click clacked\n" );
                        }
@@ -784,7 +853,7 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group )
                                drag_bar[2].colour = 0xff777777;
                                drag_bar[3].colour = 0xff777777;
                        
-                               // start drag
+                               /* start drag */
                                if( ctx->click_state == 1 )
                                {
                                        window->drag = 1;
@@ -799,79 +868,97 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group )
        return 1;
 }
 
-struct ui_scrollbar
+static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image )
 {
-       int     drag;
-       ui_px drag_offset;
-       
-       ui_px py;
-       ui_px bar_height;
-       ui_px view_height;
+       struct ui_image *img = &ctx->images[ ctx->image_count ++ ];
+       ui_rect_copy( rc, img->rc );
+       img->image = image;
+}
+
+struct ui_slider
+{
+   float *data;
+   float min, max;
 };
 
-static void ui_scrollbar( ui_ctx *ctx, struct ui_scrollbar *scrollbar, u32 id )
+struct ui_slider_vector
 {
-       scrollbar->view_height = ctx->cursor[3];
+   float *data, min, max;
+   struct ui_slider sub[4];
+   u32 len;
+};
 
-       if( scrollbar->drag )
-       {
-               scrollbar->py = ctx->mouse[1]+scrollbar->drag_offset;
-               scrollbar->py = VG_MAX( scrollbar->py, 0 );
-               scrollbar->py = VG_MIN( scrollbar->py, ctx->cursor[3] - scrollbar->bar_height );
-               
-               if( ctx->click_state == 0 || ctx->click_state == 3 )
-                       scrollbar->drag = 0;
-       }
-       
-       ui_new_node( ctx );
-       {
-               ui_fill_rect( ctx, ctx->cursor, ctx->colours->background );
-               ui_capture_mouse( ctx, id );
-               
-               ctx->cursor[1] += scrollbar->py;
-               ctx->cursor[3] = scrollbar->bar_height;
-               
-               ui_new_node( ctx );
-               {
-                       ui_capture_mouse( ctx, __COUNTER__ );
-                       struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, ctx->colours->bar );
+struct ui_checkbox
+{
+   int *data;
+};
 
-                       if( ui_hasmouse( ctx ) || scrollbar->drag )
-                       {
-                               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 )
-                               {
-                                       scrollbar->drag = 1;
-                                       scrollbar->drag_offset = scrollbar->py - ctx->mouse[1];
-                               }
-                       }
-               }
-               ui_end_down( ctx );
-       }
-       ui_end( ctx );
+static void ui_slider( ui_ctx *ctx, struct ui_slider *slider )
+{
+   ui_new_node( ctx );
+
+   ui_px slider_start = ctx->cursor[0];
+
+   float const ftotal = ctx->cursor[2],
+               fwidth = ftotal*0.25f,
+               fmove  = ftotal - fwidth,
+               fstart = fwidth*0.5f,
+               frange = slider->max-slider->min,
+               fpos   = (*slider->data - slider->min) / frange;
+
+   ui_fill_rect( ctx, ctx->cursor, 0xff111111 );
+   ctx->cursor[2] = fwidth;
+   ctx->cursor[0] = slider_start + fpos * fmove;
+   
+   u32 uid = ctx->control_id ++;
+   int status = ui_button( ctx );
+   if( ctx->capture_lock && (ctx->capture_mouse_id == uid))
+   {
+      float ui_new = ctx->mouse[0],
+            local  = ui_new - (slider_start + fstart),
+            zo     = vg_clampf(local / fmove,0.0f,1.0f);
+
+      *slider->data = vg_lerpf( slider->min, slider->max, zo );
+   }
+
+   ctx->cursor[0] += 4;
+   ctx->cursor[1] += 4;
+   
+   char buf[12];
+   snprintf( buf, 12, "%.2f", *slider->data );
+   ui_text( ctx, ctx->cursor, buf, 1, 0 );
+   ui_end_down( ctx );
+   ui_end_down( ctx );
 }
 
-static ui_px ui_calculate_content_scroll( struct ui_scrollbar *scrollbar, ui_px content )
+static void ui_slider_vector( ui_ctx *ctx, struct ui_slider_vector *slider )
 {
-       float overlap = vg_maxf( 0.0f, (float)(content - scrollbar->view_height) );
-       
-       float range = scrollbar->view_height - scrollbar->bar_height;
-       return ((float)scrollbar->py / range) * overlap;
+   for( int i=0; i<slider->len; i++ )
+   {
+      slider->sub[i].data = &slider->data[i];
+      slider->sub[i].min = slider->min;
+      slider->sub[i].max = slider->max;
+      ui_slider( ctx, &slider->sub[i] );
+   }
 }
 
-static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image )
+static void ui_checkbox( ui_ctx *ctx, struct ui_checkbox *cb )
 {
-       struct ui_image *img = &ctx->images[ ctx->image_count ++ ];
-       ui_rect_copy( rc, img->rc );
-       img->image = image;
+   if( ui_button(ctx) == k_button_click )
+      *cb->data ^= 0x1;
+   
+   ui_new_node(ctx);
+   ui_rect_pad( ctx->cursor, 4 );
+   if( *cb->data )
+      ui_fill_rect( ctx, ctx->cursor, 0xff00e052 );
+   else
+      ui_fill_rect( ctx, ctx->cursor, 0xff0052e0 );
+
+   ui_end(ctx);
+   ui_end_down(ctx);
 }
 
-// Shortnames
+/* Shortnames */
 #define gui_draw(...) ui_draw( &ui_global_ctx, __VA_ARGS__)
 #define gui_current(...) ui_current( &ui_global_ctx, __VA_ARGS__)
 #define gui_new_node() ui_new_node( &ui_global_ctx )
@@ -886,7 +973,6 @@ static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image )
 #define gui_align_top() ui_align_top( &ui_global_ctx )
 #define gui_align_left() ui_align_left( &ui_global_ctx )
 #define gui_clamp_rect(...) ui_clamp_rect( &ui_global_ctx, __VA_ARGS__)
-#define gui_group_id(...) ui_group_id( &ui_global_ctx, __VA_ARGS__)
 #define gui_capture_mouse(...) ui_capture_mouse( &ui_global_ctx, __VA_ARGS__)
 #define gui_set_clip(...) ui_set_clip( &ui_global_ctx, __VA_ARGS__)
 #define gui_release_clip() ui_release_clip( &ui_global_ctx )
@@ -900,5 +986,6 @@ static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image )
 #define gui_window(...) ui_window( &ui_global_ctx, __VA_ARGS__)
 #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_reset_colours(...) ui_reset_colours( &ui_global_ctx )
+
+#endif /* VG_UI_H */