option to force audio compression
[vg.git] / vg_imgui.h
index e58977b41f25fa09d57b7bc844d3ea68666b0016..750ebfb866a58f5f3e411c3b52de411ba4b93bd6 100644 (file)
@@ -22,6 +22,7 @@
 
 typedef i16                            ui_px;
 typedef ui_px                          ui_rect[4];
+typedef ui_px           ui_point[2];
 typedef struct ui_vert  ui_vert;
 
 enum ui_axis {
@@ -169,7 +170,8 @@ struct{
             void (*enter)( char *, u32 ),  
                  (*up)( char *, u32 ), 
                  (*down)( char *, u32 ),
-                 (*change)( char *, u32 );
+                 (*change)( char *, u32 ),
+                 (*escape)( void );
          }
          callbacks;
       }
@@ -220,6 +222,8 @@ struct{
    cursor;
 
    SDL_Cursor *cursor_map[ k_ui_cursor_max ];
+
+   v4f colour;
 } 
 static vg_ui = {
    .scheme = {
@@ -258,7 +262,8 @@ static vg_ui = {
       [ k_ui_purple + k_ui_brighter ] = UI_RGB( 0xd3869b ),
       [ k_ui_gray   + k_ui_brighter ] = UI_RGB( 0xa89984 ),
    },
-   .font = &vg_ui_font_small
+   .font = &vg_ui_font_small,
+   .colour = {1.0f,1.0f,1.0f,1.0f}
 };
 
 static struct vg_shader _shader_ui =
@@ -292,6 +297,7 @@ static struct vg_shader _shader_ui =
       .orig_file = NULL,
       .static_src = 
        "uniform sampler2D uTexGlyphs;"
+   "uniform vec4 uColour;"
        "out vec4 FragColor;"
        ""
        "in vec2 aTexCoords;"
@@ -309,7 +315,7 @@ static struct vg_shader _shader_ui =
                        "diffuse.a = texture( uTexGlyphs, aTexCoords ).r;"
                "}"
                
-               "FragColor = diffuse;"
+               "FragColor = diffuse * uColour;"
        "}"
    }
 };
@@ -344,6 +350,7 @@ static struct vg_shader _shader_ui_image = {
       .orig_file = NULL,
       .static_src = 
        "uniform sampler2D uTexImage;"
+   "uniform vec4 uColour;"
        "out vec4 FragColor;"
 
        "in vec2 aTexCoords;"
@@ -353,12 +360,12 @@ static struct vg_shader _shader_ui_image = {
        "void main()"
        "{"
                "vec4 colour = texture( uTexImage, aTexCoords );"
-               "FragColor = colour;"
+               "FragColor = colour * uColour;"
        "}"
    }
 };
 
-VG_STATIC void _vg_ui_init(void){
+static void _vg_ui_init(void){
    if( !vg_shader_compile( &_shader_ui ) ||
        !vg_shader_compile( &_shader_ui_image ) ) 
       vg_fatal_error( "Failed to compile ui shader" );
@@ -475,7 +482,7 @@ static void rect_copy( ui_rect a, ui_rect b ){
       b[i] = a[i];
 }
 
-VG_STATIC void ui_flush( enum ui_shader shader ){
+static void ui_flush( enum ui_shader shader, f32 w, f32 h ){
    u32 vertex_offset = vg_ui.vert_start*sizeof(ui_vert),
        vertex_count  = vg_ui.cur_vert-vg_ui.vert_start,
        vertex_size   = vertex_count*sizeof(ui_vert),
@@ -504,14 +511,16 @@ VG_STATIC void ui_flush( enum ui_shader shader ){
        
        m3x3f view = M3X3_IDENTITY;
    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/(w*0.5f), 
+                           -1.0f/(h*0.5f), 1.0f } );
        
    if( shader == k_ui_shader_colour ){
       glUseProgram( _shader_ui.id );
       
       glUniformMatrix3fv( glGetUniformLocation( _shader_ui.id, "uPv" ), 1, 
                           GL_FALSE, (float *)view );
+      glUniform4fv( glGetUniformLocation( _shader_ui.id, "uColour" ), 1,
+                     vg_ui.colour );
        
       glActiveTexture( GL_TEXTURE0 );
       glBindTexture( GL_TEXTURE_2D, vg_ui.tex_glyphs );
@@ -522,6 +531,8 @@ VG_STATIC void ui_flush( enum ui_shader shader ){
       glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image.id, "uPv" ), 1, 
                           GL_FALSE, (float *)view );
       glUniform1i( glGetUniformLocation(_shader_ui_image.id,"uTexImage"), 0 );
+      glUniform4fv( glGetUniformLocation( _shader_ui_image.id, "uColour" ), 1,
+                     vg_ui.colour );
    }
    else
       vg_fatal_error( "Invalid UI shader (%d)\n", shader );
@@ -850,6 +861,11 @@ static void ui_text_glyph( const struct ui_font *font, ui_px scale,
    out_texcoords[3] = out_texcoords[1] + font->glyph_height;
 }
 
+static u32 ui_opacity( u32 colour, f32 opacity ){
+   u32 alpha = opacity * 255.0f;
+   return (colour & 0x00ffffff) | (alpha << 24);
+}
+
 static u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale, 
                      enum ui_align align, u32 colour ){
        ui_rect text_cursor;
@@ -991,11 +1007,11 @@ static void ui_info( ui_rect inout_panel, const char *text ){
 }
 
 static void ui_image( ui_rect rect, GLuint image ){
-   ui_flush( k_ui_shader_colour );
+   ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
    glActiveTexture( GL_TEXTURE0 );
    glBindTexture( GL_TEXTURE_2D, image );
    ui_fill_rect( rect, 0xffffffff, (ui_px[4]){ 0,256,256,0 } );
-   ui_flush( k_ui_shader_image );
+   ui_flush( k_ui_shader_image, vg.window_x, vg.window_y );
 }
 
 static u32 v4f_u32_colour( v4f colour ){
@@ -1010,6 +1026,8 @@ static u32 v4f_u32_colour( v4f colour ){
 static void ui_defocus_all(void){
    if( vg_ui.focused_control_type == k_ui_control_textbox ){
       SDL_StopTextInput();
+      if( vg_ui.textbox.callbacks.escape )
+         vg_ui.textbox.callbacks.escape();
    }
 
    vg_ui.focused_control_id = NULL;
@@ -1184,7 +1202,7 @@ static void ui_postrender(void){
       vg_ui.wants_mouse = 1;
    }
 
-   ui_flush( k_ui_shader_colour );
+   ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
 
    if( !vg_ui.focused_control_hit ){
       ui_defocus_all();
@@ -1612,6 +1630,8 @@ static void _ui_textbox_cut(void){
 
 static void _ui_textbox_enter(void){
    if( vg_ui.focused_control_type == k_ui_control_textbox ){
+      vg_ui.ignore_input_frames = 2;
+
       if( vg_ui.textbox.callbacks.enter )
          vg_ui.textbox.callbacks.enter( vg_ui.textbuf, vg_ui.textbox.len );
 
@@ -2046,7 +2066,7 @@ static void _ui_proc_key( SDL_Keysym ev ){
 /*
  * Callback for text entry mode
  */
-VG_STATIC void ui_proc_utf8( const char *text ){
+static void ui_proc_utf8( const char *text ){
    if( vg_ui.focused_control_type == k_ui_control_textbox ){
       const char *ptr = text;