Added set_position_rotation_scale procedures

This commit is contained in:
2024-12-02 23:36:35 +01:00
parent e7522f9c55
commit 71668cc2f5
3 changed files with 68 additions and 4 deletions

View File

@@ -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};

View File

@@ -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;
}

View File

@@ -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");