modals
authorhgn <hgodden00@gmail.com>
Sat, 10 Jun 2023 13:59:32 +0000 (14:59 +0100)
committerhgn <hgodden00@gmail.com>
Sat, 10 Jun 2023 13:59:32 +0000 (14:59 +0100)
vg.h
vg_imgui.h

diff --git a/vg.h b/vg.h
index e632efae139614bd9ffb0ca95e619ba3983574a1..2a52614b5fc2803c56c0e328edae5e88d580a507 100644 (file)
--- a/vg.h
+++ b/vg.h
@@ -331,7 +331,8 @@ VG_STATIC void _vg_process_events(void)
    SDL_Event event;
    while( SDL_PollEvent( &event ) ){
       if( event.type == SDL_KEYDOWN ){
-         if( vg_console.enabled ){
+         if( vg_console.enabled && 
+               (vg_ui.focused_control_type != k_ui_control_modal) ){
             if( event.key.keysym.sym == SDLK_ESCAPE ||
                 event.key.keysym.sym == SDLK_BACKQUOTE ){
                vg_console.enabled = 0;
index 1d6cbb1139f1e76261f1de68e3a861bb25c6348c..10f93aaac29a893404c243db45ad3bcee333c457 100644 (file)
@@ -83,17 +83,16 @@ typedef u32 ui_scheme[8*4];
 #define UI_TEXTBOX_WRAP      0x2
 #define UI_TEXTBOX_AUTOFOCUS 0x4
 
-#define UI_MODAL_OK  0x1
-#define UI_MODAL_YES 0x2
-#define UI_MODAL_NO  0x4
-#define UI_MODAL_CANCEL 0x8
+#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))
 
-
-
 struct{
    struct ui_vert *vertex_buffer;
    u16            *indice_buffer;
@@ -146,7 +145,7 @@ struct{
       ui_rect rect;
    }
    dropdown;
-
+   };
        struct ui_modal{
                const char *message;
                u32 options;
@@ -155,8 +154,8 @@ struct{
                        void (*close)(u32);
                }
                callbacks;
-       };
-   };
+       }
+   modal;
        
        GLuint tex_glyphs, vao, vbo, ebo;
 
@@ -1092,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 );
 
@@ -1530,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 );
-      }
    }
 }
 
@@ -1646,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 ),
@@ -1818,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
  * -----------------------------------------------------------------------------