#load "../ui/widgets.jai"; pick_scene_view_at :: (camera: Camera, coordinates: Vector2) { ray := normalized_screen_to_ray_v2(camera, coordinates); if engine.editor.should_check_entities { hit_entity : *Entity; closest : float = 100000000; for engine.current_scene.entities { if !(it.flags & .RENDERABLE) continue; if it.flags & Entity_Flags.DELETED || it.parent != null continue; success, dist := ray_entity_intersect(ray, it); if success && dist < closest { closest = dist; hit_entity = it; } } if hit_entity != null { if !array_find(engine.editor.selected_entities, hit_entity) { if !key_pressed(.CTRL) { engine.editor.selected_entities.count = 0; } array_add(*engine.editor.selected_entities, hit_entity); } } else { engine.editor.selected_entities.count = 0; } } } editor_ui :: () { ui_full_size_background(); ui_push_parent(ui_state.last_box, alignment=.LEFT, axis=.VERTICAL); { ui_set_next_padding(1); ui_toolbar(); ui_push_parent(ui_state.last_box, alignment=.LEFT, axis=.HORIZONTAL); { if ui_toolbar_button("File") { } //ui_space(15, 10); if ui_toolbar_button("Edit") { } //ui_space(15, 10); //if ui_toolbar_button("View") { //} ui_space(20, 0); if engine.mode == { case .PLAYING; { ui_set_next_background_color(.{0,0.6,0,1}); if ui_button_with_texture(engine.editor.icons.stop) { switch_engine_mode(.EDITING); } ui_label(tprint("Playing '%'", engine.current_scene.name), .{0,0.7,0,1}); } case .EDITING; { ui_set_next_background_color(.{0.6,0,0,1}); if ui_button_with_texture(engine.editor.icons.play) { switch_engine_mode(.PLAYING); } ui_label(tprint("Editing '%'", engine.current_scene.name), .{1,1,1,1}); } } } ui_pop_parent(); ui_set_next_size_x(.PCT, 1.0); ui_set_next_size_y(.PCT, 0.965); ui_set_next_background_color(.{0,0,0,1}); background := ui_box(.NONE); ui_push_parent(ui_state.last_box, alignment=.LEFT, axis=.HORIZONTAL); { ui_set_next_size_x(.PCT, 0.1); ui_set_next_size_y(.PCT, 1.0); ui_set_next_padding(1); first_panel := ui_box(.NONE); ui_push_parent(first_panel, alignment=.LEFT, axis=.VERTICAL); { ui_set_next_size_x(.PCT, 1.0); ui_set_next_size_y(.PCT, 1.0); ui_set_next_background_color(.{0.04, 0.04, 0.04, 1.0}); background := ui_box(.DRAW_BACKGROUND|.DRAW_BORDER); ui_push_parent(background, alignment=.LEFT, axis=.VERTICAL); { ui_set_next_size_x(.PCT, 1.0); ui_tab_title_bar("ENTITIES"); if engine.current_scene != null { for engine.current_scene.entities { if it.flags & .DELETED continue; ui_set_next_padding(20); clicked := false; selected := array_find(engine.editor.selected_entities, it); if it.name.count == 0 { clicked = ui_clickable_label(tprint("%", it.type), selected, it_index); } else { clicked = ui_clickable_label(it.name, selected, it_index); } if clicked { if !key_pressed(.CTRL) { engine.editor.selected_entities.count = 0; array_add(*engine.editor.selected_entities, it); } else { if selected { array_unordered_remove_by_value(*engine.editor.selected_entities, it); } else { array_add(*engine.editor.selected_entities, it); } } } //ui_space(0, 5); } } } ui_pop_parent(); } ui_pop_parent(); // Scene ui_set_next_size_x(.PCT, 0.7); ui_set_next_size_y(.PCT, 1.0); ui_set_next_padding(1); viewport_layer := ui_box(.NONE); ui_push_parent(viewport_layer, alignment=.LEFT, axis=.VERTICAL); { ui_set_next_size_x(.PCT, 1.0); ui_tab_title_bar("SCENE"); ui_set_next_size_x(.PCT, 1.0); ui_set_next_size_y(.PCT, 0.1); ui_toolbar(); ui_push_parent(ui_state.last_box, alignment=.LEFT, axis=.HORIZONTAL); { if engine.editor.transform_gizmo.snap_to_grid { if ui_toolbar_button("Snap to grid enabled", .{0.0, 0.4, 0.0, 1.0}) { engine.editor.transform_gizmo.snap_to_grid = false; } } else { if ui_toolbar_button("Snap to grid disabled", .{0.4, 0.0, 0.0, 1.0}) { engine.editor.transform_gizmo.snap_to_grid = true; } } if engine.editor.transform_gizmo.space == .LOCAL { if ui_toolbar_button("LOCAL", .{0.0, 0.3, 0.0, 1.0}) { engine.editor.transform_gizmo.space = .WORLD; } } else { if ui_toolbar_button("WORLD", .{0.0, 0.0, 0.3, 1.0}) { engine.editor.transform_gizmo.space = .LOCAL; } } //ui_space(15, 10) ui_space(20, 0); } ui_pop_parent(); ui_set_next_size_x(.PCT, 1.0); ui_set_next_size_y(.PCT, 0.65); state := ui_interactable_texture(get_texture_from_pass("UI Blend Pass")); engine.input.viewport_mouse_position = state.local_mouse_coordinates; engine.input.normalized_viewport_mouse_position = state.normalized_local_mouse_coordinates; if state.left_mouse_down { if engine.current_scene != null { pick_scene_view_at(engine.editor.camera, .{state.normalized_local_mouse_coordinates.x, 1.0-state.normalized_local_mouse_coordinates.y}); } } engine.editor.mouse_viewport_state = state; ui_set_next_size_x(.PCT, 1.0); ui_set_next_size_y(.PCT, 0.25); ui_tab_title_bar("Create entities"); { new_entity := editor_ui_entity_creation(); if new_entity != null { set_position(*new_entity.transform, engine.editor.camera.position + engine.editor.camera.forward * 20.0); engine.editor.selected_entities.count = 0; array_add(*engine.editor.selected_entities, new_entity); } } } ui_pop_parent(); // Properties ui_set_next_size_x(.PCT, 0.2); ui_set_next_size_y(.PCT, 1.0); ui_set_next_background_color(.{0.04, 0.04, 0.04, 1.0}); ui_set_next_padding(1); inspector := ui_box(.DRAW_BACKGROUND | .DRAW_BORDER); ui_push_parent(inspector, alignment=.LEFT, axis=.VERTICAL); { ui_set_next_size_x(.PCT, 1.0); ui_tab_title_bar("PROPERTIES"); if engine.editor.selected_entities.count == 1 { entity := engine.editor.selected_entities[0]; //ui_slider(*slider_value, 0.0, 1.0); ui_textfield("Name", *entity.name); ui_label(tprint("Id: %", entity.id)); updated := ui_vector_field("Position", *entity.transform.position); euler_rotation := quaternion_to_euler_v3(entity.transform.orientation); euler_rotation *= RADIANS_TO_DEGREES; updated |= ui_vector_field("Rotation", *euler_rotation); euler_rotation *= DEGREES_TO_RADIANS; entity.transform.orientation = euler_to_quaternion(euler_rotation); updated |= ui_vector_field("Scale", *entity.transform.scale); if updated { update_matrix(*entity.transform); } entity_ui(entity); } } ui_pop_parent(); } ui_pop_parent(); } ui_pop_parent(); } text_fun : string; slider_value : float = 0.0; base_editor_update :: () { editor_ui(); camera := *engine.editor.camera; if engine.editor.focused_widget == null && engine.mode == .EDITING { engine.editor.should_check_entities = true; if engine.editor.selected_entities.count == 1 { entity := engine.editor.selected_entities[0]; gizmo_scale := distance(entity.transform.position, engine.editor.camera.position) * 0.1 * 0.5; engine.editor.transform_gizmo.uniform_gizmo_scale = gizmo_scale; set_scale(*engine.editor.transform_gizmo.transform, .{gizmo_scale, gizmo_scale, gizmo_scale}); coordinates := Vector2.{engine.editor.mouse_viewport_state.normalized_local_mouse_coordinates.x, 1.0 - engine.editor.mouse_viewport_state.normalized_local_mouse_coordinates.y}; ray := normalized_screen_to_ray_v2(engine.editor.camera, coordinates); if update_transform_gizmo(ray, coordinates) { engine.editor.should_check_entities = false; } } 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"); } if key_down(.DELETE) || key_down(.BACKSPACE) { for engine.editor.selected_entities { mark_entity_deleted(it); } engine.editor.transform_gizmo.selected_axis = .NONE; engine.editor.selected_entities.count = 0; } if key_pressed(.CTRL) && key_down(.D) { duplicated_entities: [..] *Entity; duplicated_entities.allocator = temp; for e: engine.editor.selected_entities { array_add(*duplicated_entities, duplicate_entity(e)); } engine.editor.selected_entities.count = 0; for e: duplicated_entities { array_add(*engine.editor.selected_entities, e); } } } if engine.mode == .EDITING { if key_pressed(.MOUSE_RIGHT) { set_show_cursor(false); // Update camera mouse_speed :: 54.0; dir : Vector3; if key_pressed(.W) { dir += camera.forward; } else if key_pressed(.S) { dir += -camera.forward; } if key_pressed(.A) { dir += -camera.right; } else if key_pressed(.D) { dir += camera.right; } if key_pressed(.Q) { dir += -camera.up; } else if key_pressed(.E) { dir += camera.up; } dir = normalize(dir); if length(dir) > 0.05 { multiplier := ifx key_pressed(.SHIFT) then 10.0 else 1.0; camera.position += dir * 20.0 * multiplier * dt; //print("Camera position %\n", camera.position); } MOUSE_SENSITIVITY :: 0.06; camera.rotation.yaw -= engine.input.mouse.delta_x * MOUSE_SENSITIVITY; camera.rotation.pitch += -engine.input.mouse.delta_y * MOUSE_SENSITIVITY; if key_down(.MOUSE_RIGHT) { engine.editor.last_right_mouse_click_time = time; } } else { set_show_cursor(true); if time - engine.editor.last_right_mouse_click_time < 0.2 { mouse_position : Vector2; mouse_position.x = xx engine.input.mouse.x; mouse_position.y = xx engine.input.mouse.y; engine.editor.show_menu = true; engine.editor.menu_position.x = mouse_position.x; engine.editor.menu_position.y = cast(float)engine.renderer.render_target_height - mouse_position.y; } if !key_pressed(.CTRL) { if key_down(.W) { engine.editor.transform_gizmo.transform_type = .TRANSLATION; } if key_down(.E) { engine.editor.transform_gizmo.transform_type = .ROTATION; } if key_down(.R) { engine.editor.transform_gizmo.transform_type = .SCALE; } } } update_view_matrix(camera); } if key_pressed(.CTRL) && key_down(.E) { new_mode := ifx engine.mode == .EDITING then Engine_Mode.PLAYING else .EDITING; switch_engine_mode(new_mode); } }