Пример #1
0
    /// <summary>
    /// Constructs a new ImGuiController.
    /// </summary>
    public ImGuiBindings(GraphicsDevice gd, OutputDescription outputDescription, int width, int height)
    {
        _gd           = gd;
        _windowWidth  = width;
        _windowHeight = height;

        IntPtr context = ImGui.CreateContext();

        ImGui.SetCurrentContext(context);
        var fonts = ImGui.GetIO().Fonts;

        ImGui.GetIO().Fonts.AddFontDefault();

        ImGui.GetIO().ConfigFlags  |= ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.NavEnableGamepad;
        ImGui.GetIO().BackendFlags |= ImGuiBackendFlags.HasGamepad;

        _setText = new SetClipboardTextDelegate(SetClipboardText);
        _getText = new GetClipboardTextDelegate(GetClipboardText);

        var io = ImGui.GetIO();

        io.SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_setText);
        io.GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_getText);
        io.ClipboardUserData  = IntPtr.Zero;

        CreateDeviceResources(gd, outputDescription);
        SetKeyMappings();

        SetPerFrameImGuiData(1f / 60f);

        ImGui.NewFrame();
        _frameBegun = true;
    }
Пример #2
0
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                    _setText = null;
                    _getText = null;
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                _sdlWindow = IntPtr.Zero;

                ImGui.GetIO().SetClipboardTextFn = IntPtr.Zero;
                ImGui.GetIO().GetClipboardTextFn = IntPtr.Zero;

                // Destroy SDL mouse cursors
                foreach (var cur in _mouseCursors)
                {
                    SDL_FreeCursor(cur);
                }

                if (_platformNamePtr != IntPtr.Zero)
                {
                    unsafe
                    {
                        ImGui.GetIO().NativePtr->BackendPlatformName = null;
                    }

                    Marshal.FreeHGlobal(_platformNamePtr);
                    _platformNamePtr = IntPtr.Zero;
                }

                if (_iniPathPtr != IntPtr.Zero)
                {
                    unsafe
                    {
                        ImGui.GetIO().NativePtr->IniFilename = null;
                    }

                    Marshal.FreeHGlobal(_iniPathPtr);
                    _iniPathPtr = IntPtr.Zero;
                }

                disposedValue = true;
            }
        }
