imgui API changes (buttons)
[vg.git] / vg_imgui.c
index a4af9aecb3c922005ef90081792be4fc3747cd92..600d11871d5c5391d8a40e28f7890e4d2bec40c5 100644 (file)
@@ -430,17 +430,20 @@ void ui_flush( enum ui_shader shader, f32 w, f32 h ){
    vg_ui.vert_start = vg_ui.cur_vert;
 }
 
-void ui_fill_rect( ui_rect rect, u32 colour, ui_px uv[4] )
+struct ui_vert *ui_fill_rect( ui_rect rect, u32 colour, ui_px uv[4] )
 {
    /* this if far from ideal but stops us from crashing */
    if( (vg_ui.cur_vert + 4 > vg_ui.max_verts) || 
        (vg_ui.cur_indice + 6 > vg_ui.max_indices))
-      return;
+   {
+      return &vg_ui.vertex_buffer[0];
+   }
 
    struct ui_vert *vertices = &vg_ui.vertex_buffer[ vg_ui.cur_vert ];
        u16            *indices  = &vg_ui.indice_buffer[ vg_ui.cur_indice ];
 
-   for( int i=0; i<4; i++ ){
+   for( int i=0; i<4; i++ )
+   {
       vertices[i].colour = colour;
    }
 
@@ -465,17 +468,20 @@ void ui_fill_rect( ui_rect rect, u32 colour, ui_px uv[4] )
        u16 start = vg_ui.cur_vert;
    u32 mesh[] = { 0,2,1, 0,3,2 };
 
-   for( u32 i=0; i<vg_list_size(mesh); i++ ){
+   for( u32 i=0; i<vg_list_size(mesh); i++ )
+   {
       indices[i] = start+mesh[i];
    }
 
        vg_ui.cur_indice += 6;
        vg_ui.cur_vert += 4;
+
+   return vertices;
 }
 
-void ui_fill( ui_rect rect, u32 colour )
+struct ui_vert *ui_fill( ui_rect rect, u32 colour )
 {
-   ui_fill_rect( rect, colour, (ui_px[4]){ 4,4,4,4 } );
+   return ui_fill_rect( rect, colour, (ui_px[4]){ 4,4,4,4 } );
 }
 
 void ui_outline( ui_rect rect, ui_px thickness, u32 colour, u32 mask )
@@ -734,7 +740,8 @@ void ui_prerender(void)
       return;
    }
 
-   if( ui_click_down(UI_MOUSE_LEFT)||ui_click_down(UI_MOUSE_MIDDLE) )
+   if( ui_click_down(UI_MOUSE_LEFT)||ui_click_down(UI_MOUSE_MIDDLE)||
+       ui_click_down(UI_MOUSE_RIGHT) )
    {
       vg_ui.mouse_click[0] = vg_ui.mouse[0];
       vg_ui.mouse_click[1] = vg_ui.mouse[1];
@@ -795,6 +802,8 @@ u32 ui_opacity( u32 colour, f32 opacity )
 u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale, 
               enum ui_align align, u32 colour )
 {
+   ui_px glow_text = 0;
+
        ui_rect text_cursor;
    if( colour == 0 ) colour = ui_colour( k_ui_fg );
 
@@ -850,6 +859,13 @@ u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale,
          ui_rect cursor_clipped;
          if( ui_clip( rect, text_cursor, cursor_clipped ) )
          {
+            if( glow_text )
+            {
+               cursor_clipped[1] += glow_text;
+               ui_fill_rect( cursor_clipped, 0x00ffffff, glyph );
+               cursor_clipped[1] -= glow_text;
+            }
+
             ui_fill_rect( cursor_clipped, colour, glyph );
          }
                }
@@ -893,6 +909,12 @@ u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale,
 
          continue;
                }
+      else if( c == '\x06' )
+      {
+         glow_text = *_c;
+         _c ++;
+         continue;
+      }
       else if( c == '\t' )
       {
          text_cursor[0] += vg_ui.font->sx*scale*4;
@@ -907,10 +929,10 @@ u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale,
    return printed_chars;
 }
 
-void ui_text( ui_rect rect, const char *str, ui_px scale, 
-              enum ui_align align, u32 colour )
+u32 ui_text( ui_rect rect, const char *str, ui_px scale, 
+             enum ui_align align, u32 colour )
 {
-   ui_ntext( rect, str, 1024, scale, align, colour );
+   return ui_ntext( rect, str, 1024, scale, align, colour );
 }
 
 void ui_font_face( vg_font_face *ff )
@@ -976,92 +998,100 @@ void ui_defocus_all(void)
    vg_ui.focused_control_type = k_ui_control_none;
 }
 
