Make tooltip disappear on mouse leave

This commit is contained in:
Ilia Demianenko 2023-09-07 22:59:42 -07:00 committed by Martin Fouilleul
parent b4797dcd2d
commit 3bcbc76662
2 changed files with 55 additions and 25 deletions

View File

@ -72,12 +72,13 @@ static void oc_update_mouse_move(oc_input_state* state, f32 x, f32 y, f32 deltaX
mouse->wheel = (oc_vec2){ 0, 0 };
mouse->lastUpdate = frameCounter;
}
mouse->posValid = true;
mouse->pos = (oc_vec2){ x, y };
mouse->delta.x += deltaX;
mouse->delta.y += deltaY;
}
static void oc_update_mouse_wheel(oc_input_state* state, f32 deltaX, f32 deltaY)
static void oc_update_mouse_leave(oc_input_state* state)
{
u64 frameCounter = state->frameCounter;
oc_mouse_state* mouse = &state->mouse;
@ -87,6 +88,21 @@ static void oc_update_mouse_wheel(oc_input_state* state, f32 deltaX, f32 deltaY)
mouse->wheel = (oc_vec2){ 0, 0 };
mouse->lastUpdate = frameCounter;
}
mouse->posValid = false;
}
static void oc_update_mouse_wheel(oc_input_state* state, f32 deltaX, f32 deltaY)
{
oc_log_info("wheel");
u64 frameCounter = state->frameCounter;
oc_mouse_state* mouse = &state->mouse;
if(mouse->lastUpdate != frameCounter)
{
mouse->posValid = false;
mouse->delta = (oc_vec2){ 0, 0 };
mouse->wheel = (oc_vec2){ 0, 0 };
mouse->lastUpdate = frameCounter;
}
mouse->wheel.x += deltaX;
mouse->wheel.y += deltaY;
}
@ -143,6 +159,10 @@ void oc_input_process_event(oc_input_state* state, oc_event* event)
oc_update_mouse_move(state, event->mouse.x, event->mouse.y, event->mouse.deltaX, event->mouse.deltaY);
break;
case OC_EVENT_MOUSE_LEAVE:
oc_update_mouse_leave(state);
break;
case OC_EVENT_MOUSE_WHEEL:
oc_update_mouse_wheel(state, event->mouse.deltaX, event->mouse.deltaY);
break;
@ -304,9 +324,16 @@ oc_keymod_flags oc_key_mods(oc_input_state* input)
}
oc_vec2 oc_mouse_position(oc_input_state* input)
{
if(input->mouse.posValid)
{
return (input->mouse.pos);
}
else
{
return ((oc_vec2){ -1, -1 });
}
}
oc_vec2 oc_mouse_delta(oc_input_state* input)
{

View File

@ -1820,7 +1820,6 @@ oc_ui_box* oc_ui_slider(const char* label, f32* value)
trackStyle.floatTarget.c[trackAxis] = 0;
trackStyle.floatTarget.c[secondAxis] = (thumbSize - trackThickness) / 2;
oc_ui_style_mask styleMask = OC_UI_STYLE_SIZE
| OC_UI_STYLE_BG_COLOR
| OC_UI_STYLE_ROUNDNESS;
@ -2196,21 +2195,25 @@ void oc_ui_tooltip(const char* label)
oc_ui_theme* theme = ui->theme;
oc_vec2 p = oc_ui_mouse_position();
oc_ui_container(label, 0)
oc_ui_style containerStyle = { .floating.x = true,
.floating.y = true,
.floatTarget.x = p.x,
.floatTarget.y = p.y };
oc_ui_style_next(&containerStyle, OC_UI_STYLE_FLOAT);
oc_ui_container(label, OC_UI_FLAG_OVERLAY)
{
oc_ui_style arrowStyle = { .size.width = { OC_UI_SIZE_PIXELS, 24 },
.size.height = { OC_UI_SIZE_PIXELS, 24 },
.floating.x = true,
.floating.y = true,
.floatTarget = { p.x, p.y + 5},
.floatTarget = { 0, 5 },
.bgColor = theme->palette->grey7 };
oc_ui_style_mask arrowMask = OC_UI_STYLE_SIZE
| OC_UI_STYLE_FLOAT
| OC_UI_STYLE_BG_COLOR;
oc_ui_style_next(&arrowStyle, arrowMask);
oc_ui_box* arrow = oc_ui_box_make("arrow", OC_UI_FLAG_OVERLAY | OC_UI_FLAG_DRAW_PROC);
oc_ui_box* arrow = oc_ui_box_make("arrow", OC_UI_FLAG_DRAW_PROC);
oc_ui_box_set_draw_proc(arrow, oc_ui_tooltip_arrow_draw, 0);
oc_ui_style contentsStyle = { .size.width = { OC_UI_SIZE_CHILDREN },
@ -2219,7 +2222,7 @@ void oc_ui_tooltip(const char* label)
.layout.margin.y = 8,
.floating.x = true,
.floating.y = true,
.floatTarget = { p.x + 24, p.y },
.floatTarget = { 24, 0 },
.bgColor = theme->palette->grey7,
.color = theme->bg0,
.roundness = 6 };
@ -2231,7 +2234,7 @@ void oc_ui_tooltip(const char* label)
| OC_UI_STYLE_ROUNDNESS;
oc_ui_style_next(&contentsStyle, contentsMask);
oc_ui_box* contents = oc_ui_box_begin("contents", OC_UI_FLAG_OVERLAY | OC_UI_FLAG_DRAW_BACKGROUND);
oc_ui_box* contents = oc_ui_box_begin("contents", OC_UI_FLAG_DRAW_BACKGROUND);
oc_ui_label(label);