new build method
[vg.git] / src / vg / vg_ui.h
index f36d3630dc7de62e3abb956df39616c8b995f2a9..917b5e657c3c3b9611335a4b830fead767040bcd 100644 (file)
@@ -3,8 +3,14 @@
 #ifndef VG_UI_H
 #define VG_UI_H
 
-SHADER_DEFINE( shader_ui,
-
+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 +31,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 +66,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 +123,8 @@ struct ui_ctx
        }
        *verts;
        #pragma pack(pop)
+
+   u32 control_id;
        
        u32 num_verts;
        u16 *indices;
@@ -125,8 +135,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];
@@ -260,7 +269,7 @@ static void ui_default_init(void)
    
    free( image );
        
-       SHADER_INIT( shader_ui );
+   vg_shader_register( &_shader_ui );
        ui_init_context( &ui_global_ctx, 20000 );
 }
 
@@ -298,7 +307,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 +320,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) );
@@ -454,21 +464,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;
        }
 }
 
@@ -719,11 +722,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) )
                {
@@ -748,8 +753,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 +770,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 +792,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 +829,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 +927,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 )