Пример #3
0
        public static bool Init(IntPtr sdlWindow)
        {
            _sdlWindow = sdlWindow;

            // Setup back-end capabilities flags
            var io = ImGui.GetIO();

            // We can honor GetMouseCursor() values (optional)
            // We can honor io.WantSetMousePos requests (optional, rarely used)
            io.BackendFlags = io.BackendFlags | (ImGuiBackendFlags.HasMouseCursors | ImGuiBackendFlags.HasSetMousePos);

            // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
            io.KeyMap[(int)ImGuiKey.Tab]         = (int)SDL_Scancode.SDL_SCANCODE_TAB;
            io.KeyMap[(int)ImGuiKey.LeftArrow]   = (int)SDL_Scancode.SDL_SCANCODE_LEFT;
            io.KeyMap[(int)ImGuiKey.RightArrow]  = (int)SDL_Scancode.SDL_SCANCODE_RIGHT;
            io.KeyMap[(int)ImGuiKey.UpArrow]     = (int)SDL_Scancode.SDL_SCANCODE_UP;
            io.KeyMap[(int)ImGuiKey.DownArrow]   = (int)SDL_Scancode.SDL_SCANCODE_DOWN;
            io.KeyMap[(int)ImGuiKey.PageUp]      = (int)SDL_Scancode.SDL_SCANCODE_PAGEUP;
            io.KeyMap[(int)ImGuiKey.PageDown]    = (int)SDL_Scancode.SDL_SCANCODE_PAGEDOWN;
            io.KeyMap[(int)ImGuiKey.Home]        = (int)SDL_Scancode.SDL_SCANCODE_HOME;
            io.KeyMap[(int)ImGuiKey.End]         = (int)SDL_Scancode.SDL_SCANCODE_END;
            io.KeyMap[(int)ImGuiKey.Insert]      = (int)SDL_Scancode.SDL_SCANCODE_INSERT;
            io.KeyMap[(int)ImGuiKey.Delete]      = (int)SDL_Scancode.SDL_SCANCODE_DELETE;
            io.KeyMap[(int)ImGuiKey.Backspace]   = (int)SDL_Scancode.SDL_SCANCODE_BACKSPACE;
            io.KeyMap[(int)ImGuiKey.Space]       = (int)SDL_Scancode.SDL_SCANCODE_SPACE;
            io.KeyMap[(int)ImGuiKey.Enter]       = (int)SDL_Scancode.SDL_SCANCODE_RETURN;
            io.KeyMap[(int)ImGuiKey.Escape]      = (int)SDL_Scancode.SDL_SCANCODE_ESCAPE;
            io.KeyMap[(int)ImGuiKey.KeyPadEnter] = (int)SDL_Scancode.SDL_SCANCODE_RETURN2;
            io.KeyMap[(int)ImGuiKey.A]           = (int)SDL_Scancode.SDL_SCANCODE_A;
            io.KeyMap[(int)ImGuiKey.C]           = (int)SDL_Scancode.SDL_SCANCODE_C;
            io.KeyMap[(int)ImGuiKey.V]           = (int)SDL_Scancode.SDL_SCANCODE_V;
            io.KeyMap[(int)ImGuiKey.X]           = (int)SDL_Scancode.SDL_SCANCODE_X;
            io.KeyMap[(int)ImGuiKey.Y]           = (int)SDL_Scancode.SDL_SCANCODE_Y;
            io.KeyMap[(int)ImGuiKey.Z]           = (int)SDL_Scancode.SDL_SCANCODE_Z;

            _setText = new SetClipboardTextDelegate(SetClipboardText);
            _getText = new GetClipboardTextDelegate(GetClipboardText);

            io.SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_setText);
            io.GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_getText);
            io.ClipboardUserData  = IntPtr.Zero;

            _mouseCursors[(int)ImGuiMouseCursor.Arrow]      = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_ARROW);
            _mouseCursors[(int)ImGuiMouseCursor.TextInput]  = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_IBEAM);
            _mouseCursors[(int)ImGuiMouseCursor.ResizeAll]  = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZEALL);
            _mouseCursors[(int)ImGuiMouseCursor.ResizeNS]   = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENS);
            _mouseCursors[(int)ImGuiMouseCursor.ResizeEW]   = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZEWE);
            _mouseCursors[(int)ImGuiMouseCursor.ResizeNESW] = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENESW);
            _mouseCursors[(int)ImGuiMouseCursor.ResizeNWSE] = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENWSE);
            _mouseCursors[(int)ImGuiMouseCursor.Hand]       = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_HAND);
            // This apparently does not exist in ImGui.NET
            // _mouseCursors[(int)ImGuiMouseCursor.NotAllowed] = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_NO);

            var sysWmInfo = new SDL_SysWMinfo();

            SDL_GetVersion(out sysWmInfo.version);
            SDL_GetWindowWMInfo(sdlWindow, ref sysWmInfo);
            io.ImeWindowHandle = sysWmInfo.info.win.window;

            return(true);
        }