-/* TODO: split this out into a formatless button and one that auto fills */
-enum ui_button_state ui_colourbutton( ui_rect rect, 
-                                      enum ui_scheme_colour colour,
-                                      enum ui_scheme_colour hover_colour,
-                                      enum ui_scheme_colour hi_colour,
-                                      bool const fill )
+enum ui_button_state ui_button_base( ui_rect rect )
 {
    int clickup= ui_click_up(UI_MOUSE_LEFT),
        click  = ui_clicking(UI_MOUSE_LEFT) | clickup,
        target = ui_inside_rect( rect, vg_ui.mouse_click ) && click,
        hover  = ui_inside_rect( rect, vg_ui.mouse );
 
-   u32 col_base      = vg_ui.scheme[ colour ],
-       col_highlight = vg_ui.scheme[ hi_colour? hi_colour: k_ui_fg ],
-       col_hover     = vg_ui.scheme[ hover_colour? hover_colour: 
-                                     colour + k_ui_brighter ];
-
-   if( vg_ui.focused_control_type != k_ui_control_none ){
+   if( vg_ui.focused_control_type != k_ui_control_none )
+   {
       clickup = 0;
       click = 0;
       target = 0;
       hover = 0;
    }
 
-   if( hover ){
+   if( hover )
       vg_ui.cursor = k_ui_cursor_hand;
-   }
 
-   if( click ){
-      if( target ){
-         if( hover ){
-            if( clickup ){
+   if( click )
+   {
+      if( target )
+      {
+         if( hover )
+         {
+            if( clickup )
+            {
                vg_ui.ignore_input_frames = 2;
                ui_defocus_all();
-
-               if( fill ) {
-                  ui_fill( rect, col_highlight );
-                  rect_copy( rect, vg_ui.click_fader );
-                  rect_copy( rect, vg_ui.click_fader_end );
-                  vg_ui.click_fader_end[3] = 0;
-                  ui_rect_center( rect, vg_ui.click_fader_end );
-                  vg_ui.click_fade_opacity = 1.0f;
-               }
-
                return k_ui_button_click;
             }
-            else{
-               if( fill ) ui_fill( rect, col_highlight );
-               return k_ui_button_holding_inside;
-            }
-         }
-         else{
-            if( fill ) ui_fill( rect, col_base );
-            ui_outline( rect, 1, col_highlight, 0 );
-            return k_ui_button_holding_outside;
+            else return k_ui_button_holding_inside;
          }
+         else return k_ui_button_holding_outside;
       }
-      else{
-         if( fill ) ui_fill( rect, col_base );
-         return k_ui_button_none;
-      }
+      else return k_ui_button_none;
    }
-   else{
-      if( hover ){
-         if( fill ) ui_fill( rect, col_hover );
-         return k_ui_button_hover;
-      }
-      else{
-         if( fill ) ui_fill( rect, col_base );
-         return k_ui_button_none;
-      }
+   else
+   {
+      if( hover ) return k_ui_button_hover;
+      else return k_ui_button_none;
+   }
+}
+
+/* TODO: split this out into a formatless button and one that auto fills */
+enum ui_button_state ui_colourbutton( ui_rect rect, 
+                                      enum ui_scheme_colour colour,
+                                      enum ui_scheme_colour hover_colour,
+                                      enum ui_scheme_colour hi_colour )
+{
+   enum ui_button_state state = ui_button_base( rect );
+
+   u32 col_base      = vg_ui.scheme[ colour ],
+       col_highlight = vg_ui.scheme[ hi_colour? hi_colour: k_ui_fg ],
+       col_hover     = vg_ui.scheme[ hover_colour? hover_colour: 
+                                     colour + k_ui_brighter ];
+
+   if( state == k_ui_button_click )
+   {
+      ui_fill( rect, col_highlight );
+      rect_copy( rect, vg_ui.click_fader );
+      rect_copy( rect, vg_ui.click_fader_end );
+      vg_ui.click_fader_end[3] = 0;
+      ui_rect_center( rect, vg_ui.click_fader_end );
+      vg_ui.click_fade_opacity = 1.0f;
+   }
+   else if( state == k_ui_button_holding_inside )
+   {
+      ui_fill( rect, col_highlight );
+   }
+   else if( state == k_ui_button_holding_outside )
+   {
+      ui_fill( rect, col_base );
+      ui_outline( rect, 1, col_highlight, 0 );
+   }
+   else if( state == k_ui_button_hover )
+   {
+      ui_fill( rect, col_hover );
    }
+   else ui_fill( rect, col_base );
+
+   return state;
 }
 
 enum ui_button_state ui_colourbutton_text( 
       ui_rect rect, const char *string, ui_px scale,
-      enum ui_scheme_colour colour ){
-   enum ui_button_state state = ui_colourbutton( rect, colour, 0, 0, 1 );
-   ui_rect t = { 0,0, ui_text_line_width( string )*scale, 14*scale };
-   ui_rect_center( rect, t );
+      enum ui_scheme_colour colour )
+{
+   enum ui_button_state state = ui_colourbutton( rect, colour, 0, 0 );
 
    u32 text_colour = ui_colourcont(colour);
    if( state == k_ui_button_holding_inside )
       text_colour = colour;
 
-   ui_text( t, string, scale, k_ui_align_left, text_colour );
+   ui_text( rect, string, scale, k_ui_align_middle_center, text_colour );
    return state;
 }
 
@@ -1177,7 +1207,7 @@ int ui_checkbox( ui_rect inout_panel, const char *str_label, i32 *data )
    ui_split( rect, k_ui_axis_v, -rect[3], 0, label, box );
    ui_text( label, str_label, k_ui_scale, k_ui_align_middle_left, 0 );
 
-   int changed = ui_colourbutton( box, k_ui_bg, 0, 0, 1 )==1;
+   int changed = ui_colourbutton( box, k_ui_bg, 0, 0 ) == k_ui_button_click;
    if( changed )
       *data = (*data) ^ 0x1;
 
@@ -1283,7 +1313,7 @@ static enum ui_button_state _ui_slider(
          k_ui_button_click,
       mask_brighter =
          mask_using | k_ui_button_hover,
-      state = ui_colourbutton( box, k_ui_bg, k_ui_bg+2, k_ui_bg+3, 1 );
+      state = ui_colourbutton( box, k_ui_bg, k_ui_bg+2, k_ui_bg+3 );
 
    if( state & mask_using ){
       t = vg_clampf( (f32)(vg_ui.mouse[0] - box[0]) / (f32)( box[2] ),
@@ -1357,7 +1387,7 @@ void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value )
    ui_rect preview, square;
    ui_split_ratio( left, k_ui_axis_v, 0.8f, 8, square, preview );
 
-   u32 state = ui_colourbutton( square, 0, 0, 0, 0 );
+   u32 state = ui_button_base( square );
    modified |= state;
 
    enum ui_button_state 
@@ -1366,7 +1396,8 @@ void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value )
          k_ui_button_holding_outside |
          k_ui_button_click;
    
-   if( state & mask_using ){
+   if( state & mask_using )
+   {
       for( u32 i=0; i<2; i ++ ){
          hsv[1+i] = vg_clampf( 
                         (f32)(vg_ui.mouse[i] - square[i]) / (f32)(square[2+i]),
@@ -1377,7 +1408,8 @@ void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value )
    }
 
    if( modified & (k_ui_button_click|k_ui_button_holding_inside|
-                   k_ui_button_holding_outside) ){
+                   k_ui_button_holding_outside) )
+   {
       vg_hsv_rgb( hsv, value );
       value[3] = hsv[3];
    }
@@ -1416,7 +1448,8 @@ void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value )
  * -----------------------------------------------------------------------------
  */
 
-static void _ui_textbox_make_selection( int *start, int *end ){
+static void _ui_textbox_make_selection( int *start, int *end )
+{
        *start = VG_MIN( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
        *end   = VG_MAX( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
 }
@@ -1434,7 +1467,8 @@ void _ui_textbox_move_cursor( int *cursor0, int *cursor1,
                *cursor1 = *cursor0;
 }
 
-static int _ui_textbox_makeroom( int datastart, int length ){
+static int _ui_textbox_makeroom( int datastart, int length )
+{
        int move_to = VG_MIN( datastart+length, vg_ui.textbox.len-1 );
        int move_amount = strlen( vg_ui.textbuf )-datastart;
        int move_end = VG_MIN( move_to+move_amount, vg_ui.textbox.len-1 );
@@ -1473,7 +1507,8 @@ int _ui_textbox_delete_char( int direction )
        return start;
 }
 
-static void _ui_textbox_to_clipboard(void){
+static void _ui_textbox_to_clipboard(void)
+{
        int start, end;
        _ui_textbox_make_selection( &start, &end );
        char buffer[512];
@@ -1485,7 +1520,8 @@ static void _ui_textbox_to_clipboard(void){
        }
 }
 
-static void _ui_textbox_change_callback(void){
+static void _ui_textbox_change_callback(void)
+{
    if( vg_ui.textbox.callbacks.change ){
       vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
 
@@ -1497,7 +1533,8 @@ static void _ui_textbox_change_callback(void){
 }
 
 void ui_start_modal( const char *message, u32 options );
-static void _ui_textbox_clipboard_paste(void){
+static void _ui_textbox_clipboard_paste(void)
+{
    if( !SDL_HasClipboardText() )
       return;