Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func e_ui_interaction ui_button(const char* text, s_v2 in_pos, s_ui_optional optional = zero)
- {
- assert(text);
- s_v2 size = ui_get_size(optional);
- s_font* font = ui_get_font(optional);
- float z = ui_get_z(optional);
- float font_size = ui_get_font_size(optional);
- s_v2 pos = in_pos;
- if(optional.centered)
- {
- pos -= size / 2;
- }
- s_ui_id id = zero;
- char text_without_id[512] = zero;
- id.id = get_id(text, text_without_id);
- id.layer = ui_get_layer_level(optional);
- s_v4 base_color;
- s_v4 hover_color;
- s_v4 pressed_color;
- s_v4 click_color;
- ui_get_colors(&base_color, &hover_color, &pressed_color, &click_color, optional);
- s_ui_data* data = ui_get_or_create_data(id.id, {.size = size, .color = base_color});
- data->present = true;
- s_v4 target_color = base_color;
- s_v2 target_size = size;
- ui_handle_interaction(id, pos, data->size, false, optional);
- e_ui_interaction result = e_ui_interaction_none;
- data->cooldown_timer -= render_delta;
- b8 in_state_transition = game->state0.next || game->transient.state1.next;
- // @Hack(tkap, 29/10/2022): GIGA HACK ON TOP OF HACKS. We need this because when we are in a state
- // transition, and we click the button, we will make it be "pressed", which means it will not be "hovered",
- // which means it is not drawn as big, which makes the button not look nice.
- if(
- ui->hovered_last_frame.id == id.id ||
- (in_state_transition && ui->pressed_last_frame.id == id.id)
- )
- {
- target_color = hover_color;
- target_size = size * 1.1f;
- data->hover_time = at_most(1.0f, data->hover_time + render_delta);
- result = e_ui_interaction_hovered;
- }
- else
- {
- data->hover_time = at_least(0.0f, data->hover_time - render_delta * 3.5f);
- }
- if(ui->pressed_last_frame.id == id.id)
- {
- if(!in_state_transition)
- {
- target_color = pressed_color;
- target_size = size * 0.95f;
- result = e_ui_interaction_pressed;
- }
- }
- else if(ui->active_last_frame.id == id.id)
- {
- if(data->cooldown_timer <= 0 && !in_state_transition)
- {
- data->color = click_color;
- result = e_ui_interaction_active;
- data->cooldown_timer = optional.cooldown;
- if(!(optional.flags & e_ui_no_sound))
- {
- play_sound(e_sound_ui_interact);
- }
- }
- }
- data->size = lerp_clamp(data->size, target_size, render_delta * 10);
- data->color = lerp_clamp(data->color, target_color, render_delta * 10);
- pos -= (data->size - size) * 0.5f;
- float fitting_font_size = get_fitting_font_size(text_without_id, font, font_size, size.x * 0.9f);
- // @Note(tkap, 14/10/2022): This makes big strings not look very good. Stuttery.
- // Not sure if it is worth it.
- // if(fitting_font_size == font_size)
- {
- fitting_font_size = fitting_font_size * (data->size.x / size.x);
- }
- draw_button(pos, z, data->size, clamp_color(data->color));
- {
- draw_text_with_shadow_sorted(
- text_without_id,
- font,
- get_center(pos, data->size),
- z + 1,
- c_base_text_color,
- fitting_font_size,
- true
- );
- }
- ui_handle_tooltip(optional.tooltip, data->hover_time, platform_shared->mouse, optional);
- if(optional.flags & e_ui_right_arrow)
- {
- s_v2 arrow_size = v2(64, 64);
- s_v2 arrow_pos = in_pos;
- arrow_pos.x -= arrow_size.x * 0.75f;
- arrow_pos.y += size.y / 2;
- arrow_pos.x -= get_tutorial_arrow_motion();
- draw_texture_ui(
- e_texture_atlas,
- arrow_pos,
- z,
- arrow_size,
- WHITE,
- v2i(0, 0),
- {.rotation = -c_pi * 0.5f, .vertex_offset = zero}
- );
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement