UI: Mouse coordinate improvements | Meta-program: Enum deserialization support | Transform: Added set_scale convenience procedure

This commit is contained in:
2024-11-19 22:51:04 +01:00
parent 2327b78015
commit 14892d792a
4 changed files with 38 additions and 28 deletions

View File

@@ -9,6 +9,8 @@ UI_Box_Flags :: enum_flags u32 {
CLIP :: 16;
ANIMATE_ON_HOVER :: 32;
PLAY_MODE_FOCUSABLE :: 64;
}
Rect :: struct {
@@ -926,44 +928,47 @@ is_mouse_inside :: (x: float, y: float, rect: Rect) -> bool {
}
ui_update_input :: () {
if engine.mode == .PLAYING return; // @Robustness
//if engine.mode == .PLAYING return; // @Robustness
mouse_x := engine.input.mouse.x;
mouse_y := engine.input.mouse.y;
for *box: ui_state.boxes {
if box.flags & .CLICKABLE {
local_mouse: Vector2;
local_mouse.x = clamp(mouse_x - box.rect.x, 0.0, box.rect.w);
local_mouse.y = clamp(mouse_y - box.rect.y, 0.0, box.rect.h);
box.interaction.local_mouse_coordinates = local_mouse;
box.interaction.normalized_local_mouse_coordinates.x = local_mouse.x / box.rect.w;
box.interaction.normalized_local_mouse_coordinates.y = local_mouse.y / box.rect.h;
if box.interaction.clicked {
box.interaction.clicked = false;
}
if engine.mode == .EDITING || box.flags & .PLAY_MODE_FOCUSABLE {
if box.flags & .CLICKABLE {
local_mouse: Vector2;
local_mouse.x = clamp(mouse_x - box.rect.x, 0.0, box.rect.w);
local_mouse.y = clamp(mouse_y - box.rect.y, 0.0, box.rect.h);
box.interaction.local_mouse_coordinates = local_mouse;
box.interaction.normalized_local_mouse_coordinates.x = local_mouse.x / box.rect.w;
box.interaction.normalized_local_mouse_coordinates.y = local_mouse.y / box.rect.h;
if box.interaction.left_mouse_pressed {
box.interaction.left_mouse_down = false;
if !key_pressed(.MOUSE_LEFT) {
box.interaction.clicked = true;
box.interaction.left_mouse_pressed = false;
if box.interaction.clicked {
box.interaction.clicked = false;
}
} else {
if is_mouse_inside(mouse_x, mouse_y, box.rect) && key_pressed(.MOUSE_LEFT) {
box.interaction.left_mouse_pressed = true;
box.interaction.left_mouse_down = true;
if box.interaction.left_mouse_pressed {
box.interaction.left_mouse_down = false;
if !key_pressed(.MOUSE_LEFT) {
box.interaction.clicked = true;
box.interaction.left_mouse_pressed = false;
}
} else {
if is_mouse_inside(mouse_x, mouse_y, box.rect) && key_pressed(.MOUSE_LEFT) {
box.interaction.left_mouse_pressed = true;
box.interaction.left_mouse_down = true;
}
}
}
}
if box.flags & .ANIMATE_ON_HOVER {
if is_mouse_inside(mouse_x, mouse_y, box.rect) {
box.hover_animation_t += dt * 10;
} else {
box.hover_animation_t -= dt * 10;
if box.flags & .ANIMATE_ON_HOVER {
if is_mouse_inside(mouse_x, mouse_y, box.rect) {
box.hover_animation_t += dt * 10;
} else {
box.hover_animation_t -= dt * 10;
}
box.hover_animation_t = clamp(box.hover_animation_t, 0.0, 1.0);
}
box.hover_animation_t = clamp(box.hover_animation_t, 0.0, 1.0);
}
}
}

View File

@@ -368,7 +368,7 @@ ui_texture :: (texture: Texture_Handle, identifier: s64 = 0, loc := #caller_loca
ui_interactable_texture :: (texture: Texture_Handle, identifier: s64 = 0, loc := #caller_location) -> Interaction_State {
ui_set_next_texture(texture);
ui_set_next_background_color(.{0.1,0.1,0.1,1});
box := ui_box_make(.DRAW_BACKGROUND | .CLICKABLE, get_hash(loc, identifier));
box := ui_box_make(.DRAW_BACKGROUND | .CLICKABLE | .PLAY_MODE_FOCUSABLE, get_hash(loc, identifier));
return box.interaction;
}