From 7804c6a96d26c2e757d09f1864eb73fb81eb280f Mon Sep 17 00:00:00 2001 From: Fox Caminiti Date: Thu, 20 Oct 2022 20:45:37 -0400 Subject: precomp recursion! --- imgui_helper_widgets.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 imgui_helper_widgets.cpp (limited to 'imgui_helper_widgets.cpp') diff --git a/imgui_helper_widgets.cpp b/imgui_helper_widgets.cpp new file mode 100644 index 0000000..105dad2 --- /dev/null +++ b/imgui_helper_widgets.cpp @@ -0,0 +1,68 @@ +// Widgets not directly related to drawing UI. + +// Returns a normalized UV position of the composition +static v2 +ImGui_ScreenPointToCompUV(ImVec2 ViewportMin, ImVec2 CompPos, ImVec2 CompZoom, ImVec2 MousePos) +{ + ImVec2 LocalMousePos = MousePos - ViewportMin; + ImVec2 LocalCompPos = CompPos - ViewportMin; + ImVec2 MouseScreenUV = LocalMousePos - LocalCompPos; + ImVec2 Result = MouseScreenUV / CompZoom; + return V2(Result); +} + +// NOTE(fox): We have to do a bit of hackery here to tell how many times the +// mouse has been warped during a drag, since it doesn't seem like we can rely +// on SDL_WarpMouseGlobal to update on the first frame of a WantSetPos request. + +static void +ImGui_WarpMouse(ui *UI, ImVec2 MousePos, ImVec2 Min, ImVec2 Max, int Direction = 3) +{ + if (Direction & 1) { + if (MousePos.x < Min.x) { + UI->Warp_WantSetPos = true; + UI->Warp_PositionToSet = ImVec2(Max.x - 5, MousePos.y); + UI->Warp_PositionInitial = MousePos.x; + UI->Warp_Direction = 0; + } + if (MousePos.x > Max.x) { + UI->Warp_WantSetPos = true; + UI->Warp_PositionToSet = ImVec2(Min.x + 5, MousePos.y); + UI->Warp_PositionInitial = MousePos.x; + UI->Warp_Direction = 1; + } + } + if (Direction & 2) { + if (MousePos.y < Min.y) { + UI->Warp_WantSetPos = true; + UI->Warp_PositionToSet = ImVec2(MousePos.x, Max.y - 5); + UI->Warp_PositionInitial = MousePos.y; + UI->Warp_Direction = 2; + } + if (MousePos.y > Max.y) { + UI->Warp_WantSetPos = true; + UI->Warp_PositionToSet = ImVec2(MousePos.x, Min.y + 5); + UI->Warp_PositionInitial = MousePos.y; + UI->Warp_Direction = 3; + } + } +} + +// We record the initial position and the direction of the wrap, and only +// increment the wrap amount when MousePos actually is measured to be what we expect. + +static void +ImGui_WarpMouseFinish(ui *UI, ImVec2 MousePos) +{ + if (UI->Warp_Direction == 0) { + if (MousePos.x < UI->Warp_PositionInitial) UI->Warp_X--; + } else if (UI->Warp_Direction == 1) { + if (MousePos.x > UI->Warp_PositionInitial) UI->Warp_X++; + } else if (UI->Warp_Direction == 2) { + if (MousePos.y < UI->Warp_PositionInitial) UI->Warp_Y--; + } else if (UI->Warp_Direction == 3) { + if (MousePos.y > UI->Warp_PositionInitial) UI->Warp_Y++; + } else { + Assert(0); + } +} -- cgit v1.2.3