private byte GetWindowMinimized(ImGuiViewportPtr vp) { VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; SDL_WindowFlags flags = Sdl2Native.SDL_GetWindowFlags(window.Window.SdlWindowHandle); return((flags & SDL_WindowFlags.Minimized) != 0 ? (byte)1 : (byte)0); }
private unsafe void GetWindowSize(ImGuiViewportPtr vp, Vector2 *outSize) { VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; Rectangle bounds = window.Window.Bounds; *outSize = new Vector2(bounds.Width, bounds.Height); }
private void SetWindowPos(ImGuiViewportPtr vp, Vector2 pos) { VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; window.Window.X = (int)pos.X; window.Window.Y = (int)pos.Y; }
private unsafe void GetWindowSize(Vector2 size, ImGuiViewportPtr vp) { VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; Rectangle bounds = window.Window.Bounds; size.X = bounds.Width; size.Y = bounds.Height; }
private unsafe void GetWindowPos(Vector2 pos, ImGuiViewportPtr viewport) { VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(viewport.PlatformUserData).Target; Rectangle bounds = window.Window.Bounds; pos.X = bounds.X; pos.Y = bounds.Y; }
private void DestroyWindow(ImGuiViewportPtr vp) { if (vp.PlatformUserData != IntPtr.Zero) { VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; window.Dispose(); vp.PlatformUserData = IntPtr.Zero; } }
public void SwapExtraWindows(GraphicsDevice gd) { ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); for (int i = 1; i < platformIO.Viewports.Size; i++) { ImGuiViewportPtr vp = platformIO.Viewports[i]; VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; gd.SwapBuffers(window.Swapchain); } }
private void SetWindowFocus(ImGuiViewportPtr vp) { if (p_sdl_RaiseWindow == null) { p_sdl_RaiseWindow = Sdl2Native.LoadFunction <SDL_RaiseWindow_t>("SDL_RaiseWindow"); } VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; p_sdl_RaiseWindow(window.Window.SdlWindowHandle); }
private unsafe void SetWindowTitle(ImGuiViewportPtr vp, IntPtr title) { VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; byte *titlePtr = (byte *)title; int count = 0; while (titlePtr[count] != 0) { titlePtr += 1; } window.Window.Title = System.Text.Encoding.ASCII.GetString(titlePtr, count); }
/// <summary> /// Renders the ImGui draw list data. /// This method requires a <see cref="GraphicsDevice"/> because it may create new DeviceBuffers if the size of vertex /// or index data has increased beyond the capacity of the existing buffers. /// A <see cref="CommandList"/> is needed to submit drawing and resource update commands. /// </summary> public void Render(GraphicsDevice gd, CommandList cl) { if (_frameBegun) { _frameBegun = false; ImGui.Render(); RenderImDrawData(ImGui.GetDrawData(), gd, cl); // Update and Render additional Platform Windows if ((ImGui.GetIO().ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0) { ImGui.UpdatePlatformWindows(); ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); for (int i = 1; i < platformIO.Viewports.Size; i++) { ImGuiViewportPtr vp = platformIO.Viewports[i]; VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; cl.SetFramebuffer(window.Swapchain.Framebuffer); RenderImDrawData(vp.DrawData, gd, cl); } } } }
private unsafe void GetWindowPos(ImGuiViewportPtr vp, Vector2 *outPos) { VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; *outPos = new Vector2(window.Window.Bounds.X, window.Window.Bounds.Y); }
private void ShowWindow(ImGuiViewportPtr vp) { VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; Sdl2Native.SDL_ShowWindow(window.Window.SdlWindowHandle); }
private void CreateWindow(ImGuiViewportPtr vp) { VeldridImGuiWindow window = new VeldridImGuiWindow(_gd, vp); }
private void SetWindowTitle(ImGuiViewportPtr vp, string title) { VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; window.Window.Title = title; }
private void UpdateImGuiInput(InputSnapshot snapshot) { ImGuiIOPtr io = ImGui.GetIO(); Vector2 mousePosition = snapshot.MousePosition; // Determine if any of the mouse buttons were pressed during this snapshot period, even if they are no longer held. bool leftPressed = false; bool middlePressed = false; bool rightPressed = false; foreach (MouseEvent me in snapshot.MouseEvents) { if (me.Down) { switch (me.MouseButton) { case MouseButton.Left: leftPressed = true; break; case MouseButton.Middle: middlePressed = true; break; case MouseButton.Right: rightPressed = true; break; } } } io.MouseDown[0] = leftPressed || snapshot.IsMouseDown(MouseButton.Left); io.MouseDown[1] = middlePressed || snapshot.IsMouseDown(MouseButton.Right); io.MouseDown[2] = rightPressed || snapshot.IsMouseDown(MouseButton.Middle); if (p_sdl_GetGlobalMouseState == null) { p_sdl_GetGlobalMouseState = Sdl2Native.LoadFunction <SDL_GetGlobalMouseState_t>("SDL_GetGlobalMouseState"); } int x, y; unsafe { uint buttons = p_sdl_GetGlobalMouseState(&x, &y); io.MouseDown[0] = (buttons & 0b0001) != 0; io.MouseDown[1] = (buttons & 0b0010) != 0; io.MouseDown[2] = (buttons & 0b0100) != 0; } io.MousePos = new Vector2(x, y); io.MouseWheel = snapshot.WheelDelta; IReadOnlyList <char> keyCharPresses = snapshot.KeyCharPresses; for (int i = 0; i < keyCharPresses.Count; i++) { char c = keyCharPresses[i]; io.AddInputCharacter(c); } IReadOnlyList <KeyEvent> keyEvents = snapshot.KeyEvents; for (int i = 0; i < keyEvents.Count; i++) { KeyEvent keyEvent = keyEvents[i]; io.KeysDown[(int)keyEvent.Key] = keyEvent.Down; if (keyEvent.Key == Key.ControlLeft) { _controlDown = keyEvent.Down; } if (keyEvent.Key == Key.ShiftLeft) { _shiftDown = keyEvent.Down; } if (keyEvent.Key == Key.AltLeft) { _altDown = keyEvent.Down; } if (keyEvent.Key == Key.WinLeft) { _winKeyDown = keyEvent.Down; } } io.KeyCtrl = _controlDown; io.KeyAlt = _altDown; io.KeyShift = _shiftDown; io.KeySuper = _winKeyDown; ImVector <ImGuiViewportPtr> viewports = ImGui.GetPlatformIO().Viewports; for (int i = 1; i < viewports.Size; i++) { ImGuiViewportPtr v = viewports[i]; VeldridImGuiWindow window = ((VeldridImGuiWindow)GCHandle.FromIntPtr(v.PlatformUserData).Target); window.Update(); } }
/// <summary> /// Constructs a new ImGuiController. /// </summary> public unsafe ImGuiController(GraphicsDevice gd, Sdl2Window window, OutputDescription outputDescription, int width, int height) { _gd = gd; _window = window; _windowWidth = width; _windowHeight = height; IntPtr context = ImGui.CreateContext(); ImGui.SetCurrentContext(context); ImGuiIOPtr io = ImGui.GetIO(); io.ConfigFlags |= ImGuiConfigFlags.DockingEnable; io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable; ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); ImGuiViewportPtr mainViewport = platformIO.MainViewport; mainViewport.PlatformHandle = window.Handle; _mainViewportWindow = new VeldridImGuiWindow(gd, mainViewport, _window); _createWindow = CreateWindow; _destroyWindow = DestroyWindow; _getWindowPos = GetWindowPos; _showWindow = ShowWindow; _setWindowPos = SetWindowPos; _setWindowSize = SetWindowSize; _getWindowSize = GetWindowSize; _setWindowFocus = SetWindowFocus; _getWindowFocus = GetWindowFocus; _getWindowMinimized = GetWindowMinimized; _setWindowTitle = SetWindowTitle; platformIO.Platform_CreateWindow = Marshal.GetFunctionPointerForDelegate(_createWindow); platformIO.Platform_DestroyWindow = Marshal.GetFunctionPointerForDelegate(_destroyWindow); platformIO.Platform_ShowWindow = Marshal.GetFunctionPointerForDelegate(_showWindow); platformIO.Platform_SetWindowPos = Marshal.GetFunctionPointerForDelegate(_setWindowPos); platformIO.Platform_SetWindowSize = Marshal.GetFunctionPointerForDelegate(_setWindowSize); platformIO.Platform_SetWindowFocus = Marshal.GetFunctionPointerForDelegate(_setWindowFocus); platformIO.Platform_GetWindowFocus = Marshal.GetFunctionPointerForDelegate(_getWindowFocus); platformIO.Platform_GetWindowMinimized = Marshal.GetFunctionPointerForDelegate(_getWindowMinimized); platformIO.Platform_SetWindowTitle = Marshal.GetFunctionPointerForDelegate(_setWindowTitle); ImGuiNative.ImGuiPlatformIO_Set_Platform_GetWindowPos(platformIO.NativePtr, Marshal.GetFunctionPointerForDelegate(_getWindowPos)); ImGuiNative.ImGuiPlatformIO_Set_Platform_GetWindowSize(platformIO.NativePtr, Marshal.GetFunctionPointerForDelegate(_getWindowSize)); unsafe { io.NativePtr->BackendPlatformName = (byte *)new FixedAsciiString("Veldrid.SDL2 Backend").DataPtr; } io.BackendFlags |= ImGuiBackendFlags.HasMouseCursors; io.BackendFlags |= ImGuiBackendFlags.HasSetMousePos; io.BackendFlags |= ImGuiBackendFlags.PlatformHasViewports; io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports; io.Fonts.AddFontDefault(); CreateDeviceResources(gd, outputDescription); SetKeyMappings(); SetPerFrameImGuiData(1f / 60f); UpdateMonitors(); ImGui.NewFrame(); _frameBegun = true; }
private void SetWindowSize(ImGuiViewportPtr vp, Vector2 size) { VeldridImGuiWindow window = (VeldridImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target; Sdl2Native.SDL_SetWindowSize(window.Window.SdlWindowHandle, (int)size.X, (int)size.Y); }