Moving corners like intended

This commit is contained in:
Synthasmagoria 2026-08-25 18:38:56 +02:00
commit 6bd838968d
4 changed files with 114 additions and 19 deletions

View file

@ -184,11 +184,22 @@ ProgramAnimationState :: enum {
MouseDraggableType :: enum {
Point,
RectangleCorner,
}
MouseDraggable :: struct {
data: rawptr,
type: MouseDraggableType,
MouseDraggable_Point :: struct {
ref: ^v2,
}
MouseDraggable_RectangleCornerData :: struct {
corner: RectangleCornersCorner,
corners_ref: ^RectangleCorners,
corner_ref: ^v2,
}
MouseDraggable :: union {
MouseDraggable_Point,
MouseDraggable_RectangleCornerData,
}
Program :: struct {
@ -419,10 +430,22 @@ program_update :: proc() {
switch animation.type {
case .Rectangle:
append(&program.mouse_draggables, MouseDraggable{&animation.shapes.rectangle.tl, .Point})
append(&program.mouse_draggables, MouseDraggable{&animation.shapes.rectangle.tr, .Point})
append(&program.mouse_draggables, MouseDraggable{&animation.shapes.rectangle.bl, .Point})
append(&program.mouse_draggables, MouseDraggable{&animation.shapes.rectangle.br, .Point})
append(&program.mouse_draggables, MouseDraggable_RectangleCornerData{
corner = .Tl,
corners_ref = &animation.shapes.rectangle,
corner_ref = &animation.shapes.rectangle.tl})
append(&program.mouse_draggables, MouseDraggable_RectangleCornerData{
corner = .Tr,
corners_ref = &animation.shapes.rectangle,
corner_ref = &animation.shapes.rectangle.tr})
append(&program.mouse_draggables, MouseDraggable_RectangleCornerData{
corner = .Bl,
corners_ref = &animation.shapes.rectangle,
corner_ref = &animation.shapes.rectangle.bl})
append(&program.mouse_draggables, MouseDraggable_RectangleCornerData{
corner = .Br,
corners_ref = &animation.shapes.rectangle,
corner_ref = &animation.shapes.rectangle.br})
}
}
@ -564,6 +587,11 @@ program_build_workspace_ui :: proc(ctx: ^ui.Context) ->
ui.same_line()
ui.element(.Button, button_dimensions, button_config, button_data)
divider_dimensions := ui.Dimensions{ui.Size_Pixels(4.0), ui.Size_Percentage(1.0)}
divider_config := ui.ElementConfiguration{margin = 8.0, color = ui.col(rl.WHITE)}
ui.same_line()
ui.element(.Divider, divider_dimensions, divider_config)
play_button_label: cstring
button_data = new(UiElementData_Button, context.temp_allocator)
switch program.animation_state {
@ -611,10 +639,12 @@ program_handle_mouse :: proc(mouse_state: MouseState, top_bar_container, matrix_
grid_to_world_matrix := linalg.inverse(world_to_grid_matrix)
for draggable in program.mouse_draggables {
switch draggable.type {
case .Point:
position := cast(^v2)draggable.data
position_world := (v3{**position^, 1.0} * grid_to_world_matrix).xy
switch draggable in draggable {
case MouseDraggable_Point:
position_world := (v3{**draggable.ref^, 1.0} * grid_to_world_matrix).xy
rl.DrawCircleV(position_world, program.mouse_draggable_radius, ANIMATION_COLOR_ORIGIN)
case MouseDraggable_RectangleCornerData:
position_world := (v3{**draggable.corner_ref^, 1.0} * grid_to_world_matrix).xy
rl.DrawCircleV(position_world, program.mouse_draggable_radius, ANIMATION_COLOR_ORIGIN)
}
}
@ -629,10 +659,15 @@ program_handle_mouse :: proc(mouse_state: MouseState, top_bar_container, matrix_
}
if rl.IsMouseButtonPressed(.LEFT) {
for draggable in program.mouse_draggables {
switch draggable.type {
case .Point:
position := cast(^v2)draggable.data
circle_position := (v3{**position^, 1.0} * grid_to_world_matrix).xy
switch draggable in draggable {
case MouseDraggable_Point:
circle_position := (v3{**draggable.ref^, 1.0} * grid_to_world_matrix).xy
if linalg.distance(mouse, circle_position) <= program.mouse_draggable_radius {
program.mouse_current_draggable = draggable
return .DraggingDraggable
}
case MouseDraggable_RectangleCornerData:
circle_position := (v3{**draggable.corner_ref^, 1.0} * grid_to_world_matrix).xy
if linalg.distance(mouse, circle_position) <= program.mouse_draggable_radius {
program.mouse_current_draggable = draggable
return .DraggingDraggable
@ -649,10 +684,13 @@ program_handle_mouse :: proc(mouse_state: MouseState, top_bar_container, matrix_
return .Hover
}
draggable := program.mouse_current_draggable
switch draggable.type {
case .Point:
vector := cast(^v2)draggable.data
vector^ = (v3{**mouse, 1.0} * world_to_grid_matrix).xy
switch draggable in draggable {
case MouseDraggable_Point:
draggable.ref^ = (v3{**mouse, 1.0} * world_to_grid_matrix).xy
case MouseDraggable_RectangleCornerData:
position := (v3{**mouse, 1.0} * world_to_grid_matrix).xy
corners := rectangle_corners_move_corner(draggable.corners_ref^, position, draggable.corner)
draggable.corners_ref^ = corners
}
case .DraggingGrid:
program.grid.offset -= rl.GetMouseDelta()
@ -824,6 +862,8 @@ program_render_ui_and_handle_focus :: proc(ctx: ^ui.Context, focus_element_id: u
}
case .Label:
rl.DrawTextEx(font_style.font, element.label, rect_get_tl(area), font_style.size, font_style.spacing, color)
case .Divider:
rl.DrawRectangleRec(area, color)
case:
rl.DrawRectangleRec(area, rl.MAGENTA)
}