add slider to imgui
[vg.git] / vg_imgui.h
index c6553e61c0b24352636de4c3ba6619ee3f61a724..19f4d78293980d65f099f5276cef743c939300bc 100644 (file)
@@ -1063,23 +1063,26 @@ static void ui_defocus_all(void){
 }
 
 enum ui_button_state {
-   k_ui_button_none  0,
-   k_ui_button_click 1,
-   k_ui_button_holding_inside 2,
-   k_ui_button_holding_outside = 3,
-   k_ui_button_hover = 4
+   k_ui_button_none            = 0x0,
+   k_ui_button_click           = 0x1,
+   k_ui_button_holding_inside  = 0x2,
+   k_ui_button_holding_outside = 0x4,
+   k_ui_button_hover           = 0x8
 };
 
 static enum ui_button_state ui_colourbutton( ui_rect rect, 
-                                             enum ui_scheme_colour colour ){
+                                             enum ui_scheme_colour colour,
+                                             enum ui_scheme_colour hover_colour,
+                                             enum ui_scheme_colour hi_colour ){
    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[ k_ui_fg ],
-       col_hover     = vg_ui.scheme[ colour + k_ui_brighter ];
+       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 ){
       clickup = 0;
@@ -1139,7 +1142,7 @@ static enum ui_button_state ui_colourbutton( ui_rect rect,
 static 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 );
+   enum ui_button_state state = ui_colourbutton( rect, colour, 0, 0 );
    ui_rect t = { 0,0, ui_text_line_width( string )*scale, 14*scale };
    ui_rect_center( rect, t );
 
@@ -1260,7 +1263,7 @@ static 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 )==1;
+   int changed = ui_colourbutton( box, k_ui_bg, 0, 0 )==1;
    if( changed )
       *data = (*data) ^ 0x1;
 
@@ -1348,6 +1351,47 @@ static void ui_enum_post(void){
       vg_ui.focused_control_hit = 1;
 }
 
+/*
+ * Slider
+ * -----------------------------------------------------------------------------
+ */
+
+static void ui_slider( ui_rect inout_panel, const char *str_label, 
+                       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,
+      mask_brighter =
+         mask_using | k_ui_button_hover,
+      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] ),
+                     0.0f, 1.0f );
+      *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 );
+
+   /* TODO: replace this one day with our own function */
+   char buf[32];
+   snprintf( buf, sizeof(buf), format? format: "%.2f", *value );
+   ui_text( box, buf, 1, k_ui_align_middle_center, 0 );
+}
+
 /*
  * Textbox chaos
  * -----------------------------------------------------------------------------
@@ -1420,6 +1464,17 @@ static void _ui_textbox_to_clipboard(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 );
+
+      /* we gave permission to modify the buffer in this callback so.. */
+      int len = strlen( vg_ui.textbuf );
+      vg_ui.textbox.cursor_user = VG_MIN( vg_ui.textbox.cursor_user, len );
+      vg_ui.textbox.cursor_pos  = VG_MIN( vg_ui.textbox.cursor_pos,  len );
+   }
+}
+
 static void ui_start_modal( const char *message, u32 options );
 static void _ui_textbox_clipboard_paste(void){
    if( !SDL_HasClipboardText() )
@@ -1444,6 +1499,7 @@ static void _ui_textbox_clipboard_paste(void){
        _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, 
                             &vg_ui.textbox.cursor_pos, cpylength, 1 );
    SDL_free( text );
+   _ui_textbox_change_callback();
 }
 
 static void _ui_textbox_put_char( char c ){
@@ -1582,10 +1638,7 @@ static void _ui_textbox_backspace(void){
    if( vg_ui.focused_control_type == k_ui_control_textbox ){
       vg_ui.textbox.cursor_user = _ui_textbox_delete_char( -1 );
       vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
-
-      if( vg_ui.textbox.callbacks.change ){
-         vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
-      }
+      _ui_textbox_change_callback();
    }
 }
 
@@ -1593,10 +1646,7 @@ static void _ui_textbox_delete(void){
    if( vg_ui.focused_control_type == k_ui_control_textbox ){
       vg_ui.textbox.cursor_user = _ui_textbox_delete_char( 1 );
       vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
-
-      if( vg_ui.textbox.callbacks.change ){
-         vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
-      }
+      _ui_textbox_change_callback();
    }
 }
 
@@ -1653,6 +1703,7 @@ static void _ui_textbox_cut(void){
    _ui_textbox_to_clipboard();
    vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
    vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
+   _ui_textbox_change_callback();
 }
 
 static void _ui_textbox_enter(void){
@@ -1664,8 +1715,10 @@ static void _ui_textbox_enter(void){
 
       if( vg_ui.focused_control_type != k_ui_control_textbox ) return;
 
-      if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE )
+      if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
          _ui_textbox_put_char( '\n' );
+         _ui_textbox_change_callback();
+      }
       else{
          if( !(vg_ui.textbox.flags & UI_TEXTBOX_AUTOFOCUS ) )
             ui_defocus_all();
@@ -2102,9 +2155,7 @@ static void ui_proc_utf8( const char *text ){
          ptr ++;
       }
 
-      if( vg_ui.textbox.callbacks.change ){
-         vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
-      }
+      _ui_textbox_change_callback();
    }
 }