From 71668cc2f5c9d36672eeec3ec53a267685f855cf Mon Sep 17 00:00:00 2001 From: Daniel Bross Date: Mon, 2 Dec 2024 23:36:35 +0100 Subject: [PATCH] Added set_position_rotation_scale procedures --- core/transform.jai | 11 ++++++++++ editor/editor.jai | 51 ++++++++++++++++++++++++++++++++++++++++++++ editor/editor_ui.jai | 10 +++++---- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/core/transform.jai b/core/transform.jai index 110ac88..8912761 100644 --- a/core/transform.jai +++ b/core/transform.jai @@ -166,6 +166,17 @@ set_scale :: (transform: *Transform, scale: float, calculate_matrix: bool = true if calculate_matrix update_matrix(transform); } +set_position_rotation_scale :: (transform: *Transform, position: Vector3, rotation: Quaternion, scale: Vector3, calculate_matrix: bool = true) { + transform.position = position; + transform.orientation = rotation; + transform.scale = scale; + if calculate_matrix update_matrix(transform); +} + +set_position_rotation_scale :: (e: *Entity, position: Vector3, rotation: Quaternion, scale: Vector3, calculate_matrix: bool = true) { + set_position_rotation_scale(*e.transform, position, rotation, scale, calculate_matrix); +} + get_forward :: (transform: Transform) -> Vector3 { v := rotation_matrix(Matrix4, transform.orientation) * Vector4.{0,0,1,0}; return .{v.x, v.y, v.z}; diff --git a/editor/editor.jai b/editor/editor.jai index 1d15ca8..065d29a 100644 --- a/editor/editor.jai +++ b/editor/editor.jai @@ -17,6 +17,49 @@ Transform_Type :: enum { SCALE; } +Editor_Undo_Type :: enum { + TRANSFORM_CHANGE; +} + +Editor_Undo :: struct { + type: Editor_Undo_Type; + + entity: *Entity; + + union { + transform : struct { + position: Vector3; + rotation: Quaternion; + scale: Vector3; + }; + } +} + +push_transform_undo :: (e: *Entity) { + undo: Editor_Undo; + undo.type = .TRANSFORM_CHANGE; + undo.entity = e; + undo.transform.position = e.transform.position; + undo.transform.scale = e.transform.scale; + undo.transform.rotation = e.transform.orientation; + array_add(*engine.editor.undo_stack, undo); +} + +undo :: () { + if engine.editor.undo_stack.count == 0 return; + + print("UNDO!\n"); + undo := engine.editor.undo_stack[engine.editor.undo_stack.count-1]; + + if undo.type == { + case .TRANSFORM_CHANGE; { + set_position_rotation_scale(undo.entity, undo.transform.position, undo.transform.rotation, undo.transform.scale); + } + } + + engine.editor.undo_stack.count -= 1; +} + Buffer_Info :: struct { buffer: Buffer_Handle; vertex_count: u32; @@ -81,6 +124,8 @@ Editor :: struct { play: Texture_Handle; stop: Texture_Handle; } + + undo_stack: [..] Editor_Undo; } init_editor :: () { @@ -155,6 +200,7 @@ update_transform_gizmo :: (ray: Ray, mouse_position: Vector2) -> bool { if first_update { engine.editor.transform_gizmo.actual_entity_position = selected_entity.transform.position; + push_transform_undo(engine.editor.selected_entities[0]); } // Move the currently selected entity along the selected axis @@ -234,6 +280,10 @@ update_transform_gizmo :: (ray: Ray, mouse_position: Vector2) -> bool { } else if !key_pressed(.MOUSE_LEFT) { engine.editor.transform_gizmo.clicked = false; } else { + if key_down(.MOUSE_LEFT) { + push_transform_undo(engine.editor.selected_entities[0]); + } + direction : Vector3; if engine.editor.transform_gizmo.selected_axis == { case .UP; @@ -299,6 +349,7 @@ update_transform_gizmo :: (ray: Ray, mouse_position: Vector2) -> bool { first_update := key_down(.MOUSE_LEFT); if first_update { + push_transform_undo(engine.editor.selected_entities[0]); engine.editor.transform_gizmo.actual_entity_position = selected_entity.transform.position; engine.editor.transform_gizmo.actual_entity_scale = selected_entity.transform.scale; } diff --git a/editor/editor_ui.jai b/editor/editor_ui.jai index 1de7438..35fd9e8 100644 --- a/editor/editor_ui.jai +++ b/editor/editor_ui.jai @@ -1,7 +1,5 @@ #load "../ui/widgets.jai"; -#placeholder editor_ui_entity_creation; - pick_scene_view_at :: (camera: Camera, coordinates: Vector2) { ray := normalized_screen_to_ray_v2(camera, coordinates); @@ -51,8 +49,8 @@ editor_ui :: () { //ui_space(15, 10); - if ui_toolbar_button("View") { - } + //if ui_toolbar_button("View") { + //} ui_space(20, 0); @@ -266,6 +264,10 @@ base_editor_update :: () { } } + if key_pressed(.CTRL) && key_down(.Z) { + undo(); + } + if key_pressed(.CTRL) && key_down(.S) { save_scene(engine.current_scene, "../assets/scenes/"); //show_message("Saved scene");