Пример #4
0
        public static void Init(RenderWindow window, Vector2f displaySize, bool loadDefaultFont = true)
        {
            ImGui.CreateContext();
            var io = ImGui.GetIO();

            io.BackendFlags |= ImGuiBackendFlags.HasGamepad;
            io.BackendFlags |= ImGuiBackendFlags.HasMouseCursors;
            io.BackendFlags |= ImGuiBackendFlags.HasSetMousePos;
            //io.BackendPlatformName = "imgui_impl_sfml_net";

            unsafe
            {
                io.KeyMap[(int)ImGuiKey.Tab]        = (int)Keyboard.Key.Tab;
                io.KeyMap[(int)ImGuiKey.LeftArrow]  = (int)Keyboard.Key.Left;
                io.KeyMap[(int)ImGuiKey.RightArrow] = (int)Keyboard.Key.Right;
                io.KeyMap[(int)ImGuiKey.UpArrow]    = (int)Keyboard.Key.Up;
                io.KeyMap[(int)ImGuiKey.DownArrow]  = (int)Keyboard.Key.Down;
                io.KeyMap[(int)ImGuiKey.PageUp]     = (int)Keyboard.Key.PageUp;
                io.KeyMap[(int)ImGuiKey.PageDown]   = (int)Keyboard.Key.PageDown;
                io.KeyMap[(int)ImGuiKey.Home]       = (int)Keyboard.Key.Home;
                io.KeyMap[(int)ImGuiKey.End]        = (int)Keyboard.Key.End;
                io.KeyMap[(int)ImGuiKey.Insert]     = (int)Keyboard.Key.Insert;
                io.KeyMap[(int)ImGuiKey.Delete]     = (int)Keyboard.Key.Delete;
                io.KeyMap[(int)ImGuiKey.Backspace]  = (int)Keyboard.Key.BackSpace;
                io.KeyMap[(int)ImGuiKey.Space]      = (int)Keyboard.Key.Space;
                io.KeyMap[(int)ImGuiKey.Enter]      = (int)Keyboard.Key.Return;
                io.KeyMap[(int)ImGuiKey.Escape]     = (int)Keyboard.Key.Escape;
                io.KeyMap[(int)ImGuiKey.A]          = (int)Keyboard.Key.A;
                io.KeyMap[(int)ImGuiKey.C]          = (int)Keyboard.Key.C;
                io.KeyMap[(int)ImGuiKey.V]          = (int)Keyboard.Key.V;
                io.KeyMap[(int)ImGuiKey.X]          = (int)Keyboard.Key.X;
                io.KeyMap[(int)ImGuiKey.Y]          = (int)Keyboard.Key.Y;
                io.KeyMap[(int)ImGuiKey.Z]          = (int)Keyboard.Key.Z;
            }

            s_joystickId = GetConnectedJoystickId();

            for (uint i = 0; i < (int)ImGuiNavInput.COUNT; ++i)
            {
                s_joystickMapping[i] = NULL_JOYSTICK_BUTTON;
            }

            InitDefaultJoystickMapping();

            io.DisplaySize = new System.Numerics.Vector2(displaySize.X, displaySize.Y);

            unsafe
            {
                io.SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(setClipboardTextDelegate = new SetClipboardTextDelegate(SetClipboardText));
                io.GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(getClipboardTextDelegate = new GetClipboardTextDelegate(GetClipboardText));
            }

            for (int i = 0; i < (int)ImGuiMouseCursor.COUNT; ++i)
            {
                s_mouseCursorLoaded[i] = false;
            }

            LoadMouseCursor(ImGuiMouseCursor.Arrow, ImGuiMouseCursor.Arrow);
            LoadMouseCursor(ImGuiMouseCursor.TextInput, ImGuiMouseCursor.TextInput);
            LoadMouseCursor(ImGuiMouseCursor.ResizeAll, ImGuiMouseCursor.ResizeAll);
            LoadMouseCursor(ImGuiMouseCursor.ResizeNS, ImGuiMouseCursor.ResizeNS);
            LoadMouseCursor(ImGuiMouseCursor.ResizeEW, ImGuiMouseCursor.ResizeEW);
            LoadMouseCursor(ImGuiMouseCursor.ResizeNESW, ImGuiMouseCursor.ResizeNESW);
            LoadMouseCursor(ImGuiMouseCursor.ResizeNWSE, ImGuiMouseCursor.ResizeNWSE);
            LoadMouseCursor(ImGuiMouseCursor.Hand, ImGuiMouseCursor.Hand);

            if (s_fontTexture != null)
            {
                s_fontTexture = null;
            }
            s_fontTexture = new Texture(1, 1);

            if (loadDefaultFont)
            {
                UpdateFontTexture();
            }

            s_windowHasFocus = window.HasFocus();

            // TODO: Move these to their own functions, and deregister in Shutdown
            window.MouseMoved += (s, e) =>
            {
                if (!window.HasFocus())
                {
                    return;
                }

                s_mouseMoved = true;
            };
            window.MouseButtonReleased += (s, e) =>
            {
                if (!window.HasFocus())
                {
                    return;
                }

                if ((int)e.Button >= 0 && (int)e.Button < 3)
                {
                    s_mousePressed[(int)e.Button] = true;
                }
            };
            window.TouchBegan += (s, e) =>
            {
                if (!window.HasFocus())
                {
                    return;
                }

                s_mouseMoved = false;
                if (e.Finger >= 0 && e.Finger < 3)
                {
                    s_touchDown[e.Finger] = true;
                }
            };
            window.TouchEnded += (s, e) =>
            {
                if (!window.HasFocus())
                {
                    return;
                }

                s_mouseMoved = false;
            };
            window.MouseWheelScrolled += (s, e) =>
            {
                if (!window.HasFocus())
                {
                    return;
                }

                if (e.Wheel == Mouse.Wheel.VerticalWheel || e.Wheel == Mouse.Wheel.HorizontalWheel && io.KeyShift)
                {
                    io.MouseWheel += e.Delta;
                }
                else if (e.Wheel == Mouse.Wheel.HorizontalWheel)
                {
                    io.MouseWheelH += e.Delta;
                }
            };
            window.KeyPressed += (s, e) =>
            {
                if (!window.HasFocus())
                {
                    return;
                }

                io.KeysDown[(int)e.Code] = true;
            };
            window.KeyPressed += (s, e) =>
            {
                if (!window.HasFocus())
                {
                    return;
                }

                io.KeysDown[(int)e.Code] = false;
            };
            window.TextEntered += (s, e) =>
            {
                if (!window.HasFocus())
                {
                    return;
                }

                if (e.Unicode[0] < ' ' || e.Unicode[0] == 127)
                {
                    return;
                }
                io.AddInputCharacter((uint)e.Unicode[0]);
            };
            window.JoystickConnected += (s, e) =>
            {
                if (!window.HasFocus())
                {
                    return;
                }

                if (s_joystickId == NULL_JOYSTICK_ID)
                {
                    s_joystickId = e.JoystickId;
                }
            };
            window.JoystickDisconnected += (s, e) =>
            {
                if (!window.HasFocus())
                {
                    return;
                }

                if (s_joystickId == e.JoystickId)
                {
                    s_joystickId = GetConnectedJoystickId();
                }
            };
            window.LostFocus   += (s, e) => s_windowHasFocus = false;
            window.GainedFocus += (s, e) => s_windowHasFocus = true;
        }
