Advertisement
Tkap1

Untitled

Dec 4th, 2023
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. func e_ui_interaction ui_button(const char* text, s_v2 in_pos, s_ui_optional optional = zero)
  2. {
  3.     assert(text);
  4.  
  5.     s_v2 size = ui_get_size(optional);
  6.     s_font* font = ui_get_font(optional);
  7.     float z = ui_get_z(optional);
  8.     float font_size = ui_get_font_size(optional);
  9.  
  10.     s_v2 pos = in_pos;
  11.     if(optional.centered)
  12.     {
  13.         pos -= size / 2;
  14.     }
  15.  
  16.     s_ui_id id = zero;
  17.     char text_without_id[512] = zero;
  18.     id.id = get_id(text, text_without_id);
  19.     id.layer = ui_get_layer_level(optional);
  20.  
  21.     s_v4 base_color;
  22.     s_v4 hover_color;
  23.     s_v4 pressed_color;
  24.     s_v4 click_color;
  25.     ui_get_colors(&base_color, &hover_color, &pressed_color, &click_color, optional);
  26.  
  27.     s_ui_data* data = ui_get_or_create_data(id.id, {.size = size, .color = base_color});
  28.     data->present = true;
  29.  
  30.     s_v4 target_color = base_color;
  31.     s_v2 target_size = size;
  32.  
  33.     ui_handle_interaction(id, pos, data->size, false, optional);
  34.  
  35.     e_ui_interaction result = e_ui_interaction_none;
  36.  
  37.     data->cooldown_timer -= render_delta;
  38.  
  39.     b8 in_state_transition = game->state0.next || game->transient.state1.next;
  40.  
  41.     // @Hack(tkap, 29/10/2022): GIGA HACK ON TOP OF HACKS. We need this because when we are in a state
  42.     // transition, and we click the button, we will make it be "pressed", which means it will not be "hovered",
  43.     // which means it is not drawn as big, which makes the button not look nice.
  44.     if(
  45.         ui->hovered_last_frame.id == id.id ||
  46.         (in_state_transition && ui->pressed_last_frame.id == id.id)
  47.     )
  48.     {
  49.         target_color = hover_color;
  50.         target_size = size * 1.1f;
  51.         data->hover_time = at_most(1.0f, data->hover_time + render_delta);
  52.         result = e_ui_interaction_hovered;
  53.     }
  54.  
  55.     else
  56.     {
  57.         data->hover_time = at_least(0.0f, data->hover_time - render_delta * 3.5f);
  58.     }
  59.  
  60.     if(ui->pressed_last_frame.id == id.id)
  61.     {
  62.         if(!in_state_transition)
  63.         {
  64.             target_color = pressed_color;
  65.             target_size = size * 0.95f;
  66.             result = e_ui_interaction_pressed;
  67.         }
  68.     }
  69.  
  70.     else if(ui->active_last_frame.id == id.id)
  71.     {
  72.         if(data->cooldown_timer <= 0 && !in_state_transition)
  73.         {
  74.             data->color = click_color;
  75.             result = e_ui_interaction_active;
  76.             data->cooldown_timer = optional.cooldown;
  77.  
  78.             if(!(optional.flags & e_ui_no_sound))
  79.             {
  80.                 play_sound(e_sound_ui_interact);
  81.             }
  82.         }
  83.     }
  84.  
  85.     data->size = lerp_clamp(data->size, target_size, render_delta * 10);
  86.     data->color = lerp_clamp(data->color, target_color, render_delta * 10);
  87.  
  88.     pos -= (data->size - size) * 0.5f;
  89.  
  90.     float fitting_font_size = get_fitting_font_size(text_without_id, font, font_size, size.x * 0.9f);
  91.  
  92.     // @Note(tkap, 14/10/2022): This makes big strings not look very good. Stuttery.
  93.     // Not sure if it is worth it.
  94.     // if(fitting_font_size == font_size)
  95.     {
  96.         fitting_font_size = fitting_font_size * (data->size.x / size.x);
  97.     }
  98.  
  99.     draw_button(pos, z, data->size, clamp_color(data->color));
  100.  
  101.     {
  102.         draw_text_with_shadow_sorted(
  103.             text_without_id,
  104.             font,
  105.             get_center(pos, data->size),
  106.             z + 1,
  107.             c_base_text_color,
  108.             fitting_font_size,
  109.             true
  110.         );
  111.     }
  112.  
  113.     ui_handle_tooltip(optional.tooltip, data->hover_time, platform_shared->mouse, optional);
  114.  
  115.     if(optional.flags & e_ui_right_arrow)
  116.     {
  117.         s_v2 arrow_size = v2(64, 64);
  118.         s_v2 arrow_pos = in_pos;
  119.         arrow_pos.x -= arrow_size.x * 0.75f;
  120.         arrow_pos.y += size.y / 2;
  121.  
  122.         arrow_pos.x -= get_tutorial_arrow_motion();
  123.  
  124.         draw_texture_ui(
  125.             e_texture_atlas,
  126.             arrow_pos,
  127.             z,
  128.             arrow_size,
  129.             WHITE,
  130.             v2i(0, 0),
  131.             {.rotation = -c_pi * 0.5f, .vertex_offset = zero}
  132.         );
  133.     }
  134.  
  135.  
  136.     return result;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement