Advertisement
Tkap1

Untitled

Nov 29th, 2023
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. func float ui_scroll_bar(char* text, s_v2 pos, s_v2 size, float current_scroll, float min_scroll, float max_scroll, s_ui_optional optional = zero)
  2. {
  3.     max_scroll += c_world_size.y;
  4.  
  5.     float click_percent = ilerp_clamp(pos.y, pos.y + size.y, platform_shared->mouse.y);
  6.     float scroll_percent = ilerp_clamp(min_scroll, max_scroll, current_scroll);
  7.     float full_height = max_scroll - min_scroll;
  8.     float view_height = size.y;
  9.  
  10.     {
  11.         e_ui_interaction interaction = ui_rect_button(text, pos, {.flags = e_ui_no_dynamic, .layer_override = optional.layer_override, .button_size = size, .base_color = maybe(DARK)});
  12.         if(interaction == e_ui_interaction_pressed)
  13.         {
  14.             // @TODO(tkap, 30/10/2022): repeat
  15.         }
  16.         else if(interaction == e_ui_interaction_active)
  17.         {
  18.             if(click_percent > scroll_percent)
  19.             {
  20.                 current_scroll = at_most(max_scroll, current_scroll + view_height);
  21.             }
  22.             else
  23.             {
  24.                 current_scroll = at_least(min_scroll, current_scroll - view_height);
  25.             }
  26.         }
  27.     }
  28.  
  29.     {
  30.         float percent = view_height / full_height;
  31.         float handle_height = view_height * percent;
  32.  
  33.         s_v2 handle_pos = pos;
  34.         handle_pos.y += current_scroll * percent;
  35.  
  36.         char* handle_text = format_text("%s_handle", text);
  37.         e_ui_interaction interaction = ui_rect_button(handle_text, handle_pos, {.flags = e_ui_no_dynamic, .layer_override = optional.layer_override + 1, .button_size = v2(size.x, handle_height), .base_color = maybe(GRAY)});
  38.         if(interaction == e_ui_interaction_pressed)
  39.         {
  40.             float movement = (platform_shared->mouse.y - platform_shared->last_mouse.y) * (full_height / view_height);
  41.             current_scroll = clamp(current_scroll + movement, min_scroll, max_scroll);
  42.         }
  43.     }
  44.  
  45.     return current_scroll;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement