logger update
[vg.git] / vg_imgui.h
index 7de96f1e5e8dc81f954a7c678864da9163c4f406..10f93aaac29a893404c243db45ad3bcee333c457 100644 (file)
@@ -82,6 +82,13 @@ typedef u32 ui_scheme[8*4];
 #define UI_TEXTBOX_MULTILINE 0x1
 #define UI_TEXTBOX_WRAP      0x2
 #define UI_TEXTBOX_AUTOFOCUS 0x4
+
+#define UI_MODAL_OK        0x0
+#define UI_MODAL_GOOD      0x1
+#define UI_MODAL_BAD       0x2
+#define UI_MODAL_WARN      0x3
+#define UI_MODAL_TYPE_BITS 0x3
+
 #define UI_MOUSE_LEFT   (SDL_BUTTON(SDL_BUTTON_LEFT))
 #define UI_MOUSE_RIGHT  (SDL_BUTTON(SDL_BUTTON_RIGHT))
 #define UI_MOUSE_MIDDLE (SDL_BUTTON(SDL_BUTTON_MIDDLE))
@@ -107,7 +114,8 @@ struct{
    enum ui_control_type{
       k_ui_control_none,
       k_ui_control_textbox,
-      k_ui_control_dropdown
+      k_ui_control_dropdown,
+               k_ui_control_modal
    }
    focused_control_type;
 
@@ -138,6 +146,16 @@ struct{
    }
    dropdown;
    };
+       struct ui_modal{
+               const char *message;
+               u32 options;
+               
+               struct ui_modal_callbacks{
+                       void (*close)(u32);
+               }
+               callbacks;
+       }
+   modal;
        
        GLuint tex_glyphs, vao, vbo, ebo;
 
@@ -1073,6 +1091,41 @@ static void ui_postrender(void)
    if( vg_ui.focused_control_type == k_ui_control_dropdown ){
       ui_enum_post();
    }
+   else if( vg_ui.focused_control_type == k_ui_control_modal ){
+      ui_rect screen = {0,0,vg.window_x,vg.window_y};
+      ui_fill( screen, 0xa0000000 );
+      ui_rect box = {0,0,400,200};
+
+      u32 colour = ui_colour(k_ui_fg),
+          type = vg_ui.modal.options & UI_MODAL_TYPE_BITS;
+      if     ( type == 1 ) colour = ui_colour(k_ui_green);
+      else if( type == 2 ) colour = ui_colour(k_ui_red);
+      else if( type == 3 ) colour = ui_colour(k_ui_yellow);
+
+      ui_rect_center( screen, box );
+      ui_fill( box, ui_colour(k_ui_bg) );
+      ui_outline( box, -1, colour );
+
+      ui_rect message;
+      rect_copy( box, message );
+      message[3] = 100;
+      ui_rect_center( box, message );
+      
+      ui_rect row0, row1, btn;
+      ui_split_ratio( message, k_ui_axis_h, 0.5f, 0, row0, row1 );
+      ui_text( row0, vg_ui.modal.message, 1, k_ui_align_middle_center, colour );
+
+      rect_copy( row1, btn );
+      btn[2] = 86;
+      btn[3] = 28;
+      ui_rect_center( row1, btn );
+      
+      vg_ui.focused_control_type = k_ui_control_none; /* HACK */
+      if( !ui_button_text( btn, "OK", 1 ) )
+         vg_ui.focused_control_hit = 1;
+      vg_ui.focused_control_type = k_ui_control_modal; /* HACK */
+      vg_ui.wants_mouse = 1;
+   }
 
    ui_flush( k_ui_shader_colour );
 
@@ -1511,17 +1564,17 @@ static void _ui_textbox_cut(void)
 static void _ui_textbox_enter(void)
 {
    if( vg_ui.focused_control_type == k_ui_control_textbox ){
-      if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
+      if( vg_ui.textbox.callbacks.enter )
+         vg_ui.textbox.callbacks.enter( vg_ui.textbuf, vg_ui.textbox.len );
+
+      if( vg_ui.focused_control_type != k_ui_control_textbox ) return;
+
+      if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE )
          _ui_textbox_put_char( '\n' );
-      }
       else{
          if( !(vg_ui.textbox.flags & UI_TEXTBOX_AUTOFOCUS ) )
             ui_defocus_all();
       }
-
-      if( vg_ui.textbox.callbacks.enter ){
-         vg_ui.textbox.callbacks.enter( vg_ui.textbuf, vg_ui.textbox.len );
-      }
    }
 }
 
@@ -1627,6 +1680,7 @@ static int ui_textbox( ui_rect rect, char *buf, u32 len, u32 flags,
       click = 0;
       target = 0;
       hover = 0;
+      flags &= ~UI_TEXTBOX_AUTOFOCUS;
    }
 
    u32 col_base      = ui_colour( k_ui_bg ),
@@ -1799,6 +1853,23 @@ static int ui_textbox( ui_rect rect, char *buf, u32 len, u32 flags,
    return 0;
 }
 
+/* 
+ * Modal UI
+ * -----------------------------------------------------------------------------
+ */
+static void ui_start_modal( const char *message, u32 options ){
+   vg_ui.focused_control_type = k_ui_control_modal;
+   vg_ui.modal.message = message;
+   vg_ui.modal.callbacks.close = NULL;
+   vg_ui.modal.options = options;
+   
+   u32 type = options & UI_MODAL_TYPE_BITS;
+   if(      type == UI_MODAL_OK )   vg_info( message );
+   else if( type == UI_MODAL_WARN ) vg_warn( message );
+   else if( type == UI_MODAL_GOOD ) vg_success( message );
+   else if( type == UI_MODAL_BAD )  vg_error( message );
+}
+
 /*
  * Input handling
  * -----------------------------------------------------------------------------
@@ -1896,7 +1967,7 @@ static void ui_label( ui_rect rect, const char *text, ui_px size,
                       ui_px gap, ui_rect r )
 {
    ui_rect l;
-   ui_px width = (ui_text_line_width( text )+1) * size;
+   ui_px width = (ui_text_line_width(text)+UI_GLYPH_SPACING_X) * size;
    ui_split( rect, k_ui_axis_v, width, gap, l, r );
    ui_text( l, text, 1, k_ui_align_middle_left, 0 );
 }