| 766 | } |
| 767 | |
| 768 | void imgui_context::new_frame(XrTime display_time) |
| 769 | { |
| 770 | ImGui::SetCurrentContext(context); |
| 771 | ImPlot::SetCurrentContext(plot_context); |
| 772 | |
| 773 | if (last_display_time) |
| 774 | io.DeltaTime = std::clamp((display_time - last_display_time) * 1e-9f, 0.001f, 0.0166f); |
| 775 | last_display_time = display_time; |
| 776 | |
| 777 | // Uses the window list from last frame |
| 778 | auto new_states = read_controllers_state(display_time); |
| 779 | |
| 780 | // Set the currently active controller |
| 781 | size_t new_focused_controller = choose_focused_controller(new_states); |
| 782 | |
| 783 | bool focused_change = new_focused_controller != focused_controller && focused_controller != (size_t)-1; |
| 784 | |
| 785 | // Set the source for the following events |
| 786 | if (focused_controller != (size_t)-1) |
| 787 | io.AddMouseSourceEvent(new_states[focused_controller].source); |
| 788 | |
| 789 | if (new_focused_controller != (size_t)-1) |
| 790 | { |
| 791 | auto scroll = new_states[new_focused_controller].scroll_value; |
| 792 | |
| 793 | button_pressed = new_states[new_focused_controller].trigger_clicked; |
| 794 | fingertip_touching = new_states[new_focused_controller].fingertip_touching; |
| 795 | |
| 796 | // Focused controller changed: end the current click for this frame |
| 797 | io.AddMouseButtonEvent(0, (button_pressed or fingertip_touching) and not focused_change); |
| 798 | |
| 799 | if (auto position = new_states[new_focused_controller].pointer_position) |
| 800 | { |
| 801 | io.AddMousePosEvent(position->x, position->y); |
| 802 | |
| 803 | if (glm::length(scroll) > constants::gui::scroll_value_thd) |
| 804 | { |
| 805 | io.MouseWheel = scroll.y; |
| 806 | io.MouseWheelH = scroll.x; |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | focused_controller = new_focused_controller; |
| 812 | for (auto && [controller, next_state]: std::views::zip(controllers, new_states)) |
| 813 | { |
| 814 | controller.second = next_state; |
| 815 | } |
| 816 | |
| 817 | // Start the Dear ImGui frame |
| 818 | ImGui_ImplVulkan_NewFrame(); |
| 819 | |
| 820 | io.DisplaySize = ImVec2(size.width, size.height); |
| 821 | io.DisplayFramebufferScale = ImVec2(1, 1); |
| 822 | |
| 823 | // See ImGui_ImplSDL2_ProcessEvent |
| 824 | |
| 825 | ImGui::NewFrame(); |