Пример #5
0
        public ImGui_Impl_SDL(IntPtr sdlWindow)
        {
            _sdlWindow = sdlWindow;

            // Setup back-end capabilities flags
            var io = ImGui.GetIO();

            // We can honor GetMouseCursor() values (optional)
            // We can honor io.WantSetMousePos requests (optional, rarely used)
            io.BackendFlags = io.BackendFlags | ImGuiBackendFlags.HasMouseCursors | ImGuiBackendFlags.HasSetMousePos;

            // BackendPlatformName is readonly (and null) in ImGui.NET for some reason, but we can hack it via its internal pointer
            _platformNamePtr = Marshal.StringToHGlobalAnsi("imgui_impl_sdl_c#");
            unsafe
            {
                io.NativePtr->BackendPlatformName = (byte *)_platformNamePtr.ToPointer();
            }

            // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
            io.KeyMap[(int)ImGuiKey.Tab]         = (int)SDL_Scancode.SDL_SCANCODE_TAB;
            io.KeyMap[(int)ImGuiKey.LeftArrow]   = (int)SDL_Scancode.SDL_SCANCODE_LEFT;
            io.KeyMap[(int)ImGuiKey.RightArrow]  = (int)SDL_Scancode.SDL_SCANCODE_RIGHT;
            io.KeyMap[(int)ImGuiKey.UpArrow]     = (int)SDL_Scancode.SDL_SCANCODE_UP;
            io.KeyMap[(int)ImGuiKey.DownArrow]   = (int)SDL_Scancode.SDL_SCANCODE_DOWN;
            io.KeyMap[(int)ImGuiKey.PageUp]      = (int)SDL_Scancode.SDL_SCANCODE_PAGEUP;
            io.KeyMap[(int)ImGuiKey.PageDown]    = (int)SDL_Scancode.SDL_SCANCODE_PAGEDOWN;
            io.KeyMap[(int)ImGuiKey.Home]        = (int)SDL_Scancode.SDL_SCANCODE_HOME;
            io.KeyMap[(int)ImGuiKey.End]         = (int)SDL_Scancode.SDL_SCANCODE_END;
            io.KeyMap[(int)ImGuiKey.Insert]      = (int)SDL_Scancode.SDL_SCANCODE_INSERT;
            io.KeyMap[(int)ImGuiKey.Delete]      = (int)SDL_Scancode.SDL_SCANCODE_DELETE;
            io.KeyMap[(int)ImGuiKey.Backspace]   = (int)SDL_Scancode.SDL_SCANCODE_BACKSPACE;
            io.KeyMap[(int)ImGuiKey.Space]       = (int)SDL_Scancode.SDL_SCANCODE_SPACE;
            io.KeyMap[(int)ImGuiKey.Enter]       = (int)SDL_Scancode.SDL_SCANCODE_RETURN;
            io.KeyMap[(int)ImGuiKey.Escape]      = (int)SDL_Scancode.SDL_SCANCODE_ESCAPE;
            io.KeyMap[(int)ImGuiKey.KeyPadEnter] = (int)SDL_Scancode.SDL_SCANCODE_RETURN2;
            io.KeyMap[(int)ImGuiKey.A]           = (int)SDL_Scancode.SDL_SCANCODE_A;
            io.KeyMap[(int)ImGuiKey.C]           = (int)SDL_Scancode.SDL_SCANCODE_C;
            io.KeyMap[(int)ImGuiKey.V]           = (int)SDL_Scancode.SDL_SCANCODE_V;
            io.KeyMap[(int)ImGuiKey.X]           = (int)SDL_Scancode.SDL_SCANCODE_X;
            io.KeyMap[(int)ImGuiKey.Y]           = (int)SDL_Scancode.SDL_SCANCODE_Y;
            io.KeyMap[(int)ImGuiKey.Z]           = (int)SDL_Scancode.SDL_SCANCODE_Z;

            _setText = new SetClipboardTextDelegate(SetClipboardText);
            _getText = new GetClipboardTextDelegate(GetClipboardText);

            io.SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_setText);
            io.GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_getText);
            io.ClipboardUserData  = IntPtr.Zero;

            _mouseCursors[(int)ImGuiMouseCursor.Arrow]      = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_ARROW);
            _mouseCursors[(int)ImGuiMouseCursor.TextInput]  = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_IBEAM);
            _mouseCursors[(int)ImGuiMouseCursor.ResizeAll]  = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZEALL);
            _mouseCursors[(int)ImGuiMouseCursor.ResizeNS]   = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENS);
            _mouseCursors[(int)ImGuiMouseCursor.ResizeEW]   = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZEWE);
            _mouseCursors[(int)ImGuiMouseCursor.ResizeNESW] = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENESW);
            _mouseCursors[(int)ImGuiMouseCursor.ResizeNWSE] = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENWSE);
            _mouseCursors[(int)ImGuiMouseCursor.Hand]       = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_HAND);
            _mouseCursors[(int)ImGuiMouseCursor.NotAllowed] = SDL_CreateSystemCursor(SDL_SystemCursor.SDL_SYSTEM_CURSOR_NO);

            var sysWmInfo = new SDL_SysWMinfo();

            SDL_GetVersion(out sysWmInfo.version);
            SDL_GetWindowWMInfo(_sdlWindow, ref sysWmInfo);

            // TODO: This does not exist in newer ImGui versions
            //io.ImeWindowHandle = sysWmInfo.info.win.window;
        }