medium sized dollop
[vg.git] / src / vg / vg_ui.h
index f36d3630dc7de62e3abb956df39616c8b995f2a9..844194e73867e9e468c70d9b9fcb732d983e9e2d 100644 (file)
@@ -1,10 +1,24 @@
 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
 
+/*
+ * TODO: Get rid of many context design
+ */
+
 #ifndef VG_UI_H
 #define VG_UI_H
 
-SHADER_DEFINE( shader_ui,
+#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;"
@@ -25,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;"
        ""
@@ -57,9 +74,8 @@ SHADER_DEFINE( shader_ui,
                
                "FragColor = vec4( aColour.rgb, glyph.a*clip_blend );"
        "}"
-       ,
-       UNIFORMS({ "uPv", "uTexGlyphs" })
-)
+   }
+};
 
 typedef i16                            ui_px;
 typedef u32                            ui_colour;
@@ -115,6 +131,8 @@ struct ui_ctx
        }
        *verts;
        #pragma pack(pop)
+
+   u32 control_id;
        
        u32 num_verts;
        u16 *indices;
@@ -125,8 +143,7 @@ struct ui_ctx
        u32 stack_count;
        u32 capture_mouse_id;
        int capture_lock;
-       u32 id_base;
-       int glyph_base;
+
        
        /* User input */
        ui_px mouse[2];
@@ -158,62 +175,6 @@ static ui_colourset ui_default_colours = {
 
 static ui_ctx ui_global_ctx;
 
-static void ui_init_context( ui_ctx *ctx, int index_buffer_size )
-{
-       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 );
-       }
-       
-       /* 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;
-       }
-}
-
 static void ui_context_free( ui_ctx *ctx )
 {
        glDeleteVertexArrays( 1, &ctx->vao );
@@ -224,7 +185,88 @@ static void ui_context_free( ui_ctx *ctx )
        free( ctx->indices );
 }
 
-static void ui_default_init(void)
+static int ui_init_context( ui_ctx *ctx, int index_buffer_size )
+{
+       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 );
+
+   if( VG_CHECK_GL_ERR() )
+      goto il_fail_alloc_gpu;
+
+   /* 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 );
+   
+   if( VG_CHECK_GL_ERR() )
+      goto il_fail_attributes;
+       
+
+       /* Alloc RAM default context */
+   ctx->verts = (struct ui_vert *)malloc( 
+         vertex_buffer_size * sizeof(struct ui_vert) );
+   if( !ctx->verts )
+      goto il_fail_alloc_verts;
+
+   ctx->indices = (u16*)malloc( index_buffer_size * sizeof(u16) );
+   if( !ctx->indices )
+      goto il_fail_alloc_indices;
+
+   if( !ctx->colours )
+      ctx->colours = &ui_default_colours;
+
+   return 1;
+
+                           free( ctx->indices );
+                           ctx->indices = NULL;
+il_fail_alloc_indices:
+                           free( ctx->verts );
+                           ctx->verts = NULL;
+il_fail_alloc_verts:
+il_fail_attributes:
+il_fail_alloc_gpu:
+                           glDeleteBuffers( 1, &ctx->ebo );
+                           glDeleteBuffers( 1, &ctx->vbo );
+                           glDeleteVertexArrays( 1, &ctx->vao );
+                           VG_CHECK_GL_ERR();
+                           return 0;
+}
+
+static int ui_default_init(void)
 {
        /* Load default font */
    u32 compressed[] = {
@@ -233,6 +275,7 @@ static void ui_default_init(void)
 
    u32 pixels = 0, total = 256*256, data = 0;
    u8 *image = malloc( total );
+   if( !image ) goto il_fail_alloc_image;
    
    while( pixels < total )
    {
@@ -250,22 +293,39 @@ static void ui_default_init(void)
    }
    
    glGenTextures( 1, &ui_glyph_texture );
+   if( !ui_glyph_texture ) goto il_fail_gen_texture;
+
    glBindTexture( GL_TEXTURE_2D, ui_glyph_texture );
-   
    glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0, 
          GL_RED, GL_UNSIGNED_BYTE, image );
+
+   if( VG_CHECK_GL_ERR() ) goto il_fail_alloc_gpu;
    
    vg_tex2d_clamp();
    vg_tex2d_nearest();
    
    free( image );
        
-       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_fail_alloc_gpu:
+il_generic_fail:
+   glDeleteTextures( 1, &ui_glyph_texture );
+
+il_fail_gen_texture:
+   free( image );
+
+il_fail_alloc_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 );
 }
@@ -298,7 +358,7 @@ static void ui_draw( ui_ctx *ctx, m3x3f view_override )
        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;
        
@@ -311,12 +371,13 @@ static void ui_draw( ui_ctx *ctx, m3x3f view_override )
                               -1.0f/((float)vg_window_y*0.5f), 1.0f } );
        }
        
-       glUniformMatrix3fv( SHADER_UNIFORM( shader_ui, "uPv" ), 1, 
+   /* 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) );
@@ -364,7 +425,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++ ];
@@ -454,21 +518,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;
        }
 }
 
@@ -683,7 +740,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 )
        {
@@ -719,11 +779,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) )
                {
@@ -738,6 +800,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 );
@@ -748,8 +812,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];
@@ -767,13 +829,13 @@ 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 */
                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 );
                        
@@ -789,7 +851,7 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group )
                        ui_align_top( ctx );
                        ui_rect_pad( ctx->cursor, 4 );
                        
-                       if( ui_button( ctx, __COUNTER__ ) )
+                       if( ui_button( ctx ) )
                        {
                                vg_info( "Click clacked\n" );
                        }
@@ -826,6 +888,89 @@ static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image )
        img->image = image;
 }
 
+struct ui_slider
+{
+   float *data;
+   float min, max;
+};
+
+struct ui_slider_vector
+{
+   float *data, min, max;
+   struct ui_slider sub[4];
+   u32 len;
+};
+
+struct ui_checkbox
+{
+   int *data;
+};
+
+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 void ui_slider_vector( ui_ctx *ctx, struct ui_slider_vector *slider )
+{
+   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_checkbox( ui_ctx *ctx, struct ui_checkbox *cb )
+{
+   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 */
 #define gui_draw(...) ui_draw( &ui_global_ctx, __VA_ARGS__)
 #define gui_current(...) ui_current( &ui_global_ctx, __VA_ARGS__)
@@ -841,7 +986,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 )
@@ -857,4 +1001,4 @@ static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image )
 #define gui_push_image(...) ui_push_image( &ui_global_ctx, __VA_ARGS__ )
 #define gui_reset_colours(...) ui_reset_colours( &ui_global_ctx )
 
-#endif
+#endif /* VG_UI_H */