IMGUI Slider API
authorhgn <hgodden00@gmail.com>
Wed, 17 Apr 2024 01:01:38 +0000 (02:01 +0100)
committerhgn <hgodden00@gmail.com>
Wed, 17 Apr 2024 01:01:38 +0000 (02:01 +0100)
vg_imgui.c
vg_imgui.h

index 600d11871d5c5391d8a40e28f7890e4d2bec40c5..60bb296bfab0a3d8eb9984b8f3f853d4523aa0d8 100644 (file)
@@ -1301,57 +1301,67 @@ static void ui_enum_post(void){
  * -----------------------------------------------------------------------------
  */
 
-static enum ui_button_state _ui_slider
-      ui_rect box, f32 min, f32 max, f32 *value, const char *format )
+enum ui_button_state ui_slider_base
+      ui_rect box, f32 min, f32 max, f32 *value, f32 *out_t )
 {
-   f32 t;
-
-   enum ui_button_state 
-      mask_using = 
-         k_ui_button_holding_inside |
-         k_ui_button_holding_outside |
-         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 );
+   enum ui_button_state mask_using = 
+                           k_ui_button_holding_inside |
+                           k_ui_button_holding_outside |
+                           k_ui_button_click,
+      state = ui_button_base( box );
 
-   if( state & mask_using ){
-      t = vg_clampf( (f32)(vg_ui.mouse[0] - box[0]) / (f32)( box[2] ),
-                     0.0f, 1.0f );
+   f32 t;
+   if( state & mask_using )
+   {
+      t = vg_clampf( (f32)(vg_ui.mouse[0] - box[0]) / (f32)( box[2] ), 0,1 );
       *value = vg_lerpf( min, max, t );
    }
    else
       t = vg_clampf( (*value - min) / (max-min), 0.0f, 1.0f );
-   
-   ui_rect line     = { box[0], box[1], t * (f32)box[2], box[3] };
-   ui_fill( line, ui_colour(state&mask_brighter? k_ui_bg+4: k_ui_bg+2) );
 
-   ui_outline( box, 1, ui_colour(state? k_ui_fg+3: k_ui_bg+3), 0 );
+   *out_t = t;
+   
+   return state;
+}
 
+void ui_slider_text( ui_rect box, const char *format, f32 value )
+{
    /* TODO: replace this one day with our own function */
    char buf[32];
-   snprintf( buf, sizeof(buf), format? format: "%.2f", *value );
+   snprintf( buf, sizeof(buf), format? format: "%.2f", value );
    ui_text( box, buf, 1, k_ui_align_middle_center, 0 );
-
-   return state;
 }
 
-bool ui_slider( ui_rect inout_panel, const char *str_label, 
-                f32 min, f32 max, f32 *value, const char *format )
+bool ui_slider_standard( ui_rect box, f32 min, f32 max, f32 *value,
+                         const char *format )
 {
-   ui_rect rect, label, box;
-   ui_standard_widget( inout_panel, rect, 1 );
-   ui_label( rect, str_label, k_ui_scale, 0, box );
+   f32 t;
 
    enum ui_button_state mask_using = 
          k_ui_button_holding_inside |
          k_ui_button_holding_outside |
-         k_ui_button_click;
+         k_ui_button_click,
+      mask_brighter = mask_using | k_ui_button_hover,
+      state = ui_slider_base( box, min, max, value, &t );
 
-   if( _ui_slider( box, min, max, value, format ) & mask_using )
-      return 1;
-   else
-      return 0;
+   ui_rect line = { box[0], box[1], t * (f32)box[2], box[3] };
+   ui_fill( line, ui_colour(state&mask_brighter? k_ui_bg+4: k_ui_bg+2) );
+
+   ui_fill( (ui_rect){ box[0]+line[2], box[1], box[2]-line[2], box[3] },
+               ui_colour( k_ui_bg ) );
+   ui_outline( box, 1, ui_colour(state? k_ui_fg+3: k_ui_bg+3), 0 );
+   ui_slider_text( box, NULL, *value );
+
+   return (state & mask_using) && 1;
+}
+
+bool ui_slider( ui_rect inout_panel, const char *str_label, 
+                f32 min, f32 max, f32 *value )
+{
+   ui_rect rect, label, box;
+   ui_standard_widget( inout_panel, rect, 1 );
+   ui_label( rect, str_label, k_ui_scale, 0, box );
+   return ui_slider_standard( box, min, max, value, NULL );
 }
 
 /*
@@ -1376,8 +1386,9 @@ void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value )
 
    enum ui_button_state modified = 0x00;
 
-   for( u32 i=0; i<4; i ++ ){
-      modified |= _ui_slider( sliders[i], 0.0f, 1.0f, hsv+i, 
+   for( u32 i=0; i<4; i ++ )
+   {
+      modified |= ui_slider_standard( sliders[i], 0.0f, 1.0f, hsv+i,
                      (const char *[]){ "hue %.2f",
                                        "sat %.2f",
                                        "lum %.2f",
index cbb0862cc91907a07a6125b1162b49a6bc22299a..7e882aab38317cab119f5944f7da4ab033b6b5f7 100644 (file)
@@ -280,8 +280,11 @@ void ui_postrender(void);
 int ui_checkbox( ui_rect inout_panel, const char *str_label, i32 *data );
 void ui_enum( ui_rect inout_panel, const char *str_label, 
               struct ui_enum_opt *options, u32 len, i32 *value );
+enum ui_button_state ui_slider_base( ui_rect box, f32 min, f32 max, f32 *value, 
+                                     f32 *out_t );
 bool ui_slider( ui_rect inout_panel, const char *str_label, 
-                f32 min, f32 max, f32 *value, const char *format );
+                f32 min, f32 max, f32 *value );
+void ui_slider_text( ui_rect box, const char *format, f32 value );
 void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value );
 int ui_textbox( ui_rect inout_panel, const char *label,
                 char *buf, u32 len, u32 lines, u32 flags,