From 14892d792a482829b50eadb6cb3f2ba5637b68cc Mon Sep 17 00:00:00 2001 From: Daniel Bross Date: Tue, 19 Nov 2024 22:51:04 +0100 Subject: [PATCH] UI: Mouse coordinate improvements | Meta-program: Enum deserialization support | Transform: Added set_scale convenience procedure --- core/transform.jai | 4 ++++ metaprogram.jai | 1 + ui/ui.jai | 59 +++++++++++++++++++++++++--------------------- ui/widgets.jai | 2 +- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/core/transform.jai b/core/transform.jai index e45f58a..110ac88 100644 --- a/core/transform.jai +++ b/core/transform.jai @@ -152,6 +152,10 @@ set_scale :: (e: *Entity, scale: Vector3, calculate_matrix: bool = true) { set_scale(*e.transform, scale, calculate_matrix); } +set_scale :: (e: *Entity, scale: float, calculate_matrix: bool = true) { + set_scale(*e.transform, scale, calculate_matrix); +} + set_scale :: (transform: *Transform, scale: Vector3, calculate_matrix: bool = true) { transform.scale = scale; if calculate_matrix update_matrix(transform); diff --git a/metaprogram.jai b/metaprogram.jai index ea54f35..6b5e6a0 100644 --- a/metaprogram.jai +++ b/metaprogram.jai @@ -123,6 +123,7 @@ generate_member_deserialization :: (type: *Type_Info_Struct, builder: *String_Bu } case .BOOL; #through; case .FLOAT; #through; + case .ENUM; #through; case .INTEGER; { print_to_builder(builder, "\t\t\t\tcase \"%\";\n", new_path); print_to_builder(builder, "\t\t\t\t\tscan2(values[1], \"\%\", *e.%);\n", new_path); diff --git a/ui/ui.jai b/ui/ui.jai index 9924e7e..a2d441f 100644 --- a/ui/ui.jai +++ b/ui/ui.jai @@ -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); } } } diff --git a/ui/widgets.jai b/ui/widgets.jai index 107863b..c606b07 100644 --- a/ui/widgets.jai +++ b/ui/widgets.jai @@ -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; }